-
Notifications
You must be signed in to change notification settings - Fork 0
/
luxafor-web.py
145 lines (111 loc) · 2.81 KB
/
luxafor-web.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
from flask import Flask, request
import usb.core
import usb.util
import sys
import argparse
app = Flask(__name__)
DEVICES = []
ACTION = None
LED = None
HEX = None
RED = None
GREEN = None
BLUE = None
SPEED = None
REPEAT = None
WAVE = None
PATTERN = None
def set_luxafor(color_name):
global RED
global GREEN
global BLUE
global DEVICE
global RED
global GREEN
global BLUE
global SPEED
global REPEAT
global WAVE
global PATTERN
global LED
global HEX
setupDevices()
DEVICE = 1
SPEED = 0
REPEAT = 0
WAVE = 0
PATTERN = 0
LED = 255
HEX = 0
if color_name == "green":
RED = 0
GREEN = 255
BLUE = 0
if color_name == "red":
RED = 255
GREEN = 0
BLUE = 0
if color_name == "blue":
RED = 0
GREEN = 0
BLUE = 255
if color_name == "off":
RED = 0
GREEN = 0
BLUE = 0
if HEX:
rgb = hex_to_rgb(HEX)
RED = rgb[0]
GREEN = rgb[1]
BLUE = rgb[2]
setColor()
def hex_to_rgb(value): # http://stackoverflow.com/a/214657
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def setupDevices():
global DEVICES
for flag in usb.core.find(find_all=True, idProduct=0xf372):
DEVICES.append(flag)
# Device found?
if len(DEVICES) < 1:
raise ValueError('Device(s) not found')
# Linux kernel sets up a device driver for USB device, which you have to detach.
# Otherwise trying to interact with the device gives a 'Resource Busy' error.
for flag in DEVICES:
try:
flag.detach_kernel_driver(0)
except Exception:
pass
#flag.set_configuration()
def writeValue(values):
if (DEVICE > 0):
doWriteValue(DEVICES[DEVICE-1], values)
return
for flag in DEVICES:
doWriteValue(flag, values)
def doWriteValue(target, values):
# Run it twice to ensure it works.
target.write(1, values)
target.write(1, values)
def setPattern():
writeValue( [6,PATTERN,REPEAT,0,0,0,0] )
def setWave():
writeValue( [4,WAVE,RED,GREEN,BLUE,0,REPEAT,SPEED] )
def setStrobe():
writeValue( [3,LED,RED,GREEN,BLUE,SPEED,0,REPEAT] )
def setFade():
writeValue( [2,LED,RED,GREEN,BLUE,SPEED,0] )
def setColor():
writeValue( [1,LED,RED,GREEN,BLUE,0,0] )
set_luxafor("red")
@app.route('/webhook', methods=['POST'])
def webhook():
if request.method == 'POST':
post_data = request.get_json()
print("Data received from Webhook is: ", request.get_json()['color'])
color = post_data['color']
print(color)
set_luxafor(color)
return "OK", 200
app.run(host='0.0.0.0', port=8000, threaded=True)