Skip to content

Commit 00b6336

Browse files
authored
Fix/refactor music packages update (#186)
* refactor music packages, fix onboard tests
1 parent c23ae69 commit 00b6336

File tree

7 files changed

+87
-62
lines changed

7 files changed

+87
-62
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# backend
2-
[![Build CoderBot backend](https://github.com/CoderBotOrg/backend/actions/workflows/build_backend.yml/badge.svg)
2+
![Build CoderBot backend](https://github.com/CoderBotOrg/backend/actions/workflows/build_backend.yml/badge.svg)
33

44
> CoderBot is a RaspberryPI-based programmable robot for educational purposes. Check the [project website](https://www.coderbot.org) for more information.
55
>

coderbot/api.py

+14-21
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from audio import Audio
1919
from camera import Camera
2020
from cnn.cnn_manager import CNNManager
21-
from coderbotTestUnit import run_test as runCoderbotTestUnit
21+
from runtime_test import run_test
2222
from musicPackages import MusicPackageManager
2323
from program import Program, ProgramEngine
2424

@@ -262,21 +262,19 @@ def addMusicPackage():
262262
"""
263263
Add a musical package an save the list of available packages on disk
264264
also add sounds and directory
265+
zipName = request.args.get("zipname")
265266
"""
266-
"""zipName = request.args.get("zipname")
267-
"""
268-
file_to_upload = connexion.request.files['file_to_upload']
269-
print("adding " +str(file_to_upload))
270-
print("adding " + file_to_upload.filename)
271-
file_to_upload.save(os.path.join('./updatePackages/', file_to_upload.filename))
272-
musicPkg = MusicPackageManager.get_instance()
273-
response = musicPkg.addPackage(file_to_upload.filename)
274-
if response == 1:
275-
return 200
276-
elif response == 2:
277-
return 400
278-
elif response == 3:
279-
return 400
267+
try:
268+
file_to_upload = connexion.request.files['file_to_upload']
269+
logging.info("adding " + file_to_upload.filename)
270+
file_to_upload.save(os.path.join('/tmp/', file_to_upload.filename))
271+
music_pkg = MusicPackageManager.get_instance()
272+
music_pkg.addPackage(file_to_upload.filename)
273+
return "{}", 200
274+
except ValueError:
275+
return "{}", 409
276+
except Exception:
277+
return "{}", 400
280278

281279
def deleteMusicPackage(name):
282280
"""
@@ -376,12 +374,7 @@ def resetDefaultPrograms():
376374

377375
## Test
378376
def testCoderbot(body):
379-
# taking first JSON key value (varargin)
380-
if len(body.keys()) > 0:
381-
tests_state = runCoderbotTestUnit(body[list(body.keys())[0]])
382-
return tests_state
383-
else:
384-
return 404
377+
return run_test(body.get("tests", []))
385378

386379
def listCNNModels():
387380
cnn = CNNManager.get_instance()

coderbot/musicPackages.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,16 @@ def addPackage(self, filename):
184184
if not self.verifyVersion(pkgname, version):
185185
if (version == self.packages[pkgname].getVersion()):
186186
logging.error("errore, il pacchetto " + pkgname + " ha versione identica a quello attualmente installato")
187-
return 3
187+
raise ValueError()
188188
else:
189189
logging.info("errore, il pacchetto " + pkgname + " ha versione precendente a quello attualmente installato")
190-
return 2
190+
raise ValueError()
191191
else:
192-
193-
os.system('unzip -o ' + './updatePackages/' + filename + " -d ./updatePackages")
194-
192+
os.system('unzip -o ' + '/tmp/' + filename + " -d /tmp")
195193
os.system('mkdir ' + pkgpath)
196-
os.system('mv ./updatePackages/' + pkgname + "/" + 'audio.wav ' + pkgpath + '/')
194+
os.system('mv /tmp/' + pkgname + "/" + 'audio.wav ' + pkgpath + '/')
197195

198-
with open('./updatePackages/' + pkgname + '/' + pkgname + '.json') as json_file:
196+
with open('/tmp/' + pkgname + '/' + pkgname + '.json') as json_file:
199197
logging.info("adding " + pkgname + " package")
200198
data = json.load(json_file)
201199
for p in data['packages']:
@@ -210,8 +208,7 @@ def addPackage(self, filename):
210208

211209
self.updatePackages()
212210

213-
os.system('rm -rf ./updatePackages/' + pkgname)
214-
return 1
211+
os.system('rm -rf /tmp/' + pkgname)
215212

216213

217214
def isPackageAvailable(self,namePackage):

coderbot/coderbotTestUnit.py coderbot/runtime_test.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
If a test passes for correspondent component, a 1 is returned.
1212
If no test was executed on that component, 0 is preserved.
1313
"""
14+
import logging
1415
from coderbot import CoderBot
1516

1617
# Single components tests
@@ -20,70 +21,70 @@ def __test_encoder():
2021
try:
2122
c = CoderBot.get_instance()
2223
# moving both wheels at speed 100 clockwise
23-
print("moving both wheels at speed 100 clockwise")
24+
logging.info("moving both wheels at speed 100 clockwise")
2425
assert(c.speed() == 0)
2526
c.move(speed=100, elapse=2)
2627
assert(c.distance() != 0)
2728
assert (c.speed() == 0)
2829

2930
# moving both wheels at speed 40 clockwise
30-
print("moving both wheels at speed 40 clockwise")
31+
logging.info("moving both wheels at speed 40 clockwise")
3132
assert(c.speed() == 0)
3233
c.move(speed=40, elapse=2)
3334
assert(c.distance() != 0)
3435
assert (c.speed() == 0)
3536

3637
# moving both wheels at speed 100 counter-clockwise
37-
print("moving both wheels at speed 100 counter-clockwise")
38+
logging.info("moving both wheels at speed 100 counter-clockwise")
3839
assert(c.speed() == 0)
3940
c.move(speed=-100, elapse=2)
4041
assert(c.distance() != 0)
4142
assert (c.speed() == 0)
4243

4344
# moving both wheels at speed 40 counter-clockwise
44-
print("moving both wheels at speed 40 counter-clockwise")
45+
logging.info("moving both wheels at speed 40 counter-clockwise")
4546
assert(c.speed() == 0)
4647
c.move(speed=-40, elapse=2)
4748
assert(c.distance() != 0)
4849
assert (c.speed() == 0)
4950

5051
# moving forward
51-
print("moving forward")
52+
logging.info("moving forward")
5253
assert(c.speed() == 0)
5354
c.forward(speed=100, elapse=2)
5455
assert(c.distance() != 0)
5556
assert (c.speed() == 0)
5657

5758
# moving backwards
58-
print("moving backwards")
59+
logging.info("moving backwards")
5960
assert(c.speed() == 0)
6061
c.backward(speed=100, elapse=2)
6162
assert(c.distance() != 0)
6263
assert (c.speed() == 0)
6364

6465
# moving forward for 1 meter
65-
print("moving forward for 1 meter")
66+
logging.info("moving forward for 1 meter")
6667
assert(c.speed() == 0)
6768
c.forward(speed=100, distance=1000)
6869
assert(c.distance() != 0)
6970
assert (c.speed() == 0)
7071

7172
# moving backwards for 1 meter
72-
print("moving backwards for 1 meter")
73+
logging.info("moving backwards for 1 meter")
7374
assert(c.speed() == 0)
7475
c.backward(speed=100, distance=1000)
7576
assert(c.distance() != 0)
7677
assert (c.speed() == 0)
7778

7879
# turning left
79-
print("turning left")
80+
logging.info("turning left")
8081
assert(c.speed() == 0)
8182
c.left(speed=100, elapse=2)
8283
assert(c.distance() != 0)
8384
assert (c.speed() == 0)
8485

8586
# turning right
86-
print("turning right")
87+
logging.info("turning right")
8788
assert(c.speed() == 0)
8889
c.right(speed=100, elapse=2)
8990
assert(c.distance() != 0)

coderbot/v1.yml

+30-21
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,23 @@ paths:
359359
description: "ok"
360360
tags:
361361
- Music extensions
362+
post:
363+
operationId: "api.addMusicPackage"
364+
summary: "Add Music Package"
365+
requestBody:
366+
description: Add a Music Package
367+
required: true
368+
content:
369+
application/x-www-form-urlencoded:
370+
schema:
371+
type: object
372+
tags:
373+
- System operations
374+
responses:
375+
200:
376+
description: "ok"
377+
400:
378+
description: "upload failed"
362379
/music/packages/{name}:
363380
delete:
364381
operationId: "api.deleteMusicPackage"
@@ -376,25 +393,7 @@ paths:
376393
description: "ok"
377394
400:
378395
description: "not found"
379-
380-
/system/update:
381-
post:
382-
operationId: "api.updateFromPackage"
383-
summary: "Update CoderBot from package"
384-
requestBody:
385-
description: Update Activity
386-
required: true
387-
content:
388-
application/x-www-form-urlencoded:
389-
schema:
390-
type: object
391-
tags:
392-
- System operations
393-
responses:
394-
200:
395-
description: "ok"
396-
400:
397-
description: "upload failed"
396+
398397
/system/status:
399398
get:
400399
operationId: "api.get_status"
@@ -404,22 +403,31 @@ paths:
404403
responses:
405404
200:
406405
description: "Bot status"
406+
407407
/system/test:
408408
post:
409409
summary: Tests CoderBot components.
410410
operationId: "api.testCoderbot"
411411
tags:
412412
- System operations
413413
requestBody:
414-
description: Update Activity
414+
description: Performs onboard tests
415415
required: true
416416
content:
417-
application/x-www-form-urlencoded:
417+
application/json:
418418
schema:
419419
type: object
420+
properties:
421+
tests:
422+
type: array
423+
items:
424+
type: string
420425
responses:
421426
200:
422427
description: Test ended.
428+
400:
429+
description: Invalid input.
430+
423431
/system/info:
424432
get:
425433
operationId: "api.get_info"
@@ -439,6 +447,7 @@ paths:
439447
responses:
440448
200:
441449
description: "Successfully stopped the motors"
450+
442451
/control/move:
443452
post:
444453
summary: Moves the bot forward or backward.

test/music/snake/audio.wav

671 KB
Binary file not shown.

test/music/snake/snake.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"packages": {
3+
"snake": {
4+
"category": "animal",
5+
"name_IT": "serpente",
6+
"name_EN": "snake",
7+
"version": "0.1",
8+
"date": "2020-06-01",
9+
"interface": {
10+
"base": {
11+
"available": "TRUE",
12+
"icon": "snake.png"
13+
},
14+
"intermediate": {
15+
"available": "TRUE",
16+
"icon": "snake.png"
17+
},
18+
"advanced": {
19+
"available": "TRUE",
20+
"icon": "snake.png"
21+
}
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)