forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
52 lines (44 loc) · 925 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package gobot
import (
"errors"
"log"
)
type JSONDevice struct {
Name string `json:"name"`
Driver string `json:"driver"`
Connection string `json:"connection"`
Commands []string `json:"commands"`
}
type Device DriverInterface
type devices []Device
func (d *devices) Len() int {
return len(*d)
}
func (d *devices) Each(f func(Device)) {
for _, device := range *d {
f(device)
}
}
// Start() starts all the devices.
func (d *devices) Start() error {
var err error
log.Println("Starting devices...")
for _, device := range *d {
info := "Starting device " + device.Name()
if device.Pin() != "" {
info = info + " on pin " + device.Pin()
}
log.Println(info + "...")
if device.Start() == false {
err = errors.New("Could not start device")
break
}
}
return err
}
// Halt() stop all the devices.
func (d *devices) Halt() {
for _, device := range *d {
device.Halt()
}
}