|
@@ -0,0 +1,45 @@
|
|
|
|
|
+#!/usr/bin/env python3
|
|
|
|
|
+import paho.mqtt.client as mqtt
|
|
|
|
|
+import threading
|
|
|
|
|
+import json
|
|
|
|
|
+import subprocess
|
|
|
|
|
+from time import sleep
|
|
|
|
|
+from os import path
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+p = None
|
|
|
|
|
+# The callback for when the client receives a CONNACK response from the server.
|
|
|
|
|
+def on_connect(client, userdata, flags, rc):
|
|
|
|
|
+ print("Connected with result code "+str(rc))
|
|
|
|
|
+
|
|
|
|
|
+ # Subscribing in on_connect() means that if we lose the connection and
|
|
|
|
|
+ # reconnect then subscriptions will be renewed.
|
|
|
|
|
+ client.subscribe("video/cam2/preview")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# The callback for when a PUBLISH message is received from the server.
|
|
|
|
|
+def on_message(client, userdata, msg):
|
|
|
|
|
+ print(msg.topic+" "+str(msg.payload))
|
|
|
|
|
+ if msg.topic == "video/cam2/preview":
|
|
|
|
|
+ try:
|
|
|
|
|
+ run = int(msg.payload)
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print(e)
|
|
|
|
|
+ return
|
|
|
|
|
+ if run and p is None:
|
|
|
|
|
+ print("Starting")
|
|
|
|
|
+ p = subprocess.Popen(["/home/curso/code/snowmix/camera1/play-cam2.sh"]
|
|
|
|
|
+ if p is not None and not run:
|
|
|
|
|
+ print("Stopping")
|
|
|
|
|
+ p.terminate()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+client = mqtt.Client()
|
|
|
|
|
+client.on_connect = on_connect
|
|
|
|
|
+client.on_message = on_message
|
|
|
|
|
+
|
|
|
|
|
+client.connect("192.168.2.123", 1883, 60)
|
|
|
|
|
+
|
|
|
|
|
+t = threading.Thread(target=client.loop_forever)
|
|
|
|
|
+t.start()
|
|
|
|
|
+t.join()
|