mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-24 13:48:49 +08:00
Fix errors in API
This commit is contained in:
parent
bd8d31e23f
commit
958d1941ad
@ -17,6 +17,7 @@ type AdaptorInterface interface {
|
||||
name() string
|
||||
setName(string)
|
||||
params() map[string]interface{}
|
||||
ToJSON() *JSONConnection
|
||||
}
|
||||
|
||||
func (a *Adaptor) port() string {
|
||||
|
22
api/api.go
22
api/api.go
@ -132,9 +132,9 @@ func (a *api) commands(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
func (a *api) robots(res http.ResponseWriter, req *http.Request) {
|
||||
jsonRobots := []*gobot.JSONRobot{}
|
||||
for _, robot := range a.gobot.Robots {
|
||||
jsonRobots = append(jsonRobots, robot.ToJSON())
|
||||
}
|
||||
a.gobot.Robots().Each(func(r *gobot.Robot) {
|
||||
jsonRobots = append(jsonRobots, r.ToJSON())
|
||||
})
|
||||
data, _ := json.Marshal(jsonRobots)
|
||||
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
res.Write(data)
|
||||
@ -159,11 +159,10 @@ func (a *api) robotCommands(res http.ResponseWriter, req *http.Request) {
|
||||
func (a *api) robotDevices(res http.ResponseWriter, req *http.Request) {
|
||||
robot := req.URL.Query().Get(":robot")
|
||||
|
||||
devices := a.gobot.Robot(robot).Devices()
|
||||
jsonDevices := []*gobot.JSONDevice{}
|
||||
for _, device := range devices {
|
||||
jsonDevices = append(jsonDevices, device.ToJSON())
|
||||
}
|
||||
a.gobot.Robot(robot).Devices().Each(func(d gobot.Device) {
|
||||
jsonDevices = append(jsonDevices, d.ToJSON())
|
||||
})
|
||||
data, _ := json.Marshal(jsonDevices)
|
||||
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
res.Write(data)
|
||||
@ -190,11 +189,10 @@ func (a *api) robotDeviceCommands(res http.ResponseWriter, req *http.Request) {
|
||||
func (a *api) robotConnections(res http.ResponseWriter, req *http.Request) {
|
||||
robot := req.URL.Query().Get(":robot")
|
||||
|
||||
connections := a.gobot.Robot(robot).Connections()
|
||||
jsonConnections := []*gobot.JSONConnection{}
|
||||
for _, connection := range connections {
|
||||
jsonConnections = append(jsonConnections, connection.ToJSON())
|
||||
}
|
||||
a.gobot.Robot(robot).Connections().Each(func(c gobot.Connection) {
|
||||
jsonConnections = append(jsonConnections, c.ToJSON())
|
||||
})
|
||||
data, _ := json.Marshal(jsonConnections)
|
||||
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
res.Write(data)
|
||||
@ -215,7 +213,7 @@ func (a *api) executeCommand(res http.ResponseWriter, req *http.Request) {
|
||||
data, _ := ioutil.ReadAll(req.Body)
|
||||
body := make(map[string]interface{})
|
||||
json.Unmarshal(data, &body)
|
||||
f := a.gobot.Commands[command]
|
||||
f := a.gobot.Commands()[command]
|
||||
|
||||
if f != nil {
|
||||
data, _ = json.Marshal(f(body))
|
||||
|
@ -18,11 +18,9 @@ func initTestAPI() *api {
|
||||
a.start = func(m *api) {}
|
||||
a.Start()
|
||||
|
||||
g.Robots = []*gobot.Robot{
|
||||
gobot.NewTestRobot("Robot 1"),
|
||||
gobot.NewTestRobot("Robot 2"),
|
||||
gobot.NewTestRobot("Robot 3"),
|
||||
}
|
||||
g.Robots().Add(gobot.NewTestRobot("Robot 1"))
|
||||
g.Robots().Add(gobot.NewTestRobot("Robot 2"))
|
||||
g.Robots().Add(gobot.NewTestRobot("Robot 3"))
|
||||
|
||||
return a
|
||||
}
|
||||
|
17
command.go
17
command.go
@ -1,17 +0,0 @@
|
||||
package gobot
|
||||
|
||||
type Command struct {
|
||||
Name string
|
||||
Command func(map[string]interface{}) interface{}
|
||||
}
|
||||
type Commands []Command
|
||||
|
||||
func (c commands) Add(name string, cmd Command) {
|
||||
c[name] = cmd
|
||||
}
|
||||
|
||||
func (c commands) Each(f func(Command)) {
|
||||
for _, command := range c {
|
||||
f(command)
|
||||
}
|
||||
}
|
18
driver.go
18
driver.go
@ -10,7 +10,7 @@ type Driver struct {
|
||||
Interval time.Duration
|
||||
Pin string
|
||||
Name string
|
||||
Commands map[string]func(map[string]interface{}) interface{}
|
||||
commands map[string]func(map[string]interface{}) interface{}
|
||||
Events map[string]*Event
|
||||
Type string
|
||||
}
|
||||
@ -23,7 +23,7 @@ type DriverInterface interface {
|
||||
interval() time.Duration
|
||||
setName(string)
|
||||
name() string
|
||||
commands() map[string]func(map[string]interface{}) interface{}
|
||||
Commands() map[string]func(map[string]interface{}) interface{}
|
||||
ToJSON() *JSONDevice
|
||||
}
|
||||
|
||||
@ -47,15 +47,15 @@ func (d *Driver) name() string {
|
||||
return d.Name
|
||||
}
|
||||
|
||||
func (d *Driver) commands() map[string]func(map[string]interface{}) interface{} {
|
||||
return d.Commands
|
||||
func (d *Driver) Commands() map[string]func(map[string]interface{}) interface{} {
|
||||
return d.commands
|
||||
}
|
||||
|
||||
func (d *Driver) AddCommand(name string, f func(map[string]interface{}) interface{}) {
|
||||
d.Commands[name] = f
|
||||
d.Commands()[name] = f
|
||||
}
|
||||
|
||||
func NewDriver(name string, t string, commands Commands, a AdaptorInterface) *Driver {
|
||||
func NewDriver(name string, t string, a AdaptorInterface) *Driver {
|
||||
if name == "" {
|
||||
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
|
||||
}
|
||||
@ -63,7 +63,7 @@ func NewDriver(name string, t string, commands Commands, a AdaptorInterface) *Dr
|
||||
Type: t,
|
||||
Name: name,
|
||||
Interval: 10 * time.Millisecond,
|
||||
Commands: commands,
|
||||
commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||
Adaptor: a,
|
||||
}
|
||||
}
|
||||
@ -77,10 +77,10 @@ func (d *Driver) ToJSON() *JSONDevice {
|
||||
}
|
||||
|
||||
if d.adaptor() != nil {
|
||||
//jsonDevice.Connection = d.Robot.Connection(d.adaptor().name()).ToJSON()
|
||||
jsonDevice.Connection = d.adaptor().ToJSON()
|
||||
}
|
||||
|
||||
commands := d.commands()
|
||||
commands := d.Commands()
|
||||
for command := range commands {
|
||||
jsonDevice.Commands = append(jsonDevice.Commands, command)
|
||||
}
|
||||
|
12
gobot.go
12
gobot.go
@ -13,21 +13,21 @@ type JSONGobot struct {
|
||||
|
||||
type Gobot struct {
|
||||
robots *robots
|
||||
commands commands
|
||||
commands map[string]func(map[string]interface{}) interface{}
|
||||
trap func(chan os.Signal)
|
||||
}
|
||||
|
||||
func NewGobot() *Gobot {
|
||||
return &Gobot{
|
||||
robots: &robots{},
|
||||
commands: make(commands),
|
||||
commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||
trap: func(c chan os.Signal) {
|
||||
signal.Notify(c, os.Interrupt)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Gobot) Commands() commands {
|
||||
func (g *Gobot) Commands() map[string]func(map[string]interface{}) interface{} {
|
||||
return g.commands
|
||||
}
|
||||
|
||||
@ -65,9 +65,9 @@ func (g *Gobot) ToJSON() *JSONGobot {
|
||||
Commands: []string{},
|
||||
}
|
||||
|
||||
g.commands.Each(func(c Command) {
|
||||
jsonGobot.Commands = append(jsonGobot.Commands, c.Name)
|
||||
})
|
||||
for command := range g.Commands() {
|
||||
jsonGobot.Commands = append(jsonGobot.Commands, command)
|
||||
}
|
||||
|
||||
g.robots.Each(func(r *Robot) {
|
||||
jsonGobot.Robots = append(jsonGobot.Robots, r.ToJSON())
|
||||
|
@ -11,10 +11,9 @@ type AnalogSensorDriver struct {
|
||||
func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
|
||||
d := &AnalogSensorDriver{
|
||||
Driver: gobot.Driver{
|
||||
Name: name,
|
||||
Pin: pin,
|
||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||
Adaptor: a.(gobot.AdaptorInterface),
|
||||
Name: name,
|
||||
Pin: pin,
|
||||
Adaptor: a.(gobot.AdaptorInterface),
|
||||
},
|
||||
}
|
||||
d.Driver.AddCommand("Read", func(params map[string]interface{}) interface{} {
|
||||
|
@ -12,10 +12,9 @@ type DirectPinDriver struct {
|
||||
func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
|
||||
d := &DirectPinDriver{
|
||||
Driver: gobot.Driver{
|
||||
Name: name,
|
||||
Pin: pin,
|
||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||
Adaptor: a.(gobot.AdaptorInterface),
|
||||
Name: name,
|
||||
Pin: pin,
|
||||
Adaptor: a.(gobot.AdaptorInterface),
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -15,30 +15,29 @@ func NewLedDriver(name string, a PwmDigitalWriter, pin string) *LedDriver {
|
||||
Driver: *gobot.NewDriver(
|
||||
name,
|
||||
"LedDriver",
|
||||
gobot.Commands{},
|
||||
a.(gobot.AdaptorInterface),
|
||||
),
|
||||
Pin: pin,
|
||||
High: false,
|
||||
}
|
||||
|
||||
l.Driver.Commands().Add("Brightness", func(params map[string]interface{}) interface{} {
|
||||
l.Driver.AddCommand("Brightness", func(params map[string]interface{}) interface{} {
|
||||
level := byte(params["level"].(float64))
|
||||
l.Brightness(level)
|
||||
return nil
|
||||
})
|
||||
|
||||
l.Driver.Commands().Add("Toggle", func(params map[string]interface{}) interface{} {
|
||||
l.Driver.AddCommand("Toggle", func(params map[string]interface{}) interface{} {
|
||||
l.Toggle()
|
||||
return nil
|
||||
})
|
||||
|
||||
l.Driver.Commands().Add("On", func(params map[string]interface{}) interface{} {
|
||||
l.Driver.AddCommand("On", func(params map[string]interface{}) interface{} {
|
||||
l.On()
|
||||
return nil
|
||||
})
|
||||
|
||||
l.Driver.Commands().Add("Off", func(params map[string]interface{}) interface{} {
|
||||
l.Driver.AddCommand("Off", func(params map[string]interface{}) interface{} {
|
||||
l.Off()
|
||||
return nil
|
||||
})
|
||||
|
@ -12,10 +12,9 @@ type ServoDriver struct {
|
||||
func NewServoDriver(a Servo, name string, pin string) *ServoDriver {
|
||||
s := &ServoDriver{
|
||||
Driver: gobot.Driver{
|
||||
Name: name,
|
||||
Pin: pin,
|
||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||
Adaptor: a.(gobot.AdaptorInterface),
|
||||
Name: name,
|
||||
Pin: pin,
|
||||
Adaptor: a.(gobot.AdaptorInterface),
|
||||
},
|
||||
CurrentAngle: 0,
|
||||
}
|
||||
|
4
robot.go
4
robot.go
@ -107,7 +107,7 @@ func (r *Robot) Devices() *devices {
|
||||
return r.devices
|
||||
}
|
||||
|
||||
func (r *Robot) Device(name string) DriverInterface {
|
||||
func (r *Robot) Device(name string) Device {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
@ -123,7 +123,7 @@ func (r *Robot) Connections() *connections {
|
||||
return r.connections
|
||||
}
|
||||
|
||||
func (r *Robot) Connection(name string) AdaptorInterface {
|
||||
func (r *Robot) Connection(name string) Connection {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ func (t *testDriver) Halt() bool { return true }
|
||||
func NewTestDriver(name string, adaptor *testAdaptor) *testDriver {
|
||||
t := &testDriver{
|
||||
Driver: Driver{
|
||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||
commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||
Name: name,
|
||||
Adaptor: adaptor,
|
||||
},
|
||||
@ -102,7 +102,7 @@ func NewTestRobot(name string) *Robot {
|
||||
driver2 := NewTestDriver("Device 2", adaptor2)
|
||||
driver3 := NewTestDriver("Device 3", adaptor3)
|
||||
work := func() {}
|
||||
r := NewRobot(name, []AdaptorInterface{adaptor1, adaptor2, adaptor3}, []DriverInterface{driver1, driver2, driver3}, work)
|
||||
r := NewRobot(name, []Connection{adaptor1, adaptor2, adaptor3}, []Device{driver1, driver2, driver3}, work)
|
||||
r.AddCommand("robotTestFunction", func(params map[string]interface{}) interface{} {
|
||||
message := params["message"].(string)
|
||||
robot := params["robot"].(string)
|
||||
|
Loading…
x
Reference in New Issue
Block a user