Quellcode durchsuchen

allow a service restart via mqtt

David vor 8 Jahren
Ursprung
Commit
b8846790b6
1 geänderte Dateien mit 12 neuen und 3 gelöschten Zeilen
  1. 12 3
      camera1/cam.py

+ 12 - 3
camera1/cam.py

@@ -16,6 +16,7 @@ HEIGHT = 720
 SCREEN_WIDTH = 800
 SCREEN_HEIGHT = 600
 zoom = 1
+not_exited = True
 
 
 # def bus_call(bus, msg, *args):
@@ -70,12 +71,16 @@ def on_connect(client, userdata, flags, rc):
 
 # The callback for when a PUBLISH message is received from the server.
 def on_message(client, userdata, msg):
+    global not_exited
     print(msg.topic+" "+str(msg.payload))
 
     if msg.topic == "video/cam1/preview":
         set_preview(int(msg.payload))
     elif msg.topic == "video/cam1/zoom":
         set_zoom(float(msg.payload))
+    elif msg.topic == "video/cam1/restart":
+        print("Restarting!")
+        not_exited = False
 
 
 client = mqtt.Client()
@@ -123,8 +128,12 @@ if __name__ == "__main__":
     pipeline.set_state(Gst.State.PLAYING)
     t = threading.Thread(target=client.loop_forever, daemon=True)
     t.start()
-    while True:
-        client.publish("video/heartbeat", "cam1")
-        time.sleep(10)
+    counter = 0
+    while not_exited:
+        time.sleep(1)
+        counter += 1
+        if counter >= 10:
+            client.publish("video/heartbeat", "cam1")
 
     pipeline.set_state(Gst.State.NULL)
+    print("Pipeline dead. Bye")