class XboxThread(threading.Thread): """Xbox Worker Thread""" def __init__(self, notify_window, addr, name, uuid, liveid, crypto, userhash, token): """Init Worker Thread Class.""" threading.Thread.__init__(self) self._notify_window = notify_window self.addr = addr self.name = name self.uuid = uuid self.liveid = liveid self.crypto = crypto self.userhash = userhash self.token = token self._console = None self.daemon = True self.start() def run(self): """Run Worker Thread.""" print("started worker") # Re-instantiate console in-thread to prevent gevent fuckery self._console = Console(self.addr, self.name, self.uuid, self.liveid) self._console.protocol.crypto = self.crypto # make a callback for on_system_input def on_text(console, payload): wx.PostEvent(self._notify_window, TextPromptEvent(payload)) print("reached connect") self._console.add_manager(TextManager) self._console.text.on_systemtext_input += functools.partial(on_text, self._console) status = self._console.connect(self.userhash, self.token) self._console.wait(1) if status == ConnectionState.Connected: self._notify_window.GetParent().GetParent().SetStatusText("Connected") wx.PostEvent(self._notify_window, ResultEvent("Connected")) print("Connected") self._console.protocol.serve_forever() else: self._notify_window.GetParent().GetParent().SetStatusText("Connection Failed") self._notify_window.GetParent().GetParent().SetStatusText("Disconnected") print("Disconnected") wx.PostEvent(self._notify_window, ResultEvent("Disconnected")) def abort(self): """abort worker thread.""" # Method for use by main thread to signal an abort if self._console: self._console.protocol.stop() self.join() def send_text(self, text): self._console.send_systemtext_input(text) self._console.finish_text_input()
def console(console_address, console_name, uuid_dummy, console_liveid, console_flags, public_key_bytes): c = Crypto.from_bytes(public_key_bytes) console = Console(console_address, console_name, uuid_dummy, console_liveid, console_flags, c.foreign_pubkey) console.add_manager(StumpManager) console.add_manager(MediaManager) console.add_manager(TextManager) console.add_manager(InputManager) return console
def console(console_address, console_name, console_uuid, console_liveid, console_flags, console_public_key): console = Console( address=console_address, name=console_name, uuid=console_uuid, liveid=console_liveid, flags=console_flags, public_key=console_public_key ) console.add_manager(StumpManager) console.add_manager(MediaManager) console.add_manager(TextManager) console.add_manager(InputManager) return console
class Xbox: def __init__(self): self.console = False self.console_data = False self.request_id = 0 def findDevice(self, tries=1): print("[Xbox.findDevice] called (Try #%d)" % tries) console = self.discovery(timeout=5, addr=os.environ['XBOX_IP']) if tries == 3: print("[Xbox.findDevice] Max tries reached. No consoles found") return False if isinstance(console, Console): self.console_data = { "address": console.address, "name": console.name, "uuid": console.uuid, "liveid": console.liveid, "flags": console.flags, "public_key": console.public_key, } else: self.console_data = self.findDevice(tries=tries + 1) return self.console_data # def fetchMediaStatus(self, tries = 1): # # print("[Xbox.fetchMediaStatus] called (Try #%d)" % tries) # if tries == 5: # return False # # if self.console.title_id == None: # self.console.protocol.serve_forever() # return self.fetchMediaStatus(tries = tries+1) # else: # return self.console.media_state # def onMediaState(self, state): print(state) print("[Xbox.onMediaState] called (State: %s)" % state) self.console.protocol.shutdown() return def onTimeout(self): self.console.protocol.stop() print("[Xbox.onTimeout] Connection timed out") self.console = False def getInstance(self, mode='default', connect=True): print("[Xbox.getInstance] called (mode: %s, connect: %s)" % (mode, connect)) if self.console_data == False: data = self.findDevice() if data == False: print("[Xbox.getInstance] Result of self.findDevice() = %s" % data) return False self.console = Console( address=self.console_data.get("address"), name=self.console_data.get("name"), uuid=self.console_data.get("uuid"), liveid=self.console_data.get("liveid"), flags=self.console_data.get("flags"), public_key=self.console_data.get("public_key"), ) if connect == False: return self.console else: print( "[Xbox.getInstance] Checking if console data is still up to date" ) console = self.findDevice() if console != False: print("[Xbox.getInstance] Connecting to Xbox") if mode == 'media': print("[Xbox.getInstance] Activated MediaManager (beta)") self.console.add_manager(MediaManager) #self.console.media.on_media_state += self.onMediaState if mode == 'stump': print("[Xbox.getInstance] Activated StumpManager (beta)") self.console.add_manager(StumpManager) #self.console.media.on_media_state += self.onMediaState state = self.console.connect() if state == ConnectionState.Connected: print("[Xbox.getInstance] Xbox Connected") self.console.wait(0.5) connected = True else: print( "[Xbox.getInstance] [ERROR] Could not connect to Xbox") conected = False else: print("[Xbox.getInstance] Xbox not found on network") connected = False if connected == True: return self.console else: return False def discovery(self, timeout, addr): return self.do_discovery(addr=addr, tries=timeout) def do_discovery(self, addr, tries): consoles = Console.discover(addr=addr, tries=tries) print("[Xbox.do_discovery] Consoles found:") print(consoles) if len(consoles) > 0: print("[Xbox.do_discovery] Console found") return consoles[0] else: print("[Xbox.do_discovery] No consoles found") return False def media_command(self, media_command): print("[Xbox.media_command] called (media_command: %s)" % media_command) action = self.console.media_command(0x54321, media_command, self.request_id) return action def power_on(self): if os.environ['XBOX_IP'] != "127.0.0.1": print( "[Xbox.power_on] Booting xbox from config settings (%s, %s)" % (os.environ['XBOX_IP'], os.environ['XBOX_LIVEID'])) Console.power_on(os.environ['XBOX_LIVEID'], os.environ['XBOX_IP'], tries=10) time.sleep(1) Console.power_on(os.environ['XBOX_LIVEID'], os.environ['XBOX_IP'], tries=10) time.sleep(1) Console.power_on(os.environ['XBOX_LIVEID'], os.environ['XBOX_IP'], tries=10) data = True else: console = self.getInstance(connect=False) if console != False: print( "[Xbox.power_on] Booting xbox from discovery settings(%s, %s)" % (self.console_data.get("address"), self.console.get("liveid"))) Console.power_on(self.console_data.get("liveid"), self.console.get("address"), tries=10) time.sleep(1) Console.power_on(self.console_data.get("liveid"), self.console.get("address"), tries=10) time.sleep(1) Console.power_on(self.console_data.get("liveid"), self.console.get("address"), tries=10) data = True else: data = [ 'No device found in cache. Turn on your xbox and run /api/v1/discovery' ] return data def power_off(self): self.console.power_off() self.console.wait(1) self.console = False return True def close(self): print("[Xbox.close] called ()") self.console.disconnect() self.console = False return True def connect(timeout): discovered = Console.discover(timeout=1) if len(discovered): return discovered[0] return discovered