forked from simondankelmann/LED-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
73 lines (61 loc) · 1.78 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
72
73
import socket
import select
from config import SERVER_IP, SERVER_PORT, MODE
from ledFunctions import setRGB
from effect_cops import effect_cop
from effect_fade import effect_fade
from ColorFader import ColorFader
R_CURRENT = 0
G_CURRENT = 0
B_CURRENT = 0
led_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
led_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
led_socket.bind((SERVER_IP, SERVER_PORT))
led_socket.listen(5)
print("LED SERVER STARTED AT " + SERVER_IP + " ON PORT: " + str(SERVER_PORT))
oEffectThread = effect_cop(1)
oFadeThread = ColorFader(1, R_CURRENT, G_CURRENT, B_CURRENT, 0, 0, 0)
oFadeThread.start()
read_list = [led_socket]
while True:
readable, writable, errored = select.select(read_list, [], [])
for s in readable:
if s is led_socket:
client_socket, address = led_socket.accept()
read_list.append(client_socket)
else:
byteData = s.recv(24)
data = byteData.decode('utf-8')
if len(data) > 0:
if data == "OFF":
oEffectThread.stopit()
setRGB(0, 0, 0)
if data == "STOP":
oEffectThread.stopit()
if data == "COPS":
oEffectThread.stopit()
oEffectThread = effect_cop(0.5)
oEffectThread.start()
if data == "FADE":
oEffectThread.stopit()
oEffectThread = effect_fade(0.03)
oEffectThread.start()
aRGB = data.split(',')
if len(aRGB) == 3:
oEffectThread.stopit()
r = int(aRGB[0])
g = int(aRGB[1])
b = int(aRGB[2])
if MODE is "fade":
oFadeThread.stopit()
oFadeThread = ColorFader(1000, R_CURRENT, G_CURRENT, B_CURRENT, r, g, b)
oFadeThread.start()
#BUFFER VALUES TO FADE FROM THERE NEXT TIME
R_CURRENT = r
G_CURRENT = g
B_CURRENT = b
else:
setRGB(r, g, b)
else:
s.close()
read_list.remove(s)