3.4 KiB
Sensor Node class
This class is made with a intention to store/transfer the data collected from the several node's that are going to be placed around school.
This is done to reduce confusion on what every function is meant to do.
This file imports the needed files/classes to function. Aswell as the important librarys.I
#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
The class then (being a child class) creates a private query. This is done this way because no other files need this query so it is only logical to private this.
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)
The "init" here makes variables for all the data the node needs to collect and sends to the database.
After this a function to send the gotten data to the databse is made.
The function gets all the data, puts this in an array to then send it to the database according to the given query so it gets placed correctly.
#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
The function starts with getting the database connection with the "dblogin" function and gains the the data from the websocket in the variable "processedData".
This data is then split up into different types, these are then inserted into an array.
The newly made array then gets pulled appart and the query is then combined with each segment, thereby succesfully inserting the data into it and then pushing it to the database following its guidline.
In case of a database error, a function is made to show said error. This helps with identifying problems and potentially fixing it.
#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()