def test_reading_values(self): """ Test whether dsmr_datalogger reads the correct values. """ DataloggerSettings.get_solo() DataloggerSettings.objects.all().update(track_phases=True) self._fake_dsmr_reading() self.assertTrue(DsmrReading.objects.exists()) reading = DsmrReading.objects.get() self.assertEqual(reading.timestamp, datetime(2016, 3, 3, 15, 43, 47, tzinfo=pytz.UTC)) self.assertEqual(reading.electricity_delivered_1, Decimal('1073.079')) self.assertEqual(reading.electricity_returned_1, Decimal('0')) self.assertEqual(reading.electricity_delivered_2, Decimal('1263.199')) self.assertEqual(reading.electricity_returned_2, Decimal('0')) self.assertEqual(reading.electricity_currently_delivered, Decimal('0.143')) self.assertEqual(reading.electricity_currently_returned, Decimal('0')) self.assertIsNone(reading.extra_device_timestamp) self.assertIsNone(reading.extra_device_delivered) self.assertEqual(reading.phase_currently_delivered_l1, Decimal('0.143')) self.assertEqual(reading.phase_currently_delivered_l2, None) self.assertEqual(reading.phase_currently_delivered_l3, None) meter_statistics = MeterStatistics.get_solo() self.assertEqual(meter_statistics.electricity_tariff, 2) self.assertEqual(meter_statistics.power_failure_count, 6) self.assertEqual(meter_statistics.long_power_failure_count, 3) self.assertEqual(meter_statistics.voltage_sag_count_l1, 0) self.assertEqual(meter_statistics.voltage_sag_count_l2, None) self.assertEqual(meter_statistics.voltage_sag_count_l3, None) self.assertEqual(meter_statistics.voltage_swell_count_l1, 0) self.assertEqual(meter_statistics.voltage_swell_count_l2, None) self.assertEqual(meter_statistics.voltage_swell_count_l3, None)
def test_dashboard_xhr_header(self, now_mock): now_mock.return_value = timezone.make_aware( timezone.datetime(2015, 11, 15)) # This makes sure all possible code paths are covered. for current_tariff in (None, 1, 2): if MeterStatistics.objects.exists(): meter_statistics = MeterStatistics.get_solo() meter_statistics.electricity_tariff = current_tariff meter_statistics.save() response = self.client.get( reverse('{}:dashboard-xhr-header'.format(self.namespace))) self.assertEqual(response.status_code, 200, response.content) self.assertEqual(response['Content-Type'], 'application/json') # No response when no data at all. if self.support_data: json_response = json.loads(response.content.decode("utf-8")) self.assertIn('timestamp', json_response) self.assertIn('currently_delivered', json_response) self.assertIn('currently_returned', json_response) # Costs only makes sense when set. if EnergySupplierPrice.objects.exists() and MeterStatistics.objects.exists() \ and current_tariff is not None: self.assertIn('latest_electricity_cost', json_response) self.assertEqual(json_response['latest_electricity_cost'], '0.23' if current_tariff == 1 else '0.46')
def test_reading_values(self): """ Test whether dsmr_datalogger reads the correct values. """ self._fake_dsmr_reading() self.assertTrue(DsmrReading.objects.exists()) reading = DsmrReading.objects.get() self.assertEqual( reading.timestamp, datetime(2016, 2, 10, 19, 30, 34, tzinfo=pytz.UTC) ) self.assertEqual(reading.electricity_delivered_1, Decimal('756.849')) self.assertEqual(reading.electricity_returned_1, Decimal('0')) self.assertEqual(reading.electricity_delivered_2, Decimal('714.405')) self.assertEqual(reading.electricity_returned_2, Decimal('0')) self.assertEqual(reading.electricity_currently_delivered, Decimal('0.111')) self.assertEqual(reading.electricity_currently_returned, Decimal('0')) self.assertEqual( reading.extra_device_timestamp, datetime(2016, 2, 10, 19, 0, 0, tzinfo=pytz.UTC) ) self.assertEqual(reading.extra_device_delivered, Decimal('1197.484')) # Different data source. meter_statistics = MeterStatistics.get_solo() self.assertEqual(meter_statistics.electricity_tariff, 2) self.assertEqual(meter_statistics.power_failure_count, 3) self.assertEqual(meter_statistics.long_power_failure_count, 0) self.assertEqual(meter_statistics.voltage_sag_count_l1, 2) self.assertEqual(meter_statistics.voltage_sag_count_l2, 2) self.assertEqual(meter_statistics.voltage_sag_count_l3, 0) self.assertEqual(meter_statistics.voltage_swell_count_l1, 0) self.assertEqual(meter_statistics.voltage_swell_count_l2, 0) self.assertEqual(meter_statistics.voltage_swell_count_l3, 0)
def test_reading_values(self): """ Test whether dsmr_datalogger reads the correct values. """ self._fake_dsmr_reading() self.assertTrue(DsmrReading.objects.exists()) reading = DsmrReading.objects.get() self.assertEqual( reading.timestamp, datetime(2016, 3, 3, 15, 43, 47, tzinfo=pytz.UTC) ) self.assertEqual(reading.electricity_delivered_1, Decimal('1073.079')) self.assertEqual(reading.electricity_returned_1, Decimal('0')) self.assertEqual(reading.electricity_delivered_2, Decimal('1263.199')) self.assertEqual(reading.electricity_returned_2, Decimal('0')) self.assertEqual(reading.electricity_currently_delivered, Decimal('0.143')) self.assertEqual(reading.electricity_currently_returned, Decimal('0')) self.assertEqual(reading.extra_device_timestamp, None) self.assertEqual(reading.extra_device_delivered, None) meter_statistics = MeterStatistics.get_solo() self.assertEqual(meter_statistics.electricity_tariff, 2) self.assertEqual(meter_statistics.power_failure_count, 6) self.assertEqual(meter_statistics.long_power_failure_count, 3) self.assertEqual(meter_statistics.voltage_sag_count_l1, 0) self.assertEqual(meter_statistics.voltage_sag_count_l2, None) self.assertEqual(meter_statistics.voltage_sag_count_l3, None) self.assertEqual(meter_statistics.voltage_swell_count_l1, 0) self.assertEqual(meter_statistics.voltage_swell_count_l2, None) self.assertEqual(meter_statistics.voltage_swell_count_l3, None)
def test_reading_values(self, now_mock): """ Test whether dsmr_datalogger reads the correct values. """ now_mock.return_value = timezone.make_aware(timezone.datetime(2016, 4, 10, hour=14, minute=30, second=15)) self._fake_dsmr_reading() self.assertTrue(DsmrReading.objects.exists()) reading = DsmrReading.objects.get() self.assertEqual(reading.timestamp, datetime(2016, 4, 10, 12, 30, 15, tzinfo=pytz.UTC)) self.assertEqual(reading.electricity_delivered_1, Decimal("1234.784")) self.assertEqual(reading.electricity_returned_1, Decimal("0")) self.assertEqual(reading.electricity_delivered_2, Decimal("4321.725")) self.assertEqual(reading.electricity_returned_2, Decimal("0.002")) self.assertEqual(reading.electricity_currently_delivered, Decimal("0.36")) self.assertEqual(reading.electricity_currently_returned, Decimal("0")) self.assertEqual(reading.extra_device_timestamp, datetime(2016, 4, 10, 11, 0, 0, tzinfo=pytz.UTC)) self.assertEqual(reading.extra_device_delivered, Decimal("7890.693")) meter_statistics = MeterStatistics.get_solo() self.assertEqual(meter_statistics.electricity_tariff, 1) self.assertEqual(meter_statistics.power_failure_count, None) self.assertEqual(meter_statistics.long_power_failure_count, None) self.assertEqual(meter_statistics.voltage_sag_count_l1, None) self.assertEqual(meter_statistics.voltage_sag_count_l2, None) self.assertEqual(meter_statistics.voltage_sag_count_l3, None) self.assertEqual(meter_statistics.voltage_swell_count_l1, None) self.assertEqual(meter_statistics.voltage_swell_count_l2, None) self.assertEqual(meter_statistics.voltage_swell_count_l3, None)
def test_reading_values(self): """ Test whether dsmr_datalogger reads the correct values. """ self._fake_dsmr_reading() self.assertTrue(DsmrReading.objects.exists()) reading = DsmrReading.objects.get() self.assertEqual( reading.timestamp, datetime(2015, 11, 10, 18, 29, 59, tzinfo=pytz.UTC) ) self.assertEqual(reading.electricity_delivered_1, Decimal('510.747')) self.assertEqual(reading.electricity_returned_1, Decimal('0.123')) self.assertEqual(reading.electricity_delivered_2, Decimal('500.013')) self.assertEqual(reading.electricity_returned_2, Decimal('123.456')) self.assertEqual(reading.electricity_currently_delivered, Decimal('0.192')) self.assertEqual(reading.electricity_currently_returned, Decimal('0.123')) self.assertEqual( reading.extra_device_timestamp, datetime(2015, 11, 10, 18, 0, 0, tzinfo=pytz.UTC) ) self.assertEqual(reading.extra_device_delivered, Decimal('845.206')) # Different data source. meter_statistics = MeterStatistics.get_solo() self.assertEqual(meter_statistics.electricity_tariff, Decimal('1')) self.assertEqual(meter_statistics.power_failure_count, 3) self.assertEqual(meter_statistics.long_power_failure_count, 0) self.assertEqual(meter_statistics.voltage_sag_count_l1, 2) self.assertEqual(meter_statistics.voltage_sag_count_l2, 2) self.assertEqual(meter_statistics.voltage_sag_count_l3, 0) self.assertEqual(meter_statistics.voltage_swell_count_l1, 0) self.assertEqual(meter_statistics.voltage_swell_count_l2, 0) self.assertEqual(meter_statistics.voltage_swell_count_l3, 0)
def test_reading_values(self): """ Test whether dsmr_datalogger reads the correct values. """ self._fake_dsmr_reading() self.assertTrue(DsmrReading.objects.exists()) reading = DsmrReading.objects.get() self.assertEqual( reading.timestamp, datetime(2016, 3, 17, 21, 10, 58, tzinfo=pytz.UTC) ) self.assertEqual(reading.electricity_delivered_1, Decimal('1255.252')) self.assertEqual(reading.electricity_returned_1, Decimal('0')) self.assertEqual(reading.electricity_delivered_2, Decimal('1284.838')) self.assertEqual(reading.electricity_returned_2, Decimal('0')) self.assertEqual(reading.electricity_currently_delivered, Decimal('0.187')) self.assertEqual(reading.electricity_currently_returned, Decimal('0')) self.assertEqual( reading.extra_device_timestamp, datetime(2016, 3, 17, 21, 0, 0, tzinfo=pytz.UTC) ) self.assertEqual(reading.extra_device_delivered, Decimal('1438.997')) # Different data source. meter_statistics = MeterStatistics.get_solo() self.assertEqual(meter_statistics.electricity_tariff, 2) self.assertEqual(meter_statistics.power_failure_count, 8) self.assertEqual(meter_statistics.long_power_failure_count, 0) self.assertEqual(meter_statistics.voltage_sag_count_l1, 0) self.assertIsNone(meter_statistics.voltage_sag_count_l2) self.assertIsNone(meter_statistics.voltage_sag_count_l3) self.assertEqual(meter_statistics.voltage_swell_count_l1, 0) self.assertIsNone(meter_statistics.voltage_swell_count_l2) self.assertIsNone(meter_statistics.voltage_swell_count_l3)
def test_reading_values(self, now_mock): """ Test whether dsmr_datalogger reads the correct values. """ now_mock.return_value = timezone.make_aware(timezone.datetime(2016, 4, 10, hour=14, minute=30, second=15)) self._fake_dsmr_reading() self.assertTrue(DsmrReading.objects.exists()) reading = DsmrReading.objects.get() self.assertEqual( reading.timestamp, datetime(2016, 4, 10, 12, 30, 15, tzinfo=pytz.UTC) ) self.assertEqual(reading.electricity_delivered_1, Decimal('1234.784')) self.assertEqual(reading.electricity_returned_1, Decimal('0')) self.assertEqual(reading.electricity_delivered_2, Decimal('4321.725')) self.assertEqual(reading.electricity_returned_2, Decimal('0.002')) self.assertEqual(reading.electricity_currently_delivered, Decimal('0.36')) self.assertEqual(reading.electricity_currently_returned, Decimal('0')) self.assertEqual( reading.extra_device_timestamp, datetime(2016, 4, 10, 11, 0, 0, tzinfo=pytz.UTC) ) self.assertEqual(reading.extra_device_delivered, Decimal('7890.693')) meter_statistics = MeterStatistics.get_solo() self.assertEqual(meter_statistics.electricity_tariff, 1) self.assertEqual(meter_statistics.power_failure_count, None) self.assertEqual(meter_statistics.long_power_failure_count, None) self.assertEqual(meter_statistics.voltage_sag_count_l1, None) self.assertEqual(meter_statistics.voltage_sag_count_l2, None) self.assertEqual(meter_statistics.voltage_sag_count_l3, None) self.assertEqual(meter_statistics.voltage_swell_count_l1, None) self.assertEqual(meter_statistics.voltage_swell_count_l2, None) self.assertEqual(meter_statistics.voltage_swell_count_l3, None)
def get(self, request): data = {} try: latest_reading = DsmrReading.objects.all().order_by('-pk')[0] except IndexError: # Don't even bother when no data available. return HttpResponse(json.dumps(data), content_type='application/json') data['timestamp'] = naturaltime(latest_reading.timestamp) data['currently_delivered'] = int( latest_reading.electricity_currently_delivered * 1000) data['currently_returned'] = int( latest_reading.electricity_currently_returned * 1000) try: # This WILL fail when we either have no prices at all or conflicting ranges. prices = EnergySupplierPrice.objects.by_date( target_date=timezone.now().date()) except (EnergySupplierPrice.DoesNotExist, EnergySupplierPrice.MultipleObjectsReturned): return HttpResponse(json.dumps(data), content_type='application/json') # We need to current tariff to get the right price. tariff = MeterStatistics.get_solo().electricity_tariff currently_delivered = latest_reading.electricity_currently_delivered cost_per_hour = None tariff_map = { 1: prices.electricity_1_price, 2: prices.electricity_2_price, } try: cost_per_hour = currently_delivered * tariff_map[tariff] except KeyError: pass else: data['latest_electricity_cost'] = formats.number_format( dsmr_consumption.services.round_decimal(cost_per_hour)) return HttpResponse(json.dumps(data), content_type='application/json')
def get_context_data(self, **kwargs): context_data = super(Statistics, self).get_context_data(**kwargs) context_data['capabilities'] = dsmr_backend.services.get_capabilities() try: context_data['latest_reading'] = DsmrReading.objects.all( ).order_by('-pk')[0] except IndexError: pass today = timezone.localtime(timezone.now()).date() context_data['datalogger_settings'] = DataloggerSettings.get_solo() context_data['meter_statistics'] = MeterStatistics.get_solo() try: context_data[ 'energy_prices'] = EnergySupplierPrice.objects.by_date(today) except EnergySupplierPrice.DoesNotExist: pass return context_data
def test_reading_values(self): """ Test whether dsmr_datalogger reads the correct values. """ DataloggerSettings.get_solo() DataloggerSettings.objects.all().update(track_phases=True) self._fake_dsmr_reading() self.assertTrue(DsmrReading.objects.exists()) reading = DsmrReading.objects.get() self.assertEqual(reading.timestamp, datetime(2015, 11, 10, 18, 29, 59, tzinfo=pytz.UTC)) self.assertEqual(reading.electricity_delivered_1, Decimal('510.747')) self.assertEqual(reading.electricity_returned_1, Decimal('0.123')) self.assertEqual(reading.electricity_delivered_2, Decimal('500.013')) self.assertEqual(reading.electricity_returned_2, Decimal('123.456')) self.assertEqual(reading.electricity_currently_delivered, Decimal('0.192')) self.assertEqual(reading.electricity_currently_returned, Decimal('0.123')) self.assertEqual(reading.extra_device_timestamp, datetime(2015, 11, 10, 18, 0, 0, tzinfo=pytz.UTC)) self.assertEqual(reading.extra_device_delivered, Decimal('845.206')) self.assertEqual(reading.phase_currently_delivered_l1, Decimal('0.123')) self.assertEqual(reading.phase_currently_delivered_l2, Decimal('0.456')) self.assertEqual(reading.phase_currently_delivered_l3, Decimal('0.789')) # Different data source. meter_statistics = MeterStatistics.get_solo() self.assertEqual(meter_statistics.electricity_tariff, Decimal('1')) self.assertEqual(meter_statistics.power_failure_count, 3) self.assertEqual(meter_statistics.long_power_failure_count, 0) self.assertEqual(meter_statistics.voltage_sag_count_l1, 2) self.assertEqual(meter_statistics.voltage_sag_count_l2, 2) self.assertEqual(meter_statistics.voltage_sag_count_l3, 0) self.assertEqual(meter_statistics.voltage_swell_count_l1, 0) self.assertEqual(meter_statistics.voltage_swell_count_l2, 0) self.assertEqual(meter_statistics.voltage_swell_count_l3, 0)
def get_context_data(self, **kwargs): context_data = super(Statistics, self).get_context_data(**kwargs) context_data['capabilities'] = dsmr_backend.services.get_capabilities() try: context_data['latest_reading'] = DsmrReading.objects.all().order_by('-pk')[0] except IndexError: pass today = timezone.localtime(timezone.now()).date() context_data['datalogger_settings'] = DataloggerSettings.get_solo() context_data['meter_statistics'] = MeterStatistics.get_solo() try: context_data['energy_prices'] = EnergySupplierPrice.objects.by_date(today) except EnergySupplierPrice.DoesNotExist: pass # Use stats context_data['slumber_consumption_watt'] = dsmr_consumption.services.calculate_slumber_consumption_watt() context_data['min_max_consumption_watt'] = dsmr_consumption.services.calculate_min_max_consumption_watt() return context_data
def test_reading_values(self): """ Test whether dsmr_datalogger reads the correct values. """ DataloggerSettings.get_solo() DataloggerSettings.objects.all().update(track_phases=True) self._fake_dsmr_reading() self.assertTrue(DsmrReading.objects.exists()) reading = DsmrReading.objects.get() self.assertEqual(reading.timestamp, datetime(2016, 3, 17, 21, 10, 58, tzinfo=pytz.UTC)) self.assertEqual(reading.electricity_delivered_1, Decimal('1255.252')) self.assertEqual(reading.electricity_returned_1, Decimal('0')) self.assertEqual(reading.electricity_delivered_2, Decimal('1284.838')) self.assertEqual(reading.electricity_returned_2, Decimal('0')) self.assertEqual(reading.electricity_currently_delivered, Decimal('0.187')) self.assertEqual(reading.electricity_currently_returned, Decimal('0')) self.assertEqual(reading.extra_device_timestamp, datetime(2016, 3, 17, 21, 0, 0, tzinfo=pytz.UTC)) self.assertEqual(reading.extra_device_delivered, Decimal('1438.997')) self.assertEqual(reading.phase_currently_delivered_l1, Decimal('0.187')) self.assertEqual(reading.phase_currently_delivered_l2, None) self.assertEqual(reading.phase_currently_delivered_l3, None) # Different data source. meter_statistics = MeterStatistics.get_solo() self.assertEqual(meter_statistics.electricity_tariff, 2) self.assertEqual(meter_statistics.power_failure_count, 8) self.assertEqual(meter_statistics.long_power_failure_count, 0) self.assertEqual(meter_statistics.voltage_sag_count_l1, 0) self.assertIsNone(meter_statistics.voltage_sag_count_l2) self.assertIsNone(meter_statistics.voltage_sag_count_l3) self.assertEqual(meter_statistics.voltage_swell_count_l1, 0) self.assertIsNone(meter_statistics.voltage_swell_count_l2) self.assertIsNone(meter_statistics.voltage_swell_count_l3)
def test_ordering(self): """ Test whether defaults allow the creation of any empty model. """ MeterStatistics.get_solo() self.assertTrue(MeterStatistics.objects.exists())
def setUp(self): self.instance = MeterStatistics.get_solo()
def test_ordering(self): """ Test whether defaults allow the creation of any empty model. """ MeterStatistics.get_solo() self.assertTrue(MeterStatistics.objects.exists())
def test_track_meter_statistics(self): datalogger_settings = DataloggerSettings.get_solo() datalogger_settings.track_meter_statistics = False datalogger_settings.save() fake_telegram = ''.join([ "/XMX5LGBBFFB123456789\r\n", "\r\n", "1-3:0.2.8(40)\r\n", "0-0:1.0.0(151110192959W)\r\n", "0-0:96.1.1(xxxxxxxxxxxxx)\r\n", "1-0:1.8.1(000510.747*kWh)\r\n", "1-0:2.8.1(000000.123*kWh)\r\n", "1-0:1.8.2(000500.013*kWh)\r\n", "1-0:2.8.2(000123.456*kWh)\r\n", "0-0:96.14.0(0001)\r\n", "1-0:1.7.0(00.192*kW)\r\n", "1-0:2.7.0(00.123*kW)\r\n", "0-0:17.0.0(999.9*kW)\r\n", "0-0:96.3.10(1)\r\n", "0-0:96.7.21(00003)\r\n", "0-0:96.7.9(00000)\r\n", "1-0:99.97.0(0)(0-0:96.7.19)\r\n", "1-0:32.32.0(00002)\r\n", "1-0:52.32.0(00002)\r\n", "1-0:72.32.0(00000)\r\n", "1-0:32.36.0(00000)\r\n", "1-0:52.36.0(00000)\r\n", "1-0:72.36.0(00000)\r\n", "0-0:96.13.1()\r\n", "0-0:96.13.0()\r\n", "1-0:31.7.0(000*A)\r\n", "1-0:51.7.0(000*A)\r\n", "1-0:71.7.0(001*A)\r\n", "1-0:21.7.0(00.000*kW)\r\n", "1-0:41.7.0(00.000*kW)\r\n", "1-0:61.7.0(00.192*kW)\r\n", "1-0:22.7.0(00.000*kW)\r\n", "1-0:42.7.0(00.000*kW)\r\n", "1-0:62.7.0(00.000*kW)\r\n", "0-1:24.1.0(003)\r\n", "0-1:96.1.0(xxxxxxxxxxxxx)\r\n", "0-1:24.2.1(151110190000W)(00845.206*m3)\r\n", "0-1:24.4.0(1)\r\n", "!D19A\n", ]) self.assertIsNone(MeterStatistics.get_solo().electricity_tariff) # Empty model in DB. dsmr_datalogger.services.telegram_to_reading(data=fake_telegram) self.assertIsNone(MeterStatistics.get_solo().electricity_tariff) # Unaffected # Try again, but now with tracking settings enabled. datalogger_settings = DataloggerSettings.get_solo() datalogger_settings.track_meter_statistics = True datalogger_settings.save() self.assertIsNone(MeterStatistics.get_solo().electricity_tariff) # Empty model in DB. dsmr_datalogger.services.telegram_to_reading(data=fake_telegram) # Should be populated now. meter_statistics = MeterStatistics.get_solo() self.assertIsNotNone(meter_statistics.electricity_tariff) self.assertEqual(meter_statistics.electricity_tariff, 1) self.assertEqual(meter_statistics.power_failure_count, 3) self.assertEqual(meter_statistics.voltage_sag_count_l1, 2) self.assertEqual(meter_statistics.voltage_sag_count_l2, 2)