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

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)