1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 10:24:26 +01:00

pydoorlock: doorlockd: make callbacks running again

Signed-off-by: Ralf Ramsauer <ralf@binary-kitchen.de>
This commit is contained in:
Ralf Ramsauer 2018-10-09 01:06:59 +02:00
parent 9e94d858ec
commit 92b791a835
3 changed files with 24 additions and 10 deletions

View File

@ -24,7 +24,7 @@ from configparser import ConfigParser
from os.path import abspath, join from os.path import abspath, join
from pydoorlock.Authenticator import Authenticator 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 from pydoorlock.Doorlock import DoorlockResponse, DoorHandler
SYSCONFDIR = '.' SYSCONFDIR = '.'
@ -63,9 +63,10 @@ cfg = Config(SYSCONFDIR)
class Logic: class Logic:
def __init__(self, cfg, sounds_prefix, scripts_prefix): def __init__(self, cfg, sounds_prefix, scripts_prefix, callback):
self.auth = Authenticator(cfg) self.auth = Authenticator(cfg)
self.door_handler = DoorHandler(cfg, sounds_prefix, scripts_prefix) self.door_handler = DoorHandler(cfg, sounds_prefix, scripts_prefix)
self.door_handler.register_callback(callback)
def request(self, state, credentials): def request(self, state, credentials):
err = self.auth.try_auth(credentials) err = self.auth.try_auth(credentials)
@ -88,7 +89,7 @@ if __name__ == '__main__':
sounds_prefix = join(root_prefix, 'sounds') sounds_prefix = join(root_prefix, 'sounds')
scripts_prefix = join(root_prefix, 'scripts') 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')) static_folder = abspath(join(root_prefix, 'static'))
template_folder = abspath(join(root_prefix, 'templates')) template_folder = abspath(join(root_prefix, 'templates'))

View File

@ -99,6 +99,8 @@ class DoorHandler:
wave_zonk = 'zonk.wav' wave_zonk = 'zonk.wav'
def __init__(self, cfg, sounds_prefix, scripts_prefix): def __init__(self, cfg, sounds_prefix, scripts_prefix):
self._callback = None
self.sounds = cfg.boolean('SOUNDS') self.sounds = cfg.boolean('SOUNDS')
if self.sounds: if self.sounds:
self.sounds_prefix = sounds_prefix self.sounds_prefix = sounds_prefix
@ -130,18 +132,18 @@ class DoorHandler:
if rx == DoorHandler.BUTTON_CLOSE: if rx == DoorHandler.BUTTON_CLOSE:
self.close() self.close()
log.info('Closed due to Button press') log.info('Closed due to Button press')
#emit_status(LogicResponse.ButtonLock) self.invoke_callback(DoorlockResponse.ButtonLock)
elif rx == DoorHandler.BUTTON_OPEN: elif rx == DoorHandler.BUTTON_OPEN:
self.open() self.open()
log.info('Opened due to Button press') log.info('Opened due to Button press')
#emit_status(LogicResponse.ButtonUnlock) self.invoke_callback(DoorlockResponse.ButtonUnlock)
elif rx == DoorHandler.BUTTON_PRESENT: elif rx == DoorHandler.BUTTON_PRESENT:
self.present() self.present()
log.info('Present due to Button press') log.info('Present due to Button press')
#emit_status(LogicResponse.ButtonPresent) self.invoke_callback(DoorlockResponse.ButtonPresent)
elif rx == DoorHandler.CMD_EMERGENCY_SWITCH: elif rx == DoorHandler.CMD_EMERGENCY_SWITCH:
log.warning('Emergency unlock') log.warning('Emergency unlock')
#emit_status(LogicResponse.EmergencyUnlock) self.invoke_callback(DoorlockResponse.EmergencyUnlock)
else: else:
log.error('Received unknown message "%s" from AVR' % rx) log.error('Received unknown message "%s" from AVR' % rx)
@ -195,7 +197,7 @@ class DoorHandler:
err = self.open() err = self.open()
self.sound_helper(old_state, self.state, False) self.sound_helper(old_state, self.state, False)
#emit_doorstate() self.invoke_callback(err)
return err return err
def sound_helper(self, old_state, new_state, button): def sound_helper(self, old_state, new_state, button):
@ -230,3 +232,10 @@ class DoorHandler:
return return
log.info('Starting hook %s' % script) log.info('Starting hook %s' % script)
Popen(['nohup', join(self.scripts_prefix, 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)

View File

@ -15,9 +15,13 @@ webapp = Flask(__name__)
socketio = SocketIO(webapp, async_mode='threading') socketio = SocketIO(webapp, async_mode='threading')
def emit_doorstate(): def emit_doorstate(response=None):
state = logic.state 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): class AuthenticationForm(FlaskForm):