step: GameMode
This commit is contained in:
1 parent
c1e8f2a7a4
commit
313e097a53
8 files changed
+251
-46
No files matched your search
Executable
+153
@@ -0,0 +1,153 @@
|
||||
#!/bin/env python
|
||||
# mosquitto_pub -h excaliburs-erben.de -p 8883 -t "outdoor/king-arthur/sword/set" -m "gameFinished"
|
||||
# -u "exodususer" -P "awesomeexodusoutdooradventures"
|
||||
import os
|
||||
import sys
|
||||
import ssl
|
||||
import threading
|
||||
import readline
|
||||
import atexit
|
||||
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
# MQTT configuration
|
||||
BROKER = "excaliburs-erben.de"
|
||||
PORT = 8883
|
||||
USERNAME = os.getenv("MQTT_USER", "exodususer")
|
||||
PASSWORD = os.getenv("MQTT_PASSWORD", "awesomeexodusoutdooradventures")
|
||||
BASE_TOPIC = "outdoor/king-arthur/sword"
|
||||
|
||||
YELLOW = "\033[93m"
|
||||
RESET = "\033[0m"
|
||||
|
||||
|
||||
Topics = [
|
||||
(f"{BASE_TOPIC}/ver", 0),
|
||||
# (f"{BASE_TOPIC}/stat", 0),
|
||||
(f"{BASE_TOPIC}/event", 0),
|
||||
(f"{BASE_TOPIC}/set", 0),
|
||||
(f"{BASE_TOPIC}/reset", 0),
|
||||
]
|
||||
|
||||
|
||||
def on_connect(client, userdata, flags, reason_code, properties):
|
||||
global Topics
|
||||
if reason_code.is_failure:
|
||||
print(f"MQTT connection failed: {reason_code}")
|
||||
return
|
||||
|
||||
print(f"Connected to {BROKER}:{PORT}")
|
||||
client.subscribe(Topics)
|
||||
print("Subscribed to:")
|
||||
for topic, _ in Topics:
|
||||
print(f" {topic}")
|
||||
print("\nEnter topic:message to publish (e.g. set:blabla)")
|
||||
print("Type 'quit' to exit.\n")
|
||||
|
||||
def on_connect_one(client, topic, reason_code):
|
||||
if reason_code.is_failure:
|
||||
print(f"MQTT connection failed: {reason_code}")
|
||||
return
|
||||
|
||||
print(f"Connected to {BROKER}:{PORT}")
|
||||
topics = [
|
||||
(f"{BASE_TOPIC}/{topic}", 0),
|
||||
]
|
||||
|
||||
client.subscribe(topics)
|
||||
print("Subscribed to:")
|
||||
for topic, _ in topics:
|
||||
print(f" {topic}")
|
||||
print("\nEnter topic:message to publish (e.g. set:blabla)")
|
||||
print("Type 'quit' to exit.\n")
|
||||
|
||||
|
||||
def on_message(client, userdata, message):
|
||||
topic = message.topic
|
||||
payload = message.payload.decode("utf-8", errors="replace")
|
||||
|
||||
print(f"{YELLOW}>> got: {topic} : {payload}{RESET}", flush=True)
|
||||
|
||||
|
||||
def on_disconnect(client, userdata, disconnect_flags,
|
||||
reason_code, properties):
|
||||
if reason_code.is_failure:
|
||||
print(f"MQTT disconnected: {reason_code}", file=sys.stderr)
|
||||
|
||||
def main():
|
||||
client_Id = "sword-tester"
|
||||
if len(sys.argv) > 1:
|
||||
topic = sys.argv[1]
|
||||
global Topics
|
||||
Topics = [ (f"{BASE_TOPIC}/{topic}", 0) ]
|
||||
client_Id = client_Id + topic
|
||||
|
||||
try:
|
||||
client = mqtt.Client(
|
||||
mqtt.CallbackAPIVersion.VERSION2,
|
||||
client_id=client_Id,
|
||||
)
|
||||
client.username_pw_set(USERNAME, PASSWORD)
|
||||
# Port 8883 normally uses TLS with certificate verification.
|
||||
client.tls_set(cert_reqs=ssl.CERT_REQUIRED)
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
client.on_disconnect = on_disconnect
|
||||
|
||||
|
||||
client.connect(BROKER, PORT, keepalive=60)
|
||||
client.loop_start()
|
||||
|
||||
while True:
|
||||
try:
|
||||
line = input("> ")
|
||||
except EOFError:
|
||||
break
|
||||
|
||||
if line.strip().lower() in ("quit", "exit"):
|
||||
break
|
||||
|
||||
if ":" not in line:
|
||||
print("Format: topic:message (e.g. set:blabla)")
|
||||
continue
|
||||
|
||||
topic_suffix, payload = line.split(":", 1)
|
||||
topic_suffix = topic_suffix.strip()
|
||||
|
||||
if not topic_suffix:
|
||||
print("Topic cannot be empty.")
|
||||
continue
|
||||
|
||||
# Prevent accidental topic injection or MQTT wildcards.
|
||||
if any(c in topic_suffix for c in ("+", "#", "/", "\0")):
|
||||
print("Enter a single topic name, e.g. set or event.")
|
||||
continue
|
||||
|
||||
topic = f"{BASE_TOPIC}/{topic_suffix}"
|
||||
|
||||
info = client.publish(topic, payload, qos=0)
|
||||
|
||||
if info.rc == mqtt.MQTT_ERR_SUCCESS:
|
||||
try:
|
||||
info.wait_for_publish(timeout=5)
|
||||
if info.is_published():
|
||||
print(f"Sent: {topic} : {payload}")
|
||||
else:
|
||||
print("Publish is still pending.")
|
||||
except (RuntimeError, ValueError) as exc:
|
||||
print(f"Publish error: {exc}")
|
||||
else:
|
||||
print(f"Publish failed: {mqtt.error_string(info.rc)}")
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\nStopping...")
|
||||
except (OSError, mqtt.MqttError) as exc:
|
||||
print(f"MQTT error: {exc}", file=sys.stderr)
|
||||
finally:
|
||||
client.disconnect()
|
||||
client.loop_stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user