From 5761e0a028abb17a0771296e3bcc2a6c0891bc2b Mon Sep 17 00:00:00 2001 From: Bram Barbieri Date: Tue, 2 Apr 2024 13:51:37 +0200 Subject: [PATCH] new documentations made. --- .../web-data-connection/enqueteClassFile.md | 40 +++++++++++++++++++ .../generalDatabaseFile.md | 33 +++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 server/web-data-connection/enqueteClassFile.md diff --git a/server/web-data-connection/enqueteClassFile.md b/server/web-data-connection/enqueteClassFile.md new file mode 100644 index 0000000..7c0f67c --- /dev/null +++ b/server/web-data-connection/enqueteClassFile.md @@ -0,0 +1,40 @@ +## Questionaire class +This file prmairly is dedicated to the class which gets its data form the questionare and pushes it to the database. + +```py +import mysql.connector +import json + +from classes_data import Node +from classes_data import dbLogin + +class EnqueteNode(Node): + query = "INSERT INTO `Reply` (Result, Node_NodeID, Question_QuestionID) VALUES (%s, %s, %s)" + + def __init__(self, macAdress, response, questionID): + super().__init__(macAdress) + self.response = response + self.questionID = questionID + + async def processEnqueteNodeData(data, nodeID): + try: + mydb = dbLogin() + cursor = mydb.cursor() + + processedData = json.loads(data) + + EnqueteNode.questionID = (processedData['QuestionID']) + EnqueteNode.response = (processedData['Response']) + + pushingDataArray = [(EnqueteNode.questionID, nodeID, EnqueteNode.response)] + + for i in pushingDataArray: + print(EnqueteNode.query, i) + cursor.execute(EnqueteNode.query, i) + mydb.commit() + except mysql.connector.Error as err: + print("MySQL Error:", err) + finally: + cursor.close() + mydb.close() +``` \ No newline at end of file diff --git a/server/web-data-connection/generalDatabaseFile.md b/server/web-data-connection/generalDatabaseFile.md index e69de29..81e7c49 100644 --- a/server/web-data-connection/generalDatabaseFile.md +++ b/server/web-data-connection/generalDatabaseFile.md @@ -0,0 +1,33 @@ +## General node file. +The general node file includes the database connecting and a node connection class. +This is done so the files don't go back and forth with the db connection, this otherwise would have caused problems. +```py +#importing a database library to connect to the database. +import mysql.connector + +def dbLogin(): + mydb = mysql.connector.connect( + host="localhost", + user="root", + password="**********", + database="NodeData" + ) + return mydb +# A general class which acts as a database connector and a node identifyer +class Node(): + + def __init__(self, macAdress): + self.macAdress = macAdress + self.id = None + + def getNodeId(self): + id = (self.macAdress,) + mydb = dbLogin() + cursor = mydb.cursor() + cursor.execute("""SELECT nodeID FROM Node WHERE MAC = %s""", id) + data = cursor.fetchall() + #again go along all tuple items. and asign them to the ID + for tuples in data: + for item in tuples: + self.id = item +``` \ No newline at end of file