blob: ce404110496a7097467b07f9c5935367ba77f780 [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
8mqttBroker ="localhost"
9
10resolution = {}
11timer = {}
12timestamp = {}
13
Shad Ansari3a8a3912021-11-18 00:44:50 +000014
15def person_detected(device, num):
16 timestamp[device] = time.time()
17 if device in resolution and resolution[device] == "high":
18 return
19 set_resolution(device, "high")
20
21
22def start_timer(device):
23 # log.info("Start timer for device {}".format(device))
24 timer[device] = threading.Timer(10.0, timer_expiry, device)
25 timer[device].start()
26
27
28def remove_timer(device):
29 del timer[device]
30
31
32def set_resolution(device, level):
33 log.info("Setting camera {} resolution to {}".format(device, level))
34 resolution[device] = level
35 if level == "high" and device not in timer:
36 start_timer(device)
37 client = mqtt.Client()
38 client.connect(mqttBroker)
39 client.publish("camera/" + str(5000 + int(device)), level)
40
41
42def timer_expiry(device):
43 now = time.time()
44 diff = now - timestamp[device]
45 # log.info("timer_expiry() - now:{}, timestamp:{}".format(now, timestamp[device]))
46 if diff > 5.0:
47 set_resolution(device, "low")
48 remove_timer(device)
49 else:
50 start_timer(device)