From 1ab40cff2a39bd45c71500798bc3ef319f5e5a7a Mon Sep 17 00:00:00 2001 From: s3lph Date: Sun, 10 Sep 2023 21:29:06 +0200 Subject: [PATCH] feat: configure ui labels via mqtt message --- flow3r_openhab/__init__.py | 31 ++++++++++++++++++++++++++++-- flow3r_openhab/config.example.json | 2 +- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/flow3r_openhab/__init__.py b/flow3r_openhab/__init__.py index d0f2ec2..9211fa9 100644 --- a/flow3r_openhab/__init__.py +++ b/flow3r_openhab/__init__.py @@ -1,6 +1,7 @@ import gc import json +import math import network import os import socket @@ -22,14 +23,25 @@ class Flow3rOpenhabMqtt(Application): self.state = 0 self.nic = network.WLAN(network.STA_IF) client_id = CONFIG['mqtt_client_id'].format(self.nic.config('mac').hex()) - self.base_topic = CONFIG['mqtt_topic'].format(self.nic.config('mac').hex()) + self.base_topic = CONFIG['mqtt_topic_base'].format(self.nic.config('mac').hex()).encode() self.mqtt = MQTTClient(client_id, CONFIG['mqtt_broker_ip']) + self.mqtt.set_callback(self.mqtt_cb) self.touch_time = [None]*10 self.touch_begin = [None]*10 self.touch_last = [None]*10 self.touch_base = [False]*10 self.touch_tip = [False]*10 self.touch_log = [[]]*10 + self.ui_config = [""]*10 + + def mqtt_cb(self, topic, msg): + print(topic, msg) + if topic == self.base_topic + b'ui_config': + try: + print(msg) + self.ui_config = json.loads(msg.decode()) + except Exception as e: + print(e) def draw(self, ctx: Context) -> None: if self.state == 0: @@ -39,6 +51,15 @@ class Flow3rOpenhabMqtt(Application): ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill() ctx.rgb(0.3, 0.3, 0.3).arc(0, 0, 90, 0, 2.39, 0).arc(0, 0, 90, 2.79, 6.29, 0).stroke() ctx.rgb(0.9, 0.3, 0.1).move_to(-96, 53).line_to(-1, -43).line_to(71, 29).stroke() + ctx.text_align = ctx.CENTER + ctx.text_baseline = ctx.MIDDLE + ctx.font_size = 20 + for i, text in enumerate(self.ui_config): + phi = (2*math.pi/10)*(i+1)-(math.pi/2) + radius = 50 if i%2 else 80 + x = radius*math.cos(phi) + y = radius*math.sin(phi) + ctx.rgb(1, 1, 1).move_to(x, y).text(text) if CONFIG.get('debug_touch', False): ctx.line_width =1 for i in range(10): @@ -62,9 +83,13 @@ class Flow3rOpenhabMqtt(Application): self.do_connect() if self.nic.status() == network.STAT_GOT_IP and self.state == 0: gc.collect() + self.mqtt.set_last_will(self.base_topic + 'status', 'Offline', retain=True) self.mqtt.connect() + self.mqtt.subscribe(self.base_topic + 'ui_config') + self.mqtt.publish(self.base_topic + 'status', 'Online', retain=True) self.state = 1 if self.state == 1: + self.mqtt.check_msg() for i in range(10): if ins.captouch.petals[i].pressed: # Ignore the first few ms of touch events, as position data appears to be unrealiable at first @@ -104,7 +129,9 @@ class Flow3rOpenhabMqtt(Application): event = 'swipe_left' if event is not None: human_petal = 10-(-i%10) - self.mqtt.publish(self.base_topic, b'{{"petal":"{}","event":"{}","dx":{},"dy":{},"duration":{}}}'.format(human_petal, event, d_x, d_y, duration)) + msg = b'{{"petal":"{}","event":"{}","dx":{},"dy":{},"duration":{}}}'\ + .format(human_petal, event, d_x, d_y, duration) + self.mqtt.publish(self.base_topic + 'event', msg) self.touch_time[i] = None self.touch_begin[i] = None self.touch_last[i] = None diff --git a/flow3r_openhab/config.example.json b/flow3r_openhab/config.example.json index 8f524a9..cf6e5fe 100644 --- a/flow3r_openhab/config.example.json +++ b/flow3r_openhab/config.example.json @@ -3,5 +3,5 @@ "wifi_psk": "supersecurepassword", "mqtt_broker_ip": "127.0.0.1", "mqtt_client_id": "flow3r-{}", - "mqtt_topic": "flow3r-{}/event" + "mqtt_topic_base": "flow3r-{}/" }