Shad Ansari | 3a8a391 | 2021-11-18 00:44:50 +0000 | [diff] [blame] | 1 | import paho.mqtt.client as mqtt |
| 2 | import time |
| 3 | import os |
| 4 | import sys |
| 5 | import threading |
| 6 | import logging as log |
| 7 | |
| 8 | mqttBroker ="localhost" |
| 9 | |
| 10 | resolution = {} |
| 11 | timer = {} |
| 12 | timestamp = {} |
| 13 | |
Shad Ansari | 3a8a391 | 2021-11-18 00:44:50 +0000 | [diff] [blame] | 14 | |
| 15 | def 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 | |
| 22 | def 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 | |
| 28 | def remove_timer(device): |
| 29 | del timer[device] |
| 30 | |
| 31 | |
| 32 | def 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 | |
| 42 | def 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) |