rename old_script -> old_scripts

This commit is contained in:
2025-08-19 14:15:15 +02:00
parent c6d486d0bd
commit ea5cdac7c0
7 changed files with 1 additions and 1 deletions

2587
src/old_scripts/TS_PiniScript.py Executable file

File diff suppressed because one or more lines are too long

15
src/old_scripts/dbconfig.py Executable file
View File

@@ -0,0 +1,15 @@
from configparser import ConfigParser
def read_db_config(filename='../env/config.ini', section='mysql'):
parser = ConfigParser()
parser.read(filename)
db = {}
if parser.has_section(section):
items = parser.items(section)
for item in items:
db[item[0]] = item[1]
else:
raise Exception('{0} not found in the {1} file'.format(section, filename))
return db

View File

@@ -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()

View File

@@ -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()

306
src/old_scripts/sorotecPini.py Executable file
View File

@@ -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()

171
src/old_scripts/vulinkScript.py Executable file
View File

@@ -0,0 +1,171 @@
#!/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 json
def checkBatteryLevel(db_conn, db_cursor, unit, date_time, battery_perc):
print(date_time, battery_perc)
if(float(battery_perc) < 25):#sotto il 25%
query = "select unit_name, date_time from alarms where unit_name=%s and date_time < %s and type_id=2 order by date_time desc limit 1"
db_cursor.execute(query, [unit, date_time])
result = db_cursor.fetchall()
if(len(result) > 0):
alarm_date_time = result[0]["date_time"]#datetime not str
format1 = "%Y-%m-%d %H:%M"
dt1 = datetime.strptime(date_time, format1)
time_difference = abs(dt1 - alarm_date_time)
if time_difference.total_seconds() > 24 * 60 * 60:
print("The difference is above 24 hours. Creo allarme battery")
queryInsAlarm = "INSERT IGNORE INTO alarms(type_id, unit_name, date_time, battery_level, description, send_email, send_sms) VALUES(%s,%s,%s,%s,%s,%s,%s)"
db_cursor.execute(queryInsAlarm, [2, unit, date_time, battery_perc, "75%", 1, 0])
db_conn.commit()
else:
print("Creo allarme battery")
queryInsAlarm = "INSERT IGNORE INTO alarms(type_id, unit_name, date_time, battery_level, description, send_email, send_sms) VALUES(%s,%s,%s,%s,%s,%s,%s)"
db_cursor.execute(queryInsAlarm, [2, unit, date_time, battery_perc, "75%", 1, 0])
db_conn.commit()
def checkSogliePh(db_conn, db_cursor, unit, tool, node_num, date_time, ph_value, soglie_str):
soglie = json.loads(soglie_str)
soglia = next((item for item in soglie if item.get("type") == "PH Link"), None)
ph = soglia["data"]["ph"]
ph_uno = soglia["data"]["ph_uno"]
ph_due = soglia["data"]["ph_due"]
ph_tre = soglia["data"]["ph_tre"]
ph_uno_value = soglia["data"]["ph_uno_value"]
ph_due_value = soglia["data"]["ph_due_value"]
ph_tre_value = soglia["data"]["ph_tre_value"]
ph_uno_sms = soglia["data"]["ph_uno_sms"]
ph_due_sms = soglia["data"]["ph_due_sms"]
ph_tre_sms = soglia["data"]["ph_tre_sms"]
ph_uno_email = soglia["data"]["ph_uno_email"]
ph_due_email = soglia["data"]["ph_due_email"]
ph_tre_email = soglia["data"]["ph_tre_email"]
alert_uno = 0
alert_due = 0
alert_tre = 0
ph_value_prev = 0
#print(unit, tool, node_num, date_time)
query = "select XShift, EventDate, EventTime from ELABDATADISP where UnitName=%s and ToolNameID=%s and NodeNum=%s and concat(EventDate, ' ', EventTime) < %s order by concat(EventDate, ' ', EventTime) desc limit 1"
db_cursor.execute(query, [unit, tool, node_num, date_time])
resultPhPrev = db_cursor.fetchall()
if(len(resultPhPrev) > 0):
ph_value_prev = float(resultPhPrev[0]["XShift"])
#ph_value = random.uniform(7, 10)
print(tool, unit, node_num, date_time, ph_value)
#print(ph_value_prev, ph_value)
if(ph == 1):
if(ph_tre == 1 and ph_tre_value != '' and float(ph_value) > float(ph_tre_value)):
if(ph_value_prev <= float(ph_tre_value)):
alert_tre = 1
if(ph_due == 1 and ph_due_value != '' and float(ph_value) > float(ph_due_value)):
if(ph_value_prev <= float(ph_due_value)):
alert_due = 1
if(ph_uno == 1 and ph_uno_value != '' and float(ph_value) > float(ph_uno_value)):
if(ph_value_prev <= float(ph_uno_value)):
alert_uno = 1
#print(ph_value, ph, " livelli:", ph_uno, ph_due, ph_tre, " value:", ph_uno_value, ph_due_value, ph_tre_value, " sms:", ph_uno_sms, ph_due_sms, ph_tre_sms, " email:", ph_uno_email, ph_due_email, ph_tre_email)
if(alert_tre == 1):
print("level3",tool, unit, node_num, date_time, ph_value)
queryInsAlarm = "INSERT IGNORE INTO alarms(type_id, tool_name, unit_name, date_time, registered_value, node_num, alarm_level, description, send_email, send_sms) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
db_cursor.execute(queryInsAlarm, [3, tool, unit, date_time, ph_value, node_num, 3, "pH", ph_tre_email, ph_tre_sms])
db_conn.commit()
elif(alert_due == 1):
print("level2",tool, unit, node_num, date_time, ph_value)
queryInsAlarm = "INSERT IGNORE INTO alarms(type_id, tool_name, unit_name, date_time, registered_value, node_num, alarm_level, description, send_email, send_sms) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
db_cursor.execute(queryInsAlarm, [3, tool, unit, date_time, ph_value, node_num, 2, "pH", ph_due_email, ph_due_sms])
db_conn.commit()
elif(alert_uno == 1):
print("level1",tool, unit, node_num, date_time, ph_value)
queryInsAlarm = "INSERT IGNORE INTO alarms(type_id, tool_name, unit_name, date_time, registered_value, node_num, alarm_level, description, send_email, send_sms) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
db_cursor.execute(queryInsAlarm, [3, tool, unit, date_time, ph_value, node_num, 1, "pH", ph_uno_email, ph_uno_sms])
db_conn.commit()
def getDataFromCsv(pathFile):
try:
folder_path, file_with_extension = os.path.split(pathFile)
file_name, _ = os.path.splitext(file_with_extension)#toolname
serial_number = file_name.split("_")[0]
query = "SELECT unit_name, tool_name FROM vulink_tools WHERE serial_number=%s"
query_node_depth = "SELECT depth, t.soglie, n.num as node_num FROM ase_lar.nodes as n left join tools as t on n.tool_id=t.id left join units as u on u.id=t.unit_id where u.name=%s and t.name=%s and n.nodetype_id=2"
query_nodes = "SELECT t.soglie, n.num as node_num, n.nodetype_id FROM ase_lar.nodes as n left join tools as t on n.tool_id=t.id left join units as u on u.id=t.unit_id where u.name=%s and t.name=%s"
db_config = read_db_config()
conn = MySQLConnection(**db_config)
cursor = conn.cursor(dictionary=True)
cursor.execute(query, [serial_number])
result = cursor.fetchall()
unit = result[0]["unit_name"]
tool = result[0]["tool_name"]
cursor.execute(query_node_depth, [unit, tool])
resultNode = cursor.fetchall()
cursor.execute(query_nodes, [unit, tool])
resultAllNodes = cursor.fetchall()
#print(resultAllNodes)
node_num_piezo = next((item for item in resultAllNodes if item.get('nodetype_id') == 2), None)["node_num"]
node_num_baro = next((item for item in resultAllNodes if item.get('nodetype_id') == 3), None)["node_num"]
node_num_conductivity = next((item for item in resultAllNodes if item.get('nodetype_id') == 94), None)["node_num"]
node_num_ph = next((item for item in resultAllNodes if item.get('nodetype_id') == 97), None)["node_num"]
#print(node_num_piezo, node_num_baro, node_num_conductivity, node_num_ph)
# 2 piezo
# 3 baro
# 94 conductivity
# 97 ph
node_depth = float(resultNode[0]["depth"]) #node piezo depth
with open(pathFile, 'r', encoding='ISO-8859-1') as file:
data = file.readlines()
data = [row.rstrip() for row in data]
data.pop(0) #rimuove header
data.pop(0) #rimuove header
data.pop(0) #rimuove header
data.pop(0) #rimuove header
data.pop(0) #rimuove header
data.pop(0) #rimuove header
data.pop(0) #rimuove header
data.pop(0) #rimuove header
data.pop(0) #rimuove header
data.pop(0) #rimuove header
for row in data:
row = row.split(",")
date_time = datetime.strptime(row[1], '%Y/%m/%d %H:%M').strftime('%Y-%m-%d %H:%M')
date_time = date_time.split(" ")
date = date_time[0]
time = date_time[1]
temperature_unit = float(row[2])
battery_perc = float(row[3])
pressure_baro = float(row[4])*1000#(kPa) da fare *1000 per Pa in elab->pressure
conductivity = float(row[6])
ph = float(row[11])
temperature_piezo = float(row[14])
pressure = float(row[16])*1000
depth = (node_depth * -1) + float(row[17])#da sommare alla quota del nodo (quota del nodo fare *-1)
queryInsRaw = "INSERT IGNORE INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, Val0) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)"
queryInsElab = "INSERT IGNORE INTO ELABDATADISP(UnitName, ToolNameID, NodeNum, EventDate, EventTime, pressure) VALUES(%s,%s,%s,%s,%s,%s)"
cursor.execute(queryInsRaw, [unit, tool, node_num_baro, date, time, battery_perc, temperature_unit, pressure_baro])
cursor.execute(queryInsElab, [unit, tool, node_num_baro, date, time, pressure_baro])
conn.commit()
queryInsRaw = "INSERT IGNORE INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, Val0) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)"
queryInsElab = "INSERT IGNORE INTO ELABDATADISP(UnitName, ToolNameID, NodeNum, EventDate, EventTime, XShift) VALUES(%s,%s,%s,%s,%s,%s)"
cursor.execute(queryInsRaw, [unit, tool, node_num_conductivity, date, time, battery_perc, temperature_unit, conductivity])
cursor.execute(queryInsElab, [unit, tool, node_num_conductivity, date, time, conductivity])
conn.commit()
queryInsRaw = "INSERT IGNORE INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, Val0) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)"
queryInsElab = "INSERT IGNORE INTO ELABDATADISP(UnitName, ToolNameID, NodeNum, EventDate, EventTime, XShift) VALUES(%s,%s,%s,%s,%s,%s)"
cursor.execute(queryInsRaw, [unit, tool, node_num_ph, date, time, battery_perc, temperature_unit, ph])
cursor.execute(queryInsElab, [unit, tool, node_num_ph, date, time, ph])
conn.commit()
checkSogliePh(conn, cursor, unit, tool, node_num_ph, date_time[0]+" "+date_time[1], ph, resultNode[0]["soglie"])
queryInsRaw = "INSERT IGNORE INTO RAWDATACOR(UnitName, ToolNameID, NodeNum, EventDate, EventTime, BatLevel, Temperature, Val0, Val1, Val2) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
queryInsElab = "INSERT IGNORE INTO ELABDATADISP(UnitName, ToolNameID, NodeNum, EventDate, EventTime, T_node, water_level, pressure) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)"
cursor.execute(queryInsRaw, [unit, tool, node_num_piezo, date, time, battery_perc, temperature_unit, temperature_piezo, depth, pressure])
cursor.execute(queryInsElab, [unit, tool, node_num_piezo, date, time, temperature_piezo, depth, pressure])
conn.commit()
checkBatteryLevel(conn, cursor, unit, date_time[0]+" "+date_time[1], battery_perc)
except Error as e:
print('Error:', e)
def main():
getDataFromCsv(sys.argv[1])
if __name__ == '__main__':
main()