def put(self, request): service_ip = socket.gethostbyname(socket.gethostname()) client_ip = self.get_client_ip(request) client_id = request.data.get('client_id', 'Unknown') # Figure out how the color is specified c = Converter() if 'rgb' in request.data: r, g, b = request.data['rgb'] xy = c.rgb_to_xy(r, g, b) elif 'hex' in request.data: xy = c.hex_to_xy(request.data['hex']) hex_color = c.xy_to_hex(*xy) # Log the request if a database is present if settings.DATABASE_FOUND: ce = ChangeEvent(service_ip=service_ip, client_ip=client_ip, client_id=client_id, color=hex_color) ce.save() # There should probably be a throttle in place, but I can control # that elsewhere in the demo if not settings.BRIDGE_MOCK: self.bridge().set_light(settings.BRIDGE_LIGHT, 'xy', xy) return Response()
def get(self, request): hex_color = None result = { 'hex': None, 'on': None, 'hue': None, 'brightness': None, 'saturation': None, } # See if we can get the current hue from the light itself if not settings.BRIDGE_MOCK: try: light = self.bridge().get_light(settings.BRIDGE_LIGHT) c = Converter() state = light['state'] hex_color = c.xy_to_hex(*state['xy']) result['on'] = state['on'] result['hue'] = state['hue'] result['brightness'] = state['bri'] result['saturation'] = state['sat'] except phue.PhueRequestTimeout: print('Could not connect to bridge at %s' % settings.BRIDGE_IP) # If we can't, check the database if hex_color is None: qs = ChangeEvent.objects.order_by('-timestamp')[:1] if len(qs) > 0: hex_color = qs[0].color # Worst case, default to white if hex_color is None: hex_color = 'ffffff' result['hex'] = hex_color return Response(data=result)
class Light: def __init__(self, bridge, light_id): self.color_converter = Converter(GamutA) self.speed = 0 self.userid = bridge.userid self.ipaddress = bridge.ipaddress self.light_id = light_id self.bridge = bridge self.strobe = False def get_status(self): return self.bridge.get_state(self.light_id) def on(self): task = {'on': True} self.get_color() self.bridge.set_state(task, self.light_id) def off(self): task = {'on': False} self.bridge.set_state(task, self.light_id) def brightness(self, percent): task = {"bri": percent} self.bridge.set_state(task, self.light_id) def strobe_start(self, colors, speed, is_random): prev_color = '' self.strobe = True self.speed = speed while self.strobe and self.speed < 4.5: if is_random: # Needs to be cleaned up to match non-random new_colors = colors[:] if prev_color != '': new_colors.remove(prev_color) prev_color = random.choice(new_colors) self.color(prev_color) time.sleep(int(self.speed)) else: color_index = 0 while color_index < len(colors) and self.speed < 4.5: self.color(colors[color_index]) prev_speed = self.speed no_itr = self.speed / 0.1 for i in range(int(no_itr)): if prev_speed != self.speed: break time.sleep(0.1) if self.speed == prev_speed: color_index += 1 elif color_index == len(colors) - 1: color_index = 0 def strobe_stop(self): self.strobe = False def strobe_speed(self, speed): self.speed = speed def color(self, color): xy_color = color[1:] # Remove '#' from hex value xy_color = self.color_converter.hex_to_xy(xy_color) try: task = {"xy": xy_color} self.bridge.set_state(task, self.light_id) except KeyError: raise ValueError('Color does not exist') from None def get_color(self): xy_color = self.get_status()['xy'] hex_color = self.color_converter.xy_to_hex(xy_color[0], xy_color[1]) return hex_color
def loop(): lastColor = "" while True: uri = "http://" + HUE_BRIDGE_IP + "/api/gBy8DB7KImW4A4KTOZFMndlc1Cqq3Fvgr13MSLpH/lights/13" response = requests.get(uri) if response: data = response.json() converter = Converter(gamut=GamutC) print(data) # Hard-code the brightness since it looks better bri = data["state"]["bri"] / 255 rgb = converter.xy_to_rgb(data["state"]["xy"][0], data["state"]["xy"][1], bri=bri) if str(rgb) != lastColor: # Cache last value lastColor = str(rgb) # Update liquidctl hex_adjusted = converter.xy_to_hex(data["state"]["xy"][0], data["state"]["xy"][1], bri=LED_BRI) liquidctlpath = "C:\\Users\\Alex\\Documents\\GitHub\\hue_poll\\liquidctl-1.1.0-bin-windows-x86_64\\liquidctl.exe" # subprocess.Popen([liquidctlpath, "set", "sync", "color", "fixed", hex_adjusted], shell=True) subprocess.Popen([ liquidctlpath, "set", "sync", "color", "fixed", hex_adjusted ], shell=True) # Update MSIRBG # (r, g, b) = converter.xy_to_rgb(data["state"]["xy"][0],data["state"]["xy"][1], bri=led_bri) # r = adjust(r, 100) # g = adjust(g, 100) # b = adjust(b, 100) # print(r, g, b) # huepollmsipath = "C:\\Users\\Alex\\Documents\\GitHub\\HuePollMSIRGB\\HuePollMSIRGB\\bin\\x64\\Debug\\HuePollMSIRGB.exe" # subprocess.Popen([huepollmsipath, str(r), str(g), str(b)], shell=True) # Update Arduino (r, g, b) = converter.xy_to_rgb(data["state"]["xy"][0], data["state"]["xy"][1], bri=LED_BRI) sendRGB(r, g, b) # Update windows background processImages() (r, g, b) = converter.xy_to_rgb(data["state"]["xy"][0], data["state"]["xy"][1], bri=DESKTOP_BRI) bestImg = getClosestImg([r, g, b]) if bestImg is not None: print('setting bg to:' + bestImg) ctypes.windll.user32.SystemParametersInfoW( 20, 0, bestImg, 0) print('Success: ' + str(rgb)) else: print('Request failed') time.sleep(POLL_INTERVAL)