11import 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
33import json
44import os
55import subprocess
66from 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
1215logging .basicConfig (level = logging .DEBUG , # Set the log level to DEBUG
1316 format = '%(asctime)s - %(levelname)s - %(message)s' )
1417logger = logging .getLogger (__name__ )
1518
1619app = 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"
2022if 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' )
2325else :
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
2830logger .info (f'Settings file path: { SETTINGS_FILE } ' )
@@ -58,15 +60,26 @@ def stream_journal():
5860
5961@app .route ('/journal' )
6062def journal ():
61- return render_template ('journal.html' )
63+ return render_template ('journal.html' , version = app_version )
6264
6365@app .route ('/stream' )
6466def stream ():
6567 return Response (stream_journal (), content_type = 'text/event-stream' )
6668
6769@app .route ('/' )
6870def 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' ])
7285def 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' ])
88101def 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+
144195def main ():
145196 app .run (host = '0.0.0.0' , port = SERVER_PORT )
146197
0 commit comments