saved
This commit is contained in:
@@ -13,6 +13,14 @@ class Log(db.Model):
|
||||
hostname = db.Column(db.String(100), nullable=False)
|
||||
message = db.Column(db.String(500), nullable=False)
|
||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
relay1_status = db.Column(db.String(10), default='off')
|
||||
relay2_status = db.Column(db.String(10), default='off')
|
||||
relay3_status = db.Column(db.String(10), default='off')
|
||||
relay4_status = db.Column(db.String(10), default='off')
|
||||
input1_status = db.Column(db.String(10), default='off')
|
||||
input2_status = db.Column(db.String(10), default='off')
|
||||
input3_status = db.Column(db.String(10), default='off')
|
||||
input4_status = db.Column(db.String(10), default='off')
|
||||
|
||||
# Create the database if it does not exist
|
||||
if not os.path.exists('instance/logs.db'):
|
||||
@@ -35,7 +43,63 @@ def log_message():
|
||||
hostname = data.get('hostname')
|
||||
message = data.get('message')
|
||||
if hostname and message:
|
||||
new_log = Log(hostname=hostname, message=message)
|
||||
# Get the latest log for the hostname to determine the current status
|
||||
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'
|
||||
|
||||
# Update the status based on the log message
|
||||
if "Relay 1 turned ON" in message:
|
||||
relay1_status = 'on'
|
||||
elif "Relay 1 turned OFF" in message:
|
||||
relay1_status = 'off'
|
||||
if "Relay 2 turned ON" in message:
|
||||
relay2_status = 'on'
|
||||
elif "Relay 2 turned OFF" in message:
|
||||
relay2_status = 'off'
|
||||
if "Relay 3 turned ON" in message:
|
||||
relay3_status = 'on'
|
||||
elif "Relay 3 turned OFF" in message:
|
||||
relay3_status = 'off'
|
||||
if "Relay 4 turned ON" in message:
|
||||
relay4_status = 'on'
|
||||
elif "Relay 4 turned OFF" in message:
|
||||
relay4_status = 'off'
|
||||
if "Input 1 pressed" in message:
|
||||
input1_status = 'on'
|
||||
elif "Input 1 released" in message:
|
||||
input1_status = 'off'
|
||||
if "Input 2 pressed" in message:
|
||||
input2_status = 'on'
|
||||
elif "Input 2 released" in message:
|
||||
input2_status = 'off'
|
||||
if "Input 3 pressed" in message:
|
||||
input3_status = 'on'
|
||||
elif "Input 3 released" in message:
|
||||
input3_status = 'off'
|
||||
if "Input 4 pressed" in message:
|
||||
input4_status = 'on'
|
||||
elif "Input 4 released" in message:
|
||||
input4_status = 'off'
|
||||
|
||||
new_log = Log(
|
||||
hostname=hostname,
|
||||
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.commit()
|
||||
return jsonify({'status': 'success', 'message': 'Log saved'}), 201
|
||||
@@ -73,8 +137,29 @@ def index():
|
||||
|
||||
@app.route('/board/<hostname>')
|
||||
def view_board(hostname):
|
||||
logs = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.desc()).all()
|
||||
return render_template('board.html', hostname=hostname, logs=logs)
|
||||
logs = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.asc()).all() # Order by ascending timestamp
|
||||
relay_status = ['off', 'off', 'off', 'off']
|
||||
input_status = ['off', 'off', 'off', 'off']
|
||||
relay_messages = ['', '', '', '']
|
||||
input_messages = ['', '', '', '']
|
||||
|
||||
# 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
|
||||
elif f"Input {i + 1} released" in log.message:
|
||||
input_status[i] = 'off'
|
||||
input_messages[i] = log.message
|
||||
|
||||
return render_template('board.html', hostname=hostname, logs=logs, relay_status=relay_status, input_status=input_status, relay_messages=relay_messages, input_messages=input_messages)
|
||||
|
||||
@app.route('/delete_board/<hostname>', methods=['POST'])
|
||||
def delete_board(hostname):
|
||||
@@ -85,7 +170,22 @@ def delete_board(hostname):
|
||||
@app.route('/board/<hostname>/logs', methods=['GET'])
|
||||
def get_board_logs(hostname):
|
||||
logs = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.desc()).all()
|
||||
return jsonify({'logs': [{'timestamp': log.timestamp, 'message': log.message} for log in logs]})
|
||||
latest_log = logs[0] if logs else None
|
||||
return jsonify({
|
||||
'logs': [{'timestamp': log.timestamp, 'message': log.message} for log in logs],
|
||||
'relay_status': {
|
||||
'relay1': latest_log.relay1_status if latest_log else 'off',
|
||||
'relay2': latest_log.relay2_status if latest_log else 'off',
|
||||
'relay3': latest_log.relay3_status if latest_log else 'off',
|
||||
'relay4': latest_log.relay4_status if latest_log else 'off'
|
||||
},
|
||||
'input_status': {
|
||||
'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'
|
||||
}
|
||||
})
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
@@ -27,6 +27,18 @@
|
||||
color: #6c757d;
|
||||
text-align: right;
|
||||
}
|
||||
.status-indicator {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
display: inline-block;
|
||||
border-radius: 50%;
|
||||
}
|
||||
.status-on {
|
||||
background-color: #28a745;
|
||||
}
|
||||
.status-off {
|
||||
background-color: #dc3545;
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
function updateLogs() {
|
||||
@@ -41,6 +53,22 @@
|
||||
logItem.innerHTML = `<strong>${log.timestamp}</strong>: ${log.message}`;
|
||||
logsContainer.appendChild(logItem);
|
||||
});
|
||||
|
||||
// Update relay status indicators and messages
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
const relayIndicator = document.getElementById(`relay-status-${i}`);
|
||||
relayIndicator.className = `status-indicator ${data.relay_status[`relay${i}`] === 'on' ? 'status-on' : 'status-off'}`;
|
||||
const relayMessage = document.getElementById(`relay-message-${i}`);
|
||||
relayMessage.innerText = data.logs.find(log => log.message.includes(`Relay ${i} turned`)).message;
|
||||
}
|
||||
|
||||
// Update input status indicators and messages
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
const inputIndicator = document.getElementById(`input-status-${i}`);
|
||||
inputIndicator.className = `status-indicator ${data.input_status[`input${i}`] === 'on' ? 'status-on' : 'status-off'}`;
|
||||
const inputMessage = document.getElementById(`input-message-${i}`);
|
||||
inputMessage.innerText = data.logs.find(log => log.message.includes(`Input ${i}`)).message;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -73,7 +101,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="col-md-6">
|
||||
<div class="card mb-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Logs for {{ hostname }}</h5>
|
||||
@@ -87,6 +115,38 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="row">
|
||||
{% for i in range(4) %}
|
||||
<div class="col-md-6 mb-4">
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Relay {{ i + 1 }}</h5>
|
||||
<p class="card-text">Status:
|
||||
<span id="relay-status-{{ i + 1 }}" class="status-indicator {{ 'status-on' if relay_status[i] == 'on' else 'status-off' }}"></span>
|
||||
</p>
|
||||
<p class="card-text" id="relay-message-{{ i + 1 }}">{{ relay_messages[i] }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div class="row">
|
||||
{% for i in range(4) %}
|
||||
<div class="col-md-6 mb-4">
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Input {{ i + 1 }}</h5>
|
||||
<p class="card-text">Status:
|
||||
<span id="input-status-{{ i + 1 }}" class="status-indicator {{ 'status-on' if input_status[i] == 'on' else 'status-off' }}"></span>
|
||||
</p>
|
||||
<p class="card-text" id="input-message-{{ i + 1 }}">{{ input_messages[i] }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user