forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
52 lines (44 loc) · 1.08 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package gobot
import (
"log"
"reflect"
)
type device struct {
Name string `json:"name"`
Type string `json:"driver"`
Interval string `json:"-"`
Robot *Robot `json:"-"`
Driver DriverInterface `json:"-"`
}
type Device interface {
Init() bool
Start() bool
Halt() bool
}
func NewDevice(driver DriverInterface, r *Robot) *device {
d := new(device)
s := reflect.ValueOf(driver).Type().String()
d.Type = s[1:len(s)]
d.Name = FieldByNamePtr(driver, "Name").String()
d.Robot = r
if FieldByNamePtr(driver, "Interval").String() == "" {
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 FieldByNamePtr(d.Driver, "Commands").Interface()
}