/
index.py
52 lines (41 loc) · 1.2 KB
/
index.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
#!/usr/bin/env python
import sys
import time
import json
from qhue import Bridge
from rgbxy import Converter
from rgbxy import GamutC
from chirpsdk import ChirpConnect, CallbackSet
class Callbacks(CallbackSet):
def on_received(self, payload, channel):
if payload is not None:
hex = json.loads(payload.decode('utf-8'))
print('Received: ' + hex['c'])
hueUtil = HueUtil()
hueUtil.hex(hex['c'])
else:
print('Decode failed')
class HueUtil:
ipAddress = "d780c907.ngrok.io"
userId = "IbUDZzSJqJwaMEuH3hQzXoZKPzqsy3Qvus2Io8Vr"
lightId = 2
b = Bridge(ipAddress, userId)
def hex(self, hexStr):
red = int(hexStr[1:3], 16)
green = int(hexStr[3:5], 16)
blue = int(hexStr[5:], 16)
converter = Converter(GamutC)
xy = converter.rgb_to_xy(red, green, blue)
print(xy)
self.b.lights(self.lightId, 'state', xy=xy)
chirp = ChirpConnect()
chirp.start(send=True, receive=True)
chirp.set_callbacks(Callbacks())
try:
# Process audio streams
while True:
time.sleep(0.1)
sys.stdout.write('.')
sys.stdout.flush()
except KeyboardInterrupt:
print('Exiting')