main app started

This commit is contained in:
2025-03-17 10:09:47 +02:00
commit 7c2ad1a45b
6 changed files with 124 additions and 0 deletions

Binary file not shown.

68
app.py Normal file
View File

@@ -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)

45
app_schema.txt Normal file
View File

@@ -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.

Binary file not shown.

11
instances/db.py Normal file
View File

@@ -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()

BIN
logs.db Normal file

Binary file not shown.