#!/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()