def delay(seconds): time0 = Uptime() sleeptime = seconds while sleeptime > 0: time.sleep(sleeptime) time1 = Uptime() sleeptime = seconds - time1 + time0
def obtain_uptime(show_ver): match = re.search(r".* uptime is .*", show_ver) if match: uptime_str = match.group().strip() uptime_obj = Uptime(uptime_str) return uptime_obj.uptime_seconds() else: return None
def obtain_uptime(show_ver): ''' Obtain uptime from show version data ''' match = re.search(r".* uptime is .*", show_ver) #print(type(match)) if match: uptime_str = match.group().strip() uptime_obj = Uptime(uptime_str) return uptime_obj.uptime_seconds() else: return "None"
def writedata(self): localtime = time.localtime() with codecs.open(datatempfilename, 'w', encoding='utf8') as f: f.write(u'''\ {date} {time} {S1Color.rH} {S1rH} {S2Color.rH} {S2rH} {S1Color.T} {S1T} {S2Color.T} {S2T} {S1Color.tau} {S1tau} {S2Color.tau} {S2tau} {fanstatestyle} {modetxt}{fanstatetxt} {statusstyle} {statustxt} {IPeth0} {IPwlan0} {IPwan} {uptime} {progtime} {lastsync}'''.format(date=time.strftime("%d.%m.%Y", localtime), time=time.strftime("%H:%M:%S", localtime), S1rH=prettyPrint(self.S1.rH), S2rH=prettyPrint(self.S2.rH), S1T=prettyPrint(self.S1.T), S2T=prettyPrint(self.S2.T), S1tau=prettyPrint(self.S1.tau), S2tau=prettyPrint(self.S2.tau), S1Color=CSSstyle(self.S1), S2Color=CSSstyle(self.S2), statustxt=self.statustxt, statusstyle=self.statusstyle, modetxt=self.modetxt, fanstatetxt=self.fanstatetxt, fanstatestyle=self.fanstatestyle, IPeth0=get_ip_address('eth0'), IPwlan0=get_ip_address('wlan0'), IPwan=get_wan_ip(), uptime=str(datetime.timedelta(seconds=int(Uptime()))), progtime=str( datetime.timedelta(seconds=int(Uptime() - progstart))), lastsync=self.last_sync if self.last_sync else 'None')) os.rename(datatempfilename, datafilename)
def _wait(self): GPIO.setup(self.pin_data, GPIO.IN, GPIO.PUD_UP) #try: # channel = GPIO.wait_for_edge(self.pin_data, GPIO.FALLING, timeout=1000) #except RuntimeError as error: # raise ShtCommFailure(*error.args) #if channel is None: # raise ShtCommFailure('Wait timeout') # Busy wait... more reliable t0 = Uptime() while self._data_get(): if Uptime() - t0 > 1: raise ShtCommFailure('Wait timeout')
def display(self): infolines = ( ['IP Ethernet/WLAN:'], ['', get_ip_address('eth0')], ['', get_ip_address('wlan0')], ['Bootvorgang vor:'], ['', str(datetime.timedelta(seconds=int(Uptime())))], ['Programmstart vor:'], ['', str(datetime.timedelta(seconds=int(Uptime() - self.progstart)))], ['Freies RAM:', getAvailableRAM()], ) self.index = max(min(self.index, len(infolines) - self.displayLines), 0) self.messageboard.post( 'Info', infolines[self.index:self.index + self.displayLines])
def onAverage(self, message): with self.lock: uptime0 = Uptime() timespan = message T1 = 0 rH1 = 0 tau1 = 0 count1 = 0 T2 = 0 rH2 = 0 tau2 = 0 count2 = 0 for uptime, S1Data, S2Data in self.deque: assert uptime <= uptime0 if uptime0 - uptime > timespan: break if not S1Data.Error: T1 += S1Data.T rH1 += S1Data.rH tau1 += S1Data.tau count1 += 1 if not S2Data.Error: T2 += S2Data.T rH2 += S2Data.rH tau2 += S2Data.tau count2 += 1 if count1 > 0: T1 /= count1 rH1 /= count1 tau1 /= count1 Error1 = count1 < max(1, timespan / 20) if count2 > 0: T2 /= count2 rH2 /= count2 tau2 /= count2 Error2 = count2 < max(1, timespan / 20) return (Bunch(rH=rH1, T=T1, tau=tau1, Error=Error1), Bunch(rH=rH2, T=T2, tau=tau2, Error=Error2))
class InfoScreen: progstart = Uptime() displayLines = 7 def __init__(self, messageboard): self.messageboard = messageboard self.index = 0 def display(self): infolines = ( ['IP Ethernet/WLAN:'], ['', get_ip_address('eth0')], ['', get_ip_address('wlan0')], ['Bootvorgang vor:'], ['', str(datetime.timedelta(seconds=int(Uptime())))], ['Programmstart vor:'], ['', str(datetime.timedelta(seconds=int(Uptime() - self.progstart)))], ['Freies RAM:', getAvailableRAM()], ) self.index = max(min(self.index, len(infolines) - self.displayLines), 0) self.messageboard.post( 'Info', infolines[self.index:self.index + self.displayLines]) def forward(self): self.index += 1 self.display() def back(self): self.index -= 1 self.display() def select(self, pin): pass def leave(self): pass
logger.info('Startup') signals_handler = signals_handler(messageboard) with Display(), \ Sensor(), \ Status(), \ HtmlWriter(), \ Fan(), \ Menu(), \ Devices(), \ DCF77(), \ Average(), \ RestartWLAN(), \ CheckNetwork(): time0 = Uptime() while messageboard.query('ExitThread') is None: exception = messageboard.query('Exception') if exception is not None: raise exception messageboard.post('Time', (Uptime(), time.localtime())) time1 = Uptime() if not time1 >= time0: logger.warning('Error in uptime: {} < {}.'.format(time1, time0)) time0 = time1 sleeptime = 1 - time1 + time0 if sleeptime <= 0: logger.warning(u'Zero sleep time: {} < {}, Δ={:.1f}s.'.format( time0, time1, time1 - time0)) while sleeptime > 0: time.sleep(sleeptime)
def __init__(self): ComponentWithThread.__init__(self, 'check_wlan') self.event = Event() self.lastmeasurement = Uptime()
def write(self): localtime = time.localtime() with codecs.open(pagetempfilename, 'w', encoding='utf8') as f: f.write(u'''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Fan control</title> <meta name="author" content="Daniel Müllner"> <script type="text/JavaScript"> function timedRefresh(timeoutPeriod) {{ setTimeout("location.reload(true);",timeoutPeriod); }} window.onload = timedRefresh(10000); </script> </head> <style> body {{font-family: sans-serif;}} h1 {{font-size: 125%;}} table {{border:1px solid grey; border-collapse:collapse;}} td{{padding:4px 8px}} tr.bg {{background-color:#ffb;}} tr.hr {{border-bottom:1px solid black}} td.l {{text-align:left;}} td.c {{text-align:center;}} td.r {{text-align:right;}} </style> <body> <h1>Fan control</h1> <table> <tr> <td colspan="3"> {date}<span style="float:right">{time}</span> </td> </tr> <tr class="hr"> <td> </td> <td class="c"> Indoors </td> <td class="c"> Outdoors </td> </tr> <tr class="bg"> <td> Relative humidity in % </td> <td class="c" style="{S1Color.rH}"> {S1rH} </td> <td class="c" style="{S2Color.rH}"> {S2rH} </td> </tr> <tr> <td> Temperature in °C </td> <td class="c" style="{S1Color.T}"> {S1T} </td> <td class="c" style="{S2Color.T}"> {S2T} </td> </tr> <tr class="bg"> <td> Dew point in °C </td> <td class="c" style="{S1Color.tau}"> {S1tau} </td> <td class="c" style="{S2Color.tau}"> {S2tau} </td> </tr> <tr> <td colspan="3" style="{fanstatestyle}"> {modetxt}{fanstatetxt} </td> </tr> <tr class="bg hr"> <td colspan="3" style="{statusstyle}"> {statustxt} </td> </tr> <tr> <td colspan="3"> IP Ethernet: <span style="float:right">{IPeth0}</span> </td> </tr> <tr class="bg"> <td colspan="3"> IP WLAN: <span style="float:right">{IPwlan0}</span> </td> </tr> <tr> <td colspan="3"> IP WAN: <span style="float:right">{IPwan}</span> </td> </tr> <tr class="bg"> <td colspan="3"> OS uptime: <span style="float:right">{uptime}</span> </td> </tr> <tr> <td colspan="3"> Controller uptime: <span style="float:right">{progtime}</span> </td> </tr> <tr class="bg"> <td colspan="3"> Last DCF77 signal: <span style="float:right">{lastsync}</span> </td> </tr> </table> </body> </html> '''.format(date=time.strftime("%d.%m.%Y", localtime), time=time.strftime("%H:%M:%S", localtime), S1rH=prettyPrint(self.S1.rH), S2rH=prettyPrint(self.S2.rH), S1T=prettyPrint(self.S1.T), S2T=prettyPrint(self.S2.T), S1tau=prettyPrint(self.S1.tau), S2tau=prettyPrint(self.S2.tau), S1Color=CSSstyle(self.S1), S2Color=CSSstyle(self.S2), statustxt=self.statustxt, statusstyle=self.statusstyle, modetxt=self.modetxt, fanstatetxt=self.fanstatetxt, fanstatestyle=self.fanstatestyle, IPeth0=get_ip_address('eth0'), IPwlan0=get_ip_address('wlan0'), IPwan=get_wan_ip(), uptime=str(datetime.timedelta(seconds=int(Uptime()))), progtime=str(datetime.timedelta(seconds=int(Uptime() - progstart))), lastsync=self.last_sync if self.last_sync else 'None')) os.rename(pagetempfilename, pagefilename)
from ConfigParser import RawConfigParser import Queue else: from configparser import RawConfigParser import queue import codecs import datetime import os import shutil import time from ip import get_ip_address, get_wan_ip from uptime import Uptime from component import Component progstart = Uptime() DEBUG = False config = RawConfigParser() config.read('fancontrol.cfg') pagefilename = config.get('webserver', 'page') pagetempfilename = config.get('webserver', 'temppage') datafilename = config.get('webserver', 'data') datatempfilename = config.get('webserver', 'tempdata') indexsource = config.get('webserver', 'indexsource') indextarget = config.get('webserver', 'indextarget') shutil.copyfile(indexsource, indextarget)
def __init__(self): ComponentWithThread.__init__(self, 'sensor') self.S1 = sht75.Sensor(clock1, data1) self.S2 = sht75.Sensor(clock2, data2) self.event = Event() self.lastmeasurement = Uptime()