示例#1
0
 def datagram_received(self, data, addr):
     response = unpack_lifx_message(data)
     response.ip_addr = addr[0]
     if type(response) == StateService and response.service == 1 : #Only look for UDP services
         #if self.parent.log:
             #self.parent.log.debug("DISC: " + str(response))
         #Discovered
         if response.target_addr not in self.lights and response.target_addr != BROADCAST_MAC:
             self.lights.append(response.target_addr)
             if self.ipv6prefix:
                 coro = self.loop.create_datagram_endpoint(
                     partial(Light,self.loop,response.target_addr, response.ip_addr, response.port, parent=self),
                     family = socket.AF_INET6, remote_addr=(mac_to_ipv6_linklocal(response.target_addr,self.ipv6prefix), response.port))
             else:
                 coro = self.loop.create_datagram_endpoint(
                     partial(Light,self.loop,response.target_addr, response.ip_addr, response.port,parent=self),
                     family = socket.AF_INET, remote_addr=(response.ip_addr, response.port))
             
             self.light_tp[response.target_addr] = self.loop.create_task(coro)
            
     elif type(response) == LightState and response.target_addr != BROADCAST_MAC:
         if response.target_addr not in self.lights :
             #looks like the lights are volunteering LigthState after booting 
             self.lights.append(response.target_addr)
             if self.ipv6prefix:
                 coro = self.loop.create_datagram_endpoint(
                     partial(Light,self.loop,response.target_addr, response.ip_addr, UDP_BROADCAST_PORT, parent=self),
                     family = socket.AF_INET6, remote_addr=(mac_to_ipv6_linklocal(response.target_addr,self.ipv6prefix), UDP_BROADCAST_PORT))
             else:
                 coro = self.loop.create_datagram_endpoint(
                     partial(Light,self.loop,response.target_addr, response.ip_addr, UDP_BROADCAST_PORT,parent=self),
                     family = socket.AF_INET, remote_addr=(response.ip_addr, UDP_BROADCAST_PORT))
             
             self.light_tp[response.target_addr] = self.loop.create_task(coro)
示例#2
0
    def runConcurrentThread(self):

        try:
            while True:

                if len(
                        self.publishedDevices
                ) > 0:  # no need to respond if there aren't any devices to emulate

                    try:
                        data, (ip_addr, port) = self.sock.recvfrom(1024)

                    except socket.timeout:
                        pass

                    except socket.error, msg:
                        self.debugLog('Socket recvfrom failed. Error Code : ' +
                                      str(msg[0]) + ' Message ' + msg[1])

                    else:
                        message = unpack_lifx_message(data)
                        self.lifxRespond(message, ip_addr, port)

                    self.sleep(0.1)  # short sleep for possible shutdown

                else:
                    self.sleep(
                        1.0)  # longer sleep when not looking for LIFX requests

                # Future: check here for pending responses or resends for commands

                # Plugin Update check

                if time.time() > self.next_update_check:
                    self.updater.checkForUpdate()
                    self.next_update_check = time.time() + float(
                        self.pluginPrefs['updateFrequency']) * 60.0 * 60.0

        except self.stopThread:
            pass
示例#3
0
 def datagram_received(self, data, addr):
     response = unpack_lifx_message(data)
     #if self.log:
         #self.log.debug("RECV: " + str(response))
     if response.seq_num in self.message:
         response_type,myevent,callb = self.message[response.seq_num]
         
         if type(response) == response_type:
             if response.origin == 1 and response.source_id == self.source_id:
                 self.ip_addr = addr
                 if "State" in response.__class__.__name__:
                     setmethod="resp_set_"+response.__class__.__name__.replace("State","").lower()
                     if setmethod in dir(self) and callable(getattr(self,setmethod)):
                         getattr(self,setmethod)(response)   
                 if callb:
                     callb(self,response)
                 myevent.set()
             del(self.message[response.seq_num])
         elif type(response) == Acknowledgement:
             pass
         else: #Total f**k up
             del(self.message[response.seq_num])