Skip to content

Refactor api v3 #167

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 128 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
d4b7eee
wip #164
Aug 21, 2022
c582ee1
wip #164
previ Aug 21, 2022
24ab82a
wip #164
previ Aug 22, 2022
157a5a2
wip #164
previ Aug 22, 2022
739aeec
wip #164
previ Aug 22, 2022
191df8b
wip #164
previ Aug 22, 2022
80f26cb
merge remove_old_ui
previ Aug 22, 2022
caa85dc
merge remove_old_ui
previ Aug 22, 2022
d677736
merge remove_old_ui
previ Aug 24, 2022
06f61b2
fix merge
previ Aug 24, 2022
834f96d
wip #164
previ Sep 2, 2022
5a24396
wip #164
previ Sep 10, 2022
aa262c3
wip #164
previ Sep 10, 2022
1a556fe
wip #164
previ Sep 10, 2022
9dd82d6
wip #164
previ Sep 12, 2022
7b13eb7
wip #164
previ Sep 18, 2022
4c2649b
wip #164
previ Sep 18, 2022
f12fcfc
Merge branch 'refactor_api_v3' of https://github.com/CoderBotOrg/back…
previ Sep 18, 2022
6e3c210
wip #164
previ Sep 18, 2022
f045ae9
wip #164
previ Sep 18, 2022
84b1f40
wip #164
previ Sep 18, 2022
9806fe4
wip #164
previ Sep 18, 2022
a4eb24c
wip #164
previ Sep 21, 2022
1a799d8
wip #164
previ Sep 23, 2022
78ca38f
wip #164
previ Sep 23, 2022
a4a75de
wip #164
previ Sep 24, 2022
8be5af0
wip #164
previ Sep 24, 2022
de56046
wip #164
previ Sep 24, 2022
8917d89
wip #164
previ Sep 24, 2022
0ed383a
wip #164
previ Sep 24, 2022
ef3b307
wip #164
previ Sep 24, 2022
564be4f
wip #164
previ Sep 24, 2022
374c760
wip #164
previ Sep 24, 2022
58a343b
wip #164
previ Sep 24, 2022
b3ffdb5
wip #164
previ Sep 24, 2022
8cd3942
wip #164
previ Sep 24, 2022
f07bde3
wip #164
previ Sep 24, 2022
e1dbba1
wip #164
previ Sep 24, 2022
b052a1c
wip #164
previ Sep 24, 2022
d2cc499
wip #164
previ Sep 30, 2022
3858e87
wip #164
previ Sep 30, 2022
cc41784
wip #164
previ Oct 1, 2022
f45fe0b
wip #164
previ Oct 1, 2022
f48f454
wip #164
previ Oct 1, 2022
651ed1d
wip #164
previ Oct 3, 2022
552c04c
wip #164
previ Oct 3, 2022
154212b
wip #164
previ Oct 3, 2022
d78b12d
wip #164
previ Oct 3, 2022
0d0328a
wip #164
previ Oct 3, 2022
714b5da
wip #164
previ Oct 3, 2022
f2afc19
wip #164
previ Oct 3, 2022
81c2053
wip #164
previ Oct 4, 2022
537c16d
wip #164
previ Oct 4, 2022
76bc708
wip #164
previ Oct 4, 2022
caa98d0
wip #164
previ Oct 4, 2022
0f61760
wip #164
previ Oct 4, 2022
bb6ad58
wip #164
previ Oct 4, 2022
19d44bb
wip #164
previ Oct 5, 2022
77dad87
wip #164
previ Oct 6, 2022
7387537
wip #164
previ Oct 15, 2022
49622f5
wip #164
previ Oct 15, 2022
27a029f
wip #164
previ Oct 15, 2022
000d819
wip #164
previ Oct 15, 2022
3df1918
wip #164
previ Oct 15, 2022
17d0d29
WIP #164
previ Oct 15, 2022
a4d8a8b
wip #164
previ Oct 15, 2022
140c0b0
wip #164
previ Oct 15, 2022
0aa6635
wip #164
previ Oct 15, 2022
34ca5b3
wip #164
previ Oct 15, 2022
05e6c99
wip #164
previ Oct 15, 2022
a7c0c48
wip #164
previ Oct 15, 2022
cc244bd
wip #164
previ Oct 15, 2022
a5ac40a
wip #164
previ Oct 15, 2022
deb3d4e
wip #164
previ Oct 15, 2022
2b4ed11
wip #164
previ Oct 15, 2022
b563297
wip #164
previ Oct 15, 2022
9004cf6
wip #164
previ Oct 15, 2022
0996b71
wip #164
previ Oct 16, 2022
72c5c7f
wip #164
previ Oct 16, 2022
afb3e20
wip #164
previ Oct 16, 2022
7245366
wip #164
previ Oct 16, 2022
a4bf5b3
wip #164
previ Oct 16, 2022
ac0942a
wip #164
previ Oct 16, 2022
ba569d7
wop #164
previ Oct 16, 2022
9ea27ee
wip #164
previ Oct 16, 2022
4880941
wip #164
previ Oct 16, 2022
29f6cf6
wip #164
previ Oct 16, 2022
0dc2b2c
wip #164
previ Oct 16, 2022
de2a878
wip #164
previ Oct 16, 2022
748ebba
wip #164
previ Oct 16, 2022
b2b1366
wip #184
previ Oct 16, 2022
2515002
wip #164
previ Oct 16, 2022
2b8d45f
wip #164
previ Oct 16, 2022
4ae5f95
wip #164
previ Oct 16, 2022
89e3e66
wip #164
previ Oct 16, 2022
e4899ae
wip #164
previ Oct 16, 2022
f44ece9
wip #164
previ Oct 18, 2022
454bc27
wip #164
previ Oct 18, 2022
a1688fd
wip #164
previ Oct 18, 2022
893e925
wip #164
previ Oct 18, 2022
ce29c09
wip #164
previ Oct 18, 2022
c3ef5a5
wip #164
previ Oct 18, 2022
1a9a095
wip #164
previ Oct 18, 2022
3462482
wip #164
previ Oct 18, 2022
e812592
wip #164
previ Oct 18, 2022
35913cf
wip #164
previ Oct 18, 2022
561d1fe
wip #164
previ Oct 18, 2022
c32d4ba
wip #164
previ Oct 22, 2022
cd07d87
wip #164
previ Oct 22, 2022
1f72632
wip #164
previ Oct 22, 2022
019e7a4
wip #164
previ Oct 22, 2022
63854d7
wip #164
previ Oct 23, 2022
4fb2dbf
wip #164
previ Oct 23, 2022
cccf500
wip #164
previ Oct 23, 2022
88d2d4d
wip #164
previ Oct 24, 2022
5f37b24
wip #164
previ Oct 24, 2022
43f4313
wip #164
previ Oct 24, 2022
12bf879
wip #164
previ Oct 25, 2022
92d6440
wip #164
previ Oct 25, 2022
f44c5c0
wip #164
previ Oct 25, 2022
404dca0
wip #164
previ Oct 25, 2022
ed840b3
wip #164
previ Oct 25, 2022
4fda410
wip #164
previ Oct 25, 2022
cd1ece5
wip #164
previ Oct 25, 2022
40d1d6a
wip #164
previ Oct 25, 2022
bc90663
wip #164
previ Oct 25, 2022
4f4131c
Merge branch 'develop' into refactor_api_v3
previ Oct 25, 2022
fb8e6fa
wip #164
previ Oct 25, 2022
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
11 changes: 8 additions & 3 deletions .github/workflows/build_backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@ jobs:
#python3 -m unittest test/cnn_test.py 2>&1 | tee test-reports/test_report.txt
echo "test complete"
- run: |
export PYTHONPATH=./coderbot:./stub:./test
export PYTHONPATH=./stub:./coderbot:./test
python3 coderbot/main.py > coderbot.log &
sleep 30
apt-get install -y python3-venv
mkdir -p schemathesis
python3 -m venv schemathesis
. schemathesis/bin/activate
pip install schemathesis
st run --endpoint '^(?!(\/api\/v1\/video\/stream|\/api\/v1\/control\/move|\/api\/v1\/video\/rec|\/api\/v1\/video\/stop)$).*$' --hypothesis-max-examples=10 --request-timeout=20 http://localhost:5000/api/v1/openapi.json
st run --endpoint 'activities' --hypothesis-max-examples=10 --request-timeout=20 http://localhost:5000/api/v1/openapi.json
st run --endpoint 'media' --hypothesis-max-examples=10 --request-timeout=20 http://localhost:5000/api/v1/openapi.json
st run --endpoint 'control/speak' --hypothesis-max-examples=10 --request-timeout=20 http://localhost:5000/api/v1/openapi.json
st run --endpoint 'control/stop' --hypothesis-max-examples=10 --request-timeout=20 http://localhost:5000/api/v1/openapi.json
st run --endpoint 'music' --hypothesis-max-examples=10 --request-timeout=20 http://localhost:5000/api/v1/openapi.json
st run --endpoint 'programs' --hypothesis-max-examples=10 --request-timeout=20 http://localhost:5000/api/v1/openapi.json
echo "openapi test complete"

