-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from daniviga/reloaded
Reloaded
- Loading branch information
Showing
14 changed files
with
186 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# -*- coding: utf-8 -*- | ||
# vim: tabstop=4 shiftwidth=4 softtabstop=4 | ||
# | ||
# BITE - A Basic/IoT/Example | ||
# Copyright (C) 2020-2021 Daniele Viganò <[email protected]> | ||
# | ||
# BITE is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# BITE is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import json | ||
import time | ||
from kafka import KafkaConsumer | ||
from kafka.errors import NoBrokersAvailable | ||
|
||
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 | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'MQTT to DB deamon' | ||
|
||
KAFKA_HOST = settings.KAFKA_BROKER['HOST'] | ||
KAFKA_PORT = int(settings.KAFKA_BROKER['PORT']) | ||
|
||
def get_device(self, serial): | ||
try: | ||
return Device.objects.get(serial=serial) | ||
except ObjectDoesNotExist: | ||
return None | ||
|
||
def store_telemetry(self, transport, message): | ||
Telemetry.objects.create( | ||
transport=transport, | ||
device=self.get_device(message["device"]), | ||
clock=message["clock"], | ||
payload=message["payload"] | ||
) | ||
|
||
def handle(self, *args, **options): | ||
while True: | ||
try: | ||
consumer = KafkaConsumer( | ||
"telemetry", | ||
bootstrap_servers='{}:{}'.format( | ||
self.KAFKA_HOST, self.KAFKA_PORT | ||
), | ||
group_id="handler", | ||
value_deserializer=lambda m: json.loads(m.decode('utf8')), | ||
) | ||
break | ||
except NoBrokersAvailable: | ||
self.stdout.write( | ||
self.style.WARNING('WARNING: Kafka broker not available')) | ||
time.sleep(5) | ||
|
||
self.stdout.write(self.style.SUCCESS('INFO: Kafka broker subscribed')) | ||
for message in consumer: | ||
self.store_telemetry( | ||
message.value["transport"], | ||
message.value["body"] | ||
) | ||
consumer.unsuscribe() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ ipython | |
flake8 | ||
pyinstrument | ||
django-debug-toolbar | ||
urllib3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters