2013-10-22 16:45:31 -07:00
|
|
|
package gobot
|
|
|
|
|
2013-10-23 22:00:03 -07:00
|
|
|
import (
|
2013-12-30 22:04:23 -08:00
|
|
|
"log"
|
2013-10-23 22:00:03 -07:00
|
|
|
)
|
2013-10-22 16:45:31 -07:00
|
|
|
|
2013-12-18 15:25:07 -08:00
|
|
|
type device struct {
|
2014-01-26 18:55:27 -08:00
|
|
|
Name string `json:"name"`
|
|
|
|
Interval string `json:"-"`
|
|
|
|
Robot *Robot `json:"-"`
|
|
|
|
Driver DriverInterface `json:"driver"`
|
2013-10-22 16:45:31 -07:00
|
|
|
}
|
|
|
|
|
2013-12-18 15:25:07 -08:00
|
|
|
type Device interface {
|
2014-03-31 15:26:35 -07:00
|
|
|
Init() bool
|
2013-12-18 15:25:07 -08:00
|
|
|
Start() bool
|
2014-03-31 14:25:20 -07:00
|
|
|
Halt() bool
|
2013-12-18 15:25:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDevice(driver DriverInterface, r *Robot) *device {
|
|
|
|
d := new(device)
|
|
|
|
d.Name = FieldByNamePtr(driver, "Name").String()
|
2013-11-13 20:44:54 -08:00
|
|
|
d.Robot = r
|
2013-12-18 15:25:07 -08:00
|
|
|
if FieldByNamePtr(driver, "Interval").String() == "" {
|
|
|
|
FieldByNamePtr(driver, "Interval").SetString("0.1s")
|
2013-12-15 14:26:16 -08:00
|
|
|
}
|
2013-11-13 20:44:54 -08:00
|
|
|
d.Driver = driver
|
|
|
|
return d
|
2013-10-22 16:45:31 -07:00
|
|
|
}
|
|
|
|
|
2014-03-31 15:26:35 -07:00
|
|
|
func (d *device) Init() bool {
|
|
|
|
log.Println("Device " + d.Name + " initialized")
|
|
|
|
return d.Driver.Init()
|
|
|
|
}
|
|
|
|
|
2013-12-18 15:25:07 -08:00
|
|
|
func (d *device) Start() bool {
|
2013-12-30 22:04:23 -08:00
|
|
|
log.Println("Device " + d.Name + " started")
|
2013-12-30 16:51:21 -08:00
|
|
|
return d.Driver.Start()
|
2013-10-22 16:45:31 -07:00
|
|
|
}
|
2013-10-28 18:50:09 -07:00
|
|
|
|
2014-03-31 14:25:20 -07:00
|
|
|
func (d *device) Halt() bool {
|
|
|
|
log.Println("Device " + d.Name + " halted")
|
|
|
|
return d.Driver.Halt()
|
|
|
|
}
|
|
|
|
|
2013-12-18 15:25:07 -08:00
|
|
|
func (d *device) Commands() interface{} {
|
|
|
|
return FieldByNamePtr(d.Driver, "Commands").Interface()
|
2013-10-22 16:45:31 -07:00
|
|
|
}
|