From eca3a1ca99d7dfc786d94976e88ce988649d97bf Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Wed, 30 Apr 2014 08:10:44 -0700 Subject: [PATCH] More WIP restructure --- core/adaptor/adaptor.go => adaptor.go | 2 +- {core/api => api}/api.go | 0 {core/api => api}/api_convention.go | 0 {core/api => api}/api_test.go | 0 core/robot/connection.go => connection.go | 32 +++++++------- core/robot/device.go => device.go | 32 ++++++-------- core/driver/driver.go => driver.go | 4 +- examples/firmata_blink.go | 23 ++++------ gobot.go | 15 +++---- platforms/firmata/firmata_adaptor.go | 6 ++- platforms/gpio/led_driver.go | 6 ++- core/robot/robot.go => robot.go | 51 +++++++++-------------- core/robot/robot_test.go => robot_test.go | 2 +- test_helper.go | 17 ++++---- core/utils/utils.go => utils.go | 2 +- core/utils/utils_test.go => utils_test.go | 2 +- 16 files changed, 88 insertions(+), 106 deletions(-) rename core/adaptor/adaptor.go => adaptor.go (94%) rename {core/api => api}/api.go (100%) rename {core/api => api}/api_convention.go (100%) rename {core/api => api}/api_test.go (100%) rename core/robot/connection.go => connection.go (60%) rename core/robot/device.go => device.go (52%) rename core/driver/driver.go => driver.go (92%) rename core/robot/robot.go => robot.go (63%) rename core/robot/robot_test.go => robot_test.go (98%) rename core/utils/utils.go => utils.go (99%) rename core/utils/utils_test.go => utils_test.go (99%) diff --git a/core/adaptor/adaptor.go b/adaptor.go similarity index 94% rename from core/adaptor/adaptor.go rename to adaptor.go index 33a892ac3..09772775f 100644 --- a/core/adaptor/adaptor.go +++ b/adaptor.go @@ -1,4 +1,4 @@ -package adaptor +package gobot type Adaptor struct { Name string `json:"name"` diff --git a/core/api/api.go b/api/api.go similarity index 100% rename from core/api/api.go rename to api/api.go diff --git a/core/api/api_convention.go b/api/api_convention.go similarity index 100% rename from core/api/api_convention.go rename to api/api_convention.go diff --git a/core/api/api_test.go b/api/api_test.go similarity index 100% rename from core/api/api_test.go rename to api/api_test.go diff --git a/core/robot/connection.go b/connection.go similarity index 60% rename from core/robot/connection.go rename to connection.go index 912ef11af..61e3a8aa8 100644 --- a/core/robot/connection.go +++ b/connection.go @@ -1,27 +1,25 @@ -package robot +package gobot import ( "errors" - "github.com/hybridgroup/gobot/core/adaptor" - "github.com/hybridgroup/gobot/core/utils" "log" "reflect" ) -type connection struct { - Name string `json:"name"` - Type string `json:"adaptor"` - Adaptor adaptor.AdaptorInterface `json:"-"` - Port string `json:"-"` - Robot *Robot `json:"-"` - Params map[string]interface{} `json:"-"` -} - type Connection interface { Connect() bool Finalize() bool } +type connection struct { + Name string `json:"name"` + Type string `json:"adaptor"` + Adaptor AdaptorInterface `json:"-"` + Port string `json:"-"` + Robot *Robot `json:"-"` + Params map[string]interface{} `json:"-"` +} + type connections []*connection // Start() starts all the connections. @@ -45,16 +43,16 @@ func (c connections) Finalize() { } } -func NewConnection(adaptor adaptor.AdaptorInterface, r *Robot) *connection { +func NewConnection(adaptor AdaptorInterface, r *Robot) *connection { c := new(connection) s := reflect.ValueOf(adaptor).Type().String() c.Type = s[1:len(s)] - c.Name = utils.FieldByNamePtr(adaptor, "Name").String() - c.Port = utils.FieldByNamePtr(adaptor, "Port").String() + c.Name = FieldByNamePtr(adaptor, "Name").String() + c.Port = FieldByNamePtr(adaptor, "Port").String() c.Params = make(map[string]interface{}) - keys := utils.FieldByNamePtr(adaptor, "Params").MapKeys() + keys := FieldByNamePtr(adaptor, "Params").MapKeys() for k := range keys { - c.Params[keys[k].String()] = utils.FieldByNamePtr(adaptor, "Params").MapIndex(keys[k]) + c.Params[keys[k].String()] = FieldByNamePtr(adaptor, "Params").MapIndex(keys[k]) } c.Robot = r c.Adaptor = adaptor diff --git a/core/robot/device.go b/device.go similarity index 52% rename from core/robot/device.go rename to device.go index 429deaa2d..4296f0b28 100644 --- a/core/robot/device.go +++ b/device.go @@ -1,25 +1,22 @@ -package robot +package gobot import ( "errors" - "github.com/hybridgroup/gobot/core/driver" - "github.com/hybridgroup/gobot/core/utils" "log" "reflect" ) type Device interface { - Init() bool Start() bool Halt() bool } type device struct { - Name string `json:"name"` - Type string `json:"driver"` - Interval string `json:"-"` - Robot *Robot `json:"-"` - Driver driver.DriverInterface `json:"-"` + Name string `json:"name"` + Type string `json:"driver"` + Interval string `json:"-"` + Robot *Robot `json:"-"` + Driver DriverInterface `json:"-"` } type devices []*device @@ -31,7 +28,7 @@ func (d devices) Start() error { for _, device := range d { log.Println("Starting device " + device.Name + "...") if device.Start() == false { - err = errors.New("Could not start connection") + err = errors.New("Could not start device") break } } @@ -45,24 +42,19 @@ func (d devices) Halt() { } } -func NewDevice(driver driver.DriverInterface, r *Robot) *device { +func NewDevice(driver DriverInterface, r *Robot) *device { d := new(device) s := reflect.ValueOf(driver).Type().String() d.Type = s[1:len(s)] - d.Name = utils.FieldByNamePtr(driver, "Name").String() + d.Name = FieldByNamePtr(driver, "Name").String() d.Robot = r - if utils.FieldByNamePtr(driver, "Interval").String() == "" { - utils.FieldByNamePtr(driver, "Interval").SetString("0.1s") + if FieldByNamePtr(driver, "Interval").String() == "" { + FieldByNamePtr(driver, "Interval").SetString("0.1s") } d.Driver = driver return d } -func (d *device) Init() bool { - log.Println("Device " + d.Name + " initialized") - return d.Driver.Init() -} - func (d *device) Start() bool { log.Println("Device " + d.Name + " started") return d.Driver.Start() @@ -74,5 +66,5 @@ func (d *device) Halt() bool { } func (d *device) Commands() interface{} { - return utils.FieldByNamePtr(d.Driver, "Commands").Interface() + return FieldByNamePtr(d.Driver, "Commands").Interface() } diff --git a/core/driver/driver.go b/driver.go similarity index 92% rename from core/driver/driver.go rename to driver.go index 7231f74f8..35fac7b27 100644 --- a/core/driver/driver.go +++ b/driver.go @@ -1,4 +1,4 @@ -package driver +package gobot type Driver struct { Interval string `json:"interval"` @@ -9,7 +9,7 @@ type Driver struct { } type DriverInterface interface { - Init() bool + //Init() bool Start() bool Halt() bool } diff --git a/examples/firmata_blink.go b/examples/firmata_blink.go index 3aaef1917..0a8d0ff70 100644 --- a/examples/firmata_blink.go +++ b/examples/firmata_blink.go @@ -2,18 +2,16 @@ package main import ( "github.com/hybridgroup/gobot" - "github.com/hybridgroup/gobot/firmata" - "github.com/hybridgroup/gobot/gpio" + "github.com/hybridgroup/gobot/platforms/firmata" + "github.com/hybridgroup/gobot/platforms/gpio" ) func main() { - firmataAdaptor := firmata.NewFirmataAdaptor() - firmataAdaptor.Name = "firmata" - firmataAdaptor.Port = "/dev/ttyACM0" + gbot := gobot.NewGobot() - led := gpio.NewLedDriver(firmataAdaptor) - led.Name = "led" - led.Pin = "13" + firmataAdaptor := firmata.NewFirmataAdaptor("myFirmata", "/dev/ttyACM0") + + led := gpio.NewLedDriver(firmataAdaptor, "myLed", "13") work := func() { gobot.Every("1s", func() { @@ -21,11 +19,6 @@ func main() { }) } - robot := gobot.Robot{ - Connections: []gobot.Connection{firmataAdaptor}, - Devices: []gobot.Device{led}, - Work: work, - } - - robot.Start() + gbot.Robots = append(gbot.Robots, gobot.NewRobot("Jerry", []gobot.Connection{firmataAdaptor}, []gobot.Device{led}, work)) + gbot.Start() } diff --git a/gobot.go b/gobot.go index 7c4660c84..356757941 100644 --- a/gobot.go +++ b/gobot.go @@ -1,13 +1,13 @@ package gobot import ( - "github.com/hybridgroup/gobot/core/robot" + "log" "os" "os/signal" ) type Gobot struct { - Robots robot.Robots + Robots []*Robot trap func(chan os.Signal) } @@ -20,20 +20,21 @@ func NewGobot() *Gobot { } func (g *Gobot) Start() { - g.Robots.Start() + Robots(g.Robots).Start() c := make(chan os.Signal, 1) g.trap(c) // waiting for interrupt coming on the channel _ = <-c - g.Robots.Each(func(r *robot.Robot) { - r.GetDevices().Halt() - r.GetConnections().Finalize() + Robots(g.Robots).Each(func(r *Robot) { + log.Println("Stopping Robot", r.Name, "...") + r.Devices().Halt() + r.Connections().Finalize() }) } -func (g *Gobot) Robot(name string) *robot.Robot { +func (g *Gobot) Robot(name string) *Robot { for _, r := range g.Robots { if r.Name == name { return r diff --git a/platforms/firmata/firmata_adaptor.go b/platforms/firmata/firmata_adaptor.go index 9668b701e..dc4c132fb 100644 --- a/platforms/firmata/firmata_adaptor.go +++ b/platforms/firmata/firmata_adaptor.go @@ -14,8 +14,12 @@ type FirmataAdaptor struct { connect func(*FirmataAdaptor) } -func NewFirmataAdaptor() *FirmataAdaptor { +func NewFirmataAdaptor(name, port string) *FirmataAdaptor { return &FirmataAdaptor{ + Adaptor: gobot.Adaptor{ + Name: name, + Port: port, + }, connect: func(f *FirmataAdaptor) { sp, err := serial.OpenPort(&serial.Config{Name: f.Port, Baud: 57600}) if err != nil { diff --git a/platforms/gpio/led_driver.go b/platforms/gpio/led_driver.go index 31b894207..2923de10d 100644 --- a/platforms/gpio/led_driver.go +++ b/platforms/gpio/led_driver.go @@ -10,9 +10,11 @@ type LedDriver struct { High bool } -func NewLedDriver(l PwmDigitalWriter) *LedDriver { +func NewLedDriver(a PwmDigitalWriter, name, pin string) *LedDriver { return &LedDriver{ Driver: gobot.Driver{ + Name: name, + Pin: pin, Commands: []string{ "ToggleC", "OnC", @@ -21,7 +23,7 @@ func NewLedDriver(l PwmDigitalWriter) *LedDriver { }, }, High: false, - Adaptor: l, + Adaptor: a, } } diff --git a/core/robot/robot.go b/robot.go similarity index 63% rename from core/robot/robot.go rename to robot.go index 3142301ac..48bf960ca 100644 --- a/core/robot/robot.go +++ b/robot.go @@ -1,16 +1,13 @@ -package robot +package gobot import ( "fmt" - "github.com/hybridgroup/gobot/core/utils" "log" "math/rand" "time" ) type Robot struct { - Connections []Connection `json:"connections"` - Devices []Device `json:"devices"` Name string `json:"name"` Commands map[string]interface{} `json:"-"` RobotCommands []string `json:"commands"` @@ -35,27 +32,27 @@ func (r Robots) Each(f func(*Robot)) { func NewRobot(name string, c []Connection, d []Device, work func()) *Robot { r := &Robot{ - Name: name, - Connections: c, - Devices: d, - Work: work, + Name: name, + Work: work, } r.initName() + log.Println("Initializing Robot", r.Name, "...") r.initCommands() - r.initConnections() - r.initDevices() + r.initConnections(c) + r.initDevices(d) return r } func (r *Robot) Start() { - // if !r.startConnections() { - if err := r.GetConnections().Start(); err != nil { + log.Println("Starting Robot", r.Name, "...") + if err := r.Connections().Start(); err != nil { panic("Could not start connections") } - if err := r.GetDevices().Start(); err != nil { + if err := r.Devices().Start(); err != nil { panic("Could not start devices") } if r.Work != nil { + log.Println("Starting work...") r.Work() } } @@ -75,33 +72,25 @@ func (r *Robot) initCommands() { } } -func (r *Robot) initConnections() { - r.connections = make(connections, len(r.Connections)) +func (r *Robot) initConnections(c []Connection) { + r.connections = make(connections, len(c)) log.Println("Initializing connections...") - for i, connection := range r.Connections { - log.Println("Initializing connection ", utils.FieldByNamePtr(connection, "Name"), "...") + for i, connection := range c { + log.Println("Initializing connection", FieldByNamePtr(connection, "Name"), "...") r.connections[i] = NewConnection(connection, r) } } -func (r *Robot) initDevices() bool { - r.devices = make([]*device, len(r.Devices)) +func (r *Robot) initDevices(d []Device) { + r.devices = make([]*device, len(d)) log.Println("Initializing devices...") - for i, device := range r.Devices { + for i, device := range d { + log.Println("Initializing device", FieldByNamePtr(device, "Name"), "...") r.devices[i] = NewDevice(device, r) } - success := true - for _, device := range r.devices { - log.Println("Initializing device " + device.Name + "...") - if device.Init() == false { - success = false - break - } - } - return success } -func (r *Robot) GetDevices() devices { +func (r *Robot) Devices() devices { return devices(r.devices) } @@ -117,7 +106,7 @@ func (r *Robot) Device(name string) *device { return nil } -func (r *Robot) GetConnections() connections { +func (r *Robot) Connections() connections { return connections(r.connections) } diff --git a/core/robot/robot_test.go b/robot_test.go similarity index 98% rename from core/robot/robot_test.go rename to robot_test.go index eae6bc288..866cf0c41 100644 --- a/core/robot/robot_test.go +++ b/robot_test.go @@ -1,4 +1,4 @@ -package robot +package gobot import ( . "github.com/onsi/ginkgo" diff --git a/test_helper.go b/test_helper.go index 7ba630f0f..851f475e6 100644 --- a/test_helper.go +++ b/test_helper.go @@ -1,10 +1,11 @@ package gobot +/* + import ( "fmt" - . "github.com/hybridgroup/gobot/core/adaptor" - . "github.com/hybridgroup/gobot/core/driver" - . "github.com/hybridgroup/gobot/core/robot" + . "github.com/hybridgroup/gobot/adaptor" + . "github.com/hybridgroup/gobot/driver" ) type testStruct struct { @@ -77,10 +78,10 @@ func newTestRobot(name string) *Robot { driver2 := newTestDriver("Device 2", adaptor2) driver3 := newTestDriver("Device 3", adaptor3) return &Robot{ - Name: name, - Connections: []Connection{adaptor1, adaptor2, adaptor3}, - Devices: []Device{driver1, driver2, driver3}, - Work: func() {}, + Name: name, + //Connections: []Connection{adaptor1, adaptor2, adaptor3}, + //Devices: []Device{driver1, driver2, driver3}, + Work: func() {}, Commands: map[string]interface{}{ "robotTestFunction": robotTestFunction, }, @@ -93,3 +94,5 @@ func newTestStruct() *testStruct { s.f = 0.2 return s } + +*/ diff --git a/core/utils/utils.go b/utils.go similarity index 99% rename from core/utils/utils.go rename to utils.go index a802d2a49..e41f14054 100644 --- a/core/utils/utils.go +++ b/utils.go @@ -1,4 +1,4 @@ -package utils +package gobot import ( "math" diff --git a/core/utils/utils_test.go b/utils_test.go similarity index 99% rename from core/utils/utils_test.go rename to utils_test.go index 3b09cc274..743dde4ca 100644 --- a/core/utils/utils_test.go +++ b/utils_test.go @@ -1,4 +1,4 @@ -package utils +package gobot import ( . "github.com/onsi/ginkgo"