summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <mail2xengineering@protonmail.com>2020-09-27 21:12:51 +0200
committerxengineering <mail2xengineering@protonmail.com>2020-09-27 21:12:51 +0200
commit449b490f233371fe28e9089a05982cd5634285d2 (patch)
tree64ccdf7879f0312f477cd381b5f10bd01b9dafbb
parent30551b56cb20d6cd8dbf72277dc829e0041722ed (diff)
downloadiot-barcode-scanner-449b490f233371fe28e9089a05982cd5634285d2.tar
iot-barcode-scanner-449b490f233371fe28e9089a05982cd5634285d2.tar.zst
iot-barcode-scanner-449b490f233371fe28e9089a05982cd5634285d2.zip
Implement MVP
-rw-r--r--.gitignore2
-rw-r--r--README.md1
-rw-r--r--config.json7
-rw-r--r--iot_barcode_scanner/__init__.py5
-rw-r--r--iot_barcode_scanner/config.py23
-rw-r--r--iot_barcode_scanner/mqtt.py27
-rw-r--r--iot_barcode_scanner/static.py6
-rwxr-xr-xmain.py29
8 files changed, 99 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0f4de50
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+first_test.py
+__pycache__
diff --git a/README.md b/README.md
index 6fda3ed..c1f49b8 100644
--- a/README.md
+++ b/README.md
@@ -11,4 +11,3 @@ A service that makes barcode scanners available on the network for IoT use.
- Read MQTT connection details from config file
- Disable scanner as regular input source and bind it only to the service
- Package it for Arch Linux
-
diff --git a/config.json b/config.json
new file mode 100644
index 0000000..3f046a3
--- /dev/null
+++ b/config.json
@@ -0,0 +1,7 @@
+{
+ "mqtt":{
+ "broker":"127.0.0.1",
+ "port":1883,
+ "topic":"eu/xengineering/iot-barcode-scanner"
+ }
+}
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"
diff --git a/main.py b/main.py
new file mode 100755
index 0000000..b50962a
--- /dev/null
+++ b/main.py
@@ -0,0 +1,29 @@
+#!/usr/bin/python3
+
+
+"""Main executable for the Package / Service"""
+
+
+import iot_barcode_scanner as ibs
+
+
+def main():
+ try:
+ config = ibs.get_config()
+ mqtt_service = ibs.MqttService(config)
+ mqtt_service.run()
+ print("Starting Service")
+ while True:
+ text = input()
+ mqtt_service.client.publish(
+ config["mqtt"]["topic"],
+ payload=text,
+ qos=0,
+ retain=False
+ )
+ except KeyboardInterrupt:
+ print("Service stopped")
+
+
+if __name__ == "__main__":
+ main()