def one_site(self,q,options,now): """Print results as a nice minimal table""" r = [] if 'site' not in options: r.append("Site") if 'controller' not in options: r.append("Controller") r.extend(("Valve","Start","Dauer","Ende","?")) res = [r] lengths = [0] * len(r) for sched in Schedule.objects.filter(q): if options['current'] and sched.start+sched.duration < now: continue r = [] if 'site' not in options: r.append(sched.controller.site.name) if 'controller' not in options: r.append(sched.controller.name) r.extend((sched.valve.name, str_tz(sched.start), str(sched.duration), str_tz(sched.end), "*" if sched.seen else "-")) res.append(r) for a in res: i=0 for b in a: lb = len(b) if lengths[i] < lb: lengths[i] = lb i += 1 for a in res: r = [] i=0 for b in a: r.append(b + " "*(lengths[i]-len(b))) i += 1 print(" ".join(r))
def one_site(self, q, options, now): """Print results as a nice minimal table""" r = [] if 'site' not in options: r.append("Site") if 'controller' not in options: r.append("Controller") r.extend(("Valve", "Start", "Dauer", "Ende", "?")) res = [r] lengths = [0] * len(r) for sched in Schedule.objects.filter(q): if options['current'] and sched.start + sched.duration < now: continue r = [] if 'site' not in options: r.append(sched.controller.site.name) if 'controller' not in options: r.append(sched.controller.name) r.extend( (sched.valve.name, str_tz(sched.start), str(sched.duration), str_tz(sched.end), "*" if sched.seen else "-")) res.append(r) for a in res: i = 0 for b in a: lb = len(b) if lengths[i] < lb: lengths[i] = lb i += 1 for a in res: r = [] i = 0 for b in a: r.append(b + " " * (lengths[i] - len(b))) i += 1 print(" ".join(r))
def __unicode__(self): return u"@%s %s" % (str_tz(self.start),self.valve)
def __str__(self): return u"@%s %s" % (str_tz(self.time),self.site)
def __str__(self): return u"@%s %s" % (str_tz(self.start),self.group)
def _run_schedule(self): if self.sched_job is not None: self.sched_job.kill() self.sched_job = None if self.locked: if self.v.verbose: print("SCHED LOCKED2 %s" % (self.v.name,), file=sys.stderr) return n = now() try: if self.sched is not None: self.sched.refresh() if self.sched.end <= n: if self.v.verbose: print("Turn off: %s+%s <= %s" % (self.sched.start,self.sched.duration,n), file=sys.stderr) self._off(2) self.sched = None else: self.sched_job = gevent.spawn_later((self.sched.end-n).total_seconds(),connwrap,self.run_sched_task,reason="_run_schedule 1") if self.v.verbose: print("SCHED LATER %s: %s" % (self.v.name,humandelta(self.sched.end-n)), file=sys.stderr) return except ObjectDoesNotExist: pass # somebody deleted it *shrug* sched = None if self.sched_ts is None: try: sched = self.v.schedules.filter(start__lt=n).order_by("-start")[0] except IndexError: self.sched_ts = n-timedelta(1,0) else: self.sched_ts = sched.end if sched.end > n: # still running if self.v.verbose: print("SCHED RUNNING %s: %s" % (self.v.name,humandelta(sched.end-n)), file=sys.stderr) try: self._on(1,sched, sched.end-n) except TooManyOn: self.log("Could not schedule: too many open valves") except NotConnected: self.log("Could not schedule: connection to MoaT failed") return try: sched = self.v.schedules.filter(start__gte=self.sched_ts).order_by("start")[0] except IndexError: if self.v.verbose: print("SCHED EMPTY %s: %s" % (self.v.name,str_tz(self.sched_ts)), file=sys.stderr) self._off(3) return if sched.end <= n: if self.v.verbose: print("SCHED %s: sched %d done for %s" % (self.v.name,sched.id,humandelta(n-sched.end)), file=sys.stderr) self.sched_ts = None return if sched.start > n: if self.v.verbose: print("SCHED %s: sched %d in %s" % (self.v.name,sched.id,humandelta(sched.start-n)), file=sys.stderr) self._off(4) self.sched_job = gevent.spawn_later((sched.start-n).total_seconds(),connwrap,self.run_sched_task,reason="_run_schedule 2") return try: self._on(2,sched) except TooManyOn: self.log("Could not schedule: too many open valves") except NotConnected: self.log("Could not schedule: connection to MoaT failed")
def _run_schedule(self): if self.sched_job is not None: self.sched_job.kill() self.sched_job = None if self.locked: if self.v.verbose: print("SCHED LOCKED2 %s" % (self.v.name, ), file=sys.stderr) return n = now() try: if self.sched is not None: self.sched.refresh() if self.sched.end <= n: if self.v.verbose: print("Turn off: %s+%s <= %s" % (self.sched.start, self.sched.duration, n), file=sys.stderr) self._off(2) self.sched = None else: self.sched_job = gevent.spawn_later( (self.sched.end - n).total_seconds(), connwrap, self.run_sched_task, reason="_run_schedule 1") if self.v.verbose: print("SCHED LATER %s: %s" % (self.v.name, humandelta(self.sched.end - n)), file=sys.stderr) return except ObjectDoesNotExist: pass # somebody deleted it *shrug* sched = None if self.sched_ts is None: try: sched = self.v.schedules.filter( start__lt=n).order_by("-start")[0] except IndexError: self.sched_ts = n - timedelta(1, 0) else: self.sched_ts = sched.end if sched.end > n: # still running if self.v.verbose: print("SCHED RUNNING %s: %s" % (self.v.name, humandelta(sched.end - n)), file=sys.stderr) try: self._on(1, sched, sched.end - n) except TooManyOn: self.log("Could not schedule: too many open valves") except NotConnected: self.log( "Could not schedule: connection to MoaT failed") return try: sched = self.v.schedules.filter( start__gte=self.sched_ts).order_by("start")[0] except IndexError: if self.v.verbose: print("SCHED EMPTY %s: %s" % (self.v.name, str_tz(self.sched_ts)), file=sys.stderr) self._off(3) return if sched.end <= n: if self.v.verbose: print("SCHED %s: sched %d done for %s" % (self.v.name, sched.id, humandelta(n - sched.end)), file=sys.stderr) self.sched_ts = None return if sched.start > n: if self.v.verbose: print("SCHED %s: sched %d in %s" % (self.v.name, sched.id, humandelta(sched.start - n)), file=sys.stderr) self._off(4) self.sched_job = gevent.spawn_later( (sched.start - n).total_seconds(), connwrap, self.run_sched_task, reason="_run_schedule 2") return try: self._on(2, sched) except TooManyOn: self.log("Could not schedule: too many open valves") except NotConnected: self.log("Could not schedule: connection to MoaT failed")
def __str__(self): return u"@%s %s" % (str_tz(self.start), self.group)
def __str__(self): return "@%s +%s %s" % (str_tz(self.start), self.duration, self.valve)
def force_one_valve(self,v,options): for a,b in v.range(start=soon,forced=True): print "Forced",str_tz(a),str(b) if options['save']: sc=Schedule(valve=v,start=a,duration=b,forced=True) sc.save()
def one_valve(self,v,options): if v.feed.disabled: return if (v.level < v.stop_level) if v.priority else (v.level < v.start_level): print "Nothing to do",v,v.level,v.start_level if options['save'] and v.verbose: log(v,"Nothing to do (has %s, need %s)" % (v.level,v.start_level)) return level = v.level if level > v.max_level: level = v.max_level want = v.raw_watering_time(level) has = timedelta(0,0) last_end = None for s in v.schedules.filter(start__gte=soon-timedelta(1,0)).order_by('start'): last_end=s.end if s.end < n: continue # # This code is disabled because as more # if s.start > later and not s.seen and not s.forced: # if options['save']: # if v.verbose: # log(v,"Drop schedule at %s for %s" % (str_tz(s.start),str(s.duration))) # v.update(priority=True) # s.delete() # continue has += s.duration if last_end and v.min_delay: last_end += v.min_delay if has: if options['save']: v.update(priority=(want.total_seconds() > has.total_seconds()*1.2)) log(v,"Already something to do (has %s, need %s, want %s, does %s)" % (v.level,v.start_level,want,has)) return elif want.total_seconds() < 10: if options['save']: v.update(priority=False) log(v,"Too little to do (has %s, need %s, want %s)" % (v.level,v.start_level,want)) return if options['verbose']: print "Plan",v,"for",want,"Level",v.level,v.start_level,v.stop_level,"P" if v.priority else "" for a,b in v.range(start=soon,days=options['age'], add=30): if a > soon: break # do it during the next run if last_end and last_end > a: if last_end >= a+b: continue if last_end>a: b-=(last_end-a) a=last_end last_end=None if b.total_seconds() < want.total_seconds()/5: if v.verbose: log(v,"slot too short at %s for %s (level %s; want %s)" % (str_tz(a),str(b),v.level,str(want))) continue if v.max_run and b > v.max_run: b=v.max_run if b < want: print "Partial",str_tz(a),str(b) if options['save']: sc=Schedule(valve=v,start=a,duration=b) sc.save() v.update(priority=True) if v.verbose: log(v,"Scheduled at %s for %s (level %s; want %s)" % (str_tz(a),str(b),v.level,str(want))) want -= b break # bail out makes sense, get others scheduled first / do more in the same slot if v.max_run is set else: print "Total",str_tz(a),str(want) if options['save']: sc=Schedule(valve=v,start=a,duration=want) sc.save() v.update(priority=False) if v.verbose: log(v,"Scheduled at %s for %s (level %s)" % (str_tz(a),str(want),v.level)) want = None break if want is not None: print "Missing",want
def __str__(self): return u"@%s +%s %s" % (str_tz(self.start), self.duration, self.valve)
def force_one_valve(self,v,options): for a,b in v.range(start=soon,forced=True): print("Forced",str_tz(a),str(b)) if options['save']: sc=Schedule(valve=v,start=a,duration=b,forced=True) sc.save()
def one_valve(self,v,options): if v.feed.disabled: return if (v.level < v.stop_level) if v.priority else (v.level < v.start_level): if options['save'] and v.verbose: log(v,"Nothing to do (has %s, need %s)" % (v.level,v.start_level)) return level = v.level if level > v.max_level: level = v.max_level want = v.raw_watering_time(level) has = timedelta(0,0) last_end = None for s in v.schedules.filter(start__gte=soon-timedelta(1,0)).order_by('start'): last_end=s.end if s.end < n: continue # # This code is disabled because as more # if s.start > later and not s.seen and not s.forced: # if options['save']: # if v.verbose: # log(v,"Drop schedule at %s for %s" % (str_tz(s.start),str(s.duration))) # v.update(priority=True) # s.delete() # continue has += s.duration if last_end and v.min_delay: last_end += v.min_delay if has: if options['save']: v.update(priority=(want.total_seconds() > has.total_seconds()*1.2)) if v.verbose: log(v,"Already something to do (has %s, need %s, want %s, does %s)" % (v.level,v.start_level,want,has)) return elif want.total_seconds() < 10: if options['save']: v.update(priority=False) if v.verbose: log(v,"Too little to do (has %s, need %s, want %s)" % (v.level,v.start_level,want)) return if options['verbose']: print("Plan",v,"for",want,"Level",v.level,v.start_level,v.stop_level,"P" if v.priority else "") for a,b in v.range(start=soon,days=options['age'], add=30): if a > soon: if options['verbose']: print("NotYet",a,soon) break # do it during the next run if last_end and last_end > a: if last_end >= a+b: continue if last_end>a: b-=(last_end-a) a=last_end last_end=None if b.total_seconds() < want.total_seconds()/5: if v.verbose: log(v,"slot too short at %s for %s (level %s; want %s)" % (str_tz(a),str(b),v.level,str(want))) continue if v.max_run and b > v.max_run: b=v.max_run if b < want: log(v, "Partial %s %s %s" % (str_tz(a),str(b),str(want))) if options['save']: sc=Schedule(valve=v,start=a,duration=b) sc.save() v.update(priority=True) if v.verbose: log(v,"Scheduled at %s for %s (level %s; want %s)" % (str_tz(a),str(b),v.level,str(want))) want -= b break # bail out: get others scheduled first / do more in the same slot if v.max_run is set else: log(v,"Total %s %s" % (str_tz(a),str(want))) if options['save']: sc=Schedule(valve=v,start=a,duration=want) sc.save() v.update(priority=False) if v.verbose: log(v,"Scheduled at %s for %s (level %s)" % (str_tz(a),str(want),v.level)) want = None break else: if want: log(v, "Missing %s" % (str(want),))