add watchdog for pipeListener

This commit is contained in:
petrucci4prez 2017-06-05 00:33:19 -04:00
parent 6ab3ecbf34
commit 59dee9609e
2 changed files with 42 additions and 12 deletions

View File

@ -4,10 +4,15 @@ from exceptionThreading import ExceptionThread
from evdev import InputDevice, ecodes
from select import select
from auxilary import waitForPath
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import stateMachine
logger = logging.getLogger(__name__)
inotifyLogger = logging.getLogger('watchdog.observers.inotify_buffer')
inotifyLogger.setLevel(logging.WARNING)
class KeypadListener(ExceptionThread):
def __init__(self, stateMachine, callbackDisarm, callbackArm, soundLib, passwd):
@ -114,11 +119,21 @@ class KeypadListener(ExceptionThread):
except AttributeError:
pass
class ActionHandler(FileSystemEventHandler):
def __init__(self, action):
self._action = action
def on_any_event(self, event):
self._action()
class PipeListener(ExceptionThread):
_rootDir = '/tmp'
_pipePermissions = 0o0777
def __init__(self, callback, path):
self._path = path
self._path = os.path.join(self._rootDir, path)
self._stopper = Event()
self._makeFIFO()
def listen():
while not self._stopper.isSet():
@ -131,25 +146,40 @@ class PipeListener(ExceptionThread):
except BlockingIOError:
pass
except FileNotFoundError:
# TODO: this might be easier with a watchdog
self._makeFIFO()
pass
finally:
time.sleep(0.1)
self._observer = Observer()
self._handler = ActionHandler(self._makeFIFO)
self._observer.schedule(self._handler, path=os.path.dirname(self._path), recursive=False)
self._makeFIFO()
self._observer.start()
super().__init__(target=listen, daemon=False)
self.start()
logger.debug('Started pipe listener at path %s', self._path)
def _makeFIFO(self):
if os.path.exists(self._path):
if not stat.S_ISFIFO(os.stat(self._path)[0]):
logger.warn('%s exists but is not a pipe. Deleting', self._path)
try:
st = os.stat(self._path)
if not stat.S_ISFIFO(st.st_mode):
logger.warn('%s exists but is not a pipe. Deleting and replacing', self._path)
os.remove(self._path)
os.mkfifo(self._path)
else:
os.chmod(self._path, self._pipePermissions)
elif st.st_mode % 0o10000 != self._pipePermissions:
logger.warn('%s is a valid pipe but has incorrect permissions. Changing to %s',
self._path, self._pipePermissions)
os.chmod(self._path, self._pipePermissions)
except FileNotFoundError:
pipeDir = os.path.dirname(self._path)
if not os.path.exists(pipeDir):
logger.warn('%s does not exist. Creating', pipeDir)
os.makedirs(pipeDir)
logger.warn('%s does not exist. Creating', self._path)
os.mkfifo(self._path)
os.chmod(self._path, 0o0777)
os.chmod(self._path, self._pipePermissions)
def __del__(self):
self._stopper.set()

View File

@ -175,7 +175,7 @@ class StateMachine:
self.secretListener = PipeListener(
callback = secretCallback,
path = '/tmp/secret'
path = 'pyledriver/pipes/secret'
)
self.keypadListener = KeypadListener(