Skip to content

Commit

Permalink
Add very basic waypoint storage
Browse files Browse the repository at this point in the history
Kind of closes #16.
  • Loading branch information
kragniz committed May 25, 2016
1 parent 97ee9a6 commit 9a33942
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
4 changes: 3 additions & 1 deletion boatd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .boat import Boat
from .color import color
from .config import Config
from .waypoints import WaypointManager
from .driver import BaseBoatdDriver # noqa
from .base_plugin import BasePlugin # noqa

Expand Down Expand Up @@ -124,8 +125,9 @@ def run():
plugins = plugin.load_plugins(conf, boat)

behaviour_manager = load_behaviours(conf)
waypoint_manager = WaypointManager()

httpd = BoatdHTTPServer(boat, behaviour_manager,
httpd = BoatdHTTPServer(boat, behaviour_manager, waypoint_manager,
(conf.boatd.interface, conf.boatd.port),
BoatdRequestHandler)
while httpd.running:
Expand Down
23 changes: 22 additions & 1 deletion boatd/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import json

from . import exceptions

# reported api version
VERSION = 1.2

Expand All @@ -24,7 +26,7 @@ class BoatdHTTPServer(ThreadingMixIn, HTTPServer):
The main REST server for boatd. Listens for requests on port server_address
and handles each request with RequestHandlerClass.
'''
def __init__(self, boat, behaviour_manager,
def __init__(self, boat, behaviour_manager, waypoint_manager,
server_address, RequestHandlerClass, bind_and_activate=True):

HTTPServer.__init__(self, server_address, RequestHandlerClass,
Expand All @@ -33,6 +35,7 @@ def __init__(self, boat, behaviour_manager,

self.boat = boat
self.behaviour_manager = behaviour_manager
self.waypoint_manager = waypoint_manager
self.running = True

# set API endpoints for GETs
Expand All @@ -42,14 +45,32 @@ def __init__(self, boat, behaviour_manager,
'/wind': self.wind,
'/active': self.boat_active,
'/behaviours': self.behaviours,
'/waypoints': self.waypoints,
}

# set API endpoints for POSTs
self.post_handles = {
'/': self.boatd_post,
'/behaviours': self.behaviours_post,
'/waypoints': self.waypoints_post,
}

def waypoints(self):
return {
'waypoints': self.waypoint_manager.waypoints,
'current': self.waypoint_manager.current,
}

def waypoints_post(self, content):
waypoints = content.get('waypoints', None)

try:
self.waypoint_manager.add_waypoints(waypoints)
except exceptions.WaypointMalformedError:
return {'error': 'bad waypoint values'}

return self.waypoints()

def behaviours(self):
b = {
behaviour.name: {
Expand Down
46 changes: 46 additions & 0 deletions boatd/waypoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from . import exceptions


class WaypointManager(object):
def __init__(self):
self.waypoints = []
self.current = None

def add_waypoint(self, waypoint):
if len(waypoint) == 2:
lat, lon = waypoint
else:
raise exceptions.WaypointMalformedError('waypoint is not a tuple '
'of length two')

if isinstance(lat, float) and isinstance(lon, float):
self.waypoints.append(waypoint)
else:
raise exceptions.WaypointMalformedError('waypoint is not a tuple '
'of two floats')

def add_waypoints(self, waypoints):
for point in waypoints:
self.add_waypoint(point)

def next(self):
if self.current is not None:
self.current += 1
return self.waypoints[self.current]
else:
raise exceptions.WaypointsNotLoadedError()

def current(self):
if self.current is not None:
return self.waypoints[self.current]
else:
raise exceptions.WaypointsNotLoadedError()

def previous(self):
if self.current is not None:
if self.current > 0:
return self.waypoints[self.current - 1]
else:
return self.waypoints[0]
else:
raise exceptions.WaypointsNotLoadedError()

0 comments on commit 9a33942

Please sign in to comment.