from flask import Flask, request, jsonify from datetime import datetime import sqlite3 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", "input2": "off", "input3": "off", "input4": "off", "relay1": "off", "relay2": "off", "relay3": "off", "relay4": "off" } @app.route('/log', methods=['POST']) def log(): data = request.json if not data: return jsonify({"error": "No data provided"}), 400 # Extract data from the JSON payload hostname = data.get('hostname', 'unknown') ip_address = data.get('ip_address', 'unknown') message = data.get('message', '') print(f"Board hostname: {hostname}") print(f"Board IP address: {ip_address}") print(f"Message: {message}") # Record the log in the database 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}']}") conn.close() return jsonify({"message": "Log received"}), 200 @app.route('/control', methods=['POST']) def control(): data = request.json if not data: return jsonify({"error": "No data provided"}), 400 # Control the relays based on the received data 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)