feat: configure ui labels via mqtt message
This commit is contained in:
parent
cec9afb41c
commit
1ab40cff2a
2 changed files with 30 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
import gc
|
import gc
|
||||||
import json
|
import json
|
||||||
|
import math
|
||||||
import network
|
import network
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
|
@ -22,14 +23,25 @@ class Flow3rOpenhabMqtt(Application):
|
||||||
self.state = 0
|
self.state = 0
|
||||||
self.nic = network.WLAN(network.STA_IF)
|
self.nic = network.WLAN(network.STA_IF)
|
||||||
client_id = CONFIG['mqtt_client_id'].format(self.nic.config('mac').hex())
|
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 = MQTTClient(client_id, CONFIG['mqtt_broker_ip'])
|
||||||
|
self.mqtt.set_callback(self.mqtt_cb)
|
||||||
self.touch_time = [None]*10
|
self.touch_time = [None]*10
|
||||||
self.touch_begin = [None]*10
|
self.touch_begin = [None]*10
|
||||||
self.touch_last = [None]*10
|
self.touch_last = [None]*10
|
||||||
self.touch_base = [False]*10
|
self.touch_base = [False]*10
|
||||||
self.touch_tip = [False]*10
|
self.touch_tip = [False]*10
|
||||||
self.touch_log = [[]]*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:
|
def draw(self, ctx: Context) -> None:
|
||||||
if self.state == 0:
|
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, 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.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.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):
|
if CONFIG.get('debug_touch', False):
|
||||||
ctx.line_width =1
|
ctx.line_width =1
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
|
@ -62,9 +83,13 @@ class Flow3rOpenhabMqtt(Application):
|
||||||
self.do_connect()
|
self.do_connect()
|
||||||
if self.nic.status() == network.STAT_GOT_IP and self.state == 0:
|
if self.nic.status() == network.STAT_GOT_IP and self.state == 0:
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
self.mqtt.set_last_will(self.base_topic + 'status', 'Offline', retain=True)
|
||||||
self.mqtt.connect()
|
self.mqtt.connect()
|
||||||
|
self.mqtt.subscribe(self.base_topic + 'ui_config')
|
||||||
|
self.mqtt.publish(self.base_topic + 'status', 'Online', retain=True)
|
||||||
self.state = 1
|
self.state = 1
|
||||||
if self.state == 1:
|
if self.state == 1:
|
||||||
|
self.mqtt.check_msg()
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
if ins.captouch.petals[i].pressed:
|
if ins.captouch.petals[i].pressed:
|
||||||
# Ignore the first few ms of touch events, as position data appears to be unrealiable at first
|
# 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'
|
event = 'swipe_left'
|
||||||
if event is not None:
|
if event is not None:
|
||||||
human_petal = 10-(-i%10)
|
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_time[i] = None
|
||||||
self.touch_begin[i] = None
|
self.touch_begin[i] = None
|
||||||
self.touch_last[i] = None
|
self.touch_last[i] = None
|
||||||
|
|
|
@ -3,5 +3,5 @@
|
||||||
"wifi_psk": "supersecurepassword",
|
"wifi_psk": "supersecurepassword",
|
||||||
"mqtt_broker_ip": "127.0.0.1",
|
"mqtt_broker_ip": "127.0.0.1",
|
||||||
"mqtt_client_id": "flow3r-{}",
|
"mqtt_client_id": "flow3r-{}",
|
||||||
"mqtt_topic": "flow3r-{}/event"
|
"mqtt_topic_base": "flow3r-{}/"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue