#!/usr/bin/env python3 import sys import os from mysql.connector import MySQLConnection, Error from dbconfig import read_db_config def insertData(dati): #print(dati) if dati != "null": query = "INSERT INTO ELABDATANESA(UnitName, ToolNameID, NodeNum, EventTimestamp, dataJSON) " \ "VALUES(%s,%s,%s,%s,%s)" try: db_config = read_db_config() conn = MySQLConnection(**db_config) cursor = conn.cursor() cursor.executemany(query, dati) conn.commit() except Error as e: print('Error:', e) finally: os.system("cd /usr/local/matlab_func/; ./run_Tilt_lnx.sh /usr/local/MATLAB/MATLAB_Runtime/v93/ "+dati[0][0]+" "+dati[0][1]+"") cursor.close() conn.close() def getDataFromCsv(pathFile): with open(pathFile, 'r') as file: data = file.readlines() data = [row.rstrip() for row in data] #data.pop(0) #rimuove header dati = [] i = 0 unit = "" tool = "" for row in data: row = row.split(",") if i == 0: serial_number = row[1] if serial_number[0] == "0": serial_number = serial_number[1:] #rimuove primo char (se 0) del id nesa query = "SELECT unit_name, tool_name FROM nesa_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]) date = row[7]+"-"+row[6]+"-"+row[5]+" "+row[2]+":"+row[3]+":"+row[4] nodeNum = 1 dataJSON = '{ "battery":"'+row[58]+'", "solarPanel":"'+row[61]+'", "tempAvg":"'+row[10]+'", "tempMin":"'+row[13]+'", "tempMax":"'+row[16]+'"}' dati.append((unit, tool, nodeNum, date, dataJSON)) nodeNum = 2 dataJSON = '{ "battery":"'+row[58]+'", "solarPanel":"'+row[61]+'", "humAvg":"'+row[19]+'", "humMin":"'+row[22]+'", "humMax":"'+row[25]+'"}' dati.append((unit, tool, nodeNum, date, dataJSON)) nodeNum = 3 dataJSON = '{ "battery":"'+row[58]+'", "solarPanel":"'+row[61]+'", "windDirAvg":"'+row[28]+'", "windDirMin":"'+row[31]+'", "windDirMax":"'+row[34]+'", "windSpeedAvg":"'+row[37]+'", "windSpeedMin":"'+row[40]+'", "windSpeedMax":"'+row[43]+'"}' dati.append((unit, tool, nodeNum, date, dataJSON)) nodeNum = 4 dataJSON = '{ "battery":"'+row[58]+'", "solarPanel":"'+row[61]+'", "rain":"'+row[46]+'"}' dati.append((unit, tool, nodeNum, date, dataJSON)) nodeNum = 5 dataJSON = '{ "battery":"'+row[58]+'", "solarPanel":"'+row[61]+'", "pressureAvg":"'+row[49]+'", "pressureMin":"'+row[52]+'", "pressureMax":"'+row[55]+'"}' dati.append((unit, tool, nodeNum, date, dataJSON)) i+=1 return dati def main(): insertData(getDataFromCsv(sys.argv[1])) if __name__ == '__main__': main()