-
Notifications
You must be signed in to change notification settings - Fork 2
/
provider_load.go
73 lines (56 loc) · 2.18 KB
/
provider_load.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package zda
import (
"context"
"github.com/shimmeringbee/logwrap"
"github.com/shimmeringbee/zda/implcaps/factory"
"github.com/shimmeringbee/zigbee"
)
func (z *ZDA) providerLoad() {
ctx, end := z.logger.Segment(z.ctx, "Loading persistence.")
defer end()
for _, i := range z.nodeListFromPersistence() {
z.providerLoadNode(ctx, i)
}
}
func (z *ZDA) providerLoadNode(pctx context.Context, i zigbee.IEEEAddress) {
ctx, end := z.logger.Segment(pctx, "Loading node data.", logwrap.Datum("node", i.String()))
defer end()
n, _ := z.createNode(i)
for _, d := range z.deviceListFromPersistence(i) {
z.providerLoadDevice(ctx, n, d)
}
}
func (z *ZDA) providerLoadDevice(pctx context.Context, n *node, i IEEEAddressWithSubIdentifier) {
ctx, end := z.logger.Segment(pctx, "Loading device data.", logwrap.Datum("device", i.String()))
defer end()
d := z.createSpecificDevice(n, i.SubIdentifier)
devSection := z.sectionForDevice(i)
if id, found := devSection.Int("UniqueId"); found {
z.setDeviceUniqueId(d, int(id))
}
capSection := devSection.Section("Capability")
for _, cName := range capSection.SectionKeys() {
cctx, cend := z.logger.Segment(ctx, "Loading capability data.", logwrap.Datum("capability", cName))
cSection := capSection.Section(cName)
if capImpl, ok := cSection.String("Implementation"); ok {
if capI := factory.Create(capImpl, z.zdaInterface); capI == nil {
z.logger.LogError(cctx, "Could not find capability implementation.", logwrap.Datum("Implementation", capImpl))
continue
} else {
z.logger.LogInfo(cctx, "Constructed capability implementation.", logwrap.Datum("Implementation", capImpl))
capI.Init(d, cSection.Section("Data"))
attached, err := capI.Load(cctx)
if err != nil {
z.logger.LogError(cctx, "Error while loading from persistence.", logwrap.Err(err), logwrap.Datum("Implementation", capImpl))
}
if attached {
z.attachCapabilityToDevice(d, capI)
z.logger.LogInfo(cctx, "Attached capability from persistence.", logwrap.Datum("Implementation", capImpl))
} else {
z.logger.LogWarn(cctx, "Rejected capability attach from persistence.", logwrap.Datum("Implementation", capImpl))
}
}
}
cend()
}
}