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

145 lines
4.5 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
2022-03-14 20:49:47 +01:00
# Should lights already turn on where there is any kind of motion in the sensor
ENABLE_MOTION_TRIGGERED_LIGHT = True
LOG_FILE_PATH = "log.txt" # Path for logs
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'
2022-03-14 20:49:47 +01:00
} # Custom configuration for philips hue
2021-11-03 00:26:29 +01:00
2022-03-14 20:49:47 +01:00
hue: PhilipsHue = PhilipsHue(hue_conf) # Light interface
counter: PeopleCounter = PeopleCounter(VL53L1XSensor()) # Sensor object
peopleCount: int = 0 # Global count of people on the inside
motion_triggered_lights = False # Is light on because of any detected motion
2021-11-03 00:26:29 +01:00
logging.getLogger().setLevel(logging.INFO)
2022-03-14 20:49:47 +01:00
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 = {
'previousPeopleCount': peopleCount,
'countChange': countChange,
'directionState': directionState,
'dateTime': datetime.now(),
'earlyLightState': motion_triggered_lights
}
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}')
2021-11-03 00:26:29 +01:00
def count_change(change: int) -> None:
2022-03-14 20:49:47 +01:00
"""Handles light state when people count changes
Args:
change (int): The change in the number of people. Usually on of [-1, 0, 1].
"""
2021-12-03 17:23:13 +01:00
global hue
2021-11-03 00:26:29 +01:00
global peopleCount
2022-03-14 20:49:47 +01:00
global motion_triggered_lights
2021-12-03 17:23:13 +01:00
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
2022-03-14 20:49:47 +01:00
if peopleCount <= 0 and previous_lights_state and not motion_triggered_lights:
# Count was 0, but lights were on (not because of motion triggers) => people count was not actually 0
2021-11-03 00:26:29 +01:00
peopleCount = 1
logging.debug(f'People count corrected to {peopleCount}')
elif peopleCount > 0 and not previous_lights_state:
2022-03-14 20:49:47 +01:00
# Count was >0, but lights were off => people count was actually 0
2021-11-03 00:26:29 +01:00
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:
2022-03-14 20:49:47 +01:00
# Signaling that the people count is taking control over the light now
motion_triggered = False
2021-12-03 17:23:13 +01:00
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):
2022-03-14 20:49:47 +01:00
"""Handles motion triggered light state.
Args:
triggerState (Dict): Describing in what directions the sensor is triggerd.
"""
2021-12-03 17:23:13 +01:00
global hue
2022-03-14 20:49:47 +01:00
global motion_triggered_lights
2021-12-03 17:23:13 +01:00
target_light_state = None
# Is someone walking close to the door?
2022-03-14 20:49:47 +01:00
motion_detected = triggerState[Directions.INSIDE] or triggerState[Directions.OUTSIDE]
target_light_state = motion_detected
# Does motion triggered light need to do anything?
if peopleCount > 0:
# State is successfully handled by the count
motion_triggered_lights = False
return
2021-12-03 17:23:13 +01:00
# Only look at changing situations
2022-03-14 20:49:47 +01:00
if target_light_state == motion_triggered_lights:
2021-11-03 00:26:29 +01:00
return
2022-03-14 20:49:47 +01:00
2021-12-03 17:23:13 +01:00
# Are lights on at the moment?
2022-03-14 20:49:47 +01:00
previous_lights_state = hue.get_group(hue_conf['light_group'])[
'state']['any_on']
2021-12-03 17:23:13 +01:00
if target_light_state == previous_lights_state:
return
2022-03-14 20:49:47 +01:00
2021-12-03 17:23:13 +01:00
# Adjust light as necessary
2021-11-03 00:26:29 +01:00
hue.set_group(hue_conf['light_group'], {'on': target_light_state})
2022-03-14 20:49:47 +01:00
logging.debug(
f'Light state changed to {target_light_state} for early light')
2021-12-03 17:23:13 +01:00
2022-03-14 20:49:47 +01:00
# Save state
motion_triggered_lights = target_light_state
2021-12-03 17:30:54 +01:00
2021-11-03 00:26:29 +01:00
2022-03-14 20:49:47 +01:00
# Represents callback trigger order
counter.hookChange(change_cb)
2021-11-03 00:26:29 +01:00
counter.hookCounting(count_change)
2021-12-03 17:23:13 +01:00
counter.hookTrigger(trigger_change)
2022-03-14 20:49:47 +01:00
2021-11-03 00:26:29 +01:00
counter.run()