Merge branch 'main' of gitlab.fdmci.hva.nl:propedeuse-hbo-ict/onderwijs/2023-2024/out-a-se-ti/blok-3/qaajeeqiinii59

This commit is contained in:
Dano van den Bosch
2024-03-26 23:06:38 +01:00
3 changed files with 64 additions and 16 deletions

View File

@@ -0,0 +1,31 @@
import asyncio
import websockets
import json
import random
import time
async def send_data(uri):
async with websockets.connect(uri) as websocket:
print("Connected to WebSocket server")
while True:
data = {
"node": "69:42:08:F5:00:00",
"Response": str(round(random.uniform(0, 2))),
"QuestionID": str(round(random.uniform(0, 90))),
}
await websocket.send(json.dumps(data))
print("Data sent")
response = await websocket.recv()
print("Received message:", response)
await asyncio.sleep(2) # Wait a bit before sending the next message
# Start the WebSocket connection
while True:
try:
asyncio.get_event_loop().run_until_complete(send_data("ws://145.92.8.114/ws"))
except Exception as e:
print("Exception:", e)
time.sleep(1) # Wait a bit before trying to reconnect

View File

@@ -31,10 +31,33 @@ async def processSensorNodeData(data, nodeID):
finally:
cursor.close()
mydb.close()
async def processEnqueteNodeData(data, nodeID):
try:
mydb = dbLogin()
cursor = mydb.cursor()
query = "INSERT INTO `Response` (Node_NodeID, Question_QuestionID, Response) VALUES (%s, %s, %s)"
processedData = json.loads(data)
processedQuestionID = (processedData['QuestionID'])
processedResponse = (processedData['Response'])
pushingDataArray = [(nodeID, processedQuestionID, processedResponse)]
for i in pushingDataArray:
cursor.execute(query, i)
mydb.commit()
except mysql.connector.Error as err:
print("MySQL Error:", err)
finally:
cursor.close()
mydb.close()
async def receive_data():
uri = "ws://145.92.8.114/ws"
try:
async with websockets.connect(uri) as websocket:
while True:
@@ -44,13 +67,8 @@ async def receive_data():
processedData = json.loads(data)
macAdress = processedData['node']
if processedData["Temp"]:
type = 'sensor'
else:
type = 'enquete'
type = await getNodeType(macAdress)
await getNodeInfo('sensor')
await getNodeInfo('enquete')
@@ -67,15 +85,6 @@ async def receive_data():
except websockets.ConnectionClosedError as e:
print("WebSocket connection closed:", e)
async def processEnqueteNodeData(data):
mydb = dbLogin()
cursor = mydb.cursor()
query = "INSERT INTO `Response` (NodeID, QuestionID, Response) VALUES (%s, %s, %s)"
processedData = json.loads(data)
async def main():
await receive_data()
@@ -130,8 +139,16 @@ async def getNodeID(macAdress):
return nodeID
async def getNodeType(macAdress):
id = (macAdress,)
mydb = dbLogin()
cursor = mydb.cursor()
cursor.execute("""SELECT Type FROM Node WHERE MAC = %s""", id)
data = cursor.fetchall()
Type = data[0][0]
return Type
async def newNode(mac, type):
id = (mac, type,)