Skip to content

Commit 4d61969

Browse files
committed
Document and comment api.py
1 parent fe1293c commit 4d61969

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

api.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
API methods implementation
3+
This file contains every method called by the API defined in v2.yml
24
"""
35

46
from flask import jsonify
@@ -22,7 +24,9 @@
2224
)
2325

2426
def get_serial():
25-
# Extract serial from cpuinfo file
27+
"""
28+
Extract serial from cpuinfo file
29+
"""
2630
cpuserial = "0000000000000000"
2731
try:
2832
f = open('/proc/cpuinfo','r')
@@ -37,6 +41,11 @@ def get_serial():
3741

3842
@cached(cache=TTLCache(maxsize=1, ttl=10))
3943
def get_status():
44+
"""
45+
Expose CoderBot status:
46+
temperature, uptime, and internet connectivity status.
47+
(Cached method)
48+
"""
4049
try:
4150
temp = os.popen("vcgencmd measure_temp").readline().replace("temp=","")
4251
except:
@@ -50,8 +59,12 @@ def get_status():
5059

5160
@cached(cache=TTLCache(maxsize=1, ttl=60))
5261
def get_info():
53-
# [:-2] strips out '\n' (cat)
62+
"""
63+
Expose informations about the CoderBot system.
64+
(Cached method)
65+
"""
5466
try:
67+
# manifest.json is generated while building/copying the backend
5568
with open('manifest.json', 'r') as f:
5669
metadata = json.load(f)
5770
backend_commit = metadata["backendCommit"][0:7]
@@ -81,27 +94,34 @@ def get_info():
8194
prog = None
8295
prog_engine = ProgramEngine.get_instance()
8396

97+
# Programs and Activities databases
8498
programs = TinyDB("data/programs.json")
8599
activities = TinyDB("data/activities.json")
86100

87101
query = Query()
88102

103+
## Robot control
104+
89105
def stop():
90106
bot.stop()
91107
return 200
92108

93-
94109
def move(data):
95110
print(data)
96111
bot.move(speed=data["speed"], elapse=data["elapse"])
97112
return 200
98113

99-
100114
def turn(data):
101115
print(data)
102116
bot.turn(speed=data["speed"], elapse=data["elapse"])
103117
return 200
104118

119+
def exec(data):
120+
prog = prog_engine.create(data["name"], data["code"])
121+
return json.dumps(prog.execute())
122+
123+
## System
124+
105125
def status():
106126
status = get_status()
107127

@@ -123,21 +143,22 @@ def info():
123143
"serial": info["serial"]
124144
}
125145

126-
127-
def exec(data):
128-
prog = prog_engine.create(data["name"], data["code"])
129-
return json.dumps(prog.execute())
130-
131-
132146
def restoreSettings():
133147
with open("data/defaults/config.json") as f:
134148
Config.write(json.loads(f.read()))
135149
bot_config = Config.get()
136150
return "ok"
137151

152+
def updateFromPackage():
153+
os.system('sudo bash /home/pi/clean-update.sh')
154+
file_to_upload = connexion.request.files['file_to_upload']
155+
file_to_upload.save(os.path.join('/home/pi/', 'update.tar'))
156+
os.system('sudo coderbot_update /home/pi/update.tar && sudo reboot')
157+
return 200
158+
138159

139-
## Programs
140160

161+
## Programs
141162

142163
def saveProgram(data, overwrite):
143164
print(overwrite)
@@ -156,11 +177,9 @@ def saveProgram(data, overwrite):
156177
else:
157178
return "askOverwrite"
158179

159-
160180
def loadProgram(name):
161181
return programs.search(query.name == name)[0], 200
162182

163-
164183
def deleteProgram(data):
165184
programs.remove(query.name == data["name"])
166185

@@ -180,30 +199,23 @@ def saveActivity(data):
180199
activities.update(data, query.name == data["name"])
181200
return 200
182201

183-
184202
def loadActivity(name):
185203
return activities.search(query.name == name)[0], 200
186204

187-
188205
def deleteActivity(data):
189206
activities.remove(query.name == data["name"])
190207

191208

192209
def listActivities():
193210
return activities.all()
194211

195-
# Delete everything but the defaults programs
196212
def resetDefaultPrograms():
213+
"""
214+
Delete everything but the default programs
215+
"""
197216
programs.purge()
198217
for filename in os.listdir("data/defaults/programs/"):
199218
if filename.endswith(".json"):
200219
with open("data/defaults/programs/" + filename) as p:
201220
q = p.read()
202-
programs.insert(json.loads(q))
203-
204-
def updateFromPackage():
205-
os.system('sudo bash /home/pi/clean-update.sh')
206-
file_to_upload = connexion.request.files['file_to_upload']
207-
file_to_upload.save(os.path.join('/home/pi/', 'update.tar'))
208-
os.system('sudo coderbot_update /home/pi/update.tar && sudo reboot')
209-
return 200
221+
programs.insert(json.loads(q))

0 commit comments

Comments
 (0)