Skip to content

Commit

Permalink
Make golint happy
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 10, 2014
1 parent 90423bc commit c129da6
Show file tree
Hide file tree
Showing 50 changed files with 507 additions and 510 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ Install Gobot with: `go get -u github.com/hybridgroup/gobot`

Gobot includes a RESTful API to query the status of any robot running within a group, including the connection and device status, and execute device commands.

To activate the API, require the `github.com/hybridgroup/gobot/api` package and instantiate the `Api` like this:
To activate the API, require the `github.com/hybridgroup/gobot/api` package and instantiate the `API` like this:

```go
master := gobot.NewGobot()
api.NewApi(master).Start()
api.NewAPI(master).Start()
```

You can also specify the api host and port, and turn on authentication:
```go
master := gobot.NewGobot()
server := api.NewApi(master)
server := api.NewAPI(master)
server.Port = "4000"
server.Username = "Gort"
server.Password = "klaatu"
Expand All @@ -150,7 +150,7 @@ Thank you!
* We will look at the patch, test it out, and give you feedback.
* Avoid doing minor whitespace changes, renamings, etc. along with merged content. These will be done by the maintainers from time to time but they can complicate merges and should be done seperately.
* Take care to maintain the existing coding style.
* `go fmt` your code.
* `golint` and `go fmt` your code.
* Add unit tests for any new or changed functionality.
* All pull requests should be "fast forward"
* If there are commits after yours use “git rebase -i <new_head_branch>”
Expand Down
60 changes: 30 additions & 30 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type api struct {
start func(*api)
}

func NewApi(g *gobot.Gobot) *api {
func NewAPI(g *gobot.Gobot) *api {
return &api{
gobot: g,
start: func(a *api) {
Expand Down Expand Up @@ -82,108 +82,108 @@ func (a *api) Start() {
})

a.server.Get("/robots/:robotname/commands", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_commands(params["robotname"], res, req)
a.robotCommands(params["robotname"], res, req)
})

robot_command_route := "/robots/:robotname/commands/:command"
robotCommandRoute := "/robots/:robotname/commands/:command"

a.server.Get(robot_command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.server.Get(robotCommandRoute, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.executeRobotCommand(params["robotname"], params["command"], res, req)
})
a.server.Post(robot_command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.server.Post(robotCommandRoute, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.executeRobotCommand(params["robotname"], params["command"], res, req)
})

a.server.Get("/robots/:robotname/devices", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_devices(params["robotname"], res, req)
a.robotDevices(params["robotname"], res, req)
})

a.server.Get("/robots/:robotname/devices/:devicename", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_device(params["robotname"], params["devicename"], res, req)
a.robotDevice(params["robotname"], params["devicename"], res, req)
})

a.server.Get("/robots/:robotname/devices/:devicename/commands", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_device_commands(params["robotname"], params["devicename"], res, req)
a.robotDeviceCommands(params["robotname"], params["devicename"], res, req)
})

command_route := "/robots/:robotname/devices/:devicename/commands/:command"
commandRoute := "/robots/:robotname/devices/:devicename/commands/:command"

a.server.Get(command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.server.Get(commandRoute, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req)
})
a.server.Post(command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.server.Post(commandRoute, func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.executeCommand(params["robotname"], params["devicename"], params["command"], res, req)
})

a.server.Get("/robots/:robotname/connections", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_connections(params["robotname"], res, req)
a.robotConnections(params["robotname"], res, req)
})

a.server.Get("/robots/:robotname/connections/:connectionname", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
a.robot_connection(params["robotname"], params["connectionname"], res, req)
a.robotConnection(params["robotname"], params["connectionname"], res, req)
})

a.start(a)
}

func (a *api) robots(res http.ResponseWriter, req *http.Request) {
jsonRobots := make([]*gobot.JsonRobot, 0)
jsonRobots := []*gobot.JSONRobot{}
for _, robot := range a.gobot.Robots {
jsonRobots = append(jsonRobots, robot.ToJson())
jsonRobots = append(jsonRobots, robot.ToJSON())
}
data, _ := json.Marshal(jsonRobots)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}

func (a *api) robot(name string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(a.gobot.Robot(name).ToJson())
data, _ := json.Marshal(a.gobot.Robot(name).ToJSON())
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}

func (a *api) robot_commands(name string, res http.ResponseWriter, req *http.Request) {
func (a *api) robotCommands(name string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(a.gobot.Robot(name).RobotCommands)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}

func (a *api) robot_devices(name string, res http.ResponseWriter, req *http.Request) {
func (a *api) robotDevices(name string, res http.ResponseWriter, req *http.Request) {
devices := a.gobot.Robot(name).Devices()
jsonDevices := make([]*gobot.JsonDevice, 0)
jsonDevices := []*gobot.JSONDevice{}
for _, device := range devices {
jsonDevices = append(jsonDevices, device.ToJson())
jsonDevices = append(jsonDevices, device.ToJSON())
}
data, _ := json.Marshal(jsonDevices)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}

func (a *api) robot_device(robot string, device string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(a.gobot.Robot(robot).Device(device).ToJson())
func (a *api) robotDevice(robot string, device string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(a.gobot.Robot(robot).Device(device).ToJSON())
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}

func (a *api) robot_device_commands(robot string, device string, res http.ResponseWriter, req *http.Request) {
func (a *api) robotDeviceCommands(robot string, device string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(a.gobot.Robot(robot).Device(device).Commands())
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}

func (a *api) robot_connections(name string, res http.ResponseWriter, req *http.Request) {
func (a *api) robotConnections(name string, res http.ResponseWriter, req *http.Request) {
connections := a.gobot.Robot(name).Connections()
jsonConnections := make([]*gobot.JsonConnection, 0)
jsonConnections := []*gobot.JSONConnection{}
for _, connection := range connections {
jsonConnections = append(jsonConnections, connection.ToJson())
jsonConnections = append(jsonConnections, connection.ToJSON())
}
data, _ := json.Marshal(jsonConnections)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}

func (a *api) robot_connection(robot string, connection string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(a.gobot.Robot(robot).Connection(connection).ToJson())
func (a *api) robotConnection(robot string, connection string, res http.ResponseWriter, req *http.Request) {
data, _ := json.Marshal(a.gobot.Robot(robot).Connection(connection).ToJSON())
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
Expand All @@ -196,7 +196,7 @@ func (a *api) executeCommand(robotname string, devicename string, commandname st
commands := robot.Commands().([]string)
for command := range commands {
if commands[command] == commandname {
ret := make([]interface{}, 0)
ret := []interface{}{}
for _, v := range gobot.Call(robot.Driver, commandname, body) {
ret = append(ret, v.Interface())
}
Expand All @@ -221,7 +221,7 @@ func (a *api) executeRobotCommand(robotname string, commandname string, res http
in[0] = reflect.ValueOf(body)
command := robot.Commands[commandname]
if command != nil {
ret := make([]interface{}, 0)
ret := []interface{}{}
for _, v := range reflect.ValueOf(robot.Commands[commandname]).Call(in) {
ret = append(ret, v.Interface())
}
Expand Down
10 changes: 5 additions & 5 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var _ = Describe("API", func() {

BeforeEach(func() {
m = gobot.NewGobot()
a = NewApi(m)
a = NewAPI(m)
a.start = func(m *api) {}

m.Robots = []*gobot.Robot{
Expand Down Expand Up @@ -51,7 +51,7 @@ var _ = Describe("API", func() {
It("should return all robot devices", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices", nil)
response := httptest.NewRecorder()
a.robot_devices("Robot 1", response, request)
a.robotDevices("Robot 1", response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []map[string]interface{}
json.Unmarshal(body, &i)
Expand All @@ -60,7 +60,7 @@ var _ = Describe("API", func() {
PIt("should return robot commands", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands", nil)
response := httptest.NewRecorder()
a.robot_commands("Robot 1", response, request)
a.robotCommands("Robot 1", response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []string
json.Unmarshal(body, &i)
Expand Down Expand Up @@ -89,7 +89,7 @@ var _ = Describe("API", func() {
It("should return robot device", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201", nil)
response := httptest.NewRecorder()
a.robot_device("Robot 1", "Device 1", response, request)
a.robotDevice("Robot 1", "Device 1", response, request)
body, _ := ioutil.ReadAll(response.Body)
var i map[string]interface{}
json.Unmarshal(body, &i)
Expand All @@ -98,7 +98,7 @@ var _ = Describe("API", func() {
It("should return device commands", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands", nil)
response := httptest.NewRecorder()
a.robot_device_commands("Robot 1", "Device 1", response, request)
a.robotDeviceCommands("Robot 1", "Device 1", response, request)
body, _ := ioutil.ReadAll(response.Body)
var i []string
json.Unmarshal(body, &i)
Expand Down
14 changes: 7 additions & 7 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Connection interface {
Finalize() bool
}

type JsonConnection struct {
type JSONConnection struct {
Name string `json:"name"`
Port string `json:"port"`
Adaptor string `json:"adaptor"`
Expand Down Expand Up @@ -75,10 +75,10 @@ func (c *connection) Finalize() bool {
return c.Adaptor.Finalize()
}

func (c *connection) ToJson() *JsonConnection {
jsonConnection := new(JsonConnection)
jsonConnection.Name = c.Name
jsonConnection.Port = c.Port
jsonConnection.Adaptor = c.Type
return jsonConnection
func (c *connection) ToJSON() *JSONConnection {
return &JSONConnection{
Name: c.Name,
Port: c.Port,
Adaptor: c.Type,
}
}
22 changes: 11 additions & 11 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type Device interface {
getName() string
}

type JsonDevice struct {
type JSONDevice struct {
Name string `json:"name"`
Driver string `json:"driver"`
Connection *JsonConnection `json:"connection"`
Connection *JSONConnection `json:"connection"`
Commands []string `json:"commands"`
}

Expand Down Expand Up @@ -97,13 +97,13 @@ func (d *device) Commands() interface{} {
return FieldByNamePtr(d.Driver, "Commands").Interface()
}

func (d *device) ToJson() *JsonDevice {
jsonDevice := new(JsonDevice)
jsonDevice.Name = d.Name
jsonDevice.Driver = d.Type
jsonDevice.Connection = d.Robot.Connection(FieldByNamePtr(FieldByNamePtr(d.Driver, "Adaptor").
Interface().(AdaptorInterface), "Name").
Interface().(string)).ToJson()
jsonDevice.Commands = FieldByNamePtr(d.Driver, "Commands").Interface().([]string)
return jsonDevice
func (d *device) ToJSON() *JSONDevice {
return &JSONDevice{
Name: d.Name,
Driver: d.Type,
Connection: d.Robot.Connection(FieldByNamePtr(FieldByNamePtr(d.Driver, "Adaptor").
Interface().(AdaptorInterface), "Name").
Interface().(string)).ToJSON(),
Commands: FieldByNamePtr(d.Driver, "Commands").Interface().([]string),
}
}
4 changes: 2 additions & 2 deletions examples/ardrone_face_tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func main() {
}
if face != nil {
opencv.DrawRectangles(i, []*cv.Rect{face}, 0, 255, 0, 5)
center_x := float64(image.Width()) * 0.5
turn := -(float64(face.X()) - center_x) / center_x
centerX := float64(image.Width()) * 0.5
turn := -(float64(face.X()) - centerX) / centerX
fmt.Println("turning:", turn)
if turn < 0 {
drone.Clockwise(math.Abs(turn * 0.4))
Expand Down
30 changes: 14 additions & 16 deletions examples/ardrone_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func main() {

work := func() {
offset := 32767.0
right_stick := pair{x: 0, y: 0}
left_stick := pair{x: 0, y: 0}
rightStick := pair{x: 0, y: 0}
leftStick := pair{x: 0, y: 0}

gobot.On(joystick.Events["square_press"], func(data interface{}) {
drone.TakeOff()
Expand All @@ -38,31 +38,31 @@ func main() {
})
gobot.On(joystick.Events["left_x"], func(data interface{}) {
val := float64(data.(int16))
if left_stick.x != val {
left_stick.x = val
if leftStick.x != val {
leftStick.x = val
}
})
gobot.On(joystick.Events["left_y"], func(data interface{}) {
val := float64(data.(int16))
if left_stick.y != val {
left_stick.y = val
if leftStick.y != val {
leftStick.y = val
}
})
gobot.On(joystick.Events["right_x"], func(data interface{}) {
val := float64(data.(int16))
if right_stick.x != val {
right_stick.x = val
if rightStick.x != val {
rightStick.x = val
}
})
gobot.On(joystick.Events["right_y"], func(data interface{}) {
val := float64(data.(int16))
if right_stick.y != val {
right_stick.y = val
if rightStick.y != val {
rightStick.y = val
}
})

gobot.Every(10*time.Millisecond, func() {
pair := left_stick
pair := leftStick
if pair.y < -10 {
drone.Forward(validatePitch(pair.y, offset))
} else if pair.y > 10 {
Expand All @@ -81,7 +81,7 @@ func main() {
})

gobot.Every(10*time.Millisecond, func() {
pair := right_stick
pair := rightStick
if pair.y < -10 {
drone.Up(validatePitch(pair.y, offset))
} else if pair.y > 10 {
Expand Down Expand Up @@ -111,10 +111,8 @@ func validatePitch(data float64, offset float64) float64 {
if value >= 0.1 {
if value <= 1.0 {
return float64(int(value*100)) / 100
} else {
return 1.0
}
} else {
return 0.0
return 1.0
}
return 0.0
}
Loading

0 comments on commit c129da6

Please sign in to comment.