class StatusTextManager(object): def __init__(self): self.messages = [] self.sig_changed = Signal() CommManager().register_model(self) @property def last_message(self): return self.messages[-1] def add_message(self, msg): self.messages.append((time(), msg)) if msg.severity == msgs.StatusText.DEBUG: ecu_log.debug(msg.text) elif msg.severity == msgs.StatusText.INFO: ecu_log.info(msg.text) elif msg.severity == msgs.StatusText.WARN: ecu_log.warn(msg.text) elif msg.severity == msgs.StatusText.ERROR: ecu_log.error(msg.text) elif msg.severity == msgs.StatusText.FAILURE: ecu_log.fatal(msg.text) else: ecu_log.debug("SEV(%s): %s", msg.severity, msg.text) self.sig_changed.emit() def clear(self): self.messages = []
class FileWatcher(object): EVENT_MODIFIED = 2 EVENT_RENAMED = 2048 def __init__(self, path): self.path = path self.open_file() self.bind_events() self.on_more_content = Signal() def on_file_changed(self, ignored, filepath, mask): handler = {self.EVENT_MODIFIED: self.read_more, self.EVENT_RENAMED: self.rebind_events}.get(mask) if not handler: print "Ignoring event %s (%s)" % (mask, inotify.humanReadableMask(mask)) return return handler() def read_more(self): content = self.file.read() self.on_more_content.emit(content) def rebind_events(self): self.open_file() self.bind_events() def open_file(self): self.file = open(self.path, "r") def bind_events(self): self.notifier = inotify.INotify() self.notifier.startReading() self.notifier.watch(filepath.FilePath(self.path), callbacks=[self.on_file_changed])
class StatusManager(object): def __init__(self): self.last_message = None self.sig_changed = Signal() def update_status(self, msg): self.last_message = msg self.sig_changed.emit() def clear(self): pass
class ParamManager(object): def __init__(self): self.parameters = {} self.missing_ids = set() self._event = threading.Event() self.sig_changed = Signal() CommManager().register_model(self) @property def changed(self): return [p for p in self.parameters.values() if p.is_changed] def clear(self): self.parameters.clear() self.sig_changed.emit() def update_param(self, param_id, param_index, param_count, value): if len(self.missing_ids) == 0: self.missing_ids.update(range(param_count)) p = self.parameters.get(param_id) if p: p._value = value p._changed = False log.debug("Update: %s: %s", p.param_id, p.value) else: self.parameters[param_id] = Parameter(param_id, param_index, value) log.debug("Add: %s: %s", param_id, value) self.missing_ids.discard(param_index) if len(self.missing_ids) == 0: log.debug("Retrive done") self._event.set() def retrieve_all(self): self.missing_ids = set() self._event.clear() # request all CommManager().param_request() self._event.wait(10.0) # not nesessary: try to request missing params if len(self.missing_ids) > 0: log.warn("Missing %d parameters, trying to request.", len(self.missing_ids)) self._event.clear() for idx in self.missing_ids: CommManager().param_request(param_index=idx) self._event.wait(10.0) if len(self.missing_ids): log.error("Missing %d parameters", len(self.missing_ids)) self.sig_changed.emit() return len(self.missing_ids) == 0 def sync(self): to_sync = self.changed if len(to_sync) == 0: log.info("Nothing to sync") self.sig_changed.emit() return True self.missing_ids = set((p.param_index for p in to_sync)) self._event.clear() for p in to_sync: CommManager().param_set(p.param_id, p.value) self._event.wait(10.0) if len(self.missing_ids): log.error("Not synced %d parameters", len(self.missing_ids)) self.sig_changed.emit() return len(self.missing_ids) == 0