浏览代码

mqtt-to-cam2 projection

David 8 年之前
父节点
当前提交
c8804a451c
共有 1 个文件被更改,包括 45 次插入0 次删除
  1. 45 0
      camera1/handle-cam2.py

+ 45 - 0
camera1/handle-cam2.py

@@ -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()