adding boards and settings

This commit is contained in:
2025-03-11 13:41:49 +02:00
parent 0b61f87ac5
commit a439ba8fc4
4 changed files with 198 additions and 30 deletions

View File

@@ -17,8 +17,14 @@ class Log(db.Model):
ip_address = db.Column(db.String(100), nullable=False) ip_address = db.Column(db.String(100), nullable=False)
message = db.Column(db.String(500), nullable=False) message = db.Column(db.String(500), nullable=False)
timestamp = db.Column(db.DateTime, default=datetime.utcnow) timestamp = db.Column(db.DateTime, default=datetime.utcnow)
relay_status = db.Column(db.String(4), default='off,off,off,off') # Status of 4 relays relay1_status = db.Column(db.String(3), default='off')
input_status = db.Column(db.String(4), default='off,off,off,off') # Status of 4 inputs relay2_status = db.Column(db.String(3), default='off')
relay3_status = db.Column(db.String(3), default='off')
relay4_status = db.Column(db.String(3), default='off')
input1_status = db.Column(db.String(3), default='off')
input2_status = db.Column(db.String(3), default='off')
input3_status = db.Column(db.String(3), default='off')
input4_status = db.Column(db.String(3), default='off')
# Create the database if it does not exist # Create the database if it does not exist
if not os.path.exists('instance/logs.db'): if not os.path.exists('instance/logs.db'):
@@ -41,20 +47,54 @@ def log_message():
hostname = data.get('hostname') hostname = data.get('hostname')
ip_address = data.get('ip_address') ip_address = data.get('ip_address')
message = data.get('message') message = data.get('message')
relay_status = data.get('relay_status', 'off,off,off,off')
input_status = data.get('input_status', 'off,off,off,off') # Fetch the latest log for the hostname to get the current relay and input statuses
latest_log = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.desc()).first()
relay1_status = latest_log.relay1_status if latest_log else 'off'
relay2_status = latest_log.relay2_status if latest_log else 'off'
relay3_status = latest_log.relay3_status if latest_log else 'off'
relay4_status = latest_log.relay4_status if latest_log else 'off'
input1_status = latest_log.input1_status if latest_log else 'off'
input2_status = latest_log.input2_status if latest_log else 'off'
input3_status = latest_log.input3_status if latest_log else 'off'
input4_status = latest_log.input4_status if latest_log else 'off'
if hostname and ip_address and message: if hostname and ip_address and message:
# Parse the message to update relay status # Parse the message to update relay status
if "Relay" in message and "turned" in message: if "Relay" in message and "turned" in message:
parts = message.split() parts = message.split()
relay_index = int(parts[1]) - 1 relay_index = int(parts[1])
action = parts[3].lower() action = parts[3].lower()
relay_status_list = relay_status.split(',') if relay_index == 1:
relay_status_list[relay_index] = action relay1_status = action
relay_status = ','.join(relay_status_list) elif relay_index == 2:
relay2_status = action
elif relay_index == 3:
relay3_status = action
elif relay_index == 4:
relay4_status = action
new_log = Log(hostname=hostname, ip_address=ip_address, message=message, relay_status=relay_status, input_status=input_status) # Parse the message to update input status
if "Input" in message and ("pressed" in message or "released" in message):
parts = message.split()
input_index = int(parts[1])
action = 'on' if "pressed" in message else 'off'
if input_index == 1:
input1_status = action
elif input_index == 2:
input2_status = action
elif input_index == 3:
input3_status = action
elif input_index == 4:
input4_status = action
new_log = Log(
hostname=hostname, ip_address=ip_address, message=message,
relay1_status=relay1_status, relay2_status=relay2_status,
relay3_status=relay3_status, relay4_status=relay4_status,
input1_status=input1_status, input2_status=input2_status,
input3_status=input3_status, input4_status=input4_status
)
db.session.add(new_log) db.session.add(new_log)
db.session.commit() db.session.commit()
return jsonify({'status': 'success', 'message': 'Log saved'}), 201 return jsonify({'status': 'success', 'message': 'Log saved'}), 201
@@ -97,12 +137,26 @@ def view_board(hostname):
input_status = ['off', 'off', 'off', 'off'] input_status = ['off', 'off', 'off', 'off']
if logs: if logs:
latest_log = logs[0] latest_log = logs[0]
relay_status = latest_log.relay_status.split(',') relay_status = [
input_status = latest_log.input_status.split(',') latest_log.relay1_status,
latest_log.relay2_status,
latest_log.relay3_status,
latest_log.relay4_status
]
input_status = [
latest_log.input1_status,
latest_log.input2_status,
latest_log.input3_status,
latest_log.input4_status
]
ip_address = logs[0].ip_address if logs else '' # Get the IP address from the logs 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 last_update = logs[0].timestamp if logs else None # Get the last update time
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) # Add placeholders for MIR server status and last input triggered
mir_status = "Unknown"
last_input_triggered = "None"
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, mir_status=mir_status, last_input_triggered=last_input_triggered)
@app.route('/delete_board/<hostname>', methods=['POST']) @app.route('/delete_board/<hostname>', methods=['POST'])
def delete_board(hostname): def delete_board(hostname):
@@ -139,7 +193,21 @@ def control_relay(hostname, relay, action):
@app.route('/settings', methods=['GET']) @app.route('/settings', methods=['GET'])
def settings(): def settings():
cleanup_time = app.config.get('CLEANUP_TIME', 24) cleanup_time = app.config.get('CLEANUP_TIME', 24)
return render_template('settings.html', cleanup_time=cleanup_time) mir_ip = app.config.get('MIR_IP', '')
mir_user = app.config.get('MIR_USER', '')
mir_password = app.config.get('MIR_PASSWORD', '')
boards = Log.query.with_entities(Log.hostname).distinct().all()
board_settings = []
for board in boards:
latest_log = Log.query.filter_by(hostname=board.hostname).order_by(Log.timestamp.desc()).first()
board_settings.append({
'hostname': board.hostname,
'input1': latest_log.input1_status if latest_log else 'off',
'input2': latest_log.input2_status if latest_log else 'off',
'input3': latest_log.input3_status if latest_log else 'off',
'input4': latest_log.input4_status if latest_log else 'off'
})
return render_template('settings.html', cleanup_time=cleanup_time, mir_ip=mir_ip, mir_user=mir_user, mir_password=mir_password, boards=board_settings)
@app.route('/set_cleanup_time', methods=['POST']) @app.route('/set_cleanup_time', methods=['POST'])
def set_cleanup_time(): def set_cleanup_time():
@@ -151,6 +219,41 @@ def set_cleanup_time():
flash('Invalid cleanup time!', 'danger') flash('Invalid cleanup time!', 'danger')
return redirect(url_for('settings')) return redirect(url_for('settings'))
@app.route('/set_mir_server', methods=['POST'])
def set_mir_server():
mir_ip = request.form.get('mir_ip')
mir_user = request.form.get('mir_user')
mir_password = request.form.get('mir_password')
if mir_ip and mir_user and mir_password:
app.config['MIR_IP'] = mir_ip
app.config['MIR_USER'] = mir_user
app.config['MIR_PASSWORD'] = mir_password
flash('MIR server settings updated successfully!', 'success')
else:
flash('Invalid MIR server settings!', 'danger')
return redirect(url_for('settings'))
@app.route('/set_board_settings/<hostname>', methods=['POST'])
def set_board_settings(hostname):
input1 = request.form.get('input1')
input2 = request.form.get('input2')
input3 = request.form.get('input3')
input4 = request.form.get('input4')
if input1 and input2 and input3 and input4:
latest_log = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.desc()).first()
if latest_log:
latest_log.input1_status = input1
latest_log.input2_status = input2
latest_log.input3_status = input3
latest_log.input4_status = input4
db.session.commit()
flash('Board settings updated successfully!', 'success')
else:
flash('No logs found for the specified board.', 'danger')
else:
flash('Invalid board settings!', 'danger')
return redirect(url_for('settings'))
@app.route('/run_cleanup_now', methods=['POST']) @app.route('/run_cleanup_now', methods=['POST'])
def run_cleanup_now(): def run_cleanup_now():
with app.app_context(): with app.app_context():

