mirror of
https://github.com/binary-kitchen/doorlockd
synced 2024-10-31 22:47:05 +01:00
100 lines
2.7 KiB
Python
Executable File
100 lines
2.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
"""
|
|
Doorlockd -- Binary Kitchen's smart door opener
|
|
|
|
Copyright (c) Binary Kitchen e.V., 2018
|
|
|
|
Author:
|
|
Ralf Ramsauer <ralf@binary-kitchen.de>
|
|
|
|
This work is licensed under the terms of the GNU GPL, version 2. See
|
|
the LICENSE file in the top-level directory.
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
details.
|
|
"""
|
|
|
|
import logging
|
|
import sys
|
|
|
|
from configparser import ConfigParser
|
|
from os.path import abspath, join
|
|
|
|
from pydoorlock.Authenticator import Authenticator
|
|
from pydoorlock.WebApp import webapp_run, emit_doorstate
|
|
from pydoorlock.Doorlock import DoorlockResponse, DoorHandler
|
|
|
|
SYSCONFDIR = '.'
|
|
PREFIX = '.'
|
|
|
|
__author__ = 'Ralf Ramsauer'
|
|
__copyright = 'Copyright (c) Ralf Ramsauer, 2018'
|
|
__license__ = 'GPLv2'
|
|
__email__ = 'ralf@binary-kitchen.de'
|
|
__status__ = 'Development'
|
|
__maintainer__ = 'Ralf Ramsauer'
|
|
__version__ = '2.0-rc2'
|
|
|
|
log_level = logging.DEBUG
|
|
date_fmt = '%Y-%m-%d %H:%M:%S'
|
|
log_fmt = '%(asctime)-15s %(levelname)-8s %(message)s'
|
|
log = logging.getLogger()
|
|
|
|
|
|
class Config:
|
|
config_topic = 'doorlock'
|
|
|
|
def __init__(self, sysconfdir):
|
|
self.config = ConfigParser()
|
|
self.config.read([join(sysconfdir, 'doorlockd.default.cfg'),
|
|
join(sysconfdir, 'doorlockd.cfg')])
|
|
|
|
def boolean(self, key):
|
|
return self.config.getboolean(self.config_topic, key)
|
|
|
|
def str(self, key):
|
|
return self.config.get(self.config_topic, key)
|
|
|
|
|
|
cfg = Config(SYSCONFDIR)
|
|
|
|
|
|
class Logic:
|
|
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)
|
|
if err != DoorlockResponse.Success:
|
|
return err
|
|
|
|
return self.door_handler.request(state)
|
|
|
|
@property
|
|
def state(self):
|
|
return self.door_handler.state
|
|
|
|
|
|
if __name__ == '__main__':
|
|
logging.basicConfig(level=log_level, stream=sys.stdout,
|
|
format=log_fmt, datefmt=date_fmt)
|
|
log.info('Starting doorlockd')
|
|
|
|
root_prefix = join(PREFIX, 'share', 'doorlockd')
|
|
|
|
sounds_prefix = join(root_prefix, 'sounds')
|
|
scripts_prefix = join(root_prefix, 'scripts')
|
|
logic = Logic(cfg, sounds_prefix, scripts_prefix, emit_doorstate)
|
|
|
|
static_folder = abspath(join(root_prefix, 'static'))
|
|
template_folder = abspath(join(root_prefix, 'templates'))
|
|
webapp_run(cfg, logic, __status__, __version__, template_folder,
|
|
static_folder)
|
|
|
|
sys.exit(0)
|