From cce239447f2341648a0db19f665c88d3801304ac Mon Sep 17 00:00:00 2001 From: makefu Date: Wed, 4 Sep 2019 15:25:28 +0200 Subject: ara2mqtt: init --- arafetch/ara2mqtt.py | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ default.nix | 1 + setup.py | 3 +- 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 arafetch/ara2mqtt.py diff --git a/arafetch/ara2mqtt.py b/arafetch/ara2mqtt.py new file mode 100644 index 0000000..0303a5b --- /dev/null +++ b/arafetch/ara2mqtt.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python +""" usage: ara2influx [options] DB + +Options: + --db FILE path to db [Default: db.json] + --cantine CANTINE name of the cantine [Default: thales-deutschland] + --host HOST hostname or ip of influx [Default: localhost] +""" +import sys +from docopt import docopt +import logging + + + +import json +from datetime import datetime +logging.basicConfig(level=logging.DEBUG) +log = logging.getLogger() + + +def db2points(db,cantine): + for day,things in db.items(): + time = datetime.strptime(day,"%Y-%m-%d") + time = time.replace(hour=12) + for thing in things: + ret = { + "time": time.isoformat(), + "measurement":"dish", + "tags": { + "allergies": ",".join( sorted(thing['allergies'].keys()) ), + "additives": ",".join( sorted(thing['additives'].keys()) ), + "title": thing["title"], + "type": thing["type"], + "description": thing["description"], + "cantine": cantine + }, + "fields": { + "price": thing["price"] + } + } + log.debug(ret) + yield ret + +def main(): + args = docopt(__doc__) + dbname = args['DB'] + host = args['--host'] + cantine = args['--cantine'] + import paho.mqtt.client as mqtt + mqttc = mqtt.Client() + log.info(f"connecting to mqtt {host}") + # mqttc.loop_start() + def on_connect(client, userdata, flags, rc): + print("Connected with result code "+str(rc)) + mqttc.on_connect = on_connect + mqttc.connect(host, 1883, 60) + from time import sleep + with open(dbname,'r+') as f: + from datetime import date + db = json.load(f) + + for day,things in db.items(): + today = str(date.today()) + + # only update today + if day != today: continue + else: + print(f"today {today} is the day") + + pommes = False + if things: + ret = mqttc.publish(f"/aramark/{cantine}/update",payload=today,retain=True) + + for thing in things: + title = thing['title'] + description = thing['description'] + price = thing['price'] + # print(f'{title} - {description} - {price}') + print(f"/aramark/{cantine}/{thing['type']}/title") + ret = (mqttc.publish(f"/aramark/{cantine}/{thing['type']}/title",payload=title,retain=True)) + mqttc.publish(f"/aramark/{cantine}/{thing['type']}/description",payload=description,retain=True) + mqttc.publish(f"/aramark/{cantine}/{thing['type']}/price",payload=price,retain=True) + if 'Pommes' in description or 'Wedges' in description: + log.debug(f"Yay Pommes in '{title}' - '{description}'!") + pommes = True + + mqttc.publish(f"/aramark/{cantine}/pommes",pommes,retain=True) + mqttc.loop() + +if __name__ == '__main__': + main() diff --git a/default.nix b/default.nix index 556e658..cd69bfb 100644 --- a/default.nix +++ b/default.nix @@ -7,6 +7,7 @@ let docopt influxdb beautifulsoup4 + paho-mqtt pkgs.git pkgs.wget ]; diff --git a/setup.py b/setup.py index 0b6a0d4..f94012c 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,8 @@ setup( entry_points = { 'console_scripts': [ 'arafetch=arafetch.arafetch:main', - 'ara2influx=arafetch.ara2influx:main' + 'ara2influx=arafetch.ara2influx:main', + 'ara2mqtt=arafetch.ara2mqtt:main' ], }, -- cgit v1.2.3