Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions src/LIFX Menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def applicationDidFinishLaunching_(self, notification):
subitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_( 'White', 'setColour:', '')
subitem.setRepresentedObject_( 'White' )
submenu.addItem_( subitem )
subitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_( 'Bright White', 'setColour:', '')
subitem.setRepresentedObject_( 'BrightWhite' )
submenu.addItem_( subitem )
subitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_( 'Red', 'setColour:', '')
subitem.setRepresentedObject_( 'Red' )
submenu.addItem_( subitem )
Expand All @@ -42,18 +45,26 @@ def applicationDidFinishLaunching_(self, notification):
subitem.setRepresentedObject_( 'Blue' )
submenu.addItem_( subitem )


for bulb in self.lights:
bulb.get_info()
submenu = NSMenu.alloc().init()
item = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_( bulb.get_addr(), '', '')
#name = bulb.get_addr()
#bulb.get_label()
name = bulb.bulb_label
item = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_( name, '', '')
item.setSubmenu_( submenu )
menu.addItem_( item )
subitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_( 'Toggle light', 'toggleBulb:', '')
subitem.setRepresentedObject_( bulb )
subitem.setState_(bulb.power)
submenu.addItem_( subitem )
subitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_( 'Soft light', 'softBulb:', '')
subitem.setRepresentedObject_( bulb )
submenu.addItem_( subitem )
subitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_( 'bright light', 'brightBulb:', '')
subitem.setRepresentedObject_( bulb )
submenu.addItem_( subitem )


quit_item = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Quit', 'terminate:', '')
menu.addItem_(quit_item)

Expand All @@ -71,24 +82,48 @@ def toggleBulb_(self, notification):
bulb.set_power(state)
notification.setState_(bulb.power)

def softBulb_(self, notification):
bulb = notification._.representedObject
if bulb.power :
hue = 9802
saturation = 10023
brightness = 65535 * 0.7
kelvin = 3000
bulb.set_color( hue, saturation, brightness, kelvin, 1000)

def brightBulb_(self, notification):
bulb = notification._.representedObject
if bulb.power :
hue = 54600
saturation = 0 #65535
brightness = 65535
kelvin = 7000
bulb.set_color( hue, saturation, brightness, kelvin, 1000)

def setColour_(self, notification):
hue = 0;
hue = 0
saturation = 65535
brightness = 65535
kelvin = 3500
colour = notification._.representedObject
if colour == 'White' :
hue = 9802
saturation = 10023
if colour == 'BrightWhite' :
hue = 9802
saturation = 0
kelvin = 7000
if colour == 'Red' :
hue = 0
saturation = 65535
if( colour == 'Green' ):
hue = 65535/360*120
hue = 21845 #65535/360*120
saturation = 65535
if( colour == 'Blue' ):
hue = 65535/360*240
hue = 43690 #65535/360*240
saturation = 65535
for bulb in self.lights:
bulb.set_color( hue, saturation, 65535, 3500, 1000)
bulb.set_color( hue, saturation, brightness, kelvin, 1000)


if __name__ == "__main__":
Expand Down
10 changes: 5 additions & 5 deletions src/lifx/lifx.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
lights = {}

def inttohex(n):
return unicode(hexlify(struct.pack('>H', n)), encoding='utf-8')
return str(hexlify(struct.pack('>H', n)), encoding='utf-8')

class LIFXBulb:
def __init__(self, lightstatus):
Expand All @@ -26,7 +26,7 @@ def __str__(self):
self.power)

def get_addr(self):
return unicode(hexlify(self.addr), encoding='utf-8')
return str(hexlify(self.addr), encoding='utf-8')

