forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.go
37 lines (32 loc) · 829 Bytes
/
device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package gobot
import (
"fmt"
"reflect"
)
type Device struct {
Name string
Interval string
Robot *Robot `json:"-"`
Driver interface{}
Params map[string]string
}
func NewDevice(driver interface{}, r *Robot) *Device {
d := new(Device)
d.Name = reflect.ValueOf(driver).Elem().FieldByName("Name").String()
d.Robot = r
if reflect.ValueOf(driver).Elem().FieldByName("Interval").String() == "" {
reflect.ValueOf(driver).Elem().FieldByName("Interval").SetString("0.1s")
}
d.Driver = driver
return d
}
func (d *Device) Start() {
fmt.Println("Device " + d.Name + " started")
r := reflect.ValueOf(d.Driver).MethodByName("StartDriver")
if r.IsValid() {
r.Call([]reflect.Value{})
}
}
func (d *Device) Commands() interface{} {
return reflect.ValueOf(d.Driver).Elem().FieldByName("Commands").Interface()
}