CallbackAPIVersion bump to 2

This commit is contained in:
oskar 2025-02-21 20:00:34 +01:00
parent 158cef121e
commit 6808e1222c
2 changed files with 29 additions and 13 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
venv/**
*.log
.idea/**

View file

@ -8,17 +8,21 @@ import datetime
monitor_state = 1
def on_connect(mqttc, obj, flags, rc):
def on_connect(client, userdata, flags, reason_code, properties):
now = datetime.datetime.now()
print(str(now) + ": " +"Connected to %s:%s" % (mqttc._host, mqttc._port))
def on_message(mqttc, obj, msg):
def on_disconnect(client, userdata, flags, reason_code, properties):
now = datetime.datetime.now()
print(str(now) + ": disconnected")
def on_message(client, userdata, message):
global monitor_state
now = datetime.datetime.now()
print(str(now) + ": " +msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
print(str(now) + ": " + message.topic + " " + str(message.qos) + " " + str(message.payload))
if msg.payload.decode('UTF-8') == 'halt':
if message.payload.decode('UTF-8') == 'halt':
print('powering off')
try:
publish.single("dom/pimirror-ack", "went-halt", hostname="192.168.31.5")
@ -30,28 +34,37 @@ def on_message(mqttc, obj, msg):
os.system("sudo shutdown now -h")
elif msg.payload.decode('UTF-8') == 'display_on':
elif message.payload.decode('UTF-8') == 'display_on':
if monitor_state == 0:
monitor_state = 1
os.system("/home/pi/display_on.sh")
# os.system("/home/pi/display_on.sh")
print("display on")
elif msg.payload.decode('UTF-8') == 'display_off':
elif message.payload.decode('UTF-8') == 'display_off':
if monitor_state == 1:
monitor_state = 0
os.system("/home/pi/display_off.sh")
# os.system("/home/pi/display_off.sh")
print("display off")
def on_publish(mqttc, obj, mid):
def on_publish(client, userdata, mid, reason_codes, properties):
now = datetime.datetime.now()
print(str(now) + ": " +"mid: "+str(mid))
def on_subscribe(mqttc, obj, mid, granted_qos):
def on_subscribe(client, userdata, mid, reason_codes, properties):
now = datetime.datetime.now()
print(str(now) + ": " +"Subscribed: "+str(mid)+" "+str(granted_qos))
print(str(now) + ": " +"Subscribed: "+str(mid)+" "+str(reason_codes))
def on_unsubscribe(client, userdata, mid, reason_codes, properties):
now = datetime.datetime.now()
print(str(now) + ": unsubscribe")
def on_log(mqttc, obj, level, string):
# 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):
now = datetime.datetime.now()
print(str(now) + ": " +string)
@ -60,7 +73,9 @@ def on_log(mqttc, obj, level, string):
# 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.
mqttc = mqtt.Client()
client_id = 'mqtt-client-solaria'
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id)
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish