diff --git a/doorlockd b/doorlockd index 42fa6ce..5660a6a 100755 --- a/doorlockd +++ b/doorlockd @@ -24,7 +24,7 @@ from configparser import ConfigParser from os.path import abspath, join from pydoorlock.Authenticator import Authenticator -from pydoorlock.WebApp import webapp_run +from pydoorlock.WebApp import webapp_run, emit_doorstate from pydoorlock.Doorlock import DoorlockResponse, DoorHandler SYSCONFDIR = '.' @@ -63,9 +63,10 @@ cfg = Config(SYSCONFDIR) class Logic: - def __init__(self, cfg, sounds_prefix, scripts_prefix): + def __init__(self, cfg, sounds_prefix, scripts_prefix, callback): self.auth = Authenticator(cfg) self.door_handler = DoorHandler(cfg, sounds_prefix, scripts_prefix) + self.door_handler.register_callback(callback) def request(self, state, credentials): err = self.auth.try_auth(credentials) @@ -88,7 +89,7 @@ if __name__ == '__main__': sounds_prefix = join(root_prefix, 'sounds') scripts_prefix = join(root_prefix, 'scripts') - logic = Logic(cfg, sounds_prefix, scripts_prefix) + logic = Logic(cfg, sounds_prefix, scripts_prefix, emit_doorstate) static_folder = abspath(join(root_prefix, 'static')) template_folder = abspath(join(root_prefix, 'templates')) diff --git a/pydoorlock/Doorlock.py b/pydoorlock/Doorlock.py index b69015f..d75f1c7 100644 --- a/pydoorlock/Doorlock.py +++ b/pydoorlock/Doorlock.py @@ -99,6 +99,8 @@ class DoorHandler: wave_zonk = 'zonk.wav' def __init__(self, cfg, sounds_prefix, scripts_prefix): + self._callback = None + self.sounds = cfg.boolean('SOUNDS') if self.sounds: self.sounds_prefix = sounds_prefix @@ -130,18 +132,18 @@ class DoorHandler: if rx == DoorHandler.BUTTON_CLOSE: self.close() log.info('Closed due to Button press') - #emit_status(LogicResponse.ButtonLock) + self.invoke_callback(DoorlockResponse.ButtonLock) elif rx == DoorHandler.BUTTON_OPEN: self.open() log.info('Opened due to Button press') - #emit_status(LogicResponse.ButtonUnlock) + self.invoke_callback(DoorlockResponse.ButtonUnlock) elif rx == DoorHandler.BUTTON_PRESENT: self.present() log.info('Present due to Button press') - #emit_status(LogicResponse.ButtonPresent) + self.invoke_callback(DoorlockResponse.ButtonPresent) elif rx == DoorHandler.CMD_EMERGENCY_SWITCH: log.warning('Emergency unlock') - #emit_status(LogicResponse.EmergencyUnlock) + self.invoke_callback(DoorlockResponse.EmergencyUnlock) else: log.error('Received unknown message "%s" from AVR' % rx) @@ -195,7 +197,7 @@ class DoorHandler: err = self.open() self.sound_helper(old_state, self.state, False) - #emit_doorstate() + self.invoke_callback(err) return err def sound_helper(self, old_state, new_state, button): @@ -230,3 +232,10 @@ class DoorHandler: return log.info('Starting hook %s' % script) Popen(['nohup', join(self.scripts_prefix, script)]) + + def register_callback(self, callback): + self._callback = callback + + def invoke_callback(self, val): + if self._callback: + self._callback(val) diff --git a/pydoorlock/WebApp.py b/pydoorlock/WebApp.py index 21907bf..ea76b46 100644 --- a/pydoorlock/WebApp.py +++ b/pydoorlock/WebApp.py @@ -15,9 +15,13 @@ webapp = Flask(__name__) socketio = SocketIO(webapp, async_mode='threading') -def emit_doorstate(): +def emit_doorstate(response=None): state = logic.state - socketio.emit('status', {'led': state.to_img(), 'message': str(state)}) + if response: + message = str(response) + else: + message = str(state) + socketio.emit('status', {'led': state.to_img(), 'message': message}) class AuthenticationForm(FlaskForm):