129 lines
4.1 KiB
Markdown
129 lines
4.1 KiB
Markdown
## The websocket -> database connection classes.
|
|
I have made several classes to make the database connection more clear and easyer to overlook.
|
|
In the file : "data.py" the primary connections are made to the websocket and the data recieved is split off to see which type of node came back.
|
|
|
|
These types can be the "sensorNode"(the nodes that are located around the school) and the "enqueteNode"(a questionaire node which also collects data.).
|
|
|
|
```py
|
|
#Importing all different files from all the nodes which are on different pages
|
|
import asyncio
|
|
import websockets
|
|
import json
|
|
from class_SensorNode import SensorNode
|
|
from class_enqueteNode import EnqueteNode
|
|
from classes_data import dbLogin
|
|
|
|
#Making global variables
|
|
sensorNodeArray = []
|
|
enqueteNodeArray = []
|
|
```
|
|
These variables need to be global to serve for several locations in the code.
|
|
|
|
Here the code makes connection with the websocket.
|
|
```py
|
|
#Connection making with the websocket
|
|
async def receive_data():
|
|
uri = "ws://145.92.8.114/ws"
|
|
try:
|
|
async with websockets.connect(uri) as websocket:
|
|
while True:
|
|
print("true")
|
|
data = await websocket.recv()
|
|
print(f"Received data: {data}")
|
|
|
|
processedData = json.loads(data)
|
|
macAdress = processedData['node']
|
|
|
|
#A function to see if the node is one of two types.
|
|
if "Temp" in processedData:
|
|
type = 'sensor'
|
|
else:
|
|
type = 'enquete'
|
|
|
|
await getNodeInfo('sensor')
|
|
await getNodeInfo('enquete')
|
|
|
|
#get the node id and use it in functions seperate from this file.
|
|
if macAdress in sensorNodeArray:
|
|
nodeID = await getNodeID(macAdress)
|
|
await SensorNode.processSensorNodeData(data, nodeID)
|
|
elif macAdress in enqueteNodeArray:
|
|
nodeID = await getNodeID(macAdress)
|
|
await EnqueteNode.processEnqueteNodeData(data, nodeID)
|
|
else:
|
|
await newNode(macAdress, type)
|
|
# error message if smth went wrong
|
|
except websockets.ConnectionClosedError as e:
|
|
print("WebSocket connection closed:", e)
|
|
|
|
#wait for data to come in.
|
|
async def main():
|
|
await receive_data()
|
|
```
|
|
Here we have a case of python's scoping, it couldn't read the variables correctly and by making them global the variables were now available for all functions.
|
|
```py
|
|
#by python's scuffed we had to use global variables.
|
|
async def getNodeInfo(type):
|
|
print("getNodeINfo")
|
|
global sensorNodeArray
|
|
global enqueteNodeArray
|
|
|
|
#new array which is needed.
|
|
nodeInfoArray = []
|
|
|
|
id = (type,)
|
|
mydb = dbLogin()
|
|
cursor = mydb.cursor()
|
|
cursor.execute("""SELECT MAC FROM Node WHERE Type = %s""", id)
|
|
nodeInfo = cursor.fetchall()
|
|
|
|
#go along each tuple in nodeinfo and each item in tuple, append(item)
|
|
for tuples in nodeInfo:
|
|
for item in tuples:
|
|
nodeInfoArray.append(item)
|
|
|
|
cursor.close()
|
|
mydb.close()
|
|
|
|
#if the type is a sensor do this,
|
|
if type == 'sensor':
|
|
sensorNodeArray = nodeInfoArray
|
|
return sensorNodeArray
|
|
|
|
#else, this if statement
|
|
elif type == 'enquete':
|
|
enqueteNodeArray = nodeInfoArray
|
|
return enqueteNodeArray
|
|
```
|
|
Here the database gets hinted to gain info of the existing nodes and find their macadress.
|
|
```py
|
|
async def getNodeID(macAdress):
|
|
id = (macAdress,)
|
|
#the db login is on a different page.
|
|
mydb = dbLogin()
|
|
cursor = mydb.cursor()
|
|
cursor.execute("""SELECT nodeID FROM Node WHERE MAC = %s""", id)
|
|
data = cursor.fetchall()
|
|
|
|
#again, all tuples in data, all items for all tuples, nodeID
|
|
for tuples in data:
|
|
for item in tuples:
|
|
nodeID = item
|
|
|
|
return nodeID
|
|
```
|
|
See if the node is existing, if not push it to the database.
|
|
```py
|
|
async def newNode(mac, type):
|
|
id = (mac, type)
|
|
#Same thing as before.
|
|
mydb = dbLogin()
|
|
|
|
cursor = mydb.cursor()
|
|
cursor.execute("INSERT INTO `Node` (MAC, Type) VALUES (%s, %s)", id)
|
|
print("new node assigned")
|
|
mydb.commit()
|
|
|
|
asyncio.run(main())
|
|
```
|