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

108 lines
3.1 KiB
Python
Raw Normal View History

2022-09-14 00:15:37 +02:00
from datetime import datetime
from services.philips_hue import PhilipsHue
from sensors.people_counter import PeopleCounter
import logging
import json
from setup import hue_conf, LOG_FILE_PATH, counter
2022-09-14 00:15:37 +02:00
hue: PhilipsHue = PhilipsHue(hue_conf) # Light interface
peopleCount: int = 0 # Global count of people on the inside
def change_cb(countChange: int, directionState: dict):
"""Handles basic logging of event data for later analysis.
Args:
countChange (int): The change in the number of people. Usually on of [-1, 0, 1].
directionState (dict): Object describing the internal state of the sensor.
"""
data = {
"version": "v0.0",
"previousPeopleCount": peopleCount,
"countChange": countChange,
"directionState": directionState,
"dateTime": datetime.now(),
"motionTriggeredLights": False,
}
try:
with open(LOG_FILE_PATH, "a") as f:
f.write(json.dumps(data, default=str) + "\n")
except Exception as ex:
logging.exception(f"Unable to write log file. {ex}")
def count_change(change: int) -> None:
"""Handles light state when people count changes
Args:
change (int): The change in the number of people. Usually on of [-1, 0, 1].
"""
global hue
global peopleCount
# Are lights on at the moment?
previous_lights_state = get_light_state()
# Apply correction
if peopleCount <= 0 and previous_lights_state:
# Count was 0, but lights were on => people count was not actually 0
peopleCount = 1
2023-10-09 17:24:07 +02:00
logging.info(f"People count corrected to {peopleCount}")
2022-09-14 00:15:37 +02:00
elif peopleCount > 0 and not previous_lights_state:
# Count was >0, but lights were off => people count was actually 0
peopleCount = 0
2023-10-09 17:24:07 +02:00
logging.info(f"People count corrected to {peopleCount}")
2022-09-14 00:15:37 +02:00
peopleCount += change
if peopleCount < 0:
peopleCount = 0
2023-10-09 17:24:07 +02:00
logging.info(f"People count changed by {change}")
2022-09-14 00:15:37 +02:00
# Handle light
target_light_state = peopleCount > 0
# Return, if there is no change
if previous_lights_state == target_light_state:
return
set_light_state(target_light_state)
def set_light_state(target_light_state: bool) -> bool:
"""Sets the lights to the given state.
Args:
target_light_state (bool): Should lights on the inside be on or off.
Returns:
bool: Previous light state.
"""
# Are lights on at the moment?
previous_lights_state = get_light_state()
if target_light_state == previous_lights_state:
return previous_lights_state
# Adjust light as necessary
hue.set_group(hue_conf["light_group"], {"on": target_light_state})
2023-10-09 17:24:07 +02:00
logging.info(f"Light state changed to {target_light_state}")
2022-09-14 00:15:37 +02:00
return previous_lights_state
def get_light_state() -> bool:
"""
Returns:
bool: Current light state.
"""
return hue.get_group(hue_conf["light_group"])["state"]["any_on"]
if __name__ == "__main__":
# Represents callback trigger order
counter.hookChange(change_cb)
counter.hookCounting(count_change)
counter.run()