def run(self,ctx,**k): event = self.params(ctx) if len(event): self.displayname = SName(event) if self.timespec is None: raise SyntaxError(u'Usage: wait [name…]: for|until|next ‹timespec›') if self.is_update: return Waiters[self.displayname].retime(self.timespec()) w = Waiter(self, self.displayname, self.force) w.init(self.timespec()) process_event(Event(self.ctx(loglevel=TRACE),"wait","start",ixtime(w.end,self.force),*w.name)) try: if w.job: r = w.job.get() else: r = True except Exception as ex: fix_exception(ex) log_exc(msg=u"Wait %s died:"%(self.name,), err=ex, level=TRACE) raise else: tm = ixtime(now(self.force),self.force) if r: # don't log 'done' if canceled process_event(Event(self.ctx(loglevel=TRACE),"wait","done",tm, *w.name)) ctx.wait = tm if not r: raise DelayCancelled(w)
def go_down(self, _=None): if not self.is_up: return self.is_up = False if _ is not None: process_failure(_) process_event(Event(self.ctx,"onewire","down",self.typ,self.id))
def update_all(self): try: process_event(Event(Context(),"onewire","scanning",self.name)) self._update_all() except Exception as e: fix_exception(e) process_failure(e)
def _shut_down(): """\ Code to be called last. The main loop is running and will be stopped when all events have progressed. """ try: process_event(shutdown_event) except Exception as e: fix_exception(e) process_failure(e)
def one_value(self, step): """\ Get one value from some "set monitor" command. Override this for active monitoring. """ if self.send_check_event and step==1: process_event(Event(self.ctx, "monitor","checking",*self.name)) with log_wait("monitor","one_value",*self.name): return self.watcher.get(block=True, timeout=None)
def delete(self,ctx=None): if self.timer: self.timer.cancel() self.timer = None try: if self.state: process_event(Event(self.ctx,"pcm","set",self.names[0],*self.name)) except Exception as ex: fix_exception(ex) process_failure(ex) finally: super(CommonPM,self).delete()
def run(self,ctx,**k): if self.loglevel is not None: ctx = ctx(loglevel=self.loglevel) event = self.params(ctx) if self.recurse: event.ctx = ctx._trim() if not event: raise SyntaxError("Events need at least one parameter") if self.sync: process_event(event) else: self.start_job("job",run_event,event)
def start_up(): """\ Code to be called first. The main loop is NOT running. """ register_worker(Shutdown_Worker("shutdown handler")) global running if not running: running = True try: process_event(startup_event) except Exception as e: fix_exception(e) process_failure(e)
def up(self): with log_wait("monitor up "+repr(self)): while self.job and self.job.dead: gevent.sleep(0.1) # link will clear if not self.job: self.value = None process_event(Event(Context(),"monitor","start",*self.name)) self.start_job("job",self._run_loop) self.state_change_at = now() def tell_ended(_): simple_event(Context(),"monitor","stop",*self.name) self.job.link(tell_ended)
def go_up(self): if self.is_up: return if not hasattr(self,"typ"): self.typ = None gevent.spawn(self._get_typ) return if self.typ is None: return if self.is_up is None: process_event(Event(self.ctx,"onewire","new",self.typ,self.id)) self.is_up = True process_event(Event(self.ctx,"onewire","up",self.typ,self.id))
def run(self,ctx,**k): if self.loglevel is not None: ctx = ctx(loglevel=self.loglevel) event = self.params(ctx) if not event: raise SyntaxError("Events need at least one parameter") event.ctx = ctx._trim() for k,v in self.vars.items(): if k[0] == '$': k = ctx[k[1:]] try: if v[0] == '$': v = ctx[v[1:]] except TypeError: # not an int pass if k is not None and v is not None: setattr(event.ctx, k,v) process_event(event)
def do_switch(self): """Click""" if self.state: self.state = 0 tn = self.t_off else: self.state = 1 tn = self.t_on process_event(Event(self.ctx,"pcm","set",self.names[self.state],*self.name)) try: self.last = self.next if tn is not None: self.next = self.last + dt.timedelta(0,tn) self.timer = callLater(False,self.next,self.do_timed_switch) else: self.next = None except Exception as e: fix_exception(e) process_failure(e) simple_event(self.ctx,"pcm","error",*self.name)
def _do_measure(self): log("monitor",TRACE,"Start run",self.name) try: self.running.clear() self.started_at = now() self._monitor() if self.send_check_event: process_event(Event(self.ctx, "monitor","checked",*self.name)) if self.new_value is not None: if hasattr(self,"delta"): if self.value is not None: val = self.new_value-self.value if val >= 0 or self.delta == 0: process_event(Event(Context(),"monitor","value",self.new_value-self.value,*self.name)) else: process_event(Event(Context(),"monitor","value",self.new_value,*self.name)) if self.new_value is not None: self.value = self.new_value except Exception as e: fix_exception(e) process_failure(e) finally: log("monitor",TRACE,"Stop run",self.name) self.running.set() self._schedule()
def cancel(self, err=DelayCancelled): """Cancel a waiter.""" process_event(Event(self.ctx(loglevel=TRACE),"wait","cancel",ixtime(self.end,self.force),*self.name)) self._cmd("cancel")
def main(): try: process_event(hello_ev) process_event(hello2_ev) finally: shut_down()
def _monitor(self): """This implements a monitor sequence.""" self.steps = 0 self.data = [] self.new_value = None def delay(): if isinstance(self.delay,tuple): sleepUntil(False,time_delta(self.delay)) else: sleepUntil(False,self.delay) try: while self.job and (self.maxpoints is None or self.steps < self.maxpoints): if self.steps and not self.passive: delay() self.steps += 1 try: val = self.one_value(self.steps) except MonitorAgain: pass except Exception as e: fix_exception(e) process_failure(e) break else: log("monitor",TRACE,"raw",val,*self.name) if hasattr(self,"factor"): try: val = val * self.factor + self.offset except TypeError: log("monitor",ERROR,self.name,val,self.factor,self.offset) self.data.append(val) avg = self.filter_data() if avg is not None: if self.value is None or \ self.diff is None or \ abs(self.value-avg) > self.diff: try: if self.value is not None and \ self.alarm is not None and \ abs(self.value-avg) > self.alarm: process_event(Event(Context(),"monitor","alarm",avg,*self.name)) except Exception as e: fix_exception(e) process_failure(e) else: self.new_value = avg return else: log("monitor",TRACE,"More data", self.data, "for", u"‹"+" ".join(unicode(x) for x in self.name)+u"›") try: process_event(Event(Context(),"monitor","error",*self.name)) except Exception as e: fix_exception(e) process_failure(e) finally: log("monitor",TRACE,"End run", self.name) self.stopped_at = now()
def run(self,ctx,**k): event = self.params(ctx) load_module(*event) process_event(Event(self.ctx, "module","load",*event))
def delete(self,ctx=None): process_event(Event(ctx, "module","unload",*self.name)) self.unload() super(Module,self).delete()
def retime(self, dest): process_event(Event(self.ctx(loglevel=TRACE),"wait","update",dest,*self.name)) self._cmd("update",dest)