database connection documentation update
This commit is contained in:
@@ -100,13 +100,17 @@ Sadly this version of the code is only able to push the data from the one node b
|
|||||||
print(query ,i)
|
print(query ,i)
|
||||||
cursor.execute(query, i)
|
cursor.execute(query, i)
|
||||||
mydb.commit()
|
mydb.commit()
|
||||||
|
#in the case of an error, show what and where, and after, close the database connection.
|
||||||
except mysql.connector.Error as err:
|
except mysql.connector.Error as err:
|
||||||
print("MySQL Error:", err)
|
print("MySQL Error:", err)
|
||||||
finally:
|
finally:
|
||||||
cursor.close()
|
cursor.close()
|
||||||
mydb.close()
|
mydb.close()
|
||||||
|
```
|
||||||
|
In the next function, the connection is established with the WebSocket and collects the data sent by the nodes. This data is then stored in a variable named "data". (This data is the same data that was being processed to make it readable for Python and was split up in differnt types.)
|
||||||
|
|
||||||
|
This function also verifies if the WebSocket connection can be established and provides an error message when this is not the case.
|
||||||
|
```py
|
||||||
async def receive_data():
|
async def receive_data():
|
||||||
uri = "ws://145.92.8.114/ws"
|
uri = "ws://145.92.8.114/ws"
|
||||||
|
|
||||||
@@ -118,12 +122,15 @@ async def receive_data():
|
|||||||
await process_data(data)
|
await process_data(data)
|
||||||
except websockets.ConnectionClosedError as e:
|
except websockets.ConnectionClosedError as e:
|
||||||
print("WebSocket connection closed:", e)
|
print("WebSocket connection closed:", e)
|
||||||
|
```
|
||||||
|
This is one of the last functions where the file is instructed to wait for a WebSocket connection before executing the code. This is done to prevent false data from entering the database.
|
||||||
|
```py
|
||||||
async def main():
|
async def main():
|
||||||
await receive_data()
|
await receive_data()
|
||||||
|
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
```
|
```
|
||||||
|
As a summary, this code is meant to establish connections both to the database and the WebSocket to enable a data connection between them. When new data arrives, it will be pushed to the database, and if a new MAC address is encountered, it will be added to the list of addresses.
|
||||||
|
|
||||||
# New Version (by sietse)
|
# New Version (by sietse)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user