better logging
This commit is contained in:
@@ -60,7 +60,7 @@ class BaseBrain:
|
|||||||
self.response_to_chatui = msg
|
self.response_to_chatui = msg
|
||||||
else:
|
else:
|
||||||
msg = f"I will send this command: {command}"
|
msg = f"I will send this command: {command}"
|
||||||
logger.success(msg)
|
logger.info(msg)
|
||||||
self.response_to_chatui = msg
|
self.response_to_chatui = msg
|
||||||
self.command_handler.handle(command)
|
self.command_handler.handle(command)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import sys
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
from requests.exceptions import ConnectionError
|
from requests.exceptions import ConnectionError
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
from settings.config import settings
|
from settings.config import settings
|
||||||
|
|
||||||
@@ -13,52 +14,50 @@ class CommandHandler:
|
|||||||
self.COMMANDER_COMMANDS_URL = f"{settings.COMMANDER_ROOT_URL}/command"
|
self.COMMANDER_COMMANDS_URL = f"{settings.COMMANDER_ROOT_URL}/command"
|
||||||
self._check_commander_health()
|
self._check_commander_health()
|
||||||
|
|
||||||
|
def _parse_response(self, raw_resp):
|
||||||
|
json_resp = raw_resp.json()
|
||||||
|
if json_resp["msg"] == "ok": logger.success(json_resp)
|
||||||
|
else: logger.warning(json_resp["msg"])
|
||||||
|
|
||||||
def _check_commander_health(self):
|
def _check_commander_health(self):
|
||||||
try:
|
try:
|
||||||
response = requests.get(f"{settings.COMMANDER_ROOT_URL}/test/health")
|
response = requests.get(f"{settings.COMMANDER_ROOT_URL}/test/health")
|
||||||
status = response.json()
|
status = response.json()
|
||||||
except ConnectionError:
|
except ConnectionError:
|
||||||
print(f"commander service is unavailable: {settings.COMMANDER_ROOT_URL}"); sys.exit(1)
|
logger.error(f"commander service is unavailable: {settings.COMMANDER_ROOT_URL}"); sys.exit(1)
|
||||||
#raise Exception(f"commander service is unavailable: {settings.COMMANDER_ROOT_URL}")
|
#raise Exception(f"commander service is unavailable: {settings.COMMANDER_ROOT_URL}")
|
||||||
if status["msg"] != "ok":
|
if status["msg"] != "ok":
|
||||||
print("connected to commander service")
|
logger.success("connected to commander service")
|
||||||
|
|
||||||
def _move(self, direction, distance):
|
def _move(self, direction, distance):
|
||||||
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/move/{direction}/{distance}")
|
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/move/{direction}/{distance}")
|
||||||
print(response.json())
|
self._parse_response(raw_resp=response)
|
||||||
|
|
||||||
def _turn(self, direction, degree):
|
def _turn(self, direction, degree):
|
||||||
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/turn/{direction}/{degree}")
|
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/turn/{direction}/{degree}")
|
||||||
print(response.json())
|
self._parse_response(raw_resp=response)
|
||||||
|
|
||||||
def _takeoff(self):
|
def _takeoff(self):
|
||||||
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/takeoff")
|
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/takeoff")
|
||||||
print(response.json())
|
self._parse_response(raw_resp=response)
|
||||||
|
|
||||||
def _land(self):
|
def _land(self):
|
||||||
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/land")
|
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/land")
|
||||||
print(response.json())
|
self._parse_response(raw_resp=response)
|
||||||
|
|
||||||
def _end_session(self):
|
def _end_session(self):
|
||||||
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/end")
|
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/end")
|
||||||
print(response.json())
|
self._parse_response(raw_resp=response)
|
||||||
|
|
||||||
def _emergency(self):
|
def _emergency(self):
|
||||||
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/emergency")
|
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/emergency")
|
||||||
print(response.json())
|
self._parse_response(raw_resp=response)
|
||||||
|
|
||||||
def handle(self, cmd: dict):
|
def handle(self, cmd: dict):
|
||||||
#print(f"commanding for: {cmd}")
|
|
||||||
if cmd["command"] == "move":
|
if cmd["command"] == "move":
|
||||||
self._move(
|
self._move(direction=cmd["direction"], distance=cmd["distance_angle"])
|
||||||
direction=cmd["direction"],
|
|
||||||
distance=cmd["distance_angle"]
|
|
||||||
)
|
|
||||||
elif cmd["command"] == "turn":
|
elif cmd["command"] == "turn":
|
||||||
self._turn(
|
self._turn(direction=cmd["direction"], degree=cmd["distance_angle"])
|
||||||
direction=cmd["direction"],
|
|
||||||
degree=cmd["distance_angle"]
|
|
||||||
)
|
|
||||||
elif cmd["command"] == "takeoff":
|
elif cmd["command"] == "takeoff":
|
||||||
self._takeoff()
|
self._takeoff()
|
||||||
elif cmd["command"] == "land":
|
elif cmd["command"] == "land":
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class FlightStatsCollector:
|
|||||||
temp = tello.get_temperature()
|
temp = tello.get_temperature()
|
||||||
# wsnr = tello.query_wifi_signal_noise_ratio()
|
# wsnr = tello.query_wifi_signal_noise_ratio()
|
||||||
print(f"bat: {bat} - temp: {temp}") #- wsnr: {wsnr}")
|
print(f"bat: {bat} - temp: {temp}") #- wsnr: {wsnr}")
|
||||||
time.sleep(5)
|
time.sleep(3)
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
tello = Tello()
|
tello = Tello()
|
||||||
@@ -39,7 +39,6 @@ def land_on_low_battery():
|
|||||||
tello.land()
|
tello.land()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/takeoff")
|
@router.get("/takeoff")
|
||||||
def takeoff():
|
def takeoff():
|
||||||
try:
|
try:
|
||||||
@@ -48,7 +47,7 @@ def takeoff():
|
|||||||
return {"msg": "command failed", "reason": "failed to connect"}
|
return {"msg": "command failed", "reason": "failed to connect"}
|
||||||
|
|
||||||
if tello.is_flying:
|
if tello.is_flying:
|
||||||
return {"msg": "already flying"}
|
return {"msg": "command failed", "reason": "already flying"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tello.takeoff()
|
tello.takeoff()
|
||||||
@@ -64,7 +63,7 @@ def takeoff():
|
|||||||
@router.get("/land")
|
@router.get("/land")
|
||||||
def land():
|
def land():
|
||||||
if not tello.is_flying:
|
if not tello.is_flying:
|
||||||
return {"msg": "already on land"}
|
return {"msg": "command failed", "reason": "already on land"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tello.land()
|
tello.land()
|
||||||
@@ -78,9 +77,9 @@ def land():
|
|||||||
@router.get("/turn/{direction}/{degree}")
|
@router.get("/turn/{direction}/{degree}")
|
||||||
def turn(direction: str, degree: int):
|
def turn(direction: str, degree: int):
|
||||||
if direction not in ["left", "right"]:
|
if direction not in ["left", "right"]:
|
||||||
return {"direction must be only left or right"}
|
return {"msg": "command failed", "reason": "direction must be only left or right"}
|
||||||
if degree < 1 or degree > 360:
|
if degree < 1 or degree > 360:
|
||||||
return {"degree must be between 1 and 360"}
|
return {"msg": "command failed", "reason": "degree must be between 1 and 360"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if direction == "right":
|
if direction == "right":
|
||||||
@@ -95,9 +94,11 @@ def turn(direction: str, degree: int):
|
|||||||
@router.get("/move/{direction}/{distance}")
|
@router.get("/move/{direction}/{distance}")
|
||||||
def turn(direction: str, distance: int):
|
def turn(direction: str, distance: int):
|
||||||
if direction not in ["back", "forward", "left", "right", "up" , "down"]:
|
if direction not in ["back", "forward", "left", "right", "up" , "down"]:
|
||||||
return {"direction must be only back, forward, left, right up or down"}
|
return {"msg": "command failed", "reason": "direction must be only back, forward, left, right up or down"}
|
||||||
if distance < 20 or distance > 200:
|
if distance < 20 or distance > 250:
|
||||||
return {"distance must be between 20 and 500"}
|
# tellonun kendi sınırı 500 ama nolur nolmaz diye kısıtladım
|
||||||
|
return {"msg": "command failed", "reason": "distance must be between 20 and 250"}
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tello.move(direction, distance)
|
tello.move(direction, distance)
|
||||||
@@ -107,7 +108,7 @@ def turn(direction: str, distance: int):
|
|||||||
return {"msg": "ok"}
|
return {"msg": "ok"}
|
||||||
|
|
||||||
@router.get("/emergency")
|
@router.get("/emergency")
|
||||||
def emercengy_stop():
|
def emergency_stop():
|
||||||
try:
|
try:
|
||||||
tello.emergency()
|
tello.emergency()
|
||||||
return {"msg": "ok"}
|
return {"msg": "ok"}
|
||||||
|
|||||||
Reference in New Issue
Block a user