-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNukiBridge.py
161 lines (117 loc) · 4.51 KB
/
NukiBridge.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright 2016 Fivebanger
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os.path
import sys
#Make libnuki available
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(dir_path, "libnuki"))
from environment import set_environment
set_environment(dir_path)
from bridge import NukiBridge
"""
Specify the port the Nuki Bridge uses
"""
nb_port = 8080
"""
Specify a list of allowed IP addresses. Requests from hosts using another IP address are replied with HTTP error 403
Set nb_allowed_ip = ('0.0.0.0') if any IP address shall have access.
Examples:
nb_allowed_ip = ('127.0.0.1', '192.168.162.42') # localhost and 192.168.162.42
nb_allowed_ip = ('0.0.0.0') # all hosts
nb_allowed_ip = ('127.0.0.1') # local host only
"""
nb_allowed_ip = ('0.0.0.0')
"""
Specify a token which needs to be sent along with the HTTP request.
Set nb_token = None if no token shall be used.
Examples:
nb_token = None # no need to send a token along with the HTTP request
nb_token = 'a34f42' # send &token=a34f42 as parameter along with the HTTP request
"""
nb_token = None
"""
Specify a user-agent which needs to be sent along with the HTTP header of the request.
To send a proper user-agent, HTTP header needs to be manipulated (standard web browser needs header manipulation)
Set nb_header = None if no header shall be used.
Examples:
nb_agent = None # no user-agent needs to be sent along with the HTTP header
nb_agent = 'MyFancyAgent' # user-agent = MyFancyAgent needs to be sent along with the HTTP header
"""
nb_agent = None
"""
Optional parameter for NukiBridge.start()
Usage:
nb_block = True # blocking execution of the NukiBridge (default)
nb_block = False # non-blocking execution of the NukiBridge
"""
nb_block = True
"""
http API usage:
http://localhost:8080
parameter: -
http://localhost:8080/bridgeInfo
parameter: -
return : {"bridgeId": 2736774877, "bridgeName": "Bridge"}
http://localhost:8080/bridgeConfig?bridgeId=2&bridgeName=Bridge
parameter: bridgeId: 0..4294967295
parameter: bridgeName: ASCII, UTF-8
return: {"bridgeId": 2, "bridgeName": "Bridge"}
http://localhost:8080/list
parameter: -
return: [{"nukiId": 1, "name": "Test_1"}, {"nukiId": 2, "name": "Test_2"}]
http://localhost:8080/nukiPair?nukiName=Test_1
parameter: nukiName: ASCII
return: {"nukiId": 1, "name": "Test_1", "success": true}
http://localhost:8080/lockState?nukiId=1
parameter: nukiId: 1..nn
return: {"state": 0, "stateName": "uncalibrated", "success": true, "batteryCritical": false}
stateName: 0: uncalibrated
1: locked
2: unlocked
3: unlocked (lock 'n' go)
4: unlatched
5: locking
6: unlocking
7: unlatching
254: motor blocked, 255: undefined)
http://localhost:8080/lockAction?nukiId=1&action=1
parameter: nukiId: 1..nn
parameter: action: 1..5
action 1: unlock
2: lock
3: unlatch
4: lock 'n' go
5: lock 'n' go with unlatch
return: {"success": true, "batteryCritical": false}
http://localhost:8080/updateTime?nukiId=1&nukiPin=1234
parameter: nukiId: 1..nn
parameter: nukiPin: Pin given by the Nuki configuration
return: {"success": true, "batteryCritical": "false"}
http://localhost:8080/batteryState?nukiId=1
parameter: nukiId: 1..nn
return: {"batteryVoltage": 0, "success": true, "batteryCritical": false}
http://localhost:8080
http://localhost:8080/updateTime?nukiId=1&nukiPin=1234&token=a34f42
http://localhost:8080/batteryState?nukiId=1&token=a34f42
http://localhost:8080/bridgeConfig?bridgeId=2&bridgeName=Bridge&token=a34f42
http://localhost:8080/bridgeInfo&token=a34f42
http://localhost:8080/nukiPair?nukiName=Test_1&token=a34f42
http://localhost:8080/list&token=a34f42
http://localhost:8080/lockState?nukiId=1&token=a34f42
http://localhost:8080/lockAction?nukiId=1&action=1&token=a34f42
"""
if __name__ == '__main__':
nb = NukiBridge(nb_port, nb_allowed_ip, nb_token, nb_agent)
nb.start(nb_block)
print'end'