Exemplo n.º 1
0
    def get(self, unique_id, unit, channel, past_seconds):
        """
        Return a list of measurements found within a duration from the past to the present
        """
        if not utils_general.user_has_permission('view_settings'):
            abort(403)

        if unit not in add_custom_units(Unit.query.all()):
            abort(422, custom='Unit ID not found')
        if channel < 0:
            abort(422, custom='channel must be >= 0')
        if past_seconds < 1:
            abort(422, custom='past_seconds must be >= 1')

        try:
            return_ = read_influxdb_list(unique_id,
                                         unit,
                                         channel,
                                         duration_sec=past_seconds)
            if return_ and len(return_) > 0:
                dict_return = {'measurements': []}
                for each_set in return_:
                    dict_return['measurements'].append({
                        'time': each_set[0],
                        'value': each_set[1]
                    })
                return dict_return, 200
            else:
                return return_, 200
        except Exception:
            abort(500,
                  message='An exception occurred',
                  error=traceback.format_exc())
Exemplo n.º 2
0
    def get(self, unique_id, unit, channel, epoch_start, epoch_end):
        """
        Return a list of measurements found within a time range
        """
        if not utils_general.user_has_permission('view_settings'):
            abort(403)

        if unit not in add_custom_units(Unit.query.all()):
            abort(422, custom='Unit ID not found')
        if channel < 0:
            abort(422, custom='channel must be >= 0')
        if epoch_start < 0 or epoch_end < 0:
            abort(422, custom='epoch_start and epoch_end must be >= 0')

        utc_offset_timedelta = datetime.datetime.utcnow(
        ) - datetime.datetime.now()

        if epoch_start:
            start = datetime.datetime.fromtimestamp(float(epoch_start))
            start += utc_offset_timedelta
            start_str = start.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
        else:
            start_str = None

        if epoch_end:
            end = datetime.datetime.fromtimestamp(float(epoch_end))
            end += utc_offset_timedelta
            end_str = end.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
        else:
            end_str = None

        try:
            return_ = read_influxdb_list(unique_id,
                                         unit,
                                         channel,
                                         start_str=start_str,
                                         end_str=end_str)
            if return_ and len(return_) > 0:
                dict_return = {'measurements': []}
                for each_set in return_:
                    dict_return['measurements'].append({
                        'time': each_set[0],
                        'value': each_set[1]
                    })
                return dict_return, 200
            else:
                return return_, 200
        except Exception:
            abort(500,
                  message='An exception occurred',
                  error=traceback.format_exc())