mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-24 13:48:49 +08:00
More WIP restructure
This commit is contained in:
parent
90ee5d7d70
commit
eca3a1ca99
@ -1,4 +1,4 @@
|
||||
package adaptor
|
||||
package gobot
|
||||
|
||||
type Adaptor struct {
|
||||
Name string `json:"name"`
|
@ -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
|
@ -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()
|
||||
}
|
@ -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
|
||||
}
|
@ -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()
|
||||
}
|
||||
|
15
gobot.go
15
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
|
||||
|
@ -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 {
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package robot
|
||||
package gobot
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
@ -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
|
||||
}
|
||||
|
||||
*/
|
||||
|
@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package gobot
|
||||
|
||||
import (
|
||||
"math"
|
@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package gobot
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
Loading…
x
Reference in New Issue
Block a user