Camera scripts

Change-Id: Idb5a5d8bae420a095906f22874f2bb90f56639e5
diff --git a/bin/openvino-camera.sh b/bin/openvino-camera.sh
deleted file mode 100644
index 17f8bbd..0000000
--- a/bin/openvino-camera.sh
+++ /dev/null
@@ -1,22 +0,0 @@
-#!/bin/bash
-set -o errexit
-
-readonly LOG_FILE="/var/log/openvino-camera.txt"
-
-sudo touch $LOG_FILE
-exec 1>$LOG_FILE
-exec 2>&1
-
-if [[ "$HOST" != "" ]]; then
-    HOST="$1"
-else
-    HOST=10.92.2.22
-fi
-
-if [[ "$2" != "" ]]; then
-    PORT="$2"
-else
-    PORT=5000
-fi
-
-/usr/bin/gst-launch-1.0 rpicamsrc bitrate=1000000 ! video/x-h264,level=4,width=640,height=480,framerate=15/1 ! h264parse ! rtph264pay config-interval=10  pt=96 ! udpsink host=10.92.2.22  port=$PORT
diff --git a/camera/openvino-camera.sh b/camera/openvino-camera.sh
new file mode 100644
index 0000000..08d8a79
--- /dev/null
+++ b/camera/openvino-camera.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+set -o errexit
+
+readonly LOG_FILE="/var/log/openvino-camera.txt"
+
+sudo touch $LOG_FILE
+exec 1>$LOG_FILE
+exec 2>&1
+
+echo $@
+
+if [ $1 = "low" ]; then
+    width="320"
+    height="240"
+else
+    width="640"
+    height="480"
+fi
+host="$2"
+port="$3"
+
+/usr/bin/gst-launch-1.0 rpicamsrc bitrate=1000000 ! video/x-h264,level=4,width=$width,height=$height,framerate=15/1 ! h264parse ! rtph264pay config-interval=10  pt=96 ! udpsink host=$host  port=$port
diff --git a/camera/openvino-mqtt.py b/camera/openvino-mqtt.py
new file mode 100644
index 0000000..836f513
--- /dev/null
+++ b/camera/openvino-mqtt.py
@@ -0,0 +1,49 @@
+import paho.mqtt.client as mqtt
+import time
+import os
+import sys
+
+class Mqtt:
+
+    def __init__(self, ip, port):
+        self.topic = "camera/" + port
+        self.ip = ip
+        self.client = mqtt.Client()
+        self.client.on_connect = self.on_connect
+        self.client.on_message = self.on_message
+
+    # The callback for when the client receives a CONNACK response from the server.
+    def on_connect(self, client, userdata, flags, rc):
+        print("Connected to {} with result code ".format(self.ip, str(rc)))
+        # Subscribing in on_connect() means that if we lose the connection and
+        # reconnect then subscriptions will be renewed.
+        client.subscribe(self.topic)
+
+    # The callback for when a PUBLISH message is received from the server.
+    def on_message(self, client, userdata, msg):
+        cmd = msg.payload.decode()
+        print(msg.topic+" "+cmd)
+        if cmd == "high":
+            print("Change to high resolution")
+            os.system("sudo systemctl stop openvino-camera@low.service")
+            os.system("sudo systemctl start openvino-camera@high.service")
+        elif cmd == "low":
+            print("Change to low resolution")
+            os.system("sudo systemctl stop openvino-camera@high.service")
+            os.system("sudo systemctl start openvino-camera@low.service")
+        else:
+            print("Unknown cmd: " + cmd)
+
+    def start(self):
+        while True:
+            try:
+                self.client.connect(self.ip)
+            except:
+                time.sleep(1)
+                continue
+            break
+        self.client.loop_forever()
+
+if __name__ == '__main__':
+    mqtt = Mqtt(sys.argv[1], sys.argv[2])
+    mqtt.start()