#!/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 import random 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 = random.uniform(8, 10) #print(ph_value) if(ph == 1): if(ph_tre == 1 and ph_tre_value != '' and float(ph_value) > float(ph_tre_value)): alert_tre = 1 if(ph_due == 1 and ph_due_value != '' and float(ph_value) > float(ph_due_value)): alert_due = 1 if(ph_uno == 1 and ph_uno_value != '' and float(ph_value) > 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) 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) 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) 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" 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() 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, 1, date, time, battery_perc, temperature_unit, pressure_baro]) cursor.execute(queryInsElab, [unit, tool, 1, 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, 2, date, time, battery_perc, temperature_unit, conductivity]) cursor.execute(queryInsElab, [unit, tool, 2, 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, 3, date, time, battery_perc, temperature_unit, ph]) cursor.execute(queryInsElab, [unit, tool, 3, date, time, ph]) conn.commit() checkSogliePh(conn, cursor, unit, tool, resultNode[0]["node_num"], 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, 4, date, time, battery_perc, temperature_unit, temperature_piezo, depth, pressure]) cursor.execute(queryInsElab, [unit, tool, 4, date, time, temperature_piezo, depth, pressure]) conn.commit() except Error as e: print('Error:', e) def main(): getDataFromCsv(sys.argv[1]) if __name__ == '__main__': main()