release-backend:
#needs: [test]
needs: [test]
runs-on: ubuntu-latest
steps:
- name: Docker meta
Expand Down
16 changes: 6 additions & 10 deletions coderbot/activity.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from tinydb import TinyDB, Query
from tinydb.operations import delete
import json

# Programs and Activities databases
class Activities():
_instance = None

@classmethod
def get_instance(cls):
if cls._instance == None:
if cls._instance is None:
cls._instance = Activities()
return cls._instance

Expand All @@ -22,15 +20,14 @@ def load(self, name, default):
if len(activities) > 0:
return activities[0]
elif default is not None:
default_Activities = self.activities.search(self.query.default == True)
if len(self.activities.search(self.query.default == True)) > 0:
return self.activities.search(self.query.default == True)[0]
else:
return None
return None
return None

def save(self, name, activity):
# if saved activity is "default", reset existing default activity to "non-default"
if activity.get("default", False) == True:
if activity.get("default", False) is True:
self.activities.update({'default': False})
if self.activities.search(self.query.name == name) == []:
self.activities.insert(activity)
Expand All @@ -41,10 +38,9 @@ def delete(self, name):
activities = self.activities.search(self.query.name == name)
if len(activities) > 0:
activity = activities[0]
if activity.get("default", False) == True:
if activity.get("default", False) is True:
self.activities.update({'default': True}, self.query.stock == True)
self.activities.remove(self.query.name == activity["name"])

