server/mock-actor/mock-actor.py
Dorian Zedler b6f8b54bc2
- Add Dockerfile
- Add Config in backend using env vars
- Clean up mock actor
2021-08-19 11:27:39 +02:00

36 lines
No EOL
1.1 KiB
Python

import paho.mqtt.client as mqtt
import logging, coloredlogs
coloredlogs.install(level='INFO', fmt='%(asctime)s - [%(levelname)s] %(message)s')
ACTOR_ID = "101"
ACTOR_PASSWORD = "MgUiSW1dloFt9TVKJM5E"
def on_connect(client, userdata, flags, rc):
global ACTOR_ID
if rc != 0:
logging.error(f"Error connecting to MQTT broker: {rc}")
return
logging.info("Successfully connected to MQTT broker")
client.subscribe(f"mlmAccess/actor/{ACTOR_ID}/action")
client.publish(f"mlmAccess/actor/{ACTOR_ID}/status", "0", retain=True)
def on_message(client, userdata, message):
messageContent = str(message.payload.decode("utf-8"))
logging.info(f"Got request to perform action: {messageContent}")
# report back action to let the backend know that it was executed successfully
client.publish(f"mlmAccess/actor/{ACTOR_ID}/status", messageContent, retain=True)
client = mqtt.Client(client_id=f"actor-{ACTOR_ID}")
client.username_pw_set(f"actor-{ACTOR_ID}", ACTOR_PASSWORD)
client.on_connect = on_connect
client.on_message = on_message
logging.info("Initializing MQTT")
client.connect("localhost", 1883, 60)
client.loop_forever()