adding boards and settings
This commit is contained in:
@@ -17,8 +17,14 @@ 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
|
||||
relay1_status = db.Column(db.String(3), default='off')
|
||||
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
|
||||
if not os.path.exists('instance/logs.db'):
|
||||
@@ -41,20 +47,54 @@ 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')
|
||||
|
||||
# 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:
|
||||
# Parse the message to update relay status
|
||||
if "Relay" in message and "turned" in message:
|
||||
parts = message.split()
|
||||
relay_index = int(parts[1]) - 1
|
||||
relay_index = int(parts[1])
|
||||
action = parts[3].lower()
|
||||
relay_status_list = relay_status.split(',')
|
||||
relay_status_list[relay_index] = action
|
||||
relay_status = ','.join(relay_status_list)
|
||||
if relay_index == 1:
|
||||
relay1_status = action
|
||||
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.commit()
|
||||
return jsonify({'status': 'success', 'message': 'Log saved'}), 201
|
||||
@@ -97,12 +137,26 @@ def view_board(hostname):
|
||||
input_status = ['off', 'off', 'off', 'off']
|
||||
if logs:
|
||||
latest_log = logs[0]
|
||||
relay_status = latest_log.relay_status.split(',')
|
||||
input_status = latest_log.input_status.split(',')
|
||||
relay_status = [
|
||||
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
|
||||
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'])
|
||||
def delete_board(hostname):
|
||||
@@ -139,7 +193,21 @@ def control_relay(hostname, relay, action):
|
||||
@app.route('/settings', methods=['GET'])
|
||||
def settings():
|
||||
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'])
|
||||
def set_cleanup_time():
|
||||
@@ -151,6 +219,41 @@ def set_cleanup_time():
|
||||
flash('Invalid cleanup time!', 'danger')
|
||||
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'])
|
||||
def run_cleanup_now():
|
||||
with app.app_context():
|
||||
|
||||
Reference in New Issue
Block a user