forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
190 changed files
with
307 additions
and
304 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gobot | ||
package adaptor | ||
|
||
type Adaptor struct { | ||
Name string `json:"name"` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gobot | ||
package api | ||
|
||
type jsonRobot struct { | ||
Name string `json:"name"` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gobot | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gobot | ||
package driver | ||
|
||
type Driver struct { | ||
Interval string `json:"interval"` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package robot | ||
|
||
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 connections []*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 + "...") | ||
if connection.Connect() == false { | ||
err = errors.New("Could not start connection") | ||
break | ||
} | ||
} | ||
return err | ||
} | ||
|
||
// Filanize() finalizes all the connections. | ||
func (c connections) Finalize() { | ||
for _, connection := range c { | ||
connection.Finalize() | ||
} | ||
} | ||
|
||
func NewConnection(adaptor 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.Params = make(map[string]interface{}) | ||
keys := utils.FieldByNamePtr(adaptor, "Params").MapKeys() | ||
for k := range keys { | ||
c.Params[keys[k].String()] = utils.FieldByNamePtr(adaptor, "Params").MapIndex(keys[k]) | ||
} | ||
c.Robot = r | ||
c.Adaptor = adaptor | ||
return c | ||
} | ||
|
||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package robot | ||
|
||
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:"-"` | ||
} | ||
|
||
type devices []*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 + "...") | ||
if device.Start() == false { | ||
err = errors.New("Could not start connection") | ||
break | ||
} | ||
} | ||
return err | ||
} | ||
|
||
// Halt() stop all the devices. | ||
func (d devices) Halt() { | ||
for _, device := range d { | ||
device.Halt() | ||
} | ||
} | ||
|
||
func NewDevice(driver 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.Robot = r | ||
if utils.FieldByNamePtr(driver, "Interval").String() == "" { | ||
utils.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() | ||
} | ||
|
||
func (d *device) Halt() bool { | ||
log.Println("Device " + d.Name + " halted") | ||
return d.Driver.Halt() | ||
} | ||
|
||
func (d *device) Commands() interface{} { | ||
return utils.FieldByNamePtr(d.Driver, "Commands").Interface() | ||
} |
Oops, something went wrong.