Files
Server-scripts/torrent/torrent_mqtt.py
T

57 lines
1.2 KiB
Python
Executable File

#!/config/.venv/bin/python3
"""
torrent_mqtt.py
Veröffentlicht die übergebenen Kommandozeilenargumente als MQTT-Nachricht.
Verwendung:
torrent_mqtt.py "Download abgeschlossen: Film XYZ"
Topic:
serverhost_torrent_sensor/topic
Exit Codes:
0 = Erfolgreich
1 = Keine Nachricht übergeben
2 = MQTT-Fehler
"""
import sys
import paho.mqtt.client as mqtt
MQTT_BROKER_HOST = "192.168.0.4"
MQTT_BROKER_PORT = 1883
MQTT_USERNAME = "mqttbrokeruser"
MQTT_PASSWORD = "TKtWrtWGLSouf6"
MQTT_TOPIC = "serverhost_torrent_sensor/topic"
def main():
message = " ".join(sys.argv[1:]).strip()
if not message:
print("No message supplied.")
sys.exit(1)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
try:
client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT, 60)
info = client.publish(MQTT_TOPIC, message)
info.wait_for_publish()
print(f"Published to '{MQTT_TOPIC}': {message}")
client.disconnect()
sys.exit(0)
except Exception as e:
print(f"MQTT error: {e}")
sys.exit(2)
if __name__ == "__main__":
main()