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:
Sietse Jonker
2024-03-22 12:35:23 +01:00
2 changed files with 51 additions and 21 deletions

View File

@@ -28,26 +28,42 @@ async def process_data(data):
Then after this we code in the infromation we want to put inside of the database.
The data collected from the websocket is json data, so this has to be changed.
```js
cursor = mydb.cursor()
// the segments in wich the infromation will be sent.
query = "INSERT INTO `Measurement` (NodeID, Type, Value) VALUES (%s, %s, %s)"
// the json data collected from the websocket gets collected and transformed into data usable by python.
mrdata = json.loads(data)
//variables get the different types of data.
mrtemp = (mrdata['Temp'])
mrhumi = (mrdata['Humi'])
mrco = (mrdata['eCO2'])
mrtvoc = (mrdata['TVOC'])
mrnode = (mrdata['node'])
//an array is made to holster the different information clusters, for the time being it holds the infromation of one node.
mrarray = [(1, "Temp", mrtemp), (1, "Humi", mrhumi), (1, "eCO2", mrco), (1, "TVOC", mrtvoc)]
// a for statement to go trough the array.
for i in mrarray:
print(query ,i)
cursor.execute(query, i)
//commit the information to the database.
mydb.commit()
// if the connection to the database can't be made this error wil show with further information.
//Making a variable for the database connection
cursor = mydb.cursor()
//Making a variable for the database connection
MACDataReading = mydb.cursor()
//find the correct section and interact with it.
MACDataReading.execute("SELECT MAC FROM Node")
//the query for what needs to be inserted into the database ( atleast where it has to go).
query = "INSERT INTO `Measurement` (NodeID, Type, Value) VALUES (%s, %s, %s)"
//the recieved data from the websocket is json data so needs to be changed.
processedData = json.loads(data)
//variables about the recieved data points.
processedTemp = (processedData['Temp'])
processedHumi = (processedData['Humi'])
processedECo = (processedData['eCO2'])
processedTvoc = (processedData['TVOC'])
processedMAC = (processedData['node'])
//change the recieved macadress to a tuple.
MACTuple = (processedMAC,)
//fetch the data from the database an[d put it in an array.
MACDataFetching = MACDataReading.fetchall()
MACArray = list(MACDataFetching)
//see if the fetched data is not in the gotten array.
//otehrwise insert it into the database directly.
if MACTuple not in MACArray:
addingNode = "INSERT INTO `Node` (MAC) VALUES (%s)"
cursor.execute(addingNode, MACTuple)
mydb.commit()
//the websocket data that needs to be sent to the database.
pushingDataArray = [(1, "Temp", processedTemp), (1, "Humi", processedHumi), (1, "eCO2", processedECo), (1, "TVOC", processedTvoc)]
// forloop, go allong the array.
for i in pushingDataArray:
print(query ,i)
cursor.execute(query, i)
mydb.commit()
// show me the error it there is one.
except mysql.connector.Error as err:
print("MySQL Error:", err)
finally:

View File

@@ -66,4 +66,18 @@ Sam, tft scherm, rest api ombouwen om data in node tabel te douwen
Dano: Adding comments for OOP Arduino
Bram: Python send node data to database
Sam: flask rerouten to use put recuest
Sietse: WireFrame
Sietse: WireFrame
21/03/2024
Sietse: fritsing node documentation
Bram: personal documetation.
Sam: Rest api improvements
Dano: prepering for retro and making docu for aruidno
22/03/2024
Bram: Fritsing, documentation for code
Sietse: Database
Dano: Docuemtation for arduino
Sam: