blob: 5cd241f8c927f348a83378e4ab18761405e7b279 [file] [log] [blame]
Shad Ansari3a8a3912021-11-18 00:44:50 +00001import paho.mqtt.client as mqtt
2import time
3import os
4import sys
5import threading
6import logging as log
7
Shad Ansari2ecc6522021-12-03 21:03:48 +00008import roc
9
Shad Ansari3a8a3912021-11-18 00:44:50 +000010mqttBroker ="localhost"
11
12resolution = {}
13timer = {}
14timestamp = {}
15
Shad Ansari3a8a3912021-11-18 00:44:50 +000016
17def person_detected(device, num):
18 timestamp[device] = time.time()
19 if device in resolution and resolution[device] == "high":
20 return
Shad Ansari2ecc6522021-12-03 21:03:48 +000021 set_resolution_high(device)
Shad Ansari3a8a3912021-11-18 00:44:50 +000022
23
24def start_timer(device):
25 # log.info("Start timer for device {}".format(device))
26 timer[device] = threading.Timer(10.0, timer_expiry, device)
27 timer[device].start()
28
29
30def remove_timer(device):
31 del timer[device]
32
33
Shad Ansari2ecc6522021-12-03 21:03:48 +000034def set_resolution_high(device):
35 set_resolution(device, "high")
36 roc.set_uplink_mbr_high()
37
38
39def set_resolution_low(device):
40 set_resolution(device, "low")
41 roc.set_uplink_mbr_low()
42
43
Shad Ansari3a8a3912021-11-18 00:44:50 +000044def set_resolution(device, level):
45 log.info("Setting camera {} resolution to {}".format(device, level))
46 resolution[device] = level
47 if level == "high" and device not in timer:
48 start_timer(device)
49 client = mqtt.Client()
50 client.connect(mqttBroker)
51 client.publish("camera/" + str(5000 + int(device)), level)
52
53
54def timer_expiry(device):
55 now = time.time()
56 diff = now - timestamp[device]
57 # log.info("timer_expiry() - now:{}, timestamp:{}".format(now, timestamp[device]))
58 if diff > 5.0:
Shad Ansari2ecc6522021-12-03 21:03:48 +000059 set_resolution_low(device)
Shad Ansari3a8a3912021-11-18 00:44:50 +000060 remove_timer(device)
61 else:
62 start_timer(device)