def post(self, request): api_settings = APISettings.get_solo() # API disabled. if not api_settings.allow: return HttpResponseNotAllowed(permitted_methods=['POST'], content=_('API is disabled')) # Auth key mismatch. if request.META.get('HTTP_X_AUTHKEY') != api_settings.auth_key: return HttpResponseForbidden(content=_('Invalid auth key')) post_form = DsmrReadingForm(request.POST) # Data omitted. if not post_form.is_valid(): return HttpResponseBadRequest(_('Invalid data')) try: dsmr_reading = dsmr_datalogger.services.telegram_to_reading(data=post_form.cleaned_data['telegram']) except: dsmr_reading = None # Data invalid. if not dsmr_reading: return HttpResponseServerError(content=_('Failed to parse telegram')) return HttpResponse()
def post(self, request): api_settings = APISettings.get_solo() if not api_settings.allow: return HttpResponseNotAllowed(permitted_methods=['POST'], content='API is disabled') if request.META.get('HTTP_X_AUTHKEY') != api_settings.auth_key \ and request.META.get('HTTP_AUTHORIZATION') != 'Token {}'.format(api_settings.auth_key): return HttpResponseForbidden(content='Invalid auth key') post_form = DsmrReadingForm(request.POST) if not post_form.is_valid(): logger.warning('API validation failed with POST data: {}'.format( request.POST)) return HttpResponseBadRequest('Invalid data') dsmr_reading = None try: dsmr_reading = dsmr_datalogger.services.telegram_to_reading( data=post_form.cleaned_data['telegram']) except InvalidTelegramError: # The service called already logs the error. pass if not dsmr_reading: return HttpResponseServerError(content='Failed to parse telegram') return HttpResponse(status=201)
def setUp(self): self.client = Client() self._api_settings = APISettings.get_solo() self._api_settings.allow = True self._api_settings.save() # Since we're only having one call anyway. self._api_url = reverse('{}:datalogger-dsmrreading'.format(self.namespace)) self._telegram = ''.join([ "/KFM5KAIFA-METER\r\n", "\r\n", "1-3:0.2.8(42)\r\n", "0-0:1.0.0(160303164347W)\r\n", "0-0:96.1.1(*******************************)\r\n", "1-0:1.8.1(001073.079*kWh)\r\n", "1-0:1.8.2(001263.199*kWh)\r\n", "1-0:2.8.1(000000.000*kWh)\r\n", "1-0:2.8.2(000000.000*kWh)\r\n", "0-0:96.14.0(0002)\r\n", "1-0:1.7.0(00.143*kW)\r\n", "1-0:2.7.0(00.000*kW)\r\n", "0-0:96.7.21(00006)\r\n", "0-0:96.7.9(00003)\r\n", "1-0:99.97.0(1)(0-0:96.7.19)(000101000001W)(2147483647*s)\r\n", "1-0:32.32.0(00000)\r\n", "1-0:32.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:21.7.0(00.143*kW)\r\n", "1-0:22.7.0(00.000*kW)\r\n", "!74B0\n", ])
def get_context_data(self, **kwargs): context_data = super(Configuration, self).get_context_data(**kwargs) context_data['api_settings'] = APISettings.get_solo() context_data['consumption_settings'] = ConsumptionSettings.get_solo() context_data['datalogger_settings'] = DataloggerSettings.get_solo() context_data['frontend_settings'] = FrontendSettings.get_solo() context_data['weather_settings'] = WeatherSettings.get_solo() context_data['backup_settings'] = BackupSettings.get_solo() context_data['dropbox_settings'] = DropboxSettings.get_solo() context_data['mindergas_settings'] = MinderGasSettings.get_solo() return context_data
def handle(self, **options): if not settings.DEBUG: raise CommandError('Only meant for development environment!') self.API_KEY = APISettings.get_solo().auth_key self._test_dsmrreading() self._test_electricity_consumption() self._test_gas_consumption() self._test_day_statistics() self._test_hour_statistics()
def authenticate(self, request): api_settings = APISettings.get_solo() if not api_settings.allow: raise exceptions.PermissionDenied('API is disabled') if request.META.get('HTTP_X_AUTHKEY') != api_settings.auth_key \ and request.META.get('HTTP_AUTHORIZATION') != 'Token {}'.format(api_settings.auth_key): raise exceptions.AuthenticationFailed('Invalid auth key') try: user = User.objects.get(username=settings.DSMRREADER_REST_FRAMEWORK_API_USER) except User.DoesNotExist: raise exceptions.APIException('API user not found') return (user, None)
def get_context_data(self, **kwargs): context_data = super(Configuration, self).get_context_data(**kwargs) # 20+ queries, we should cache this at some point. context_data.update( dict( api_settings=APISettings.get_solo(), backend_settings=BackendSettings.get_solo(), backup_settings=BackupSettings.get_solo(), consumption_settings=ConsumptionSettings.get_solo(), datalogger_settings=DataloggerSettings.get_solo(), dropbox_settings=DropboxSettings.get_solo(), email_settings=EmailSettings.get_solo(), frontend_settings=FrontendSettings.get_solo(), mindergas_settings=MinderGasSettings.get_solo(), mqtt_broker_settings=MQTTBrokerSettings.get_solo(), mqtt_jsondaytotals_settings=JSONDayTotalsMQTTSettings.get_solo( ), mqtt_splittopicdaytotals_settings= SplitTopicDayTotalsMQTTSettings.get_solo(), mqtt_jsoncurrentperiodtotals_settings= JSONCurrentPeriodTotalsMQTTSettings.get_solo(), mqtt_splittopiccurrentperiodtotals_settings= SplitTopicCurrentPeriodTotalsMQTTSettings.get_solo(), mqtt_jsongasconsumption_settings=JSONGasConsumptionMQTTSettings .get_solo(), mqtt_splittopicgasconsumption_settings= SplitTopicGasConsumptionMQTTSettings.get_solo(), mqtt_splittopicmeterstatistics_settings= SplitTopicMeterStatisticsMQTTSettings.get_solo(), mqtt_jsontelegram_settings=JSONTelegramMQTTSettings.get_solo(), mqtt_rawtelegram_settings=RawTelegramMQTTSettings.get_solo(), mqtt_splittopictelegram_settings=SplitTopicTelegramMQTTSettings .get_solo(), notification_settings=NotificationSetting.get_solo(), pvoutput_api_settings=PVOutputAPISettings.get_solo(), pvoutput_addstatus_settings=PVOutputAddStatusSettings.get_solo( ), retention_settings=RetentionSettings.get_solo(), weather_settings=WeatherSettings.get_solo(), influxdb_settings=InfluxdbIntegrationSettings.get_solo(), )) return context_data
def setUp(self): self.client = Client() self._api_settings = APISettings.get_solo() self._api_settings.allow = True self._api_settings.save() # Since we're only having one call anyway. self._api_url = reverse("{}:datalogger-dsmrreading".format(self.namespace)) self._telegram = "".join( [ "/KFM5KAIFA-METER\r\n", "\r\n", "1-3:0.2.8(42)\r\n", "0-0:1.0.0(160303164347W)\r\n", "0-0:96.1.1(*******************************)\r\n", "1-0:1.8.1(001073.079*kWh)\r\n", "1-0:1.8.2(001263.199*kWh)\r\n", "1-0:2.8.1(000000.000*kWh)\r\n", "1-0:2.8.2(000000.000*kWh)\r\n", "0-0:96.14.0(0002)\r\n", "1-0:1.7.0(00.143*kW)\r\n", "1-0:2.7.0(00.000*kW)\r\n", "0-0:96.7.21(00006)\r\n", "0-0:96.7.9(00003)\r\n", "1-0:99.97.0(1)(0-0:96.7.19)(000101000001W)(2147483647*s)\r\n", "1-0:32.32.0(00000)\r\n", "1-0:32.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:21.7.0(00.143*kW)\r\n", "1-0:22.7.0(00.000*kW)\r\n", "!74B0\n", ] )
def setUp(self): self.client = Client() self.api_settings = APISettings.get_solo() self.api_settings.allow = True self.api_settings.save()