class PS3RemoteThread ( StoppableThread ): def __init__(self, csock, isock, ipaddr="127.0.0.1"): StoppableThread.__init__(self) self.csock = csock self.isock = isock self.xbmc = XBMCClient(name="PS3 Blu-Ray Remote", icon_file=ICON_PATH + "/bluetooth.png", ip=ipaddr) self.set_timeout(600) self.services = [] self.current_xbmc = 0 def run(self): self.xbmc.connect() try: # start the zeroconf thread if possible try: self.zeroconf_thread = ZeroconfThread() self.zeroconf_thread.add_service('_xbmc-events._udp', self.zeroconf_service_handler) self.zeroconf_thread.start() except Exception as e: print(str(e)) # main thread loop while not self.stop(): status = process_remote(self.isock, self.xbmc) if status == 2: # 2 = socket read timeout if self.timed_out(): raise Exception("PS3 Blu-Ray Remote powering off, "\ "timed out") elif status == 3: # 3 = ps and skip + self.next_xbmc() elif status == 4: # 4 = ps and skip - self.previous_xbmc() elif not status: # 0 = keys are normally processed self.reset_timeout() # process_remote() will raise an exception on read errors except Exception as e: print(str(e)) self.zeroconf_thread.stop() self.close_sockets() def next_xbmc(self): """ Connect to the next XBMC instance """ self.current_xbmc = (self.current_xbmc + 1) % len( self.services ) self.reconnect() return def previous_xbmc(self): """ Connect to the previous XBMC instance """ self.current_xbmc -= 1 if self.current_xbmc < 0 : self.current_xbmc = len( self.services ) - 1 self.reconnect() return def reconnect(self): """ Reconnect to an XBMC instance based on self.current_xbmc """ try: service = self.services[ self.current_xbmc ] print("Connecting to %s" % service['name']) self.xbmc.connect( service['address'], service['port'] ) self.xbmc.send_notification("PS3 Blu-Ray Remote", "New Connection", None) except Exception as e: print(str(e)) def zeroconf_service_handler(self, event, service): """ Zeroconf event handler """ if event == zeroconf.SERVICE_FOUND: # new xbmc service detected self.services.append( service ) elif event == zeroconf.SERVICE_LOST: # xbmc service lost try: # search for the service by name, since IP+port isn't available for s in self.services: # nuke it, if found if service['name'] == s['name']: self.services.remove(s) break except: pass return
# wait for events from the rfid reader for event in dev.read_loop(): # if we get a "key pressed down" event if event.type == ecodes.EV_KEY and event.value == 1: # if it is the enter key we've hit the end of a card number if event.code == 28: # merge the list in to a string card = ''.join(str(num) for num in cardnumber) # get the playlist which is assigned to this card number # if it doesn't have one assigned, set up the error message flag playlist = db.get(card,None) # if the card didn't have a playlist assigned, fire api call to display msg in xbmc if playlist == None: xbmc.send_notification(PROGRAM, "Card has no playlist assigned", "3000") print >> sys.stderr, "Card {c} has no playlist assigned".format(c=card) # check if this playlist is one of the "special" ones elif playlist == "shuffle": print "Card {c} is assigned to {p}".format(c=card, p=playlist) xbmc.send_action("XBMC.PlayerControl(Random)") elif playlist == "reboot": print "Card {c} is assigned to {p}".format(c=card, p=playlist) xbmc.send_action("XBMC.Reset") # if we do have a playlist we use send_action to fire off the PlayMedia command else: if playlist.endswith('.m3u'): print "Card's playlist has m3u extension" else: print "Card's playlist didn't have m3u extension" playlist = playlist + '.m3u'
class PS3RemoteThread(StoppableThread): def __init__(self, csock, isock, ipaddr="127.0.0.1"): StoppableThread.__init__(self) self.csock = csock self.isock = isock self.xbmc = XBMCClient(name="PS3 Blu-Ray Remote", icon_file=ICON_PATH + "/bluetooth.png", ip=ipaddr) self.set_timeout(600) self.services = [] self.current_xbmc = 0 def run(self): self.xbmc.connect() try: # start the zeroconf thread if possible try: self.zeroconf_thread = ZeroconfThread() self.zeroconf_thread.add_service('_xbmc-events._udp', self.zeroconf_service_handler) self.zeroconf_thread.start() except Exception as e: print(str(e)) # main thread loop while not self.stop(): status = process_remote(self.isock, self.xbmc) if status == 2: # 2 = socket read timeout if self.timed_out(): raise Exception("PS3 Blu-Ray Remote powering off, "\ "timed out") elif status == 3: # 3 = ps and skip + self.next_xbmc() elif status == 4: # 4 = ps and skip - self.previous_xbmc() elif not status: # 0 = keys are normally processed self.reset_timeout() # process_remote() will raise an exception on read errors except Exception as e: print(str(e)) self.zeroconf_thread.stop() self.close_sockets() def next_xbmc(self): """ Connect to the next XBMC instance """ self.current_xbmc = (self.current_xbmc + 1) % len(self.services) self.reconnect() return def previous_xbmc(self): """ Connect to the previous XBMC instance """ self.current_xbmc -= 1 if self.current_xbmc < 0: self.current_xbmc = len(self.services) - 1 self.reconnect() return def reconnect(self): """ Reconnect to an XBMC instance based on self.current_xbmc """ try: service = self.services[self.current_xbmc] print("Connecting to %s" % service['name']) self.xbmc.connect(service['address'], service['port']) self.xbmc.send_notification("PS3 Blu-Ray Remote", "New Connection", None) except Exception as e: print(str(e)) def zeroconf_service_handler(self, event, service): """ Zeroconf event handler """ if event == zeroconf.SERVICE_FOUND: # new xbmc service detected self.services.append(service) elif event == zeroconf.SERVICE_LOST: # xbmc service lost try: # search for the service by name, since IP+port isn't available for s in self.services: # nuke it, if found if service['name'] == s['name']: self.services.remove(s) break except: pass return
# wait for events from the rfid reader for event in dev.read_loop(): # if we get a "key pressed down" event if event.type == ecodes.EV_KEY and event.value == 1: # if it is the enter key we've hit the end of a card number if event.code == 28: # merge the list in to a string card = ''.join(str(num) for num in cardnumber) # get the playlist which is assigned to this card number # if it doesn't have one assigned, set up the error message flag playlist = db.get(card, None) # if the card didn't have a playlist assigned, fire api call to display msg in xbmc if playlist == None: xbmc.send_notification(PROGRAM, "Card has no playlist assigned", "3000") print >> sys.stderr, "Card {c} has no playlist assigned".format( c=card) # check if this playlist is one of the "special" ones elif playlist == "shuffle": print "Card {c} is assigned to {p}".format(c=card, p=playlist) xbmc.send_action("XBMC.PlayerControl(Random)") elif playlist == "reboot": print "Card {c} is assigned to {p}".format(c=card, p=playlist) xbmc.send_action("XBMC.Reset") # if we do have a playlist we use send_action to fire off the PlayMedia command else: if playlist.endswith('.m3u'): print "Card's playlist has m3u extension" else: print "Card's playlist didn't have m3u extension"