def list(self):
return self.activities.all()

50 changes: 27 additions & 23 deletions coderbot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from audio import Audio
from event import EventManager
from coderbotTestUnit import run_test as runCoderbotTestUnit
from balena import Balena

BUTTON_PIN = 16

Expand Down Expand Up @@ -95,7 +96,7 @@ def get_info():
backend_commit = "undefined"
coderbot_version = "undefined"
update_status = "ok"
kernel = 'undefined'
device = {}
motors = 'undefined'

try:
Expand All @@ -107,11 +108,6 @@ def get_info():
except Exception:
pass

try:
kernel = subprocess.check_output(["uname", "-r"]).decode('utf-8').replace('\n', '')
except Exception:
pass

try:
encoder = bool(Config.read().get('encoder'))
if(encoder):
Expand All @@ -123,12 +119,16 @@ def get_info():

serial = get_serial()

return {'backend_commit': backend_commit,
'coderbot_version': coderbot_version,
'update_status': update_status,
'kernel': kernel,
'serial': serial,
'motors': motors}
try:
device = Baleba.get_instance().device()
except Exception:
pass
return { 'backend_commit': device.get("commit"),
'coderbot_version': coderbot_version,
'update_status': device.get("status"),
'kernel': device.get("os_version"),
'serial': serial,
'motors': motors }

prog = None
prog_engine = ProgramEngine.get_instance()
Expand Down Expand Up @@ -161,21 +161,24 @@ def turn(body):
def takePhoto():
try:
cam.photo_take()
Audio.say(config.get("sound_shutter"))
audio_device.say(config.get("sound_shutter"))
return 200
except Exception as e:
logging.warning("Error: %s", e)

def recVideo():
try:
cam.video_rec()
audio_device.say(config.get("sound_shutter"))
return 200
except Exception as e:
logging.warning("Error: %s", e)

def stopVideo():
try:
cam.video_stop()
audio_device.say(config.get("sound_shutter"))
return 200
except Exception as e:
logging.warning("Error: %s", e)

Expand All @@ -184,24 +187,24 @@ def speak(body):
locale = body.get("locale", "")
logging.debug("say: " + text + " in: " + locale)
audio_device.say(text, locale)
return 200

def reset():
logging.debug("reset bot")
shutil.rmtree("data/*")
bot.restart()
Balena.get_instance().purge()
return 200

def halt():
logging.debug("shutting down")
audio_device.say(what=config.get("sound_stop"))
bot.halt()
Balena.get_instance().shutdown()
return 200

def restart():
logging.debug("restarting bot")
bot.restart()
Balena.get_instance().restart()

def reboot():
logging.debug("rebooting")
bot.reboot()
audio_device.say(what=config.get("sound_stop"))
Balena.get_instance().reboot()
return 200

def video_stream(a_cam):
while True:
Expand Down Expand Up @@ -287,13 +290,14 @@ def info():

def restoreSettings():
Config.restore()
restart()
return restart()

def loadSettings():
return Config.get()

def saveSettings(body):
Config.write(body)
return 200

def updateFromPackage():
os.system('sudo bash /home/pi/clean-update.sh')
Expand Down
44 changes: 44 additions & 0 deletions coderbot/balena/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
from urllib.request import urlopen, Request
import json
import logging

