From abde312698876851bdd3f9ad38d36030a65a3d9d Mon Sep 17 00:00:00 2001 From: vitrinekast Date: Sat, 2 Nov 2024 18:33:01 +0100 Subject: [PATCH] change rules --- .../__pycache__/script.cpython-312.pyc | Bin 0 -> 7338 bytes rule-listener/data.py | 134 +++++++++++++----- rule-listener/script.py | 114 +++++++++------ 3 files changed, 168 insertions(+), 80 deletions(-) create mode 100644 rule-listener/__pycache__/script.cpython-312.pyc diff --git a/rule-listener/__pycache__/script.cpython-312.pyc b/rule-listener/__pycache__/script.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dcc902964ed2ba40378ae95b451317bfcaa9e293 GIT binary patch literal 7338 zcmbtZeQX;?cHdnt$>oPAN}?>uAM(nQ?UQAF*s*=KlQ@v|;rM)(V_S(6#g0OAS2Agm z%gnCqh?ZIqG8ld{4{-;oE z7cKl(-<#!QFX&n{nOOQ#;0jtzz(qt`6SzQW~fr)q4rC5sH-Fg)YXy`>KaLaIv}~Au9e(S zZ;(7t*TD{KkuH-*X$|zcFY*Ly4VD>aTfO&@I?&1*E)tAO@Eeswr8DjD@*eyiehSqI zh6Mp?`Ass6##uunmXIw1JgtR#nF}2Za~{S{GLx+4w1z|kt5tkTtTpRBIF@TRhWR;n z)Akd)9ah#n*0>2);=ahQ%`&rHJTuPxkeRLdOWRep)i!V+H%PF*)nT~g_*hIPK}|Mz zDX0YvS1=kKR1)EkYB+=HcsvwV47(bY<%Ho(j9rX|)l03MVGqY8`E7#@tH?(bANO^> ztja{~Jfo<=&R!)1Oyio`*-fOea9rtxX__1l$48~098=<*WGpJTn?#v(sv#0iXdQ`h z!>f_-=%`Hk!$j3cCGf6*pZY9R|H}O56tfc8@{#mO{8s$iK>Lg^ZJ&N?#aBBQn~9~m zGrq>0(3ln)SA@!Wc7D&FRD5q|YCD#N#>Xl$diP+rpZy!Q+voV(0JNSsMi3-}zlVPY z73csv`F}Y^&@p<9>?Ahxd+>Ys1Gsypnt+U{lp26MX)940+#f9w zYbYLCB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ld*Ra7AAg?10 z)r*S$vG80>f#5 0: + publish(theseTopics[index - 1], 0) + print(f"Go off: {theseTopics[index - 1]}") + await asyncio.sleep(data.walkDelay * 0.8) + + if(random.random() > 0.8): + # TODO send message that gesture is finished + print("finished the walk") + publish("main/gesture" + topic, 0) + isW = False + - 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/#") + async def publish_later(rule, transformTopic): - print(f"publish_later: Processing message with doTopic {rule['doTopic']} after {rule['delay']} seconds delay") + 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}") result = mqttc.publish(topic, msg) - status = result[0] - if not status == 0: print(f"Failed to send message to topic {topic}") + def check_against_rules(msg): if msg.topic in rules: - print(f"Found {len(rules[msg.topic])} rules on this topic"), - + print(f"Found {len(rules[msg.topic])} rules on {msg.topic}"), willReset = False - - for rule in rules[msg.topic]: - 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']): + for rule in rules[msg.topic]: + if (state[msg.topic]['count'][msg.payload] >= rule['count'] + or rule['count'] is None) and int(float(msg.payload)) == rule['state']: + + transformTopic = f"transform" + \ + msg.topic.removeprefix("main") + rule['doTopic'].removeprefix("main") + print(f"---- rule is met----") + print(rule) + + if (rule['reset']): willReset = True if rule['delay']: - if not 'doingDelay' in rule: + if 'doingDelay' not in rule: rule['doingDelay'] = True asyncio.run_coroutine_threadsafe(publish_later(rule, transformTopic), loop) - else: + else: print('not scheduling') else: publish(transformTopic, 1) publish(rule['doTopic'], rule['doMessage']) - + if willReset: state[msg.topic]['count'][msg.payload] = 0 - + else: print("... ... i have no rules for this topic") - + + # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): - print("on message") if msg.retain: return False else: msg.payload = str(msg.payload.decode("utf-8")) - + print(f'msg: {msg.payload} topic: {msg.topic}') + if len(msg.payload) > 140: print("way to long of a message, not gonna store that") return False - + if msg.topic not in state: state[msg.topic] = {'count': {}} - + state[msg.topic]['count'].setdefault(msg.payload, 0) state[msg.topic]['count'][msg.payload] += 1 # 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) + if msg.payload.strip() != 0: + asyncio.run_coroutine_threadsafe(do_a_gesture(msg.topic.removeprefix('main/gesture')), loop) else: check_against_rules(msg) + async def main(): global loop loop = asyncio.get_running_loop() # Store the main event loop + mqttc.on_connect = on_connect mqttc.on_message = on_message @@ -113,9 +143,11 @@ async def main(): # Start the MQTT loop in a background thread mqttc.loop_start() - - # Keep the asyncio event loop running + + # asyncio.run_coroutine_threadsafe(do_a_gesture(data.topics[0].removeprefix('main')), loop) + + # # Keep the asyncio event loop running await asyncio.Event().wait() # Run the main function -asyncio.run(main()) \ No newline at end of file +asyncio.run(main())