using logging in move.py, too
parent
8543c3c3f0
commit
cb287b21a4
|
|
@ -29,7 +29,7 @@ class Logger(object):
|
|||
def setup_data_log(self):
|
||||
log = logging.getLogger("weatherstation.datalog")
|
||||
log.setLevel(logging.INFO)
|
||||
fh = logging.FileHandler(os.path.join(settings.records, "records.log"))
|
||||
fh = logging.FileHandler(os.path.join(settings.records, settings.recordlog))
|
||||
fformat = logging.Formatter()
|
||||
fh.setFormatter(fformat)
|
||||
log.addHandler(fh)
|
||||
|
|
@ -87,6 +87,7 @@ class Logger(object):
|
|||
# exception logging #
|
||||
###########################################
|
||||
def printException(self, inst):
|
||||
#TODO: LOG
|
||||
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)})
|
||||
|
|
|
|||
|
|
@ -13,12 +13,11 @@ Python2, Bash, Tinkerforge
|
|||
* SensorTyp
|
||||
3. Gewünschte Callback-zeiten in *TIMES* anpassen
|
||||
3. Tinkerforge-python-bindings installieren
|
||||
4. `all.py` starten, um Aufzeichnung zu starten
|
||||
4. `main.py` starten, um Aufzeichnung zu starten
|
||||
* Aufzeichnung des aktuellen Tages werden in `records` gespeichert
|
||||
* (ftp.sh bzw move.py) Nach dem Upload und dem Tagesende werden die Aufzeichnungen nach `arch` verschoben
|
||||
5. Cronjob für Upload mit ftp.sh einrichten
|
||||
|
||||
# TODOS
|
||||
* Logging auf loghandler umstellen [https://docs.python.org/2/library/logging.html#handler-objects](https://docs.python.org/2/library/logging.html#handler-objects)
|
||||
* auf python3 umstellen
|
||||
* settings aus python auslagern
|
||||
|
|
|
|||
23
Setup.py
23
Setup.py
|
|
@ -46,6 +46,7 @@ class SensorSetup(object):
|
|||
self.cbtimes = cbtimes
|
||||
self.cb_generic = cb_generic
|
||||
self.log = log
|
||||
self._previous_sensors={}
|
||||
|
||||
def parametrizedCallback(self, name, type):
|
||||
return partial(self.cb_generic, sensor=name, type=type)
|
||||
|
|
@ -100,14 +101,21 @@ class SensorSetup(object):
|
|||
# return obj, setcb, get, cb
|
||||
|
||||
def __setupSensor__(self, callback, id, cbtime, var):
|
||||
obj = None
|
||||
if id in self._previous_sensors:
|
||||
self.log.debug("reusing instance for %s", id)
|
||||
obj = self._previous_sensors[id] # restore instance for another callback
|
||||
else:
|
||||
self.log.debug("new instance for %s", id)
|
||||
obj = var[0](id, self.connection) # construct instance
|
||||
self._previous_sensors[id] = obj # save instance for multiple callbacks
|
||||
var[1](obj, cbtime) # set callback period
|
||||
callback(var[2](obj ), supress=True) # execute callback with raw getter as value
|
||||
obj.register_callback(var[3], callback) # register callback
|
||||
return obj
|
||||
|
||||
def genericSensorSetup(self, name, sensor):
|
||||
status = "setup device "+ sensor[0] +" ("+ name +"): "
|
||||
status = "setup device "+ sensor[0] +" ("+ name +"):"
|
||||
callback = self.parametrizedCallback(name, type=sensor[1])
|
||||
cbtime = self.cbtimes[sensor[1]]
|
||||
obj = None
|
||||
|
|
@ -119,22 +127,21 @@ class SensorSetup(object):
|
|||
var = self.getAmbi()
|
||||
elif sensor[1] is SensorType.baro:
|
||||
var = self.getBaro()
|
||||
elif sensor[1] is SensorType.rain:
|
||||
self.log.error("rain is not yet implemented (%s, %s)", sensor[0], name)
|
||||
return None
|
||||
elif sensor[1] is SensorType.iram:
|
||||
var = self.getIram()
|
||||
elif sensor[1] is SensorType.irob:
|
||||
var = self.getIrob()
|
||||
else:
|
||||
self.log.error("FAILED TO LOAD "+name)
|
||||
self.log.error("%s FAIL (unknown type)", status)
|
||||
return None
|
||||
try:
|
||||
obj = self.__setupSensor__(callback, sensor[0], cbtime, var)
|
||||
status += "OK"
|
||||
self.log.info(status)
|
||||
self.log.info("%s OK", status)
|
||||
except Exception as e:
|
||||
status += "FAIL"
|
||||
#print(e)
|
||||
#print(traceback.format_exc())
|
||||
self.log.error(status)
|
||||
self.log.error("%s FAIL:: %s",status, e)
|
||||
return obj
|
||||
|
||||
def setupSensors(self):
|
||||
|
|
|
|||
6
main.py
6
main.py
|
|
@ -19,7 +19,7 @@ def setupLogger():
|
|||
formatter = logging.Formatter('%(asctime)s:[%(levelname)s] - %(message)s')
|
||||
ch.setFormatter(formatter)
|
||||
log.addHandler(ch)
|
||||
fh = logging.FileHandler(os.path.join(settings.logs, "logging.log"))
|
||||
fh = logging.FileHandler(os.path.join(settings.logs, settings.logname))
|
||||
fh.setFormatter(formatter)
|
||||
log.addHandler(fh)
|
||||
return log
|
||||
|
|
@ -30,8 +30,6 @@ def check_dirs_and_files():
|
|||
# log
|
||||
if not os.path.exists(settings.logs):
|
||||
os.mkdir(settings.logs, 0000755)
|
||||
#if not os.path.exists(settings.logname):
|
||||
# open(settings.logname, 'w').close()
|
||||
if not os.path.exists(settings.exceptionlog):
|
||||
file=open(settings.exceptionlog, 'w')
|
||||
file.write("<exceptions></exceptions>")
|
||||
|
|
@ -44,6 +42,7 @@ def check_dirs_and_files():
|
|||
os.mkdir(settings.records, 0000755)
|
||||
|
||||
def obtainLock(lockfile = settings.lockname):
|
||||
#TODO: path
|
||||
if not os.path.exists(lockfile):
|
||||
lock = open(lockfile, 'w')
|
||||
lock.write( str(time.time()) )
|
||||
|
|
@ -52,6 +51,7 @@ def obtainLock(lockfile = settings.lockname):
|
|||
return False
|
||||
|
||||
def freeLock(lockfile = settings.lockname):
|
||||
#TODO: path
|
||||
if os.path.exists(lockfile):
|
||||
os.remove(lockfile)
|
||||
|
||||
|
|
|
|||
42
move.py
42
move.py
|
|
@ -3,27 +3,40 @@
|
|||
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
from shutil import move
|
||||
|
||||
from timeFunctions import *
|
||||
from settings import locks, logs
|
||||
from settings import locks, logs, arch, records, movelog, movelock
|
||||
|
||||
checkfile=locks+'/records_moved'
|
||||
def setupLogger():
|
||||
log = logging.getLogger("weatherstation.move")
|
||||
log.setLevel(logging.INFO)
|
||||
ch = logging.StreamHandler()
|
||||
#formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
formatter = logging.Formatter('%(asctime)s:[%(levelname)s] - %(message)s')
|
||||
ch.setFormatter(formatter)
|
||||
log.addHandler(ch)
|
||||
fh = logging.FileHandler(os.path.join(logs, movelog))
|
||||
fh.setFormatter(formatter)
|
||||
log.addHandler(fh)
|
||||
return log
|
||||
|
||||
log = setupLogger()
|
||||
|
||||
checkfile=os.path.join(locks,movelock)
|
||||
|
||||
if not os.path.exists(logs+"/move.log"):
|
||||
open(logs+"/move.log", 'w').close()
|
||||
if not os.path.exists(checkfile):
|
||||
open(checkfile,'w').close()
|
||||
if not os.path.exists("arch"):
|
||||
os.mkdir("arch", 0000755)
|
||||
if not os.path.exists(arch):
|
||||
os.mkdir(arch, 0000755)
|
||||
|
||||
def mycopy(keep):
|
||||
names = os.listdir("records")
|
||||
names = os.listdir(records)
|
||||
for name in names:
|
||||
if keep in name:
|
||||
continue
|
||||
move(os.path.join("records", name), "arch")
|
||||
|
||||
log=open(logs+"/move.log",'a')
|
||||
move(os.path.join(records, name), arch)
|
||||
|
||||
check=open(checkfile,'r')
|
||||
temp=check.read()
|
||||
|
|
@ -32,20 +45,17 @@ if len(temp)<1:
|
|||
check=open(checkfile,'w')
|
||||
check.write(str(time.time()))
|
||||
check.flush()
|
||||
log.info("updated time since file was empty")
|
||||
else:
|
||||
last=time.gmtime(float(temp))
|
||||
now=time.gmtime()
|
||||
if(prevday(last,now)):
|
||||
print("move")
|
||||
log.write("moving logs... @"+time.ctime()+"\n")
|
||||
log.flush()
|
||||
if not os.path.exists("arch"):
|
||||
os.mkdir("arch")
|
||||
log.info("moving records")
|
||||
mycopy(preptime())
|
||||
check.close()
|
||||
check=open(checkfile,'w')
|
||||
check.write(str(time.time()))
|
||||
check.flush()
|
||||
else:
|
||||
print("today")
|
||||
log.info("records were moved today already")
|
||||
check.close()
|
||||
|
|
|
|||
55
settings.py
55
settings.py
|
|
@ -9,31 +9,14 @@ class SensorType:
|
|||
ambi = 3 # ambient light bricklet
|
||||
baro = 4 # barometer bricklet
|
||||
rain = 5 # IO4 #TODO
|
||||
iram = 6 # temperature ir bricklet, ambient #TODO
|
||||
irob = 7 # temperature ir bricklet, object #TODO
|
||||
"""
|
||||
0: {
|
||||
"host": {
|
||||
"name": "192.168.2.60",
|
||||
"port": 4223
|
||||
},
|
||||
"sensors": {
|
||||
"temp1": ["7B5", SensorType.temp],
|
||||
"temp2": ["8js", SensorType.temp],
|
||||
"humi1": ["7RY", SensorType.humi],
|
||||
"ambi1": ["8Fw", SensorType.ambi],
|
||||
"ambi2": ["8DJ", SensorType.ambi],
|
||||
"baro1": ["bB7", SensorType.baro],
|
||||
"temp3": ["8ms", SensorType.temp],
|
||||
"humi2": ["9V5", SensorType.humi],
|
||||
}
|
||||
},"""
|
||||
iram = 6 # temperature ir bricklet, ambient
|
||||
irob = 7 # temperature ir bricklet, object
|
||||
|
||||
SENSORS={
|
||||
"irtest": {
|
||||
"host":{"name": "localhost", "port":4223},
|
||||
"sensors":{
|
||||
"iram": ["c8w", SensorType.iram],
|
||||
"iram2": ["c8ws", SensorType.iram],
|
||||
"irob": ["c8w", SensorType.irob]
|
||||
}
|
||||
}
|
||||
|
|
@ -44,9 +27,9 @@ TIMES={
|
|||
SensorType.humi: 30000,
|
||||
SensorType.ambi: 60000,
|
||||
SensorType.baro: 60000,
|
||||
SensorType.rain: 60000,
|
||||
SensorType.iram: 1000,
|
||||
SensorType.irob: 1000,
|
||||
SensorType.rain: 0,
|
||||
SensorType.iram: 60000,
|
||||
SensorType.irob: 60000,
|
||||
}
|
||||
|
||||
tempmaxdiff=200 # 200== 2.0 C
|
||||
|
|
@ -55,11 +38,15 @@ prev_temps_default=20000
|
|||
logs='logs'
|
||||
locks='locks'
|
||||
records='records'
|
||||
arch='arch'
|
||||
|
||||
#TODO: add move-log, move-lock, logging instead of writing to files
|
||||
#TODO: lockname, exceptionslog: path.join
|
||||
lockname=locks+"/all.lock"
|
||||
logname=logs+"/all.log"
|
||||
logname="logging.log"
|
||||
exceptionlog=logs+"/exceptions.xml"
|
||||
recordlog="record.log"
|
||||
movelog="move.log"
|
||||
movelock="last_move"
|
||||
|
||||
waitDelay = 10
|
||||
|
||||
|
|
@ -88,3 +75,21 @@ for i in SENSORS:
|
|||
if SENSORS[i]['sensors'][j][1] == SensorType.temp:
|
||||
tempSensors+=1
|
||||
|
||||
"""
|
||||
0: {
|
||||
"host": {
|
||||
"name": "192.168.2.60",
|
||||
"port": 4223
|
||||
},
|
||||
"sensors": {
|
||||
"temp1": ["7B5", SensorType.temp],
|
||||
"temp2": ["8js", SensorType.temp],
|
||||
"humi1": ["7RY", SensorType.humi],
|
||||
"ambi1": ["8Fw", SensorType.ambi],
|
||||
"ambi2": ["8DJ", SensorType.ambi],
|
||||
"baro1": ["bB7", SensorType.baro],
|
||||
"temp3": ["8ms", SensorType.temp],
|
||||
"humi2": ["9V5", SensorType.humi],
|
||||
}
|
||||
},"""
|
||||
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@
|
|||
# Aktionen
|
||||
case "$1" in
|
||||
start)
|
||||
cd /home/XXXX/temp/python
|
||||
sudo -u XXXX screen -dmS recordall python all.py&
|
||||
cd /home/XXXX/weather
|
||||
sudo -u XXXX screen -dmS recordall python main.py&
|
||||
;;
|
||||
stop)
|
||||
# /opt/beispiel stop
|
||||
|
|
|
|||
Loading…
Reference in New Issue