113 lines
3.6 KiB
Python
113 lines
3.6 KiB
Python
from typing import Dict, Tuple
|
|
|
|
import requests
|
|
import json
|
|
import os
|
|
|
|
from compLib.LogstashLogging import Logging
|
|
|
|
API_URL = os.getenv("API_URL", "http://localhost:5000/") + "api/"
|
|
API_URL_GET_POS = API_URL + "getPos"
|
|
API_URL_GET_OP = API_URL + "getOp"
|
|
API_URL_GET_GOAL = API_URL + "getGoal"
|
|
API_URL_GET_ITEMS = API_URL + "getItems"
|
|
API_URL_GET_PARK = API_URL + "getPark"
|
|
API_URL_PAY_PARK = API_URL + "payPark"
|
|
API_URL_SIMON_SAYS = API_URL + "simonSays"
|
|
|
|
|
|
class Seeding:
|
|
"""Class used for communicating with seeding api
|
|
"""
|
|
|
|
@staticmethod
|
|
def get_park() -> Tuple[Dict, int]:
|
|
"""Get a parkingsapce from the api.
|
|
|
|
:return: Json Object and status code as returned by the api.
|
|
:rtype: Tuple[Dict, int]
|
|
"""
|
|
res = requests.get(API_URL_GET_PARK)
|
|
result = json.loads(res.content)
|
|
Logging.get_logger().debug(f"Seeding.get_park = {result}, status code = {res.status_code}")
|
|
return result, res.status_code
|
|
|
|
@staticmethod
|
|
def pay_park() -> int:
|
|
"""Pay for parking.
|
|
|
|
:return: Status code as returned by the api.
|
|
:rtype: int
|
|
"""
|
|
result = requests.get(API_URL_PAY_PARK).status_code
|
|
Logging.get_logger().debug(f"Seeding.pay_park = {result}")
|
|
return result
|
|
|
|
@staticmethod
|
|
def simon_says() -> Tuple[Dict, int]:
|
|
"""Get next simon says zone from the api.
|
|
|
|
:return: Json Object and status code as returned by the api.
|
|
:rtype: Tuple[Dict, int]
|
|
"""
|
|
res = requests.get(API_URL_SIMON_SAYS)
|
|
result = json.loads(res.content)
|
|
Logging.get_logger().debug(f"Seeding.simon_says = {result}, status code = {res.status_code}")
|
|
return result, res.status_code
|
|
|
|
|
|
class Position:
|
|
"""Datastructure for holding a position
|
|
"""
|
|
def __init__(self, x, y, degrees):
|
|
self.x = x
|
|
self.y = y
|
|
self.degrees = degrees
|
|
|
|
|
|
class DoubleElim:
|
|
"""Class used for communicating with double elimination api
|
|
"""
|
|
@staticmethod
|
|
def get_position() -> Position:
|
|
"""Get position of the robot
|
|
|
|
:return: A Position object with robot position
|
|
:rtype: Position
|
|
"""
|
|
response = json.loads(requests.get(API_URL_GET_POS).content)
|
|
Logging.get_logger().debug(f"DoubleElim.get_position = {response}")
|
|
return Position(response["x"], response["y"], response["degrees"])
|
|
|
|
@staticmethod
|
|
def get_opponent() -> Position:
|
|
"""Get position of the opponents robot
|
|
|
|
:return: A Position object with opponents robot position
|
|
:rtype: Position
|
|
"""
|
|
response = json.loads(requests.get(API_URL_GET_OP).content)
|
|
Logging.get_logger().debug(f"DoubleElim.get_opponent = x:{response}")
|
|
return Position(response["x"], response["y"], response["degrees"])
|
|
|
|
@staticmethod
|
|
def get_goal() -> Position:
|
|
"""Get position of the goal
|
|
|
|
:return: A Position object with x and y coordinates of the goal, rotation is always -1
|
|
:rtype: Position
|
|
"""
|
|
response = json.loads(requests.get(API_URL_GET_GOAL).content)
|
|
Logging.get_logger().debug(f"DoubleElim.get_goal = x:{response}")
|
|
return Position(response["x"], response["y"], -1)
|
|
|
|
@staticmethod
|
|
def get_items() -> list:
|
|
"""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}
|
|
:rtype: list
|
|
"""
|
|
result = json.loads(requests.get(API_URL_GET_ITEMS).content)
|
|
Logging.get_logger().debug(f"DoubleElim.get_items = {result}")
|
|
return result
|