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())
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())
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)
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)
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)
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)
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
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)
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)
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)
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)
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
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)
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)
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)
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
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)
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)
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())
def latest(self, obj): latest = DPMeasurements(obj).latest() if latest: return "{value} ({time} UTC) ".format(value=latest['value'], time=latest['time']) return "-"