differentiate b/t readonly and readwrite config files
This commit is contained in:
parent
c543cebf57
commit
1a2c82da33
26
config.py
26
config.py
|
@ -5,23 +5,33 @@ Presents an interface for yaml files as a dict-like object
|
||||||
import yaml
|
import yaml
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
class _ConfigFile():
|
class _ReadOnlyFile():
|
||||||
|
'''
|
||||||
|
Opens a yaml file for reading. Intended for config files.
|
||||||
|
'''
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
self._path = path
|
self._path = path
|
||||||
self._lock = Lock()
|
|
||||||
with open(self._path, 'r') as f:
|
with open(self._path, 'r') as f:
|
||||||
self._dict = yaml.safe_load(f)
|
self._dict = yaml.safe_load(f)
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
return self._dict[key]
|
return self._dict[key]
|
||||||
|
|
||||||
|
class _ReadWriteFile(_ReadOnlyFile):
|
||||||
|
'''
|
||||||
|
Same as above but adds write functionality. Intended for files that retain
|
||||||
|
program state so that it may return to the same state when recovering from
|
||||||
|
a crash (eg someone can't crash the system to disarm it)
|
||||||
|
'''
|
||||||
|
def __init__(self, path):
|
||||||
|
super().__init__(path)
|
||||||
|
self._lock = Lock()
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
with self._lock:
|
with self._lock:
|
||||||
self._dict[key] = value
|
self._dict[key] = value
|
||||||
self._sync()
|
with open(self._path, 'w') as f:
|
||||||
|
yaml.dump(self._dict, f, default_flow_style=False)
|
||||||
def _sync(self):
|
|
||||||
with open(self._path, 'w') as f:
|
|
||||||
yaml.dump(self._dict, f, default_flow_style=False)
|
|
||||||
|
|
||||||
configFile = _ConfigFile('config.yaml')
|
configFile = _ReadOnlyFile('config/pyledriver.yaml')
|
||||||
|
stateFile = _ReadWriteFile('config/state.yaml')
|
||||||
|
|
|
@ -3,4 +3,3 @@ gmail:
|
||||||
recipientList:
|
recipientList:
|
||||||
- natedwarshuis@gmail.com
|
- natedwarshuis@gmail.com
|
||||||
username: natedwarshuis@gmail.com
|
username: natedwarshuis@gmail.com
|
||||||
state: disarmed
|
|
|
@ -5,7 +5,7 @@ from functools import partial
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from auxilary import CountdownTimer, resetUSBDevice
|
from auxilary import CountdownTimer, resetUSBDevice
|
||||||
from config import configFile
|
from config import stateFile
|
||||||
from sensors import setupDoorSensor, setupMotionSensor
|
from sensors import setupDoorSensor, setupMotionSensor
|
||||||
from gmail import intruderAlert
|
from gmail import intruderAlert
|
||||||
from listeners import KeypadListener, PipeListener
|
from listeners import KeypadListener, PipeListener
|
||||||
|
@ -110,7 +110,7 @@ class StateMachine:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.currentState = getattr(self.states, configFile['state'])
|
self.currentState = getattr(self.states, stateFile['state'])
|
||||||
|
|
||||||
self.transitionTable = {
|
self.transitionTable = {
|
||||||
(self.states.disarmed, SIGNALS.ARM): self.states.disarmedCountdown,
|
(self.states.disarmed, SIGNALS.ARM): self.states.disarmedCountdown,
|
||||||
|
@ -198,7 +198,7 @@ class StateMachine:
|
||||||
self.currentState = nextState
|
self.currentState = nextState
|
||||||
self.currentState.entry()
|
self.currentState.entry()
|
||||||
|
|
||||||
configFile['state'] = self.currentState.name
|
stateFile['state'] = self.currentState.name
|
||||||
|
|
||||||
logger.info('state changed to %s', self.currentState)
|
logger.info('state changed to %s', self.currentState)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue