splited logic from connection
parent
1d657d3c64
commit
1395675600
|
|
@ -0,0 +1,106 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
from timeFunctions import *
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
|
class Logger(object):
|
||||||
|
def __init__(self,names, temperature_config, log, records):
|
||||||
|
self.names = names
|
||||||
|
self.temp_sensors = temperature_config[0]
|
||||||
|
self.prev_temps =[]
|
||||||
|
for i in range(self.temp_sensors):
|
||||||
|
self.prev_temps.append(temperature_config[1])
|
||||||
|
self.temp_max_diff = temperature_config[2]
|
||||||
|
self.log = log
|
||||||
|
self.records = records
|
||||||
|
|
||||||
|
def temp_rise(self,old,new,sensor):
|
||||||
|
if(old==20000):
|
||||||
|
return True
|
||||||
|
if((old-new)>self.temp_max_diff or (new-old)>self.temp_max_diff):
|
||||||
|
self.log.write('error checking temp '+sensor+';prev('+str(old)+');cur('+str(new)+'); ... @'+time.ctime()+"\n")
|
||||||
|
self.log.flush()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
##########################################
|
||||||
|
# common function to write value to file #
|
||||||
|
##########################################
|
||||||
|
def write_value(self,value,sensor):
|
||||||
|
valuename=self.records+"/"+self.names[sensor]+"_"+preptime()
|
||||||
|
valuelog=open(valuename,'a')
|
||||||
|
valuelog.write(str(value) + ';' + str(int(time.time())) +"\n")
|
||||||
|
valuelog.close()
|
||||||
|
|
||||||
|
#########################################
|
||||||
|
# generic checking of temperature #
|
||||||
|
#########################################
|
||||||
|
def check_and_write_temperature(self,value,sensor):
|
||||||
|
if(sensor>=self.temp_sensors):
|
||||||
|
return
|
||||||
|
if(self.temp_rise(self.prev_temps[sensor],value,str(sensor+1))):
|
||||||
|
self.write_value(value,sensor)
|
||||||
|
self.prev_temps[sensor]=value
|
||||||
|
|
||||||
|
##########################################
|
||||||
|
# callbacks for temp1+2 #
|
||||||
|
##########################################
|
||||||
|
def cb_temperature0(self,value):
|
||||||
|
sensor=0
|
||||||
|
self.check_and_write_temperature(value,sensor)
|
||||||
|
print(self.names[sensor] +': ' + str(value/100.0) + ' °C,' + str(time.ctime()))
|
||||||
|
#
|
||||||
|
def cb_temperature1(self,value):
|
||||||
|
sensor=1
|
||||||
|
self.check_and_write_temperature(value,sensor)
|
||||||
|
print(self.names[sensor] +': ' + str(value/100.0) + ' °C,' + str(time.ctime()))
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
# callback for humidity1 #
|
||||||
|
###########################################
|
||||||
|
def cb_humidity2(self,rh):
|
||||||
|
sensor=2
|
||||||
|
self.write_value(rh,sensor)
|
||||||
|
print(self.names[sensor] +': '+ str(rh/10.0) + ' %RH,' + str(time.ctime()))
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
# callback for ambi-light1+2 #
|
||||||
|
###########################################
|
||||||
|
def cb_illuminance3(self,illuminance):
|
||||||
|
sensor=3
|
||||||
|
self.write_value(illuminance,sensor)
|
||||||
|
print(self.names[sensor] +': '+ str(illuminance/10.0) + ' Lux,' + str(time.ctime()))
|
||||||
|
#
|
||||||
|
def cb_illuminance4(self,illuminance):
|
||||||
|
sensor=4
|
||||||
|
self.write_value(illuminance,sensor)
|
||||||
|
print(self.names[sensor] +': '+ str(illuminance/10.0) + ' Lux,' + str(time.ctime()))
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
# callback for barometer1 #
|
||||||
|
###########################################
|
||||||
|
def cb_pressure5(self,pressure):
|
||||||
|
sensor=5
|
||||||
|
self.write_value(pressure,sensor)
|
||||||
|
print(self.names[sensor] +": "+str(pressure/1000)+ "mbar"+str(time.ctime()))
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
# exception logging #
|
||||||
|
###########################################
|
||||||
|
def printException(self,inst):
|
||||||
|
tree=ET.parse('logs/exceptions.xml')
|
||||||
|
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('logs/exceptions.xml')
|
||||||
|
|
||||||
|
self.log.write('an Exception happen during connection @'+time.ctime()+"\n")
|
||||||
|
self.log.flush()
|
||||||
|
print('an Exception happen during connection @'+time.ctime()+"\n")
|
||||||
|
#end#
|
||||||
153
all.py
153
all.py
|
|
@ -1,30 +1,20 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
#rev2 :*barometer added (b1:#6) [not physically added!!!]
|
|
||||||
# *lcd disabled
|
import os.path
|
||||||
# *exception-logging [dont forget to add empty .xml!]
|
import os
|
||||||
#rev3 :*generic callbacks (15.01.2013-??)
|
import time
|
||||||
# *lcd removed
|
|
||||||
#rev4 *V2.0
|
from Logger import Logger
|
||||||
# *barometer
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#TODO:
|
|
||||||
# *sensorNames=array()...
|
|
||||||
# *sensorUIDs=array()?
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#
|
|
||||||
|
|
||||||
#HOST = "localhost"
|
#HOST = "localhost"
|
||||||
HOST = "192.168.2.30"
|
HOST = "192.168.2.30"
|
||||||
PORT = 4223
|
PORT = 4223
|
||||||
|
|
||||||
UID = ["7B5", "8js", "7RY", "8Fw", "8DJ", "bB7"]
|
UID = ["7B5", "8js", "7RY", "8Fw", "8DJ", "bB7"]
|
||||||
NAME = ["temp1", "temp2", "humi1", "ambi1", "ambi2", "baro1"]
|
NAMES = ["temp1", "temp2", "humi1", "ambi1", "ambi2", "baro1"]
|
||||||
|
|
||||||
tempSensors=2
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from tinkerforge.ip_connection import IPConnection
|
from tinkerforge.ip_connection import IPConnection
|
||||||
|
|
@ -36,22 +26,14 @@ except ImportError:
|
||||||
print("package 'tinkerforge' not installed, canceling")
|
print("package 'tinkerforge' not installed, canceling")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
import time
|
|
||||||
import string
|
|
||||||
import os.path
|
|
||||||
import os
|
|
||||||
import sys,traceback
|
|
||||||
import array
|
|
||||||
from timeFunctions import *
|
|
||||||
|
|
||||||
|
|
||||||
cbtimetemp=30000
|
cbtimetemp=30000
|
||||||
cbtimehumi=30000
|
cbtimehumi=30000
|
||||||
cbtimeambi=60000
|
cbtimeambi=60000
|
||||||
cbtimebaro=60000
|
cbtimebaro=60000
|
||||||
|
|
||||||
|
tempSensors=2
|
||||||
tempmaxdiff=200 # 200== 2.0 C
|
tempmaxdiff=200 # 200== 2.0 C
|
||||||
prev_temps=[20000,20000]
|
prev_temps_default=20000
|
||||||
|
|
||||||
logs='logs'
|
logs='logs'
|
||||||
locks='locks'
|
locks='locks'
|
||||||
|
|
@ -60,88 +42,7 @@ records='records'
|
||||||
lockname=locks+"/all.lock"
|
lockname=locks+"/all.lock"
|
||||||
log=open(logs+"/all.log",'a')
|
log=open(logs+"/all.log",'a')
|
||||||
|
|
||||||
def temp_rise(old,new,sensor):
|
|
||||||
if(old==20000):
|
|
||||||
return True
|
|
||||||
if((old-new)>tempmaxdiff or (new-old)>tempmaxdiff):
|
|
||||||
log.write('error checking temp '+sensor+';prev('+str(old)+');cur('+str(new)+'); ... @'+time.ctime()+"\n")
|
|
||||||
log.flush()
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
##########################################
|
|
||||||
# common function to write value to file #
|
|
||||||
##########################################
|
|
||||||
def write_value(value,sensor):
|
|
||||||
valuename=records+"/"+NAME[sensor]+"_"+preptime()
|
|
||||||
valuelog=open(valuename,'a')
|
|
||||||
valuelog.write(str(value) + ';' + str(int(time.time())) +"\n")
|
|
||||||
valuelog.close()
|
|
||||||
|
|
||||||
#########################################
|
|
||||||
# generic checking of temperature #
|
|
||||||
#########################################
|
|
||||||
def check_and_write_temperature(value,sensor):
|
|
||||||
if(sensor>=tempSensors):
|
|
||||||
return
|
|
||||||
global prev_temps
|
|
||||||
if(temp_rise(prev_temps[sensor],value,str(sensor+1))):
|
|
||||||
write_value(value,sensor)
|
|
||||||
prev_temps[sensor]=value
|
|
||||||
|
|
||||||
##########################################
|
|
||||||
# callbacks for temp1+2 #
|
|
||||||
##########################################
|
|
||||||
def cb_temperature0(value):
|
|
||||||
check_and_write_temperature(value,0)
|
|
||||||
print(name1+': ' + str(value/100.0) + ' °C,' + str(time.ctime()))
|
|
||||||
#
|
|
||||||
def cb_temperature1(value):
|
|
||||||
check_and_write_temperature(value,1)
|
|
||||||
print(name2+': ' + str(value/100.0) + ' °C,' + str(time.ctime()))
|
|
||||||
|
|
||||||
###########################################
|
|
||||||
# callback for humidity1 #
|
|
||||||
###########################################
|
|
||||||
def cb_humidity2(rh):
|
|
||||||
write_value(rh,2)
|
|
||||||
print(name3 +': '+ str(rh/10.0) + ' %RH,' + str(time.ctime()))
|
|
||||||
|
|
||||||
###########################################
|
|
||||||
# callback for ambi-light1+2 #
|
|
||||||
###########################################
|
|
||||||
def cb_illuminance3(illuminance):
|
|
||||||
write_value(illuminance,3)
|
|
||||||
print(name4 +': '+ str(illuminance/10.0) + ' Lux,' + str(time.ctime()))
|
|
||||||
#
|
|
||||||
def cb_illuminance4(illuminance):
|
|
||||||
write_value(illuminance,4)
|
|
||||||
print(name5 +': '+ str(illuminance/10.0) + ' Lux,' + str(time.ctime()))
|
|
||||||
|
|
||||||
###########################################
|
|
||||||
# callback for barometer1 #
|
|
||||||
###########################################
|
|
||||||
def cb_pressure5(pressure):
|
|
||||||
write_value(pressure,5)
|
|
||||||
print(name6+": "+str(pressure/1000)+ "mbar"+str(time.ctime()))
|
|
||||||
|
|
||||||
###########################################
|
|
||||||
# exception logging #
|
|
||||||
###########################################
|
|
||||||
def printException(inst):
|
|
||||||
global log
|
|
||||||
import xml.etree.ElementTree as ET
|
|
||||||
tree=ET.parse('logs/exceptions.xml')
|
|
||||||
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('logs/exceptions.xml')
|
|
||||||
|
|
||||||
log.write('an Exception happen during connection @'+time.ctime()+"\n")
|
|
||||||
print('an Exception happen during connection @'+time.ctime()+"\n")
|
|
||||||
log.flush()
|
|
||||||
#end#
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if not os.path.exists(lockname):
|
if not os.path.exists(lockname):
|
||||||
|
|
@ -149,6 +50,7 @@ if __name__ == "__main__":
|
||||||
lock.write(str(time.time()))
|
lock.write(str(time.time()))
|
||||||
lock.close()
|
lock.close()
|
||||||
# lock obtained
|
# lock obtained
|
||||||
|
logger=Logger(NAMES, (tempSensors, prev_temps_default, tempmaxdiff), log, records)
|
||||||
try:
|
try:
|
||||||
ipcon = IPConnection()
|
ipcon = IPConnection()
|
||||||
t0 = Temperature(UID[0], ipcon)
|
t0 = Temperature(UID[0], ipcon)
|
||||||
|
|
@ -159,16 +61,13 @@ if __name__ == "__main__":
|
||||||
b5 = Barometer(UID[5], ipcon)
|
b5 = Barometer(UID[5], ipcon)
|
||||||
# connect
|
# connect
|
||||||
ipcon.connect(HOST, PORT)
|
ipcon.connect(HOST, PORT)
|
||||||
except Exception as inst:
|
# ensure all sensors are present
|
||||||
#connection failed, log and exit
|
logger.cb_temperature0(t0.get_temperature())
|
||||||
printException(inst)
|
logger.cb_temperature1(t1.get_temperature())
|
||||||
else:
|
logger.cb_humidity2(h2.get_humidity())
|
||||||
cb_temperature0(t0.get_temperature())
|
logger.cb_illuminance3(al3.get_illuminance())
|
||||||
cb_temperature1(t1.get_temperature())
|
logger.cb_illuminance4(al4.get_illuminance())
|
||||||
cb_humidity2(h2.get_humidity())
|
logger.cb_pressure5(b5.get_air_pressure())
|
||||||
cb_illuminance3(al3.get_illuminance())
|
|
||||||
cb_illuminance4(al4.get_illuminance())
|
|
||||||
cb_pressure5(b5.get_air_pressure())
|
|
||||||
|
|
||||||
t0.set_temperature_callback_period(cbtimetemp)
|
t0.set_temperature_callback_period(cbtimetemp)
|
||||||
t1.set_temperature_callback_period(cbtimetemp)
|
t1.set_temperature_callback_period(cbtimetemp)
|
||||||
|
|
@ -180,21 +79,21 @@ if __name__ == "__main__":
|
||||||
log.write('start logging "all" ... @'+time.ctime()+"\n")
|
log.write('start logging "all" ... @'+time.ctime()+"\n")
|
||||||
log.flush()
|
log.flush()
|
||||||
|
|
||||||
t0.register_callback(t0.CALLBACK_TEMPERATURE, cb_temperature0)
|
t0.register_callback(t0.CALLBACK_TEMPERATURE, logger.cb_temperature0)
|
||||||
t1.register_callback(t1.CALLBACK_TEMPERATURE, cb_temperature1)
|
t1.register_callback(t1.CALLBACK_TEMPERATURE, logger.cb_temperature1)
|
||||||
h2.register_callback(h2.CALLBACK_HUMIDITY, cb_humidity2)
|
h2.register_callback(h2.CALLBACK_HUMIDITY, logger.cb_humidity2)
|
||||||
al3.register_callback(al3.CALLBACK_ILLUMINANCE, cb_illuminance3)
|
al3.register_callback(al3.CALLBACK_ILLUMINANCE, logger.cb_illuminance3)
|
||||||
al4.register_callback(al4.CALLBACK_ILLUMINANCE, cb_illuminance4)
|
al4.register_callback(al4.CALLBACK_ILLUMINANCE, logger.cb_illuminance4)
|
||||||
b5.register_callback(b5.CALLBACK_AIR_PRESSURE,cb_pressure5)
|
b5.register_callback(b5.CALLBACK_AIR_PRESSURE,logger.cb_pressure5)
|
||||||
|
|
||||||
raw_input('Press key to exit\n')
|
raw_input('Press key to exit\n')
|
||||||
ipcon.disconnect()
|
ipcon.disconnect()
|
||||||
|
|
||||||
log.write('stop logging... @'+time.ctime()+"\n")
|
log.write('stop logging... @'+time.ctime()+"\n")
|
||||||
|
except Exception as inst:
|
||||||
|
#connection failed, log and exit
|
||||||
|
logger.printException(inst)
|
||||||
os.remove(lockname)
|
os.remove(lockname)
|
||||||
else:
|
else:
|
||||||
print('lock file active!!')
|
print('lock file active!!')
|
||||||
log.write('lock collision: lock "all" active @ '+time.ctime()+"\n")
|
log.write('lock collision: lock "all" active @ '+time.ctime()+"\n")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue