aboutsummaryrefslogtreecommitdiff
path: root/bigclown-leds
blob: fdba7e47d31d530931059c9417eda0607b521dbb (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
#!/usr/bin/env python3
import configparser
import sys
import time

import paho.mqtt.client as mqtt


def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))
    client.subscribe("bigclown/node/lcd-thermostat:0/led/#")


# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    ptopic = msg.topic.split("/")
    topic = "/".join(ptopic[ptopic.index("led") + 1 :])
    if topic == "effect/set":
        # Well for now just this way
        client.publish(
            "bigclown/node/power-controller:0/led-strip/-/effect/set",
            '{"type": "rainbow", "wait":50}',
        )
    else:
        client.publish(
            "bigclown/node/power-controller:0/led-strip/-/" + topic, payload=msg.payload
        )


# def on_log(mqttc, obj, level, string):
#    print(string)


def main():
    cnf = configparser.ConfigParser()
    cnf.read(sys.argv[1])
    cnf_mqtt = cnf["mqtt"]

    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message
    # client.on_log = on_log

    client.username_pw_set(cnf_mqtt["username"], cnf_mqtt["password"])
    while True:
        try:
            client.connect("127.0.0.1", 1883, 60)
            break
        except ConnectionRefusedError:
            time.sleep(1)
    client.loop_forever()


if __name__ == "__main__":
    main()