diff --git a/PC/FileToSql.py b/PC/FileToSql.py index ed36e617f761c359c5d091ab522009330fbc400a..a73adb47da83ece4b14fc8b33e921a9f7fad3992 100644 --- a/PC/FileToSql.py +++ b/PC/FileToSql.py @@ -30,26 +30,22 @@ def formatTopicCorrect(topic): except: return False - -log = open("logBDD.txt", "w") - -# ================ CONNECTION BDD ================== # -try: - db = MySQLdb.connect(host="localhost",#"pil-09.univlehavre.lan", - user="firediag", - passwd="firediag$!", - db="firediag"); -except Exception as ex: - log.write(str(datetime.now())+" ==> Exception Demarrage : "+str(type(ex))+" ("+str(ex.args)+")\n") - print(ex); - log.close() - sys.exit(1) - - -# ================ RECUPERATION TOPIC ================= # - -topics = [] -try: +def initBDD(): + try: + db = MySQLdb.connect(host="localhost",#"pil-09.univlehavre.lan", + user="firediag", + passwd="firediag$!", + db="firediag"); + return db + except Exception as ex: + log.write(str(datetime.now())+" ==> Exception Demarrage : "+str(type(ex))+" ("+str(ex.args)+")\n") + print(ex); + log.close() + sys.exit(1) + +def recuperationTopic(db): + topics = [] + #--- Recupere les topic des capteurs en base de donnees requete = "SELECT DISTINCT topic FROM capteur" cursor = db.cursor(); @@ -58,20 +54,12 @@ try: # On filtre les mauvais topics (qui ne peuvent pas etre split par /) if formatTopicCorrect(row[0]): topics.append(row[0]) -except Exception as ex: - log.write(str(datetime.now())+" ==> Exception Init : "+str(type(ex))+" ("+str(ex.args)+")\n") - print(ex); - log.close() - sys.exit(1) - -print("Topics : ", topics) + return topics -# ================ RECUPERATION ID ================= # +def recuperationId(db): + tabId = {} -tabId = {} - -try: #--- Recupere les id des capteurs en fonction des topics for topic in topics: requete = "SELECT DISTINCT id FROM capteur WHERE topic = '"+topic+"'" @@ -79,22 +67,39 @@ try: cursor.execute(requete) idTab = cursor.fetchall() tabId[topic] = int(idTab[0][0]) -except Exception as ex: - log.write(str(datetime.now())+" ==> Exception Init : "+str(type(ex))+" ("+str(ex.args)+")\n") - print(ex); - log.close() - sys.exit(1) -print("Id : ",tabId) + return tabId + +log = open("logBDD.txt", "w") +FOLDER = "./data/" +TRAITEES = "./traitees/" + +# ================ CONNECTION BDD ================== # +db = initBDD() + # ================ File ==> SQL ================= # while(True): - FOLDER = "./data/" - TRAITEES = "./traitees/" + topics = [] + tabId = {} + DIR = os.listdir(FOLDER) DIR.sort() + try : + # ================ RECUPERATION TOPIC ================= # + topics = recuperationTopic(db) + + # ================ RECUPERATION ID ================= # + tabId = recuperationId(db) + print("Id : ",tabId) + except: + log.write(str(datetime.now())+" ==> Exception Init : "+str(type(ex))+" ("+str(ex.args)+")\n") + print(ex); + db = initBDD() + continue + cursor = db.cursor(); #--- Parcours les fichiers, ajoute les valeurs en base et supprime le fichier @@ -129,7 +134,10 @@ while(True): sys.exit(0) except Exception as ex: - db.rollback(); + try: + db.rollback(); + except: + db = initBDD() log.write(str(datetime.now())+" ==> Exception Requete : "+str(type(ex))+" ("+str(ex.args)+")\n") print("rollback "); print(ex); diff --git a/PC/server.py b/PC/server.py index 89f00ad296074189de539c8c0ddb2de6d3aa3cc9..51613333bba53605e8c6b78ed15db86e2ae3e823 100644 --- a/PC/server.py +++ b/PC/server.py @@ -53,3 +53,4 @@ while True: except Exception as ex: print("Exception ", type(ex)) log.write(str(datetime.now())+" ==> Exception "+str(type(ex))+" ("+str(ex.args)+")\n") +