diff --git a/adaptor.go b/adaptor.go index 259b40535..9a7192a04 100644 --- a/adaptor.go +++ b/adaptor.go @@ -17,6 +17,7 @@ type AdaptorInterface interface { name() string setName(string) params() map[string]interface{} + ToJSON() *JSONConnection } func (a *Adaptor) port() string { diff --git a/api/api.go b/api/api.go index 6cf2106cd..8ef04278e 100644 --- a/api/api.go +++ b/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)) diff --git a/api/api_test.go b/api/api_test.go index f46ca0d25..57317a5d3 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -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 } diff --git a/command.go b/command.go deleted file mode 100644 index 7f5ac3204..000000000 --- a/command.go +++ /dev/null @@ -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) - } -} diff --git a/driver.go b/driver.go index 594ea3435..c1f7dcced 100644 --- a/driver.go +++ b/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) } diff --git a/gobot.go b/gobot.go index 24bfb36e9..d6da89f4d 100644 --- a/gobot.go +++ b/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()) diff --git a/platforms/gpio/analog_sensor_driver.go b/platforms/gpio/analog_sensor_driver.go index d3996f896..56f518c60 100644 --- a/platforms/gpio/analog_sensor_driver.go +++ b/platforms/gpio/analog_sensor_driver.go @@ -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{} { diff --git a/platforms/gpio/direct_pin_driver.go b/platforms/gpio/direct_pin_driver.go index 0c889a571..6bde19ac2 100644 --- a/platforms/gpio/direct_pin_driver.go +++ b/platforms/gpio/direct_pin_driver.go @@ -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), }, } diff --git a/platforms/gpio/led_driver.go b/platforms/gpio/led_driver.go index 00ac87505..de103111f 100644 --- a/platforms/gpio/led_driver.go +++ b/platforms/gpio/led_driver.go @@ -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 }) diff --git a/platforms/gpio/servo_driver.go b/platforms/gpio/servo_driver.go index 6664404e3..46b161a45 100644 --- a/platforms/gpio/servo_driver.go +++ b/platforms/gpio/servo_driver.go @@ -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, } diff --git a/robot.go b/robot.go index c70a6f602..27481688a 100644 --- a/robot.go +++ b/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 } diff --git a/test_helper.go b/test_helper.go index a7706c627..b51ca17bb 100644 --- a/test_helper.go +++ b/test_helper.go @@ -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)