mqtt-halt-pack/client/client.py

117 lines
3.8 KiB
Python
Raw Permalink Normal View History

2025-02-21 16:58:18 +01:00
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import traceback
import os
import datetime
2025-02-22 18:35:27 +01:00
import time
2025-02-21 16:58:18 +01:00
monitor_state = 1
2025-02-22 18:35:27 +01:00
FIRST_RECONNECT_DELAY = 1
RECONNECT_RATE = 2
MAX_RECONNECT_COUNT = 12
MAX_RECONNECT_DELAY = 60
2025-02-21 20:00:34 +01:00
def on_connect(client, userdata, flags, reason_code, properties):
2025-02-21 16:58:18 +01:00
now = datetime.datetime.now()
print(str(now) + ": " +"Connected to %s:%s" % (mqttc._host, mqttc._port))
2025-02-22 18:35:27 +01:00
if reason_code == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", reason_code)
2025-02-21 16:58:18 +01:00
2025-02-21 20:00:34 +01:00
def on_disconnect(client, userdata, flags, reason_code, properties):
now = datetime.datetime.now()
print(str(now) + ": disconnected")
2025-02-22 18:35:27 +01:00
print("Disconnected with result code: %s", reason_code)
reconnect_count, reconnect_delay = 0, FIRST_RECONNECT_DELAY
while reconnect_count < MAX_RECONNECT_COUNT:
print("Reconnecting in %d seconds...", reconnect_delay)
time.sleep(reconnect_delay)
try:
client.reconnect()
print("Reconnected successfully!")
return
except Exception as err:
print("%s. Reconnect failed. Retrying...", err)
reconnect_delay *= RECONNECT_RATE
reconnect_delay = min(reconnect_delay, MAX_RECONNECT_DELAY)
reconnect_count += 1
print("Reconnect failed after %s attempts. Exiting...", reconnect_count)
2025-02-21 20:00:34 +01:00
def on_message(client, userdata, message):
2025-02-21 16:58:18 +01:00
global monitor_state
now = datetime.datetime.now()
2025-02-21 20:00:34 +01:00
print(str(now) + ": " + message.topic + " " + str(message.qos) + " " + str(message.payload))
2025-02-21 16:58:18 +01:00
2025-02-21 20:00:34 +01:00
if message.payload.decode('UTF-8') == 'halt':
2025-02-21 16:58:18 +01:00
print('powering off')
try:
publish.single("dom/pimirror-ack", "went-halt", hostname="192.168.31.5")
print("ack sent")
except Exception as e:
print("Exception when sending:" + str(e))
except:
print("Exception when sending:" + traceback.format_exc())
os.system("sudo shutdown now -h")
2025-02-21 20:00:34 +01:00
elif message.payload.decode('UTF-8') == 'display_on':
2025-02-21 16:58:18 +01:00
if monitor_state == 0:
monitor_state = 1
2025-02-21 21:23:29 +01:00
os.system("/home/pi/display_on.sh")
2025-02-21 16:58:18 +01:00
print("display on")
2025-02-21 20:00:34 +01:00
elif message.payload.decode('UTF-8') == 'display_off':
2025-02-21 16:58:18 +01:00
if monitor_state == 1:
monitor_state = 0
2025-02-21 21:23:29 +01:00
os.system("/home/pi/display_off.sh")
2025-02-21 16:58:18 +01:00
print("display off")
2025-02-21 20:00:34 +01:00
def on_publish(client, userdata, mid, reason_codes, properties):
2025-02-21 16:58:18 +01:00
now = datetime.datetime.now()
print(str(now) + ": " +"mid: "+str(mid))
2025-02-21 20:00:34 +01:00
def on_subscribe(client, userdata, mid, reason_codes, properties):
2025-02-21 16:58:18 +01:00
now = datetime.datetime.now()
2025-02-21 20:00:34 +01:00
print(str(now) + ": " +"Subscribed: "+str(mid)+" "+str(reason_codes))
2025-02-21 16:58:18 +01:00
2025-02-21 20:00:34 +01:00
def on_unsubscribe(client, userdata, mid, reason_codes, properties):
now = datetime.datetime.now()
print(str(now) + ": unsubscribe")
2025-02-21 16:58:18 +01:00
2025-02-21 20:00:34 +01:00
# In NEW version, reason_codes is always a list. Empty for MQTTv3
for unsub_result in reason_codes:
# Any reason code >= 128 is a failure.
if reason_codes[0] >= 128:
print("unsubscribe code: " + str(reason_codes[0]))
def on_log(client, obj, level, string):
2025-02-21 16:58:18 +01:00
now = datetime.datetime.now()
print(str(now) + ": " +string)
# If you want to use a specific client id, use
# mqttc = mqtt.Client("client-id")
# but note that the client id must be unique on the broker. Leaving the client
# id parameter empty will generate a random id for you.
2025-02-22 18:35:27 +01:00
client_id = 'mqtt-client-solaria'
2025-02-21 20:00:34 +01:00
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id)
2025-02-21 16:58:18 +01:00
mqttc.on_message = on_message
mqttc.on_connect = on_connect
2025-02-22 15:35:10 +01:00
mqttc.on_disconnect = on_disconnect
2025-02-21 16:58:18 +01:00
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
2025-02-22 15:35:10 +01:00
mqttc.on_unsubscribe = on_unsubscribe
2025-02-21 16:58:18 +01:00
# Uncomment to enable debug messages
mqttc.on_log = on_log
mqttc.connect("192.168.31.5")
mqttc.subscribe("dom/pimirror", 0)
mqttc.loop_forever()