Made hook execution predictable

This commit is contained in:
Maximilian Giller 2021-12-03 18:33:20 +01:00
parent 8b28cd0f7c
commit f2a77fae2b

View file

@ -61,9 +61,8 @@ class PeopleCounter ():
countChange: int = self.getCountChange(self.directionState) countChange: int = self.getCountChange(self.directionState)
# Hooks # Hooks
self.handleChangeCallbacks(countChange) th = threading.Thread(target=self.handleCallbacks, args=(self,countChange))
self.handleCountingCallbacks(countChange) th.start()
self.handleTriggerCallbacks()
# Reset records # Reset records
if countChange != 0: if countChange != 0:
@ -132,14 +131,18 @@ class PeopleCounter ():
#! TODO: Should be based on the distance from the ground, not them the sensor #! TODO: Should be based on the distance from the ground, not them the sensor
return distance <= self.maxTriggerDistance return distance <= self.maxTriggerDistance
def handleCallbacks(self, countChange: int):
self.handleChangeCallbacks(countChange)
self.handleCountingCallbacks(countChange)
self.handleTriggerCallbacks()
def handleCountingCallbacks(self, countChange: int) -> None: def handleCountingCallbacks(self, countChange: int) -> None:
# Only notify counting on actual count change # Only notify counting on actual count change
if countChange == 0: if countChange == 0:
return return
for cb in self.callbacks[COUNTING_CB]: for cb in self.callbacks[COUNTING_CB]:
th = threading.Thread(target=cb, args=(countChange,)) cb(countChange)
th.start()
def handleTriggerCallbacks(self) -> None: def handleTriggerCallbacks(self) -> None:
insideTrigger = len(self.directionState[Directions.INSIDE]) > 0 and self.directionState[Directions.INSIDE][-1][END_TIME] is None insideTrigger = len(self.directionState[Directions.INSIDE]) > 0 and self.directionState[Directions.INSIDE][-1][END_TIME] is None
@ -151,13 +154,11 @@ class PeopleCounter ():
} }
for cb in self.callbacks[TRIGGER_CB]: for cb in self.callbacks[TRIGGER_CB]:
th = threading.Thread(target=cb, args=(triggerState,)) cb(triggerState)
th.start()
def handleChangeCallbacks(self, countChange: int) -> None: def handleChangeCallbacks(self, countChange: int) -> None:
for cb in self.callbacks[CHANGE_CB]: for cb in self.callbacks[CHANGE_CB]:
th = threading.Thread(target=cb, args=(countChange, self.directionState)) cb(countChange, self.directionState)
th.start()
def updateState(self, direction: Directions, triggered: bool) -> bool: def updateState(self, direction: Directions, triggered: bool) -> bool:
previouslyTriggered = False previouslyTriggered = False