diff --git a/README.md b/README.md index 417fdbcb..5d803f11 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # backend -[![Build CoderBot backend](https://github.com/CoderBotOrg/backend/actions/workflows/build_backend.yml/badge.svg) +![Build CoderBot backend](https://github.com/CoderBotOrg/backend/actions/workflows/build_backend.yml/badge.svg) > CoderBot is a RaspberryPI-based programmable robot for educational purposes. Check the [project website](https://www.coderbot.org) for more information. > diff --git a/coderbot/api.py b/coderbot/api.py index 3cfc8432..88052aff 100644 --- a/coderbot/api.py +++ b/coderbot/api.py @@ -18,7 +18,7 @@ from audio import Audio from camera import Camera from cnn.cnn_manager import CNNManager -from coderbotTestUnit import run_test as runCoderbotTestUnit +from runtime_test import run_test from musicPackages import MusicPackageManager from program import Program, ProgramEngine @@ -262,21 +262,19 @@ def addMusicPackage(): """ Add a musical package an save the list of available packages on disk also add sounds and directory + zipName = request.args.get("zipname") """ - """zipName = request.args.get("zipname") - """ - file_to_upload = connexion.request.files['file_to_upload'] - print("adding " +str(file_to_upload)) - print("adding " + file_to_upload.filename) - file_to_upload.save(os.path.join('./updatePackages/', file_to_upload.filename)) - musicPkg = MusicPackageManager.get_instance() - response = musicPkg.addPackage(file_to_upload.filename) - if response == 1: - return 200 - elif response == 2: - return 400 - elif response == 3: - return 400 + try: + file_to_upload = connexion.request.files['file_to_upload'] + logging.info("adding " + file_to_upload.filename) + file_to_upload.save(os.path.join('/tmp/', file_to_upload.filename)) + music_pkg = MusicPackageManager.get_instance() + music_pkg.addPackage(file_to_upload.filename) + return "{}", 200 + except ValueError: + return "{}", 409 + except Exception: + return "{}", 400 def deleteMusicPackage(name): """ @@ -376,12 +374,7 @@ def resetDefaultPrograms(): ## Test def testCoderbot(body): - # taking first JSON key value (varargin) - if len(body.keys()) > 0: - tests_state = runCoderbotTestUnit(body[list(body.keys())[0]]) - return tests_state - else: - return 404 + return run_test(body.get("tests", [])) def listCNNModels(): cnn = CNNManager.get_instance() diff --git a/coderbot/musicPackages.py b/coderbot/musicPackages.py index 051b8738..e671b7e7 100644 --- a/coderbot/musicPackages.py +++ b/coderbot/musicPackages.py @@ -184,18 +184,16 @@ def addPackage(self, filename): if not self.verifyVersion(pkgname, version): if (version == self.packages[pkgname].getVersion()): logging.error("errore, il pacchetto " + pkgname + " ha versione identica a quello attualmente installato") - return 3 + raise ValueError() else: logging.info("errore, il pacchetto " + pkgname + " ha versione precendente a quello attualmente installato") - return 2 + raise ValueError() else: - - os.system('unzip -o ' + './updatePackages/' + filename + " -d ./updatePackages") - + os.system('unzip -o ' + '/tmp/' + filename + " -d /tmp") os.system('mkdir ' + pkgpath) - os.system('mv ./updatePackages/' + pkgname + "/" + 'audio.wav ' + pkgpath + '/') + os.system('mv /tmp/' + pkgname + "/" + 'audio.wav ' + pkgpath + '/') - with open('./updatePackages/' + pkgname + '/' + pkgname + '.json') as json_file: + with open('/tmp/' + pkgname + '/' + pkgname + '.json') as json_file: logging.info("adding " + pkgname + " package") data = json.load(json_file) for p in data['packages']: @@ -210,8 +208,7 @@ def addPackage(self, filename): self.updatePackages() - os.system('rm -rf ./updatePackages/' + pkgname) - return 1 + os.system('rm -rf /tmp/' + pkgname) def isPackageAvailable(self,namePackage): diff --git a/coderbot/coderbotTestUnit.py b/coderbot/runtime_test.py similarity index 86% rename from coderbot/coderbotTestUnit.py rename to coderbot/runtime_test.py index e397c14a..4a6260a5 100644 --- a/coderbot/coderbotTestUnit.py +++ b/coderbot/runtime_test.py @@ -11,6 +11,7 @@ If a test passes for correspondent component, a 1 is returned. If no test was executed on that component, 0 is preserved. """ +import logging from coderbot import CoderBot # Single components tests @@ -20,70 +21,70 @@ def __test_encoder(): try: c = CoderBot.get_instance() # moving both wheels at speed 100 clockwise - print("moving both wheels at speed 100 clockwise") + logging.info("moving both wheels at speed 100 clockwise") assert(c.speed() == 0) c.move(speed=100, elapse=2) assert(c.distance() != 0) assert (c.speed() == 0) # moving both wheels at speed 40 clockwise - print("moving both wheels at speed 40 clockwise") + logging.info("moving both wheels at speed 40 clockwise") assert(c.speed() == 0) c.move(speed=40, elapse=2) assert(c.distance() != 0) assert (c.speed() == 0) # moving both wheels at speed 100 counter-clockwise - print("moving both wheels at speed 100 counter-clockwise") + logging.info("moving both wheels at speed 100 counter-clockwise") assert(c.speed() == 0) c.move(speed=-100, elapse=2) assert(c.distance() != 0) assert (c.speed() == 0) # moving both wheels at speed 40 counter-clockwise - print("moving both wheels at speed 40 counter-clockwise") + logging.info("moving both wheels at speed 40 counter-clockwise") assert(c.speed() == 0) c.move(speed=-40, elapse=2) assert(c.distance() != 0) assert (c.speed() == 0) # moving forward - print("moving forward") + logging.info("moving forward") assert(c.speed() == 0) c.forward(speed=100, elapse=2) assert(c.distance() != 0) assert (c.speed() == 0) # moving backwards - print("moving backwards") + logging.info("moving backwards") assert(c.speed() == 0) c.backward(speed=100, elapse=2) assert(c.distance() != 0) assert (c.speed() == 0) # moving forward for 1 meter - print("moving forward for 1 meter") + logging.info("moving forward for 1 meter") assert(c.speed() == 0) c.forward(speed=100, distance=1000) assert(c.distance() != 0) assert (c.speed() == 0) # moving backwards for 1 meter - print("moving backwards for 1 meter") + logging.info("moving backwards for 1 meter") assert(c.speed() == 0) c.backward(speed=100, distance=1000) assert(c.distance() != 0) assert (c.speed() == 0) # turning left - print("turning left") + logging.info("turning left") assert(c.speed() == 0) c.left(speed=100, elapse=2) assert(c.distance() != 0) assert (c.speed() == 0) # turning right - print("turning right") + logging.info("turning right") assert(c.speed() == 0) c.right(speed=100, elapse=2) assert(c.distance() != 0) diff --git a/coderbot/v1.yml b/coderbot/v1.yml index 210ddf7b..a3c8872f 100644 --- a/coderbot/v1.yml +++ b/coderbot/v1.yml @@ -359,6 +359,23 @@ paths: description: "ok" tags: - Music extensions + post: + operationId: "api.addMusicPackage" + summary: "Add Music Package" + requestBody: + description: Add a Music Package + required: true + content: + application/x-www-form-urlencoded: + schema: + type: object + tags: + - System operations + responses: + 200: + description: "ok" + 400: + description: "upload failed" /music/packages/{name}: delete: operationId: "api.deleteMusicPackage" @@ -376,25 +393,7 @@ paths: description: "ok" 400: description: "not found" - - /system/update: - post: - operationId: "api.updateFromPackage" - summary: "Update CoderBot from package" - requestBody: - description: Update Activity - required: true - content: - application/x-www-form-urlencoded: - schema: - type: object - tags: - - System operations - responses: - 200: - description: "ok" - 400: - description: "upload failed" + /system/status: get: operationId: "api.get_status" @@ -404,6 +403,7 @@ paths: responses: 200: description: "Bot status" + /system/test: post: summary: Tests CoderBot components. @@ -411,15 +411,23 @@ paths: tags: - System operations requestBody: - description: Update Activity + description: Performs onboard tests required: true content: - application/x-www-form-urlencoded: + application/json: schema: type: object + properties: + tests: + type: array + items: + type: string responses: 200: description: Test ended. + 400: + description: Invalid input. + /system/info: get: operationId: "api.get_info" @@ -439,6 +447,7 @@ paths: responses: 200: description: "Successfully stopped the motors" + /control/move: post: summary: Moves the bot forward or backward. diff --git a/test/music/snake/audio.wav b/test/music/snake/audio.wav new file mode 100644 index 00000000..39775e14 Binary files /dev/null and b/test/music/snake/audio.wav differ diff --git a/test/music/snake/snake.json b/test/music/snake/snake.json new file mode 100644 index 00000000..476eebe6 --- /dev/null +++ b/test/music/snake/snake.json @@ -0,0 +1,25 @@ +{ + "packages": { + "snake": { + "category": "animal", + "name_IT": "serpente", + "name_EN": "snake", + "version": "0.1", + "date": "2020-06-01", + "interface": { + "base": { + "available": "TRUE", + "icon": "snake.png" + }, + "intermediate": { + "available": "TRUE", + "icon": "snake.png" + }, + "advanced": { + "available": "TRUE", + "icon": "snake.png" + } + } + } + } +} \ No newline at end of file