예제 #1
0
    def test_simple_DPMeasurements_exists(self):
        m = DPMeasurements(device_parameter=type('test_device_param', (object,),
                                                 {'id': 1, 'type': MagicMock(), 'device': MagicMock()})())

        m.add(time=timezone.now(), value=EmptyDBTestCase.v)
        time.sleep(3)
        self.assertTrue(m.exists())
예제 #2
0
 def test_simple_DPMeasurements_exists_not(self):
     m = DPMeasurements(
         device_parameter=type('test_device_param', (object, ), {
             'id': 1,
             'type': MagicMock(),
             'device': MagicMock()
         })())
     self.assertTrue(not m.exists())
예제 #3
0
 def test_simple_add(self):
     m = DPMeasurements(device_parameter=DeviceParameter.objects.first())
     before = len(list(m.all()))
     m.add(time=timezone.now(), value=255)
     m.add(time=timezone.now(), value=0)
     m.add(time=timezone.now(), value=20.5)
     time.sleep(1.5)
     after = len(list(m.all()))
     self.assertTrue(before + 3 == after)
예제 #4
0
    def test_simple_DPMeasurements_add(self):
        t = DeviceParameterType(code="TEST")
        m = DPMeasurements(device_parameter=type('test_device_param', (object,),
                                                 {'id': 1, 'type': t, 'device': MagicMock()})())

        m.add(time=timezone.now(), value=EmptyDBTestCase.v)
        time.sleep(2.5)
        measurements = list(m.all())
        self.assertTrue(measurements[0]['value'] == EmptyDBTestCase.v)
예제 #5
0
    def test_simple_DPMeasurements_count(self):
        m = DPMeasurements(
            device_parameter=type('test_device_param', (object, ), {
                'id': 1,
                'type': MagicMock(),
                'device': MagicMock()
            })())

        m.add(time=timezone.now(), value=EmptyDBTestCase.v)

        self.assertTrue(m.count() == 1)
예제 #6
0
    def test_DPMeasurements_exists_notify_listeners(self, mock):
        """
        Trigger "latest value changed signal" while patching out the signal receiver
        """
        m = DPMeasurements(device_parameter=type('test_device_param', (object,),
                                                 {'id': 1, 'type': MagicMock(), 'device': MagicMock()})())

        m.add(time=timezone.now(), value=EmptyDBTestCase.v)
        time.sleep(2.5)
        m.add(time=timezone.now(), value=EmptyDBTestCase.v + 1)
        self.assertEqual(mock.call_count, 1)
예제 #7
0
    def test_parse_latest_TS(self):
        m = DPMeasurements(device_parameter=type('test_device_param', (object,),
                                                 {'id': 1, 'type': MagicMock(), 'device': MagicMock()})())

        m.add(time=timezone.now(), value=EmptyDBTestCase.v + 1)
        time.sleep(2.5)

        latest = m.latest()
        ts = parse_datetime(latest['time'])

        now = timezone.now()
        assert ts < now
예제 #8
0
 def test_simple_add(self):
     m = DPMeasurements(device_parameter=DeviceParameter.objects.first())
     before = len(list(m.all()))
     m.add(time=timezone.now(), value=255)
     m.add(time=timezone.now(), value=0)
     m.add(time=timezone.now(), value=20.5)
     time.sleep(1.5)
     after = len(list(m.all()))
     self.assertTrue(before + 3 == after)
예제 #9
0
    def test_simple_DPMeasurements_add(self):
        t = DeviceParameterType(code="TEST")
        m = DPMeasurements(
            device_parameter=type('test_device_param', (object, ), {
                'id': 1,
                'type': t,
                'device': MagicMock()
            })())

        m.add(time=timezone.now(), value=EmptyDBTestCase.v)
        time.sleep(2.5)
        measurements = list(m.all())
        self.assertTrue(measurements[0]['value'] == EmptyDBTestCase.v)
예제 #10
0
    def test_DPMeasurements_notify_listeners(self, receiver):
        """
        Trigger "latest value changed signal" and call the signal receiver
        """

        m = DPMeasurements(device_parameter=type('test_device_param', (object,),
                                                 {'id': 1, 'type': MagicMock(), 'device': MagicMock()})())

        m.add(time=timezone.now(), value=EmptyDBTestCase.v)
        time.sleep(2.5)
        m.add(time=timezone.now(), value=EmptyDBTestCase.v + 1)
        self.assertEqual(receiver.call_count, 1)
        self.assertEqual(receiver.call_args[1]['exchange'], settings.IODICUS_MESSAGING_EXCHANGE_NAME)
예제 #11
0
    def test_simple_add(self):
        print(settings.INFLUXDB_HOST)
        m = DPMeasurements(device_parameter=DeviceParameter.objects.first())
        before = len(list(m.all()))
        print(before)
        m.add(time=timezone.now(), value=255)
        m.add(time=timezone.now(), value=0)
        m.add(time=timezone.now(), value=20.5)
        time.sleep(5)
        after = len(list(m.all()))
        print(after)

        self.assertTrue(before + 3 == after)
예제 #12
0
    def test_simple_DPMeasurements_all_start_date(self):
        m = DPMeasurements(device_parameter=type('test_device_param', (object,),
                                                 {'id': 1, 'type': MagicMock(), 'device': MagicMock()})())

        now = timezone.now()
        for i in range(0, 60):
            m.add(time=now - datetime.timedelta(seconds=i), value=EmptyDBTestCase.v)
        time.sleep(2.5)
        self.assertTrue(m.count() == 60)

        res = m.all(start_date=now - datetime.timedelta(seconds=30))
        size = len(list(res))
        # print(size)
        self.assertTrue(abs(size - 30) < 2)
