add watchdog for pipeListener
This commit is contained in:
parent
6ab3ecbf34
commit
59dee9609e
50
listeners.py
50
listeners.py
|
@ -4,10 +4,15 @@ from exceptionThreading import ExceptionThread
|
||||||
from evdev import InputDevice, ecodes
|
from evdev import InputDevice, ecodes
|
||||||
from select import select
|
from select import select
|
||||||
from auxilary import waitForPath
|
from auxilary import waitForPath
|
||||||
|
from watchdog.observers import Observer
|
||||||
|
from watchdog.events import FileSystemEventHandler
|
||||||
import stateMachine
|
import stateMachine
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
inotifyLogger = logging.getLogger('watchdog.observers.inotify_buffer')
|
||||||
|
inotifyLogger.setLevel(logging.WARNING)
|
||||||
|
|
||||||
class KeypadListener(ExceptionThread):
|
class KeypadListener(ExceptionThread):
|
||||||
def __init__(self, stateMachine, callbackDisarm, callbackArm, soundLib, passwd):
|
def __init__(self, stateMachine, callbackDisarm, callbackArm, soundLib, passwd):
|
||||||
|
|
||||||
|
@ -114,11 +119,21 @@ class KeypadListener(ExceptionThread):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class ActionHandler(FileSystemEventHandler):
|
||||||
|
def __init__(self, action):
|
||||||
|
self._action = action
|
||||||
|
|
||||||
|
def on_any_event(self, event):
|
||||||
|
self._action()
|
||||||
|
|
||||||
class PipeListener(ExceptionThread):
|
class PipeListener(ExceptionThread):
|
||||||
|
|
||||||
|
_rootDir = '/tmp'
|
||||||
|
_pipePermissions = 0o0777
|
||||||
|
|
||||||
def __init__(self, callback, path):
|
def __init__(self, callback, path):
|
||||||
self._path = path
|
self._path = os.path.join(self._rootDir, path)
|
||||||
self._stopper = Event()
|
self._stopper = Event()
|
||||||
self._makeFIFO()
|
|
||||||
|
|
||||||
def listen():
|
def listen():
|
||||||
while not self._stopper.isSet():
|
while not self._stopper.isSet():
|
||||||
|
@ -131,25 +146,40 @@ class PipeListener(ExceptionThread):
|
||||||
except BlockingIOError:
|
except BlockingIOError:
|
||||||
pass
|
pass
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
# TODO: this might be easier with a watchdog
|
pass
|
||||||
self._makeFIFO()
|
|
||||||
finally:
|
finally:
|
||||||
time.sleep(0.1)
|
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)
|
super().__init__(target=listen, daemon=False)
|
||||||
self.start()
|
self.start()
|
||||||
logger.debug('Started pipe listener at path %s', self._path)
|
logger.debug('Started pipe listener at path %s', self._path)
|
||||||
|
|
||||||
def _makeFIFO(self):
|
def _makeFIFO(self):
|
||||||
if os.path.exists(self._path):
|
try:
|
||||||
if not stat.S_ISFIFO(os.stat(self._path)[0]):
|
st = os.stat(self._path)
|
||||||
logger.warn('%s exists but is not a pipe. Deleting', 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.remove(self._path)
|
||||||
os.mkfifo(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.mkfifo(self._path)
|
||||||
|
os.chmod(self._path, self._pipePermissions)
|
||||||
os.chmod(self._path, 0o0777)
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self._stopper.set()
|
self._stopper.set()
|
||||||
|
|
|
@ -175,7 +175,7 @@ class StateMachine:
|
||||||
|
|
||||||
self.secretListener = PipeListener(
|
self.secretListener = PipeListener(
|
||||||
callback = secretCallback,
|
callback = secretCallback,
|
||||||
path = '/tmp/secret'
|
path = 'pyledriver/pipes/secret'
|
||||||
)
|
)
|
||||||
|
|
||||||
self.keypadListener = KeypadListener(
|
self.keypadListener = KeypadListener(
|
||||||
|
|
Loading…
Reference in New Issue