def write_file(controller, filename, raw=False): """Write File Routine @param controller the controller object instance. @param filename The specified filename to write the contents to. @param raw the raw object instance setting. True or False.""" with open(filename, "r") as output_file: corrupt_entries = 0 message_count = 0 start_time = time.time() for line in output_file: try: parsed_message = JsonFormatter.deserialize(line.encode("utf-8")) if not isinstance(parsed_message, dict): raise ValueError() except ValueError: corrupt_entries += 1 else: # TODO at the moment it's taking longer to write all of # individual CAN messages than the time that actually # elapsed in receiving the trace - need to implement # batching to speed this up. right now this will never sleep # because it's always behind. if 'timestamp' in parsed_message: time.sleep(max(.0002, ( parsed_message['timestamp'] + start_time) - time.time())) message_count += 1 controller.write(raw=raw, **parsed_message) print("%d lines sent" % message_count) if corrupt_entries > 0: print("%d invalid lines in the data file were not sent" % corrupt_entries)
def write_file(interface, filename): first_timestamp = None with open(filename, "r") as output_file: corrupt_entries = 0 message_count = 0 start_time = time.time() for line in output_file: try: parsed_message = JsonFormatter.deserialize(line.encode("utf-8")) if not isinstance(parsed_message, dict): raise ValueError() except ValueError: corrupt_entries += 1 else: # TODO at the moment it's taking longer to write all of # individual CAN messages than the time that actually # elapsed in receiving the trace - need to implement # batching to speed this up. right now this will never sleep # because it's always behind. timestamp = parsed_message.get('timestamp', None) # TODO this duplicates some code from sources/trace.py if timestamp is not None: first_timestamp = first_timestamp or timestamp target_time = start_time + (timestamp - first_timestamp) time.sleep(max(.0002, target_time - time.time())) message_count += 1 interface.write(**parsed_message) print("%d lines sent" % message_count) if corrupt_entries > 0: print("%d invalid lines in the data file were not sent" % corrupt_entries)
def write_file(interface, filename): first_timestamp = None with open(filename, "r") as output_file: corrupt_entries = 0 message_count = 0 start_time = time.time() for line in output_file: try: parsed_message = JsonFormatter.deserialize( line.encode("utf-8")) if not isinstance(parsed_message, dict): raise ValueError() except ValueError: corrupt_entries += 1 else: # TODO at the moment it's taking longer to write all of # individual CAN messages than the time that actually # elapsed in receiving the trace - need to implement # batching to speed this up. right now this will never sleep # because it's always behind. timestamp = parsed_message.get('timestamp', None) # TODO this duplicates some code from sources/trace.py if timestamp is not None: first_timestamp = first_timestamp or timestamp target_time = start_time + (timestamp - first_timestamp) time.sleep(max(.0002, target_time - time.time())) message_count += 1 interface.write(**parsed_message) print(("%d lines sent" % message_count)) if corrupt_entries > 0: print(("%d invalid lines in the data file were not sent" % corrupt_entries))
def write_file(controller, filename, raw=False): with open(filename, "r") as output_file: corrupt_entries = 0 message_count = 0 start_time = time.time() for line in output_file: try: parsed_message = JsonFormatter.deserialize( line.encode("utf-8")) if not isinstance(parsed_message, dict): raise ValueError() except ValueError: corrupt_entries += 1 else: # TODO at the moment it's taking longer to write all of # individual CAN messages than the time that actually # elapsed in receiving the trace - need to implement # batching to speed this up. right now this will never sleep # because it's always behind. if 'timestamp' in parsed_message: time.sleep( max(.0002, (parsed_message['timestamp'] + start_time) - time.time())) message_count += 1 controller.write(raw=raw, **parsed_message) print("%d lines sent" % message_count) if corrupt_entries > 0: print("%d invalid lines in the data file were not sent" % corrupt_entries)
def write_translated(self, name, value, event): """Send a translated write request to the VI. """ data = {'name': name} if value is not None: data['value'] = self._massage_write_value(value) if event is not None: data['event'] = self._massage_write_value(event); message = JsonFormatter.serialize(data) bytes_written = self.write_bytes(message) assert bytes_written == len(message) return bytes_written
def main(): configure_logging() arguments = parse_options() if arguments.split == "trip": splitter = TripSplitter() else: splitter = TimeSplitter(arguments.split) for key, split in splitter.split(arguments.files).items(): with open("%s.json" % key, "w") as output_file: for record in split: output_file.write(JsonFormatter.serialize(record) + "\n")
def main(): configure_logging() arguments = parse_options() if arguments.split == 'trip': splitter = TripSplitter() else: splitter = TimeSplitter(arguments.split) for key, split in list(splitter.split(arguments.files).items()): with open("%s.json" % key, 'w') as output_file: for record in split: output_file.write(JsonFormatter.serialize(record) + "\n")
def _parse_json_message(self, message_buffer): parsed_message = None remainder = message_buffer message = "" if b"\x00" in message_buffer: message, _, remainder = message_buffer.partition(b"\x00") try: parsed_message = JsonFormatter.deserialize(message) if not isinstance(parsed_message, dict): raise ValueError() except ValueError: pass return parsed_message, remainder, len(message)
def write_translated(self, name, value, event): """Format the given signal name and value into an OpenXC write request and write it out to the controller interface as bytes, ending with a \0 character. """ data = {'name': name} if value is not None: data['value'] = self._massage_write_value(value) if event is not None: data['event'] = self._massage_write_value(event); message = JsonFormatter.serialize(data) bytes_written = self.write_bytes(message + "\x00") assert bytes_written == len(message) + 1 return bytes_written
def write_translated(self, name, value, event): """Format the given signal name and value into an OpenXC write request and write it out to the controller interface as bytes, ending with a \0 character. """ data = {'name': name} if value is not None: data['value'] = self._massage_write_value(value) if event is not None: data['event'] = self._massage_write_value(event) message = JsonFormatter.serialize(data) bytes_written = self.write_bytes(message + "\x00") assert bytes_written == len(message) + 1 return bytes_written
def write_raw(self, message_id, data, bus=None): """Send a raw write request to the VI. """ if not isinstance(message_id, numbers.Number): try: message_id = int(message_id, 0) except ValueError: raise ValueError("ID must be numerical") data = {'id': message_id, 'data': data} if bus is not None: data['bus'] = bus message = JsonFormatter.serialize(data) bytes_written = self.write_bytes(message) assert bytes_written == len(message) return bytes_written
def read(self): """Read a line of data from the input source at a time.""" line = self.trace_file.readline() if line == "": if self.loop: self._reopen_file() else: self.trace_file.close() self.trace_file = None raise DataSourceError() message = JsonFormatter.deserialize(line) timestamp = message.get("timestamp", None) if self.realtime and timestamp is not None: self._store_timestamp(timestamp) self._wait(self.starting_time, self.first_timestamp, timestamp) return line + "\x00"
def read(self): """Read a line of data from the input source at a time.""" line = self.trace_file.readline() if line == '': if self.loop: self._reopen_file() else: self.trace_file.close() self.trace_file = None raise DataSourceError() message = JsonFormatter.deserialize(line) timestamp = message.get('timestamp', None) if self.realtime and timestamp is not None: self._store_timestamp(timestamp) self._wait(self.starting_time, self.first_timestamp, timestamp) return line + "\x00"
def complex_request(self, request, wait_for_first_response=True): """Send a compound command request to the interface over the normal data channel. request - A dict storing the request to send to the VI. It will be serialized to JSON, as that is the only supported format for commands on the VI in the current firmware. wait_for_first_response - If true, this function will block waiting for a response from the VI and return it to the caller. Otherwise, it will send the command and return immediately and any response will be lost. """ self.write_bytes(JsonFormatter.serialize(request)) response = None if wait_for_first_response: response = self._wait_for_response(request) return response
def write_raw(self, message_id, data): """Format the given CAN ID and data into a JSON message and write it out to the controller interface as bytes, ending with a \0 character. TODO this could write to a separate USB endpoint that is expecting raw-style JSON messages. """ if not isinstance(message_id, numbers.Number): try: message_id = int(message_id, 0) except ValueError: raise ValueError("ID must be numerical") message = JsonFormatter.serialize({'id': message_id, 'data': data}) bytes_written = self.write_bytes(message + "\x00") assert bytes_written == len(message) + 1 return bytes_written
def _parse_message(self, message_buffer): """If a message can be parsed from the given buffer, return it and remove it. Returns the message if one could be parsed, otherwise None, and the remainder of the buffer. """ if not isinstance(message_buffer, bytes): message_buffer = message_buffer.encode("utf-8") parsed_message = None remainder = message_buffer message = "" if b"\n" in message_buffer: message, _, remainder = message_buffer.partition(b"\n") try: parsed_message = JsonFormatter.deserialize(message) if not isinstance(parsed_message, dict): raise ValueError() except ValueError: pass return parsed_message, remainder, len(message)
def receive(message, **kwargs): message['timestamp'] = time.time() print(JsonFormatter.serialize(message))
def receive(message, **kwargs): """Receive Routine @param message the message object instance. @param kwargs the kwargs object instance.""" message['timestamp'] = time.time() print(JsonFormatter.serialize(message))
def receive(message, **kwargs): message['timestamp'] = time.time() print((JsonFormatter.serialize(message)))