-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
154 lines (134 loc) · 4.82 KB
/
client.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
import socket
from sys import argv
from time import sleep
# Globals
UDP_IP = "255.255.255.255"
UDP_PORT = 988
TCP_IP = argv[1] if len(argv) > 1 else ""
TCP_PORT = 8080
DEBUG_NETWORK = False
PACKET_SIZES = [752, 1008, 1009, 1010, 1266, 1522, 5677]
# Network Functions
def parseMessage(message):
if type(message) == bytes:
return message
else:
return message.encode()
def sendUDPPacket(address, message, wait_response=True):
packet = parseMessage(message)
if DEBUG_NETWORK:
print("UDP target IP:", UDP_IP)
print("UDP target port:", UDP_PORT)
print("message string:", packet)
print("message bytes:", packet)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sent = sock.sendto(packet, (UDP_IP, UDP_PORT))
if wait_response:
try:
sock.settimeout(1)
return sock.recvfrom(2048)
except Exception as e:
print(e)
return None
return sent
def sendTCPPacket(address, message, wait_response=False):
packet = parseMessage(message)
if DEBUG_NETWORK:
print("TCP target IP:", address[0])
print("TCP target port", address[1])
try:
print("message string:", packet.decode())
except Exception as e:
pass
print("message bytes", packet)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(address)
sent = sock.send(packet)
if wait_response:
try:
sock.settimeout(0.3)
response = sock.recv(1024)
return [hex(byte) for byte in response]
except Exception as e:
print(e)
return sent
# Lamp Functions
def probeDevices():
deviceId, address = sendUDPPacket((UDP_IP, UDP_PORT), "HLK")
print("Device discovered : ", deviceId, address[0])
return (address[0], 8080)
def sendInstruction(instruction, write_switch, data, final_byte=0):
header = [0xfe, 0xef]
message_length = len(data) + 1 + 1 + 1 + (1 if final_byte else 0)# data + instruction + write_switch + last_byte
packet = header + [message_length, instruction, write_switch] + data
packet_size = final_byte
last_byte = 0
for byte in packet:
packet_size += byte
for size in PACKET_SIZES:
if size >= packet_size:
last_byte = size - packet_size + (1 if final_byte else 0)
print('Last byte:', last_byte, "size", size)
if last_byte > 255:
continue
packet.append(last_byte)
if final_byte:
packet.append(final_byte)
print('last_byte', last_byte, packet_size + last_byte)
print('Packet bytes', packet)
return bytes(packet)
packet[2] -= 1
packet.append(final_byte)
print(packet)
return bytes(packet)
def getLampJSONData():
return sendInstruction(0x2e, 0, [0xff])
def setPredefinedLight(lightId): # IDs are between 0 and 19
"""
0: Strong white/yellow (strong, warm)
1: Candlelight
2: Morning Light (strong, warm)
3: Nature Light (strong, white)
4: Snow Light (strong, cold)
5: Squirrel Light (soft, red)
6: Coffee Light (soft, chill)
7: Desk Light (strong, good for working)
8: Hipster food light. (strong, very white)
9: Yellow light (soft, warm)
# 10 - 19 : Animations
10: 0xa => Buggy? red (soft, pure red)
11: 0xb => Slow rotation of all white luminescent colors
12: 0xc => Slow rotation from yellow (Morning)
13: 0xd => Circle (cycles on all colors smoothly)
14: 0xe => Party (cycles on all colors less smoothly)
15: 0xf => Romantic colors (Smooth from purple to red)
16: 0x10 => Smooth yellow/red
17: 0x11 => Blue wave
18: 0x12 => Strong green gradient
19: 0x13 => Strong white/yellow alternate
"""
print("setPredefinedLight : %s" %lightId)
return sendInstruction(0xa5, 1, [lightId])
def setWarmth(value): #value between 0 and 200
print("setWarmth : %s" %value)
return sendInstruction(0xa1, 1, [value], 94)
def setLuminance(value): #value between 0 and 200
print("setLuminance : %s" %value)
return sendInstruction(0xa7, 1, [value])
def setCustomColor(r, g, b): # value between 0 and 255
# For colors w/ a packet length > 1266
print("setCustomColor : rgb(%s, %s, %s)" %(r, g, b))
return sendInstruction(0xa1, 1, [r,g,b])
def setAbsoluteColor(color):
# if the packet size is < 1266 it must be 1010
if color == "red":
return sendInstruction(0xa1, 1, [255, 0, 0], 94)
elif color == "green":
return sendInstruction(0xa1, 1, [0, 255, 0], 94)
elif color == "blue":
return sendInstruction(0xa1, 1, [0, 0, 255], 94)
def turnOnOff(on):
print("turnOnOff : %s" %on)
value = 17 if on else 18
return sendInstruction(0xa3, 1, [value])