Merge branch 'main' of ssh://gitlab.fdmci.hva.nl/propedeuse-hbo-ict/onderwijs/2023-2024/out-a-se-ti/blok-3/qaajeeqiinii59
This commit is contained in:
@@ -43,7 +43,7 @@ async def receive_data():
|
|||||||
await getNodeInfo('sensor')
|
await getNodeInfo('sensor')
|
||||||
await getNodeInfo('enquete')
|
await getNodeInfo('enquete')
|
||||||
|
|
||||||
#get the node id and use it in functions seperate from this file.
|
#Get the node id and use it in functions seperate from this file.
|
||||||
if macAdress in sensorNodeArray:
|
if macAdress in sensorNodeArray:
|
||||||
nodeID = await getNodeID(macAdress)
|
nodeID = await getNodeID(macAdress)
|
||||||
await SensorNode.processSensorNodeData(data, nodeID)
|
await SensorNode.processSensorNodeData(data, nodeID)
|
||||||
@@ -52,23 +52,23 @@ async def receive_data():
|
|||||||
await EnqueteNode.processEnqueteNodeData(data, nodeID)
|
await EnqueteNode.processEnqueteNodeData(data, nodeID)
|
||||||
else:
|
else:
|
||||||
await newNode(macAdress, type)
|
await newNode(macAdress, type)
|
||||||
# error message if smth went wrong
|
# Error message if smth went wrong
|
||||||
except websockets.ConnectionClosedError as e:
|
except websockets.ConnectionClosedError as e:
|
||||||
print("WebSocket connection closed:", e)
|
print("WebSocket connection closed:", e)
|
||||||
|
|
||||||
#wait for data to come in.
|
#Wait for data to come in.
|
||||||
async def main():
|
async def main():
|
||||||
await receive_data()
|
await receive_data()
|
||||||
```
|
```
|
||||||
Here we have a case of python's scoping, it couldn't read the variables correctly and by making them global the variables were now available for all functions.
|
Here we have a case of python's scoping, it couldn't read the variables correctly and by making them global the variables were now available for all functions.
|
||||||
```py
|
```py
|
||||||
#by python's scuffed we had to use global variables.
|
#By python's scuffed we had to use global variables.
|
||||||
async def getNodeInfo(type):
|
async def getNodeInfo(type):
|
||||||
print("getNodeINfo")
|
print("getNodeINfo")
|
||||||
global sensorNodeArray
|
global sensorNodeArray
|
||||||
global enqueteNodeArray
|
global enqueteNodeArray
|
||||||
|
|
||||||
#new array which is needed.
|
#New array which is needed.
|
||||||
nodeInfoArray = []
|
nodeInfoArray = []
|
||||||
|
|
||||||
id = (type,)
|
id = (type,)
|
||||||
@@ -77,7 +77,7 @@ async def getNodeInfo(type):
|
|||||||
cursor.execute("""SELECT MAC FROM Node WHERE Type = %s""", id)
|
cursor.execute("""SELECT MAC FROM Node WHERE Type = %s""", id)
|
||||||
nodeInfo = cursor.fetchall()
|
nodeInfo = cursor.fetchall()
|
||||||
|
|
||||||
#go along each tuple in nodeinfo and each item in tuple, append(item)
|
#Go along each tuple in nodeinfo and each item in tuple, append(item)
|
||||||
for tuples in nodeInfo:
|
for tuples in nodeInfo:
|
||||||
for item in tuples:
|
for item in tuples:
|
||||||
nodeInfoArray.append(item)
|
nodeInfoArray.append(item)
|
||||||
@@ -85,12 +85,12 @@ async def getNodeInfo(type):
|
|||||||
cursor.close()
|
cursor.close()
|
||||||
mydb.close()
|
mydb.close()
|
||||||
|
|
||||||
#if the type is a sensor do this,
|
#If the type is a sensor do this,
|
||||||
if type == 'sensor':
|
if type == 'sensor':
|
||||||
sensorNodeArray = nodeInfoArray
|
sensorNodeArray = nodeInfoArray
|
||||||
return sensorNodeArray
|
return sensorNodeArray
|
||||||
|
|
||||||
#else, this if statement
|
#Else, this if statement
|
||||||
elif type == 'enquete':
|
elif type == 'enquete':
|
||||||
enqueteNodeArray = nodeInfoArray
|
enqueteNodeArray = nodeInfoArray
|
||||||
return enqueteNodeArray
|
return enqueteNodeArray
|
||||||
@@ -99,13 +99,13 @@ Here the database gets hinted to gain info of the existing nodes and find their
|
|||||||
```py
|
```py
|
||||||
async def getNodeID(macAdress):
|
async def getNodeID(macAdress):
|
||||||
id = (macAdress,)
|
id = (macAdress,)
|
||||||
#the db login is on a different page.
|
#The db login is on a different page.
|
||||||
mydb = dbLogin()
|
mydb = dbLogin()
|
||||||
cursor = mydb.cursor()
|
cursor = mydb.cursor()
|
||||||
cursor.execute("""SELECT nodeID FROM Node WHERE MAC = %s""", id)
|
cursor.execute("""SELECT nodeID FROM Node WHERE MAC = %s""", id)
|
||||||
data = cursor.fetchall()
|
data = cursor.fetchall()
|
||||||
|
|
||||||
#again, all tuples in data, all items for all tuples, nodeID
|
#Again, all tuples in data, all items for all tuples, nodeID
|
||||||
for tuples in data:
|
for tuples in data:
|
||||||
for item in tuples:
|
for item in tuples:
|
||||||
nodeID = item
|
nodeID = item
|
||||||
|
@@ -1,21 +1,27 @@
|
|||||||
## 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 prmairly is dedicated to the class which gets its data form the questionare and pushes it to the database.
|
||||||
|
This is done this way to make the code more visable and more clear.
|
||||||
```py
|
```py
|
||||||
|
#Importing different librarys.
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
import json
|
import json
|
||||||
|
#Importing different classes.
|
||||||
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.
|
||||||
|
The
|
||||||
|
```py
|
||||||
|
#Node is between brackets to show that this class is a child class from the parent class "Node"
|
||||||
class EnqueteNode(Node):
|
class EnqueteNode(Node):
|
||||||
query = "INSERT INTO `Reply` (Result, Node_NodeID, Question_QuestionID) VALUES (%s, %s, %s)"
|
#A private query to use later in a function.
|
||||||
|
__query = "INSERT INTO `Reply` (Result, Node_NodeID, Question_QuestionID) VALUES (%s, %s, %s)"
|
||||||
|
#use a super to get info from the parent class.
|
||||||
def __init__(self, macAdress, response, questionID):
|
def __init__(self, macAdress, response, questionID):
|
||||||
super().__init__(macAdress)
|
super().__init__(macAdress)
|
||||||
self.response = response
|
self.response = response
|
||||||
self.questionID = questionID
|
self.questionID = questionID
|
||||||
|
#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()
|
||||||
@@ -26,12 +32,15 @@ class EnqueteNode(Node):
|
|||||||
EnqueteNode.questionID = (processedData['QuestionID'])
|
EnqueteNode.questionID = (processedData['QuestionID'])
|
||||||
EnqueteNode.response = (processedData['Response'])
|
EnqueteNode.response = (processedData['Response'])
|
||||||
|
|
||||||
|
#An array with the data to push.
|
||||||
pushingDataArray = [(EnqueteNode.questionID, nodeID, EnqueteNode.response)]
|
pushingDataArray = [(EnqueteNode.questionID, nodeID, EnqueteNode.response)]
|
||||||
|
|
||||||
|
#Push the data according to the query to the database.
|
||||||
for i in pushingDataArray:
|
for i in pushingDataArray:
|
||||||
print(EnqueteNode.query, i)
|
print(EnqueteNode.__query, i)
|
||||||
cursor.execute(EnqueteNode.query, i)
|
cursor.execute(EnqueteNode.__query, i)
|
||||||
mydb.commit()
|
mydb.commit()
|
||||||
|
#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:
|
||||||
|
@@ -5,7 +5,8 @@ from classes_data import Node
|
|||||||
from classes_data import dbLogin
|
from classes_data import dbLogin
|
||||||
|
|
||||||
class EnqueteNode(Node):
|
class EnqueteNode(Node):
|
||||||
query = "INSERT INTO `Reply` (Result, Node_NodeID, Question_QuestionID) VALUES (%s, %s, %s)"
|
__query = "INSERT INTO `Reply` (Result, Node_NodeID, Question_QuestionID) VALUES (%s, %s, %s)"
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, macAdress, response, questionID):
|
def __init__(self, macAdress, response, questionID):
|
||||||
super().__init__(macAdress)
|
super().__init__(macAdress)
|
||||||
@@ -25,8 +26,8 @@ class EnqueteNode(Node):
|
|||||||
pushingDataArray = [(EnqueteNode.questionID, nodeID, EnqueteNode.response)]
|
pushingDataArray = [(EnqueteNode.questionID, nodeID, EnqueteNode.response)]
|
||||||
|
|
||||||
for i in pushingDataArray:
|
for i in pushingDataArray:
|
||||||
print(EnqueteNode.query, i)
|
print(EnqueteNode.__query, i)
|
||||||
cursor.execute(EnqueteNode.query, i)
|
cursor.execute(EnqueteNode.__query, i)
|
||||||
mydb.commit()
|
mydb.commit()
|
||||||
except mysql.connector.Error as err:
|
except mysql.connector.Error as err:
|
||||||
print("MySQL Error:", err)
|
print("MySQL Error:", err)
|
||||||
|
@@ -2,7 +2,7 @@
|
|||||||
The general node file includes the database connecting and a node connection class.
|
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.
|
This is done so the files don't go back and forth with the db connection, this otherwise would have caused problems.
|
||||||
```py
|
```py
|
||||||
#importing a database library to connect to the database.
|
#Importing a database library to connect to the database.
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
|
|
||||||
def dbLogin():
|
def dbLogin():
|
||||||
@@ -26,7 +26,7 @@ class Node():
|
|||||||
cursor = mydb.cursor()
|
cursor = mydb.cursor()
|
||||||
cursor.execute("""SELECT nodeID FROM Node WHERE MAC = %s""", id)
|
cursor.execute("""SELECT nodeID FROM Node WHERE MAC = %s""", id)
|
||||||
data = cursor.fetchall()
|
data = cursor.fetchall()
|
||||||
#again go along all tuple items. and asign them to the ID
|
#Again go along all tuple items. and asign them to the ID
|
||||||
for tuples in data:
|
for tuples in data:
|
||||||
for item in tuples:
|
for item in tuples:
|
||||||
self.id = item
|
self.id = item
|
||||||
|
@@ -5,7 +5,7 @@ from classes_data import Node
|
|||||||
from classes_data import dbLogin
|
from classes_data import dbLogin
|
||||||
|
|
||||||
class SensorNode(Node):
|
class SensorNode(Node):
|
||||||
query = "INSERT INTO `Measurement` (NodeID, Type, Value) VALUES (%s, %s, %s)"
|
__query = "INSERT INTO `Measurement` (NodeID, Type, Value) VALUES (%s, %s, %s)"
|
||||||
|
|
||||||
def __init__(self, macAdress, temp, humi, eCO2, TVOC):
|
def __init__(self, macAdress, temp, humi, eCO2, TVOC):
|
||||||
super().__init__(macAdress)
|
super().__init__(macAdress)
|
||||||
@@ -30,7 +30,7 @@ class SensorNode(Node):
|
|||||||
|
|
||||||
pushingDataArray = [(nodeID, "Temp", SensorNode.temperature), (nodeID, "Humi", SensorNode.humidity), (nodeID, "eCO2", SensorNode.eCO2), (nodeID, "TVOC", SensorNode.TVOC)]
|
pushingDataArray = [(nodeID, "Temp", SensorNode.temperature), (nodeID, "Humi", SensorNode.humidity), (nodeID, "eCO2", SensorNode.eCO2), (nodeID, "TVOC", SensorNode.TVOC)]
|
||||||
for i in pushingDataArray:
|
for i in pushingDataArray:
|
||||||
cursor.execute(SensorNode.query, i)
|
cursor.execute(SensorNode.__query, i)
|
||||||
mydb.commit()
|
mydb.commit()
|
||||||
|
|
||||||
|
|
||||||
|
51
server/web-data-connection/sensorNodeClassFile.md
Normal file
51
server/web-data-connection/sensorNodeClassFile.md
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
## Sensor Node class
|
||||||
|
This class is made to get the info of the sensor-nodes and send this to the database.
|
||||||
|
This is done this way to make the code more clear and readable.
|
||||||
|
|
||||||
|
```py
|
||||||
|
#Import library's
|
||||||
|
import mysql.connector
|
||||||
|
import json
|
||||||
|
#Import classes and functions from different files.
|
||||||
|
from classes_data import Node
|
||||||
|
from classes_data import dbLogin
|
||||||
|
#A class to send data to the database. child class of "Node"
|
||||||
|
class SensorNode(Node):
|
||||||
|
#A private query only to be used in this class.
|
||||||
|
__query = "INSERT INTO `Measurement` (NodeID, Type, Value) VALUES (%s, %s, %s)"
|
||||||
|
|
||||||
|
#All variables to be used.
|
||||||
|
def __init__(self, macAdress, temp, humi, eCO2, TVOC):
|
||||||
|
super().__init__(macAdress)
|
||||||
|
self.temperature = temp
|
||||||
|
self.humidity = humi
|
||||||
|
self.eCO2 = eCO2
|
||||||
|
self.TVOC = TVOC
|
||||||
|
|
||||||
|
#A function to connect to the database, grab the info which is given, and push this to the database.
|
||||||
|
async def processSensorNodeData(data, nodeID):
|
||||||
|
try:
|
||||||
|
mydb = dbLogin()
|
||||||
|
cursor = mydb.cursor()
|
||||||
|
|
||||||
|
processedData = json.loads(data)
|
||||||
|
#The variables to give to the database.
|
||||||
|
SensorNode.temperature = (processedData['Temp'])
|
||||||
|
SensorNode.humidity = (processedData['Humi'])
|
||||||
|
SensorNode.eCO2 = (processedData['eCO2'])
|
||||||
|
SensorNode.TVOC = (processedData['TVOC'])
|
||||||
|
|
||||||
|
#A array of the info to be given to the database in the correct format.
|
||||||
|
pushingDataArray = [(nodeID, "Temp", SensorNode.temperature), (nodeID, "Humi", SensorNode.humidity), (nodeID, "eCO2", SensorNode.eCO2), (nodeID, "TVOC", SensorNode.TVOC)]
|
||||||
|
#Go along all files of the array, and push it out to the database following the query.
|
||||||
|
for i in pushingDataArray:
|
||||||
|
cursor.execute(SensorNode.__query, i)
|
||||||
|
mydb.commit()
|
||||||
|
|
||||||
|
|
||||||
|
except mysql.connector.Error as err:
|
||||||
|
print("MySQL Error:", err)
|
||||||
|
finally:
|
||||||
|
cursor.close()
|
||||||
|
mydb.close()
|
||||||
|
```
|
BIN
server/web-data-connection/umlpython.png
Normal file
BIN
server/web-data-connection/umlpython.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
Reference in New Issue
Block a user