Skip to content

Commit

Permalink
Switch to adaptor, driver, connection and device interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Dec 18, 2013
1 parent 6f01605 commit 05bdb70
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 88 deletions.
35 changes: 5 additions & 30 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,9 @@ type Adaptor struct {
Params map[string]interface{}
}

func (Adaptor) NewAdaptor(a Adaptor) Adaptor {
return a
}

func (a *Adaptor) Finalize() bool {
if a.IsConnected() {
a.Disconnect()
}
return true
}

func (a *Adaptor) Connect() bool {
a.Connected = true
return true
}

func (a *Adaptor) Disconnect() bool {
a.Connected = false
return true
}

func (a *Adaptor) Reconnect() bool {
if !a.IsConnected() {
return a.Connect()
}
return true
}

func (a *Adaptor) IsConnected() bool {
return a.Connected
type AdaptorInterface interface {
Finalize() bool
Connect() bool
Disconnect() bool
Reconnect() bool
}
50 changes: 30 additions & 20 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,54 @@ package gobot

import (
"fmt"
"reflect"
)

type Connection struct {
type connection struct {
Name string
Adaptor interface{}
Port string
Robot *Robot `json:"-"`
Params map[string]interface{}
Adaptor AdaptorInterface
Port string `json:"-"`
Robot *Robot `json:"-"`
Params map[string]interface{} `json:"-"`
}

func NewConnection(a interface{}, r *Robot) *Connection {
c := new(Connection)
c.Name = reflect.ValueOf(a).Elem().FieldByName("Name").String()
c.Port = reflect.ValueOf(a).Elem().FieldByName("Port").String()
type Connection interface {
Connect() bool
Disconnect() bool
Finalize() bool
Reconnect() bool
}

func NewConnection(adaptor AdaptorInterface, r *Robot) *connection {
c := new(connection)
c.Name = FieldByNamePtr(adaptor, "Name").String()
c.Port = FieldByNamePtr(adaptor, "Port").String()
c.Params = make(map[string]interface{})
keys := reflect.ValueOf(a).Elem().FieldByName("Params").MapKeys()
keys := FieldByNamePtr(adaptor, "Params").MapKeys()
for k := range keys {
c.Params[keys[k].String()] = reflect.ValueOf(a).Elem().FieldByName("Params").MapIndex(keys[k])
c.Params[keys[k].String()] = FieldByNamePtr(adaptor, "Params").MapIndex(keys[k])
}
c.Robot = r
c.Adaptor = a
c.Adaptor = adaptor
return c
}

func (c *Connection) Connect() {
func (c *connection) Connect() bool {
fmt.Println("Connecting to " + c.Name + " on port " + c.Port + "...")
reflect.ValueOf(c.Adaptor).MethodByName("Connect").Call([]reflect.Value{})
return c.Adaptor.Connect()
}

func (c *connection) Disconnect() bool {
return c.Adaptor.Disconnect()
}

func (c *Connection) Disconnect() {
reflect.ValueOf(c.Adaptor).MethodByName("Disconnect").Call([]reflect.Value{})
func (c *connection) Finalize() bool {
return c.Adaptor.Finalize()
}

func (c *Connection) IsConnected() bool {
return reflect.ValueOf(c.Adaptor).MethodByName("IsConnected").Call([]reflect.Value{})[0].Bool()
func (c *connection) Reconnect() bool {
return c.Adaptor.Reconnect()
}

func (c *Connection) AdaptorName() string {
func (c *connection) AdaptorName() string {
return c.Name
}
35 changes: 18 additions & 17 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,37 @@ package gobot

import (
"fmt"
"reflect"
)

type Device struct {
type device struct {
Name string
Interval string
Interval string `json:"-"`
Robot *Robot `json:"-"`
Driver interface{}
Params map[string]string
Driver DriverInterface
Params map[string]string `json:"-"`
}

func NewDevice(driver interface{}, r *Robot) *Device {
d := new(Device)
d.Name = reflect.ValueOf(driver).Elem().FieldByName("Name").String()
type Device interface {
Start() bool
}

func NewDevice(driver DriverInterface, r *Robot) *device {
d := new(device)
d.Name = FieldByNamePtr(driver, "Name").String()
d.Robot = r
if reflect.ValueOf(driver).Elem().FieldByName("Interval").String() == "" {
reflect.ValueOf(driver).Elem().FieldByName("Interval").SetString("0.1s")
if FieldByNamePtr(driver, "Interval").String() == "" {
FieldByNamePtr(driver, "Interval").SetString("0.1s")
}
d.Driver = driver
return d
}

func (d *Device) Start() {
func (d *device) Start() bool {
fmt.Println("Device " + d.Name + " started")
r := reflect.ValueOf(d.Driver).MethodByName("StartDriver")
if r.IsValid() {
r.Call([]reflect.Value{})
}
d.Driver.Start()
return true
}

func (d *Device) Commands() interface{} {
return reflect.ValueOf(d.Driver).Elem().FieldByName("Commands").Interface()
func (d *device) Commands() interface{} {
return FieldByNamePtr(d.Driver, "Commands").Interface()
}
10 changes: 2 additions & 8 deletions driver.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package gobot

import "fmt"

type Driver struct {
Interval string
Pin string
Expand All @@ -11,10 +9,6 @@ type Driver struct {
Events map[string]chan interface{} `json:"-"`
}

func NewDriver(d Driver) Driver {
return d
}

func (d *Driver) Start() {
fmt.Println("Starting driver " + d.Name + "...")
type DriverInterface interface {
Start() bool
}
4 changes: 2 additions & 2 deletions master.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (m *Master) FindRobot(name string) *Robot {
return nil
}

func (m *Master) FindRobotDevice(name string, device string) *Device {
func (m *Master) FindRobotDevice(name string, device string) *device {
for r := range m.Robots {
if m.Robots[r].Name == name {
for d := range m.Robots[r].devices {
Expand All @@ -38,7 +38,7 @@ func (m *Master) FindRobotDevice(name string, device string) *Device {
return nil
}

func (m *Master) FindRobotConnection(name string, connection string) *Connection {
func (m *Master) FindRobotConnection(name string, connection string) *connection {
for r := range m.Robots {
if m.Robots[r].Name == name {
for c := range m.Robots[r].connections {
Expand Down
21 changes: 10 additions & 11 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ package gobot
import (
"fmt"
"math/rand"
"reflect"
"time"
)

type Robot struct {
Connections []interface{}
Devices []interface{}
Connections []Connection
Devices []Device
Name string
Commands map[string]interface{} `json:"-"`
RobotCommands []string `json:"Commands"`
Work func() `json:"-"`
connections []*Connection `json:"-"`
devices []*Device `json:"-"`
connections []*connection `json:"-"`
devices []*device `json:"-"`
}

func (r *Robot) Start() {
Expand Down Expand Up @@ -46,19 +45,19 @@ func (r *Robot) initCommands() {
}

func (r *Robot) initConnections() {
r.connections = make([]*Connection, len(r.Connections))
r.connections = make([]*connection, len(r.Connections))
fmt.Println("Initializing connections...")
for i := range r.Connections {
fmt.Println("Initializing connection " + reflect.ValueOf(r.Connections[i]).Elem().FieldByName("Name").String() + "...")
fmt.Sprintln("Initializing connection %v...", FieldByNamePtr(r.Connections[i], "Name"))
r.connections[i] = NewConnection(r.Connections[i], r)
}
}

func (r *Robot) initDevices() {
r.devices = make([]*Device, len(r.Devices))
r.devices = make([]*device, len(r.Devices))
fmt.Println("Initializing devices...")
for i := range r.Devices {
fmt.Println("Initializing device " + reflect.ValueOf(r.Devices[i]).Elem().FieldByName("Name").String() + "...")
fmt.Sprintln("Initializing device %v...", FieldByNamePtr(r.Devices[i], "Name"))
r.devices[i] = NewDevice(r.Devices[i], r)
}
}
Expand All @@ -79,11 +78,11 @@ func (r *Robot) startDevices() {
}
}

func (r *Robot) GetDevices() []*Device {
func (r *Robot) GetDevices() []*device {
return r.devices
}

func (r *Robot) GetDevice(name string) *Device {
func (r *Robot) GetDevice(name string) *device {
for i := range r.devices {
if r.devices[i].Name == name {
return r.devices[i]
Expand Down
7 changes: 7 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ func Call(thing interface{}, method string, params ...interface{}) []reflect.Val
return reflect.ValueOf(thing).MethodByName(method).Call(in)
}

func FieldByName(thing interface{}, field string) reflect.Value {
return reflect.ValueOf(thing).FieldByName(field)
}
func FieldByNamePtr(thing interface{}, field string) reflect.Value {
return reflect.ValueOf(thing).Elem().FieldByName(field)
}

func toJson(obj interface{}) string {
b, _ := json.Marshal(obj)
return string(b)
Expand Down

0 comments on commit 05bdb70

Please sign in to comment.