13
13
from tinydb .operations import delete
14
14
import os
15
15
import subprocess
16
+ from cachetools import cached , TTLCache
16
17
17
18
bot_config = Config .get ()
18
19
bot = CoderBot .get_instance (
19
20
servo = (bot_config .get ("move_motor_mode" ) == "servo" ),
20
21
motor_trim_factor = float (bot_config .get ("move_motor_trim" , 1.0 )),
21
22
)
22
23
23
- def getserial ():
24
+ def get_serial ():
24
25
# Extract serial from cpuinfo file
25
26
cpuserial = "0000000000000000"
26
27
try :
@@ -34,6 +35,37 @@ def getserial():
34
35
35
36
return cpuserial
36
37
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
+
37
69
prog = None
38
70
prog_engine = ProgramEngine .get_instance ()
39
71
@@ -58,10 +90,9 @@ def turn(data):
58
90
bot .turn (speed = data ["speed" ], elapse = data ["elapse" ])
59
91
return 200
60
92
61
-
62
93
# Bot status (STUB)
63
94
def status ():
64
- internet_status = subprocess . check_output ([ "./scripts/check_conn.sh" ]). decode ( 'utf-8' ). replace ( ' \n ' , '' )
95
+ internet_status = get_internet_status ( )
65
96
66
97
return {
67
98
"status" : "ok" ,
@@ -72,33 +103,14 @@ def status():
72
103
73
104
# Hardware and software information (STUB)
74
105
def 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 ()
94
107
return {
95
108
"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" ]
102
114
}
103
115
104
116
0 commit comments