def _playback(cls): """ Reads data from a file and plays it back on the emulator. """ try: with open(cls._args.data_source, "rb") as in_f: lines = in_f.readlines() recorded_data = [] for line in lines: line = line.decode("utf-8") recorded_data.append(json.loads(line)) except IOError: logging.critical("Failed to open {0}." .format(cls._args.data_source)) return -1 keyboard = Keyboard() prev_time = 0 while not cls._phys.receive_message(timeout_ms=500): pass for data in recorded_data: if data["type"] == "keyboard": payload = keyboard.encode(data) time.sleep((data["timestamp"] - prev_time) / 1000.0) prev_time = data["timestamp"] if not cls._transmit_with_retry(payload): return -1 return 0
def _playback(cls): """ Reads data from a file and plays it back on the emulator. """ try: with open(cls._args.data_source, "rb") as in_f: lines = in_f.readlines() recorded_data = [] for line in lines: recorded_data.append(json.loads(line)) except IOError: logging.critical("Failed to open {0}." .format(cls._args.data_source)) return -1 keyboard = Keyboard() prev_time = 0 while not cls._phys.receive_message(timeout_ms=500): pass for data in recorded_data: if data["type"] == "keyboard": payload = keyboard.encode(data) time.sleep((data["timestamp"] - prev_time) / 1000.0) prev_time = data["timestamp"] if not cls._transmit_with_retry(payload): return -1 return 0
def _record(cls): """ Reads data from the selected input device and stores it to a file. The data is also forwarded to the emulator. """ # pylint: disable=too-many-branches def keyboard_formatter(timestamp, buf): """ Handler for recording the events. """ return {"type": "keyboard", "timestamp": timestamp, "buffer": buf} def control(command): """ Handler for passing commands. """ return {"type": "control", "command": command} keyboard = Keyboard() handlers = {"keyboard": keyboard_formatter, "control": control} cls._in_queue = start_ui(handlers) while True: if cls._in_queue.empty() is False: data = cls._in_queue.get() if data["type"] == "control": if data["command"] == "Start": output_data = [] elif data["command"] in ("Stop", "Quit"): if "output_data" in locals(): try: with open(cls._args.data_sink, "wb") as out_f: for line in output_data: line = line.decode("utf-8") out_f.write(json.dumps(line) + '\n') except IOError: logging.critical("Cannot save data to {0}." .format(cls._args.data_sink)) return -1 if data["command"] == "Quit": wait_for_ui() return 0 elif data["type"] == "keyboard": output_data.append(data) payload = keyboard.encode(data) if not cls._transmit_with_retry(payload): return -1 else: if cls._phys.receive_message(timeout_ms=500) is False: logging.critical("Device is dead\n") # pylint: enable=too-many-branches return 0
def _record(cls): """ Reads data from the selected input device and stores it to a file. The data is also forwarded to the emulator. """ # pylint: disable=too-many-branches def keyboard_formatter(timestamp, buf): """ Handler for recording the events. """ return {"type": "keyboard", "timestamp": timestamp, "buffer": buf} def control(command): """ Handler for passing commands. """ return {"type": "control", "command": command} keyboard = Keyboard() handlers = {"keyboard": keyboard_formatter, "control": control} cls._in_queue = start_ui(handlers) while True: if cls._in_queue.empty() is False: data = cls._in_queue.get() if data["type"] == "control": if data["command"] == "Start": output_data = [] elif data["command"] in ("Stop", "Quit"): if "output_data" in locals(): try: with open(cls._args.data_sink, "wb") as out_f: for line in output_data: out_f.write(json.dumps(line) + '\n') except IOError: logging.critical("Cannot save data to {0}." .format(cls._args.data_sink)) return -1 if data["command"] == "Quit": wait_for_ui() return 0 elif data["type"] == "keyboard": output_data.append(data) payload = keyboard.encode(data) if not cls._transmit_with_retry(payload): return -1 else: if cls._phys.receive_message(timeout_ms=500) is False: logging.critical("Device is dead\n") # pylint: enable=too-many-branches return 0