Changed activity callback to trigger callback
This commit is contained in:
parent
ee6d000338
commit
4cb31afdb8
1 changed files with 20 additions and 12 deletions
|
@ -5,7 +5,7 @@ import threading
|
||||||
|
|
||||||
|
|
||||||
COUNTING_CB = "counting"
|
COUNTING_CB = "counting"
|
||||||
ACTIVITY_CB = "activity"
|
TRIGGER_CB = "trigger"
|
||||||
CHANGE_CB = "changes"
|
CHANGE_CB = "changes"
|
||||||
START_TIME = "start"
|
START_TIME = "start"
|
||||||
END_TIME = "end"
|
END_TIME = "end"
|
||||||
|
@ -14,7 +14,7 @@ END_TIME = "end"
|
||||||
class PeopleCounter ():
|
class PeopleCounter ():
|
||||||
def __init__(self, sensor: ToFSensor) -> None:
|
def __init__(self, sensor: ToFSensor) -> None:
|
||||||
self.sensor = sensor
|
self.sensor = sensor
|
||||||
self.callbacks = {COUNTING_CB: [], ACTIVITY_CB: [], CHANGE_CB: []}
|
self.callbacks = {COUNTING_CB: [], TRIGGER_CB: [], CHANGE_CB: []}
|
||||||
self.maxTriggerDistance = 120 # In cm
|
self.maxTriggerDistance = 120 # In cm
|
||||||
|
|
||||||
def hookCounting(self, cb) -> None:
|
def hookCounting(self, cb) -> None:
|
||||||
|
@ -23,11 +23,11 @@ class PeopleCounter ():
|
||||||
def unhookCounting(self, cb) -> None:
|
def unhookCounting(self, cb) -> None:
|
||||||
self.callbacks[COUNTING_CB].remove(cb)
|
self.callbacks[COUNTING_CB].remove(cb)
|
||||||
|
|
||||||
def hookActivity(self, cb) -> None:
|
def hookTrigger(self, cb) -> None:
|
||||||
self.callbacks[ACTIVITY_CB].append(cb)
|
self.callbacks[TRIGGER_CB].append(cb)
|
||||||
|
|
||||||
def unhookActivity(self, cb) -> None:
|
def unhookTrigger(self, cb) -> None:
|
||||||
self.callbacks[ACTIVITY_CB].remove(cb)
|
self.callbacks[TRIGGER_CB].remove(cb)
|
||||||
|
|
||||||
def hookChange(self, cb) -> None:
|
def hookChange(self, cb) -> None:
|
||||||
self.callbacks[CHANGE_CB].append(cb)
|
self.callbacks[CHANGE_CB].append(cb)
|
||||||
|
@ -57,13 +57,13 @@ class PeopleCounter ():
|
||||||
triggered: bool = self.isTriggerDistance(distance)
|
triggered: bool = self.isTriggerDistance(distance)
|
||||||
changed: bool = self.updateState(direction, triggered)
|
changed: bool = self.updateState(direction, triggered)
|
||||||
|
|
||||||
if triggered:
|
|
||||||
self.handleActivityCallbacks(direction)
|
|
||||||
|
|
||||||
if changed:
|
if changed:
|
||||||
countChange: int = self.getCountChange(self.directionState)
|
countChange: int = self.getCountChange(self.directionState)
|
||||||
|
|
||||||
|
# Hooks
|
||||||
self.handleChangeCallbacks(countChange)
|
self.handleChangeCallbacks(countChange)
|
||||||
self.handleCountingCallbacks(countChange)
|
self.handleCountingCallbacks(countChange)
|
||||||
|
self.handleTriggerCallbacks()
|
||||||
|
|
||||||
# Reset records
|
# Reset records
|
||||||
self.directionState = self.getInitialDirectionState()
|
self.directionState = self.getInitialDirectionState()
|
||||||
|
@ -140,9 +140,17 @@ class PeopleCounter ():
|
||||||
th = threading.Thread(target=cb, args=(countChange,))
|
th = threading.Thread(target=cb, args=(countChange,))
|
||||||
th.start()
|
th.start()
|
||||||
|
|
||||||
def handleActivityCallbacks(self, direction: Directions) -> None:
|
def handleTriggerCallbacks(self) -> None:
|
||||||
for cb in self.callbacks[ACTIVITY_CB]:
|
insideTrigger = len(self.directionState[Directions.INSIDE]) > 0 and self.directionState[Directions.INSIDE][END_TIME] is None
|
||||||
th = threading.Thread(target=cb, args=(direction,))
|
outsideTrigger = len(self.directionState[Directions.OUTSIDE]) > 0 and self.directionState[Directions.OUTSIDE][END_TIME] is None
|
||||||
|
|
||||||
|
triggerState = {
|
||||||
|
Directions.INSIDE: insideTrigger,
|
||||||
|
Directions.OUTSIDE: outsideTrigger
|
||||||
|
}
|
||||||
|
|
||||||
|
for cb in self.callbacks[TRIGGER_CB]:
|
||||||
|
th = threading.Thread(target=cb, args=(triggerState,))
|
||||||
th.start()
|
th.start()
|
||||||
|
|
||||||
def handleChangeCallbacks(self, countChange: int) -> None:
|
def handleChangeCallbacks(self, countChange: int) -> None:
|
||||||
|
|
Loading…
Reference in a new issue