def __init__(self, plugin, frame): GObject.__init__(self) PropertyObject.__init__(self) log.debug('Initializing FreezeFrame ' + frame) self.plugin = plugin self._frame = frame self._pref_cbs = [] self._app_cbs = [] self._notebook_cbs = [] self._queue_cbs = [] self._obd_cbs = [] if plugin.app.prefs.get('imperial', False): self._unit_standard = 'Imperial' else: self._unit_standard = 'Metric' cb_id = plugin.app.prefs.add_watch('imperial', self._notify_units_cb) self._pref_cbs.append(('imperial', cb_id)) self._setup_gui() self._setup_sensors() self._get_supported_pids()
def start(self): log.debug('CommandQueue.start') if (self.device.connected): if not self._working: self._working = True self.emit('state-changed', self._working) self._execute_next_command()
def _read_result(self): log.debug('entering ELMDevice._read_result') timeout_count = 0 try: buf = '' while timeout_count <= MAX_TIMEOUT: ch = self._serial.read(1) if ch == '': timeout_count += 1 if ch == '>': break else: buf = buf + ch if buf == '': raise OBDPortError('PortIOFailed', _('Read timeout from ') + self.port) buf = buf.replace('\r\r', '') return buf except serial.SerialException: raise OBDPortError('PortIOFailed', _('Unable to read from ') + self.port) return None
def atkw_success_cb(cmd, msg, args): log.debug('in atkw_success_cb') if not 'OK' in res: log.debug('invalid response') atkw_error_cb(cmd, res, args) else: self._read_supported_pids()
def _command_success_cb(self, cmd, result, args): log.debug('entering CommandQueue._command_success_cb: %s' % cmd) # We only care about the first result result = result[0] for item in cmd.list: item.data = result self._execute_next_command()
def _update_supported_views(self): log.debug('in update_supported_views') for view in self._views: if view.command.command in self._supported_pids: view.supported=True view.active=True else: view.supported=False view.active=False
def ate_success_cb(cmd, res, args): log.debug('in ate_success_cb') if not 'OK' in res: log.debug('invalid response') ate_error_cb(cmd, res, args) else: #if self.app.get('device.ignore-keywords'): # self._send_command('atkw0', atkw_success_cb, atkw_error_cb) #else: self._read_supported_pids()
def _update_supported_views(self): log.debug('in update_supported_views') for views in (self.views, self.os_views): for view in views: if self.app.device: if view.command.command in self.app.device.supported_commands: view.supported=True if view in self.os_views or view.command.command == '0101': view.active=True else: view.supported=False else: view.supported=False
def _port_io_watch_cb(self, fd, condition, data=None): log.debug('entering ELMDevice._port_io_watch_cb') if condition & gobject.IO_HUP: log.debug('received HUP signal') self._sent_command = None self._ret_cb = None self._err_cb = None self._cb_args = None self.close() return False elif condition & gobject.IO_ERR: log.debug('received ERR signal') self._sent_command = None self._ret_cb = None self._err_cb = None self._cb_args = None self.close() return False elif condition & gobject.IO_IN: try: result = self._read_result() self._parse_result(result) except OBDPortError, e: log.debug('CONDITION = IO_IN but reading times out. Error: %s' % e[0]) finally:
def decode_pids_from_bitstring(data, offset, suffix): log.debug('entering decode_pids_from_bitstring') pids = [] for item in data: bitstr = sensor.hex_to_bitstr(item) for i, bit in enumerate(bitstr): if bit == "1": pid = i + 1 + offset if pid < 16: pid_str = '020' + hex(pid)[2:] + suffix else: pid_str = '02' + hex(pid)[2:] + suffix pids.append(pid_str.upper()) return pids
def decode_pids_from_bitstring(data, mode, offset, suffix=''): log.debug('entering decode_pids_from_bitstring') pids = [] decoded = decode_result(data) for item in decoded: bitstr = sensor.hex_to_bitstr(item) for i, bit in enumerate(bitstr): if bit == "1": pid = i + 1 + offset if pid < 16: pid_str = mode + '0' + hex(pid)[2:] + suffix else: pid_str = mode + hex(pid)[2:] + suffix pids.append(pid_str.upper()) return pids
def success_cb(cmd, data, args): log.debug('entering zero_success_cb') mode = cmd[:2] offset = int(cmd[2:4]) self._supported_pids += decode_pids_from_bitstring(data, mode, offset) next = '%d' % (offset + 20) if mode + next in self._supported_pids: self._send_command(mode + next, success_cb, error_cb) else: if len(modes): mode = modes.pop() self._send_command(mode + '00', success_cb, error_cb) else: log.info('supported pids: %s\n' % self._supported_pids) if not self._connected: self._connected = True self.emit('connected', True) self.emit('supported-pids-changed')
def decode_result(result): log.debug('entering decode_result') if not result: raise OBDDataError('Data Read Error', _('No data was received from the device')) ret = [] result = string.split(result, "\r") for data in result: if data: data = string.split(data) data = string.join(data, '') if data[:2] == '7F': log.debug('we got back 7F which is an error') else: ret.append(data[4:]) return ret
def _deactivate_plugin(self, plugin): instance = self._plugin_instance_from_string(plugin) istr = str(instance) if instance.action_group: self.app.ui.remove_action_group(instance.action_group) if instance.merge_id: self.app.ui.remove_ui(instance.merge_id) if hasattr(instance, 'unload'): instance.unload() del instance for item in self._active_plugins: string, i = item if string == plugin: self._active_plugins.remove(item) log.info('Plugin deactivated: %s' % plugin) log.debug('Plugin deactivated: %s' % istr)
def update(self): log.debug('entering FreezeFrame.update') for view in self._views: if view.supported: self.plugin.app.queue.add(view.command, True) self.plugin.app.queue.start() def unload(self): self.app.notebook.remove(self) for name, cb_id in self._pref_cbs: self.plugin.app.prefs.remove_watch(name, cb_id) for cb_id in self._app_cbs: self.plugin.app.disconnect(cb_id) for cb_id in self._notebook_cbs: self.plugin.app.notebook.disconnect(cb_id) for cb_id in self._queue_cbs: self.plugin.app.queue.disconnect(cb_id) for cb_id in self._obd_cbs: self.plugin.app.device.disconnect(cb_id)
def atz_success_cb(cmd, res, args): log.debug('in atz_success_cb') if not 'ELM327' in res: log.debug('invalid response') atz_error_cb(cmd, res, None) else: log.debug('received answer valid') self._send_command('ate0', ate_success_cb, ate_error_cb)
def _send_command(self, command, ret, err, cleanup=True, *args): log.debug('entering ELMDevice._send_command: %s' % command) if not self._serial.isOpen(): raise OBDPortError('PortNotOpen', _('The port is not open')) self._cleanup_command = cleanup self._sent_command = command self._ret_cb = ret self._err_cb = err self._cb_args = args try: self._serial.flushOutput() self._serial.flushInput() self._serial.write(command) self._serial.write("\r") except serial.SerialException: self._sent_command = None self._ret_cb = None self._err_cb = None self._cb_args = None self.close() raise OBDPortError('PortIOFailed', _('Unable to write to ') + self.port)
def _execute_next_command(self): log.debug('entering CommandQueue._execute_next_command') if self._working: if len(self._queue): queue_item = self._queue.pop(0) if not queue_item.oneshot: self._queue.append(queue_item) else: log.debug('CommandQueue: nothing in queue') self.stop() return log.debug('CommandQueue: executing next command: %s' % queue_item ) self.device.read_command(queue_item, self._command_success_cb, self._command_error_cb)
self._sent_command = None self._ret_cb = None self._err_cb = None self._cb_args = None self.close() return False elif condition & gobject.IO_IN: try: result = self._read_result() self._parse_result(result) except OBDPortError, e: log.debug('CONDITION = IO_IN but reading times out. Error: %s' % e[0]) finally: return True else: log.debug('received an unknown io signal') return False def _read_supported_pids(self, modes=['09','01']): def success_cb(cmd, data, args): log.debug('entering zero_success_cb') mode = cmd[:2] offset = int(cmd[2:4]) self._supported_pids += decode_pids_from_bitstring(data, mode, offset) next = '%d' % (offset + 20) if mode + next in self._supported_pids: self._send_command(mode + next, success_cb, error_cb) else:
def atkw_error_cb(cmd, msg, args): log.debug('in atkw_error_cb') raise OBDPortError('OpenPortFailed', _('atkw0 command failed'))
def stop(self): log.debug('CommandQueue.stop') if self._working: self._working = False self.emit('state-changed', self._working) self._working = False
log.error('%s does not seem to be a valid Plugin' % instance) instance = None return None try: if instance.ui_info: instance.merge_id = self.app.ui.add_ui_from_string(instance.ui_info) self.app.ui.insert_action_group(instance.action_group, 0) except gobject.GError, msg: log.error('building menus failed: %s' % msg) if hasattr(instance, 'load'): instance.load() log.info('Plugin activated: %s' % plugin) log.debug(instance) return instance def _deactivate_plugin(self, plugin): instance = self._plugin_instance_from_string(plugin) istr = str(instance) if instance.action_group: self.app.ui.remove_action_group(instance.action_group) if instance.merge_id: self.app.ui.remove_ui(instance.merge_id) if hasattr(instance, 'unload'): instance.unload() del instance
def _parse_result(self, data): log.debug('entering ELMDevice._parse_result: %s' % data) error = False success = False res = None msg = None cmd = self._sent_command err_cb = self._err_cb ret_cb = self._ret_cb args = self._cb_args if self._sent_command: if data[0] == '>': log.debug('command sent, received >') error = True elif data[0] == '?': log.debug('command sent, received ?') error = True msg = '?' elif 'ERROR' in data: log.info('received ERROR') error = True msg = data elif 'SEARCHING' in data: log.info('received SEARCHING: resending command') self._send_command(cmd, ret_cb, err_cb, args) elif 'BUS INIT' in data: log.info('received BUS INIT: resending command') self._send_command(cmd, ret_cb, err_cb, args) elif 'UNABLE TO CONNECT' in data: log.debug('received UNABLE TO CONNECT') error = True msg = 'UNABLE TO CONNECT' elif 'NO DATA' in data: log.debug('received NO DATA') error = True msg = 'NO DATA' else: res = data success = True if self._cleanup_command: self._err_cb = None self._ret_cb = None self._sent_command = None self._cb_args = None if error: err_cb(cmd, msg, args) if success: ret_cb(cmd, data, args) else: # no command sent # are we interested anyway? if '>' in data: log.debug('received >') else: log.debug('no command sent, received %s' % data)
def _command_error_cb(self, cmd, msg, args): log.debug('CommandQueue._command_error_cb: command was: %s' % cmd) log.debug('CommandQueue._command_error_cb: msg is %s' % msg) if self._working: self._execute_next_command()