created utils py

This commit is contained in:
2025-03-18 12:20:06 +02:00
parent e1ea17f26f
commit 2290c8dbf8
8 changed files with 241 additions and 23 deletions

88
app.py
View File

@@ -1,14 +1,23 @@
from flask import Flask, request, jsonify
from datetime import datetime
import sqlite3
from instances.db import init_db
import requests
import os
from instances.db import init_db, DB_PATH
from mission_utils import delayed_post_mission, post_mission, get_robots, get_status
app = Flask(__name__)
# Variable to store the status of inputs and outputs
status = {
"input1": "off",
"relay1": "off"
"input2": "off",
"input3": "off",
"input4": "off",
"relay1": "off",
"relay2": "off",
"relay3": "off",
"relay4": "off"
}
@app.route('/log', methods=['POST'])
@@ -26,28 +35,65 @@ def log():
print(f"Board IP address: {ip_address}")
print(f"Message: {message}")
# Update the status of inputs and relays based on the message
if 'input1' in message:
status['input1'] = 'on' if 'input1 on' in message else 'off'
print(f"Input 1 is {status['input1']}")
if 'relay1' in message:
status['relay1'] = 'on' if 'relay1 on' in message else 'off'
print(f"Relay 1 status is {status['relay1']}")
# Record the log in the database
conn = sqlite3.connect('logs.db')
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("INSERT INTO logs (timestamp, hostname, ip_address, message) VALUES (?, ?, ?, ?)",
(datetime.now().isoformat(), hostname, ip_address, message))
conn.commit()
print(f"Log entry added: {hostname}, {ip_address}, {message}")
# Check if the board exists in the database
c.execute("SELECT * FROM boards WHERE ip_address = ?", (ip_address,))
board = c.fetchone()
if not board:
# Create a new board entry if it doesn't exist
c.execute('''INSERT INTO boards (hostname, ip_address, input1, input2, input3, input4, relay1, relay2, relay3, relay4)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''',
(hostname, ip_address, 'off', 'off', 'off', 'off', 'off', 'off', 'off', 'off'))
conn.commit()
print(f"New board entry created: {hostname}, {ip_address}")
# Update the status of inputs and relays based on the message
for i in range(1, 5):
if f"Input {i} pressed" in message:
status[f"input{i}"] = 'on'
print(f"Input {i} is {status[f'input{i}']}")
if i == 1:
print("Input 1 is on, triggering delayed_post_mission")
delayed_post_mission({"mission_id": "a1343edc-975c-11ef-87e6-0242ac120002"}) # Example mission ID
c.execute(f'''UPDATE boards SET input{i} = ? WHERE ip_address = ?''',
(status[f"input{i}"], ip_address))
conn.commit()
print(f"Board status updated: {hostname}, {ip_address}, input{i}={status[f'input{i}']}")
if f"Input {i} released" in message:
status[f"input{i}"] = 'off'
print(f"Input {i} is {status[f'input{i}']}")
c.execute(f'''UPDATE boards SET input{i} = ? WHERE ip_address = ?''',
(status[f"input{i}"], ip_address))
conn.commit()
print(f"Board status updated: {hostname}, {ip_address}, input{i}={status[f'input{i}']}")
for i in range(1, 5):
if f"Relay {i} turned ON" in message:
status[f"relay{i}"] = 'on'
print(f"Relay {i} status is {status[f'relay{i}']}")
c.execute(f'''UPDATE boards SET relay{i} = ? WHERE ip_address = ?''',
(status[f"relay{i}"], ip_address))
conn.commit()
print(f"Board status updated: {hostname}, {ip_address}, relay{i}={status[f'relay{i}']}")
if f"Relay {i} turned OFF" in message:
status[f"relay{i}"] = 'off'
print(f"Relay {i} status is {status[f'relay{i}']}")
c.execute(f'''UPDATE boards SET relay{i} = ? WHERE ip_address = ?''',
(status[f"relay{i}"], ip_address))
conn.commit()
print(f"Board status updated: {hostname}, {ip_address}, relay{i}={status[f'relay{i}']}")
# Update the board status in the database
c.execute('''INSERT OR REPLACE INTO boards (hostname, ip_address, input1, relay1)
VALUES (?, ?, ?, ?)''',
(hostname, ip_address, status['input1'], status['relay1']))
conn.commit()
conn.close()
return jsonify({"message": "Log received"}), 200
@app.route('/control', methods=['POST'])
@@ -57,12 +103,14 @@ def control():
return jsonify({"error": "No data provided"}), 400
# Control the relays based on the received data
if 'relay1' in data:
status['relay1'] = data['relay1']
print(f"Setting Relay 1 to {data['relay1']}")
for i in range(1, 5):
if f'relay{i}' in data:
status[f'relay{i}'] = data[f'relay{i}']
print(f"Setting Relay {i} to {data[f'relay{i}']}")
return jsonify({"message": "Control command received"}), 200
if __name__ == '__main__':
init_db()
get_robots()
app.run(host='0.0.0.0', port=80)