6
6
import os
7
7
import subprocess
8
8
import json
9
+ import logging
9
10
import connexion
10
- from tinydb import TinyDB , Query
11
- from tinydb .operations import delete
11
+ import pigpio
12
12
from cachetools import cached , TTLCache
13
13
from coderbot import CoderBot
14
14
from program import ProgramEngine , Program
15
15
from config import Config
16
+ from activity import Activities
16
17
from coderbotTestUnit import run_test as runCoderbotTestUnit
17
- import pigpio
18
+ from cnn_manager import CNNManager
18
19
from musicPackages import MusicPackageManager
19
20
20
21
BUTTON_PIN = 16
25
26
encoder = bool (bot_config .get ("encoder" ))
26
27
)
27
28
28
- query = Query ()
29
-
30
29
def get_serial ():
31
30
"""
32
31
Extract serial from cpuinfo file
@@ -108,8 +107,7 @@ def get_info():
108
107
prog = None
109
108
prog_engine = ProgramEngine .get_instance ()
110
109
111
- # Programs and Activities databases
112
- activities = TinyDB ("data/activities.json" )
110
+ activities = Activities .get_instance ()
113
111
114
112
## Robot control
115
113
@@ -133,7 +131,8 @@ def turn(data):
133
131
134
132
def exec (data ):
135
133
program = prog_engine .create (data ["name" ], data ["code" ])
136
- return json .dumps (program .execute ())
134
+ options = data ["options" ]
135
+ return json .dumps (program .execute (options ))
137
136
138
137
## System
139
138
@@ -173,14 +172,23 @@ def restoreSettings():
173
172
Config .get ()
174
173
return "ok"
175
174
175
+
176
176
def updateFromPackage ():
177
177
os .system ('sudo bash /home/pi/clean-update.sh' )
178
178
file_to_upload = connexion .request .files ['file_to_upload' ]
179
179
file_to_upload .save (os .path .join ('/home/pi/' , 'update.tar' ))
180
180
os .system ('sudo reboot' )
181
181
return 200
182
182
183
- def updatePackages ():
183
+ def listMusicPackages ():
184
+ """
185
+ list available music packages
186
+ """
187
+ musicPkg = MusicPackageManager .get_instance ()
188
+ response = musicPkg .listPackages ()
189
+ return json .dumps (response )
190
+
191
+ def updateMusicPackages ():
184
192
"""
185
193
Add a musical package an save the list of available packages on disk
186
194
also add sounds and directory
@@ -196,14 +204,23 @@ def updatePackages():
196
204
if response == 1 :
197
205
return 200
198
206
elif response == 2 :
199
- return 2
207
+ return 400
200
208
elif response == 3 :
201
- return 3
209
+ return 400
202
210
211
+ def deleteMusicPackage (package_data ):
212
+ """
213
+ Delete a musical package an save the list of available packages on disk
214
+ also delete package sounds and directory
215
+ """
216
+ musicPkg = MusicPackageManager .get_instance ()
217
+ musicPkg .deletePackage (package_data ['package_name' ])
218
+ return 200
203
219
204
220
## Programs
205
221
206
- def saveProgram (data , overwrite ):
222
+ def saveProgram (data ):
223
+ overwrite = data ["overwrite" ]
207
224
existing_program = prog_engine .load (data ["name" ])
208
225
if existing_program and not overwrite :
209
226
return "askOverwrite"
@@ -227,23 +244,17 @@ def listPrograms():
227
244
## Activities
228
245
229
246
def saveActivity (data ):
230
- data = data ["activity" ]
231
- if activities .search (query .name == data ["name" ]) == []:
232
- activities .insert (data )
233
- return 200
234
- else :
235
- activities .update (data , query .name == data ["name" ])
236
- return 200
247
+ activity = data ["activity" ]
248
+ activities .save (activity )
237
249
238
- def loadActivity (name ):
239
- return activities .search ( query . name == name )[ 0 ], 200
250
+ def loadActivity (name = None , default = None ):
251
+ return activities .load ( name , default )
240
252
241
253
def deleteActivity (data ):
242
- activities .remove (query .name == data ["name" ])
243
-
254
+ activities .delete (data ), 200
244
255
245
256
def listActivities ():
246
- return activities .all ()
257
+ return activities .list ()
247
258
248
259
def resetDefaultPrograms ():
249
260
"""
@@ -273,3 +284,9 @@ def testCoderbot(data):
273
284
# taking first JSON key value (varargin)
274
285
tests_state = runCoderbotTestUnit (data [list (data .keys ())[0 ]])
275
286
return tests_state
287
+
288
+ def list_cnn_models ():
289
+ cnn = CNNManager .get_instance ()
290
+ logging .info ("cnn_models_list" )
291
+ return json .dumps (cnn .get_models ())
292
+
0 commit comments