Compare commits
No commits in common. "89e8eabde5c58bcc4de4e678af46b5ce722ceb27" and "d6816fcbf5487c8d92506088c4b651673b9e070f" have entirely different histories.
89e8eabde5
...
d6816fcbf5
@ -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/three", # i will send to topic
|
||||
"doTopic": "main/fan/one", # 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/three", # i will send to topic
|
||||
"doTopic": "main/fan/one", # 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,14 +43,3 @@ rules = {
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
gestures = {
|
||||
"hi": {
|
||||
"on": "0",
|
||||
"off": "1",
|
||||
"delay": 1,
|
||||
"multiplier": 0.7,
|
||||
"range": [4, 8]
|
||||
}
|
||||
}
|
@ -9,32 +9,35 @@ state = {}
|
||||
rules = data.rules
|
||||
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
||||
|
||||
async def do_a_gesture(gesture, topic):
|
||||
async def on_gesture_message():
|
||||
state = False
|
||||
delay = gesture["delay"]
|
||||
for i in range(randrange(gesture["range"][0],gesture["range"][1])):
|
||||
delay = 3
|
||||
for i in range(randrange(4,8)):
|
||||
await asyncio.sleep(delay)
|
||||
delay = delay * gesture["multiplier"]
|
||||
delay = delay * 0.7
|
||||
if state:
|
||||
publish("gesture" + topic, gesture["on"])
|
||||
print("do left")
|
||||
state = False
|
||||
else:
|
||||
publish("gesture" + topic, gesture["off"])
|
||||
print("do right")
|
||||
state = True
|
||||
|
||||
publish("main/gesture" + topic, 0)
|
||||
print("done")
|
||||
|
||||
|
||||
|
||||
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, transformTopic):
|
||||
async def publish_later(rule):
|
||||
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}")
|
||||
@ -55,17 +58,15 @@ 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, transformTopic), loop)
|
||||
asyncio.run_coroutine_threadsafe(publish_later(rule), loop)
|
||||
else:
|
||||
print('not scheduling')
|
||||
else:
|
||||
publish(transformTopic, 1)
|
||||
publish(rule['doTopic'], rule['doMessage'])
|
||||
|
||||
if willReset:
|
||||
@ -91,14 +92,7 @@ def on_message(client, userdata, msg):
|
||||
|
||||
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)
|
||||
|
||||
else:
|
||||
check_against_rules(msg)
|
||||
check_against_rules(msg)
|
||||
|
||||
async def main():
|
||||
global loop
|
||||
|
Loading…
Reference in New Issue
Block a user