Skip to content

Commit

Permalink
WIP threepio support
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jul 24, 2014
1 parent 3002007 commit 2d8ba72
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 74 deletions.
1 change: 0 additions & 1 deletion adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func (a *Adaptor) SetConnected(b bool) {
func (a *Adaptor) ToJSON() *JSONConnection {
return &JSONConnection{
Name: a.Name(),
Port: a.Port(),
Adaptor: a.Type(),
}
}
60 changes: 32 additions & 28 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,32 @@ func (a *api) SetDebug() {
// start starts the api using the start function
// sets on the API on initialization.
func (a *api) Start() {
mcpCommandRoute := "/commands/:command"
deviceCommandRoute := "/robots/:robot/devices/:device/commands/:command"
robotCommandRoute := "/robots/:robot/commands/:command"

// api
a.Get("/", a.mcp)
a.Get("/commands", a.mcpCommands)
mcpCommandRoute := "/api/commands/:command"
deviceCommandRoute := "/api/robots/:robot/devices/:device/commands/:command"
robotCommandRoute := "/api/robots/:robot/commands/:command"

a.Get("/api/commands", a.mcpCommands)
a.Get(mcpCommandRoute, a.executeMcpCommand)
a.Post(mcpCommandRoute, a.executeMcpCommand)
a.Get("/robots", a.robots)
a.Get("/robots/:robot", a.robot)
a.Get("/robots/:robot/commands", a.robotCommands)
a.Get("/api/robots", a.robots)
a.Get("/api/robots/:robot", a.robot)
a.Get("/api/robots/:robot/commands", a.robotCommands)
a.Get(robotCommandRoute, a.executeRobotCommand)
a.Post(robotCommandRoute, a.executeRobotCommand)
a.Get("/robots/:robot/devices", a.robotDevices)
a.Get("/robots/:robot/devices/:device", a.robotDevice)
a.Get("/robots/:robot/devices/:device/commands", a.robotDeviceCommands)
a.Get("/api/robots/:robot/devices", a.robotDevices)
a.Get("/api/robots/:robot/devices/:device", a.robotDevice)
a.Get("/api/robots/:robot/devices/:device/commands", a.robotDeviceCommands)
a.Get(deviceCommandRoute, a.executeDeviceCommand)
a.Post(deviceCommandRoute, a.executeDeviceCommand)
a.Get("/robots/:robot/connections", a.robotConnections)
a.Get("/robots/:robot/connections/:connection", a.robotConnection)
a.Get("/api/robots/:robot/connections", a.robotConnections)
a.Get("/api/robots/:robot/connections/:connection", a.robotConnection)
a.Get("/api/", a.mcp)

// robeaux
a.Get("/", func(res http.ResponseWriter, req *http.Request) {
http.Redirect(res, req, "/index.html", http.StatusMovedPermanently)
})
a.Get("/index.html", a.robeaux)
a.Get("/images/:a", a.robeaux)
a.Get("/js/:a", a.robeaux)
Expand Down Expand Up @@ -152,48 +156,48 @@ func (a *api) robeaux(res http.ResponseWriter, req *http.Request) {
}

func (a *api) mcp(res http.ResponseWriter, req *http.Request) {
a.writeJSON(a.gobot.ToJSON(), res)
a.writeJSON(map[string]interface{}{"MCP": a.gobot.ToJSON()}, res)
}

func (a *api) mcpCommands(res http.ResponseWriter, req *http.Request) {
a.writeJSON(a.gobot.ToJSON().Commands, res)
a.writeJSON(map[string]interface{}{"commands": a.gobot.ToJSON().Commands}, res)
}

func (a *api) robots(res http.ResponseWriter, req *http.Request) {
jsonRobots := []*gobot.JSONRobot{}
a.gobot.Robots().Each(func(r *gobot.Robot) {
jsonRobots = append(jsonRobots, r.ToJSON())
})
a.writeJSON(jsonRobots, res)
a.writeJSON(map[string]interface{}{"robots": jsonRobots}, res)
}

func (a *api) robot(res http.ResponseWriter, req *http.Request) {
a.writeJSON(a.gobot.Robot(req.URL.Query().Get(":robot")).ToJSON(), res)
a.writeJSON(map[string]interface{}{"robot": a.gobot.Robot(req.URL.Query().Get(":robot")).ToJSON()}, res)
}

func (a *api) robotCommands(res http.ResponseWriter, req *http.Request) {
a.writeJSON(a.gobot.Robot(req.URL.Query().Get(":robot")).ToJSON().Commands, res)
a.writeJSON(map[string]interface{}{"commands": a.gobot.Robot(req.URL.Query().Get(":robot")).ToJSON().Commands}, res)
}

func (a *api) robotDevices(res http.ResponseWriter, req *http.Request) {
jsonDevices := []*gobot.JSONDevice{}
a.gobot.Robot(req.URL.Query().Get(":robot")).Devices().Each(func(d gobot.Device) {
jsonDevices = append(jsonDevices, d.ToJSON())
})
a.writeJSON(jsonDevices, res)
a.writeJSON(map[string]interface{}{"devices": jsonDevices}, res)
}

func (a *api) robotDevice(res http.ResponseWriter, req *http.Request) {
a.writeJSON(
a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).ToJSON(), res,
map[string]interface{}{"device": a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).ToJSON()}, res,
)
}

func (a *api) robotDeviceCommands(res http.ResponseWriter, req *http.Request) {
a.writeJSON(
a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).ToJSON().Commands, res,
map[string]interface{}{"commands": a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).ToJSON().Commands}, res,
)
}

Expand All @@ -202,13 +206,13 @@ func (a *api) robotConnections(res http.ResponseWriter, req *http.Request) {
a.gobot.Robot(req.URL.Query().Get(":robot")).Connections().Each(func(c gobot.Connection) {
jsonConnections = append(jsonConnections, c.ToJSON())
})
a.writeJSON(jsonConnections, res)
a.writeJSON(map[string]interface{}{"connections": jsonConnections}, res)
}

func (a *api) robotConnection(res http.ResponseWriter, req *http.Request) {
a.writeJSON(
a.gobot.Robot(req.URL.Query().Get(":robot")).
Connection(req.URL.Query().Get(":connection")).ToJSON(),
map[string]interface{}{"connection": a.gobot.Robot(req.URL.Query().Get(":robot")).
Connection(req.URL.Query().Get(":connection")).ToJSON()},
res,
)
}
Expand Down Expand Up @@ -248,7 +252,7 @@ func (a *api) executeCommand(f func(map[string]interface{}) interface{},
json.NewDecoder(req.Body).Decode(&body)

if f != nil {
a.writeJSON(f(body), res)
a.writeJSON(map[string]interface{}{"result": f(body)}, res)
} else {
a.writeJSON("Unknown Command", res)
}
Expand Down
75 changes: 38 additions & 37 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func TestBasicAuth(t *testing.T) {

a.SetBasicAuth("admin", "password")

request, _ := http.NewRequest("GET", "/", nil)
request, _ := http.NewRequest("GET", "/api/", nil)
request.SetBasicAuth("admin", "password")
response := httptest.NewRecorder()
a.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 200)

request, _ = http.NewRequest("GET", "/", nil)
request, _ = http.NewRequest("GET", "/api/", nil)
request.SetBasicAuth("admin", "wrongPassword")
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
Expand Down Expand Up @@ -74,24 +74,25 @@ func TestRobeaux(t *testing.T) {

func TestMcp(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/", nil)
request, _ := http.NewRequest("GET", "/api/", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body), 2)
gobot.Refute(t, body["MCP"].(map[string]interface{})["robots"], nil)
gobot.Refute(t, body["MCP"].(map[string]interface{})["commands"], nil)
}

func TestMcpCommands(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/commands", nil)
request, _ := http.NewRequest("GET", "/api/commands", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body []string
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body, []string{"TestFunction"})
gobot.Assert(t, body["commands"], []interface{}{"TestFunction"})
}

func TestExecuteMcpCommand(t *testing.T) {
Expand All @@ -100,19 +101,19 @@ func TestExecuteMcpCommand(t *testing.T) {

// known command
request, _ := http.NewRequest("GET",
"/commands/TestFunction",
"/api/commands/TestFunction",
bytes.NewBufferString(`{"message":"Beep Boop"}`),
)
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body, "hey Beep Boop")
gobot.Assert(t, body.(map[string]interface{})["result"], "hey Beep Boop")

// unknown command
request, _ = http.NewRequest("GET",
"/commands/TestFuntion1",
"/api/commands/TestFuntion1",
bytes.NewBufferString(`{"message":"Beep Boop"}`),
)
request.Header.Add("Content-Type", "application/json")
Expand All @@ -125,66 +126,66 @@ func TestExecuteMcpCommand(t *testing.T) {

func TestRobots(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots", nil)
request, _ := http.NewRequest("GET", "/api/robots", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body []map[string]interface{}
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body), 3)
gobot.Assert(t, len(body["robots"].([]interface{})), 3)
}

func TestRobot(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot1", nil)
request, _ := http.NewRequest("GET", "/api/robots/Robot1", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["name"].(string), "Robot1")
gobot.Assert(t, body["robot"].(map[string]interface{})["name"].(string), "Robot1")
}

func TestRobotDevices(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot1/devices", nil)
request, _ := http.NewRequest("GET", "/api/robots/Robot1/devices", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body []map[string]interface{}
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body), 3)
gobot.Assert(t, len(body["devices"].([]interface{})), 3)
}

func TestRobotCommands(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot1/commands", nil)
request, _ := http.NewRequest("GET", "/api/robots/Robot1/commands", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body []string
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body, []string{"robotTestFunction"})
gobot.Assert(t, body["commands"], []interface{}{"robotTestFunction"})
}

func TestExecuteRobotCommand(t *testing.T) {
var body interface{}
a := initTestAPI()
// known command
request, _ := http.NewRequest("GET",
"/robots/Robot1/commands/robotTestFunction",
"/api/robots/Robot1/commands/robotTestFunction",
bytes.NewBufferString(`{"message":"Beep Boop", "robot":"Robot1"}`),
)
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body, "hey Robot1, Beep Boop")
gobot.Assert(t, body.(map[string]interface{})["result"], "hey Robot1, Beep Boop")

// unknown command
request, _ = http.NewRequest("GET",
"/robots/Robot1/commands/robotTestFuntion1",
"/api/robots/Robot1/commands/robotTestFuntion1",
bytes.NewBufferString(`{"message":"Beep Boop"}`),
)
request.Header.Add("Content-Type", "application/json")
Expand All @@ -198,29 +199,29 @@ func TestExecuteRobotCommand(t *testing.T) {
func TestRobotDevice(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET",
"/robots/Robot1/devices/Device1",
"/api/robots/Robot1/devices/Device1",
nil,
)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["name"].(string), "Device1")
gobot.Assert(t, body["device"].(map[string]interface{})["name"].(string), "Device1")
}

