138 lines
5.1 KiB
Python
138 lines
5.1 KiB
Python
#!/usr/bin/python
|
|
|
|
import time
|
|
import datetime
|
|
import ADS1256
|
|
import redis
|
|
|
|
#configurazione per lavorare su server redis locale
|
|
REDIS_PORT = 6379
|
|
REDIS_HOST = '127.0.0.1'
|
|
redSrv0 = redis.Redis(
|
|
host=REDIS_HOST,
|
|
port=REDIS_PORT,
|
|
db=0)
|
|
|
|
#funzione per leggere e decodificare da redis
|
|
def getRedisVal(redisKey):
|
|
if redSrv0.get(redisKey) is None:
|
|
return redSrv0.get(redisKey)
|
|
else:
|
|
return redSrv0.get(redisKey).decode('utf-8')
|
|
|
|
#assegno director log per file di debug
|
|
LOG_path = "/home/pi/Flythis/logger/log/"
|
|
|
|
#intervallo in millisecondi fra un campionamento e il successivo
|
|
startingSampleFreq = 250
|
|
redSampleFreq = 'SETTINGS:LOG:FREQ'
|
|
redSrv0.set(redSampleFreq,startingSampleFreq)
|
|
|
|
#se non c'è un valore nei campi di redis, popolo il db
|
|
redSrv0.set('SETTINGS:LOG:STATUS',0)
|
|
if redSrv0.get('SETTINGS:PASSWORD:CURRENT') is None:
|
|
redSrv0.set('SETTINGS:PASSWORD:CURRENT',"-")
|
|
if redSrv0.get('SETTINGS:FOLDER:CURRENT') is None:
|
|
redSrv0.set('SETTINGS:FOLDER:CURRENT', "default")
|
|
if redSrv0.get('SETTINGS:SELECTED_CH') is None:
|
|
redSrv0.set('SETTINGS:SELECTED_CH',0)
|
|
for numCh in range(0,8,+1):
|
|
if redSrv0.get('SETTINGS:IN:MAX:'+str(numCh)) is None:
|
|
redSrv0.set('SETTINGS:IN:MAX:'+str(numCh),100)
|
|
if redSrv0.get('SETTINGS:IN:MIN:'+str(numCh)) is None:
|
|
redSrv0.set('SETTINGS:IN:MIN:'+str(numCh),0)
|
|
if redSrv0.get('SETTINGS:OUT:MAX:'+str(numCh)) is None:
|
|
redSrv0.set('SETTINGS:OUT:MAX:'+str(numCh),100)
|
|
if redSrv0.get('SETTINGS:OUT:MIN:'+str(numCh)) is None:
|
|
redSrv0.set('SETTINGS:OUT:MIN:'+str(numCh),0)
|
|
if redSrv0.get('SETTINGS:UNIT:MEASURE:'+str(numCh)) is None:
|
|
redSrv0.set('SETTINGS:UNIT:MEASURE:'+str(numCh),"-")
|
|
|
|
# init oggetto lettura da
|
|
try:
|
|
CH = ADS1256.ADS1256()
|
|
CH.ADS1256_init()
|
|
#funzione salva time+data di log e otto valori su redis
|
|
def redisSave(CHvalue):
|
|
redLastLog = 'RTDATA:TIME:LOG'
|
|
dateFormat ="%d/%m/%Y %H:%M:%S"
|
|
rawLastLog = datetime.datetime.now()
|
|
lastLog = rawLastLog.strftime(dateFormat)
|
|
redSrv0.set(redLastLog,str(lastLog))
|
|
for chIndex in range(0,8,+1):
|
|
redAreaIn = 'RTDATA:CH:'+str(chIndex)
|
|
redAreaOut = 'RTDATA:OUT:'+str(chIndex)
|
|
strValIn = "%.4f" % (CHvalue[chIndex]*5.0/0x7fffff)
|
|
redSrv0.set(redAreaIn,strValIn)
|
|
# salvo i valori scalati in OUT
|
|
strValOut = "%.4f" % scaleVal(CHvalue[chIndex]*5.0/0x7fffff, chIndex)
|
|
redSrv0.set(redAreaOut,strValOut)
|
|
|
|
#funzione di refresh valori scalati
|
|
def scaleVal(inValue, chIndex):
|
|
#rileggo da redis i valori min/max secondo chIndex
|
|
chInMin = float(0)
|
|
if (redSrv0.get('SETTINGS:IN:MIN:'+str(chIndex)) != 0):
|
|
chInMin = float(redSrv0.get('SETTINGS:IN:MIN:'+str(chIndex)))
|
|
chInMax = float(3.3)
|
|
if (redSrv0.get('SETTINGS:IN:MAX:'+str(chIndex)) != 100):
|
|
chInMax = float(redSrv0.get('SETTINGS:IN:MAX:'+str(chIndex)))
|
|
chOutMin = float(0)
|
|
if (redSrv0.get('SETTINGS:OUT:MIN:'+str(chIndex)) != 0):
|
|
chOutMin = float(redSrv0.get('SETTINGS:OUT:MIN:'+str(chIndex)))
|
|
chOutMax = float(1000)
|
|
if (redSrv0.get('SETTINGS:OUT:MAX:'+str(chIndex)) != 100):
|
|
chOutMax = float(redSrv0.get('SETTINGS:OUT:MAX:'+str(chIndex)))
|
|
# check denom zero
|
|
deltaOut = (chOutMax - chOutMin)
|
|
deltaIn = (chInMax - chInMin)
|
|
if(deltaIn==0):
|
|
deltaIn = 1
|
|
# calcolo scalato
|
|
outVal = chOutMin + ((inValue-chInMin) * (deltaOut / deltaIn))
|
|
return outVal
|
|
|
|
#ciclo principale
|
|
while(1):
|
|
redisTime = 'RTDATA:TIME:SRV'
|
|
now = datetime.datetime.now()
|
|
dateFormat ="%d/%m/%Y %H:%M:%S"
|
|
lastLog = now.strftime(dateFormat)
|
|
redSrv0.set(redisTime,str(lastLog))
|
|
CHvalue = CH.ADS1256_GetAll()
|
|
redisSave(CHvalue)
|
|
# riporto ultima esecuzione ad adesso
|
|
endExec = datetime.datetime.now()
|
|
# calcolo il delta time dovuto alle esecuzioni
|
|
delta = endExec - now
|
|
waitTime = int(redSrv0.get(redSampleFreq)) / 1000 - delta.microseconds/1000000
|
|
if(waitTime < 0.1):
|
|
waitTime = 0.1
|
|
# attesa
|
|
time.sleep(waitTime)
|
|
|
|
#eccezione da ctrl+c in terminale e chiusura
|
|
except KeyboardInterrupt:
|
|
lastLog = datetime.datetime.now()
|
|
print (str(lastLog)+" Program End. Ctrl+C from user\r\n")
|
|
try:
|
|
logFile = open(LOG_path+"PyLog.txt", "a")
|
|
except FileNotFoundError:
|
|
print("Creating new PyLog.txt")
|
|
logFile = open(LOG_path+"PyLog.txt", "w+")
|
|
logFile.write(str(lastLog)+" Program end. Ctrl+C from user\r\n")
|
|
logFile.close()
|
|
exit()
|
|
|
|
#eccezione da errore e chiusura
|
|
except Exception as errorMessage:
|
|
lastLog = datetime.datetime.now()
|
|
print(str(lastLog)+" Caught error " + str(errorMessage)+"\r\n")
|
|
try:
|
|
logFile = open(LOG_path+"PyLog.txt", "a")
|
|
except FileNotFoundError:
|
|
print("Creating new PyLog.txt")
|
|
logFile = open(LOG_path+"PyLog.txt", "w+")
|
|
logFile.write(str(lastLog)+" Caught error: "+str(errorMessage)+"\r\n")
|
|
logFile.close()
|
|
exit() |