-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
42 lines (31 loc) · 916 Bytes
/
client.py
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
import paho.mqtt.client as mqtt
import random
import os
from time import sleep
from dotenv import load_dotenv
from garagedoor import GarageDoor
from gd_io import IO
from mqttObserver import MqttObserver
load_dotenv(override=True)
def on_message(client, userdata, message):
requested_state = str(message.payload.decode("utf-8"))
if requested_state == 'open':
gd.request_open()
if requested_state == 'closed':
gd.request_closed()
if requested_state == 'toggle':
gd.toggle()
def on_connect(client, userdata, flags, rc):
mqttc.subscribe(os.getenv("COMMAND_TOPIC"))
mqttc = mqtt.Client("MQTT_Client")
mqttc.on_connect=on_connect
mqttc.on_message=on_message
mqttc.connect(os.getenv("MQ_BROKER"))
mqttc.loop_start()
gd = GarageDoor(IO())
gd.addObserver(MqttObserver(mqttc, os.getenv("STATE_TOPIC")))
def main():
epic = True
while epic:
sleep(1)
main()