/
devices.go
55 lines (47 loc) · 1.35 KB
/
devices.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 main
import (
"github.com/koestler/go-iotdevice/config"
"github.com/koestler/go-iotdevice/dataflow"
"github.com/koestler/go-iotdevice/device"
"github.com/koestler/go-iotdevice/victron"
"github.com/pkg/errors"
"log"
)
func runDevices(
cfg *config.Config,
target dataflow.Fillable,
initiateShutdown chan<- error,
) (devicePoolInstance *device.DevicePool) {
devicePoolInstance = device.RunPool()
// register creators
device.RegisterCreator(config.RandomBmvKind, device.CreateRandomDeviceFactory(victron.RegisterListBmv712))
device.RegisterCreator(config.RandomSolarKind, device.CreateRandomDeviceFactory(victron.RegisterListSolar))
device.RegisterCreator(config.VedirectKind, victron.CreateVictronDevice)
countStarted := 0
for _, cfgDev := range cfg.Devices() {
if cfg.LogWorkerStart() {
log.Printf(
"deviceClient[%s]: start %s, cfgDev='%s'",
cfgDev.Name(),
cfgDev.Kind().String(),
cfgDev.Device(),
)
}
if dev, err := device.RunDevice(cfgDev, target); err != nil {
log.Printf("deviceClient[%s]: start failed: %s", cfgDev.Name(), err)
} else {
devicePoolInstance.AddDevice(dev)
countStarted += 1
if cfg.LogWorkerStart() {
log.Printf(
"deviceClient[%s]: started",
dev.Config().Name(),
)
}
}
}
if countStarted < 1 {
initiateShutdown <- errors.New("no cfgDev was started")
}
return
}