def __init__(self, pid, name, desc, target): PMCUStandardParameter.__init__(self, pid, name, desc, PMCUParameter.CU_INVALID_BYTE_INDEX(), PMCUParameter.CU_INVALID_BIT_INDEX(), target) self._cu_type = PMCUParameter.CU_TYPE_CALCULATED_PARAMETER() self._dependencies = []
def __init__(self, pid, name, desc, byte_index, bit_index, target): PMCUParameter.__init__(self, PMCUParameter.CU_TYPE_STD_PARAMETER()) self._id = pid self._name = name self._desc = desc self._byte_index = byte_index self._bit_index = bit_index self._target = target self._conversions = [] self._address = None
def __init__(self, pid, name, desc, address, byte_index, bit_index, target): PMCUStandardParameter.__init__(self, pid, name, desc, byte_index, bit_index, target) self._cu_type = PMCUParameter.CU_TYPE_SWITCH_PARAMETER() self.set_address(PMCUAddress(address, 1))
def endElement(self, name): if self._proto_id != "SSM": return if name == "parameter": self._parameters.add(self._parameter) self._parameter = None elif name == "ecuparam": self._parameters.add(self._parameter) self._parameter = None elif name == "switch": self._parameters.add(self._parameter) self._parameter = None elif name == "address": self._characters = self._characters.strip() if len(self._characters.strip()) > 0: if self._parameter.get_cu_type( ) == PMCUParameter.CU_TYPE_STD_PARAMETER(): self._parameter.set_address( PMCUAddress(int(self._characters, 16), self._address_length)) elif self._parameter.get_cu_type( ) == PMCUParameter.CU_TYPE_FIXED_ADDRESS_PARAMETER(): address = PMCUAddress(int(self._characters, 16), self._address_length) for ecu_id in self._ecu_ids: self._parameter.add_ecu_id(ecu_id, address) self._address_length = 0 self._characters = '' elif name == "ecu": self._ecu_ids = None self._element_no += 1 if self._element_no % 1000 == 0: self.log_progress()
def get_calculated_value(self, packets, unit=None): value = "" local_vars = locals() if len(self._conversions) > 0 and unit is None: unit = self._conversions[0].get_unit() for conversion in self._conversions: curr_unit = conversion.get_unit() expr = conversion.get_expr() value_format = conversion.get_format() conversion_map = {} if unit == curr_unit: param_pairs = re.findall(r'\[([^]]*)\]', expr) for pair in param_pairs: attributes = pair.split(":") key = attributes[0] unit = attributes[1] expr = expr.replace("[" + key + ":" + unit + "]", key) conversion_map.update({key: unit}) param_no = 0 for param in self._dependencies: if param.get_id() in conversion_map: conversion_unit = conversion_map[param.get_id()] else: conversion_unit = None if param.get_cu_type( ) == PMCUParameter.CU_TYPE_CALCULATED_PARAMETER(): return "ERROR DEPS" #param.get_calculated_value(packets, conversion_unit) else: value = param.get_value(packets[param_no], conversion_unit) local_vars[param.get_id()] = float(value) param_no += 1 try: value = eval(expr) except (SyntaxError, NameError): return "ERROR EVAL" except ZeroDivisionError: return "0.0" format_tokens = value_format.split(".") output_format = "%.0f" if len(format_tokens) > 1: output_format = "%." + str(len(format_tokens[1])) + "f" value = output_format % value return value
def match_parameters(self, parameters): matched = [] rom_id = self.get_rom_id() print 'rom id=' + rom_id for parameter in parameters: if parameter.get_target() not in self._targets: continue cu_type = parameter.get_cu_type() if cu_type == PMCUParameter.CU_TYPE_STD_PARAMETER(): if parameter.is_supported(self._packet.to_bytes()): matched.append(parameter) elif cu_type == PMCUParameter.CU_TYPE_FIXED_ADDRESS_PARAMETER(): if parameter.is_supported(rom_id): print 'match=' + parameter.get_id() parameter.switch_to_id(rom_id) matched.append(parameter) return matched
def match_calculated_parameters(self, parameters, supported_parameters): matched = [] for parameter in parameters: if parameter.get_target() not in self._targets: continue cu_type = parameter.get_cu_type() if cu_type == PMCUParameter.CU_TYPE_CALCULATED_PARAMETER(): if parameter.is_supported(supported_parameters): parameter.fill_dependencies(supported_parameters) matched.append(parameter) return matched
def match_switch_parameters(self, parameters): matched = [] for parameter in parameters: if parameter.get_target() not in self._targets: continue cu_type = parameter.get_cu_type() if cu_type == PMCUParameter.CU_TYPE_SWITCH_PARAMETER(): if parameter.is_supported(self._packet.to_bytes()): matched.append(parameter) return matched
def render(self): value = "??" parameter = self._parameters[0] if self._packets is not None: if parameter.get_cu_type( ) == PMCUParameter.CU_TYPE_CALCULATED_PARAMETER(): value = parameter.get_calculated_value(self._packets) else: value = parameter.get_value(self._packets[0]) try: self._sum_value += float(value) self._readings += 1 except: pass value_lbl_width = self._value_font.render(value, self._font_aa, self._fg_color).get_width() self._x_offset = (self._width - value_lbl_width) / 2 value_lbl = self._value_font.render(value, self._font_aa, self._fg_color) avg_value_lbl = None if self._readings != 0: avg_value_lbl = self._unit_font.render( "%.2f" % (self._sum_value / self._readings), self._font_aa, self._fg_color_dim) self._surface.blit( avg_value_lbl, (self._x_offset + value_lbl_width / 2, 10 + self._title_lbl.get_height() + value_lbl.get_height())) self._surface.blit(self._title_lbl, (2, 2)) self._surface.blit(value_lbl, (self._x_offset, 10 + self._title_font_size)) if avg_value_lbl == None: self._surface.blit( self._unit_lbl, (self._end_x_offset, 10 + self._title_lbl.get_height() + value_lbl.get_height())) else: self._surface.blit( self._unit_lbl, (self._end_x_offset, 10 + self._title_lbl.get_height() + value_lbl.get_height() + avg_value_lbl.get_height()))
for parameter in supported_parameters: window = PMSingleWindow(parameter) screen.add_window(window) screen.next_window() while True: window = screen.get_window() parameters = window.get_parameters() # TODO refactor - not possible to test at the moment, so leave working part untouched if len(parameters) == 1: parameter = parameters[0] if parameter.get_cu_type( ) == PMCUParameter.CU_TYPE_STD_PARAMETER(): packet = connection.read_parameter(parameter) window.set_packets([packet]) elif parameter.get_cu_type( ) == PMCUParameter.CU_TYPE_FIXED_ADDRESS_PARAMETER(): packet = connection.read_parameter(parameter) window.set_packets([packet]) elif parameter.get_cu_type( ) == PMCUParameter.CU_TYPE_SWITCH_PARAMETER(): packet = connection.read_parameter(parameter) window.set_packets([packet]) elif parameter.get_cu_type( ) == PMCUParameter.CU_TYPE_CALCULATED_PARAMETER(): packets = connection.read_parameters( parameter.get_dependencies()) window.set_packets(packets)
def startElement(self, name, attrs): pid = None desc = None target = 0 units = None expr = None value_format = None address = None byte_index = PMCUParameter.CU_INVALID_BYTE_INDEX() bit_index = PMCUParameter.CU_INVALID_BIT_INDEX() if name == "protocol": for (k, v) in attrs.items(): if k == "id": print 'protocol ' + v self._proto_id = v if self._proto_id != "SSM": return if name == "parameter": for (k, v) in attrs.items(): if k == "id": pid = v if k == "name": name = v if k == "desc": desc = v if k == "ecubyteindex": byte_index = int(v) if k == "ecubit": bit_index = int(v) if k == "target": target = int(v) if byte_index is not PMCUParameter.CU_INVALID_BYTE_INDEX( ) and bit_index is not PMCUParameter.CU_INVALID_BIT_INDEX(): self._parameter = PMCUStandardParameter( pid, name, desc, byte_index, bit_index, target) elif byte_index is PMCUParameter.CU_INVALID_BYTE_INDEX( ) and bit_index is PMCUParameter.CU_INVALID_BIT_INDEX(): self._parameter = PMCUCalculatedParameter( pid, name, desc, target) else: raise Exception elif name == "ecuparam": for (k, v) in attrs.items(): if k == "id": pid = v if k == "name": name = v if k == "desc": desc = v if k == "target": target = int(v) self._parameter = PMCUFixedAddressParameter( pid, name, desc, target) elif name == "switch": for (k, v) in attrs.items(): if k == "id": pid = v if k == "name": name = v if k == "desc": desc = v if k == "byte": address = int(v, 16) if k == "ecubyteindex": byte_index = int(v) if k == "bit": bit_index = int(v) if k == "target": target = int(v) self._parameter = PMCUSwitchParameter(pid, name, desc, address, byte_index, bit_index, target) elif name == "address": self._address_length = 1 for (k, v) in attrs.items(): if k == "length": self._address_length = int(v) elif name == "conversion": for (k, v) in attrs.items(): if k == "units": units = v if k == "expr": expr = v if k == "format": value_format = v self._parameter.add_conversion( PMCUConversion(units, expr, value_format)) elif name == "ecu": for (k, v) in attrs.items(): if k == "id": self._ecu_ids = v.split(",") elif name == "ref": for (k, v) in attrs.items(): if k == "parameter": self._parameter.add_dependency(v)
def render(self): if self._packets is None: return first_row_height = self._title_font_size + self._value_font_size + 10 second_row_height = first_row_height + self._title_font_size + self._value_font_size + 20 pygame.draw.line(self._surface, self._fg_color, (0, first_row_height + 10), (self._width, first_row_height + 10)) parameter_number = 0 for parameter in self._parameters: if parameter.get_cu_type( ) == PMCUParameter.CU_TYPE_CALCULATED_PARAMETER(): value = parameter.get_calculated_value( self._packets[len(self._parameters) - 1:]) self._reading_no += 1 self._sum_fuel_consumption += float(value) else: value = parameter.get_value(self._packets[parameter_number]) title = parameter.get_name( ) # + " (" + param.get_default_unit() + ")" first_row_ids = ["E114", "P104", "P122"] if parameter.get_id() in first_row_ids: index = first_row_ids.index(parameter.get_id()) x_offset = (self._width / len(first_row_ids)) * index + 2 titlelbl = self._title_font.render(title, self._font_aa, self._fg_color) valuelbl = self._value_font.render(value, self._font_aa, self._fg_color) self._surface.blit(titlelbl, (x_offset + 4, 10)) self._surface.blit(valuelbl, (x_offset + 4, 10 + self._title_font_size)) pygame.draw.line(self._surface, self._fg_color, (x_offset, 0), (x_offset, first_row_height)) second_row_ids = ["P203"] if parameter.get_id() in second_row_ids: index = second_row_ids.index(parameter.get_id()) x_offset = (self._width / len(second_row_ids)) * index + 2 titlelbl = self._title_font.render(title, self._font_aa, self._fg_color) valuelbl = self._value_font.render(value, self._font_aa, self._fg_color) self._surface.blit(titlelbl, (x_offset + 4, first_row_height + 20)) self._surface.blit(valuelbl, (x_offset + 4, first_row_height + 20 + self._title_font_size)) pygame.draw.line(self._surface, self._fg_color, (x_offset, first_row_height + 20), (x_offset, second_row_height)) index = 1 x_offset = (self._width / 2) * index + 2 titlelbl = self._title_font.render( "Average Fuel Cons. l/100km", self._font_aa, self._fg_color) valuelbl = self._value_font.render( str(self._sum_fuel_consumption / self._reading_no), self._font_aa, self._fg_color) self._surface.blit(titlelbl, (x_offset + 4, first_row_height + 20)) self._surface.blit(valuelbl, (x_offset + 4, first_row_height + 20 + self._title_font_size)) pygame.draw.line(self._surface, self._fg_color, (x_offset, first_row_height + 20), (x_offset, second_row_height)) parameter_number += 1