Skip to content

Fix/refactor music packages update #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
>
Expand Down
35 changes: 14 additions & 21 deletions coderbot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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()
Expand Down
15 changes: 6 additions & 9 deletions coderbot/musicPackages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']:
Expand All @@ -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):
Expand Down
21 changes: 11 additions & 10 deletions coderbot/coderbotTestUnit.py → coderbot/runtime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
51 changes: 30 additions & 21 deletions coderbot/v1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -404,22 +403,31 @@ paths:
responses:
200:
description: "Bot status"

/system/test:
post:
summary: Tests CoderBot components.
operationId: "api.testCoderbot"
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"
Expand All @@ -439,6 +447,7 @@ paths:
responses:
200:
description: "Successfully stopped the motors"

/control/move:
post:
summary: Moves the bot forward or backward.
Expand Down
Binary file added test/music/snake/audio.wav
Binary file not shown.
25 changes: 25 additions & 0 deletions test/music/snake/snake.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}