1
0
mirror of https://github.com/daniviga/bite.git synced 2025-04-20 14:31:20 +02:00

Do not crash mqtt-to-db if devices isn't proviosioned

This commit is contained in:
Daniele Viganò 2020-06-21 22:51:06 +02:00
parent 45d12b11a8
commit f5ff716ff3
Signed by: dani
GPG Key ID: DB49AFC03C40EE02

View File

@ -8,6 +8,8 @@ from asyncio_mqtt import Client
from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.exceptions import ObjectDoesNotExist
from api.models import Device
from telemetry.models import Telemetry
@ -20,7 +22,10 @@ class Command(BaseCommand):
@sync_to_async
def get_device(self, serial):
try:
return Device.objects.get(serial=serial)
except ObjectDoesNotExist:
return None
@sync_to_async
def store_telemetry(self, device, payload):
@ -39,7 +44,12 @@ class Command(BaseCommand):
async for message in messages:
payload = json.loads(message.payload.decode('utf-8'))
device = await self.get_device(message.topic)
if device is not None:
await self.store_telemetry(device, payload)
else:
self.stdout.write(
self.style.ERROR(
'DEBUG: message discarded'))
def handle(self, *args, **options):
client = mqtt.Client()