database documentation updated

This commit is contained in:
Bram Barbieri
2024-03-22 12:28:23 +01:00
parent 61c8d09760
commit 11f9f53f6b

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. 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. The data collected from the websocket is json data, so this has to be changed.
```js ```js
cursor = mydb.cursor() //Making a variable for the database connection
// the segments in wich the infromation will be sent. cursor = mydb.cursor()
query = "INSERT INTO `Measurement` (NodeID, Type, Value) VALUES (%s, %s, %s)" //Making a variable for the database connection
// the json data collected from the websocket gets collected and transformed into data usable by python. MACDataReading = mydb.cursor()
mrdata = json.loads(data) //find the correct section and interact with it.
//variables get the different types of data. MACDataReading.execute("SELECT MAC FROM Node")
mrtemp = (mrdata['Temp']) //the query for what needs to be inserted into the database ( atleast where it has to go).
mrhumi = (mrdata['Humi']) query = "INSERT INTO `Measurement` (NodeID, Type, Value) VALUES (%s, %s, %s)"
mrco = (mrdata['eCO2']) //the recieved data from the websocket is json data so needs to be changed.
mrtvoc = (mrdata['TVOC']) processedData = json.loads(data)
mrnode = (mrdata['node']) //variables about the recieved data points.
//an array is made to holster the different information clusters, for the time being it holds the infromation of one node. processedTemp = (processedData['Temp'])
mrarray = [(1, "Temp", mrtemp), (1, "Humi", mrhumi), (1, "eCO2", mrco), (1, "TVOC", mrtvoc)] processedHumi = (processedData['Humi'])
// a for statement to go trough the array. processedECo = (processedData['eCO2'])
for i in mrarray: processedTvoc = (processedData['TVOC'])
print(query ,i) processedMAC = (processedData['node'])
cursor.execute(query, i) //change the recieved macadress to a tuple.
//commit the information to the database. MACTuple = (processedMAC,)
mydb.commit() //fetch the data from the database an[d put it in an array.
// if the connection to the database can't be made this error wil show with further information. 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: except mysql.connector.Error as err:
print("MySQL Error:", err) print("MySQL Error:", err)
finally: finally: