diff options
author | holger <holgercremer@gmail.com> | 2019-11-01 21:55:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-01 21:55:22 +0100 |
commit | 04e0406316320f6039365b80de8c58c3c58413c5 (patch) | |
tree | ce31b694a0f2604ea16a9ae5580426ea666941ba /shop2mqtt/sendStock2Mqtt.py | |
parent | 2714fc190f01894eaaa64bf3869a4a4269ab1dcf (diff) | |
parent | d92aa9d79a3f80557119b660f7191e96b7d79878 (diff) | |
download | serial-barcode-scanner-04e0406316320f6039365b80de8c58c3c58413c5.tar.bz2 |
Merge pull request #45 from smilix/master
mqtt script & pdf generation improvements
Diffstat (limited to 'shop2mqtt/sendStock2Mqtt.py')
-rwxr-xr-x | shop2mqtt/sendStock2Mqtt.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/shop2mqtt/sendStock2Mqtt.py b/shop2mqtt/sendStock2Mqtt.py new file mode 100755 index 0000000..a54675d --- /dev/null +++ b/shop2mqtt/sendStock2Mqtt.py @@ -0,0 +1,39 @@ +#!/usr/bin/python3 +import configparser +import json +import os +import sqlite3 +import ssl + +from paho.mqtt import publish + + +def get_current_stock(shop_config): + db_file = shop_config["shop_db"] + if not os.path.isfile(db_file): + print("Shop db not found: %s" % db_file) + exit(1) + conn = sqlite3.connect(db_file) + c = conn.cursor() + + data = [] + for row in c.execute('SELECT name, amount FROM products WHERE deprecated = 0 AND amount > 0'): + data.append(row) + + return data + + +def send_to_mqtt(mqtt_config, payload): + publish.single(mqtt_config["topic"], payload, + hostname=mqtt_config["host"], port=int(mqtt_config["port"]), + auth={'username': mqtt_config["username"], 'password': mqtt_config["password"]}, + tls={'ca_certs': mqtt_config["host_cert"], 'tls_version': ssl.PROTOCOL_TLS, 'insecure': False}, + client_id="send2Stock2Mqtt") + + +if __name__ == '__main__': + config = configparser.ConfigParser() + config.read("shop2mqtt.conf") + + stock_data = get_current_stock(config["shop"]) + send_to_mqtt(config["mqtt"], json.dumps(stock_data, indent=None, separators=(',', ':'))) |