Resolution control via MQTT

Change-Id: I3a98f19fac8a34098703c7bdf60e1e17f644a635
diff --git a/person_detection/mqtt.py b/person_detection/mqtt.py
new file mode 100644
index 0000000..89bed3f
--- /dev/null
+++ b/person_detection/mqtt.py
@@ -0,0 +1,53 @@
+import paho.mqtt.client as mqtt
+import time
+import os
+import sys
+import threading
+import logging as log
+
+mqttBroker ="localhost"
+
+resolution = {}
+timer = {}
+timestamp = {}
+
+def init(device):
+    set_resolution(device, "low")
+
+
+def person_detected(device, num):
+    timestamp[device] = time.time()
+    if device in resolution and resolution[device] == "high":
+        return
+    set_resolution(device, "high")
+
+
+def start_timer(device):
+    # log.info("Start timer for device {}".format(device))
+    timer[device] = threading.Timer(10.0, timer_expiry, device)
+    timer[device].start()
+
+
+def remove_timer(device):
+    del timer[device]
+
+
+def set_resolution(device, level):
+    log.info("Setting camera {} resolution to {}".format(device, level))
+    resolution[device] = level
+    if level == "high" and device not in timer:
+        start_timer(device)
+    client = mqtt.Client()
+    client.connect(mqttBroker)
+    client.publish("camera/" + str(5000 + int(device)), level)
+
+
+def timer_expiry(device):
+    now = time.time()
+    diff = now - timestamp[device]
+    # log.info("timer_expiry() - now:{}, timestamp:{}".format(now, timestamp[device]))
+    if diff > 5.0:
+        set_resolution(device, "low")
+        remove_timer(device)
+    else:
+        start_timer(device)