class Balena():
_instance = None

@classmethod
def get_instance(cls):
if cls._instance is None:
cls._instance = Balena()
return cls._instance

def __init__(self):
self.supervisor_address = os.environ["BALENA_SUPERVISOR_ADDRESS"]
self.supervisor_key = os.environ["BALENA_SUPERVISOR_API_KEY"]
self.app_id_data = json.dumps({ "appId": os.environ["BALENA_APP_ID"] }).encode("utf-8")
self.headers = { 'Content-Type': 'application/json' }

def purge(self):
logging.debug("reset bot")
req = Request(f'{self.supervisor_address}/v1/purge?apikey={self.supervisor_key}', data=self.app_id_data, headers=self.headers, method='POST')
return urlopen(req).read()

def shutdown(self):
logging.debug("shutdown bot")
req = Request(f'{self.supervisor_address}/v1/shutdown?apikey={self.supervisor_key}', headers=self.headers, method='POST')
return urlopen(req).read()

def restart(self):
logging.debug("restarting bot")
req = Request(f'{self.supervisor_address}/v1/restart?apikey={self.supervisor_key}', data=self.app_id_data, headers=self.headers, method='POST')
return urlopen(req).read()

def reboot(self):
logging.debug("reboot bot")
req = Request(f'{self.supervisor_address}/v1/reboot?apikey={self.supervisor_key}', headers=self.headers, method='POST')
return urlopen(req).read()

def device(self):
logging.debug("reboot bot", f'{self.supervisor_address}get?apikey={self.supervisor_key}')
req = Request(f'{self.supervisor_address}/device?apikey={self.supervisor_key}', headers=self.headers, method='GET')
return json.load(urlopen(req))
12 changes: 1 addition & 11 deletions coderbot/coderbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import logging
import pigpio
import sonar
import hw.mpu
from hw import mpu
from rotary_encoder.wheelsaxel import WheelsAxel

# GPIO
Expand Down Expand Up @@ -268,13 +268,3 @@ def _cb_button(self, gpio, level, tick):
logging.info("pushed: %d, %d", level, tick)
cb()

def halt(self):
logging.info("halt requested")
pass

def restart(self):
sys.exit()

def reboot(self):
logging.info("reboot requested")

Empty file added coderbot/hw/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion coderbot/hw/mpu.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hw import lsm9ds1
from . import lsm9ds1
import time

class AccelGyroMag:
Expand Down
7 changes: 2 additions & 5 deletions coderbot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@
import os
import logging
import logging.handlers
import subprocess
import picamera
import connexion

from flask import (send_from_directory, redirect)

from flask_cors import CORS

from coderbot import CoderBot
from camera import Camera
from motion import Motion
from audio import Audio
from program import ProgramEngine, Program
from program import ProgramEngine
from config import Config
from cnn.cnn_manager import CNNManager
from event import EventManager
from coderbot import CoderBot

# Logging configuration
logger = logging.getLogger()
Expand Down
4 changes: 2 additions & 2 deletions coderbot/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import event
import music
import musicPackages
import hw.atmega328p
from hw.atmega328p import ATMega328

PROGRAM_PATH = "./data/"
PROGRAM_PREFIX = "program_"
Expand Down Expand Up @@ -66,7 +66,7 @@ def get_music():
return music.Music.get_instance(musicPackageManager)

def get_atmega():
return atmega328p.ATMega328.get_instance()
return ATMega328.get_instance()

class ProgramEngine:

Expand Down
2 changes: 1 addition & 1 deletion defaults/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"hw_version":"5",
"audio_volume_level":"100",
"wifi_mode":"ap",
"wifi_ssid":"coderbot_CHANGEMEATFIRSTRUN",
"wifi_ssid":"coderbot",
"wifi_psk":"coderbot",
"packages_installed":"",
"admin_password":"",
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ ADD docker/scripts/*.sh /tmp/.
RUN /tmp/install_generic_cnn_models.sh
RUN /tmp/install_lib_firmware.sh

ENTRYPOINT cd /coderbot && python3 coderbot/main.py
ENTRYPOINT cd /coderbot && modprobe i2c-dev && python3 coderbot/main.py
26 changes: 26 additions & 0 deletions stub/balena/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Balena():
_instance = None

@classmethod
def get_instance(cls):
if cls._instance is None:
cls._instance = Balena()
return cls._instance

def __init__(self):
pass

def purge(self):
return ""

def shutdown(self):
return ""

def restart(self):
return ""

def reboot(self):
return ""

def device(self):
return {}