예제 #13
0
    def test_parse_latest_TS(self):
        m = DPMeasurements(
            device_parameter=type('test_device_param', (object, ), {
                'id': 1,
                'type': MagicMock(),
                'device': MagicMock()
            })())

        m.add(time=timezone.now(), value=EmptyDBTestCase.v + 1)
        time.sleep(2.5)

        latest = m.latest()
        ts = parse_datetime(latest['time'])

        now = timezone.now()
        assert ts < now
예제 #14
0
    def add_sensor_device(type_code,
                          type_description,
                          sensor_type,
                          sensor_device,
                          value=None):
        sensor_device_type, created = DeviceParameterType.objects.get_or_create(
            code=type_code, description=type_description)
        if created:
            logger.debug(
                'Created DeviceParameterType: {}'.format(sensor_device_type))

        device_param, created = DeviceParameter.objects.get_or_create(
            device=sensor_device,
            type=sensor_device_type,
            unit=sensor_type['unit'])
        if created:
            logger.debug('Created DeviceParameter: {}'.format(device_param))

        if value is not None:
            DPMeasurements(device_param).add(DPMeasurements.time(), value)
예제 #15
0
    def add_sensor_device(type_code, type_description, sensor_type, sensor_device, value=None):
        sensor_device_type, created = DeviceParameterType.objects.get_or_create(
            code=type_code,
            description=type_description
        )
        if created:
            logger.debug('Created DeviceParameterType: {}'.format(sensor_device_type))

        device_param, created = DeviceParameter.objects.get_or_create(
            device=sensor_device,
            type=sensor_device_type,
            unit=sensor_type['unit']
        )
        if created:
            logger.debug('Created DeviceParameter: {}'.format(device_param))

        if value is not None:
            DPMeasurements(device_param).add(DPMeasurements.time(), value)
예제 #16
0
    def test_DPMeasurements_exists_notify_listeners(self, mock):
        """
        Trigger "latest value changed signal" while patching out the signal receiver
        """
        m = DPMeasurements(
            device_parameter=type('test_device_param', (object, ), {
                'id': 1,
                'type': MagicMock(),
                'device': MagicMock()
            })())

        m.add(time=timezone.now(), value=EmptyDBTestCase.v)
        time.sleep(2.5)
        m.add(time=timezone.now(), value=EmptyDBTestCase.v + 1)
        self.assertEqual(mock.call_count, 1)
예제 #17
0
    def retrieve_sensor_readings(self, device, sensors, start, end):
        """
        Retrieve archival data for a specified list of sensors from a single device, storing the readings
        in the time series database

        An example reading is the energy generated in kwh

        :param device: a Node object describing a Rayleigh Device
        :param sensors: a list of Devices describing Rayleigh sensors
        :param start: a timestamp for the beginning of the required data, in ms from Jan. 1st 1970
        :param end: a timestamp for the end of the required data, in ms from Jan. 1st 1970
        """
        device_id = device.external_id
        path = 'data/' + device_id + ':(' + make_csv(sensors) + ')'
        extra = '&from=' + start + '&to=' + end

        res, req = self.consumer_api_call(path=path, extra=extra)
        if res is None:
            logger.warn('Something went wrong, aborting')
            return

        sensors = res[device_id]
        for sensor_id in sensors.keys():
            readings = sensors[sensor_id]

            sensor_devices = Device.objects.filter(node=device,
                                                   external_id=sensor_id)
            if len(sensor_devices) == 0:
                continue

            sensor_device = sensor_devices.get()
            for reading in readings:
                time = reading[0]
                value = reading[1]
                logger.debug(
                    'Adding sensor reading for {sensor}: {time} {value}'.
                    format(sensor=sensor_id, time=time, value=value))
                DPMeasurements(sensor_device).add(time, value)

        return sensors
예제 #18
0
    def test_DPMeasurements_notify_listeners(self, receiver):
        """
        Trigger "latest value changed signal" and call the signal receiver
        """

        m = DPMeasurements(
            device_parameter=type('test_device_param', (object, ), {
                'id': 1,
                'type': MagicMock(),
                'device': MagicMock()
            })())

        m.add(time=timezone.now(), value=EmptyDBTestCase.v)
        time.sleep(2.5)
        m.add(time=timezone.now(), value=EmptyDBTestCase.v + 1)
        self.assertEqual(receiver.call_count, 1)
        self.assertEqual(receiver.call_args[1]['exchange'],
                         settings.IODICUS_MESSAGING_EXCHANGE_NAME)
예제 #19
0
    def test_simple_DPMeasurements_all_start_date(self):
        m = DPMeasurements(
            device_parameter=type('test_device_param', (object, ), {
                'id': 1,
                'type': MagicMock(),
                'device': MagicMock()
            })())

        now = timezone.now()
        for i in range(0, 60):
            m.add(time=now - datetime.timedelta(seconds=i),
                  value=EmptyDBTestCase.v)
        time.sleep(2.5)
        self.assertTrue(m.count() == 60)

        res = m.all(start_date=now - datetime.timedelta(seconds=30))
        size = len(list(res))
        # print(size)
        self.assertTrue(abs(size - 30) < 2)
예제 #20
0
 def test_simple_DPMeasurements_exists_not(self):
     m = DPMeasurements(device_parameter=type('test_device_param', (object,),
                                              {'id': 1, 'type': MagicMock(), 'device': MagicMock()})())
     self.assertTrue(not m.exists())
예제 #21
0
 def latest(self, obj):
     latest = DPMeasurements(obj).latest()
     if latest:
         return "{value} ({time} UTC) ".format(value=latest['value'], time=latest['time'])
     return "-"