2018-03-13 18:26:12 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-03-18 18:25:06 +01:00
|
|
|
"""
|
|
|
|
Doorlockd -- Binary Kitchen's smart door opener
|
|
|
|
|
2019-02-22 20:13:32 +01:00
|
|
|
Copyright (c) Binary Kitchen e.V., 2018-2019
|
2018-03-18 18:25:06 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
2018-10-08 02:40:36 +02:00
|
|
|
from configparser import ConfigParser
|
2018-10-08 03:11:24 +02:00
|
|
|
from os.path import abspath, join
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-10-09 00:00:49 +02:00
|
|
|
from pydoorlock.Authenticator import Authenticator
|
2018-10-09 01:06:59 +02:00
|
|
|
from pydoorlock.WebApp import webapp_run, emit_doorstate
|
2018-10-09 00:35:14 +02:00
|
|
|
from pydoorlock.Doorlock import DoorlockResponse, DoorHandler
|
2019-06-14 19:02:20 +02:00
|
|
|
from pydoorlock.Config import Config
|
2018-10-08 01:12:32 +02:00
|
|
|
|
2018-09-05 21:26:38 +02:00
|
|
|
SYSCONFDIR = '.'
|
|
|
|
PREFIX = '.'
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
__author__ = 'Ralf Ramsauer'
|
2019-02-22 20:13:32 +01:00
|
|
|
__copyright = 'Copyright (c) Ralf Ramsauer, 2018-2019'
|
2018-03-13 18:26:12 +01:00
|
|
|
__license__ = 'GPLv2'
|
|
|
|
__email__ = 'ralf@binary-kitchen.de'
|
|
|
|
__status__ = 'Development'
|
|
|
|
__maintainer__ = 'Ralf Ramsauer'
|
2018-10-16 21:23:52 +02:00
|
|
|
__version__ = '0.0'
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
log_level = logging.DEBUG
|
|
|
|
date_fmt = '%Y-%m-%d %H:%M:%S'
|
|
|
|
log_fmt = '%(asctime)-15s %(levelname)-8s %(message)s'
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
2018-10-09 00:15:54 +02:00
|
|
|
cfg = Config(SYSCONFDIR)
|
2018-03-18 22:03:48 +01:00
|
|
|
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
class Logic:
|
2018-10-09 01:06:59 +02:00
|
|
|
def __init__(self, cfg, sounds_prefix, scripts_prefix, callback):
|
2018-10-08 19:47:26 +02:00
|
|
|
self.auth = Authenticator(cfg)
|
2018-10-09 00:15:54 +02:00
|
|
|
self.door_handler = DoorHandler(cfg, sounds_prefix, scripts_prefix)
|
2018-10-09 01:06:59 +02:00
|
|
|
self.door_handler.register_callback(callback)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-10-08 22:59:34 +02:00
|
|
|
def request(self, state, credentials):
|
2018-10-08 01:12:32 +02:00
|
|
|
err = self.auth.try_auth(credentials)
|
2018-10-08 22:59:34 +02:00
|
|
|
if err != DoorlockResponse.Success:
|
2018-03-13 18:26:12 +01:00
|
|
|
return err
|
|
|
|
|
2018-10-08 22:59:34 +02:00
|
|
|
return self.door_handler.request(state)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
@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')
|
|
|
|
|
2018-10-09 00:15:54 +02:00
|
|
|
root_prefix = join(PREFIX, 'share', 'doorlockd')
|
|
|
|
|
2018-10-09 00:00:49 +02:00
|
|
|
sounds_prefix = join(root_prefix, 'sounds')
|
2018-10-09 00:15:54 +02:00
|
|
|
scripts_prefix = join(root_prefix, 'scripts')
|
2018-10-09 01:06:59 +02:00
|
|
|
logic = Logic(cfg, sounds_prefix, scripts_prefix, emit_doorstate)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-10-09 00:02:21 +02:00
|
|
|
static_folder = abspath(join(root_prefix, 'static'))
|
|
|
|
template_folder = abspath(join(root_prefix, 'templates'))
|
2018-10-08 03:11:24 +02:00
|
|
|
webapp_run(cfg, logic, __status__, __version__, template_folder,
|
|
|
|
static_folder)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
sys.exit(0)
|