unified whitespace
parent
7177e470e5
commit
bf7859f05f
48
Logger.py
48
Logger.py
|
|
@ -22,11 +22,11 @@ class Logger(object):
|
|||
self.records = settings.records
|
||||
self.units = settings.SENSOR_UNITS
|
||||
|
||||
def temp_rise(self,old,new,sensor):
|
||||
if(old==self.temp_prev_default):
|
||||
def temp_rise(self, old, new,sensor):
|
||||
if(old == self.temp_prev_default):
|
||||
return True
|
||||
if((old-new)>self.temp_max_diff or (new-old)>self.temp_max_diff):
|
||||
self.log.write('error checking '+sensor+';prev('+str(old)+');cur('+str(new)+'); ... @'+time.ctime()+"\n")
|
||||
if(((old-new) > self.temp_max_diff) or ((new-old) > self.temp_max_diff)):
|
||||
self.log.write('error checking ' + sensor + ';prev(' + str(old) + ');cur(' + str(new) + '); ... @' + time.ctime() + "\n")
|
||||
self.log.flush()
|
||||
return False
|
||||
else:
|
||||
|
|
@ -35,42 +35,42 @@ class Logger(object):
|
|||
##########################################
|
||||
# common function to write value to file #
|
||||
##########################################
|
||||
def write_value(self,value,sensor):
|
||||
def write_value(self, value, sensor):
|
||||
# TODO: catch IOError
|
||||
valuename=self.records+"/"+sensor+"_"+preptime()
|
||||
valuelog=open(valuename,'a')
|
||||
valuelog.write(str(value) + ';' + str(int(time.time())) +"\n")
|
||||
valuename = self.records + "/" + sensor + "_" + preptime()
|
||||
valuelog=open(valuename, 'a')
|
||||
valuelog.write(str(value) + ';' + str( int(time.time()) ) + "\n")
|
||||
valuelog.close()
|
||||
|
||||
##########################################
|
||||
# generic callback #
|
||||
##########################################
|
||||
def cb_generic(self,value, sensor, type, supress = False):
|
||||
if(type == SensorType.temp):
|
||||
if type == SensorType.temp:
|
||||
i = int(sensor[-1])-1 #sensor contains name, followed by int
|
||||
if(self.temp_rise(self.prev_temps[i],value,sensor)):
|
||||
self.write_value(value,sensor)
|
||||
self.prev_temps[i]=value
|
||||
if self.temp_rise(self.prev_temps[i], value, sensor):
|
||||
self.write_value(value, sensor)
|
||||
self.prev_temps[i] = value
|
||||
elif (type == SensorType.none):
|
||||
return
|
||||
else:
|
||||
self.write_value(value,sensor)
|
||||
unit=self.units[type]
|
||||
self.write_value(value, sensor)
|
||||
unit = self.units[type]
|
||||
if not supress:
|
||||
print(sensor +': ' + str(value/unit[0]) + ' '+unit[1]+', ' + str(time.ctime()))
|
||||
print(sensor + ': ' + str(value/unit[0]) + ' ' + unit[1] + ', ' + str(time.ctime()) )
|
||||
|
||||
###########################################
|
||||
# exception logging #
|
||||
###########################################
|
||||
def printException(self,inst):
|
||||
tree=ET.parse(settings.exceptionlog)
|
||||
root=tree.getroot()
|
||||
new=ET.Element('exception',{'class':str(type(inst)).split("'")[1],'date':str(time.ctime()),'time':str(int(time.time())),'type':str(inst)})
|
||||
new.text=traceback.format_exc()
|
||||
def printException(self, inst):
|
||||
tree = ET.parse(settings.exceptionlog)
|
||||
root = tree.getroot()
|
||||
new = ET.Element('exception', {'class':str( type(inst) ).split("'")[1], 'date':str( time.ctime() ), 'time':str( int(time.time()) ), 'type':str(inst)})
|
||||
new.text = traceback.format_exc()
|
||||
root.append(new)
|
||||
tree.write(settings.exceptionlog)
|
||||
|
||||
self.log.write('an Exception happen during connection @'+time.ctime()+"\n")
|
||||
message = 'an Exception happend @' + time.ctime()+"\n"
|
||||
self.log.write(message)
|
||||
self.log.flush()
|
||||
print('an Exception happen during connection @'+time.ctime()+"\n")
|
||||
#end#
|
||||
print(message)
|
||||
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ class SensorSetup(object):
|
|||
obj = Barometer(id, self.connection)
|
||||
callback(obj.get_air_pressure())
|
||||
obj.set_air_pressure_callback_period(cbtime)
|
||||
obj.register_callback(obj.CALLBACK_AIR_PRESSURE,callback)
|
||||
obj.register_callback(obj.CALLBACK_AIR_PRESSURE, callback)
|
||||
return obj
|
||||
|
||||
def setupNone(self, callback, id, cbtime):
|
||||
|
|
|
|||
26
all.py
26
all.py
|
|
@ -28,8 +28,8 @@ def check_dirs_and_files():
|
|||
|
||||
def obtainLock(lockfile = settings.lockname):
|
||||
if not os.path.exists(lockfile):
|
||||
lock=open(lockfile,'w')
|
||||
lock.write(str(time.time()))
|
||||
lock = open(lockfile, 'w')
|
||||
lock.write( str(time.time()) )
|
||||
lock.close()
|
||||
return True
|
||||
return False
|
||||
|
|
@ -39,16 +39,16 @@ def freeLock(lockfile = settings.lockname):
|
|||
os.remove(lockfile)
|
||||
|
||||
def formatHost(host):
|
||||
return "%s:%d"%(host['name'], host['port'])
|
||||
return "%s:%d" % (host['name'], host['port'])
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_dirs_and_files()
|
||||
log=open(settings.logname,'a')
|
||||
log=open(settings.logname, 'a')
|
||||
try:
|
||||
log.write('setting up "all" ... @'+time.ctime()+"\n")
|
||||
log.write("setting up all ... @" + time.ctime() + "\n")
|
||||
while True:
|
||||
if obtainLock():
|
||||
logger=Logger(log, )
|
||||
logger = Logger(log)
|
||||
try:
|
||||
connections = []
|
||||
connectedSensors = []
|
||||
|
|
@ -58,10 +58,10 @@ if __name__ == "__main__":
|
|||
connection, sensors = conSetup.setupConnectionAndSensors(con['host'], con['sensors'], settings.TIMES, logger.cb_generic)
|
||||
connections.append(connection)
|
||||
connectedSensors.append(sensors)
|
||||
log.write("started logging at %s ... @ %s\n"% (formatHost(con['host']), time.ctime()))
|
||||
log.write("started logging at " + formatHost(con['host']) + " ... @ " + time.ctime() + "\n")
|
||||
log.flush()
|
||||
raw_input('Press key to restart\n')
|
||||
log.write('stop logging... @'+time.ctime()+"\n")
|
||||
raw_input("Press key to restart\n")
|
||||
log.write("stop logging... @" + time.ctime() + "\n")
|
||||
conSetup.disconnectAny(connections)
|
||||
except Exception as inst:
|
||||
#connection failed, log and exit
|
||||
|
|
@ -69,14 +69,14 @@ if __name__ == "__main__":
|
|||
print(inst)
|
||||
freeLock()
|
||||
else:
|
||||
print('lock file active!!')
|
||||
log.write('lock collision: lock "all" active @ '+time.ctime()+"\n")
|
||||
print("wait for retry ("+str(settings.waitDelay)+")")
|
||||
print("lock file active!!")
|
||||
log.write("lock collision: lock 'all' active @ " + time.ctime() + "\n")
|
||||
print("wait for retry (" + str(settings.waitDelay) + ")")
|
||||
time.sleep(settings.waitDelay)
|
||||
except KeyboardInterrupt:
|
||||
print(" Interrupted, cleaning up")
|
||||
conSetup.disconnectAny(connections)
|
||||
log.write("keyboard-interrupt happened @"+time.ctime()+"\n")
|
||||
log.write("keyboard-interrupt happened @" + time.ctime() + "\n")
|
||||
log.close()
|
||||
freeLock()
|
||||
|
||||
|
|
|
|||
|
|
@ -3,27 +3,27 @@
|
|||
|
||||
import time
|
||||
|
||||
def prevday(then,now):
|
||||
def prevday(then, now):
|
||||
#ist "then" gestern (oder noch älter)?
|
||||
greaterDay=(then.tm_yday < now.tm_yday) and (then.tm_year == now.tm_year)
|
||||
if(greaterDay):
|
||||
newYear=False
|
||||
greaterDay = (then.tm_yday < now.tm_yday) and (then.tm_year == now.tm_year)
|
||||
if greaterDay:
|
||||
newYear = False
|
||||
else:
|
||||
newYear=then.tm_year < now.tm_year
|
||||
return (greaterDay) or (newYear)
|
||||
newYear = then.tm_year < now.tm_year
|
||||
return greaterDay or newYear
|
||||
|
||||
def preptime():
|
||||
now=time.localtime()
|
||||
day=now.tm_mday
|
||||
month=now.tm_mon
|
||||
year=str(now.tm_year)
|
||||
if(day<10):
|
||||
day="0"+str(day)
|
||||
now = time.localtime()
|
||||
day = now.tm_mday
|
||||
month = now.tm_mon
|
||||
year = str(now.tm_year)
|
||||
if day < 10:
|
||||
day = "0" + str(day)
|
||||
else:
|
||||
day=str(day)
|
||||
if(month<10):
|
||||
month="0"+str(month)
|
||||
day = str(day)
|
||||
if( month < 10:
|
||||
month = "0" str(month)
|
||||
else:
|
||||
month=str(month)
|
||||
return month+"."+day+"."+year
|
||||
month = str(month)
|
||||
return month + "." + day + "." + year
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue