Skip to content

Commit

Permalink
More WIP restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Apr 30, 2014
1 parent 90ee5d7 commit eca3a1c
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 106 deletions.
2 changes: 1 addition & 1 deletion core/adaptor/adaptor.go → adaptor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package adaptor
package gobot

type Adaptor struct {
Name string `json:"name"`
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 15 additions & 17 deletions core/robot/connection.go → connection.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
32 changes: 12 additions & 20 deletions core/robot/device.go → device.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
}
Expand All @@ -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()
Expand All @@ -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()
}
4 changes: 2 additions & 2 deletions core/driver/driver.go → driver.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package driver
package gobot

type Driver struct {
Interval string `json:"interval"`
Expand All @@ -9,7 +9,7 @@ type Driver struct {
}

type DriverInterface interface {
Init() bool
//Init() bool
Start() bool
Halt() bool
}
23 changes: 8 additions & 15 deletions examples/firmata_blink.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,23 @@ 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() {
led.Toggle()
})
}

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 changes: 8 additions & 7 deletions gobot.go
Original file line number Diff line number Diff line change
@@ -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)
}

Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion platforms/firmata/firmata_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions platforms/gpio/led_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -21,7 +23,7 @@ func NewLedDriver(l PwmDigitalWriter) *LedDriver {
},
},
High: false,
Adaptor: l,
Adaptor: a,
}
}

Expand Down
51 changes: 20 additions & 31 deletions core/robot/robot.go → robot.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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()
}
}
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion core/robot/robot_test.go → robot_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package robot
package gobot

import (
. "github.com/onsi/ginkgo"
Expand Down
Loading

0 comments on commit eca3a1c

Please sign in to comment.