From 93492db324f3ec08c1b2c505d4eeb750b4966279 Mon Sep 17 00:00:00 2001 From: Bram Barbieri Date: Tue, 2 Apr 2024 13:25:39 +0200 Subject: [PATCH 1/2] documentation updated. --- docs/rpi-documentation/Databaseconnection.md | 2 +- server/web-data-connection/datatransfer.md | 128 ++++++++++++++++++ .../generalDatabaseFile.md | 0 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 server/web-data-connection/datatransfer.md create mode 100644 server/web-data-connection/generalDatabaseFile.md diff --git a/docs/rpi-documentation/Databaseconnection.md b/docs/rpi-documentation/Databaseconnection.md index 8f6dd7f..86cf47b 100644 --- a/docs/rpi-documentation/Databaseconnection.md +++ b/docs/rpi-documentation/Databaseconnection.md @@ -237,7 +237,7 @@ async def getNodeID(macAdress): This function is alot like the original one, with the only 2 changes being that it now also commits the nodeID and that the code to make a new node is now in a different function. -[Link to code](server\data.py) +[link to code](../../server/web-data-connection/data.py) ## The function to commit the data from the enqueteNodes diff --git a/server/web-data-connection/datatransfer.md b/server/web-data-connection/datatransfer.md new file mode 100644 index 0000000..d1f9547 --- /dev/null +++ b/server/web-data-connection/datatransfer.md @@ -0,0 +1,128 @@ +## 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()) +``` diff --git a/server/web-data-connection/generalDatabaseFile.md b/server/web-data-connection/generalDatabaseFile.md new file mode 100644 index 0000000..e69de29 From 276ec2ed120c5657bd8ba41e41fd35bcac77aeb7 Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Tue, 2 Apr 2024 13:32:35 +0200 Subject: [PATCH 2/2] Add UML diagram and classes for infrastructure V2 --- docs/brainstorm/UML-infrastrucuteV2.md | 72 ++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 docs/brainstorm/UML-infrastrucuteV2.md diff --git a/docs/brainstorm/UML-infrastrucuteV2.md b/docs/brainstorm/UML-infrastrucuteV2.md new file mode 100644 index 0000000..059a1e5 --- /dev/null +++ b/docs/brainstorm/UML-infrastrucuteV2.md @@ -0,0 +1,72 @@ +```mermaid +classDiagram + setup --> websocketSetup + loop --> screenButtonHandler + screenButtonHandler --> DisplayText + screenButtonHandler --> sendData + sendData --> Server + setup --> loop + python --> Server + Server --> website + + +namespace ESP32Questionbox { + class setup { + +int questionID + +char*[] Question + +char*[] Answer + +Adafruit_ST7796S_kbv tft + +DisplayText displayText + +void websocketSetup() + } + + class loop { + +void screenButtonHandler() + +void sendData(int question, String answer) + +void hexdump(const void* mem, uint32_t len, uint8_t cols) + } + + class websocketSetup { + +connectWifi() + +websocketConnect() + } + + class screenButtonHandler { + -bool redButton + -bool greenButton + -bool whiteButton + +displayText.writeText() + +sendData() + } + class DisplayText { + +void writeText(char* text, int size, int posX, int posY, int screenTime, bool center, bool bottom) + -int centerText(char* text) + -void printWordsFull(char* text, bool bottom) + } + class sendData{ + +webSocket.sendTXT + } +} +namespace server { + class python { + +databaseScript() + +websocketScript() + +flaskScript() + } + class Server { + +websocket() + +flask() + +mariaDB() + } +} + +namespace user { + class website { + + getLiveData() + + getHistoricalData() + + showData() + + } +} + +```