Added so you can change node information

This commit is contained in:
2024-03-21 13:22:09 +01:00
parent 09522218f3
commit 54a2eef7c5
2 changed files with 21 additions and 32 deletions

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