-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
132 lines (114 loc) · 3.98 KB
/
app.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import sys
from paho.mqtt import client as mqtt
import json
import schedule
import logging
import time
from lib.config import config
from lib import alarm
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(name)s - %(levelname)s - %(message)s",
stream=sys.stdout,
)
logger = logging.getLogger("alarm")
def mqtt_send_alarm_status(client: mqtt.Client):
logger.info("Enviando status da Central para o servidor MQTT...")
alarm_status = None
try:
alarm_status = alarm.request_partial_status()
except Exception as err:
logger.error("Erro ao requisitar o status da Central")
logger.exception(err)
return
try:
payload = json.dumps(alarm_status.dict())
response = client.publish(config.mqtt_topic_alarm_status, payload)
response.wait_for_publish()
if response.is_published():
logger.info(
"Status enviado para o tópico '%s': %s"
% (config.mqtt_topic_alarm_status, payload)
)
except Exception as err:
logger.error("Não foi possível enviar o status para o MQTT")
logger.exception(err)
def mqtt_handle_message(client, userdata, message):
cmd = None
try:
cmd = str(message.payload.decode("utf-8", "ignore"))
except Exception as err:
logger.error("Erro ao codificar mensagem do MQTT.")
logger.exception(err)
return
logger.info(
"Recebida mensagem no tópico '%s': %s" % (config.mqtt_topic_alarm_control, cmd)
)
try:
if cmd == "ACTIVATE_ALARM":
alarm.activate_alarm()
elif cmd == "DEACTIVATE_ALARM":
alarm.deactivate_alarm()
elif cmd == "ACTIVATE_PARTITION_A":
alarm.activate_partition_A()
elif cmd == "DEACTIVATE_PARTITION_A":
alarm.deactivate_partition_A()
elif cmd == "ACTIVATE_PARTITION_B":
alarm.activate_partition_B()
elif cmd == "DEACTIVATE_PARTITION_B":
alarm.deactivate_partition_B()
elif cmd == "ACTIVATE_PARTITION_C":
alarm.activate_partition_C()
elif cmd == "DEACTIVATE_PARTITION_C":
alarm.deactivate_partition_C()
elif cmd == "ACTIVATE_PARTITION_D":
alarm.activate_partition_D()
elif cmd == "DEACTIVATE_PARTITION_D":
alarm.deactivate_partition_D()
except Exception as err:
logger.error("Erro ao executar o comando %s na central." % (cmd))
logger.exception(err)
return
logger.info("Iniciando...")
logger.info("Estabelecendo conexão com o servidor MQTT...")
client = None
def mqtt_on_connect(client, userdata, flags, rc):
if rc == 0:
logger.info("[MQTT] Conectado.")
client.subscribe(config.mqtt_topic_alarm_control)
else:
logger.info("[MQTT] Erro ao se conectar com o servidor.")
def mqtt_on_disconnect(client, userdata, rc):
logger.info("[MQTT] Cliente desconectado.")
try:
logger.info("[MQTT] Tentando reconectar...")
client.connect(
config.mqtt_addr,
port=config.mqtt_port,
)
except:
logger.info("[MQTT] Erro ao se reconectar com o servidor.")
try:
client = mqtt.Client()
client.on_message = mqtt_handle_message
client.on_connect = mqtt_on_connect
client.on_disconnect = mqtt_on_disconnect
client.username_pw_set(config.mqtt_user, password=config.mqtt_passwd)
client.connect(
config.mqtt_addr,
port=config.mqtt_port,
)
client.loop(timeout=1.0, max_packets=1)
if client.is_connected() == False:
raise Exception("Não foi possível se conectar com o servidor MQTT.")
except Exception as err:
logger.error("Erro na conexão com o servidor MQTT.")
logger.exception(err)
sys.exit()
logger.info("Conexão MQTT OK.")
client.loop_start()
mqtt_send_alarm_status(client)
schedule.every(config.send_status_interval).seconds.do(mqtt_send_alarm_status, client)
while True:
schedule.run_pending()
time.sleep(1)