hybridgroup.gobot/device.go

38 lines
829 B
Go
Raw Normal View History

2013-10-23 07:45:31 +08:00
package gobot
2013-10-24 13:00:03 +08:00
import (
2013-11-14 12:44:54 +08:00
"fmt"
"reflect"
2013-10-24 13:00:03 +08:00
)
2013-10-23 07:45:31 +08:00
type Device struct {
2013-11-14 12:44:54 +08:00
Name string
Interval string
2013-11-24 02:36:08 +08:00
Robot *Robot `json:"-"`
2013-11-14 12:44:54 +08:00
Driver interface{}
Params map[string]string
2013-10-23 07:45:31 +08:00
}
2013-10-29 09:50:09 +08:00
func NewDevice(driver interface{}, r *Robot) *Device {
2013-11-14 12:44:54 +08:00
d := new(Device)
d.Name = reflect.ValueOf(driver).Elem().FieldByName("Name").String()
d.Robot = r
2013-12-16 06:29:01 +08:00
if reflect.ValueOf(driver).Elem().FieldByName("Interval").String() == "" {
reflect.ValueOf(driver).Elem().FieldByName("Interval").SetString("0.1s")
2013-12-16 06:26:16 +08:00
}
2013-11-14 12:44:54 +08:00
d.Driver = driver
return d
2013-10-23 07:45:31 +08:00
}
2013-10-29 09:50:09 +08:00
func (d *Device) Start() {
2013-11-14 12:44:54 +08:00
fmt.Println("Device " + d.Name + " started")
r := reflect.ValueOf(d.Driver).MethodByName("StartDriver")
if r.IsValid() {
r.Call([]reflect.Value{})
}
2013-10-23 07:45:31 +08:00
}
2013-10-29 09:50:09 +08:00
2013-11-25 06:41:36 +08:00
func (d *Device) Commands() interface{} {
return reflect.ValueOf(d.Driver).Elem().FieldByName("Commands").Interface()
2013-10-23 07:45:31 +08:00
}