Skip to content

Commit

Permalink
MPS Wrapper Additions (#30)
Browse files Browse the repository at this point in the history
* update mps wrapper to include new endpoints

* update to match MPS vtol depreciation

* add missing drone/insert url

* change route names to be consistently api/drone/*

---------

Co-authored-by: Aden Chan <[email protected]>
  • Loading branch information
jimgeng and 21chanas3 authored Oct 27, 2024
1 parent 8157494 commit 14f0e33
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 22 deletions.
50 changes: 39 additions & 11 deletions src/drone/mps_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class DroneApiClient:
_mission_planner_api_url = "http://localhost:9001"
_mission_planner_api_url = "http://localhost:9000"

@staticmethod
def _fetch_from_mission_planner(endpoint, method="GET", data=None):
Expand All @@ -26,45 +26,73 @@ def get_status_history():
@staticmethod
def takeoff(altitude):
return DroneApiClient._fetch_from_mission_planner(
"drone/takeoff", method="POST", data={"altitude": altitude}
"takeoff", method="POST", data={"altitude": altitude}
)

@staticmethod
def arm(arm_value):
return DroneApiClient._fetch_from_mission_planner(
"drone/arm", method="POST", data={"arm": arm_value}
"arm", method="POST", data={"arm": arm_value}
)

@staticmethod
def land():
return DroneApiClient._fetch_from_mission_planner("drone/land")
return DroneApiClient._fetch_from_mission_planner("land")

@staticmethod
def rtl(altitude):
def get_rlt():
return DroneApiClient._fetch_from_mission_planner("rtl")

@staticmethod
def post_rtl(altitude):
return DroneApiClient._fetch_from_mission_planner(
"drone/rtl", method="POST", data={"altitude": altitude}
"rtl", method="POST", data={"altitude": altitude}
)

@staticmethod
def lock():
return DroneApiClient._fetch_from_mission_planner("drone/lock")
return DroneApiClient._fetch_from_mission_planner("lock")

@staticmethod
def unlock():
return DroneApiClient._fetch_from_mission_planner("drone/unlock")
return DroneApiClient._fetch_from_mission_planner("unlock")

@staticmethod
def get_queue():
return DroneApiClient._fetch_from_mission_planner("drone/queue")
return DroneApiClient._fetch_from_mission_planner("queue")

@staticmethod
def post_queue(queue):
return DroneApiClient._fetch_from_mission_planner(
"drone/queue", method="POST", data=queue
"queue", method="POST", data=queue
)

@staticmethod
def post_home(wp):
return DroneApiClient._fetch_from_mission_planner(
"drone/home", method="POST", data=wp
"home", method="POST", data=wp
)

@staticmethod
def insert(queue):
return DroneApiClient._fetch_from_mission_planner(
"insert", method="POST", data=queue
)

@staticmethod
def clear():
return DroneApiClient._fetch_from_mission_planner("clear")

@staticmethod
def diversion(exclude_wps, rejoin_wp):
return DroneApiClient._fetch_from_mission_planner(
"diversion",
method="POST",
data={"exclude": exclude_wps, "rejoin_at": rejoin_wp},
)

@staticmethod
def flightmode(mode):
return DroneApiClient._fetch_from_mission_planner(
"flightmode", method="POST", data={"mode": mode}
)
23 changes: 14 additions & 9 deletions src/drone/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
urlpatterns = [
path("status", views.get_current_status, name="get_current_status"),
path("status/history", views.get_status_history, name="get_status_history"),
path("drone/takeoff", views.takeoff, name="takeoff"),
path("drone/arm", views.arm, name="arm"),
path("drone/land", views.land, name="land"),
path("drone/rtl", views.rtl, name="rtl"),
path("drone/lock", views.lock, name="lock"),
path("drone/unlock", views.unlock, name="unlock"),
path("drone/queue", views.get_queue, name="get_queue"),
path("drone/queue", views.post_queue, name="post_queue"),
path("drone/home", views.post_home, name="post_home"),
path("takeoff", views.takeoff, name="takeoff"),
path("arm", views.arm, name="arm"),
path("land", views.land, name="land"),
path("rtl", views.get_rtl, name="rtl"),
path("rtl", views.post_rtl, name="rtl"),
path("lock", views.lock, name="lock"),
path("insert", views.insert, name="insert"),
path("unlock", views.unlock, name="unlock"),
path("queue", views.get_queue, name="get_queue"),
path("queue", views.post_queue, name="post_queue"),
path("home", views.post_home, name="post_home"),
path("clear", views.clear, name="clear"),
path("diversion", views.diversion, name="diversion"),
path("flightmode", views.flightmode, name="flight_mode"),
]
51 changes: 49 additions & 2 deletions src/drone/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ def land(request):
return HttpResponse(status=response.status_code)


@require_http_methods(["GET"])
def get_rtl(request):
response = DroneApiClient.get_rtl()
return JsonResponse(status=response.status_code)


@csrf_exempt
@require_http_methods(["POST"])
def rtl(request):
def post_rtl(request):
try:
data = json.loads(request.body)
altitude = data.get("altitude")
response = DroneApiClient.rtl(altitude)
response = DroneApiClient.post_rtl(altitude)
return HttpResponse(status=response.status_code)
except (KeyError, ValueError, TypeError):
return JsonResponse({"error": "Invalid input"}, status=400)
Expand Down Expand Up @@ -97,3 +103,44 @@ def post_home(request):
return HttpResponse(status=response.status_code)
except (KeyError, ValueError, TypeError):
return JsonResponse({"error": "Invalid input"}, status=400)


@csrf_exempt
@require_http_methods(["POST"])
def insert(request):
try:
queue = json.loads(request.body)
response = DroneApiClient.insert(queue)
return HttpResponse(status=response.status_code)
except (KeyError, ValueError, TypeError):
return JsonResponse({"error": "Invalid input"}, status=400)


@require_http_methods(["GET"])
def clear(request):
response = DroneApiClient.clear()
return HttpResponse(status=response.status_code)


@require_http_methods(["POST"])
def diversion(request):
try:
data = json.loads(request.body)
exclude_wps = data.get("exclude")
rejoin_wp = data.get("rejoin_at")
response = DroneApiClient.diversion(exclude_wps, rejoin_wp)
return HttpResponse(status=response.status_code)
except (KeyError, ValueError, TypeError):
return JsonResponse({"error": "Invalid input"}, status=400)


@csrf_exempt
@require_http_methods(["POST"])
def flightmode(request):
try:
data = json.loads(request.body)
mode = data.get("mode")
response = DroneApiClient.flightmode(mode)
return HttpResponse(status=response.status_code)
except (KeyError, ValueError, TypeError):
return JsonResponse({"error": "Invalid input"}, status=400)

0 comments on commit 14f0e33

Please sign in to comment.