added cleanup after while
parent
9de48194c8
commit
f68f842fad
103
all.py
103
all.py
|
|
@ -53,53 +53,58 @@ lockname=locks+"/all.lock"
|
||||||
log=open(logs+"/all.log",'a')
|
log=open(logs+"/all.log",'a')
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
while True:
|
try:
|
||||||
if not os.path.exists(lockname):
|
while True:
|
||||||
lock=open(lockname,'w')
|
if not os.path.exists(lockname):
|
||||||
lock.write(str(time.time()))
|
lock=open(lockname,'w')
|
||||||
lock.close()
|
lock.write(str(time.time()))
|
||||||
# lock obtained
|
lock.close()
|
||||||
logger=Logger(names, (tempSensors, prev_temps_default, tempmaxdiff), log, records)
|
# lock obtained
|
||||||
try:
|
logger=Logger(names, (tempSensors, prev_temps_default, tempmaxdiff), log, records)
|
||||||
ipcon = IPConnection()
|
try:
|
||||||
# connect
|
ipcon = IPConnection()
|
||||||
ipcon.connect(HOST, PORT)
|
# connect
|
||||||
log.write('start logging "all" ... @'+time.ctime()+"\n")
|
ipcon.connect(HOST, PORT)
|
||||||
log.flush()
|
log.write('start logging "all" ... @'+time.ctime()+"\n")
|
||||||
connected=[]
|
log.flush()
|
||||||
for i,sensor in enumerate(SENSORS):
|
connected=[]
|
||||||
callback=partial(logger.cb_generic, sensor=i, type=sensor[2])
|
for i,sensor in enumerate(SENSORS):
|
||||||
if(sensor[2] == SensorType.temp):
|
callback=partial(logger.cb_generic, sensor=i, type=sensor[2])
|
||||||
obj = Temperature(sensor[1], ipcon)
|
if(sensor[2] == SensorType.temp):
|
||||||
obj.set_temperature_callback_period(cbtimetemp)
|
obj = Temperature(sensor[1], ipcon)
|
||||||
callback(obj.get_temperature())
|
obj.set_temperature_callback_period(cbtimetemp)
|
||||||
obj.register_callback(obj.CALLBACK_TEMPERATURE, callback)
|
callback(obj.get_temperature())
|
||||||
elif (sensor[2] == SensorType.humi):
|
obj.register_callback(obj.CALLBACK_TEMPERATURE, callback)
|
||||||
obj = Humidity(sensor[1], ipcon)
|
elif (sensor[2] == SensorType.humi):
|
||||||
obj.set_humidity_callback_period(cbtimehumi)
|
obj = Humidity(sensor[1], ipcon)
|
||||||
callback(obj.get_humidity())
|
obj.set_humidity_callback_period(cbtimehumi)
|
||||||
obj.register_callback(obj.CALLBACK_HUMIDITY, callback)
|
callback(obj.get_humidity())
|
||||||
elif(sensor[2] == SensorType.ambi):
|
obj.register_callback(obj.CALLBACK_HUMIDITY, callback)
|
||||||
obj = AmbientLight(sensor[1], ipcon)
|
elif(sensor[2] == SensorType.ambi):
|
||||||
obj.set_illuminance_callback_period(cbtimeambi)
|
obj = AmbientLight(sensor[1], ipcon)
|
||||||
callback(obj.get_illuminance())
|
obj.set_illuminance_callback_period(cbtimeambi)
|
||||||
obj.register_callback(obj.CALLBACK_ILLUMINANCE, callback)
|
callback(obj.get_illuminance())
|
||||||
elif (sensor[2] == SensorType.baro):
|
obj.register_callback(obj.CALLBACK_ILLUMINANCE, callback)
|
||||||
obj = Barometer(sensor[1], ipcon)
|
elif (sensor[2] == SensorType.baro):
|
||||||
callback(obj.get_air_pressure())
|
obj = Barometer(sensor[1], ipcon)
|
||||||
obj.set_air_pressure_callback_period(cbtimebaro)
|
callback(obj.get_air_pressure())
|
||||||
obj.register_callback(obj.CALLBACK_AIR_PRESSURE,callback)
|
obj.set_air_pressure_callback_period(cbtimebaro)
|
||||||
else:
|
obj.register_callback(obj.CALLBACK_AIR_PRESSURE,callback)
|
||||||
continue
|
else:
|
||||||
connected.append(obj)
|
continue
|
||||||
raw_input('Press key to exit\n')
|
connected.append(obj)
|
||||||
ipcon.disconnect()
|
raw_input('Press key to exit\n')
|
||||||
log.write('stop logging... @'+time.ctime()+"\n")
|
ipcon.disconnect()
|
||||||
except Exception as inst:
|
log.write('stop logging... @'+time.ctime()+"\n")
|
||||||
#connection failed, log and exit
|
except Exception as inst:
|
||||||
logger.printException(inst)
|
#connection failed, log and exit
|
||||||
os.remove(lockname)
|
logger.printException(inst)
|
||||||
else:
|
os.remove(lockname)
|
||||||
print('lock file active!!')
|
else:
|
||||||
log.write('lock collision: lock "all" active @ '+time.ctime()+"\n")
|
print('lock file active!!')
|
||||||
|
log.write('lock collision: lock "all" active @ '+time.ctime()+"\n")
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("Interrupted")
|
||||||
|
log.write("keyboard-interrupt happened @"+time.ctime()+"\n")
|
||||||
|
os.remove(lockname)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue