sett 123
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from flask import Flask, request, jsonify, render_template, redirect, url_for
|
||||
from flask import Flask, request, jsonify, render_template, redirect, url_for, flash
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from datetime import datetime, timedelta
|
||||
import os
|
||||
@@ -8,6 +8,7 @@ import requests
|
||||
app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///logs.db'
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
app.secret_key = 'supersecretkey'
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
class Log(db.Model):
|
||||
@@ -16,6 +17,8 @@ class Log(db.Model):
|
||||
ip_address = db.Column(db.String(100), nullable=False)
|
||||
message = db.Column(db.String(500), nullable=False)
|
||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
relay_status = db.Column(db.String(4), default='off,off,off,off') # Status of 4 relays
|
||||
input_status = db.Column(db.String(4), default='off,off,off,off') # Status of 4 inputs
|
||||
|
||||
# Create the database if it does not exist
|
||||
if not os.path.exists('instance/logs.db'):
|
||||
@@ -38,8 +41,20 @@ def log_message():
|
||||
hostname = data.get('hostname')
|
||||
ip_address = data.get('ip_address')
|
||||
message = data.get('message')
|
||||
relay_status = data.get('relay_status', 'off,off,off,off')
|
||||
input_status = data.get('input_status', 'off,off,off,off')
|
||||
|
||||
if hostname and ip_address and message:
|
||||
new_log = Log(hostname=hostname, ip_address=ip_address, message=message)
|
||||
# Parse the message to update relay status
|
||||
if "Relay" in message and "turned" in message:
|
||||
parts = message.split()
|
||||
relay_index = int(parts[1]) - 1
|
||||
action = parts[3].lower()
|
||||
relay_status_list = relay_status.split(',')
|
||||
relay_status_list[relay_index] = action
|
||||
relay_status = ','.join(relay_status_list)
|
||||
|
||||
new_log = Log(hostname=hostname, ip_address=ip_address, message=message, relay_status=relay_status, input_status=input_status)
|
||||
db.session.add(new_log)
|
||||
db.session.commit()
|
||||
return jsonify({'status': 'success', 'message': 'Log saved'}), 201
|
||||
@@ -80,30 +95,14 @@ def view_board(hostname):
|
||||
logs = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.desc()).limit(50).all()
|
||||
relay_status = ['off', 'off', 'off', 'off']
|
||||
input_status = ['off', 'off', 'off', 'off']
|
||||
relay_messages = ['', '', '', '']
|
||||
input_messages = ['', '', '', '']
|
||||
if logs:
|
||||
latest_log = logs[0]
|
||||
relay_status = latest_log.relay_status.split(',')
|
||||
input_status = latest_log.input_status.split(',')
|
||||
ip_address = logs[0].ip_address if logs else '' # Get the IP address from the logs
|
||||
last_update = logs[0].timestamp if logs else None # Get the last update time
|
||||
|
||||
# Determine the status of each relay and input based on the latest log messages
|
||||
for log in logs:
|
||||
for i in range(4):
|
||||
if f"Relay {i + 1} turned ON" in log.message:
|
||||
relay_status[i] = 'on'
|
||||
relay_messages[i] = log.message
|
||||
elif f"Relay {i + 1} turned OFF" in log.message:
|
||||
relay_status[i] = 'off'
|
||||
relay_messages[i] = log.message
|
||||
if f"Input {i + 1} pressed" in log.message:
|
||||
input_status[i] = 'on'
|
||||
input_messages[i] = log.message
|
||||
post_action_to_server(hostname, f"Input {i + 1} pressed")
|
||||
elif f"Input {i + 1} released" in log.message:
|
||||
input_status[i] = 'off'
|
||||
input_messages[i] = log.message
|
||||
post_action_to_server(hostname, f"Input {i + 1} released")
|
||||
|
||||
return render_template('board.html', hostname=hostname, ip_address=ip_address, logs=logs, relay_status=relay_status, input_status=input_status, relay_messages=relay_messages, input_messages=input_messages, last_update=last_update)
|
||||
return render_template('board.html', hostname=hostname, ip_address=ip_address, logs=logs, relay_status=relay_status, input_status=input_status, last_update=last_update)
|
||||
|
||||
@app.route('/delete_board/<hostname>', methods=['POST'])
|
||||
def delete_board(hostname):
|
||||
@@ -137,6 +136,20 @@ def control_relay(hostname, relay, action):
|
||||
else:
|
||||
return f"No logs found for hostname: {hostname}", 404
|
||||
|
||||
@app.route('/settings', methods=['GET'])
|
||||
def settings():
|
||||
return render_template('settings.html')
|
||||
|
||||
@app.route('/set_cleanup_time', methods=['POST'])
|
||||
def set_cleanup_time():
|
||||
cleanup_time = request.form.get('cleanup_time')
|
||||
if cleanup_time:
|
||||
app.config['CLEANUP_TIME'] = int(cleanup_time)
|
||||
flash('Cleanup time set successfully!', 'success')
|
||||
else:
|
||||
flash('Invalid cleanup time!', 'danger')
|
||||
return redirect(url_for('settings'))
|
||||
|
||||
def post_action_to_server(hostname, action):
|
||||
url = "http://your-server-url.com/action"
|
||||
payload = {
|
||||
@@ -154,7 +167,7 @@ def post_action_to_server(hostname, action):
|
||||
|
||||
def schedule_cleanup():
|
||||
with app.app_context():
|
||||
cutoff_date = datetime.utcnow() - timedelta(hours=24)
|
||||
cutoff_date = datetime.utcnow() - timedelta(hours=app.config.get('CLEANUP_TIME', 24))
|
||||
Log.query.filter(Log.timestamp < cutoff_date).delete()
|
||||
db.session.commit()
|
||||
threading.Timer(3600, schedule_cleanup).start()
|
||||
|
||||
Binary file not shown.
@@ -46,8 +46,21 @@
|
||||
<body>
|
||||
<div class="container mt-5">
|
||||
<h1 class="text-center mb-4">ESP Board Details - {{ hostname }}</h1>
|
||||
<div class="row">
|
||||
|
||||
<div class="row mb-4">
|
||||
<div class="col-md-12">
|
||||
<div class="card">
|
||||
<div class="card-body d-flex justify-content-between align-items-center">
|
||||
<div>
|
||||
<a href="{{ url_for('index') }}" class="btn btn-primary">Back to Main Page</a>
|
||||
</div>
|
||||
<div>
|
||||
<form action="{{ url_for('delete_board', hostname=hostname) }}" method="post" style="display:inline;">
|
||||
<button type="submit" class="btn btn-danger">Delete Board</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@@ -81,20 +94,20 @@
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="row mt-4">
|
||||
<div class="col-md-12">
|
||||
<div class="card mb-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title"> Next update in <span id="timer">5</span> seconds</h5>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-12">
|
||||
<h3>Logs</h3>
|
||||
<div class="card mb-4">
|
||||
<div class="card-body d-flex justify-content-between align-items-center">
|
||||
<div>
|
||||
<h5 class="card-title">Last Update</h5>
|
||||
<p class="card-text">{{ last_update }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<p class="card-text">Next update in <span id="timer">5</span> seconds</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<ul class="list-group" id="log-list">
|
||||
{% for log in logs %}
|
||||
<li class="list-group-item">
|
||||
|
||||
@@ -46,6 +46,9 @@
|
||||
<body>
|
||||
<div class="container mt-5">
|
||||
<h1 class="text-center mb-4">ESP Board Status</h1>
|
||||
<div class="text-right mb-4">
|
||||
<a href="{{ url_for('settings') }}" class="btn btn-primary">Settings</a>
|
||||
</div>
|
||||
<div class="row">
|
||||
{% for board in boards %}
|
||||
<div class="col-md-4 col-sm-6">
|
||||
|
||||
64
server_api/templates/settings.html
Normal file
64
server_api/templates/settings.html
Normal file
@@ -0,0 +1,64 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Settings</title>
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
||||
<style>
|
||||
body {
|
||||
background-color: #343a40;
|
||||
color: #f8f9fa;
|
||||
}
|
||||
.card {
|
||||
background-color: #495057;
|
||||
border: 1px solid #6c757d;
|
||||
border-radius: 0.25rem;
|
||||
box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
|
||||
}
|
||||
.card-title {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 500;
|
||||
color: #f8f9fa;
|
||||
}
|
||||
.card-text {
|
||||
font-size: 1rem;
|
||||
color: #ced4da;
|
||||
}
|
||||
.btn-primary {
|
||||
background-color: #007bff;
|
||||
border-color: #007bff;
|
||||
}
|
||||
.btn-primary:hover {
|
||||
background-color: #0056b3;
|
||||
border-color: #004085;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container mt-5">
|
||||
<h1 class="text-center mb-4">Settings</h1>
|
||||
<div class="card mb-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Set Cleanup Time</h5>
|
||||
<form action="{{ url_for('set_cleanup_time') }}" method="post">
|
||||
<div class="form-group">
|
||||
<label for="cleanup_time">Cleanup Time (in hours):</label>
|
||||
<input type="number" class="form-control" id="cleanup_time" name="cleanup_time" value="{{ cleanup_time }}" required>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Set Time</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card mb-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Run Cleanup Now</h5>
|
||||
<form action="{{ url_for('run_cleanup_now') }}" method="post">
|
||||
<button type="submit" class="btn btn-danger">Run Cleanup Now</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<a href="{{ url_for('index') }}" class="btn btn-secondary">Back to Main Page</a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user