Skip to content

Commit

Permalink
WIP refactor device and connection
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 24, 2014
1 parent 73ea4e5 commit e10d617
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 257 deletions.
22 changes: 22 additions & 0 deletions adaptor.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package gobot

import "fmt"

type Adaptor struct {
Name string
Port string
Connected bool
Params map[string]interface{}
Type string
}

type AdaptorInterface interface {
Expand All @@ -31,3 +34,22 @@ func (a *Adaptor) setName(s string) {
func (a *Adaptor) params() map[string]interface{} {
return a.Params
}

func (a *Adaptor) ToJSON() *JSONConnection {
return &JSONConnection{
Name: a.Name,
Port: a.Port,
Adaptor: a.Type,
}
}

func NewAdaptor(name, port, t string) *Adaptor {
if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
}
return &Adaptor{
Type: t,
Name: name,
Port: port,
}
}
17 changes: 17 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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)
}
}
86 changes: 19 additions & 67 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,40 @@ package gobot

import (
"errors"
"fmt"
"log"
"reflect"
)

type Connection interface {
Connect() bool
Finalize() bool
port() string
name() string
setName(string)
params() map[string]interface{}
}

type JSONConnection struct {
Name string `json:"name"`
Port string `json:"port"`
Adaptor string `json:"adaptor"`
}

