forked from BeauJBurroughs/GoveeAPI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller.py
152 lines (145 loc) · 4.62 KB
/
controller.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
from random import randint
import time
import pexpect
import signal
import sys
from constants import *
def int_to_hex(intv):
h = hex(intv).replace("0x", "")
while len(h) < 2:
h = "0" + h
return h
def get_on():
sig = (51 ^ 1 ^ 1)
bins = [51, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, sig]
bins_str = map(int_to_hex, bins)
return "".join(bins_str)
def get_off():
sig = (51 ^ 1)
bins = [51, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, sig]
bins_str = map(int_to_hex, bins)
return "".join(bins_str)
def get_rgb_hex(r,g,b):
sig = (51) ^ (5) ^ (2) ^ r ^ g ^ b
#bins = [51, 5, 2, r, g, b, 0, 255, 174, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, sig]
bins = [51, 5, 2, r, g, b, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, sig]
bins_str = map(int_to_hex, bins)
return "".join(bins_str)
def get_brightness_hex(bright):
bright = round((bright*255/100)) # converted to a percentage instead of value to 255.
sig = (51) ^ (4) ^ bright
bins = [51, 4, bright, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, sig]
bins_str = map(int_to_hex, bins)
return "".join(bins_str)
def get_music_mode(music,r,g,b):
if music == "Energic":
musicnum = 0
r,g,b = 0, 0, 0
elif music== "Spectrum":
musicnum = 1
elif music== "Rolling":
musicnum = 2
else:
musicnum = 3
r,g,b =0, 0, 0
sig = ((51)^(5)^(1)^musicnum^r^g^b)
bins = bins = [51, 5, 1, musicnum,0, r, g, b, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, sig]
bins_str = map(int_to_hex, bins)
return "".join(bins_str)
def get_scene(scene):
if scene == "Sunrise":
scenenum = 0
elif scene == "Sunset":
scenenum = 1
elif scene == "Movie":
scenenum = 4
elif scene == "Dating":
scenenum = 5
elif scene == "Romantic":
scenenum = 7
elif scene == "Blinking":
scenenum = 8
elif scene == "Candlelight":
scenenum = 9
else:
scenenum = 15
sig = ((51)^(5)^(4)^scenenum)
bins = [51, 5, 4, scenenum, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, sig]
bins_str = map(int_to_hex, bins)
return "".join(bins_str)
def write_data(data, addr):
dev = addr_dev_dict[addr]
command_str = f"gatttool -i hci0 -b {addr} --char-write-req -a {handle_hex} -n {data}"
for attempt in range(10):
try:
gatt = pexpect.spawn(command_str)
gatt.expect("Characteristic value was written successfully", timeout=2)
except pexpect.exceptions.TIMEOUT:
print(f"Failed to connect to {dev} {addr}")
continue
except pexpect.exceptions.EOF:
# these happen sometimes during a keepalive
# just retry until it's figured out.
continue
else:
break
else:
dev = addr_dev_dict[addr]
print(f"Failed all attempts to connect to {dev} {addr}")
def send_keepalive(addr):
hexstr = keepalive
write_data(hexstr,addr)
print(f"Sent keepalive to {addr_dev_dict[addr]}")
def turn_on(addr):
hexstr = get_on()
write_data(hexstr,addr)
print(f"Turned {addr_dev_dict[addr]} On")
def turn_off(addr):
hexstr = get_off()
write_data(hexstr,addr)
print(f"Turned {addr_dev_dict[addr]} Off")
def change_color(rgbt, addr):
r, g, b = rgbt
hexstr = get_rgb_hex(r,g,b)
print(hexstr)
write_data(hexstr, addr)
print(f"Changed {addr_dev_dict[addr]} color to {rgbt}")
def change_brightness(bright, addr):
hexstr = get_brightness_hex(bright)
print(hexstr)
write_data(hexstr, addr)
print(f"Changed {addr_dev_dict[addr]} brightness to {bright}%")
def change_music(music, rgbt, addr):
r, g, b = rgbt
hexstr = get_music_mode(music,r,g,b)
print(hexstr)
write_data(hexstr, addr)
if music=="Energic" or music=="Rhythm":
print(f"Changed {addr_dev_dict[addr]} Music mode to {music}")
else:
print(f"Changed {addr_dev_dict[addr]} Music mode to {music} and Color to {rgbt}")
def change_scene(scene, addr):
hexstr = get_scene(scene)
print(hexstr)
write_data(hexstr, addr)
print(f"Changed {addr_dev_dict[addr]} Scene to {scene}")
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
def change_color_all(rgbt):
for addr in devices.values():
change_color(rgbt, addr)
def change_brightness_both(bright):
for addr in devices.values():
change_brightness(bright, addr)
def gen_rand_color():
r = randint(0,255)
g = randint(0,255)
b = randint(0,255)
max_c = max([r,g,b])
factor = 255/max_c
rp = round(r*factor)
gp = round(g*factor)
bp = round(b*factor)
rgbt = (rp, gp, bp)
return rgbt