database documentation updated
This commit is contained in:
@@ -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:
|
||||
|
Reference in New Issue
Block a user