changed api according to specification

This commit is contained in:
Joel 2021-02-10 22:41:23 +01:00
parent 5e46e8068a
commit d468732040
No known key found for this signature in database
GPG key ID: BDDDBECD0808290E
6 changed files with 150 additions and 58 deletions

View file

@ -34,7 +34,7 @@ fpm -s python --python-bin python3 --python-pip pip3 --python-package-name-prefi
-d "python3-pigpio" \ -d "python3-pigpio" \
-d "python3-numpy" \ -d "python3-numpy" \
-d "ffmpeg" \ -d "ffmpeg" \
-v 0.0.4-20 -t deb setup.py -v 0.1.0-0 -t deb setup.py
# --deb-changelog changelog \ # --deb-changelog changelog \
# --deb-upstream-changelog changelog \ # --deb-upstream-changelog changelog \

View file

@ -1,4 +1,4 @@
from typing import Dict, Tuple from typing import Dict, Tuple, List
import requests import requests
import json import json
@ -59,6 +59,7 @@ class Seeding:
class Position: class Position:
"""Datastructure for holding a position """Datastructure for holding a position
""" """
def __init__(self, x, y, degrees): def __init__(self, x, y, degrees):
self.x = x self.x = x
self.y = y self.y = y
@ -68,46 +69,51 @@ class Position:
class DoubleElim: class DoubleElim:
"""Class used for communicating with double elimination api """Class used for communicating with double elimination api
""" """
@staticmethod @staticmethod
def get_position() -> Position: def get_position() -> Tuple[Position, int]:
"""Get position of the robot """Get position of the robot
:return: A Position object with robot position :return: A Position object with robot position
:rtype: Position :rtype: Tuple[Position, int]
""" """
response = json.loads(requests.get(API_URL_GET_POS).content) res = requests.get(API_URL_GET_POS)
Logging.get_logger().debug(f"DoubleElim.get_position = {response}") response = json.loads(res.content)
return Position(response["x"], response["y"], response["degrees"]) Logging.get_logger().debug(f"DoubleElim.get_position = {response}, status code = {res.status_code}")
return Position(response["x"], response["y"], response["degrees"]), res.status_code
@staticmethod @staticmethod
def get_opponent() -> Position: def get_opponent() -> Tuple[Position, int]:
"""Get position of the opponents robot """Get position of the opponents robot
:return: A Position object with opponents robot position :return: A Position object with opponents robot position
:rtype: Position :rtype: Tuple[Position, int]
""" """
response = json.loads(requests.get(API_URL_GET_OP).content) res = requests.get(API_URL_GET_OP)
Logging.get_logger().debug(f"DoubleElim.get_opponent = x:{response}") response = json.loads(res.content)
return Position(response["x"], response["y"], response["degrees"]) Logging.get_logger().debug(f"DoubleElim.get_opponent = x:{response}, status code = {res.status_code}")
return Position(response["x"], response["y"], response["degrees"]), res.status_code
@staticmethod @staticmethod
def get_goal() -> Position: def get_goal() -> Tuple[Position, int]:
"""Get position of the goal """Get position of the goal
:return: A Position object with x and y coordinates of the goal, rotation is always -1 :return: A Position object with x and y coordinates of the goal, rotation is always -1
:rtype: Position :rtype: Tuple[Position, int]
""" """
response = json.loads(requests.get(API_URL_GET_GOAL).content) res = requests.get(API_URL_GET_GOAL)
Logging.get_logger().debug(f"DoubleElim.get_goal = x:{response}") response = json.loads(res.content)
return Position(response["x"], response["y"], -1) Logging.get_logger().debug(f"DoubleElim.get_goal = {response}, status code = {res.status_code}")
return Position(response["x"], response["y"], -1), res.status_code
@staticmethod @staticmethod
def get_items() -> list: def get_items() -> Tuple[List[Dict], int]:
"""Get a list with all current items """Get a list with all current items
:return: A list will all items currently on the game field. Items are dictionaries that look like: {"id": 0} :return: A list will all items currently on the game field. Items are dictionaries that look like: {"id": 0, "x": 0, "y": 0}
:rtype: list :rtype: Tuple[List[Dict], int]
""" """
result = json.loads(requests.get(API_URL_GET_ITEMS).content) res = requests.get(API_URL_GET_ITEMS)
Logging.get_logger().debug(f"DoubleElim.get_items = {result}") response = json.loads(res.content)
return result Logging.get_logger().debug(f"DoubleElim.get_items = {response}, status code = {res.status_code}")
return response, res.status_code

View file

@ -1,4 +1,4 @@
__version__ = "0.0.4-20" __version__ = "0.1.0-0"
import compLib.LogstashLogging import compLib.LogstashLogging
import logging import logging

View file

@ -32,7 +32,11 @@ Calling Seeding API
from compLib.Api import Seeding from compLib.Api import Seeding
park = Seeding.get_park() park, code = Seeding.get_park()
if code == 403:
print(f"I am not in the correct zone to make that request!")
else:
park = park["id"]
print(f"I should move to parking position: {park}") print(f"I should move to parking position: {park}")
if park == 0: if park == 0:
@ -48,7 +52,8 @@ Calling Seeding API
# do something similar to park == 1.. # do something similar to park == 1..
success = Seeding.pay_park() success = Seeding.pay_park()
if success: # check which code the api returned
if success == 204:
print(f"We scored some points!") print(f"We scored some points!")
else: else:
print(f"We failed :(") print(f"We failed :(")
@ -60,8 +65,8 @@ Calling Double Elimination API
from compLib.Api import DoubleElim from compLib.Api import DoubleElim
position = DoubleElim.get_position() position, status = DoubleElim.get_position()
print(f"Position of my robot is: x={position.x}, y={position.y} and rotation is: {position.degrees}") print(f"Position of my robot is: x={position.x}, y={position.y} and rotation is: {position.degrees}, the server responded with status code: {status}")
goal = DoubleElim.get_goal() goal, status = DoubleElim.get_goal()
print(f"Goal is at: x={goal.x}, y={goal.y}") print(f"Goal is at: x={goal.x}, y={goal.y}, the server responded with status code: {status}")

