1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00
hybridgroup.gobot/device.go

132 lines
2.5 KiB
Go
Raw Normal View History

2014-04-30 08:10:44 -07:00
package gobot
2014-04-29 13:20:32 -07:00
import (
"errors"
"fmt"
2014-04-29 13:20:32 -07:00
"log"
"reflect"
2014-05-03 03:31:11 -07:00
"time"
2014-04-29 13:20:32 -07:00
)
type Device interface {
Start() bool
Halt() bool
2014-06-06 14:44:16 -07:00
setInterval(time.Duration)
2014-06-13 10:09:03 -07:00
interval() time.Duration
2014-06-06 14:44:16 -07:00
setName(string)
2014-06-13 10:09:03 -07:00
name() string
adaptor() AdaptorInterface
2014-06-13 10:09:03 -07:00
commands() map[string]func(map[string]interface{}) interface{}
2014-04-29 13:20:32 -07:00
}
2014-06-10 15:16:11 -07:00
type JSONDevice struct {
2014-05-15 11:50:45 -07:00
Name string `json:"name"`
Driver string `json:"driver"`
2014-06-10 15:16:11 -07:00
Connection *JSONConnection `json:"connection"`
2014-05-15 11:50:45 -07:00
Commands []string `json:"commands"`
}
2014-04-29 13:20:32 -07:00
type device struct {
Name string
Type string
Robot *Robot
Driver DriverInterface
2014-04-29 13:20:32 -07:00
}
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 {
2014-04-30 08:10:44 -07:00
err = errors.New("Could not start device")
2014-04-29 13:20:32 -07:00
break
}
}
return err
}
// Halt() stop all the devices.
func (d devices) Halt() {
for _, device := range d {
device.Halt()
}
}
2014-04-30 08:10:44 -07:00
func NewDevice(driver DriverInterface, r *Robot) *device {
if driver.name() == "" {
driver.setName(fmt.Sprintf("%X", Rand(int(^uint(0)>>1))))
}
2014-06-13 10:09:03 -07:00
t := reflect.ValueOf(driver).Type().String()
if driver.interval() == 0 {
2014-06-06 14:44:16 -07:00
driver.setInterval(10 * time.Millisecond)
2014-04-29 13:20:32 -07:00
}
2014-06-13 10:09:03 -07:00
return &device{
Type: t[1:len(t)],
Name: driver.name(),
Robot: r,
Driver: driver,
}
2014-04-29 13:20:32 -07:00
}
func (d *device) adaptor() AdaptorInterface {
return d.Driver.adaptor()
}
2014-06-06 14:44:16 -07:00
func (d *device) setInterval(t time.Duration) {
d.Driver.setInterval(t)
2014-06-06 14:44:16 -07:00
}
2014-06-13 10:09:03 -07:00
func (d *device) interval() time.Duration {
return d.Driver.interval()
2014-06-06 14:44:16 -07:00
}
func (d *device) setName(s string) {
d.Name = s
}
2014-06-13 10:09:03 -07:00
func (d *device) name() string {
2014-06-06 14:44:16 -07:00
return d.Name
}
2014-06-13 10:09:03 -07:00
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()
}
2014-04-29 13:20:32 -07:00
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()
}
2014-06-10 15:16:11 -07:00
func (d *device) ToJSON() *JSONDevice {
2014-06-11 17:41:04 -07:00
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()
2014-06-11 17:41:04 -07:00
}
2014-06-13 10:09:03 -07:00
commands := d.commands()
2014-06-11 17:41:04 -07:00
for command := range commands {
jsonDevice.Commands = append(jsonDevice.Commands, command)
2014-06-10 15:16:11 -07:00
}
2014-06-11 17:41:04 -07:00
return jsonDevice
2014-05-15 11:50:45 -07:00
}