enquete class documentation + a bit of the others
This commit is contained in:
@@ -1,6 +1,14 @@
|
|||||||
## Questionaire class
|
## Questionaire class
|
||||||
This file prmairly is dedicated to the class which gets its data form the questionare and pushes it to the database.
|
This File and class are dedicated to storing/using data that is related to the questionaire. This class is primairly used as a gateway for pushing data and loading data.
|
||||||
This is done this way to make the code more visable and more clear.
|
|
||||||
|
By doing this a lot of space is saved on the main file and the readability wil increase.
|
||||||
|
|
||||||
|
By doing this, it also solves the issues with the very precise naming and the often similar types of names.
|
||||||
|
This way it ensures no confusion on what the purpous of each segement is.
|
||||||
|
|
||||||
|
First up this page imports different types of information, like the library's and the needed files and/or Node.
|
||||||
|
|
||||||
|
|
||||||
```py
|
```py
|
||||||
#Importing different librarys.
|
#Importing different librarys.
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
@@ -9,8 +17,19 @@ import json
|
|||||||
from classes_data import Node
|
from classes_data import Node
|
||||||
from classes_data import dbLogin
|
from classes_data import dbLogin
|
||||||
```
|
```
|
||||||
Here a class is made to again provide more clear viewing and more reusability.
|
Here a Class is made as a child-class of the parent-class: "Node".
|
||||||
The
|
|
||||||
|
This clas first makes a private variable, this being: "__query".
|
||||||
|
This is done so no other outside sources can interfere with this query and potentially cause problems down the line.
|
||||||
|
|
||||||
|
After this the "__init__"function is called.
|
||||||
|
(In javascript this is known as the "constructor".
|
||||||
|
For further information visit https://www.w3schools.com/python/gloss_python_class_init.asp)
|
||||||
|
|
||||||
|
Because this class is a child class, we want to use some functionality from the parent class. That is where the "super" comes in. This makes it so the class uses the values and propperties of the parent class. (for more information visit https://www.w3schools.com/python/python_inheritance.asp)
|
||||||
|
|
||||||
|
The rest of the class contains a function in which the gatherd data
|
||||||
|
gets put into the database using the query and the gatherd data.
|
||||||
```py
|
```py
|
||||||
#Node is between brackets to show that this class is a child class from the parent class "Node"
|
#Node is between brackets to show that this class is a child class from the parent class "Node"
|
||||||
class EnqueteNode(Node):
|
class EnqueteNode(Node):
|
||||||
@@ -21,14 +40,25 @@ class EnqueteNode(Node):
|
|||||||
super().__init__(macAdress)
|
super().__init__(macAdress)
|
||||||
self.response = response
|
self.response = response
|
||||||
self.questionID = questionID
|
self.questionID = questionID
|
||||||
|
```
|
||||||
|
The following function is meant to make a database connection, get the data from the websocket (this data will only be from the questionaire-node)
|
||||||
|
and send the gotten data to the database.
|
||||||
|
|
||||||
|
The function starts with the database connection and calls uppon the websocket data.
|
||||||
|
It then creates variables with the data to put it in an array.
|
||||||
|
|
||||||
|
This array then gets sorted and pushed thogether with the query to correctly sort it and push it to the database.
|
||||||
|
|
||||||
|
In case of an error, it also asks for errors and prints it.
|
||||||
|
```py
|
||||||
#making a database connection to then load in the processed data.
|
#making a database connection to then load in the processed data.
|
||||||
async def processEnqueteNodeData(data, nodeID):
|
async def processEnqueteNodeData(data, nodeID):
|
||||||
try:
|
try:
|
||||||
mydb = dbLogin()
|
mydb = dbLogin()
|
||||||
cursor = mydb.cursor()
|
cursor = mydb.cursor()
|
||||||
|
#Getting the websocket data.
|
||||||
processedData = json.loads(data)
|
processedData = json.loads(data)
|
||||||
|
#Making variables of the different types of data.
|
||||||
EnqueteNode.questionID = (processedData['QuestionID'])
|
EnqueteNode.questionID = (processedData['QuestionID'])
|
||||||
EnqueteNode.response = (processedData['Response'])
|
EnqueteNode.response = (processedData['Response'])
|
||||||
|
|
||||||
@@ -37,10 +67,9 @@ class EnqueteNode(Node):
|
|||||||
|
|
||||||
#Push the data according to the query to the database.
|
#Push the data according to the query to the database.
|
||||||
for i in pushingDataArray:
|
for i in pushingDataArray:
|
||||||
print(EnqueteNode.__query, i)
|
|
||||||
cursor.execute(EnqueteNode.__query, i)
|
cursor.execute(EnqueteNode.__query, i)
|
||||||
mydb.commit()
|
mydb.commit()
|
||||||
#print an error.
|
#print an error.
|
||||||
except mysql.connector.Error as err:
|
except mysql.connector.Error as err:
|
||||||
print("MySQL Error:", err)
|
print("MySQL Error:", err)
|
||||||
finally:
|
finally:
|
||||||
|
@@ -13,6 +13,11 @@ def dbLogin():
|
|||||||
database="NodeData"
|
database="NodeData"
|
||||||
)
|
)
|
||||||
return mydb
|
return mydb
|
||||||
|
```
|
||||||
|
The "__init__"function is called.
|
||||||
|
(In javascript this is known as the "constructor".
|
||||||
|
For further information visit https://www.w3schools.com/python/gloss_python_class_init.asp)
|
||||||
|
```py
|
||||||
# A general class which acts as a database connector and a node identifyer
|
# A general class which acts as a database connector and a node identifyer
|
||||||
class Node():
|
class Node():
|
||||||
|
|
||||||
|
@@ -9,6 +9,11 @@ import json
|
|||||||
#Import classes and functions from different files.
|
#Import classes and functions from different files.
|
||||||
from classes_data import Node
|
from classes_data import Node
|
||||||
from classes_data import dbLogin
|
from classes_data import dbLogin
|
||||||
|
```
|
||||||
|
After this the "__init__"function is called.
|
||||||
|
(In javascript this is known as the "constructor".
|
||||||
|
For further information visit https://www.w3schools.com/python/gloss_python_class_init.asp)
|
||||||
|
```py
|
||||||
#A class to send data to the database. child class of "Node"
|
#A class to send data to the database. child class of "Node"
|
||||||
class SensorNode(Node):
|
class SensorNode(Node):
|
||||||
#A private query only to be used in this class.
|
#A private query only to be used in this class.
|
||||||
|
Reference in New Issue
Block a user