func TestRobotDeviceCommands(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET",
"/robots/Robot1/devices/Device1/commands",
"/api/robots/Robot1/devices/Device1/commands",
nil,
)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body []string
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body), 2)
gobot.Assert(t, len(body["commands"].([]interface{})), 2)
}

func TestExecuteRobotDeviceCommand(t *testing.T) {
Expand All @@ -229,19 +230,19 @@ func TestExecuteRobotDeviceCommand(t *testing.T) {

// known command
request, _ := http.NewRequest("GET",
"/robots/Robot1/devices/Device1/commands/TestDriverCommand",
"/api/robots/Robot1/devices/Device1/commands/TestDriverCommand",
bytes.NewBufferString(`{"name":"human"}`),
)
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body, "hello human")
gobot.Assert(t, body.(map[string]interface{})["result"].(string), "hello human")

// unknown command
request, _ = http.NewRequest("GET",
"/robots/Robot1/devices/Device1/commands/DriverCommand1",
"/api/robots/Robot1/devices/Device1/commands/DriverCommand1",
bytes.NewBufferString(`{"name":"human"}`),
)
request.Header.Add("Content-Type", "application/json")
Expand All @@ -254,27 +255,27 @@ func TestExecuteRobotDeviceCommand(t *testing.T) {

func TestRobotConnections(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot1/connections", nil)
request, _ := http.NewRequest("GET", "/api/robots/Robot1/connections", nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body []map[string]interface{}
var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, len(body), 3)
gobot.Assert(t, len(body["connections"].([]interface{})), 3)
}

func TestRobotConnection(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET",
"/robots/Robot1/connections/Connection1",
"/api/robots/Robot1/connections/Connection1",
nil,
)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

var body map[string]interface{}
json.NewDecoder(response.Body).Decode(&body)
gobot.Assert(t, body["name"].(string), "Connection1")
gobot.Assert(t, body["connection"].(map[string]interface{})["name"].(string), "Connection1")
}

func TestAPIRouter(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

type JSONConnection struct {
Name string `json:"name"`
Port string `json:"port"`
Adaptor string `json:"adaptor"`
}

Expand Down
Loading

0 comments on commit 2d8ba72

Please sign in to comment.