include some gestures

This commit is contained in:
vitrinekast 2024-10-31 17:45:52 +01:00
parent 82daab9b7a
commit c7c7afcba1
2 changed files with 33 additions and 16 deletions

View File

@ -20,7 +20,7 @@ rules = {
{ # Turn the fan off every time after 1 minute
"count": False, # the amount of times. If its false, just do it.
"state": "on", # it has had this state
"doTopic": "main/fan/one", # i will send to topic
"doTopic": "main/fan/three", # i will send to topic
"doMessage": "off", # this message
"delay": 4, # after this amount of seconds
"reset": False, # and i should reset my count afterwards
@ -28,7 +28,7 @@ rules = {
{ # Turn the fan on every time after 6 minute
"count": 3, # the amount of times. If its false, just do it.
"state": "on", # it has had this state
"doTopic": "main/fan/one", # i will send to topic
"doTopic": "main/fan/three", # i will send to topic
"doMessage": "off", # this message
"delay": 360, # after this amount of seconds
"reset": False, # and i should reset my count afterwards
@ -43,3 +43,14 @@ rules = {
}
]
}
gestures = {
"hi": {
"on": "0",
"off": "1",
"delay": 1,
"multiplier": 0.7,
"range": [4, 8]
}
}

View File

@ -9,35 +9,32 @@ state = {}
rules = data.rules
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
async def on_gesture_message():
async def do_a_gesture(gesture, topic):
state = False
delay = 3
for i in range(randrange(4,8)):
delay = gesture["delay"]
for i in range(randrange(gesture["range"][0],gesture["range"][1])):
await asyncio.sleep(delay)
delay = delay * 0.7
delay = delay * gesture["multiplier"]
if state:
print("do left")
publish("gesture" + topic, gesture["on"])
state = False
else:
print("do right")
publish("gesture" + topic, gesture["off"])
state = True
print("done")
publish("main/gesture" + topic, 0)
def on_connect(client, userdata, flags, reason_code, properties):
print(f"Connected with result code {reason_code}")
client.subscribe("main/#")
mqttc.message_callback_add("gesture/#", on_gesture_message)
client.subscribe("gesture/#")
async def publish_later(rule):
async def publish_later(rule, transformTopic):
print(f"publish_later: Processing message with doTopic {rule['doTopic']} after {rule['delay']} seconds delay")
await asyncio.sleep(rule['delay'])
del rule['doingDelay']
publish(rule['doTopic'], rule['doMessage'])
publish(transformTopic, 1)
def publish(topic, msg):
print(f"publish {topic} {msg}")
@ -58,15 +55,17 @@ def check_against_rules(msg):
print(f"Rule: when the count is > {rule['count']}. Current = {state[msg.topic]['count'][msg.payload]} on payload {msg.payload}"),
if (state[msg.topic]['count'][msg.payload] >= rule['count'] or rule['count'] == False) and msg.payload == rule['state']:
print(f"transform topic is {transformTopic}")
if(rule['reset']):
willReset = True
if rule['delay']:
if not 'doingDelay' in rule:
rule['doingDelay'] = True
asyncio.run_coroutine_threadsafe(publish_later(rule), loop)
asyncio.run_coroutine_threadsafe(publish_later(rule, transformTopic), loop)
else:
print('not scheduling')
else:
publish(transformTopic, 1)
publish(rule['doTopic'], rule['doMessage'])
if willReset:
@ -92,7 +91,14 @@ def on_message(client, userdata, msg):
state[msg.topic]['count'].setdefault(msg.payload, 0)
state[msg.topic]['count'][msg.payload] += 1
check_against_rules(msg)
# check if the message has a gesture
if "gesture" in msg.topic:
if msg.payload.strip() in data.gestures:
asyncio.run_coroutine_threadsafe(do_a_gesture(data.gestures[msg.payload], msg.topic.removeprefix('main/gesture')), loop)
else:
check_against_rules(msg)
async def main():
global loop