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:
Bram Barbieri
2024-03-21 16:23:39 +01:00
5 changed files with 119 additions and 33 deletions

View File

@@ -0,0 +1,71 @@
# To edit data in the database we wanna use PUT request.
### What is a put request?
To edit data in the database we wanna use PUT request. A PUT request is used to update an existing resource. If the resource does not exist, it will create a new one. The PUT request requires the client to send the entire updated resource, not just the changes. This means that the client must send all the data, even if only one field has changed.
A put request is a json its for example looks like this:
```json
{
"NodeID": 1,
"Location": "testlocation",
"Name": "testname",
}
```
### How to use a put request
We have been trying to use a PUT request with flask. But that didn't work because the reverse proxy would reform the request to a GET request. So we tried to let apache 2 run it with wsgi.
### What is wsgi?
WSGI stands for Web Server Gateway Interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request. WSGI is a Python standard, and it is implemented by most Python web frameworks.
We couldnt get it to work even though we worked trough all the errors apache 2 gave us.
We first had some paths misconfigured. Then we had Forbidden error because we didn't give permission for apache2 to use that path.
And now we ended on not found and we verified that all configuration is in the correct syntrax with ```apachectl configtest```
### How to use wsgi
To use wsgi you need to install mod_wsgi with the following command:
```bash
sudo apt-get install libapache2-mod-wsgi
```
Then you need to enable the module with the following command:
```bash
sudo a2enmod wsgi
```
Then you need to create a wsgi file in the same directory as your python file. The wsgi file should look like this:
```python
import sys
sys.path.insert(0, '/home/pi/webapp')
from mainflask import app as application
```
Then you need to configure your apache2 configuration file to use the wsgi file. The configuration file should look like this:
```apache
<VirtualHost *:80>
DocumentRoot /home/pi/www/html/
WSGIDaemonProcess webapp python-path=/home/pi/webapp
WSGIScriptAlias /flask /home/pi/webapp/main.wsgi
<Directory /home/pi/webapp>
WSGIProcessGroup webapp
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
# Logging
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
```
Then you need to restart apache2 with the following command:
```bash
sudo systemctl restart apache2
```

View File

@@ -10,42 +10,20 @@ def index():
MAC = request.args.get('MAC', default = None)
return getData(node, dataType, MAC)
@app.route('/putData', methods=['PUT'])
def putData():
node_id = request.json.get('node_id', None)
new_name = request.json.get('new_name', None)
new_location = request.json.get('new_location', None)
@app.route('/updateData')
def updateDataIndex():
node_id = request.args.get('node', None)
new_name = request.args.get('name', None)
new_location = request.args.get('location', None)
return updateData(node_id, new_name, new_location)
if node_id is None:
return jsonify({"error": "node_id is required"}), 400
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="Dingleberries69!",
database="NodeData"
)
cursor = mydb.cursor()
if new_name is not None:
cursor.execute("UPDATE Node SET Name = %s WHERE NodeID = %s", (new_name, node_id))
mydb.commit()
if new_location is not None:
cursor.execute("UPDATE Node SET Location = %s WHERE NodeID = %s", (new_location, node_id))
mydb.commit()
cursor.close()
mydb.close()
return jsonify({"message": "Node updated successfully"}), 200
def putData(node, name, location, MAC):
def updateData(node, name, location):
mydb = loginDB()
query = get_query(node, name, location, MAC)
query = update_query(node, name, location)
cursor = mydb.cursor(dictionary=True) # Enable dictionary output
cursor.execute(query)
mydb.commit()
result = cursor.fetchall() # Fetch the results
cursor.close()
mydb.close()

View File

@@ -10,3 +10,14 @@ def get_query(node, dataType, MAC):
else:
query = "SELECT * FROM `Measurement`"
return query
def update_query(node, name, location):
if node and name and location:
query = f"""
UPDATE Node
SET Name = '{name}', Location = '{location}'
WHERE NodeID = {node};
"""
return query

20
server/reverseproxy Normal file
View File

@@ -0,0 +1,20 @@
<VirtualHost *:80>
ProxyPreserveHost On
DocumentRoot /home/pi/www/html/
# Enable proxying WebSockets
ProxyPass /ws ws://localhost:8001/
ProxyPassReverse /ws ws://localhost:8001/
# Enable proxying HTTP
ProxyPass /http http://localhost:8080/
ProxyPass /putData http://localhost:5000/putData
ProxyPassReverse /putData http://localhost:5000/putData
ProxyPass /flask http://localhost:5000/
ProxyPassReverse /flask http://localhost:5000/
ProxyPreserveHost On
# Logging
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

View File

@@ -60,4 +60,10 @@ and im going to continue with working on the connection between the websocket an
Dano, oop arduino
Bram, nodes registereen als er een nieuwe binnenkomt
Sietse, onderdelen bestellen, verder user story 46: nodes beheren op website.
Sam, tft scherm, rest api ombouwen om data in node tabel te douwen
Sam, tft scherm, rest api ombouwen om data in node tabel te douwen
20/03/2024:
Dano: Adding comments for OOP Arduino
Bram: Python send node data to database
Sam: flask rerouten to use put recuest
Sietse: WireFrame