Skip to content

Commit d0901c5

Browse files
authored
fix music packages upload, fix onboard tests, bump and remove requirements (#188)
* Fix/refactor music packages update (#186) * Fix onboard tests * Fix/remove bump requirements (#187) * Remove and bump requirements
1 parent 57d3272 commit d0901c5

File tree

9 files changed

+108
-119
lines changed

9 files changed

+108
-119
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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

Lines changed: 14 additions & 21 deletions
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

Lines changed: 6 additions & 9 deletions
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 renamed to coderbot/runtime_test.py

Lines changed: 11 additions & 10 deletions
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

Lines changed: 30 additions & 21 deletions
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.

docker/stub/requirements.txt

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,26 @@
11
# API framework
2-
connexion==2.14.1
3-
Flask==2.2.2
2+
connexion==2.14.2
3+
Flask==2.2.3
44
Flask-Cors==3.0.10
5-
itsdangerous==2.1.2
6-
jsonschema==4.15.0
7-
Markdown==3.4.1
8-
MarkupSafe==2.1.1
9-
pyyaml==6.0
10-
requests==2.28.1
11-
swagger-spec-validator==2.7.6
12-
tinydb==4.7.0
13-
urllib3==1.26.12
14-
Werkzeug==2.2.2
5+
tinydb==4.7.1
6+
Werkzeug==2.2.3
157

168
# Misc utils
17-
setuptools==65.3.0
18-
certifi==2022.5.18.1
19-
absl-py==1.2.0
20-
chardet==3.0.4
21-
click==8.1.3
22-
clickclick==20.10.2
23-
gast==0.5.3
24-
idna==3.3
25-
pybind11==2.10.0
26-
inflection==0.5.1
9+
setuptools==67.4.0
2710
event-channel==0.4.3
28-
pytz==2022.2.1
11+
12+
# IO extensions
13+
spidev==3.5
2914

3015
# Audio
3116
sox==1.4.1
3217

3318
# Computer Vision
3419
grpcio==1.48.1
35-
numpy==1.23.2
36-
Pillow==9.2.0
37-
protobuf==4.21.8
20+
numpy==1.24.2
21+
Pillow==9.4.0
22+
protobuf==4.22.0
3823
opencv-contrib-python==4.5.5.62
39-
tflite-runtime==2.10.0
24+
tflite-runtime==2.11.0
4025
pytesseract==0.3.10
4126
pyzbar==0.1.9
42-
43-
#I/O
44-
spidev==3.5

requirements.txt

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
11
# API framework
2-
connexion==2.14.1
3-
Flask==2.2.2
2+
connexion==2.14.2
3+
Flask==2.2.3
44
Flask-Cors==3.0.10
5-
itsdangerous==2.1.2
6-
jsonschema==4.15.0
7-
Markdown==3.4.1
8-
MarkupSafe==2.1.1
9-
pyyaml==6.0
10-
requests==2.28.1
11-
swagger-spec-validator==2.7.6
12-
tinydb==4.7.0
13-
urllib3==1.26.12
14-
Werkzeug==2.2.2
5+
tinydb==4.7.1
6+
Werkzeug==2.2.3
157

168
# Misc utils
17-
setuptools==65.3.0
18-
certifi==2022.5.18.1
19-
absl-py==1.2.0
20-
chardet==3.0.4
21-
click==8.1.3
22-
clickclick==20.10.2
23-
gast==0.5.3
24-
idna==3.3
25-
pybind11==2.10.0
26-
inflection==0.5.1
9+
setuptools==67.4.0
2710
event-channel==0.4.3
28-
pytz==2022.2.1
2911

3012
# IO extensions
3113
pigpio==1.78
@@ -39,11 +21,11 @@ pyalsaaudio==0.9.2
3921

4022
# Computer Vision
4123
grpcio==1.48.1
42-
numpy==1.23.2
43-
Pillow==9.2.0
44-
protobuf==4.21.8
24+
numpy==1.24.2
25+
Pillow==9.4.0
26+
protobuf==4.22.0
4527
opencv-contrib-python==4.5.5.62
46-
tflite-runtime==2.10.0
28+
tflite-runtime==2.11.0
4729
pytesseract==0.3.10
4830
picamera==1.13
4931
pyzbar==0.1.9

test/music/snake/audio.wav

671 KB
Binary file not shown.

0 commit comments

Comments
 (0)