def deliverpacket(self, packet):
if isinstance(packet.payload, packetcodec.LightStatusPayload):
Expand All @@ -53,7 +53,7 @@ def recv_lightstatus(self, lightstatus):
self.power = True
else:
self.power = False
self.bulb_label = unicode(lightstatus.payload.data['bulb_label'],
self.bulb_label = str(lightstatus.payload.data['bulb_label'],
encoding='utf-8').strip('\00')
self.tags = lightstatus.payload.data['tags']

Expand All @@ -64,7 +64,7 @@ def recv_powerstate(self, powerstate):
self.power = False

def recv_bulblabelstate(self, labelstate):
self.bulb_label = unicode(labelstate.payload.data['bulb_label'],
self.bulb_label = str(labelstate.payload.data['bulb_label'],
encoding='utf-8').strip('\00')

def recv_timestate(self, timestate):
Expand All @@ -91,7 +91,7 @@ def get_state(self):

def set_power(self, power):
set_power(self.addr, power)
listen_and_interpret(5, packetcodec.PowerStatePayload, self.addr)
#listen_and_interpret(5, packetcodec.PowerStatePayload, self.addr)

def set_color(self, hue, saturation, brightness, kelvin, fade_time):
set_color(self.addr, hue, saturation, brightness, kelvin, fade_time)
Expand Down
62 changes: 38 additions & 24 deletions src/lifx/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
BCAST = '255.255.255.255'
PORT = 56700

targetaddr = None
connection = None
debug = False
site = b'\00\00\00\00\00\00'

def connect():
global connection, site
global connection, site, targetaddr
udpsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udpsock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
udpsock.bind((IP, PORT))
Expand All @@ -29,53 +30,66 @@ def connect():
if packet is not None and \
isinstance(packet.payload, packetcodec.PANGatewayPayload):
break
udpsock.close()
#udpsock.close()
if debug:
print 'found light: %s' % (addr[0], )
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.settimeout(2.0)
tcpsock.connect(addr)
connection = tcpsock
print( 'found light: %s' % (addr[0], ))
#tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#tcpsock.settimeout(2.0)
#tcpsock.connect(addr)
#connection = tcpsock
targetaddr = addr
udpsock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 0)
udpsock.settimeout(0.1)
connection = udpsock
site = packet.site
if debug:
print 'connection established with %s' % (unicode(hexlify(site),
encoding='utf-8'))
print( 'connection established with %s' % (unicode(hexlify(site),
encoding='utf-8')))

def sendpacket(p):
global connection, site
global connection, site, targetaddr
if connection is None:
connect()
p.site = site
if debug:
print p
connection.sendall(p.__bytes__())
print('sendpacket: ', p)
print('IP: ', targetaddr[0])
print('Port: ', targetaddr[1])
#connection.sendall(bytes(p))
connection.sendto(bytes(p), targetaddr)

def recvpacket(timeout = None):
global connection
if connection is None:
connect()
connection.settimeout(timeout)
#connection.settimeout(timeout)
#try:
# lengthdatum, addr = connection.recvfrom(2)
#except socket.timeout:
# return None
#connection.settimeout(None)
#try:
# (length, ) = struct.unpack('<H', lengthdatum)
#except struct.error:
# connect()
# return None
#data, addr = connection.recvfrom(length - 2)
try:
lengthdatum, addr = connection.recvfrom(2)
data, addr = connection.recvfrom(1024)
except socket.timeout:
return None
connection.settimeout(None)
try:
(length, ) = struct.unpack('<H', lengthdatum)
except struct.error:
connect()
return None
data, addr = connection.recvfrom(length - 2)
packet = packetcodec.decode_packet(lengthdatum + data)
packet = packetcodec.decode_packet(data)
if debug:
print packet
print('recvpacket(): ', packet)
return packet

def listenforpackets(seconds, desired = None, target = None):
start = time()
packets = []
if debug:
print('listenforpackets() Start: ', start)
while time() - start < seconds:
p = recvpacket(0.2)
p = recvpacket(0.1)
if p is not None:
packets.append(p)
if desired is not None and isinstance(p.payload, desired):
Expand Down
4 changes: 2 additions & 2 deletions src/lifx/packetcodec.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def encode(self):

def decode(self, bs):
if len(bs) != calcsize(self.pack_str):
print 'could not decode %s' % (self.name, )
print tohex(bs)
print( 'could not decode %s' % (self.name, ) )
print( tohex(bs))
return
data = unpack(self.pack_str, bs)
self.data = dict( zip(self.pack_struct, data) )
Expand Down