forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
55 lines (46 loc) · 1005 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
53
54
55
package gobot
import (
"errors"
"log"
)
type JSONDevice struct {
Name string `json:"name"`
Driver string `json:"driver"`
Connection *JSONConnection `json:"connection"`
Commands []string `json:"commands"`
}
type Device DriverInterface
type devices struct {
devices []Device
}
func (d *devices) Len() int {
return len(d.devices)
}
func (d *devices) Add(dev Device) Device {
d.devices = append(d.devices, dev)
return dev
}
func (d *devices) Each(f func(Device)) {
for _, device := range d.devices {
f(device)
}
}
// Start() starts all the devices.
func (d devices) Start() error {
var err error
log.Println("Starting devices...")
for _, device := range d.devices {
log.Println("Starting device " + device.name() + "...")
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.devices {
device.Halt()
}
}