| import paho.mqtt.client as mqtt |
| import time |
| import os |
| import sys |
| import threading |
| import logging as log |
| |
| mqttBroker ="localhost" |
| |
| resolution = {} |
| timer = {} |
| timestamp = {} |
| |
| |
| 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) |