Hab es geschafft das er die Bridge nicht immer mit Packeten flutet sondern nur, wenn sich etwas geändert hat. Sonst konnte ich nämlich auch die anderen Gruppen nicht mehr ansteuern da die Bridge total busy war. Und so kann ich, wenn kein Film läuft, die Beleuchtung auch normal ansteuern.
import colorsys
import sys
import socket
#import time
class boblightMilightConnector:
def __init__(self):
self.readInputStream()
def readInputStream(self):
milight=milightController("192.168.1.121",8899)
while True:
input = sys.stdin.readline()
inputData=input.split(' ')
if(len(inputData)>3):
r = float(inputData[0])
g = float(inputData[1])
b = float(inputData[2])
milight.setRGB(r,g,b)
class milightController:
ip=None
port=None
sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Msg1State=""
Msg2State=""
def __init__(self,IP,Port):
self.ip=IP
self.port=Port
def setRGB(self,r,g,b):
h, s, v = colorsys.rgb_to_hsv(float(r), float(g), float(b))
if (r, g, b) == (0, 0, 0):
MESSAGE1 = "\x48\x00" # Turn Lights Off - ALL: x41 / 1:x46 / 2: x48 / 3: x4a / 4: x4c
if self.Msg1State != MESSAGE1:
self.sock.sendto(MESSAGE1, (self.ip, self.port))
self.Msg1State = MESSAGE1
#time.sleep(0.01)
return # we are done, no need to do more
elif (s<0.02):
MESSAGE1 = "\xC7\x00" # Turn Lights WHITE - All: xC2 / 1: xC5 / 2: xC7 / 3: xC9 / 4: xCB
if(h>0.3333):
htmp=h-0.3333
vtmp=htmp/0.6666
vtmp=(2*vtmp)
v=v/vtmp
else:
#Korrektur Gelb
if (h < 0.33333):
h= h *0.5
#Korrektur Cyan
if (h > 0.33333 and h < 0.5):
h= h*0.9
if(h>0.3333):
htmp=h-0.3333
vtmp=htmp/0.6666
vtmp=(2*vtmp)
v=v/vtmp
h = int((h) * 256)
#Korrektur Farbverschiebung
h=h+85
if(h>256):
h=256-(h%256)
else:
h=abs(h-256)
h=int(h)
MESSAGE1 = "\x40" + chr(h) # Set Color
if (v>=0.75):
v=0.75
v=v*25
v=int(round(v))
v=v+2
v=min(27,v)
v = max(2,v)
MESSAGE2 = "\x4E" + chr(v)
if self.Msg1State != MESSAGE1 or self.Msg2State != MESSAGE2:
self.sock.sendto("\x47\x00", (self.ip, self.port)) # Group ON (select Group) - All: x42 / 1: x45 / 2: x47 / 3: x49 / 4: x4B
#time.sleep(0.01)
self.sock.sendto(MESSAGE1, (self.ip, self.port)) # Set Color
#time.sleep(0.01)
self.sock.sendto(MESSAGE2, (self.ip, self.port)) # Set Brightness
self.Msg1State = MESSAGE1
self.Msg2State = MESSAGE2
#time.sleep(0.01)
boblightMilightConnector()
Alles anzeigen