type connection struct {
Name string
Type string
Adaptor AdaptorInterface
Robot *Robot
type connections struct {
connections []AdaptorInterface
}

func (c *connections) Len() int {
return len(c.connections)
}

type connections []*connection
func (c *connections) Add(a AdaptorInterface) AdaptorInterface {
c.connections = append(c.connections, a)
return a
}

func (c *connections) Each(f func(AdaptorInterface)) {
for _, connection := range c.connections {
f(connection)
}
}

// Start() starts all the connections.
func (c connections) Start() error {
var err error
log.Println("Starting connections...")
for _, connection := range c {
log.Println("Starting connection " + connection.Name + "...")
for _, connection := range c.connections {
log.Println("Starting connection " + connection.name() + "...")
if connection.Connect() == false {
err = errors.New("Could not start connection")
break
Expand All @@ -47,54 +46,7 @@ func (c connections) Start() error {

// Filanize() finalizes all the connections.
func (c connections) Finalize() {
for _, connection := range c {
for _, connection := range c.connections {
connection.Finalize()
}
}

func NewConnection(adaptor AdaptorInterface, r *Robot) *connection {
if adaptor.name() == "" {
adaptor.setName(fmt.Sprintf("%X", Rand(int(^uint(0)>>1))))
}
t := reflect.ValueOf(adaptor).Type().String()
return &connection{
Type: t[1:len(t)],
Name: adaptor.name(),
Robot: r,
Adaptor: adaptor,
}
}

func (c *connection) Connect() bool {
log.Println("Connecting to " + c.Name + " on port " + c.port() + "...")
return c.Adaptor.Connect()
}

func (c *connection) Finalize() bool {
log.Println("Finalizing " + c.Name + "...")
return c.Adaptor.Finalize()
}

func (c *connection) ToJSON() *JSONConnection {
return &JSONConnection{
Name: c.Name,
Port: c.port(),
Adaptor: c.Type,
}
}

func (c *connection) port() string {
return c.Adaptor.port()
}

func (c *connection) name() string {
return c.Name
}

func (c *connection) setName(s string) {
c.Name = s
}

func (c *connection) params() map[string]interface{} {
return c.Adaptor.params()
}
116 changes: 19 additions & 97 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,41 @@ package gobot

import (
"errors"
"fmt"
"log"
"reflect"
"time"
)

type Device interface {
Start() bool
Halt() bool
setInterval(time.Duration)
interval() time.Duration
setName(string)
name() string
adaptor() AdaptorInterface
commands() map[string]func(map[string]interface{}) interface{}
}

type JSONDevice struct {
Name string `json:"name"`
Driver string `json:"driver"`
Connection *JSONConnection `json:"connection"`
Commands []string `json:"commands"`
}

type device struct {
Name string
Type string
Robot *Robot
Driver DriverInterface
type devices struct {
devices []DriverInterface
}

func (d *devices) Len() int {
return len(d.devices)
}

func (d *devices) Add(dev DriverInterface) DriverInterface {
d.devices = append(d.devices, dev)
return dev
}

type devices []*device
func (d *devices) Each(f func(DriverInterface)) {
for _, device := range d.devices {
f(device)
}
}

// Start() starts all the devices.
func (d devices) Start() error {
var err error
log.Println("Starting devices...")
for _, device := range d {
log.Println("Starting device " + device.Name + "...")
for _, device := range d.devices {
log.Println("Starting device " + device.name() + "...")
if device.Start() == false {
err = errors.New("Could not start device")
break
Expand All @@ -51,81 +47,7 @@ func (d devices) Start() error {

// Halt() stop all the devices.
func (d devices) Halt() {
for _, device := range d {
for _, device := range d.devices {
device.Halt()
}
}

func NewDevice(driver DriverInterface, r *Robot) *device {
if driver.name() == "" {
driver.setName(fmt.Sprintf("%X", Rand(int(^uint(0)>>1))))
}
t := reflect.ValueOf(driver).Type().String()
if driver.interval() == 0 {
driver.setInterval(10 * time.Millisecond)
}
return &device{
Type: t[1:len(t)],
Name: driver.name(),
Robot: r,
Driver: driver,
}
}

func (d *device) adaptor() AdaptorInterface {
return d.Driver.adaptor()
}

func (d *device) setInterval(t time.Duration) {
d.Driver.setInterval(t)
}

func (d *device) interval() time.Duration {
return d.Driver.interval()
}

func (d *device) setName(s string) {
d.Name = s
}

func (d *device) name() string {
return d.Name
}

func (d *device) commands() map[string]func(map[string]interface{}) interface{} {
return d.Driver.commands()
}

func (d *device) Commands() map[string]func(map[string]interface{}) interface{} {
return d.commands()
}

func (d *device) Start() bool {
log.Println("Device " + d.Name + " started")
return d.Driver.Start()
}

func (d *device) Halt() bool {
log.Println("Device " + d.Name + " halted")
return d.Driver.Halt()
}

func (d *device) ToJSON() *JSONDevice {
jsonDevice := &JSONDevice{
Name: d.Name,
Driver: d.Type,
Commands: []string{},
Connection: nil,
}

if d.adaptor() != nil {
jsonDevice.Connection = d.Robot.Connection(d.adaptor().name()).ToJSON()
}

commands := d.commands()
for command := range commands {
jsonDevice.Commands = append(jsonDevice.Commands, command)
}

return jsonDevice
}
40 changes: 39 additions & 1 deletion driver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gobot

import "time"
import (
"fmt"
"time"
)

type Driver struct {
Adaptor AdaptorInterface
Expand All @@ -9,6 +12,7 @@ type Driver struct {
Name string
Commands map[string]func(map[string]interface{}) interface{}
Events map[string]*Event
Type string
}

type DriverInterface interface {
Expand All @@ -20,6 +24,7 @@ type DriverInterface interface {
setName(string)
name() string
commands() map[string]func(map[string]interface{}) interface{}
ToJSON() *JSONDevice
}

func (d *Driver) adaptor() AdaptorInterface {
Expand Down Expand Up @@ -49,3 +54,36 @@ func (d *Driver) commands() map[string]func(map[string]interface{}) interface{}
func (d *Driver) AddCommand(name string, f func(map[string]interface{}) interface{}) {
d.Commands[name] = f
}

func NewDriver(name string, t string, commands Commands, a AdaptorInterface) *Driver {
if name == "" {
name = fmt.Sprintf("%X", Rand(int(^uint(0)>>1)))
}
return &Driver{
Type: t,
Name: name,
Interval: 10 * time.Millisecond,
Commands: commands,
Adaptor: a,
}
}

func (d *Driver) ToJSON() *JSONDevice {
jsonDevice := &JSONDevice{
Name: d.Name,
Driver: d.Type,
Commands: []string{},
Connection: nil,
}

if d.adaptor() != nil {
//jsonDevice.Connection = d.Robot.Connection(d.adaptor().name()).ToJSON()
}

commands := d.commands()
for command := range commands {
jsonDevice.Commands = append(jsonDevice.Commands, command)
}

return jsonDevice
}
Loading

0 comments on commit e10d617

Please sign in to comment.