mash-sensor-tof-pc/src/philips_hue_counter.py

111 lines
3.2 KiB
Python
Raw Normal View History

2021-12-03 17:30:54 +01:00
from datetime import datetime
from pathlib import Path
2021-12-03 17:23:13 +01:00
from typing import Dict
2021-11-03 00:26:29 +01:00
from interface.philips_hue import PhilipsHue
from sensor.people_counter import PeopleCounter
2021-12-03 17:23:13 +01:00
from sensor.tof_sensor import Directions
2021-11-03 00:26:29 +01:00
from sensor.vl53l1x_sensor import VL53L1XSensor
import logging
2021-12-03 17:30:54 +01:00
import json
2021-11-03 00:26:29 +01:00
2021-12-03 17:30:54 +01:00
LOG_FILE_PATH = "log.txt"
2021-11-03 00:26:29 +01:00
hue_conf = {
'bridge_ip': '',
'transition_time': 10, # seconds
'light_group': '',
# If file exists, application is considered 'registered' at the bridge
'registered_file': 'smart_light_registered.bridge'
}
hue = PhilipsHue(hue_conf)
counter = PeopleCounter(VL53L1XSensor())
peopleCount = 0
2021-12-03 18:13:30 +01:00
early_light_state = False
2021-11-03 00:26:29 +01:00
logging.getLogger().setLevel(logging.INFO)
def count_change(change: int) -> None:
2021-12-03 17:23:13 +01:00
global hue
2021-11-03 00:26:29 +01:00
global peopleCount
2021-12-03 17:23:13 +01:00
global early_light_state
2021-11-03 00:26:29 +01:00
# Are lights on at the moment?
2021-12-03 17:23:13 +01:00
previous_lights_state = hue.get_group(hue_conf['light_group'])[
'state']['any_on']
2021-11-03 00:26:29 +01:00
# Apply correction
2021-12-03 17:23:13 +01:00
if peopleCount <= 0 and previous_lights_state and not early_light_state:
2021-11-03 00:26:29 +01:00
# User indicates, that people count was not actually 0
peopleCount = 1
logging.debug(f'People count corrected to {peopleCount}')
elif peopleCount > 0 and not previous_lights_state:
# User indicates, that people count was actually 0
peopleCount = 0
logging.debug(f'People count corrected to {peopleCount}')
2021-12-03 17:23:13 +01:00
2021-11-03 00:26:29 +01:00
peopleCount += change
if peopleCount < 0:
peopleCount = 0
logging.debug(f'People count changed by {change}')
2021-12-03 17:23:13 +01:00
2021-11-03 00:26:29 +01:00
# Handle light
target_light_state = peopleCount > 0
2021-12-03 17:23:13 +01:00
2021-11-03 00:26:29 +01:00
# Return, if there is no change
if previous_lights_state == target_light_state:
2021-12-03 17:23:13 +01:00
if previous_lights_state:
early_light_state = False
return
hue.set_group(hue_conf['light_group'], {'on': target_light_state})
logging.debug(f'Light state changed to {target_light_state}')
def trigger_change(triggerState: Dict):
global hue
global early_light_state
target_light_state = None
# Is someone walking close to the door?
target_light_state = triggerState[Directions.INSIDE] or triggerState[Directions.OUTSIDE]
# Only look at changing situations
if target_light_state == early_light_state:
2021-11-03 00:26:29 +01:00
return
2021-12-03 17:23:13 +01:00
# Are lights on at the moment?
previous_lights_state = hue.get_group(hue_conf['light_group'])['state']['any_on']
if target_light_state == previous_lights_state:
return
# Adjust light as necessary
2021-11-03 00:26:29 +01:00
hue.set_group(hue_conf['light_group'], {'on': target_light_state})
2021-12-03 17:30:54 +01:00
logging.debug(f'Light state changed to {target_light_state} for early light')
2021-12-03 17:23:13 +01:00
early_light_state = target_light_state
2021-11-03 00:26:29 +01:00
2021-12-03 17:30:54 +01:00
def change_cb(countChange: int, directionState: Dict):
data = {
'previousPeopleCount': peopleCount,
'countChange': countChange,
'directionState': directionState,
2021-12-03 18:13:30 +01:00
'dateTime': datetime.now(),
'earlyLightState': early_light_state
2021-12-03 17:30:54 +01:00
}
try:
with open(LOG_FILE_PATH, 'a') as f:
2021-12-03 18:00:02 +01:00
f.write(json.dumps(data, default=str) + "\n")
2021-12-03 17:30:54 +01:00
except Exception as ex:
logging.exception(f'Unable to write log file. {ex}')
2021-11-03 00:26:29 +01:00
counter.hookCounting(count_change)
2021-12-03 17:23:13 +01:00
counter.hookTrigger(trigger_change)
2021-12-03 17:30:54 +01:00
counter.hookChange(change_cb)
2021-11-03 00:26:29 +01:00
counter.run()