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()
|
||||
|
||||
Reference in New Issue
Block a user