remove transition table

This commit is contained in:
petrucci4prez 2017-06-10 01:13:51 -04:00
parent b35aa83b5b
commit eab05e05c4
1 changed files with 43 additions and 43 deletions

View File

@ -1,9 +1,8 @@
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import time, logging, enum import time, logging, enum, weakref
from threading import Lock from threading import Lock
from functools import partial from functools import partial
from collections import namedtuple from collections import namedtuple
from enum import Enum, auto
from auxilary import CountdownTimer, resetUSBDevice from auxilary import CountdownTimer, resetUSBDevice
from config import stateFile from config import stateFile
@ -25,38 +24,37 @@ class SIGNALS(enum.Enum):
TRIGGER = enum.auto() TRIGGER = enum.auto()
class State: class State:
def __init__(self, stateMachine, name, entryCallbacks=[], exitCallbacks=[], sound=None): def __init__(self, sfx, name, entryCallbacks=[], exitCallbacks=[], sound=None):
self.stateMachine = stateMachine
self.name = name self.name = name
self.entryCallbacks = entryCallbacks self.entryCallbacks = entryCallbacks
self.exitCallbacks = exitCallbacks self.exitCallbacks = exitCallbacks
self._transTbl = {}
sfx = stateMachine.soundLib.soundEffects #~ self._sound = sfx[name] if not sound and name in sfx else sound
self._sound = sfx[name] if not sound and name in sfx else sound
def entry(self): def entry(self):
logger.info('entering ' + self.name) logger.info('entering ' + self.name)
if self._sound: #~ if self._sound:
self._sound.play() #~ self._sound.play()
for c in self.entryCallbacks: for c in self.entryCallbacks:
c() c()
def exit(self): def exit(self):
logger.info('exiting ' + self.name) logger.info('exiting ' + self.name)
if self._sound: #~ if self._sound:
self._sound.stop() #~ self._sound.stop()
for c in self.exitCallbacks: for c in self.exitCallbacks:
c() c()
def next(self, signal): def next(self, signal):
if signal in SIGNALS: if signal in SIGNALS:
s = (self, signal) return self if signal not in self._transTbl else self._transTbl[signal]
t = self.stateMachine.transitionTable
return self if s not in t else t[s]
else: else:
raise Exception('Illegal signal') raise Exception('Illegal signal')
def addTransition(self, signal, state):
self._transTbl[signal] = weakref.ref(state)
def __str__(self): def __str__(self):
return self.name return self.name
@ -115,29 +113,31 @@ class StateMachine:
stateObjs = [ stateObjs = [
State( State(
self, sfx,
name = 'disarmed', name = 'disarmed',
entryCallbacks = [partial(self.LED.setBlink, False)] entryCallbacks = [partial(self.LED.setBlink, False)]
), ),
State( State(
self, sfx,
name = 'disarmedCountdown', name = 'disarmedCountdown',
entryCallbacks = [blinkingLED, partial(startTimer, 30, sfx['disarmedCountdown'])], #~ entryCallbacks = [blinkingLED, partial(startTimer, 30, sfx['disarmedCountdown'])],
entryCallbacks = [blinkingLED, partial(startTimer, 30)],
exitCallbacks = [stopTimer] exitCallbacks = [stopTimer]
), ),
State( State(
self, sfx,
name = 'armed', name = 'armed',
entryCallbacks = [blinkingLED] entryCallbacks = [blinkingLED]
), ),
State( State(
self, sfx,
name = 'armedCountdown', name = 'armedCountdown',
entryCallbacks = [blinkingLED, partial(startTimer, 30, sfx['armedCountdown'])], #~ entryCallbacks = [blinkingLED, partial(startTimer, 30, sfx['armedCountdown'])],
entryCallbacks = [blinkingLED, partial(startTimer, 30)],
exitCallbacks = [stopTimer] exitCallbacks = [stopTimer]
), ),
State( State(
self, sfx,
name = 'triggered', name = 'triggered',
entryCallbacks = [blinkingLED, intruderAlert] entryCallbacks = [blinkingLED, intruderAlert]
) )
@ -146,29 +146,29 @@ class StateMachine:
for s in stateObjs: for s in stateObjs:
s.entryCallbacks.append(self.keypadListener.resetBuffer) s.entryCallbacks.append(self.keypadListener.resetBuffer)
self.states = namedtuple('States', [s.name for s in stateObjs])(*stateObjs) self.states = s = namedtuple('States', [s.name for s in stateObjs])(*stateObjs)
s.disarmed.addTransition( SIGNALS.ARM, s.disarmedCountdown)
s.disarmed.addTransition( SIGNALS.INSTANT_ARM, s.armed)
s.disarmedCountdown.addTransition( SIGNALS.DISARM, s.disarmed)
s.disarmedCountdown.addTransition( SIGNALS.TIMOUT, s.armed)
s.disarmedCountdown.addTransition( SIGNALS.INSTANT_ARM, s.armed)
s.armed.addTransition( SIGNALS.DISARM, s.disarmed)
s.armed.addTransition( SIGNALS.TRIGGER, s.armedCountdown)
s.armedCountdown.addTransition( SIGNALS.DISARM, s.disarmed)
s.armedCountdown.addTransition( SIGNALS.TIMOUT, s.triggered)
s.armedCountdown.addTransition( SIGNALS.ARM, s.armed)
s.armedCountdown.addTransition( SIGNALS.INSTANT_ARM, s.armed)
s.triggered.addTransition( SIGNALS.DISARM, s.disarmed)
s.triggered.addTransition( SIGNALS.ARM, s.armed)
s.triggered.addTransition( SIGNALS.INSTANT_ARM, s.armed)
self.currentState = getattr(self.states, stateFile['state']) self.currentState = getattr(self.states, stateFile['state'])
self.transitionTable = {
(self.states.disarmed, SIGNALS.ARM): self.states.disarmedCountdown,
(self.states.disarmed, SIGNALS.INSTANT_ARM): self.states.armed,
(self.states.disarmedCountdown, SIGNALS.DISARM): self.states.disarmed,
(self.states.disarmedCountdown, SIGNALS.TIMOUT): self.states.armed,
(self.states.disarmedCountdown, SIGNALS.INSTANT_ARM): self.states.armed,
(self.states.armed, SIGNALS.DISARM): self.states.disarmed,
(self.states.armed, SIGNALS.TRIGGER): self.states.armedCountdown,
(self.states.armedCountdown, SIGNALS.DISARM): self.states.disarmed,
(self.states.armedCountdown, SIGNALS.ARM): self.states.armed,
(self.states.armedCountdown, SIGNALS.TIMOUT): self.states.triggered,
(self.states.triggered, SIGNALS.DISARM): self.states.disarmed,
(self.states.triggered, SIGNALS.ARM): self.states.armed,
}
def start(self): def start(self):
resetUSBDevice('1-1', logger) resetUSBDevice('1-1', logger)