websocket, database, file made.
This commit is contained in:
97
server/web-data-connection/data.py
Normal file
97
server/web-data-connection/data.py
Normal file
@@ -0,0 +1,97 @@
|
||||
import asyncio
|
||||
import websockets
|
||||
import json
|
||||
from class_SensorNode import SensorNode
|
||||
from class_enqueteNode import EnqueteNode
|
||||
from classes_data import dbLogin
|
||||
|
||||
sensorNodeArray = []
|
||||
enqueteNodeArray = []
|
||||
|
||||
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']
|
||||
|
||||
if "Temp" in processedData:
|
||||
type = 'sensor'
|
||||
else:
|
||||
type = 'enquete'
|
||||
|
||||
await getNodeInfo('sensor')
|
||||
await getNodeInfo('enquete')
|
||||
|
||||
|
||||
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)
|
||||
except websockets.ConnectionClosedError as e:
|
||||
print("WebSocket connection closed:", e)
|
||||
|
||||
async def main():
|
||||
await receive_data()
|
||||
|
||||
async def getNodeInfo(type):
|
||||
print("getNodeINfo")
|
||||
global sensorNodeArray
|
||||
global enqueteNodeArray
|
||||
|
||||
nodeInfoArray = []
|
||||
|
||||
id = (type,)
|
||||
mydb = dbLogin()
|
||||
cursor = mydb.cursor()
|
||||
cursor.execute("""SELECT MAC FROM Node WHERE Type = %s""", id)
|
||||
nodeInfo = cursor.fetchall()
|
||||
|
||||
for tuples in nodeInfo:
|
||||
for item in tuples:
|
||||
nodeInfoArray.append(item)
|
||||
|
||||
cursor.close()
|
||||
mydb.close()
|
||||
|
||||
if type == 'sensor':
|
||||
sensorNodeArray = nodeInfoArray
|
||||
return sensorNodeArray
|
||||
|
||||
elif type == 'enquete':
|
||||
enqueteNodeArray = nodeInfoArray
|
||||
return enqueteNodeArray
|
||||
|
||||
|
||||
async def getNodeID(macAdress):
|
||||
id = (macAdress,)
|
||||
mydb = dbLogin()
|
||||
cursor = mydb.cursor()
|
||||
cursor.execute("""SELECT nodeID FROM Node WHERE MAC = %s""", id)
|
||||
data = cursor.fetchall()
|
||||
|
||||
for tuples in data:
|
||||
for item in tuples:
|
||||
nodeID = item
|
||||
|
||||
return nodeID
|
||||
|
||||
async def newNode(mac, type):
|
||||
id = (mac, type)
|
||||
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())
|
26
server/web-data-connection/databaseGeneralClass.py
Normal file
26
server/web-data-connection/databaseGeneralClass.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import mysql.connector
|
||||
|
||||
def dbLogin():
|
||||
mydb = mysql.connector.connect(
|
||||
host="localhost",
|
||||
user="root",
|
||||
password="Dingleberries69!",
|
||||
database="NodeData"
|
||||
)
|
||||
return mydb
|
||||
|
||||
class Node():
|
||||
def __init__(self, macAdress):
|
||||
self.macAdress = macAdress
|
||||
self.id = None
|
||||
|
||||
def getNodeId(self):
|
||||
id = (self.macAdress,)
|
||||
mydb = dbLogin()
|
||||
cursor = mydb.cursor()
|
||||
cursor.execute("""SELECT nodeID FROM Node WHERE MAC = %s""", id)
|
||||
data = cursor.fetchall()
|
||||
|
||||
for tuples in data:
|
||||
for item in tuples:
|
||||
self.id = item
|
35
server/web-data-connection/enqueteNodeClass.py
Normal file
35
server/web-data-connection/enqueteNodeClass.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import mysql.connector
|
||||
import json
|
||||
|
||||
from classes_data import Node
|
||||
from classes_data import dbLogin
|
||||
|
||||
class EnqueteNode(Node):
|
||||
query = "INSERT INTO `Reply` (Result, Node_NodeID, Question_QuestionID) VALUES (%s, %s, %s)"
|
||||
|
||||
def __init__(self, macAdress, response, questionID):
|
||||
super().__init__(macAdress)
|
||||
self.response = response
|
||||
self.questionID = questionID
|
||||
|
||||
async def processEnqueteNodeData(data, nodeID):
|
||||
try:
|
||||
mydb = dbLogin()
|
||||
cursor = mydb.cursor()
|
||||
|
||||
processedData = json.loads(data)
|
||||
|
||||
EnqueteNode.questionID = (processedData['QuestionID'])
|
||||
EnqueteNode.response = (processedData['Response'])
|
||||
|
||||
pushingDataArray = [(EnqueteNode.questionID, nodeID, EnqueteNode.response)]
|
||||
|
||||
for i in pushingDataArray:
|
||||
print(EnqueteNode.query, i)
|
||||
cursor.execute(EnqueteNode.query, i)
|
||||
mydb.commit()
|
||||
except mysql.connector.Error as err:
|
||||
print("MySQL Error:", err)
|
||||
finally:
|
||||
cursor.close()
|
||||
mydb.close()
|
Reference in New Issue
Block a user