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
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2018-03-23 00:32:34 +01:00
|
|
|
import ldap
|
2018-03-13 18:26:12 +01:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from enum import Enum
|
2018-09-05 21:26:38 +02:00
|
|
|
from os.path import join
|
2018-03-13 18:26:12 +01:00
|
|
|
from random import sample
|
|
|
|
from serial import Serial
|
2018-03-18 22:03:48 +01:00
|
|
|
from subprocess import Popen
|
2018-03-18 15:48:59 +01:00
|
|
|
from threading import Thread
|
|
|
|
from time import sleep
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-06-29 23:42:13 +02:00
|
|
|
from flask import abort, Flask, jsonify, render_template, request
|
2018-03-13 18:26:12 +01:00
|
|
|
from flask_bootstrap import Bootstrap
|
|
|
|
from flask_socketio import SocketIO
|
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import PasswordField, StringField, SubmitField
|
|
|
|
from wtforms.validators import DataRequired, Length
|
|
|
|
|
2018-09-05 21:26:38 +02:00
|
|
|
SYSCONFDIR = '.'
|
|
|
|
PREFIX = '.'
|
|
|
|
|
2018-09-05 21:29:12 +02:00
|
|
|
root_prefix = join(PREFIX, 'share', 'doorlockd')
|
|
|
|
sounds_prefix = join(root_prefix, 'sounds')
|
2018-09-05 23:14:28 +02:00
|
|
|
static_folder = join(root_prefix, 'static')
|
|
|
|
template_folder = join(root_prefix, 'templates')
|
2018-09-05 23:47:42 +02:00
|
|
|
scripts_prefix = join(root_prefix, 'scripts')
|
2018-09-05 23:14:28 +02:00
|
|
|
|
|
|
|
flask_config = join(SYSCONFDIR, 'doorlockd.cfg')
|
2018-09-05 21:29:12 +02:00
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
__author__ = 'Ralf Ramsauer'
|
|
|
|
__copyright = 'Copyright (c) Ralf Ramsauer, 2018'
|
|
|
|
__license__ = 'GPLv2'
|
|
|
|
__email__ = 'ralf@binary-kitchen.de'
|
|
|
|
__status__ = 'Development'
|
|
|
|
__maintainer__ = 'Ralf Ramsauer'
|
|
|
|
__version__ = '0.01a'
|
|
|
|
|
|
|
|
log_level = logging.DEBUG
|
|
|
|
date_fmt = '%Y-%m-%d %H:%M:%S'
|
|
|
|
log_fmt = '%(asctime)-15s %(levelname)-8s %(message)s'
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
|
|
|
default_ldap_uri = 'ldaps://ldap1.binary.kitchen/ ' \
|
|
|
|
'ldaps://ldap2.binary.kitchen/ ' \
|
|
|
|
'ldaps://ldapm.binary.kitchen/'
|
|
|
|
default_binddn = 'cn=%s,ou=people,dc=binary-kitchen,dc=de'
|
|
|
|
|
|
|
|
html_title = 'Binary Kitchen Doorlock (%s - v%s)' % (__status__, __version__)
|
|
|
|
|
2018-09-05 23:14:28 +02:00
|
|
|
webapp = Flask(__name__,
|
|
|
|
template_folder=template_folder,
|
|
|
|
static_folder=static_folder)
|
|
|
|
webapp.config.from_pyfile(flask_config)
|
2018-04-03 23:29:51 +02:00
|
|
|
socketio = SocketIO(webapp, async_mode='threading')
|
2018-03-13 18:26:12 +01:00
|
|
|
Bootstrap(webapp)
|
2018-03-16 16:52:43 +01:00
|
|
|
serial_port = webapp.config.get('SERIAL_PORT')
|
2018-09-02 04:00:03 +02:00
|
|
|
simulate_ldap = webapp.config.get('SIMULATE_LDAP')
|
|
|
|
simulate_serial = webapp.config.get('SIMULATE_SERIAL')
|
2018-03-21 00:58:16 +01:00
|
|
|
run_hooks = webapp.config.get('RUN_HOOKS')
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-03-23 00:32:34 +01:00
|
|
|
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND)
|
|
|
|
ldap.set_option(ldap.OPT_REFERRALS, 0)
|
|
|
|
if 'LDAP_CA' in webapp.config.keys():
|
|
|
|
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, webapp.config.get('LDAP_CA'))
|
|
|
|
|
|
|
|
ldap_uri = webapp.config.get('LDAP_URI')
|
|
|
|
ldap_binddn = webapp.config.get('LDAP_BINDDN')
|
|
|
|
|
2018-09-05 21:29:12 +02:00
|
|
|
wave_emergency = 'emergency_unlock.wav'
|
|
|
|
wave_lock = 'lock.wav'
|
|
|
|
wave_lock_button = 'lock_button.wav'
|
|
|
|
wave_unlock = 'unlock.wav'
|
|
|
|
wave_unlock_button = 'unlock_button.wav'
|
|
|
|
wave_zonk = 'zonk.wav'
|
|
|
|
|
2018-03-23 23:00:25 +01:00
|
|
|
sounds = webapp.config.get('SOUNDS')
|
|
|
|
|
2018-09-05 20:23:39 +02:00
|
|
|
host = 'localhost'
|
|
|
|
if webapp.config.get('DEBUG'):
|
|
|
|
host = '0.0.0.0'
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
# copied from sudo
|
|
|
|
eperm_insults = {
|
|
|
|
'Wrong! You cheating scum!',
|
|
|
|
'And you call yourself a Rocket Scientist!',
|
|
|
|
'No soap, honkie-lips.',
|
|
|
|
'Where did you learn to type?',
|
|
|
|
'Are you on drugs?',
|
|
|
|
'My pet ferret can type better than you!',
|
|
|
|
'You type like i drive.',
|
|
|
|
'Do you think like you type?',
|
|
|
|
'Your mind just hasn\'t been the same since the electro-shock, has it?',
|
|
|
|
'Maybe if you used more than just two fingers...',
|
|
|
|
'BOB says: You seem to have forgotten your passwd, enter another!',
|
|
|
|
'stty: unknown mode: doofus',
|
|
|
|
'I can\'t hear you -- I\'m using the scrambler.',
|
|
|
|
'The more you drive -- the dumber you get.',
|
|
|
|
'Listen, broccoli brains, I don\'t have time to listen to this trash.',
|
|
|
|
'I\'ve seen penguins that can type better than that.',
|
|
|
|
'Have you considered trying to match wits with a rutabaga?',
|
|
|
|
'You speak an infinite deal of nothing',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def choose_insult():
|
|
|
|
return(sample(eperm_insults, 1)[0])
|
|
|
|
|
|
|
|
|
2018-03-23 23:00:25 +01:00
|
|
|
def playsound(filename):
|
|
|
|
if not sounds:
|
|
|
|
return
|
2018-09-05 21:29:12 +02:00
|
|
|
Popen(['nohup', 'aplay', join(sounds_prefix, filename)])
|
2018-03-23 23:00:25 +01:00
|
|
|
|
|
|
|
|
2018-03-18 22:03:48 +01:00
|
|
|
def start_hook(script):
|
2018-03-21 00:58:16 +01:00
|
|
|
if not run_hooks:
|
|
|
|
log.info('Hooks disabled: not starting %s' % script)
|
2018-03-18 22:03:48 +01:00
|
|
|
return
|
|
|
|
log.info('Starting hook %s' % script)
|
2018-09-05 23:47:42 +02:00
|
|
|
Popen(['nohup', join(scripts_prefix, script)])
|
2018-03-18 22:03:48 +01:00
|
|
|
|
|
|
|
|
2018-03-26 22:50:49 +02:00
|
|
|
def run_lock():
|
2018-09-05 23:47:42 +02:00
|
|
|
start_hook('post_lock')
|
2018-03-26 22:50:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
def run_unlock():
|
2018-09-05 23:47:42 +02:00
|
|
|
start_hook('post_unlock')
|
2018-03-26 22:50:49 +02:00
|
|
|
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
class AuthMethod(Enum):
|
|
|
|
LDAP_USER_PW = 1
|
|
|
|
|
|
|
|
|
|
|
|
class DoorState(Enum):
|
2018-09-03 00:19:24 +02:00
|
|
|
# These numbers are used by the App since version 3.0, do NOT change them
|
|
|
|
Open = 0
|
|
|
|
Present = 1
|
|
|
|
Closed = 2
|
2018-09-02 04:11:15 +02:00
|
|
|
|
2018-09-02 23:21:26 +02:00
|
|
|
def from_string(string):
|
|
|
|
if string == 'lock':
|
|
|
|
return DoorState.Closed
|
|
|
|
elif string == 'unlock':
|
|
|
|
return DoorState.Open
|
|
|
|
elif string == 'present':
|
|
|
|
return DoorState.Present
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2018-09-02 04:11:15 +02:00
|
|
|
def is_open(self):
|
|
|
|
if self != DoorState.Closed:
|
|
|
|
return True
|
|
|
|
return False
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
def to_img(self):
|
|
|
|
led = 'red'
|
2018-09-02 04:11:15 +02:00
|
|
|
if self == DoorState.Present:
|
|
|
|
led = 'yellow'
|
|
|
|
elif self == DoorState.Open:
|
|
|
|
led = 'green'
|
2018-03-18 17:22:08 +01:00
|
|
|
return 'static/led-%s.png' % led
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
def to_html(self):
|
|
|
|
if self == DoorState.Open:
|
|
|
|
return 'Offen'
|
2018-09-02 04:11:15 +02:00
|
|
|
elif self == DoorState.Present:
|
|
|
|
return 'Jemand da'
|
2018-03-23 23:52:36 +01:00
|
|
|
return 'Geschlossen'
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LogicResponse(Enum):
|
|
|
|
Success = 0
|
|
|
|
Perm = 1
|
2018-09-03 00:29:30 +02:00
|
|
|
AlreadyActive = 2
|
|
|
|
# don't break old apps, value 3 is reserved now
|
|
|
|
RESERVED = 3
|
2018-03-13 18:26:12 +01:00
|
|
|
Inval = 4
|
|
|
|
LDAP = 5
|
|
|
|
|
2018-03-19 18:46:33 +01:00
|
|
|
EmergencyUnlock = 10,
|
|
|
|
ButtonLock = 11,
|
|
|
|
ButtonUnlock = 12,
|
2018-09-02 04:11:15 +02:00
|
|
|
ButtonPresent = 13,
|
2018-03-19 18:46:33 +01:00
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
def to_html(self):
|
|
|
|
if self == LogicResponse.Success:
|
|
|
|
return 'Yo, passt.'
|
|
|
|
elif self == LogicResponse.Perm:
|
|
|
|
return choose_insult()
|
2018-09-03 00:29:30 +02:00
|
|
|
elif self == LogicResponse.AlreadyActive:
|
|
|
|
return 'Zustand bereits aktiv'
|
2018-03-13 18:26:12 +01:00
|
|
|
elif self == LogicResponse.Inval:
|
|
|
|
return 'Das was du willst geht nicht.'
|
|
|
|
elif self == LogicResponse.LDAP:
|
|
|
|
return 'Moep! Geh LDAP fixen!'
|
2018-03-19 18:46:33 +01:00
|
|
|
elif self == LogicResponse.EmergencyUnlock:
|
|
|
|
return '!!! Emergency Unlock !!!'
|
|
|
|
elif self == LogicResponse.ButtonLock:
|
2018-09-02 04:11:15 +02:00
|
|
|
return 'Closed by button'
|
2018-03-19 18:46:33 +01:00
|
|
|
elif self == LogicResponse.ButtonUnlock:
|
2018-09-02 04:11:15 +02:00
|
|
|
return 'Opened by button'
|
|
|
|
elif self == LogicResponse.ButtonPresent:
|
|
|
|
return 'Present by button'
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
return 'Bitte spezifizieren Sie.'
|
|
|
|
|
|
|
|
|
|
|
|
class DoorHandler:
|
2018-09-02 04:11:15 +02:00
|
|
|
state = DoorState.Closed
|
|
|
|
do_close = False
|
|
|
|
|
|
|
|
CMD_PRESENT = b'y'
|
|
|
|
CMD_OPEN = b'g'
|
|
|
|
CMD_CLOSE = b'r'
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-09-02 04:11:15 +02:00
|
|
|
BUTTON_PRESENT = b'Y'
|
|
|
|
BUTTON_OPEN = b'G'
|
|
|
|
BUTTON_CLOSE = b'R'
|
2018-09-04 01:05:46 +02:00
|
|
|
|
|
|
|
CMD_EMERGENCY_SWITCH = b'E'
|
2018-09-02 04:11:15 +02:00
|
|
|
# TBD DOOR NOT CLOSED
|
2018-03-19 18:46:33 +01:00
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
def __init__(self, device):
|
2018-09-02 04:00:03 +02:00
|
|
|
if simulate_serial:
|
2018-03-16 16:52:43 +01:00
|
|
|
return
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
self.serial = Serial(device, baudrate=9600, bytesize=8, parity='N',
|
2018-03-19 18:46:33 +01:00
|
|
|
stopbits=1, timeout=0)
|
2018-03-13 18:26:12 +01:00
|
|
|
self.thread = Thread(target=self.thread_worker)
|
|
|
|
self.thread.start()
|
|
|
|
|
2018-03-19 18:46:33 +01:00
|
|
|
def handle_input(self, recv, expect=None):
|
2018-09-02 04:11:15 +02:00
|
|
|
if recv == DoorHandler.BUTTON_CLOSE:
|
2018-03-23 23:00:25 +01:00
|
|
|
playsound(wave_lock_button)
|
2018-09-02 04:11:15 +02:00
|
|
|
if self.state.is_open():
|
2018-03-26 22:50:49 +02:00
|
|
|
run_lock()
|
2018-09-02 04:11:15 +02:00
|
|
|
self.state = DoorState.Closed
|
2018-03-19 18:46:33 +01:00
|
|
|
logic.emit_status(LogicResponse.ButtonLock)
|
2018-09-02 04:11:15 +02:00
|
|
|
elif recv == DoorHandler.BUTTON_OPEN:
|
2018-03-23 23:00:25 +01:00
|
|
|
playsound(wave_unlock_button)
|
2018-09-02 04:11:15 +02:00
|
|
|
self.state = DoorState.Open
|
2018-03-19 18:46:33 +01:00
|
|
|
logic.emit_status(LogicResponse.ButtonUnlock)
|
2018-09-02 04:11:15 +02:00
|
|
|
elif recv == DoorHandler.BUTTON_PRESENT:
|
|
|
|
# playsound...
|
|
|
|
self.state = DoorState.Present
|
|
|
|
logic.emit_status(LogicResponse.ButtonPresent)
|
2018-09-04 01:05:46 +02:00
|
|
|
elif recv == DoorHandler.CMD_EMERGENCY_SWITCH:
|
|
|
|
playsound(wave_emergency)
|
|
|
|
logic.emit_status(LogicResponse.EmergencyUnlock)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-03-19 18:46:33 +01:00
|
|
|
if expect is None:
|
|
|
|
return True
|
|
|
|
return recv == expect
|
|
|
|
|
|
|
|
def send_command(self, cmd):
|
|
|
|
self.serial.write(cmd)
|
|
|
|
self.serial.flush()
|
|
|
|
sleep(0.1)
|
|
|
|
char = self.serial.read(1)
|
|
|
|
if not self.handle_input(char, cmd):
|
|
|
|
log.warning('Sent serial command, got wrong response')
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
def thread_worker(self):
|
|
|
|
while True:
|
2018-03-19 18:46:33 +01:00
|
|
|
sleep(0.4)
|
|
|
|
while True:
|
|
|
|
char = self.serial.read(1)
|
|
|
|
if len(char) == 0:
|
|
|
|
break
|
|
|
|
self.handle_input(char)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-09-02 04:11:15 +02:00
|
|
|
if self.do_close:
|
|
|
|
self.send_command(DoorHandler.CMD_CLOSE)
|
|
|
|
self.do_close = False
|
|
|
|
elif self.state.is_open():
|
|
|
|
if self.state == DoorState.Present:
|
|
|
|
self.send_command(DoorHandler.CMD_PRESENT)
|
|
|
|
else:
|
|
|
|
self.send_command(DoorHandler.CMD_OPEN)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
def open(self):
|
|
|
|
if self.state == DoorState.Open:
|
2018-09-03 00:29:30 +02:00
|
|
|
return LogicResponse.AlreadyActive
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
self.state = DoorState.Open
|
2018-03-26 22:50:49 +02:00
|
|
|
run_unlock()
|
2018-03-13 18:26:12 +01:00
|
|
|
return LogicResponse.Success
|
|
|
|
|
|
|
|
def close(self):
|
2018-09-02 04:11:15 +02:00
|
|
|
if self.state == DoorState.Closed:
|
2018-09-03 00:29:30 +02:00
|
|
|
return LogicResponse.AlreadyActive
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-09-02 04:11:15 +02:00
|
|
|
self.do_close = True
|
|
|
|
self.state = DoorState.Closed
|
2018-03-26 22:50:49 +02:00
|
|
|
run_lock()
|
2018-03-13 18:26:12 +01:00
|
|
|
return LogicResponse.Success
|
|
|
|
|
2018-09-02 04:11:15 +02:00
|
|
|
def present(self):
|
|
|
|
if self.state == DoorState.Present:
|
2018-09-03 00:29:30 +02:00
|
|
|
return LogicResponse.AlreadyActive
|
2018-09-02 04:11:15 +02:00
|
|
|
|
|
|
|
self.state = DoorState.Present
|
|
|
|
# new hook?
|
|
|
|
return LogicResponse.Success
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
def request(self, state):
|
2018-09-02 04:11:15 +02:00
|
|
|
if state == DoorState.Closed:
|
2018-03-13 18:26:12 +01:00
|
|
|
return self.close()
|
2018-09-02 04:11:15 +02:00
|
|
|
elif state == DoorState.Present:
|
|
|
|
return self.present()
|
2018-03-13 18:26:12 +01:00
|
|
|
elif state == DoorState.Open:
|
|
|
|
return self.open()
|
|
|
|
|
|
|
|
|
|
|
|
class Logic:
|
2018-03-18 16:35:09 +01:00
|
|
|
def __init__(self):
|
2018-03-16 16:52:43 +01:00
|
|
|
self.door_handler = DoorHandler(serial_port)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
def _try_auth_ldap(self, user, password):
|
2018-09-02 04:00:03 +02:00
|
|
|
if simulate_ldap:
|
2018-03-16 16:52:43 +01:00
|
|
|
log.info('SIMULATION MODE! ACCEPTING ANYTHING!')
|
|
|
|
return LogicResponse.Success
|
|
|
|
|
2018-03-23 00:32:34 +01:00
|
|
|
log.info(' Trying to LDAP auth (user, password) as user %s', user)
|
|
|
|
ldap_username = ldap_binddn % user
|
|
|
|
try:
|
|
|
|
l = ldap.initialize(ldap_uri)
|
|
|
|
l.simple_bind_s(ldap_username, password)
|
|
|
|
l.unbind_s()
|
|
|
|
except ldap.INVALID_CREDENTIALS:
|
|
|
|
log.info(' Invalid credentials')
|
|
|
|
return LogicResponse.Perm
|
|
|
|
except ldap.LDAPError as e:
|
|
|
|
log.info(' LDAP Error: %s' % e)
|
|
|
|
return LogicResponse.LDAP
|
|
|
|
return LogicResponse.Success
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
def try_auth(self, credentials):
|
|
|
|
method = credentials[0]
|
|
|
|
if method == AuthMethod.LDAP_USER_PW:
|
|
|
|
return self._try_auth_ldap(credentials[1], credentials[2])
|
|
|
|
|
|
|
|
return LogicResponse.Inval
|
|
|
|
|
|
|
|
def _request(self, state, credentials):
|
|
|
|
err = self.try_auth(credentials)
|
|
|
|
if err != LogicResponse.Success:
|
|
|
|
return err
|
|
|
|
return self.door_handler.request(state)
|
|
|
|
|
|
|
|
def request(self, state, credentials):
|
|
|
|
err = self._request(state, credentials)
|
2018-03-23 23:00:25 +01:00
|
|
|
if err == LogicResponse.Success:
|
|
|
|
if self.door_handler.state == DoorState.Open:
|
|
|
|
playsound(wave_unlock)
|
2018-09-02 04:11:15 +02:00
|
|
|
if self.door_handler.state == DoorState.Closed:
|
2018-03-23 23:00:25 +01:00
|
|
|
playsound(wave_lock)
|
2018-09-03 00:29:30 +02:00
|
|
|
elif err == LogicResponse.AlreadyActive:
|
2018-03-23 23:00:25 +01:00
|
|
|
playsound(wave_zonk)
|
2018-03-18 17:47:50 +01:00
|
|
|
self.emit_status(err)
|
2018-03-13 18:26:12 +01:00
|
|
|
return err
|
|
|
|
|
2018-03-18 17:47:50 +01:00
|
|
|
def emit_status(self, message=None):
|
|
|
|
led = self.state.to_img()
|
|
|
|
if message is None:
|
|
|
|
message = self.state.to_html()
|
|
|
|
else:
|
|
|
|
message = message.to_html()
|
|
|
|
|
|
|
|
socketio.emit('status', {'led': led, 'message': message})
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
return self.door_handler.state
|
|
|
|
|
|
|
|
|
|
|
|
class AuthenticationForm(FlaskForm):
|
2018-04-11 23:49:44 +02:00
|
|
|
username = StringField('Username', [Length(min=3, max=25)])
|
2018-03-13 18:26:12 +01:00
|
|
|
password = PasswordField('Password', [DataRequired()])
|
|
|
|
open = SubmitField('Open')
|
2018-09-02 04:11:15 +02:00
|
|
|
present = SubmitField('Present')
|
2018-03-13 18:26:12 +01:00
|
|
|
close = SubmitField('Close')
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
FlaskForm.__init__(self, *args, **kwargs)
|
2018-09-02 04:11:15 +02:00
|
|
|
self.desired_state = DoorState.Closed
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
def validate(self):
|
|
|
|
if not FlaskForm.validate(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.open.data:
|
|
|
|
self.desired_state = DoorState.Open
|
2018-09-02 04:11:15 +02:00
|
|
|
elif self.present.data:
|
|
|
|
self.desired_state = DoorState.Present
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-03-18 18:21:07 +01:00
|
|
|
@socketio.on('request_status')
|
2018-03-13 18:26:12 +01:00
|
|
|
@socketio.on('connect')
|
|
|
|
def on_connect():
|
2018-03-18 17:47:50 +01:00
|
|
|
logic.emit_status()
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
@webapp.route('/display')
|
|
|
|
def display():
|
|
|
|
return render_template('display.html')
|
|
|
|
|
|
|
|
|
2018-06-29 23:42:13 +02:00
|
|
|
@webapp.route('/api', methods=['POST'])
|
|
|
|
def api():
|
|
|
|
def json_response(response, msg=None):
|
|
|
|
json = dict()
|
|
|
|
json['err'] = response.value
|
|
|
|
json['msg'] = response.to_html() if msg is None else msg
|
|
|
|
if response == LogicResponse.Success or \
|
2018-09-03 00:29:30 +02:00
|
|
|
response == LogicResponse.AlreadyActive:
|
2018-09-03 00:19:24 +02:00
|
|
|
# TBD: Remove 'open'. No more users. Still used in App Version 2.1.1!
|
2018-09-02 04:11:15 +02:00
|
|
|
json['open'] = logic.state.is_open()
|
2018-09-03 00:19:24 +02:00
|
|
|
json['status'] = logic.state.value
|
2018-06-29 23:42:13 +02:00
|
|
|
return jsonify(json)
|
|
|
|
|
|
|
|
user = request.form.get('user')
|
|
|
|
password = request.form.get('pass')
|
|
|
|
command = request.form.get('command')
|
|
|
|
|
|
|
|
if any(v is None for v in [user, password, command]):
|
|
|
|
log.warning('Incomplete API request')
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
if not user or not password:
|
|
|
|
log.warning('Invalid username or password format')
|
|
|
|
return json_response(LogicResponse.Inval,
|
|
|
|
'Invalid username or password format')
|
|
|
|
|
|
|
|
credentials = AuthMethod.LDAP_USER_PW, user, password
|
|
|
|
|
|
|
|
if command == 'status':
|
|
|
|
return json_response(logic.try_auth(credentials))
|
|
|
|
|
2018-09-02 23:21:26 +02:00
|
|
|
desired_state = DoorState.from_string(command)
|
2018-06-29 23:42:13 +02:00
|
|
|
if not desired_state:
|
|
|
|
return json_response(LogicResponse.Inval, "Invalid command requested")
|
|
|
|
|
|
|
|
log.info('Incoming API request from %s' % user.encode('utf-8'))
|
|
|
|
log.info(' desired state: %s' % desired_state)
|
|
|
|
log.info(' current state: %s' % logic.state)
|
|
|
|
|
|
|
|
response = logic.request(desired_state, credentials)
|
|
|
|
|
|
|
|
return json_response(response)
|
|
|
|
|
|
|
|
|
2018-03-13 18:26:12 +01:00
|
|
|
@webapp.route('/', methods=['GET', 'POST'])
|
|
|
|
def home():
|
|
|
|
authentication_form = AuthenticationForm()
|
|
|
|
response = None
|
|
|
|
|
|
|
|
if request.method == 'POST' and authentication_form.validate():
|
|
|
|
user = authentication_form.username.data
|
|
|
|
password = authentication_form.password.data
|
|
|
|
credentials = AuthMethod.LDAP_USER_PW, user, password
|
|
|
|
|
2018-03-20 00:54:52 +01:00
|
|
|
log.info('Incoming request from %s' % user.encode('utf-8'))
|
2018-03-13 18:26:12 +01:00
|
|
|
desired_state = authentication_form.desired_state
|
|
|
|
log.info(' desired state: %s' % desired_state)
|
|
|
|
log.info(' current state: %s' % logic.state)
|
|
|
|
response = logic.request(desired_state, credentials)
|
|
|
|
log.info(' response: %s' % response)
|
|
|
|
|
2018-03-21 00:29:13 +01:00
|
|
|
# Don't trust python, zero credentials
|
|
|
|
user = password = credentials = None
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
return render_template('index.html',
|
|
|
|
authentication_form=authentication_form,
|
|
|
|
response=response,
|
|
|
|
state_text=logic.state.to_html(),
|
2018-03-18 17:22:08 +01:00
|
|
|
led=logic.state.to_img(),
|
2018-03-13 18:26:12 +01:00
|
|
|
title=html_title)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
logging.basicConfig(level=log_level, stream=sys.stdout,
|
|
|
|
format=log_fmt, datefmt=date_fmt)
|
|
|
|
log.info('Starting doorlockd')
|
2018-03-18 16:35:09 +01:00
|
|
|
log.info('Using serial port: %s' % webapp.config.get('SERIAL_PORT'))
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-03-18 16:35:09 +01:00
|
|
|
logic = Logic()
|
2018-03-13 18:26:12 +01:00
|
|
|
|
2018-09-05 20:23:39 +02:00
|
|
|
socketio.run(webapp, host=host, port=8080)
|
2018-03-13 18:26:12 +01:00
|
|
|
|
|
|
|
sys.exit(0)
|