1313from tinydb .operations import delete
1414import os
1515import subprocess
16+ from cachetools import cached , TTLCache
1617
1718bot_config = Config .get ()
1819bot = CoderBot .get_instance (
1920 servo = (bot_config .get ("move_motor_mode" ) == "servo" ),
2021 motor_trim_factor = float (bot_config .get ("move_motor_trim" , 1.0 )),
2122)
2223
23- def getserial ():
24+ def get_serial ():
2425 # Extract serial from cpuinfo file
2526 cpuserial = "0000000000000000"
2627 try :
@@ -34,6 +35,37 @@ def getserial():
3435
3536 return cpuserial
3637
38+ @cached (cache = TTLCache (maxsize = 1 , ttl = 10 ))
39+ def get_internet_status ():
40+ return subprocess .check_output (["./scripts/check_conn.sh" ]).decode ('utf-8' ).replace ('\n ' , '' )
41+
42+ @cached (cache = TTLCache (maxsize = 1 , ttl = 60 ))
43+ def get_info ():
44+ # [:-2] strips out '\n' (cat)
45+ try :
46+ backend_commit = subprocess .check_output (["git" , "rev-parse" , "HEAD" ])[0 :7 ].decode ('utf-8' )
47+ except :
48+ backend_commit = 'undefined'
49+ try :
50+ coderbot_version = subprocess .check_output (["cat" , "/etc/coderbot/version" ]).decode ('utf-8' ).replace ('\n ' , '' )
51+ except :
52+ coderbot_version = 'undefined'
53+ try :
54+ kernel = subprocess .check_output (["uname" , "-r" ]).decode ('utf-8' ).replace ('\n ' , '' )
55+ except :
56+ kernel = 'undefined'
57+ try :
58+ update_status = subprocess .check_output (["cat" , "/etc/coderbot/update_status" ]).decode ('utf-8' ).replace ('\n ' , '' )
59+ except :
60+ update_status = 'undefined'
61+
62+ serial = get_serial ();
63+ return {'backend_commit' :backend_commit ,
64+ 'coderbot_version' :coderbot_version ,
65+ 'update_status' : update_status ,
66+ 'kernel' :kernel ,
67+ 'serial' :serial }
68+
3769prog = None
3870prog_engine = ProgramEngine .get_instance ()
3971
@@ -58,10 +90,9 @@ def turn(data):
5890 bot .turn (speed = data ["speed" ], elapse = data ["elapse" ])
5991 return 200
6092
61-
6293# Bot status (STUB)
6394def status ():
64- internet_status = subprocess . check_output ([ "./scripts/check_conn.sh" ]). decode ( 'utf-8' ). replace ( ' \n ' , '' )
95+ internet_status = get_internet_status ( )
6596
6697 return {
6798 "status" : "ok" ,
@@ -72,33 +103,14 @@ def status():
72103
73104# Hardware and software information (STUB)
74105def info ():
75- # [:-2] strips out '\n' (cat)
76- try :
77- backend_commit = subprocess .check_output (["git" , "rev-parse" , "HEAD" ])[0 :7 ].decode ('utf-8' )
78- except :
79- backend_commit = 'undefined'
80- try :
81- coderbot_version = subprocess .check_output (["cat" , "/etc/coderbot/version" ]).decode ('utf-8' ).replace ('\n ' , '' )
82- except :
83- coderbot_version = 'undefined'
84- try :
85- kernel = subprocess .check_output (["uname" , "-r" ]).decode ('utf-8' ).replace ('\n ' , '' )
86- except :
87- kernel = 'undefined'
88-
89- try :
90- update_status = subprocess .check_output (["cat" , "/etc/coderbot/update_status" ]).decode ('utf-8' ).replace ('\n ' , '' )
91- except :
92- update_status = 'undefined'
93-
106+ info = get_info ()
94107 return {
95108 "model" : 1 ,
96- "serial" : 2 ,
97- "version" : coderbot_version ,
98- "backend commit build" : backend_commit ,
99- "kernel" : kernel ,
100- "update status" : update_status ,
101- "serial" : getserial ()
109+ "version" : info ["coderbot_version" ],
110+ "backend commit build" : info ["backend_commit" ],
111+ "kernel" : info ["kernel" ],
112+ "update status" : info ["update_status" ],
113+ "serial" : info ["serial" ]
102114 }
103115
104116
0 commit comments