2828
2929app = Flask (__name__ , static_url_path = "/" )
3030configureLogging ()
31- logger = getLogger ()
31+ logger = getLogger (with_console = False )
32+ get_packet_logger (with_console = False )
3233
3334
3435class RestApiException (Exception ):
@@ -99,8 +100,9 @@ def _archive_pod():
99100 archive_suffix = datetime .utcnow ().strftime ("_%Y%m%d_%H%M%S" )
100101
101102 if os .path .isfile (DATA_PATH + POD_FILE + POD_FILE_SUFFIX ):
102- archive_name = os .rename (DATA_PATH + POD_FILE + POD_FILE_SUFFIX ,
103- DATA_PATH + POD_FILE + archive_suffix + POD_FILE_SUFFIX )
103+ archive_name = DATA_PATH + POD_FILE + archive_suffix + POD_FILE_SUFFIX
104+ os .rename (DATA_PATH + POD_FILE + POD_FILE_SUFFIX ,
105+ archive_name )
104106 if os .path .isfile (DATA_PATH + POD_FILE + POD_DB_SUFFIX ):
105107 os .rename (DATA_PATH + POD_FILE + POD_DB_SUFFIX ,
106108 DATA_PATH + POD_FILE + archive_suffix + POD_DB_SUFFIX )
@@ -148,6 +150,7 @@ def _save_activated_pod_address(addr):
148150 except :
149151 logger .exception ("Error while storing activated radio address" )
150152
153+
151154def _create_response (success , response , pod_status = None ):
152155
153156 if pod_status is None :
@@ -164,7 +167,8 @@ def _create_response(success, response, pod_status=None):
164167 "response" : response ,
165168 "status" : pod_status ,
166169 "datetime" : time .time (),
167- "api" : {"version_major" : API_VERSION_MAJOR , "version_minor" : API_VERSION_MINOR }
170+ "api" : {"version_major" : API_VERSION_MAJOR , "version_minor" : API_VERSION_MINOR ,
171+ "version_revision" : API_VERSION_REVISION , "version_build" : API_VERSION_BUILD }
168172 }, indent = 4 , sort_keys = True )
169173
170174
@@ -197,6 +201,7 @@ def _verify_auth(request_obj):
197201 logger .exception ("Error during verify_auth" )
198202 raise
199203
204+
200205def _adjust_time (adjustment ):
201206 logger .info ("Adjusting local time by %d ms" % adjustment )
202207 pdm = _get_pdm ()
@@ -242,10 +247,9 @@ def _api_result(result_lambda, generic_err_message):
242247def _get_pdm_address (timeout ):
243248 packet = None
244249 with PdmLock ():
245- radio = _get_pdm ().get_radio ()
246- radio .stop ()
247-
248250 try :
251+ radio = _get_pdm ().get_radio ()
252+ radio .stop ()
249253 packet = radio .get_packet (timeout )
250254 finally :
251255 radio .disconnect ()
@@ -277,6 +281,7 @@ def create_token():
277281def check_password ():
278282 _verify_auth (request )
279283
284+
280285def get_pdm_address ():
281286 _verify_auth (request )
282287
@@ -290,6 +295,7 @@ def get_pdm_address():
290295
291296 return {"radio_address" : address , "radio_address_hex" : "%8X" % address }
292297
298+
293299def new_pod ():
294300 _verify_auth (request )
295301
@@ -310,6 +316,7 @@ def new_pod():
310316 _archive_pod ()
311317 _set_pod (pod )
312318
319+
313320def activate_pod ():
314321 _verify_auth (request )
315322
@@ -326,6 +333,7 @@ def activate_pod():
326333 pdm .activate_pod (req_address , utc_offset = utc_offset )
327334 _save_activated_pod_address (req_address )
328335
336+
329337def start_pod ():
330338 _verify_auth (request )
331339
@@ -339,12 +347,14 @@ def start_pod():
339347
340348 pdm .inject_and_start (schedule )
341349
350+
342351def _int_parameter (obj , parameter ):
343352 if request .args .get (parameter ) is not None :
344353 obj .__dict__ [parameter ] = int (request .args .get (parameter ))
345354 return True
346355 return False
347356
357+
348358def _float_parameter (obj , parameter ):
349359 if request .args .get (parameter ) is not None :
350360 obj .__dict__ [parameter ] = float (request .args .get (parameter ))
@@ -403,6 +413,7 @@ def get_rl_info():
403413 r = RileyLink ()
404414 return r .get_info ()
405415
416+
406417def get_status ():
407418 _verify_auth (request )
408419 t = request .args .get ('type' )
@@ -415,13 +426,15 @@ def get_status():
415426 id = pdm .update_status (req_type )
416427 return {"row_id" :id }
417428
429+
418430def deactivate_pod ():
419431 _verify_auth (request )
420432 pdm = _get_pdm ()
421433 id = pdm .deactivate_pod ()
422434 _archive_pod ()
423435 return {"row_id" :id }
424436
437+
425438def bolus ():
426439 _verify_auth (request )
427440
@@ -478,6 +491,7 @@ def is_pdm_busy():
478491 pdm = _get_pdm ()
479492 return {"busy" : pdm .is_busy ()}
480493
494+
481495def acknowledge_alerts ():
482496 _verify_auth (request )
483497
@@ -499,6 +513,7 @@ def shutdown():
499513 os .system ("sudo shutdown -h" )
500514 return {"shutdown" : time .time ()}
501515
516+
502517def restart ():
503518 global g_deny
504519 _verify_auth (request )
@@ -511,14 +526,26 @@ def restart():
511526 os .system ("sudo shutdown -r" )
512527 return {"restart" : time .time ()}
513528
529+
530+ def update_omnipy ():
531+ global g_deny
532+ _verify_auth (request )
533+
534+ g_deny = True
535+ pdm = _get_pdm ()
536+ while pdm .is_busy ():
537+ time .sleep (1 )
538+ os .system ("/bin/bash /home/pi/omnipy/scripts/pi-update.sh" )
539+ return {"update started" : time .time ()}
540+
541+
514542@app .route ("/" )
515543def main_page ():
516544 try :
517545 return app .send_static_file ("omnipy.html" )
518546 except :
519547 logger .exception ("Error while serving root file" )
520548
521-
522549@app .route ('/content/<path:path>' )
523550def send_content (path ):
524551 try :
@@ -610,12 +637,18 @@ def a19():
610637def a20 ():
611638 return _api_result (lambda : archive_pod (), "Failure while archiving pod" )
612639
640+ @app .route (REST_URL_OMNIPY_UPDATE )
641+ def a20 ():
642+ return _api_result (lambda : update_omnipy (), "Failure while archiving pod" )
643+
644+
613645def _run_flask ():
614646 try :
615647 app .run (host = '0.0.0.0' , port = 4444 , debug = True , use_reloader = False )
616648 except :
617649 logger .exception ("Error while running rest api, exiting" )
618650
651+
619652def _exit_with_grace ():
620653 try :
621654 global g_deny
@@ -630,6 +663,7 @@ def _exit_with_grace():
630663
631664 exit (0 )
632665
666+
633667if __name__ == '__main__' :
634668 logger .info ("Rest api is starting" )
635669
0 commit comments