split config into its own file and add threading lock

This commit is contained in:
petrucci4prez 2017-06-03 17:13:45 -04:00
parent d6356bbfc5
commit c543cebf57
4 changed files with 35 additions and 28 deletions

View File

@ -2,30 +2,11 @@
Various helper functions and classes
'''
import time, yaml, os
import time, os
from subprocess import check_output, DEVNULL, CalledProcessError
from threading import Event
from exceptionThreading import ExceptionThread
class ConfigFile():
'''
Presents a config yaml file as a dict-like object
'''
def __init__(self, path):
self._path = path
with open(self._path, 'r') as f:
self._dict = yaml.safe_load(f)
def __getitem__(self, key):
return self._dict[key]
def __setitem__(self, key, value):
self._dict[key] = value
def sync(self):
with open(self._path, 'w') as f:
yaml.dump(self._dict, f, default_flow_style=False)
class CountdownTimer(ExceptionThread):
'''
Launches thread which self terminates after some time (given in seconds).

27
config.py Normal file
View File

@ -0,0 +1,27 @@
'''
Presents an interface for yaml files as a dict-like object
'''
import yaml
from threading import Lock
class _ConfigFile():
def __init__(self, path):
self._path = path
self._lock = Lock()
with open(self._path, 'r') as f:
self._dict = yaml.safe_load(f)
def __getitem__(self, key):
return self._dict[key]
def __setitem__(self, key, value):
with self._lock:
self._dict[key] = value
self._sync()
def _sync(self):
with open(self._path, 'w') as f:
yaml.dump(self._dict, f, default_flow_style=False)
configFile = _ConfigFile('config.yaml')

View File

@ -1,5 +1,5 @@
import logging, time
from auxilary import ConfigFile
from config import configFile
from exceptionThreading import async
from smtplib import SMTP
from datetime import datetime
@ -8,7 +8,7 @@ from email.mime.text import MIMEText
logger = logging.getLogger(__name__)
gmail = ConfigFile('config.yaml')['gmail']
gmail = configFile['gmail']
def _getNextDate():
m = datetime.now().month + 1

View File

@ -4,7 +4,8 @@ from threading import Lock
from functools import partial
from collections import namedtuple
from auxilary import CountdownTimer, ConfigFile, resetUSBDevice
from auxilary import CountdownTimer, resetUSBDevice
from config import configFile
from sensors import setupDoorSensor, setupMotionSensor
from gmail import intruderAlert
from listeners import KeypadListener, PipeListener
@ -69,7 +70,6 @@ class State:
class StateMachine:
def __init__(self):
self.soundLib = SoundLib()
self._cfg = ConfigFile('config.yaml')
def startTimer(t, sound):
self._timer = CountdownTimer(t, partial(self.selectState, SIGNALS.TIMOUT), sound)
@ -110,7 +110,7 @@ class StateMachine:
)
)
self.currentState = getattr(self.states, self._cfg['state'])
self.currentState = getattr(self.states, configFile['state'])
self.transitionTable = {
(self.states.disarmed, SIGNALS.ARM): self.states.disarmedCountdown,
@ -191,15 +191,14 @@ class StateMachine:
self.currentState.entry()
def selectState(self, signal):
with self._lock
with self._lock:
nextState = self.currentState.next(signal)
if nextState != self.currentState:
self.currentState.exit()
self.currentState = nextState
self.currentState.entry()
self._cfg['state'] = self.currentState.name
self._cfg.sync()
configFile['state'] = self.currentState.name
logger.info('state changed to %s', self.currentState)