2024-10-28 16:45:30 +00:00
|
|
|
import paho.mqtt.client as mqtt
|
2024-10-28 20:33:14 +00:00
|
|
|
import asyncio
|
2024-10-28 16:45:30 +00:00
|
|
|
import secrets
|
2024-10-28 20:33:14 +00:00
|
|
|
import rules
|
2024-10-28 16:45:30 +00:00
|
|
|
|
|
|
|
state = {}
|
|
|
|
|
2024-10-28 20:33:14 +00:00
|
|
|
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
2024-10-28 16:45:30 +00:00
|
|
|
|
|
|
|
def on_connect(client, userdata, flags, reason_code, properties):
|
|
|
|
print(f"Connected with result code {reason_code}")
|
|
|
|
client.subscribe("main/#")
|
2024-10-28 20:33:14 +00:00
|
|
|
|
|
|
|
async def publish_later(topic, msg, delay):
|
|
|
|
print(f"publish_later: Processing message with topic {topic} after {delay} seconds delay")
|
|
|
|
await asyncio.sleep(delay)
|
|
|
|
print(f"publish_later: completed delay")
|
2024-10-28 16:45:30 +00:00
|
|
|
publish(topic, msg)
|
|
|
|
|
|
|
|
def 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:
|
2024-10-28 20:33:14 +00:00
|
|
|
print(f"... ... Found {len(rules[msg.topic])} rules on this topic")
|
2024-10-28 16:45:30 +00:00
|
|
|
for rule in rules[msg.topic]:
|
2024-10-28 20:33:14 +00:00
|
|
|
print(f"... ... Rule is met when the count is above {rule['count']}. Current count is {state[msg.topic]['count']}")
|
2024-10-28 16:45:30 +00:00
|
|
|
|
|
|
|
if state[msg.topic]['count'] > rule['count']:
|
|
|
|
if(rule['reset']):
|
|
|
|
state[msg.topic]['count'] = 0
|
|
|
|
|
|
|
|
if rule['delay']:
|
2024-10-28 20:33:14 +00:00
|
|
|
asyncio.run_coroutine_threadsafe(publish_later(rule['doTopic'], rule['doMessage'], rule['delay']), loop)
|
2024-10-28 16:45:30 +00:00
|
|
|
else:
|
|
|
|
publish(rule['doTopic'], rule['doMessage'])
|
|
|
|
else:
|
2024-10-28 20:33:14 +00:00
|
|
|
print("... ... i have no rules for this topic")
|
|
|
|
|
2024-10-28 16:45:30 +00:00
|
|
|
# The callback for when a PUBLISH message is received from the server.
|
|
|
|
def on_message(client, userdata, msg):
|
2024-10-28 20:33:14 +00:00
|
|
|
print("on_message")
|
2024-10-28 16:45:30 +00:00
|
|
|
if msg.retain:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if msg.topic in state:
|
|
|
|
state[msg.topic]["count"] += 1
|
|
|
|
else:
|
|
|
|
state[msg.topic] = {}
|
|
|
|
state[msg.topic]["count"] = 0
|
|
|
|
|
|
|
|
check_against_rules(msg)
|
|
|
|
|
2024-10-28 20:33:14 +00:00
|
|
|
async def main():
|
|
|
|
global loop
|
|
|
|
loop = asyncio.get_running_loop() # Store the main event loop
|
2024-10-28 16:45:30 +00:00
|
|
|
|
2024-10-28 20:33:14 +00:00
|
|
|
mqttc.on_connect = on_connect
|
|
|
|
mqttc.on_message = on_message
|
2024-10-28 16:45:30 +00:00
|
|
|
|
2024-10-28 20:33:14 +00:00
|
|
|
mqttc.username_pw_set(secrets.mqtt_username, secrets.mqtt_password)
|
|
|
|
print("connect to klank.school")
|
|
|
|
mqttc.connect("mqtt.klank.school", 7000, 60)
|
2024-10-28 16:45:30 +00:00
|
|
|
|
2024-10-28 20:33:14 +00:00
|
|
|
# Start the MQTT loop in a background thread
|
|
|
|
mqttc.loop_start()
|
2024-10-28 16:45:30 +00:00
|
|
|
|
2024-10-28 20:33:14 +00:00
|
|
|
# Keep the asyncio event loop running
|
|
|
|
await asyncio.Event().wait()
|
2024-10-28 16:45:30 +00:00
|
|
|
|
2024-10-28 20:33:14 +00:00
|
|
|
# Run the main function
|
|
|
|
asyncio.run(main())
|