Added distances to direction state

This commit is contained in:
Maximilian Giller 2021-12-13 18:15:48 +01:00
parent 5a249f7247
commit 298721c820

View file

@ -7,8 +7,10 @@ import threading
COUNTING_CB = "counting" COUNTING_CB = "counting"
TRIGGER_CB = "trigger" TRIGGER_CB = "trigger"
CHANGE_CB = "changes" CHANGE_CB = "changes"
START_TIME = "start" START_TIME = "start_time"
END_TIME = "end" END_TIME = "end_time"
TRIGGER_DISTANCES = "trigger_distances"
END_DISTANCE = "end_distance"
class PeopleCounter (): class PeopleCounter ():
@ -54,8 +56,7 @@ class PeopleCounter ():
self.sensor.setDirection(direction) self.sensor.setDirection(direction)
distance: float = self.sensor.getDistance() distance: float = self.sensor.getDistance()
triggered: bool = self.isTriggerDistance(distance) changed: bool = self.updateState(direction, distance)
changed: bool = self.updateState(direction, triggered)
if changed: if changed:
countChange: int = self.getCountChange(self.directionState) countChange: int = self.getCountChange(self.directionState)
@ -160,7 +161,9 @@ class PeopleCounter ():
def isDirectionTriggered(self, direction: Directions) -> bool: def isDirectionTriggered(self, direction: Directions) -> bool:
return len(self.directionState[direction]) > 0 and self.directionState[direction][-1][END_TIME] is None return len(self.directionState[direction]) > 0 and self.directionState[direction][-1][END_TIME] is None
def updateState(self, direction: Directions, triggered: bool) -> bool: def updateState(self, direction: Directions, distance: float) -> bool:
triggered: bool = self.isTriggerDistance(distance)
previouslyTriggered = False previouslyTriggered = False
if len(self.directionState[direction]) > 0: if len(self.directionState[direction]) > 0:
previouslyTriggered = self.directionState[direction][-1][END_TIME] is None previouslyTriggered = self.directionState[direction][-1][END_TIME] is None
@ -169,12 +172,18 @@ class PeopleCounter ():
# Set as new beginning for this direction # Set as new beginning for this direction
self.directionState[direction].append({ self.directionState[direction].append({
START_TIME: datetime.now(), START_TIME: datetime.now(),
END_TIME: None END_TIME: None,
TRIGGER_DISTANCES: [distance],
END_DISTANCE: None
}) })
return True return True
elif not triggered and previouslyTriggered: elif not triggered and previouslyTriggered:
# Set as end for this direction # Set as end for this direction
self.directionState[direction][-1][END_TIME] = datetime.now() self.directionState[direction][-1][END_TIME] = datetime.now()
self.directionState[direction][-1][END_DISTANCE] = distance
return True return True
elif previouslyTriggered:
# Add distance at least
self.directionState[direction][-1][TRIGGER_DISTANCES].append(distance)
return False return False