forked from giejay/nukiPyBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
73 lines (58 loc) · 2.05 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from flask import Flask
from configparser import ConfigParser
import nuki
from nacl.public import PrivateKey
from flask import jsonify
import logging
from pathlib import Path
cwd = Path.cwd()
configfile = cwd.joinpath('nuki.cfg')
print("Config file: {}".format(configfile))
app = Flask(__name__)
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def parse_config():
parser = ConfigParser()
parser.read('nuki.cfg')
config_dict = {}
for sect in parser.sections():
for name, value in parser.items(sect):
if name == "name":
config_dict[value] = sect
return config_dict
config = parse_config()
print(config)
@app.route("/")
def get_config():
return config
@app.route("/connect/<mac_address>/<name>")
def connect(mac_address, name):
# generate the private key which must be kept secret
keypair = PrivateKey.generate()
myPublicKeyHex = keypair.public_key.__bytes__().hex()
myPrivateKeyHex = keypair.__bytes__().hex()
myID = 50
# id-type = 00 (app), 01 (bridge) or 02 (fob)
# take 01 (bridge) if you want to make sure that the 'new state available'-flag is cleared on the Nuki if you read it out the state using this library
myIDType = '01'
nuki.Nuki(mac_address, configfile).authenticateUser(myPublicKeyHex, myPrivateKeyHex, myID, myIDType, name)
config = parse_config()
print(config)
return "Connected to " + mac_address
@app.route("/<door>/lock")
def lock_door(door):
return execute_action('LOCK', door)
@app.route("/<door>/unlock")
def unlock_door(door):
return execute_action('UNLOCK', door)
@app.route("/<door>/open")
def open_door(door):
return execute_action('UNLATCH', door)
@app.route("/<door>/state")
def state(door):
return nuki.Nuki(config[door], configfile).readLockState().show()
@app.route("/<door>/logs")
def get_log_entries(door):
return jsonify(nuki.Nuki(config[door], configfile).getLogEntries(1, "%04x" % 0000))
def execute_action(type, door):
return nuki.Nuki(config[door], configfile).lockAction(type).show()