-
Notifications
You must be signed in to change notification settings - Fork 0
/
lumen.py
executable file
·323 lines (276 loc) · 8.73 KB
/
lumen.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/python
import sys
import socket
import os
import json
import signal
import select
import time
from time import sleep
from btle import Peripheral, BTLEException
SERVER_SOCKET = "/tmp/lumen_socket"
BULB = "7C:66:9D:7D:38:16"
#def connect():
#from gattlib import GATTRequester
#class Requester(GATTRequester):
#def on_notification(self, handle, data):
#print("- notification on handle: {}\n".format(handle))
#global req;
#if req == None:
#bulb = "7C:66:9D:7D:38:16"
#req = Requester(bulb)
#req.write_by_handle(0x25, str(bytearray( [0x08, 0x61, 0x07, 0x66, 0xa7, 0x68, 0x0f, 0x5a, 0x18, 0x3e, 0x5e, 0x7a, 0x3e, 0x3c, 0xbe, 0xaa, 0x8a, 0x21, 0x4b, 0x6b] )))
#connect.data1 = req.read_by_handle(0x28)
#req.write_by_handle(0x25, str(bytearray( [0x07, 0xdf, 0xd9, 0x9b, 0xfd, 0xdd, 0x54, 0x5a, 0x18, 0x3e, 0x5e, 0x7a, 0x3e, 0x3c, 0xbe, 0xaa, 0x8a, 0x21, 0x4b, 0x6b] )))
#connect.data2 = req.read_by_handle(0x28)
#return req
def is_server_running():
return os.path.exists(SERVER_SOCKET)
class Server:
req = None
class BTconnect:
chars = {}
def __init__(self):
try:
self.p = Peripheral(BULB)
characteristics = self.p.getCharacteristics()
for c in characteristics:
self.chars[c.getHandle()] = c
except BTLEException as e:
print str(e)
def read_by_handle(self, handle):
return self.chars[handle].read()
def write_by_handle(self, handle, data):
self.chars[handle].write(data, True)
def connect(self):
if self.req == None:
self.req = self.BTconnect()
self.req.write_by_handle(0x25, str(bytearray( [0x08, 0x61, 0x07, 0x66, 0xa7, 0x68, 0x0f, 0x5a, 0x18, 0x3e, 0x5e, 0x7a, 0x3e, 0x3c, 0xbe, 0xaa, 0x8a, 0x21, 0x4b, 0x6b] )))
self.data1 = bytearray(self.req.read_by_handle(0x28))
self.req.write_by_handle(0x25, str(bytearray( [0x07, 0xdf, 0xd9, 0x9b, 0xfd, 0xdd, 0x54, 0x5a, 0x18, 0x3e, 0x5e, 0x7a, 0x3e, 0x3c, 0xbe, 0xaa, 0x8a, 0x21, 0x4b, 0x6b] )))
self.data2 = bytearray(self.req.read_by_handle(0x28))
return self.req
def battery(self):
#from gattlib import GATTResponse
#class notify(GATTResponse):
# def on_response(self, data):
# print("notify: {}".format(data))
#battery.notify = notify()
#connect().read_by_handle_async(0x36, battery.notify)
batt = self.connect().read_by_handle(0x36)
return batt
def devicename(self):
name = self.connect().read_by_handle(0x03)
return name
def status(self):
data = bytearray(self.connect().read_by_handle(0x25))
modes = { 0x50: 'cool', 0x51: 'warm', 0x52 : 'disco1', 0x53 : 'disco2', 0x54: 'normal' }
status = {}
status["on"] = (data[0] == 0x01)
try:
status["mode"] = modes[data[6]]
if status["mode"] == "normal":
if data[1] == 0xdf and data[2] == 0xd9 and (data[3] == 0x9a or data[3] == 0x9b) :
status["mode"] = "warm_white"
warm_percent= { 0x58: 100, 0xa3: 90, 0xb5 : 70, 0x87 : 50, 0x99: 30, 0xf2 : 0 }
status["warm_percent"] = warm_percent[data[4]]
except:
pass
return status
def off(self):
self.data1[0] = 0x00
self.connect().write_by_handle(0x25, str(self.data1))
return 0
def on(self):
self.data1[0] = 0x01
self.data1[4] = 0xb5
self.connect().write_by_handle(0x25, str(self.data1))
return 0
def cool(self):
self.data1[0] = 0x01
self.data1[6] = 0x50
self.connect().write_by_handle(0x25, str(self.data1))
return 0
def warm(self):
self.data1[0] = 0x01
self.data1[6] = 0x51
self.connect().write_by_handle(0x25, str(self.data1))
return 0
def disco1(self):
self.data1[0] = 0x01
self.data1[6] = 0x52
self.connect().write_by_handle(0x25, str(self.data1))
return 0
def disco2(self):
self.data1[0] = 0x01
self.data1[6] = 0x53
self.connect().write_by_handle(0x25, str(self.data1))
return 0
def normal(self):
self.data1[0] = 0x01
self.data1[6] = 0x54
self.connect().write_by_handle(0x25, str(self.data1))
return 0
def warm_white(self, n):
self.data1[0] = 0x01
self.data1[1] = 0xdf
self.data1[6] = 0x54
n = float(n)
if n >= 100:
self.data1[2] = 0xd9
self.data1[3] = 0x9a
self.data1[4] = 0x58
elif n >= 90:
self.data1[2] = 0xd9
self.data1[3] = 0x9b
self.data1[4] = 0xa3
elif n >= 70:
self.data1[2] = 0xd9
self.data1[3] = 0x9b
self.data1[4] = 0xb5
elif n >= 50:
self.data1[2] = 0xd9
self.data1[3] = 0x9b
self.data1[4] = 0x87
elif n >= 30:
self.data1[2] = 0xd9
self.data1[3] = 0x9b
self.data1[4] = 0x99
else:
self.data1[2] = 0xd9
self.data1[3] = 0x9b
self.data1[4] = 0xf2
self.connect().write_by_handle(0x25, str(self.data1))
return 0
def white(self):
return self.warm_white(100)
def color(self, r, g, b):
self.data1[0] = 0x01
self.data1[6] = 0x54
_r = float(r) / 255;
_g = float(g) / 255;
_b = float(b) / 255;
k = 1 - max(_r, _g, _b)
c = (1 - _r - k) / (1 - k)
m = (1 - _g - k) / (1 - k)
y = (1 - _b - k) / (1 - k)
self.data1[1] = int(round(c * 105) + 120)
self.data1[2] = int(round(m * 105) + 120)
self.data1[3] = int(round(y * 105) + 120)
self.data1[4] = int(round((1 - k) * 15) + 240)
self.connect().write_by_handle(0x25, str(self.data1))
return 0
def restart(self):
print("restart")
#print(sys.argv[0], sys.argv)
self.restart = True
def stop(self):
self.alive = False
def ping(self):
self.battery()
def server(self):
self.alive = True
def handler(sig, frame):
if sig == signal.SIGTERM:
alive = False
signal.signal(signal.SIGTERM, handler)
print("Start server")
#self.battery()
lastping = time.time()
sock = socket.socket( socket.AF_UNIX, socket.SOCK_DGRAM )
sock.setblocking(0)
try:
umask_ = os.umask(0000);
sock.bind(SERVER_SOCKET)
os.umask(umask_);
while self.alive and not self.restart:
#ping
if time.time() - lastping >= 5:
lastping = time.time()
self.ping()
#get commands
try:
r, w, e = select.select([sock], [], [sock], 0.5)
except select.error as e:
alive = False
pass
for s in r:
try:
datagram, address = s.recvfrom(1024)
args = json.loads(datagram)
if self.commands[args[1]] != None:
try:
#print args[1] + "(" + str(args[2:]) + ")"
if address == None:
self.commands[args[1]](*args[2:])
else:
data = self.commands[args[1]](*args[2:])
s.sendto(json.dumps(data), address)
except TypeError as e:
print(e.args)
pass
except RuntimeError as e:
print(e.errno, e.strerror)
pass
except:
print("Exception: ", sys.exc_info()[0])
pass
except KeyError:
# unknown command
pass
except:
pass
except:
# die. we will be back
pass
sock.close()
print("End server")
os.remove(SERVER_SOCKET)
if self.restart:
os.execvp(sys.argv[0], sys.argv)
def __init__(self):
self.connect()
self.restart = None
self.alive = None
self.commands = {}
def register(command, fn):
self.commands[command] = fn
register("restart", self.restart)
register("on", self.on)
register("off", self.off)
register("cool", self.cool)
register("warm", self.warm)
register("disco1", self.disco1)
register("disco2", self.disco2)
register("normal", self.normal)
register("white", self.white)
register("warm_white", self.warm_white)
register("color", self.color)
register("status", self.status)
register("devicename", self.devicename)
register("battery", self.battery)
register("stop", self.stop)
self.server()
def should_launch_server():
if not is_server_running():
if (os.fork() == 0):
Server()
sys.exit(0)
should_launch_server()
if os.path.exists(SERVER_SOCKET):
client = socket.socket( socket.AF_UNIX, socket.SOCK_DGRAM )
try:
client.bind("/tmp/lumen_client")
except socket.error as e:
os.remove("/tmp/lumen_client")
client.bind("/tmp/lumen_client")
try:
client.connect(SERVER_SOCKET)
client.send(json.dumps(sys.argv))
response = client.recv(1024)
print(response)
client.close()
os.remove("/tmp/lumen_client")
except socket.error as e:
os.remove(SERVER_SOCKET)
should_launch_server()