diff --git a/env/ftp.ini b/env/ftp.ini index 927f35a..fe75176 100644 --- a/env/ftp.ini +++ b/env/ftp.ini @@ -28,7 +28,7 @@ [tool] Types = MUX|MUMS|MODB|IPTM|MUSA|LOC|GD|D2W|CR1000X|G301|NESA|GS1|G201|TLP|DSAS|HORTUS|HEALTH-|READINGS-|INTEGRITY MONITOR|MESSPUNKTEPINI_|HIRPINIA|CO_[0-9]{4}_[0-9]|VULINK Names = LOC[0-9]{4}|DT[0-9]{4}|GD[0-9]{4}|[0-9]{18}|MEASUREMENTS_|CHESA_ARCOIRIS_[0-9]*|TS_PS_PETITES_CROISETTES|CO_[0-9]{4}_[0-9] - Alias = HEALTH-:SISGEO|READINGS-:SISGEO|INTEGRITY MONITOR:STAZIONETOTALE|MESSPUNKTEPINI_:STAZIONETOTALE|CO_:SOROTECPINI + Alias = CO_:=|HEALTH-:HEALTH|READINGS-:READINGS|MESSPUNKTEPINI_:MESSPUNKTEPINI| [csv] Infos = IP|Subnet|Gateway diff --git a/src/old_script/hirpiniaLoadScript.py b/src/old_script/hirpiniaLoadScript.py new file mode 100755 index 0000000..87e37e4 --- /dev/null +++ b/src/old_script/hirpiniaLoadScript.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +import sys +import os +from mysql.connector import MySQLConnection, Error +from dbconfig import read_db_config +from decimal import Decimal +from datetime import datetime +import ezodf + +def getDataFromCsv(pathFile): + try: + folder_path, file_with_extension = os.path.split(pathFile) + unit_name = os.path.basename(folder_path)#unitname + tool_name, _ = os.path.splitext(file_with_extension)#toolname + tool_name = tool_name.replace("HIRPINIA_", "") + tool_name = tool_name.split("_")[0] + print(unit_name, tool_name) + datiRaw = [] + doc = ezodf.opendoc(pathFile) + for sheet in doc.sheets: + node_num = sheet.name.replace("S-", "") + print(f"Sheet Name: {sheet.name}") + rows_to_skip = 2 + for i, row in enumerate(sheet.rows()): + if i < rows_to_skip: + continue + row_data = [cell.value for cell in row] + date_time = datetime.strptime(row_data[0], "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S").split(" ") + date = date_time[0] + time = date_time[1] + val0 = row_data[2] + val1 = row_data[4] + val2 = row_data[6] + val3 = row_data[8] + datiRaw.append((unit_name, tool_name, node_num, date, time, -1, -273, val0, val1, val2, val3)) + try: + db_config = read_db_config() + conn = MySQLConnection(**db_config) + cursor = conn.cursor(dictionary=True) + queryRaw = "insert ignore into RAWDATACOR(UnitName,ToolNameID,NodeNum,EventDate,EventTime,BatLevel,Temperature,Val0,Val1,Val2,Val3) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" + cursor.executemany(queryRaw, datiRaw) + conn.commit() + except Error as e: + print('Error:', e) + finally: + queryMatlab = "select m.matcall from tools as t join units as u on u.id=t.unit_id join matfuncs as m on m.id=t.matfunc where u.name=%s and t.name=%s" + cursor.execute(queryMatlab, [unit_name, tool_name]) + resultMatlab = cursor.fetchall() + if(resultMatlab): + print("Avvio "+str(resultMatlab[0]["matcall"])) + os.system("cd /usr/local/matlab_func/; ./run_"+str(resultMatlab[0]["matcall"])+".sh /usr/local/MATLAB/MATLAB_Runtime/v93/ "+str(unit_name)+" "+str(tool_name)+"") + cursor.close() + conn.close() + except Exception as e: + print(f"An unexpected error occurred: {str(e)}\n") + +def main(): + print("Avviato.") + getDataFromCsv(sys.argv[1]) + print("Finito.") + +if __name__ == '__main__': + main() diff --git a/src/old_script/sisgeoLoadScript.py b/src/old_script/sisgeoLoadScript.py new file mode 100755 index 0000000..14735e0 --- /dev/null +++ b/src/old_script/sisgeoLoadScript.py @@ -0,0 +1,305 @@ +#!/usr/bin/env python3 +import sys +import os +from mysql.connector import MySQLConnection, Error +from dbconfig import read_db_config +from decimal import Decimal +from datetime import datetime + +def insertData(dati): + #print(dati) + #print(len(dati)) + if(len(dati) > 0): + db_config = read_db_config() + conn = MySQLConnection(**db_config) + cursor = conn.cursor() + if(len(dati) == 2): + u = "" + t = "" + rawdata = dati[0] + elabdata = dati[1] + if(len(rawdata) > 0): + for r in rawdata: + #print(r) + #print(len(r)) + if(len(r) == 6):#nodo1 + unitname = r[0] + toolname = r[1] + nodenum = r[2] + pressure = Decimal(r[3])*100 + date = r[4] + time = r[5] + query = "SELECT * from RAWDATACOR WHERE UnitName=%s AND ToolNameID=%s AND NodeNum=%s ORDER BY EventDate desc,EventTime desc limit 1" + try: + cursor.execute(query, [unitname, toolname, nodenum]) + result = cursor.fetchall() + if(result): + if(result[0][8] is None): + datetimeOld = datetime.strptime(str(result[0][4]) + " " + str(result[0][5]), "%Y-%m-%d %H:%M:%S") + datetimeNew = datetime.strptime(str(date) + " " + str(time), "%Y-%m-%d %H:%M:%S") + dateDiff = datetimeNew - datetimeOld + if(dateDiff.total_seconds() / 3600 >= 5): + query = "INSERT INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, val0, BatLevelModule, TemperatureModule) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" + try: + cursor.execute(query, [unitname, toolname, nodenum, date, time, -1, -273, pressure, -1, -273]) + conn.commit() + except Error as e: + print('Error:', e) + else: + query = "UPDATE RAWDATACOR SET val0=%s, EventDate=%s, EventTime=%s WHERE UnitName=%s AND ToolNameID=%s AND NodeNum=%s AND val0 is NULL ORDER BY EventDate desc,EventTime desc limit 1" + try: + cursor.execute(query, [pressure, date, time, unitname, toolname, nodenum]) + conn.commit() + except Error as e: + print('Error:', e) + elif(result[0][8] is not None): + query = "INSERT INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, val0, BatLevelModule, TemperatureModule) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" + try: + cursor.execute(query, [unitname, toolname, nodenum, date, time, -1, -273, pressure, -1, -273]) + conn.commit() + except Error as e: + print('Error:', e) + else: + query = "INSERT INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, val0, BatLevelModule, TemperatureModule) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" + try: + cursor.execute(query, [unitname, toolname, nodenum, date, time, -1, -273, pressure, -1, -273]) + conn.commit() + except Error as e: + print('Error:', e) + except Error as e: + print('Error:', e) + else:#altri 2->5 + unitname = r[0] + toolname = r[1] + nodenum = r[2] + freqinhz = r[3] + therminohms = r[4] + freqindigit = r[5] + date = r[6] + time = r[7] + query = "SELECT * from RAWDATACOR WHERE UnitName=%s AND ToolNameID=%s AND NodeNum=%s ORDER BY EventDate desc,EventTime desc limit 1" + try: + cursor.execute(query, [unitname, toolname, nodenum]) + result = cursor.fetchall() + if(result): + if(result[0][8] is None): + query = "UPDATE RAWDATACOR SET val0=%s, val1=%s, val2=%s, EventDate=%s, EventTime=%s WHERE UnitName=%s AND ToolNameID=%s AND NodeNum=%s AND val0 is NULL ORDER BY EventDate desc,EventTime desc limit 1" + try: + cursor.execute(query, [freqinhz, therminohms, freqindigit, date, time, unitname, toolname, nodenum]) + conn.commit() + except Error as e: + print('Error:', e) + elif(result[0][8] is not None): + query = "INSERT INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, val0, val1, val2, BatLevelModule, TemperatureModule) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" + try: + cursor.execute(query, [unitname, toolname, nodenum, date, time, -1, -273, freqinhz, therminohms, freqindigit, -1, -273]) + conn.commit() + except Error as e: + print('Error:', e) + else: + query = "INSERT INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, val0, val1, val2, BatLevelModule, TemperatureModule) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" + try: + cursor.execute(query, [unitname, toolname, nodenum, date, time, -1, -273, freqinhz, therminohms, freqindigit, -1, -273]) + conn.commit() + except Error as e: + print('Error:', e) + except Error as e: + print('Error:', e) + + if(len(elabdata) > 0): + for e in elabdata: + #print(e) + #print(len(e)) + if(len(e) == 6):#nodo1 + unitname = e[0] + toolname = e[1] + nodenum = e[2] + pressure = Decimal(e[3])*100 + date = e[4] + time = e[5] + try: + query = "INSERT INTO ELABDATADISP(UnitName, ToolNameID, NodeNum, EventDate, EventTime, pressure) VALUES(%s,%s,%s,%s,%s,%s)" + cursor.execute(query, [unitname, toolname, nodenum, date, time, pressure]) + conn.commit() + except Error as e: + print('Error:', e) + else:#altri 2->5 + unitname = e[0] + toolname = e[1] + u = unitname + t = toolname + nodenum = e[2] + pch = e[3] + tch = e[4] + date = e[5] + time = e[6] + try: + query = "INSERT INTO ELABDATADISP(UnitName, ToolNameID, NodeNum, EventDate, EventTime, XShift, T_node) VALUES(%s,%s,%s,%s,%s,%s,%s)" + cursor.execute(query, [unitname, toolname, nodenum, date, time, pch, tch]) + conn.commit() + except Error as e: + print('Error:', e) + #os.system("cd /usr/local/matlab_func/; ./run_ATD_lnx.sh /usr/local/MATLAB/MATLAB_Runtime/v93/ "+u+" "+t+"") + else: + for r in dati: + #print(r) + unitname = r[0] + toolname = r[1] + nodenum = r[2] + date = r[3] + time = r[4] + battery = r[5] + temperature = r[6] + query = "SELECT * from RAWDATACOR WHERE UnitName=%s AND ToolNameID=%s AND NodeNum=%s ORDER BY EventDate desc,EventTime desc limit 1" + try: + cursor.execute(query, [unitname, toolname, nodenum]) + result = cursor.fetchall() + if(result): + if(result[0][25] is None or result[0][25] == -1.00): + datetimeOld = datetime.strptime(str(result[0][4]) + " " + str(result[0][5]), "%Y-%m-%d %H:%M:%S") + datetimeNew = datetime.strptime(str(date) + " " + str(time), "%Y-%m-%d %H:%M:%S") + dateDiff = datetimeNew - datetimeOld + #print(dateDiff.total_seconds() / 3600) + if(dateDiff.total_seconds() / 3600 >= 5): + query = "INSERT INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, BatLevelModule, TemperatureModule) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)" + try: + cursor.execute(query, [unitname, toolname, nodenum, date, time, -1, -273, battery, temperature]) + conn.commit() + except Error as e: + print('Error:', e) + else: + query = "UPDATE RAWDATACOR SET BatLevelModule=%s, TemperatureModule=%s WHERE UnitName=%s AND ToolNameID=%s AND NodeNum=%s AND (BatLevelModule is NULL or BatLevelModule = -1.00) ORDER BY EventDate desc,EventTime desc limit 1" + try: + cursor.execute(query, [battery, temperature, unitname, toolname, nodenum]) + conn.commit() + except Error as e: + print('Error:', e) + elif(result[0][25] is not None and result[0][25] != -1.00): + query = "INSERT INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, BatLevelModule, TemperatureModule) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)" + try: + cursor.execute(query, [unitname, toolname, nodenum, date, time, -1, -273, battery, temperature]) + conn.commit() + except Error as e: + print('Error:', e) + else: + query = "INSERT INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, BatLevelModule, TemperatureModule) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)" + try: + cursor.execute(query, [unitname, toolname, nodenum, date, time, -1, -273, battery, temperature]) + conn.commit() + except Error as e: + print('Error:', e) + except Error as e: + print('Error:', e) + cursor.close() + conn.close() + +def getDataFromCsv(pathFile): + with open(pathFile, 'r') as file: + data = file.readlines() + data = [row.rstrip() for row in data] + serial_number = data[0].split(",")[1] + data = data[10:] #rimuove righe header + dati = [] + rawDatiReadings = []#tmp + elabDatiReadings = []#tmp + datiReadings = [] + i = 0 + unit = "" + tool = "" + #row = data[0]#quando non c'era il for solo 1 riga + for row in data:#se ci sono righe multiple + row = row.split(",") + if i == 0: + query = "SELECT unit_name, tool_name FROM sisgeo_tools WHERE serial_number='"+serial_number+"'" + try: + db_config = read_db_config() + conn = MySQLConnection(**db_config) + cursor = conn.cursor() + cursor.execute(query) + result = cursor.fetchall() + except Error as e: + print('Error:', e) + unit = result[0][0] + tool = result[0][1] + #print(result[0][0]) + #print(result[0][1]) + if("health" in pathFile): + datetime = str(row[0]).replace("\"", "").split(" ") + date = datetime[0] + time = datetime[1] + battery = row[1] + temperature = row[2] + dati.append((unit, tool, 1, date, time, battery, temperature)) + dati.append((unit, tool, 2, date, time, battery, temperature)) + dati.append((unit, tool, 3, date, time, battery, temperature)) + dati.append((unit, tool, 4, date, time, battery, temperature)) + dati.append((unit, tool, 5, date, time, battery, temperature)) + else: + datetime = str(row[0]).replace("\"", "").split(" ") + date = datetime[0] + time = datetime[1] + atmpressure = row[1]#nodo1 + #raw + freqinhzch1 = row[2]#nodo2 + freqindigitch1 = row[3]#nodo2 + thermResInOhmsch1 = row[4]#nodo2 + freqinhzch2 = row[5]#nodo3 + freqindigitch2 = row[6]#nodo3 + thermResInOhmsch2 = row[7]#nodo3 + freqinhzch3 = row[8]#nodo4 + freqindigitch3 = row[9]#nodo4 + thermResInOhmsch3 = row[10]#nodo4 + freqinhzch4 = row[11]#nodo5 + freqindigitch4 = row[12]#nodo5 + thermResInOhmsch4 = row[13]#nodo5 + #elab + pch1 = row[18]#nodo2 + tch1 = row[19]#nodo2 + pch2 = row[20]#nodo3 + tch2 = row[21]#nodo3 + pch3 = row[22]#nodo4 + tch3 = row[23]#nodo4 + pch4 = row[24]#nodo5 + tch4 = row[25]#nodo5 + + rawDatiReadings.append((unit, tool, 1, atmpressure, date, time)) + rawDatiReadings.append((unit, tool, 2, freqinhzch1, thermResInOhmsch1, freqindigitch1, date, time)) + rawDatiReadings.append((unit, tool, 3, freqinhzch2, thermResInOhmsch2, freqindigitch2, date, time)) + rawDatiReadings.append((unit, tool, 4, freqinhzch3, thermResInOhmsch3, freqindigitch3, date, time)) + rawDatiReadings.append((unit, tool, 5, freqinhzch4, thermResInOhmsch4, freqindigitch4, date, time)) + + elabDatiReadings.append((unit, tool, 1, atmpressure, date, time)) + elabDatiReadings.append((unit, tool, 2, pch1, tch1, date, time)) + elabDatiReadings.append((unit, tool, 3, pch2, tch2, date, time)) + elabDatiReadings.append((unit, tool, 4, pch3, tch3, date, time)) + elabDatiReadings.append((unit, tool, 5, pch4, tch4, date, time)) + + #[ram],[elab]#quando c'era solo 1 riga + #dati = [ + # [ + # (unit, tool, 1, atmpressure, date, time), + # (unit, tool, 2, freqinhzch1, thermResInOhmsch1, freqindigitch1, date, time), + # (unit, tool, 3, freqinhzch2, thermResInOhmsch2, freqindigitch2, date, time), + # (unit, tool, 4, freqinhzch3, thermResInOhmsch3, freqindigitch3, date, time), + # (unit, tool, 5, freqinhzch4, thermResInOhmsch4, freqindigitch4, date, time), + # ], [ + # (unit, tool, 1, atmpressure, date, time), + # (unit, tool, 2, pch1, tch1, date, time), + # (unit, tool, 3, pch2, tch2, date, time), + # (unit, tool, 4, pch3, tch3, date, time), + # (unit, tool, 5, pch4, tch4, date, time), + # ] + # ] + i+=1 + #print(dati) + if(len(rawDatiReadings) > 0 or len(elabDatiReadings) > 0): + datiReadings = [rawDatiReadings, elabDatiReadings] + if(len(datiReadings) > 0): + return datiReadings + return dati + +def main(): + insertData(getDataFromCsv(sys.argv[1])) + +if __name__ == '__main__': + main() diff --git a/src/old_script/sorotecPini.py b/src/old_script/sorotecPini.py new file mode 100755 index 0000000..b46e9dc --- /dev/null +++ b/src/old_script/sorotecPini.py @@ -0,0 +1,306 @@ +#!/usr/bin/env python3 +import sys +import os +from mysql.connector import MySQLConnection, Error +from dbconfig import read_db_config +from datetime import datetime +import math +import shutil + +def removeDuplicates(lst): + return list(set([i for i in lst])) + +def getDataFromCsvAndInsert(pathFile): + try: + print(pathFile) + folder_name = pathFile.split("/")[-2]#cartella + with open(pathFile, 'r') as file: + data = file.readlines() + data = [row.rstrip() for row in data] + if(len(data) > 0 and data is not None): + if(folder_name == "ID0247"): + unit_name = "ID0247" + tool_name = "DT0001" + data.pop(0) #rimuove header + data.pop(0) + data.pop(0) + data.pop(0) + data = [element for element in data if element != ""] + try: + db_config = read_db_config() + conn = MySQLConnection(**db_config) + cursor = conn.cursor() + queryElab = "insert ignore into ELABDATADISP(UnitName,ToolNameID,NodeNum,EventDate,EventTime,load_value) values (%s,%s,%s,%s,%s,%s)" + queryRaw = "insert ignore into RAWDATACOR(UnitName,ToolNameID,NodeNum,EventDate,EventTime,BatLevel,Temperature,Val0) values (%s,%s,%s,%s,%s,%s,%s,%s)" + if("_1_" in pathFile): + print("File tipo 1.\n") + #print(unit_name, tool_name) + dataToInsertElab = [] + dataToInsertRaw = [] + for row in data: + rowSplitted = row.replace("\"","").split(";") + eventTimestamp = rowSplitted[0].split(" ") + date = eventTimestamp[0].split("-") + date = date[2]+"-"+date[1]+"-"+date[0] + time = eventTimestamp[1] + an3 = rowSplitted[1] + an4 = rowSplitted[2]#V unit battery + OUTREG2 = rowSplitted[3] + E8_181_CH2 = rowSplitted[4]#2 + E8_181_CH3 = rowSplitted[5]#3 + E8_181_CH4 = rowSplitted[6]#4 + E8_181_CH5 = rowSplitted[7]#5 + E8_181_CH6 = rowSplitted[8]#6 + E8_181_CH7 = rowSplitted[9]#7 + E8_181_CH8 = rowSplitted[10]#8 + E8_182_CH1 = rowSplitted[11]#9 + E8_182_CH2 = rowSplitted[12]#10 + E8_182_CH3 = rowSplitted[13]#11 + E8_182_CH4 = rowSplitted[14]#12 + E8_182_CH5 = rowSplitted[15]#13 + E8_182_CH6 = rowSplitted[16]#14 + E8_182_CH7 = rowSplitted[17]#15 + E8_182_CH8 = rowSplitted[18]#16 + E8_183_CH1 = rowSplitted[19]#17 + E8_183_CH2 = rowSplitted[20]#18 + E8_183_CH3 = rowSplitted[21]#19 + E8_183_CH4 = rowSplitted[22]#20 + E8_183_CH5 = rowSplitted[23]#21 + E8_183_CH6 = rowSplitted[24]#22 + E8_183_CH7 = rowSplitted[25]#23 + E8_183_CH8 = rowSplitted[26]#24 + E8_184_CH1 = rowSplitted[27]#25 + E8_184_CH2 = rowSplitted[28]#26 + E8_184_CH3 = rowSplitted[29]#27 mv/V + E8_184_CH4 = rowSplitted[30]#28 mv/V + E8_184_CH5 = rowSplitted[31]#29 mv/V + E8_184_CH6 = rowSplitted[32]#30 mv/V + E8_184_CH7 = rowSplitted[33]#31 mv/V + E8_184_CH8 = rowSplitted[34]#32 mv/V + E8_181_CH1 = rowSplitted[35]#1 + an1 = rowSplitted[36] + an2 = rowSplitted[37] + #print(unit_name, tool_name, 1, E8_181_CH1) + #print(unit_name, tool_name, 2, E8_181_CH2) + #print(unit_name, tool_name, 3, E8_181_CH3) + #print(unit_name, tool_name, 4, E8_181_CH4) + #print(unit_name, tool_name, 5, E8_181_CH5) + #print(unit_name, tool_name, 6, E8_181_CH6) + #print(unit_name, tool_name, 7, E8_181_CH7) + #print(unit_name, tool_name, 8, E8_181_CH8) + #print(unit_name, tool_name, 9, E8_182_CH1) + #print(unit_name, tool_name, 10, E8_182_CH2) + #print(unit_name, tool_name, 11, E8_182_CH3) + #print(unit_name, tool_name, 12, E8_182_CH4) + #print(unit_name, tool_name, 13, E8_182_CH5) + #print(unit_name, tool_name, 14, E8_182_CH6) + #print(unit_name, tool_name, 15, E8_182_CH7) + #print(unit_name, tool_name, 16, E8_182_CH8) + #print(unit_name, tool_name, 17, E8_183_CH1) + #print(unit_name, tool_name, 18, E8_183_CH2) + #print(unit_name, tool_name, 19, E8_183_CH3) + #print(unit_name, tool_name, 20, E8_183_CH4) + #print(unit_name, tool_name, 21, E8_183_CH5) + #print(unit_name, tool_name, 22, E8_183_CH6) + #print(unit_name, tool_name, 23, E8_183_CH7) + #print(unit_name, tool_name, 24, E8_183_CH8) + #print(unit_name, tool_name, 25, E8_184_CH1) + #print(unit_name, tool_name, 26, E8_184_CH2) + #print(unit_name, tool_name, 27, E8_184_CH3) + #print(unit_name, tool_name, 28, E8_184_CH4) + #print(unit_name, tool_name, 29, E8_184_CH5) + #print(unit_name, tool_name, 30, E8_184_CH6) + #print(unit_name, tool_name, 31, E8_184_CH7) + #print(unit_name, tool_name, 32, E8_184_CH8) + #--------------------------------------------------------------------------------------- + dataToInsertRaw.append((unit_name, tool_name, 1, date, time, an4, -273, E8_181_CH1)) + dataToInsertRaw.append((unit_name, tool_name, 2, date, time, an4, -273, E8_181_CH2)) + dataToInsertRaw.append((unit_name, tool_name, 3, date, time, an4, -273, E8_181_CH3)) + dataToInsertRaw.append((unit_name, tool_name, 4, date, time, an4, -273, E8_181_CH4)) + dataToInsertRaw.append((unit_name, tool_name, 5, date, time, an4, -273, E8_181_CH5)) + dataToInsertRaw.append((unit_name, tool_name, 6, date, time, an4, -273, E8_181_CH6)) + dataToInsertRaw.append((unit_name, tool_name, 7, date, time, an4, -273, E8_181_CH7)) + dataToInsertRaw.append((unit_name, tool_name, 8, date, time, an4, -273, E8_181_CH8)) + dataToInsertRaw.append((unit_name, tool_name, 9, date, time, an4, -273, E8_182_CH1)) + dataToInsertRaw.append((unit_name, tool_name, 10, date, time, an4, -273, E8_182_CH2)) + dataToInsertRaw.append((unit_name, tool_name, 11, date, time, an4, -273, E8_182_CH3)) + dataToInsertRaw.append((unit_name, tool_name, 12, date, time, an4, -273, E8_182_CH4)) + dataToInsertRaw.append((unit_name, tool_name, 13, date, time, an4, -273, E8_182_CH5)) + dataToInsertRaw.append((unit_name, tool_name, 14, date, time, an4, -273, E8_182_CH6)) + dataToInsertRaw.append((unit_name, tool_name, 15, date, time, an4, -273, E8_182_CH7)) + dataToInsertRaw.append((unit_name, tool_name, 16, date, time, an4, -273, E8_182_CH8)) + dataToInsertRaw.append((unit_name, tool_name, 17, date, time, an4, -273, E8_183_CH1)) + dataToInsertRaw.append((unit_name, tool_name, 18, date, time, an4, -273, E8_183_CH2)) + dataToInsertRaw.append((unit_name, tool_name, 19, date, time, an4, -273, E8_183_CH3)) + dataToInsertRaw.append((unit_name, tool_name, 20, date, time, an4, -273, E8_183_CH4)) + dataToInsertRaw.append((unit_name, tool_name, 21, date, time, an4, -273, E8_183_CH5)) + dataToInsertRaw.append((unit_name, tool_name, 22, date, time, an4, -273, E8_183_CH6)) + dataToInsertRaw.append((unit_name, tool_name, 23, date, time, an4, -273, E8_183_CH7)) + dataToInsertRaw.append((unit_name, tool_name, 24, date, time, an4, -273, E8_183_CH8)) + dataToInsertRaw.append((unit_name, tool_name, 25, date, time, an4, -273, E8_184_CH1)) + dataToInsertRaw.append((unit_name, tool_name, 26, date, time, an4, -273, E8_184_CH2)) + #--------------------------------------------------------------------------------------- + dataToInsertElab.append((unit_name, tool_name, 1, date, time, E8_181_CH1)) + dataToInsertElab.append((unit_name, tool_name, 2, date, time, E8_181_CH2)) + dataToInsertElab.append((unit_name, tool_name, 3, date, time, E8_181_CH3)) + dataToInsertElab.append((unit_name, tool_name, 4, date, time, E8_181_CH4)) + dataToInsertElab.append((unit_name, tool_name, 5, date, time, E8_181_CH5)) + dataToInsertElab.append((unit_name, tool_name, 6, date, time, E8_181_CH6)) + dataToInsertElab.append((unit_name, tool_name, 7, date, time, E8_181_CH7)) + dataToInsertElab.append((unit_name, tool_name, 8, date, time, E8_181_CH8)) + dataToInsertElab.append((unit_name, tool_name, 9, date, time, E8_182_CH1)) + dataToInsertElab.append((unit_name, tool_name, 10, date, time, E8_182_CH2)) + dataToInsertElab.append((unit_name, tool_name, 11, date, time, E8_182_CH3)) + dataToInsertElab.append((unit_name, tool_name, 12, date, time, E8_182_CH4)) + dataToInsertElab.append((unit_name, tool_name, 13, date, time, E8_182_CH5)) + dataToInsertElab.append((unit_name, tool_name, 14, date, time, E8_182_CH6)) + dataToInsertElab.append((unit_name, tool_name, 15, date, time, E8_182_CH7)) + dataToInsertElab.append((unit_name, tool_name, 16, date, time, E8_182_CH8)) + dataToInsertElab.append((unit_name, tool_name, 17, date, time, E8_183_CH1)) + dataToInsertElab.append((unit_name, tool_name, 18, date, time, E8_183_CH2)) + dataToInsertElab.append((unit_name, tool_name, 19, date, time, E8_183_CH3)) + dataToInsertElab.append((unit_name, tool_name, 20, date, time, E8_183_CH4)) + dataToInsertElab.append((unit_name, tool_name, 21, date, time, E8_183_CH5)) + dataToInsertElab.append((unit_name, tool_name, 22, date, time, E8_183_CH6)) + dataToInsertElab.append((unit_name, tool_name, 23, date, time, E8_183_CH7)) + dataToInsertElab.append((unit_name, tool_name, 24, date, time, E8_183_CH8)) + dataToInsertElab.append((unit_name, tool_name, 25, date, time, E8_184_CH1)) + dataToInsertElab.append((unit_name, tool_name, 26, date, time, E8_184_CH2)) + #--------------------------------------------------------------------------------------- + cursor.executemany(queryElab, dataToInsertElab) + cursor.executemany(queryRaw, dataToInsertRaw) + conn.commit() + #print(dataToInsertElab) + #print(dataToInsertRaw) + elif("_2_" in pathFile): + print("File tipo 2.\n") + #print(unit_name, tool_name) + dataToInsertElab = [] + dataToInsertRaw = [] + for row in data: + rowSplitted = row.replace("\"","").split(";") + eventTimestamp = rowSplitted[0].split(" ") + date = eventTimestamp[0].split("-") + date = date[2]+"-"+date[1]+"-"+date[0] + time = eventTimestamp[1] + an2 = rowSplitted[1] + an3 = rowSplitted[2] + an1 = rowSplitted[3] + OUTREG2 = rowSplitted[4] + E8_181_CH1 = rowSplitted[5]#33 mv/V + E8_181_CH2 = rowSplitted[6]#34 mv/V + E8_181_CH3 = rowSplitted[7]#35 mv/V + E8_181_CH4 = rowSplitted[8]#36 mv/V + E8_181_CH5 = rowSplitted[9]#37 mv/V + E8_181_CH6 = rowSplitted[10]#38 mv/V + E8_181_CH7 = rowSplitted[11]#39 mv/V + E8_181_CH8 = rowSplitted[12]#40 mv/V + E8_182_CH1 = rowSplitted[13]#41 + E8_182_CH2 = rowSplitted[14]#42 + E8_182_CH3 = rowSplitted[15]#43 + E8_182_CH4 = rowSplitted[16]#44 + E8_182_CH5 = rowSplitted[17]#45 mv/V + E8_182_CH6 = rowSplitted[18]#46 mv/V + E8_182_CH7 = rowSplitted[19]#47 mv/V + E8_182_CH8 = rowSplitted[20]#48 mv/V + E8_183_CH1 = rowSplitted[21]#49 + E8_183_CH2 = rowSplitted[22]#50 + E8_183_CH3 = rowSplitted[23]#51 + E8_183_CH4 = rowSplitted[24]#52 + E8_183_CH5 = rowSplitted[25]#53 mv/V + E8_183_CH6 = rowSplitted[26]#54 mv/V + E8_183_CH7 = rowSplitted[27]#55 mv/V + E8_183_CH8 = rowSplitted[28]#56 + E8_184_CH1 = rowSplitted[29]#57 + E8_184_CH2 = rowSplitted[30]#58 + E8_184_CH3 = rowSplitted[31]#59 + E8_184_CH4 = rowSplitted[32]#60 + E8_184_CH5 = rowSplitted[33]#61 + E8_184_CH6 = rowSplitted[34]#62 + E8_184_CH7 = rowSplitted[35]#63 mv/V + E8_184_CH8 = rowSplitted[36]#64 mv/V + an4 = rowSplitted[37]#V unit battery + #print(unit_name, tool_name, 33, E8_181_CH1) + #print(unit_name, tool_name, 34, E8_181_CH2) + #print(unit_name, tool_name, 35, E8_181_CH3) + #print(unit_name, tool_name, 36, E8_181_CH4) + #print(unit_name, tool_name, 37, E8_181_CH5) + #print(unit_name, tool_name, 38, E8_181_CH6) + #print(unit_name, tool_name, 39, E8_181_CH7) + #print(unit_name, tool_name, 40, E8_181_CH8) + #print(unit_name, tool_name, 41, E8_182_CH1) + #print(unit_name, tool_name, 42, E8_182_CH2) + #print(unit_name, tool_name, 43, E8_182_CH3) + #print(unit_name, tool_name, 44, E8_182_CH4) + #print(unit_name, tool_name, 45, E8_182_CH5) + #print(unit_name, tool_name, 46, E8_182_CH6) + #print(unit_name, tool_name, 47, E8_182_CH7) + #print(unit_name, tool_name, 48, E8_182_CH8) + #print(unit_name, tool_name, 49, E8_183_CH1) + #print(unit_name, tool_name, 50, E8_183_CH2) + #print(unit_name, tool_name, 51, E8_183_CH3) + #print(unit_name, tool_name, 52, E8_183_CH4) + #print(unit_name, tool_name, 53, E8_183_CH5) + #print(unit_name, tool_name, 54, E8_183_CH6) + #print(unit_name, tool_name, 55, E8_183_CH7) + #print(unit_name, tool_name, 56, E8_183_CH8) + #print(unit_name, tool_name, 57, E8_184_CH1) + #print(unit_name, tool_name, 58, E8_184_CH2) + #print(unit_name, tool_name, 59, E8_184_CH3) + #print(unit_name, tool_name, 60, E8_184_CH4) + #print(unit_name, tool_name, 61, E8_184_CH5) + #print(unit_name, tool_name, 62, E8_184_CH6) + #print(unit_name, tool_name, 63, E8_184_CH7) + #print(unit_name, tool_name, 64, E8_184_CH8) + #print(rowSplitted) + #--------------------------------------------------------------------------------------- + dataToInsertRaw.append((unit_name, tool_name, 41, date, time, an4, -273, E8_182_CH1)) + dataToInsertRaw.append((unit_name, tool_name, 42, date, time, an4, -273, E8_182_CH2)) + dataToInsertRaw.append((unit_name, tool_name, 43, date, time, an4, -273, E8_182_CH3)) + dataToInsertRaw.append((unit_name, tool_name, 44, date, time, an4, -273, E8_182_CH4)) + dataToInsertRaw.append((unit_name, tool_name, 49, date, time, an4, -273, E8_183_CH1)) + dataToInsertRaw.append((unit_name, tool_name, 50, date, time, an4, -273, E8_183_CH2)) + dataToInsertRaw.append((unit_name, tool_name, 51, date, time, an4, -273, E8_183_CH3)) + dataToInsertRaw.append((unit_name, tool_name, 52, date, time, an4, -273, E8_183_CH4)) + dataToInsertRaw.append((unit_name, tool_name, 56, date, time, an4, -273, E8_183_CH8)) + dataToInsertRaw.append((unit_name, tool_name, 57, date, time, an4, -273, E8_184_CH1)) + dataToInsertRaw.append((unit_name, tool_name, 58, date, time, an4, -273, E8_184_CH2)) + dataToInsertRaw.append((unit_name, tool_name, 59, date, time, an4, -273, E8_184_CH3)) + dataToInsertRaw.append((unit_name, tool_name, 60, date, time, an4, -273, E8_184_CH4)) + dataToInsertRaw.append((unit_name, tool_name, 61, date, time, an4, -273, E8_184_CH5)) + dataToInsertRaw.append((unit_name, tool_name, 62, date, time, an4, -273, E8_184_CH6)) + #--------------------------------------------------------------------------------------- + dataToInsertElab.append((unit_name, tool_name, 41, date, time, E8_182_CH1)) + dataToInsertElab.append((unit_name, tool_name, 42, date, time, E8_182_CH2)) + dataToInsertElab.append((unit_name, tool_name, 43, date, time, E8_182_CH3)) + dataToInsertElab.append((unit_name, tool_name, 44, date, time, E8_182_CH4)) + dataToInsertElab.append((unit_name, tool_name, 49, date, time, E8_183_CH1)) + dataToInsertElab.append((unit_name, tool_name, 50, date, time, E8_183_CH2)) + dataToInsertElab.append((unit_name, tool_name, 51, date, time, E8_183_CH3)) + dataToInsertElab.append((unit_name, tool_name, 52, date, time, E8_183_CH4)) + dataToInsertElab.append((unit_name, tool_name, 56, date, time, E8_183_CH8)) + dataToInsertElab.append((unit_name, tool_name, 57, date, time, E8_184_CH1)) + dataToInsertElab.append((unit_name, tool_name, 58, date, time, E8_184_CH2)) + dataToInsertElab.append((unit_name, tool_name, 59, date, time, E8_184_CH3)) + dataToInsertElab.append((unit_name, tool_name, 60, date, time, E8_184_CH4)) + dataToInsertElab.append((unit_name, tool_name, 61, date, time, E8_184_CH5)) + dataToInsertElab.append((unit_name, tool_name, 62, date, time, E8_184_CH6)) + #--------------------------------------------------------------------------------------- + cursor.executemany(queryElab, dataToInsertElab) + cursor.executemany(queryRaw, dataToInsertRaw) + conn.commit() + #print(dataToInsertElab) + #print(dataToInsertRaw) + except Error as e: + print('Error:', e) + finally: + cursor.close() + conn.close() + except Exception as e: + print(f"An unexpected error occurred: {str(e)}\n") + +def main(): + getDataFromCsvAndInsert(sys.argv[1]) + +if __name__ == '__main__': + main() diff --git a/src/utils/config/loader_ftp_csv.py b/src/utils/config/loader_ftp_csv.py index 3732e93..d275d97 100644 --- a/src/utils/config/loader_ftp_csv.py +++ b/src/utils/config/loader_ftp_csv.py @@ -57,7 +57,7 @@ class Config: self.tools_name = [part for part in c.get("tool", "Names").split('|')] self.tools_type = [part for part in c.get("tool", "Types").split('|')] self.tools_alias = { - key: value + key: key if value == '=' else value for item in c.get("tool", "Alias").split('|') for key, value in [item.split(':', 1)] } diff --git a/src/utils/ftp/file_management.py b/src/utils/ftp/file_management.py index 67a7550..63cb88e 100644 --- a/src/utils/ftp/file_management.py +++ b/src/utils/ftp/file_management.py @@ -32,7 +32,15 @@ def on_file_received(self: object, file: str) -> None: tool_type = extract_value(cfg.tools_type, filename, str(lines[0:10])) tool_info = "{}" - unit_type = cfg.units_alias.get(unit_type.upper(), unit_type) + # se esiste l'alias in alias_unit_type, allora prende il valore dell'alias... verifica sia lo unit_type completo che i primi 3 caratteri per CO_xxxxx + upper_unit_type = unit_type.upper() + unit_type = cfg.units_alias.get(upper_unit_type) or \ + cfg.units_alias.get(upper_unit_type[:3]) or \ + upper_unit_type + upper_tool_type = tool_type.upper() + tool_type = cfg.tools_alias.get(upper_tool_type) or \ + cfg.tools_alias.get(upper_tool_type[:3]) or \ + upper_tool_type try: conn = connetti_db(cfg) diff --git a/src/utils/parsers/by_type/hirpinia_hirpinia.py b/src/utils/parsers/by_type/hirpinia_hirpinia.py new file mode 100644 index 0000000..4584809 --- /dev/null +++ b/src/utils/parsers/by_type/hirpinia_hirpinia.py @@ -0,0 +1,45 @@ +import asyncio +import tempfile +import os + +from utils.database import WorkflowFlags +from utils.database.loader_action import update_status, unlock +from utils.csv.data_preparation import get_data + +import logging + +logger = logging.getLogger(__name__) + +async def main_loader(cfg: object, id: int, pool: object) -> None: + + filename, UnitName, ToolNameID, ToolData = await get_data(cfg, id, pool) + # Creare un file temporaneo + with tempfile.NamedTemporaryFile(mode='w', prefix= filename, suffix='.csv', delete=False) as temp_file: + temp_file.write(ToolData) + temp_filename = temp_file.name + + try: + # Usa asyncio.subprocess per vero async + process = await asyncio.create_subprocess_exec( + 'python3', 'old_script/hirpiniaLoadScript.py', temp_filename, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE + ) + stdout, stderr = await process.communicate() + + result_stdout = stdout.decode('utf-8') + result_stderr = stderr.decode('utf-8') + + finally: + # Pulire il file temporaneo + os.unlink(temp_filename) + + if process.returncode != 0: + logger.error(f"Errore nell'esecuzione del programma hirpiniaLoadScript.py: {result_stderr}") + raise Exception(f"Errore nel programma: {result_stderr}") + else: + logger.info("Programma hirpiniaLoadScript.py eseguito con successo.") + logger.debug(f"Stdout: {result_stdout}") + await update_status(cfg, id, WorkflowFlags.DATA_LOADED, pool) + await update_status(cfg, id, WorkflowFlags.DATA_ELABORATED, pool) + await unlock(cfg, id, pool) \ No newline at end of file diff --git a/src/utils/parsers/by_type/sisgeo_readings-.py b/src/utils/parsers/by_type/sisgeo_health.py similarity index 84% rename from src/utils/parsers/by_type/sisgeo_readings-.py rename to src/utils/parsers/by_type/sisgeo_health.py index a7c7dc0..fe6db3b 100644 --- a/src/utils/parsers/by_type/sisgeo_readings-.py +++ b/src/utils/parsers/by_type/sisgeo_health.py @@ -21,7 +21,7 @@ async def main_loader(cfg: object, id: int, pool: object) -> None: try: # Usa asyncio.subprocess per vero async process = await asyncio.create_subprocess_exec( - 'python3', 'old_script/TS_PiniScript.py', temp_filename, + 'python3', 'old_script/sisgeoLoadScript.py', temp_filename, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) @@ -35,10 +35,10 @@ async def main_loader(cfg: object, id: int, pool: object) -> None: os.unlink(temp_filename) if process.returncode != 0: - logger.error(f"Errore nell'esecuzione del programma TS_PiniScript.py: {result_stderr}") + logger.error(f"Errore nell'esecuzione del programma sisgeoLoadScript.py: {result_stderr}") raise Exception(f"Errore nel programma: {result_stderr}") else: - logger.info("Programma TS_PiniScript.py eseguito con successo.") + logger.info("Programma sisgeoLoadScript.py eseguito con successo.") logger.debug(f"Stdout: {result_stdout}") await update_status(cfg, id, WorkflowFlags.DATA_LOADED, pool) await update_status(cfg, id, WorkflowFlags.DATA_ELABORATED, pool) diff --git a/src/utils/parsers/by_type/stazionetotale_messpunktepini_.py b/src/utils/parsers/by_type/sisgeo_readings.py similarity index 84% rename from src/utils/parsers/by_type/stazionetotale_messpunktepini_.py rename to src/utils/parsers/by_type/sisgeo_readings.py index a7c7dc0..fe6db3b 100644 --- a/src/utils/parsers/by_type/stazionetotale_messpunktepini_.py +++ b/src/utils/parsers/by_type/sisgeo_readings.py @@ -21,7 +21,7 @@ async def main_loader(cfg: object, id: int, pool: object) -> None: try: # Usa asyncio.subprocess per vero async process = await asyncio.create_subprocess_exec( - 'python3', 'old_script/TS_PiniScript.py', temp_filename, + 'python3', 'old_script/sisgeoLoadScript.py', temp_filename, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) @@ -35,10 +35,10 @@ async def main_loader(cfg: object, id: int, pool: object) -> None: os.unlink(temp_filename) if process.returncode != 0: - logger.error(f"Errore nell'esecuzione del programma TS_PiniScript.py: {result_stderr}") + logger.error(f"Errore nell'esecuzione del programma sisgeoLoadScript.py: {result_stderr}") raise Exception(f"Errore nel programma: {result_stderr}") else: - logger.info("Programma TS_PiniScript.py eseguito con successo.") + logger.info("Programma sisgeoLoadScript.py eseguito con successo.") logger.debug(f"Stdout: {result_stdout}") await update_status(cfg, id, WorkflowFlags.DATA_LOADED, pool) await update_status(cfg, id, WorkflowFlags.DATA_ELABORATED, pool) diff --git a/src/utils/parsers/by_type/sisgeo_health-.py b/src/utils/parsers/by_type/sorotecpini_co.py similarity index 85% rename from src/utils/parsers/by_type/sisgeo_health-.py rename to src/utils/parsers/by_type/sorotecpini_co.py index a7c7dc0..1ebf312 100644 --- a/src/utils/parsers/by_type/sisgeo_health-.py +++ b/src/utils/parsers/by_type/sorotecpini_co.py @@ -21,7 +21,7 @@ async def main_loader(cfg: object, id: int, pool: object) -> None: try: # Usa asyncio.subprocess per vero async process = await asyncio.create_subprocess_exec( - 'python3', 'old_script/TS_PiniScript.py', temp_filename, + 'python3', 'old_script/sorotecPini.py', temp_filename, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) @@ -35,10 +35,10 @@ async def main_loader(cfg: object, id: int, pool: object) -> None: os.unlink(temp_filename) if process.returncode != 0: - logger.error(f"Errore nell'esecuzione del programma TS_PiniScript.py: {result_stderr}") + logger.error(f"Errore nell'esecuzione del programma sorotecPini.py: {result_stderr}") raise Exception(f"Errore nel programma: {result_stderr}") else: - logger.info("Programma TS_PiniScript.py eseguito con successo.") + logger.info("Programma sorotecPini.py eseguito con successo.") logger.debug(f"Stdout: {result_stdout}") await update_status(cfg, id, WorkflowFlags.DATA_LOADED, pool) await update_status(cfg, id, WorkflowFlags.DATA_ELABORATED, pool) diff --git a/src/utils/parsers/by_type/integrity_monitor_integrity_monitor.py b/src/utils/parsers/by_type/stazionetotale_messpunktepini.py similarity index 100% rename from src/utils/parsers/by_type/integrity_monitor_integrity_monitor.py rename to src/utils/parsers/by_type/stazionetotale_messpunktepini.py