diff options
Diffstat (limited to 'iot_barcode_scanner')
-rw-r--r-- | iot_barcode_scanner/__init__.py | 5 | ||||
-rw-r--r-- | iot_barcode_scanner/config.py | 23 | ||||
-rw-r--r-- | iot_barcode_scanner/mqtt.py | 27 | ||||
-rw-r--r-- | iot_barcode_scanner/static.py | 6 |
4 files changed, 61 insertions, 0 deletions
diff --git a/iot_barcode_scanner/__init__.py b/iot_barcode_scanner/__init__.py new file mode 100644 index 0000000..6f92d9b --- /dev/null +++ b/iot_barcode_scanner/__init__.py @@ -0,0 +1,5 @@ + + +from iot_barcode_scanner.config import * +from iot_barcode_scanner.mqtt import * +from iot_barcode_scanner.static import * diff --git a/iot_barcode_scanner/config.py b/iot_barcode_scanner/config.py new file mode 100644 index 0000000..6b83393 --- /dev/null +++ b/iot_barcode_scanner/config.py @@ -0,0 +1,23 @@ + + +"""Module to read the Configuration File for the Service""" + + +import sys +import json +from iot_barcode_scanner.static import CONFIG_PATH + + +def get_config(): + """Read Config File and return it as Python Object""" + + try: + with open(CONFIG_PATH, "r") as config_file: + text = config_file.read() + except FileNotFoundError: + print("Config file does not exist.") + sys.exit(1) + + data = json.loads(text) + + return data diff --git a/iot_barcode_scanner/mqtt.py b/iot_barcode_scanner/mqtt.py new file mode 100644 index 0000000..c5a075c --- /dev/null +++ b/iot_barcode_scanner/mqtt.py @@ -0,0 +1,27 @@ + + +"""Provide MQTT Functionality for the Package""" + + +import paho.mqtt.client as mqtt + + +class MqttService(): + """Bundle for MQTT Functionality""" + + def __init__(self, config): + self.config = config + self.client = mqtt.Client() + self.client.on_connect = self.on_connect + + def run(self): + self.client.connect( + self.config["mqtt"]["broker"], + self.config["mqtt"]["port"], + 60 + ) + self.client.loop_start() + + def on_connect(self, client, userdata, flags, rc): + print("Connected with result code " + str(rc)) + client.subscribe(self.config["mqtt"]["topic"]) diff --git a/iot_barcode_scanner/static.py b/iot_barcode_scanner/static.py new file mode 100644 index 0000000..22b3842 --- /dev/null +++ b/iot_barcode_scanner/static.py @@ -0,0 +1,6 @@ + + +"""Module to store static Data for the whole Package""" + + +CONFIG_PATH = "config.json" |