diff --git a/README.md b/README.md index 328d0f6..81597d1 100644 --- a/README.md +++ b/README.md @@ -47,11 +47,11 @@ In the photos below you can see my current setup. The piece of cardboard is supp The piece of cardboard is on the side of my room, which might help you to orient the sensor properly.

- +
- +
- +

Sadly, due to my limited time, there is so far no proper, easy way of changing the direction of the sensor in software. It is on my todo list tho (see below) and please don't hesistate to open issue if you require a certain feature! I am more than happy to help and expand this project. diff --git a/src/sensors/people_counter.py b/src/sensors/people_counter.py index bcd49e7..42f1453 100644 --- a/src/sensors/people_counter.py +++ b/src/sensors/people_counter.py @@ -6,6 +6,7 @@ import threading COUNTING_CB = "counting" TRIGGER_CB = "trigger" CHANGE_CB = "changes" +MEASUREMENT_CB = "measurement" START_TIME = "start_time" END_TIME = "end_time" TRIGGER_DISTANCES = "trigger_distances" @@ -15,9 +16,15 @@ END_DISTANCE = "end_distance" class PeopleCounter: def __init__(self, sensor: ToFSensor, maxTriggerDistanceInCm: int = 90) -> None: self.sensor = sensor - self.callbacks = {COUNTING_CB: [], TRIGGER_CB: [], CHANGE_CB: []} + self.callbacks = {COUNTING_CB: [], TRIGGER_CB: [], CHANGE_CB: [], MEASUREMENT_CB: []} self.maxTriggerDistance = maxTriggerDistanceInCm + def hookMeasurement(self, cb) -> None: + self.callbacks[MEASUREMENT_CB].append(cb) + + def unhookMeasurement(self, cb) -> None: + self.callbacks[MEASUREMENT_CB].remove(cb) + def hookCounting(self, cb) -> None: self.callbacks[COUNTING_CB].append(cb) @@ -54,6 +61,9 @@ class PeopleCounter: distance: float = self.sensor.getDistance() changed: bool = self.updateState(direction, distance) + th = threading.Thread(target=self.handleMeasurementCallbacks, args=(direction, distance)) + th.start() + if changed: countChange: int = self.getCountChange(self.directionState) @@ -133,6 +143,10 @@ class PeopleCounter: #! TODO: Should be based on the distance from the ground, not from the sensor return distance <= self.maxTriggerDistance + def handleMeasurementCallbacks(self, direction: Directions, distance: float) -> None: + for cb in self.callbacks[MEASUREMENT_CB]: + cb(direction, distance) + def handleCallbacks(self, countChange: int): self.handleChangeCallbacks(countChange) self.handleCountingCallbacks(countChange) diff --git a/src/setup.py b/src/setup.py index 8c319ac..8714a37 100644 --- a/src/setup.py +++ b/src/setup.py @@ -1,9 +1,10 @@ from sensors import VL53L1XSensor, VL53L3CXSensor, PeopleCounter +from statistics.debug_logging import register_debug_logger from datetime import time import logging LOG_FILE_PATH = "log.txt" # Path for logs -logging.getLogger().setLevel(logging.INFO) +logging.getLogger().setLevel(logging.DEBUG) # If the distance (in cm) is lower or equal to this value, the people counter will trigger MAX_TRIGGER_DISTANCE = 130 @@ -32,3 +33,5 @@ hue_conf = { sensor = VL53L1XSensor() counter: PeopleCounter = PeopleCounter(sensor, MAX_TRIGGER_DISTANCE) # Sensor object + +register_debug_logger(counter) diff --git a/src/statistics/debug_logging.py b/src/statistics/debug_logging.py new file mode 100644 index 0000000..b806105 --- /dev/null +++ b/src/statistics/debug_logging.py @@ -0,0 +1,23 @@ +from ..sensors.tof_sensor import Directions +from ..sensors.people_counter import PeopleCounter +import logging +import json + +def debug_log_change(countChange: int, directionState: dict) -> None: + json_state = json.dumps(directionState) + logging.debug(f"CHANGE;Count Change;{str(countChange)};Direction State;{json_state};") + +def debug_log_trigger(triggerState: dict) -> None: + logging.debug(f"TRIGGER;Inside Triggered;{str(triggerState[Directions.INSIDE])};Outside Triggered;{str(triggerState[Directions.OUTSIDE])};") + +def debug_log_counting(countChange: int) -> None: + logging.debug(f"COUNTING;Count Change;{str(countChange)};") + +def debug_log_measurement(direction: Directions, distance: float) -> None: + logging.debug(f"MEASUREMENT;Direction;{str(direction)};Distance;{str(distance)};") + +def register_debug_logger(counter: PeopleCounter) -> None: + counter.hookChange(debug_log_change) + counter.hookCounting(debug_log_counting) + counter.hookTrigger(debug_log_trigger) + counter.hookMeasurement(debug_log_measurement) \ No newline at end of file