removed code duplication

master
agp8x 2014-01-17 15:16:48 +01:00
parent 09fe751fa0
commit fe4e39b313
3 changed files with 71 additions and 89 deletions

107
all.py
View File

@ -20,18 +20,9 @@
#HOST = "localhost" #HOST = "localhost"
HOST = "192.168.2.30" HOST = "192.168.2.30"
PORT = 4223 PORT = 4223
UID1 = "7B5"
name1="temp1"#not used UID = ["7B5", "8js", "7RY", "8Fw", "8DJ", "bB7"]
UID2 = "8js" NAME = ["temp1", "temp2", "humi1", "ambi1", "ambi2", "baro1"]
name2="temp2"#not used
UID3 = "7RY"
name3="humi1"
UID4="8Fw"
name4="ambi1"
UID5="8DJ"
name5="ambi2"
UID6="bB7"
name6="baro1"
tempSensors=2 tempSensors=2
@ -51,6 +42,7 @@ import os.path
import os import os
import sys,traceback import sys,traceback
import array import array
from timeFunctions import *
cbtimetemp=30000 cbtimetemp=30000
@ -59,6 +51,7 @@ cbtimeambi=60000
cbtimebaro=60000 cbtimebaro=60000
tempmaxdiff=200 # 200== 2.0 C tempmaxdiff=200 # 200== 2.0 C
prev_temps=[20000,20000]
logs='logs' logs='logs'
locks='locks' locks='locks'
@ -67,26 +60,11 @@ records='records'
lockname=locks+"/all.lock" lockname=locks+"/all.lock"
log=open(logs+"/all.log",'a') log=open(logs+"/all.log",'a')
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)
else:
day=str(day)
if(month<10):
month="0"+str(month)
else:
month=str(month)
return month+"."+day+"."+year
def temp_rise(old,new,sensor): def temp_rise(old,new,sensor):
if(old==20000): if(old==20000):
return True return True
if((old-new)>tempmaxdiff or (new-old)>tempmaxdiff): if((old-new)>tempmaxdiff or (new-old)>tempmaxdiff):
log.write('error checking '+sensor+';prev('+str(old)+');cur('+str(new)+'); ... @'+time.ctime()+"\n") log.write('error checking temp '+sensor+';prev('+str(old)+');cur('+str(new)+'); ... @'+time.ctime()+"\n")
log.flush() log.flush()
return False return False
else: else:
@ -94,65 +72,62 @@ def temp_rise(old,new,sensor):
########################################## ##########################################
# common function to write value to file # # common function to write value to file #
########################################## ##########################################
def write_value(value,name): def write_value(value,sensor):
valuename=records+"/"+name+"_"+preptime() valuename=records+"/"+NAME[sensor]+"_"+preptime()
valuelog=open(valuename,'a') valuelog=open(valuename,'a')
valuelog.write(str(value) + ';' + str(int(time.time())) +"\n") valuelog.write(str(value) + ';' + str(int(time.time())) +"\n")
valuelog.close() valuelog.close()
#end# #end#
prev_temps=[20000,20000]
######################################### #########################################
# generic callback for temp# # # generic callback for temp# #
######################################### #########################################
def callback_temperature(value,sensor): def check_and_write_temperature(value,sensor):
if(sensor>=tempSensors): if(sensor>=tempSensors):
return return
global prev_temps global prev_temps
name="temp"+str(sensor+1) if(temp_rise(prev_temps[sensor],value,str(sensor+1))):
if(temp_rise(prev_temps[sensor],value,name)): write_value(value,sensor)
write_value(value,name)
prev_temps[sensor]=value prev_temps[sensor]=value
#end# #end#
########################################## ##########################################
# callbacks for temp1+2 # # callbacks for temp1+2 #
########################################## ##########################################
def cb_temperature1(value): def cb_temperature0(value):
callback_temperature(value,0) check_and_write_temperature(value,0)
print(name1+': ' + str(value/100.0) + ' °C,' + str(time.ctime())) print(name1+': ' + str(value/100.0) + ' °C,' + str(time.ctime()))
# #
def cb_temperature2(value): def cb_temperature1(value):
callback_temperature(value,1) check_and_write_temperature(value,1)
print(name2+': ' + str(value/100.0) + ' °C,' + str(time.ctime())) print(name2+': ' + str(value/100.0) + ' °C,' + str(time.ctime()))
#end# #end#
########################################### ###########################################
# callback for humidity1 # # callback for humidity1 #
########################################### ###########################################
def cb_humidity3(rh): def cb_humidity2(rh):
write_value(rh,name3) write_value(rh,2)
print(name3 +': '+ str(rh/10.0) + ' %RH,' + str(time.ctime())) print(name3 +': '+ str(rh/10.0) + ' %RH,' + str(time.ctime()))
#end# #end#
########################################### ###########################################
# callback for ambi-light1+2 # # callback for ambi-light1+2 #
########################################### ###########################################
def cb_illuminance4(illuminance): def cb_illuminance3(illuminance):
write_value(illuminance,name4) write_value(illuminance,3)
print(name4 +': '+ str(illuminance/10.0) + ' Lux,' + str(time.ctime())) print(name4 +': '+ str(illuminance/10.0) + ' Lux,' + str(time.ctime()))
# #
def cb_illuminance5(illuminance): def cb_illuminance4(illuminance):
write_value(illuminance,name5) write_value(illuminance,4)
print(name5 +': '+ str(illuminance/10.0) + ' Lux,' + str(time.ctime())) print(name5 +': '+ str(illuminance/10.0) + ' Lux,' + str(time.ctime()))
#end# #end#
########################################### ###########################################
# callback for barometer1 # # callback for barometer1 #
########################################### ###########################################
def cb_pressure6(pressure): def cb_pressure5(pressure):
write_value(pressure,name6) write_value(pressure,5)
print(name6+": "+str(pressure/1000)+ "mbar"+str(time.ctime())) print(name6+": "+str(pressure/1000)+ "mbar"+str(time.ctime()))
#end# #end#
@ -182,41 +157,41 @@ if not os.path.exists(lockname):
# #
try: try:
ipcon = IPConnection() ipcon = IPConnection()
t1 = Temperature(UID1, ipcon) t0 = Temperature(UID[0], ipcon)
t2 = Temperature(UID2, ipcon) t1 = Temperature(UID[1], ipcon)
h3 = Humidity(UID3, ipcon) h2 = Humidity(UID[2], ipcon)
al4 = AmbientLight(UID4, ipcon) al3 = AmbientLight(UID[3], ipcon)
al5 = AmbientLight(UID5, ipcon) al4 = AmbientLight(UID[4], ipcon)
b6 = Barometer(UID6, ipcon) b5 = Barometer(UID[5], ipcon)
ipcon.connect(HOST, PORT) ipcon.connect(HOST, PORT)
except Exception as inst: except Exception as inst:
printException(inst) printException(inst)
else: else:
cb_temperature0(t0.get_temperature())
cb_temperature1(t1.get_temperature()) cb_temperature1(t1.get_temperature())
cb_temperature2(t2.get_temperature()) cb_humidity2(h2.get_humidity())
cb_humidity3(h3.get_humidity()) cb_illuminance3(al3.get_illuminance())
cb_illuminance4(al4.get_illuminance()) cb_illuminance4(al4.get_illuminance())
cb_illuminance5(al5.get_illuminance()) cb_pressure5(b5.get_air_pressure())
cb_pressure6(b6.get_air_pressure())
t0.set_temperature_callback_period(cbtimetemp)
t1.set_temperature_callback_period(cbtimetemp) t1.set_temperature_callback_period(cbtimetemp)
t2.set_temperature_callback_period(cbtimetemp) h2.set_humidity_callback_period(cbtimehumi)
h3.set_humidity_callback_period(cbtimehumi) al3.set_illuminance_callback_period(cbtimeambi)
al4.set_illuminance_callback_period(cbtimeambi) al4.set_illuminance_callback_period(cbtimeambi)
al5.set_illuminance_callback_period(cbtimeambi) b5.set_air_pressure_callback_period(cbtimebaro)
b6.set_air_pressure_callback_period(cbtimebaro)
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)
t1.register_callback(t1.CALLBACK_TEMPERATURE, cb_temperature1) t1.register_callback(t1.CALLBACK_TEMPERATURE, cb_temperature1)
t2.register_callback(t2.CALLBACK_TEMPERATURE, cb_temperature2) h2.register_callback(h2.CALLBACK_HUMIDITY, cb_humidity2)
h3.register_callback(h3.CALLBACK_HUMIDITY, cb_humidity3) al3.register_callback(al3.CALLBACK_ILLUMINANCE, cb_illuminance3)
al4.register_callback(al4.CALLBACK_ILLUMINANCE, cb_illuminance4) al4.register_callback(al4.CALLBACK_ILLUMINANCE, cb_illuminance4)
al5.register_callback(al5.CALLBACK_ILLUMINANCE, cb_illuminance5) b5.register_callback(b5.CALLBACK_AIR_PRESSURE,cb_pressure5)
b6.register_callback(b6.CALLBACK_AIR_PRESSURE,cb_pressure6)
raw_input('Press key to exit\n') raw_input('Press key to exit\n')
ipcon.disconnect() ipcon.disconnect()

24
move.py
View File

@ -3,32 +3,10 @@
import time import time
import os import os
from timeFunctions import *
checkfile='locks/records_moved' checkfile='locks/records_moved'
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
else:
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)
else:
day=str(day)
if(month<10):
month="0"+str(month)
else:
month=str(month)
return month+"."+day+"."+year
if not os.path.exists(checkfile): if not os.path.exists(checkfile):
check=open(checkfile,'w') check=open(checkfile,'w')
check.write('') check.write('')

29
timeFunctions.py Normal file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
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
else:
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)
else:
day=str(day)
if(month<10):
month="0"+str(month)
else:
month=str(month)
return month+"."+day+"."+year