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