Skip to content

Commit f41a949

Browse files
authored
Merge pull request #5 from mikecarr/service-control
formatting fixes, add service control section, add warning on edit page
2 parents 67ea545 + 79748f2 commit f41a949

File tree

11 files changed

+306
-66
lines changed

11 files changed

+306
-66
lines changed

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include py_config_gs/settings.json
1+
include py_config_gs/py-config-gs.json
22
recursive-include systemd *
33
recursive-include py_config_gs/templates *
44
recursive-include py_config_gs/static *

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@ pip install .
5656
or
5757
python setup.py install
5858
59+
5960
```
6061
## config file
6162
```
62-
cp /usr/local/lib/python3.9/dist-packages/config/settings.json /config
63+
cp /usr/local/lib/python3.9/dist-packages/config/py-config-gs.json /config
6364
cp /usr/local/lib/python3.9/dist-packages/etc/systemd/system/py-config-gs.service /etc/systemd/system/
6465
sudo systemctl daemon-reload
6566
6667
sudo systemctl start py-config-gs
68+
69+
# Optional, you can always run the command above out in the field if you are worried about resource consumption
6770
sudo systemctl enable py-config-gs
6871
```
6972

images/home.png

134 KB
Loading

py_config_gs/app.py

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
import logging
2-
from flask import Flask, render_template, request, Response, redirect, url_for, send_from_directory
2+
from flask import Flask, render_template, request, Response, redirect, url_for, send_from_directory, flash
33
import json
44
import os
55
import subprocess
66
from importlib.metadata import version
77

88

9-
app_version = version('py-config-gs')
9+
#app_version = version('py-config-gs')
10+
with open('version.txt', 'r') as f:
11+
app_version = f.read().strip()
12+
1013

1114
# Configure logging
1215
logging.basicConfig(level=logging.DEBUG, # Set the log level to DEBUG
1316
format='%(asctime)s - %(levelname)s - %(message)s')
1417
logger = logging.getLogger(__name__)
1518

1619
app = Flask(__name__)
20+
app.secret_key = os.getenv('SECRET_KEY', 'default_secret_key')
1721

18-
#SETTINGS_FILE = "/Users/mcarr/config/settings.json"
19-
#SETTINGS_FILE = "/config/settings.json"
2022
if os.getenv('FLASK_ENV') == 'development':
2123
# In development, use the home folder settings file
22-
SETTINGS_FILE = os.path.expanduser('~/config/settings.json')
24+
SETTINGS_FILE = os.path.expanduser('~/config/py-config-gs.json')
2325
else:
2426
# In production, use the config folder
25-
SETTINGS_FILE = '/config/settings.json'
27+
SETTINGS_FILE = '/config/py-config-gs.json'
2628

2729
# Log the SETTINGS_FILE path
2830
logger.info(f'Settings file path: {SETTINGS_FILE}')
@@ -58,15 +60,26 @@ def stream_journal():
5860

5961
@app.route('/journal')
6062
def journal():
61-
return render_template('journal.html')
63+
return render_template('journal.html', version=app_version)
6264

6365
@app.route('/stream')
6466
def stream():
6567
return Response(stream_journal(), content_type='text/event-stream')
6668

6769
@app.route('/')
6870
def home():
69-
return render_template('home.html', config_files=config_files, version=app_version)
71+
# List of services that you want to control
72+
services = ['openipc']
73+
service_statuses = {}
74+
75+
# Fetches the current status (enabled/disabled) for each service.
76+
for service in services:
77+
# Check if the service is enabled or disabled
78+
result = subprocess.run(['systemctl', 'is-enabled', service], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
79+
status = result.stdout.decode('utf-8').strip()
80+
service_statuses[service] = status
81+
82+
return render_template('home.html', config_files=config_files, version=app_version, services=service_statuses )
7083

7184
@app.route('/edit/<filename>', methods=['GET', 'POST'])
7285
def edit(filename):
@@ -82,7 +95,7 @@ def edit(filename):
8295
with open(file_path, 'r') as f:
8396
content = f.read()
8497

85-
return render_template('edit.html', filename=filename, content=content)
98+
return render_template('edit.html', filename=filename, content=content, version=app_version)
8699

87100
@app.route('/save/<filename>', methods=['POST'])
88101
def save(filename):
@@ -98,7 +111,7 @@ def videos():
98111
video_files = [f for f in os.listdir(VIDEO_DIR) if f.endswith(('.mp4', '.mkv', '.avi'))]
99112
logger.debug(f'VIDEO_DIR: {VIDEO_DIR}')
100113
logger.debug(f'Video files found: {video_files}')
101-
return render_template('videos.html', video_files=video_files)
114+
return render_template('videos.html', video_files=video_files, version=app_version)
102115

103116

104117
@app.route('/play/<filename>')
@@ -119,10 +132,10 @@ def get_temperature():
119132
gpu_temp_f = (gpu_temp * 9/5) + 32
120133

121134
return {
122-
'soc_temperature': f"{soc_temp:.1f} °C",
123-
'soc_temperature_f': f"{soc_temp_f:.1f} °F",
124-
'gpu_temperature': f"{gpu_temp:.1f} °C",
125-
'gpu_temperature_f': f"{gpu_temp_f:.1f} °F"
135+
'soc_temperature': f"{soc_temp:.1f}",
136+
'soc_temperature_f': f"{soc_temp_f:.1f}",
137+
'gpu_temperature': f"{gpu_temp:.1f}",
138+
'gpu_temperature_f': f"{gpu_temp_f:.1f}"
126139
}
127140

128141
except Exception as e:
@@ -141,6 +154,44 @@ def backup():
141154
logger.debug('Backup created for configuration files.')
142155
return redirect(url_for('home'))
143156

157+
@app.route('/run_command', methods=['POST'])
158+
def run_command():
159+
selected_command = request.form.get('command')
160+
161+
# Construct the first command based on the dropdown value
162+
cli_command = f"echo cli -s {selected_command} > /dev/udp/localhost/14550"
163+
logger.debug(f'Running command: {cli_command}')
164+
165+
# Run the commands
166+
subprocess.run(cli_command, shell=True)
167+
subprocess.run("echo killall -1 majestic > /dev/udp/localhost/14550", shell=True)
168+
169+
# Redirect back to the home page after the commands are run
170+
return redirect(url_for('home'))
171+
172+
@app.route('/service_action', methods=['POST'])
173+
def service_action():
174+
service_name = request.form.get('service_name')
175+
action = request.form.get('action')
176+
177+
if service_name and action:
178+
try:
179+
if action == 'enable':
180+
subprocess.run(['sudo', 'systemctl', 'enable', service_name], check=True)
181+
flash(f'Service {service_name} enabled successfully.', 'success')
182+
elif action == 'disable':
183+
subprocess.run(['sudo', 'systemctl', 'disable', service_name], check=True)
184+
flash(f'Service {service_name} disabled successfully.', 'success')
185+
elif action == 'restart':
186+
subprocess.run(['sudo', 'systemctl', 'restart', service_name], check=True)
187+
flash(f'Service {service_name} restarted successfully.', 'success')
188+
else:
189+
flash('Invalid action.', 'error')
190+
except subprocess.CalledProcessError as e:
191+
flash(f'Failed to {action} service {service_name}: {e}', 'error')
192+
193+
return redirect(url_for('home'))
194+
144195
def main():
145196
app.run(host='0.0.0.0', port=SERVER_PORT)
146197

File renamed without changes.

py_config_gs/static/css/styles.css

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ body {
88
background-color: #f4f4f4;
99
}
1010

11-
h1, h2 {
11+
h1,
12+
h2 {
1213
color: #333;
1314
}
1415

@@ -86,12 +87,6 @@ p {
8687
}
8788

8889
/* Footer Styles */
89-
/* footer {
90-
margin-top: 20px;
91-
text-align: center;
92-
font-size: 0.8em;
93-
color: #666;
94-
} */
9590
footer {
9691
position: fixed;
9792
bottom: 0;
@@ -100,3 +95,86 @@ footer {
10095
text-align: center;
10196
padding: 10px;
10297
}
98+
99+
/* Styled Box */
100+
.styled-box {
101+
border: 1px solid #ccc;
102+
background-color: #f0f0f0;
103+
padding: 10px;
104+
margin: 15px 0;
105+
border-radius: 5px;
106+
font-family: Arial, sans-serif;
107+
}
108+
109+
/* Warning Styles */
110+
.warning {
111+
color: red;
112+
font-weight: bold;
113+
font-style: italic;
114+
margin-bottom: 20px;
115+
}
116+
117+
/* Table Styles */
118+
table {
119+
width: 100%; /* Make the table take full width */
120+
border-collapse: collapse;
121+
margin: 20px 0;
122+
font-size: 16px;
123+
}
124+
125+
table th, table td {
126+
padding: 15px; /* Increase padding for more space inside cells */
127+
text-align: left;
128+
border: 1px solid #ddd;
129+
}
130+
131+
table th {
132+
background-color: #f2f2f2;
133+
font-weight: bold;
134+
}
135+
136+
table th:nth-child(1),
137+
table td:nth-child(1) {
138+
width: 30%; /* Service column */
139+
}
140+
141+
table th:nth-child(2),
142+
table td:nth-child(2) {
143+
width: 20%; /* Status column */
144+
}
145+
146+
table th:nth-child(3),
147+
table td:nth-child(3) {
148+
width: 50%; /* Actions column */
149+
}
150+
151+
/* Status Styles */
152+
.status-enabled {
153+
color: green;
154+
font-weight: bold;
155+
}
156+
157+
.status-disabled {
158+
color: red;
159+
font-weight: bold;
160+
}
161+
162+
/* Button Styles */
163+
button {
164+
padding: 8px 12px; /* Increase button size */
165+
margin: 0 5px;
166+
border: none;
167+
cursor: pointer;
168+
background-color: #007bff;
169+
color: white;
170+
border-radius: 4px;
171+
}
172+
173+
button[type="submit"]:hover {
174+
background-color: #0056b3;
175+
}
176+
177+
/* Form Styles */
178+
form {
179+
display: inline;
180+
}

py_config_gs/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<head>
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<title>{% block title %}My Flask App{% endblock %}</title>
7+
<title>{% block title %}Py-Config-GS{% endblock %}</title>
88
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
99
</head>
1010
<body>

py_config_gs/templates/edit.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
{% block content %}
55
<div class="content">
6+
<div class="warning">
7+
WARNING: YOU ARE RESPONSIBLE FOR ANY CHANGES YOU MAKE, ALWAYS MAKE SURE YOU HAVE A BACKUP
8+
</div>
9+
610
<h2>Edit Configuration File: {{ filename }}</h2>
711
<form method="POST">
812
<textarea name="content" rows="20" cols="80">{{ content }}</textarea><br>

0 commit comments

Comments
 (0)