summaryrefslogtreecommitdiffstats
path: root/arafetch/ara2mqtt.py
blob: 0303a5bc136feec2f9416e92a83051d6aab82adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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()