Binary file not shown.

View File

@@ -118,7 +118,7 @@
</div> </div>
</div> </div>
<div class="row mt-4"> <div class="row mt-4">
<div class="col-md-12"> <div class="col-md-6">
<h3>Logs</h3> <h3>Logs</h3>
<div class="card mb-4"> <div class="card mb-4">
<div class="card-body d-flex justify-content-between align-items-center"> <div class="card-body d-flex justify-content-between align-items-center">
@@ -139,6 +139,16 @@
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>
<div class="col-md-6">
<h3>MIR Server Status</h3>
<div class="card mb-4">
<div class="card-body">
<h5 class="card-title">Input Mission Status</h5>
<p class="card-text">Status: <span id="mir-status">Unknown</span></p>
<p class="card-text">Last Input Triggered: <span id="last-input-triggered">None</span></p>
</div>
</div>
</div>
</div> </div>
</div> </div>
<script> <script>

View File

@@ -33,11 +33,21 @@
background-color: #0056b3; background-color: #0056b3;
border-color: #004085; border-color: #004085;
} }
.btn-danger {
background-color: #dc3545;
border-color: #dc3545;
}
.btn-danger:hover {
background-color: #c82333;
border-color: #bd2130;
}
</style> </style>
</head> </head>
<body> <body>
<div class="container mt-5"> <div class="container mt-5">
<h1 class="text-center mb-4">Settings</h1> <h1 class="text-center mb-4">Settings</h1>
<div class="row">
<div class="col-md-6">
<div class="card mb-4"> <div class="card mb-4">
<div class="card-body"> <div class="card-body">
<h5 class="card-title">Set Cleanup Time</h5> <h5 class="card-title">Set Cleanup Time</h5>
@@ -48,16 +58,61 @@
</div> </div>
<button type="submit" class="btn btn-primary">Set Time</button> <button type="submit" class="btn btn-primary">Set Time</button>
</form> </form>
<form action="{{ url_for('run_cleanup_now') }}" method="post" class="form-inline">
<button type="submit" class="btn btn-danger mb-2 ml-2">Run Cleanup Now</button>
</form>
</div> </div>
</div> </div>
<div class="card mb-4"> <div class="card mb-4">
<div class="card-body"> <div class="card-body">
<h5 class="card-title">Run Cleanup Now</h5> <h5 class="card-title">MIR Server Connection</h5>
<form action="{{ url_for('run_cleanup_now') }}" method="post"> <form action="{{ url_for('set_mir_server') }}" method="post">
<button type="submit" class="btn btn-danger">Run Cleanup Now</button> <div class="form-group">
<label for="mir_ip">MIR Server IP Address:</label>
<input type="text" class="form-control" id="mir_ip" name="mir_ip" value="{{ mir_ip }}" required>
</div>
<div class="form-group">
<label for="mir_user">MIR Server User:</label>
<input type="text" class="form-control" id="mir_user" name="mir_user" value="{{ mir_user }}" required>
</div>
<div class="form-group">
<label for="mir_password">MIR Server Password:</label>
<input type="password" class="form-control" id="mir_password" name="mir_password" value="{{ mir_password }}" required>
</div>
<button type="submit" class="btn btn-primary">Set MIR Server</button>
</form> </form>
</div> </div>
</div> </div>
</div>
<div class="col-md-6">
{% for board in boards %}
<div class="card mb-4">
<div class="card-body">
<h5 class="card-title">Board: {{ board.hostname }}</h5>
<form action="{{ url_for('set_board_settings', hostname=board.hostname) }}" method="post">
<div class="form-group">
<label for="input1">Input 1:</label>
<input type="text" class="form-control" id="input1" name="input1" value="{{ board.input1 }}" required>
</div>
<div class="form-group">
<label for="input2">Input 2:</label>
<input type="text" class="form-control" id="input2" name="input2" value="{{ board.input2 }}" required>
</div>
<div class="form-group">
<label for="input3">Input 3:</label>
<input type="text" class="form-control" id="input3" name="input3" value="{{ board.input3 }}" required>
</div>
<div class="form-group">
<label for="input4">Input 4:</label>
<input type="text" class="form-control" id="input4" name="input4" value="{{ board.input4 }}" required>
</div>
<button type="submit" class="btn btn-primary">Save Settings</button>
</form>
</div>
</div>
{% endfor %}
</div>
</div>
<a href="{{ url_for('index') }}" class="btn btn-secondary">Back to Main Page</a> <a href="{{ url_for('index') }}" class="btn btn-secondary">Back to Main Page</a>
</div> </div>
</body> </body>