From 43ba1bc18acde05bed3fb919ec19579d343a3d8d Mon Sep 17 00:00:00 2001 From: arofarn Date: Wed, 22 Apr 2020 23:20:10 +0200 Subject: [PATCH] Add control over MQTT --- code/main.py | 55 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/code/main.py b/code/main.py index 982d414..c97c33c 100644 --- a/code/main.py +++ b/code/main.py @@ -101,7 +101,7 @@ def now(clock): return "{0:04d}/{1:02d}/{2:02d}_{4:02d}:{5:02d}:{6:02d}".format(*clock.datetime()) -def MqttPublish(topic, message, retain=False, qos=0, sleep=0): +def mqtt_publish(topic, message, retain=False, qos=0, sleep=0): """MQTT publish helper.""" global CLIENT @@ -114,10 +114,35 @@ def MqttPublish(topic, message, retain=False, qos=0, sleep=0): time.sleep_ms(sleep) -def MqttMultiPublish(payload, sleep=0): +def mqtt_multipub(payload, sleep=0): """Send multiple data""" for pl in payload: - MqttPublish(pl[0], pl[1], retain=pl[2], qos=pl[3], sleep=sleep) + mqtt_publish(pl[0], pl[1], retain=pl[2], qos=pl[3], sleep=sleep) + + +def mqtt_callback(topic, msg): + """Callback en cas de changement de mode""" + global CURRENT_MODE + global BRIGHTN + global PWR + # print(topic, msg) + t = topic.split(b'/') + l = len(t) + if t[l-1] == b'value' and msg.isdigit(): + msg = int(msg) + # print(t[l-2], msg) + if t[l-2] == b'mode' and msg in range(0, len(light_modes.MODES_LST)): + # print("Nouveau mode: ", int(msg)) + CURRENT_MODE = msg + if t[l-2] == b'brightness' and msg in range(0, 101): + # print("Modif. luminosité :", int(msg)) + BRIGHTN = msg + ENCODER.reset(BRIGHTN) + if t[l-2] == b'power' and msg in (0, 1): + PWR = bool(msg) + else: + print("Pas une valeur. Ignore...") + def power_cycle(): @@ -143,8 +168,8 @@ def power_cycle(): time.sleep_ms(10) # Envoi de la nouvelle info au MQTT - MqttMultiPublish([['power/date', now(rtc), True, 1], - ['power/value', "{}".format(PWR), True, 0]]) + mqtt_multipub([['power/date', now(rtc), True, 1], + ['power/value', "{:d}".format(PWR), True, 0]]) def update_button_c(button, button_state, pwr, mode): @@ -174,8 +199,8 @@ def update_button_c(button, button_state, pwr, mode): if mode >= len(light_modes.MODES_LST): mode = 0 - MqttMultiPublish([['mode/date', now(rtc), True, 1], - ['mode/value', "{}".format(mode), True, 1]]) + mqtt_multipub([['mode/date', now(rtc), True, 1], + ['mode/value', "{}".format(mode), True, 1]]) return mode @@ -202,10 +227,18 @@ data = [["last_boot", now(rtc), True, 1], ['brightness/date', now(rtc), True, 1], ["brightness/value", "{}".format(BRIGHTN), True, 1], ['power/date', now(rtc), True, 1], - ["power/value", "{}".format(PWR), True, 1], + ["power/value", "{:d}".format(PWR), True, 1], ] -MqttMultiPublish(data) +mqtt_multipub(data) +# MQTT subscription +print("Set callback...") +CLIENT.set_callback(mqtt_callback) +print("OK") +print("Subcribe...") +CLIENT.subscribe(b"{location}/{device}/#".format(location=config.LOCATION, + device=CLIENT.client_id)) +print("OK") time.sleep(1) BRIGHTN_DATA = None @@ -236,9 +269,11 @@ while True: update_conn_led() + CLIENT.check_msg() + # On n'envoie les données de luminosité qu'une fois stabilisée (MQTT_DELAY) if BRIGHTN_DATA is not None and time.ticks_diff(time.ticks_ms(), MQTT_TIMER) >= 0: - MqttMultiPublish(BRIGHTN_DATA) + mqtt_multipub(BRIGHTN_DATA) BRIGHTN_DATA = None time_to_test = rtc.datetime()