discovery.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:alexa-hue-bridge 作者: IndigoDomotics 项目源码 文件源码
def run(self):
        try:
            PLUGIN.responderLogger.debug("Responder.run called")
            sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
            try:
                sock.bind(('', UPNP_PORT))
                sock.setsockopt(socket.IPPROTO_IP,
                                socket.IP_ADD_MEMBERSHIP,
                                socket.inet_aton(BCAST_IP) + socket.inet_aton(PLUGIN.globals['alexaHueBridge'][self.ahbDevId]['host']))
                sock.settimeout(1)
                start_time = time.time()
                end_time = start_time + (PLUGIN.globals['alexaHueBridge'][self.ahbDevId]['discoveryExpiration'] * 60)
                while True:
                    try:
                        data, addr = sock.recvfrom(1024)
                        # Following code will only time out the Broadcaster Thread if PLUGIN.globals['alexaHueBridge'][self.ahbDevId]['discoveryExpiration'] > 0 (valid values 0 thru 10 inclusive)
                        # A value of zero means 'always on'
                        if PLUGIN.globals['alexaHueBridge'][self.ahbDevId]['discoveryExpiration'] and time.time() > end_time:
                            PLUGIN.responderLogger.debug("Responder.run thread timed out")
                            self.stop()
                            raise socket.error
                    except socket.error:
                        if self.interrupted:
                            PLUGIN.responderLogger.debug("Responder.run: self.interrupted: True")
                            PLUGIN.setDeviceDiscoveryState(False, self.ahbDevId)
                            sock.close()
                            return
                    else:
                        if M_SEARCH_REQ_MATCH in data:
                            PLUGIN.responderLogger.debug("Responder.run: received: {}".format(str(data)))
                            self.respond(addr)
            except socket.error as e:
                # This is the exception thrown when someone else has bound to the UPNP port, so write some errors and
                # stop the thread (which really isn't needed, but it logs a nice stop debug message).
                if e.errno == errno.EADDRINUSE:
                    PLUGIN.responderLogger.error(u"'{}' Responder startup failed because another app or plugin is using the UPNP port.".format(indigo.devices[self.ahbDevId].name))
                    PLUGIN.responderLogger.error(u"Open a terminal window and type 'sudo lsof -i :{}}' to see a list of processes that have bound to that port and quit those applications.".format(UPNP_PORT))
                    self.stop()
                elif e.errno == errno.EADDRNOTAVAIL:
                    PLUGIN.responderLogger.error(u"'{}' Responder startup failed because host address is not available.".format(indigo.devices[self.ahbDevId].name))
                    PLUGIN.responderLogger.error(u"Double check that the host is specified correctly in the Plugin Config. Correct if invalid and then reload the plugin.")
                    self.stop()
                else:
                    PLUGIN.responderLogger.error("'{}' Responder.run: socket error: {}".format(indigo.devices[self.ahbDevId].name, e))

            PLUGIN.setDeviceDiscoveryState(False, self.ahbDevId)
        except StandardError, e:
            PLUGIN.responderLogger.error(u"StandardError detected in Responder.Run for '{}'. Line '{}' has error='{}'".format(indigo.devices[self.ahbDevId].name, sys.exc_traceback.tb_lineno, e))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号