refresh script
This commit is contained in:
180
loadCSVModbGDLora.py
Executable file
180
loadCSVModbGDLora.py
Executable file
@@ -0,0 +1,180 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime
|
||||
from mysql.connector import MySQLConnection, Error
|
||||
from dbconfig import read_db_config
|
||||
|
||||
def insertData(dati):
|
||||
print("DATI:")
|
||||
print(dati)
|
||||
if dati != "null" and dati is not None:
|
||||
print(dati[0][1])
|
||||
print(dati[0][0])
|
||||
matlab_func = ""
|
||||
conn_via_radio = 0
|
||||
operation_mode = 0
|
||||
queryMatFunc = "select m.matcall, t.conn_via_radio, t.operation_mode from matfuncs as m " \
|
||||
"inner join tools as t on t.matfunc = m.id " \
|
||||
"inner join units as u on u.id = t.unit_id " \
|
||||
"inner join statustools as s on t.statustool_id = s.id " \
|
||||
"where t.name = '"+dati[0][1]+"' and u.name = '"+dati[0][0]+"'"
|
||||
try:
|
||||
db_config = read_db_config()
|
||||
conn = MySQLConnection(**db_config)
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(queryMatFunc)
|
||||
|
||||
result = cursor.fetchall()
|
||||
matlab_func = result[0][0]
|
||||
conn_via_radio = result[0][1]
|
||||
operation_mode = result[0][2]
|
||||
except Error as e:
|
||||
print('Error:', e)
|
||||
|
||||
if conn_via_radio == 1:
|
||||
if operation_mode == 1:#listening mode(no rssi 'al momento')
|
||||
query = "INSERT INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, BatLevelModule, TemperatureModule, Val0, RssiModule) " \
|
||||
"VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
cursor.executemany(query, dati)
|
||||
|
||||
conn.commit()
|
||||
print(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+"-> Inserito in RAWDATACOR "+dati[0][0]+" "+dati[0][1])
|
||||
|
||||
except Error as e:
|
||||
print('Error:', e)
|
||||
|
||||
finally:
|
||||
if matlab_func != "":
|
||||
print(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+"-> Avvio "+matlab_func)
|
||||
os.system("cd /usr/local/matlab_func/; ./run_"+matlab_func+".sh /usr/local/MATLAB/MATLAB_Runtime/v93/ "+dati[0][0]+" "+dati[0][1]+" >> /home/asega/log/loadgdmodblora.log 2>&1")
|
||||
else:#standard no val0 con rssi
|
||||
try:
|
||||
for drow in dati:
|
||||
unit_name = drow[0]
|
||||
tool_name = drow[1]
|
||||
date = drow[3]
|
||||
time = drow[4]
|
||||
batM = drow[7]
|
||||
tempM = drow[8]
|
||||
rssiM = drow[10].replace("dB", "")
|
||||
|
||||
query = "UPDATE RAWDATACOR SET BatLevelModule=%s, TemperatureModule=%s, RssiModule=%s where UnitName=%s and ToolNameID=%s and EventDate=%s and EventTime=%s"
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, [batM, tempM, rssiM, unit_name, tool_name, date, time])
|
||||
conn.commit()
|
||||
print(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+"-> Aggiornato in RAWDATACOR "+dati[0][0]+" "+dati[0][1])
|
||||
except Error as e:
|
||||
print('Error:', e)
|
||||
finally:
|
||||
if matlab_func != "":
|
||||
print(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+"-> Avvio "+matlab_func)
|
||||
os.system("cd /usr/local/matlab_func/; ./run_"+matlab_func+".sh /usr/local/MATLAB/MATLAB_Runtime/v93/ "+dati[0][0]+" "+dati[0][1]+" >> /home/asega/log/loadgdmodblora.log 2>&1")
|
||||
cursor.close()
|
||||
conn.close()
|
||||
print("-------")
|
||||
|
||||
def getDataFromFile(pathFile):
|
||||
print(pathFile)
|
||||
path = pathFile.split("/")
|
||||
unitname = path[len(path) - 2]
|
||||
toolname = path[len(path) - 1].split("_")[0]
|
||||
if(toolname.startswith("GD") and unitname):
|
||||
dati = []
|
||||
print(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+"-> apro "+pathFile)
|
||||
with open(pathFile, 'r') as file:
|
||||
data = file.readlines()
|
||||
data = [row.rstrip() for row in data]
|
||||
if("/modb/" in data[5] or "/dsas/" in data[5]): #gd e modb (lora)
|
||||
#print("modb")
|
||||
pathFile = pathFile.replace("GD", "DT")
|
||||
print(pathFile)
|
||||
pathFileName = pathFile.split("_")[0]
|
||||
pathFileDate = int(pathFile.split("_")[1].split(".")[0])
|
||||
pathFileExt = pathFile.split("_")[1].split(".")[1]
|
||||
for xsec in range(1, 60):
|
||||
if os.path.isfile(pathFile):
|
||||
#print(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+"-> file DT trovato")
|
||||
#print(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+"-> apro "+pathFile)
|
||||
with open(pathFile, 'r') as fileDT:
|
||||
dataDT = fileDT.readlines()
|
||||
dataDT = [rowDT.rstrip() for rowDT in dataDT]
|
||||
dataDT = dataDT[7:]
|
||||
nodenum = 2
|
||||
toolname = toolname.replace("GD", "DT")
|
||||
data = data[7:]
|
||||
for index, row in enumerate(data):
|
||||
#print(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+"-> index " + str(index))
|
||||
#print(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+"-> row " + row)
|
||||
try:
|
||||
lineDT = dataDT[index].split(";")
|
||||
if(lineDT[1] == '' or lineDT[1] == '-.-'):
|
||||
tempindex = index
|
||||
for ind in range(len(data)):
|
||||
tempindex-=1
|
||||
if(tempindex >= 0):
|
||||
lineDT[1] = dataDT[tempindex].split(";")[1]
|
||||
else:
|
||||
lineDT[1] = 12.0
|
||||
if(lineDT[1] != '' and lineDT[1] != '-.-'):
|
||||
break
|
||||
print(lineDT[1])
|
||||
if(lineDT[2] == '' or lineDT[2] == '-.-'):
|
||||
tempindex = index
|
||||
for ind in range(len(data)):
|
||||
tempindex-=1
|
||||
if(tempindex >= 0):
|
||||
lineDT[2] = dataDT[tempindex].split(";")[2]
|
||||
else:
|
||||
lineDT[2] = 20.0
|
||||
if(lineDT[2] != '' and lineDT[2] != '-.-'):
|
||||
break
|
||||
batUnit = float(lineDT[1])
|
||||
tempUnit = float(lineDT[2])
|
||||
line = row.split(";")
|
||||
if(line[2] == '' and line[3] == ''):#se bat e temp sono vuoti
|
||||
continue
|
||||
dt = lineDT[0].split(" ")
|
||||
if("/" in dt[0]):
|
||||
try:
|
||||
date = str(datetime.strptime(dt[0], "%d/%m/%Y").strftime("%Y-%m-%d"))
|
||||
#print("The string is a date with format " + "%d/%m/%Y")
|
||||
except ValueError:
|
||||
print()
|
||||
#print("The string IS NOT a date with format " + "%d/%m/%Y")#debug
|
||||
try:
|
||||
date = str(datetime.strptime(dt[0], "%Y/%m/%d").strftime("%Y-%m-%d"))
|
||||
#print("The string is a date with format " + "%Y/%m/%d")
|
||||
except ValueError:
|
||||
print()
|
||||
#print("The string IS NOT a date with format " + "%Y/%m/%d")#debug
|
||||
else:
|
||||
date = dt[0]
|
||||
time = dt[1]
|
||||
batlevel = float(line[2])
|
||||
temp = float(line[3])
|
||||
if len(line) == 6:
|
||||
if line[4] == "|":
|
||||
val0 = line[5]
|
||||
dati.append((unitname, toolname, nodenum, date, time, batUnit, tempUnit, batlevel, temp, val0, None))
|
||||
elif len(line) == 5:
|
||||
rssi = line[4]
|
||||
dati.append((unitname, toolname, nodenum, date, time, batUnit, tempUnit, batlevel, temp, None, rssi))
|
||||
except IndexError:
|
||||
print("exception: different lenght break")
|
||||
break
|
||||
#print("The string IS NOT a date with format " + "%d/%m/%Y")#debug
|
||||
return dati
|
||||
pathFileDate -= 1
|
||||
pathFile = pathFileName + "_" + str(pathFileDate)+ "." + pathFileExt
|
||||
print(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+"-> CHECK "+pathFile)
|
||||
|
||||
def main():
|
||||
insertData(getDataFromFile(sys.argv[1]))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user