View file

@ -12,7 +12,7 @@ else:
setuptools.setup( setuptools.setup(
name="complib", name="complib",
version="0.0.4-20", version="0.1.0-0",
author="F-WuTs", author="F-WuTs",
author_email="--", author_email="--",
description="", description="",

101
test.py
View file

@ -35,6 +35,19 @@ def change_api_state(park_id: int = 0, in_get_park: bool = False, was_in_park: b
requests.post("http://localhost:5000/api/api/updateState", json=data) requests.post("http://localhost:5000/api/api/updateState", json=data)
def change_double_elim_api_state(started: bool = False, goal: list = [0, 0], position: list = [0, 0, 0],
op_position: list = [0, 0, 0], items: list = []):
data = {
"started": started,
"goal": goal,
"position": position,
"opPosition": op_position,
"items": items
}
requests.post("http://localhost:5000/api/api/updateDoubleElimState", json=data)
class TestApiServer(unittest.TestCase): class TestApiServer(unittest.TestCase):
def test_get_park(self): def test_get_park(self):
ret, code = Api.Seeding.get_park() ret, code = Api.Seeding.get_park()
@ -70,7 +83,7 @@ class TestApiServer(unittest.TestCase):
ret = Api.Seeding.pay_park() ret = Api.Seeding.pay_park()
assert type(ret) is int assert type(ret) is int
assert ret == 201 assert ret == 204
def test_simon_says(self): def test_simon_says(self):
change_api_state(in_simon_zone=False) change_api_state(in_simon_zone=False)
@ -163,26 +176,94 @@ class TestApiServer(unittest.TestCase):
last_pos = next_pos last_pos = next_pos
def test_get_position(self): def test_get_position(self):
response = Api.DoubleElim.get_position() response, status = Api.DoubleElim.get_position()
assert type(status) is int
assert status == 503
assert type(response) == Api.Position assert type(response) == Api.Position
assert 0 <= response.x assert 0 == response.x
assert 0 <= response.y assert 0 == response.y
assert 0 <= response.degrees <= 360 assert 0 == response.degrees
change_double_elim_api_state(started=True, position=[50, 75, 90])
response, status = Api.DoubleElim.get_position()
assert type(status) is int
assert status == 200
assert type(response) == Api.Position
assert 50 == response.x
assert 75 == response.y
assert 90 == response.degrees
def test_get_opponent(self): def test_get_opponent(self):
response = Api.DoubleElim.get_opponent() response, status = Api.DoubleElim.get_opponent()
assert type(status) is int
assert status == 503
assert type(response) == Api.Position assert type(response) == Api.Position
assert 0 == response.x
assert 0 == response.y
assert 0 == response.degrees
change_double_elim_api_state(started=True, op_position=[50, 75, 90])
response, status = Api.DoubleElim.get_opponent()
assert type(status) is int
assert status == 200
assert type(response) == Api.Position
assert 50 == response.x
assert 75 == response.y
assert 90 == response.degrees
def test_get_goal(self): def test_get_goal(self):
response = Api.DoubleElim.get_goal() response, status = Api.DoubleElim.get_goal()
assert type(status) is int
assert status == 503
assert type(response) == Api.Position assert type(response) == Api.Position
assert 0 <= response.x assert 0 == response.x
assert 0 <= response.y assert 0 == response.y
assert response.degrees == -1
change_double_elim_api_state(started=True, goal=[50, 75])
response, status = Api.DoubleElim.get_goal()
assert type(status) is int
assert status == 200
assert type(response) == Api.Position
assert 50 == response.x
assert 75 == response.y
assert response.degrees == -1 assert response.degrees == -1
def test_get_items(self): def test_get_items(self):
response = Api.DoubleElim.get_items() response, status = Api.DoubleElim.get_items()
assert type(status) is int
assert status == 503
assert type(response) == list assert type(response) == list
assert response == []
change_double_elim_api_state(started=True)
response, status = Api.DoubleElim.get_items()
assert type(status) is int
assert status == 200
assert type(response) == list
assert response == []
change_double_elim_api_state(started=True, items=[{"id": 0, "x": 50, "y": 75}])
response, status = Api.DoubleElim.get_items()
assert type(status) is int
assert status == 200
assert type(response) == list
assert len(response) == 1
assert response == [{"id": 0, "x": 50, "y": 75}]
change_double_elim_api_state(started=True, items=[{"id": 0, "x": 50, "y": 75}, {"id": 2, "x": -50, "y": -75}])
response, status = Api.DoubleElim.get_items()
assert type(status) is int
assert status == 200
assert type(response) == list
assert len(response) == 2
assert response == [{"id": 0, "x": 50, "y": 75}, {"id": 2, "x": -50, "y": -75}]
def setUp(self) -> None: def setUp(self) -> None:
if START_SERVER: if START_SERVER: