3535import json
3636import os
3737import shutil
38- import signal
3938import sys
4039import requests
4140import tempfile
4241import argparse
4342
4443from threading import Thread
4544from RTKLIB import RTKLIB
46- from port import changeBaudrateTo115200
47- from reach_tools import reach_tools , provisioner
4845from ServiceController import ServiceController
4946from RTKBaseConfigManager import RTKBaseConfigManager
5047
7168from werkzeug .security import generate_password_hash
7269from werkzeug .security import check_password_hash
7370from werkzeug .utils import safe_join
74- import urllib
7571import gunicorn .app .base
7672
7773app = Flask (__name__ )
@@ -157,7 +153,7 @@ def update_password(config_object):
157153 if new_password != "" :
158154 config_object .update_setting ("general" , "web_password_hash" , generate_password_hash (new_password ))
159155 config_object .update_setting ("general" , "new_web_password" , "" )
160-
156+
161157def manager ():
162158 """ This manager runs inside a separate thread
163159 It checks how long rtkrcv is running since the last user leaves the
@@ -191,7 +187,7 @@ def manager():
191187 "volume_total" : round (volume_usage .total / 10E8 , 2 ),
192188 "volume_percent_used" : volume_usage .percent }
193189 socketio .emit ("sys_informations" , json .dumps (sys_infos ), namespace = "/test" )
194-
190+
195191 if rtk .sleep_count > rtkcv_standby_delay and rtk .state != "inactive" or \
196192 main_service .get ("active" ) == False and rtk .state != "inactive" :
197193 print ("DEBUG Stopping rtkrcv" )
@@ -222,7 +218,7 @@ def repaint_services_button(services_list):
222218 service ["btn_off_color" ] = "outline-danger"
223219 elif service .get ("state_ok" ) == True :
224220 service ["btn_off_color" ] = "outline-secondary"
225-
221+
226222 return services_list
227223
228224def old_get_cpu_temp ():
@@ -258,15 +254,15 @@ def get_sbc_model():
258254 Try to detect the single board computer used
259255 :return the model name or unknown if not detected
260256 """
261- answer = subprocess .run (["cat" , "/proc/device-tree/model" ], encoding = "UTF-8" , stderr = subprocess .PIPE , stdout = subprocess .PIPE )
257+ answer = subprocess .run (["cat" , "/proc/device-tree/model" ], encoding = "UTF-8" , stderr = subprocess .PIPE , stdout = subprocess .PIPE , check = False )
262258 if answer .returncode == 0 :
263259 sbc_model = answer .stdout .split ("\n " ).pop ().strip ()
264260 else :
265261 sbc_model = "unknown"
266262 return sbc_model
267263
268264@socketio .on ("check update" , namespace = "/test" )
269- def check_update (source_url = None , current_release = None , prerelease = rtkbaseconfig .getboolean ("general" , "prerelease" ), emit = True ):
265+ def check_update (source_url = None , current_release = None , prerelease = rtkbaseconfig .getboolean ("general" , "prerelease" ), return_emit = True ):
270266 """
271267 Check if a RTKBase update exists
272268 :param source_url: the url where we will try to find an update. It uses the github api.
@@ -285,7 +281,7 @@ def check_update(source_url = None, current_release = None, prerelease=rtkbaseco
285281 source_url = source_url if source_url is not None else "https://api.github.com/repos/stefal/rtkbase/releases"
286282 current_release = current_release if current_release is not None else rtkbaseconfig .get ("general" , "version" ).strip ("v" )
287283 current_release = current_release .split ("-beta" , 1 )[0 ].split ("-alpha" , 1 )[0 ].split ("-rc" , 1 )[0 ].split ("b" , 1 )[0 ]
288-
284+
289285 try :
290286 response = requests .get (source_url )
291287 response = response .json ()
@@ -300,12 +296,12 @@ def check_update(source_url = None, current_release = None, prerelease=rtkbaseco
300296 new_release ["url" ] = asset .get ("browser_download_url" )
301297 break
302298 break
303-
299+
304300 except Exception as e :
305301 print ("Check update error: " , e )
306302 new_release = { "error" : repr (e )}
307-
308- if emit :
303+
304+ if return_emit :
309305 socketio .emit ("new release" , json .dumps (new_release ), namespace = "/test" )
310306 return new_release
311307
@@ -321,7 +317,7 @@ def update_rtkbase(update_file=False):
321317
322318 if not update_file :
323319 #Check if an update is available
324- update_url = check_update (emit = False ).get ("url" )
320+ update_url = check_update (return_emit = False ).get ("url" )
325321 if update_url is None :
326322 return
327323 #Download update
@@ -331,7 +327,7 @@ def update_rtkbase(update_file=False):
331327 update_file .save ("/var/tmp/rtkbase_update.tar.gz" )
332328 update_archive = "/var/tmp/rtkbase_update.tar.gz"
333329 print ("update stored in /var/tmp/" )
334-
330+
335331 if update_archive is None :
336332 socketio .emit ("downloading_update" , json .dumps ({"result" : 'false' }), namespace = "/test" )
337333 return
@@ -349,7 +345,7 @@ def update_rtkbase(update_file=False):
349345 os .rmdir ("/var/tmp/rtkbase" )
350346 except FileNotFoundError :
351347 print ("/var/tmp/rtkbase directory doesn't exist" )
352-
348+
353349 #Extract archive
354350 tar .extractall ("/var/tmp" )
355351
@@ -359,7 +355,7 @@ def update_rtkbase(update_file=False):
359355 current_release = rtkbaseconfig .get ("general" , "version" ).strip ("v" )
360356 standard_user = rtkbaseconfig .get ("general" , "user" )
361357 #launch update verifications
362- answer = subprocess .run ([script_path , source_path , rtkbase_path , data_dir , current_release , standard_user , "--checking" ], encoding = "UTF-8" , stderr = subprocess .PIPE , stdout = subprocess .PIPE )
358+ answer = subprocess .run ([script_path , source_path , rtkbase_path , data_dir , current_release , standard_user , "--checking" ], encoding = "UTF-8" , stderr = subprocess .PIPE , stdout = subprocess .PIPE , check = False )
363359 if answer .returncode != 0 :
364360 socketio .emit ("updating_rtkbase_stopped" , json .dumps ({"error" : answer .stderr .splitlines ()}), namespace = "/test" )
365361 print ("Checking OS release failed. Update aborted!" )
@@ -394,8 +390,8 @@ def inject_release():
394390 g .sbc_model = get_sbc_model ()
395391
396392@login .user_loader
397- def load_user (id ):
398- return User (id )
393+ def load_user (user_id ):
394+ return User (user_id )
399395
400396@app .route ('/' )
401397@app .route ('/index' )
@@ -425,8 +421,11 @@ def settings_page():
425421 return ('' , 204 )
426422 # variable needed when the gnss receiver offer a web interface
427423 host_url = urllib .parse .urlparse (request .host_url )
428- gnss_rcv_url = urllib .parse .ParseResult (scheme = host_url .scheme , netloc = "{}:{}" .format (host_url .hostname , rtkbaseconfig .get ("main" , "gnss_rcv_web_proxy_port" )),
429- path = host_url .path , params = host_url .params , query = host_url .query , fragment = host_url .fragment )
424+ gnss_rcv_url = urllib .parse .ParseResult (scheme = host_url .scheme ,
425+ netloc = "{}:{}" .format (host_url .hostname , rtkbaseconfig .get ("main" , "gnss_rcv_web_proxy_port" )),
426+ path = host_url .path ,
427+ params = host_url .params , query = host_url .query ,
428+ fragment = host_url .fragment )
430429 #TODO use dict and not list
431430 main_settings = rtkbaseconfig .get_main_settings ()
432431 main_settings .append (gnss_rcv_url .geturl ())
@@ -480,14 +479,14 @@ def login_page():
480479 password = loginform .password .data
481480 if not user .check_password (password ):
482481 return abort (401 )
483-
482+
484483 login_user (user , remember = loginform .remember_me .data )
485484 next_page = request .args .get ('next' )
486485 if not next_page or urllib .parse .urlsplit (next_page ).netloc != '' :
487486 next_page = url_for ('status_page' )
488487
489488 return redirect (next_page )
490-
489+
491490 return render_template ('login.html' , title = 'Sign In' , form = loginform )
492491
493492@app .route ('/logout' )
@@ -507,10 +506,12 @@ def diagnostic():
507506 for service in services_list + [rtkbase_web_service ]:
508507 sysctl_status = subprocess .run (['systemctl' , 'status' , service ['service_unit' ]],
509508 stdout = subprocess .PIPE ,
510- universal_newlines = True )
509+ universal_newlines = True ,
510+ check = False )
511511 journalctl = subprocess .run (['journalctl' , '--since' , '7 days ago' , '-u' , service ['service_unit' ]],
512512 stdout = subprocess .PIPE ,
513- universal_newlines = True )
513+ universal_newlines = True ,
514+ check = False )
514515
515516 #Replace carrier return to <br> for html view
516517 sysctl_status = sysctl_status .stdout .replace ('\n ' , '<br>' )
@@ -519,7 +520,7 @@ def diagnostic():
519520 logs .append ({'name' : service ['service_unit' ], 'active' : active_state , 'sysctl_status' : sysctl_status , 'journalctl' : journalctl })
520521
521522 return render_template ('diagnostic.html' , logs = logs )
522-
523+
523524
524525#### Handle connect/disconnect events ####
525526
@@ -579,11 +580,11 @@ def startBase():
579580 if rtk .get_rtkcv_option ("inpstr1-format" ) != saved_input_type :
580581 rtk .set_rtkcv_option ("inpstr1-format" , saved_input_type )
581582 rtk .set_rtkcv_pending_refresh (True )
582-
583+
583584 if rtk .get_rtkcv_pending_status ():
584585 print ("REFRESH NEEDED !!!!!!!!!!!!!!!!" )
585586 rtk .startBase ()
586-
587+
587588@socketio .on ("stop base" , namespace = "/test" )
588589def stopBase ():
589590 rtk .stopBase ()
@@ -592,12 +593,6 @@ def stopBase():
592593def continueBase ():
593594 rtk .sleep_count = 0
594595
595- #### Free space handler
596-
597- @socketio .on ("get available space" , namespace = "/test" )
598- def getAvailableSpace ():
599- rtk .socketio .emit ("available space" , reach_tools .getFreeSpace (path_to_gnss_log ), namespace = "/test" )
600-
601596#### Delete log button handler ####
602597
603598@socketio .on ("delete log" , namespace = "/test" )
@@ -612,7 +607,7 @@ def deleteLog(json_msg):
612607def detect_receiver (json_msg ):
613608 print ("Detecting gnss receiver" )
614609 #print("DEBUG json_msg: ", json_msg)
615- answer = subprocess .run ([os .path .join (rtkbase_path , "tools" , "install.sh" ), "--user" , rtkbaseconfig .get ("general" , "user" ), "--detect-gnss" , "--no-write-port" ], encoding = "UTF-8" , stderr = subprocess .PIPE , stdout = subprocess .PIPE )
610+ answer = subprocess .run ([os .path .join (rtkbase_path , "tools" , "install.sh" ), "--user" , rtkbaseconfig .get ("general" , "user" ), "--detect-gnss" , "--no-write-port" ], encoding = "UTF-8" , stderr = subprocess .PIPE , stdout = subprocess .PIPE , check = False )
616611 if answer .returncode == 0 and "/dev/" in answer .stdout :
617612 #print("DEBUG ok stdout: ", answer.stdout)
618613 try :
@@ -644,7 +639,7 @@ def configure_receiver(brand="", model=""):
644639 restart_main = False
645640
646641 print ("configuring {} gnss receiver model {}" .format (brand , model ))
647- answer = subprocess .run ([os .path .join (rtkbase_path , "tools" , "install.sh" ), "--user" , rtkbaseconfig .get ("general" , "user" ), "--configure-gnss" ], encoding = "UTF-8" , stderr = subprocess .PIPE , stdout = subprocess .PIPE )
642+ answer = subprocess .run ([os .path .join (rtkbase_path , "tools" , "install.sh" ), "--user" , rtkbaseconfig .get ("general" , "user" ), "--configure-gnss" ], encoding = "UTF-8" , stderr = subprocess .PIPE , stdout = subprocess .PIPE , check = False )
648643 #print("DEBUG - stdout: ", answer.stdout)
649644 #print("DEBUG - returncode: ", answer.returncode)
650645
@@ -715,7 +710,7 @@ def rinex_ign(json_msg):
715710 convpath = os .path .abspath (os .path .join (rtkbase_path , "tools" , "convbin.sh" ))
716711 convbin_user = rtkbaseconfig .get ("general" , "user" ).strip ("'" )
717712 #print("DEBUG", convpath, json_msg.get("filename"), rtk.logm.log_path, rinex_type)
718- answer = subprocess .run (["sudo" , "-u" , convbin_user , convpath , json_msg .get ("filename" ), rtk .logm .log_path , rinex_type ], encoding = "UTF-8" , stderr = subprocess .PIPE , stdout = subprocess .PIPE )
713+ answer = subprocess .run (["sudo" , "-u" , convbin_user , convpath , json_msg .get ("filename" ), rtk .logm .log_path , rinex_type ], encoding = "UTF-8" , stderr = subprocess .PIPE , stdout = subprocess .PIPE , check = False )
719714 if answer .returncode == 0 and "rinex_file=" in answer .stdout :
720715 rinex_file = answer .stdout .split ("\n " ).pop ().strip ("rinex_file=" )
721716 result = {"result" : "success" , "file" : rinex_file }
@@ -829,7 +824,7 @@ def restartServices(restart_services_list=None):
829824 service ["unit" ].start ()
830825 else :
831826 service ["unit" ].restart ()
832-
827+
833828 #refresh service status
834829 getServicesStatus ()
835830
@@ -865,9 +860,9 @@ def getServicesStatus(emit_pingback=True):
865860 pass
866861
867862 services_status = []
868- for service in services_list :
863+ for service in services_list :
869864 services_status .append ({key :service [key ] for key in service if key != 'unit' })
870-
865+
871866 services_status = repaint_services_button (services_status )
872867 #print(services_status)
873868 if emit_pingback :
0 commit comments