commit 7c2ad1a45b7982a9941d21115ffa8f63c4b5bc53 Author: ske087 Date: Mon Mar 17 10:09:47 2025 +0200 main app started diff --git a/__pycache__/db.cpython-311.pyc b/__pycache__/db.cpython-311.pyc new file mode 100644 index 0000000..3ddf1e6 Binary files /dev/null and b/__pycache__/db.cpython-311.pyc differ diff --git a/app.py b/app.py new file mode 100644 index 0000000..c612c21 --- /dev/null +++ b/app.py @@ -0,0 +1,68 @@ +from flask import Flask, request, jsonify +from datetime import datetime +import sqlite3 +from instances.db import init_db + +app = Flask(__name__) + +# Variable to store the status of inputs and outputs +status = { + "input1": "off", + "relay1": "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}") + + # 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') + c = conn.cursor() + c.execute("INSERT INTO logs (timestamp, hostname, ip_address, message) VALUES (?, ?, ?, ?)", + (datetime.now().isoformat(), hostname, ip_address, message)) + conn.commit() + + # 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']) +def control(): + data = request.json + if not data: + 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']}") + + return jsonify({"message": "Control command received"}), 200 + +if __name__ == '__main__': + init_db() + app.run(host='0.0.0.0', port=80) \ No newline at end of file diff --git a/app_schema.txt b/app_schema.txt new file mode 100644 index 0000000..24e4822 --- /dev/null +++ b/app_schema.txt @@ -0,0 +1,45 @@ +# Database Schema + +## Table: logs +- timestamp: TEXT +- hostname: TEXT +- ip_address: TEXT +- message: TEXT + +## Table: boards +- hostname: TEXT PRIMARY KEY +- ip_address: TEXT +- input1: TEXT +- input2: TEXT +- input3: TEXT +- input4: TEXT +- relay1: TEXT +- relay2: TEXT +- relay3: TEXT +- relay4: TEXT + +# Explanation of Functions in app.py + +## Function: init_db +- Initializes the SQLite database. +- Creates two tables: logs and boards. +- The logs table stores log entries with timestamp, hostname, IP address, and message. +- The boards table stores the status of each board with hostname, IP address, and status of inputs and relays. + +## Function: log +- Endpoint: /log +- Method: POST +- Receives log messages from the ESP board. +- Extracts hostname, IP address, and message from the JSON payload. +- Prints the hostname, IP address, and message. +- Updates the status of inputs and relays based on the message. +- Records the log in the logs table. +- Updates the board status in the boards table. + +## Function: control +- Endpoint: /control +- Method: POST +- Receives control commands to update the status of relays. +- Extracts the relay status from the JSON payload. +- Prints the new relay status. +- Updates the status of the relays. \ No newline at end of file diff --git a/instances/__pycache__/db.cpython-311.pyc b/instances/__pycache__/db.cpython-311.pyc new file mode 100644 index 0000000..932f3dc Binary files /dev/null and b/instances/__pycache__/db.cpython-311.pyc differ diff --git a/instances/db.py b/instances/db.py new file mode 100644 index 0000000..b1b8311 --- /dev/null +++ b/instances/db.py @@ -0,0 +1,11 @@ +import sqlite3 + +def init_db(): + conn = sqlite3.connect('logs.db') + c = conn.cursor() + c.execute('''CREATE TABLE IF NOT EXISTS logs + (timestamp TEXT, hostname TEXT, ip_address TEXT, message TEXT)''') + c.execute('''CREATE TABLE IF NOT EXISTS boards + (hostname TEXT PRIMARY KEY, ip_address TEXT, input1 TEXT, input2 TEXT, input3 TEXT, input4 TEXT, relay1 TEXT, relay2 TEXT, relay3 TEXT, relay4 TEXT)''') + conn.commit() + conn.close() \ No newline at end of file diff --git a/logs.db b/logs.db new file mode 100644 index 0000000..2c6aec5 Binary files /dev/null and b/logs.db differ