Skip to content

Commit

Permalink
Add reloading devices and capabilties from persistence, add devices a…
Browse files Browse the repository at this point in the history
…nd nodes to persistence.
  • Loading branch information
pwood committed Mar 19, 2024
1 parent 8c6deec commit 9faae42
Show file tree
Hide file tree
Showing 20 changed files with 389 additions and 129 deletions.
5 changes: 3 additions & 2 deletions da_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zda

import (
"context"
"github.com/shimmeringbee/persistence/impl/memory"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
Expand All @@ -18,7 +19,7 @@ func (m *mockEventSender) sendEvent(event interface{}) {

func TestZigbeeGateway_ReadEvent(t *testing.T) {
t.Run("context which expires should result in error", func(t *testing.T) {
zgw := New(context.Background(), nil, nil)
zgw := New(context.Background(), memory.New(), nil, nil)

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
defer cancel()
Expand All @@ -28,7 +29,7 @@ func TestZigbeeGateway_ReadEvent(t *testing.T) {
})

t.Run("sent events are received through ReadEvent", func(t *testing.T) {
zgw := New(context.Background(), nil, nil).(*gateway)
zgw := New(context.Background(), memory.New(), nil, nil).(*gateway)

ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
defer cancel()
Expand Down
1 change: 1 addition & 0 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type device struct {
m *sync.RWMutex
eda *enumeratedDeviceAttachment
dr *deviceRemoval
n *node

// Mutable data, obtain lock first.
deviceId uint16
Expand Down
12 changes: 10 additions & 2 deletions enumerate_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (e enumerateDevice) interrogateNode(ctx context.Context, n *node) (inventor
}

for ep, desc := range inv.endpoints {
if Contains(desc.description.InClusterList, zcl.BasicId) {
if contains(desc.description.InClusterList, zcl.BasicId) {
e.logger.LogTrace(ctx, "Querying vendor information from endpoint.", logwrap.Datum("Endpoint", ep))

resp, err := retry.RetryWithValue(ctx, EnumerationNetworkTimeout, EnumerationNetworkRetries, func(ctx context.Context) ([]global.ReadAttributeResponseRecord, error) {
Expand Down Expand Up @@ -417,10 +417,18 @@ func (e enumerateDevice) enumerateCapabilityOnDevice(ctx context.Context, d *dev
e.logger.LogError(ctx, "Failed to find implementation of capability.")
return false, []error{fmt.Errorf("failed to find concrete implementation: %s", capImplName)}
}

section := e.gw.sectionForDevice(d.address).Section("capability", capabilities.StandardNames[cF])
if err := section.Set("implementation", capImplName); err != nil {
e.logger.LogError(ctx, "Failed to set value on capability persistence.", logwrap.Err(err))
return false, []error{fmt.Errorf("failed to find set value on persistence: %w", err)}
}

c.Init(d, section.Section("data"))
}

e.logger.LogInfo(ctx, "Attaching capability implementation.")
attached, err := c.Attach(ctx, d, implcaps.Enumeration, settings)
attached, err := c.Enumerate(ctx, settings)
if err != nil {
e.logger.LogWarn(ctx, "Errored while attaching new capability.", logwrap.Err(err), logwrap.Datum("Attached", attached))
errs = append(errs, fmt.Errorf("error while attaching: %s: %w", capImplName, err))
Expand Down
9 changes: 6 additions & 3 deletions enumerate_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/shimmeringbee/da/capabilities"
"github.com/shimmeringbee/logwrap"
"github.com/shimmeringbee/logwrap/impl/discard"
"github.com/shimmeringbee/persistence/impl/memory"
"github.com/shimmeringbee/zcl"
"github.com/shimmeringbee/zcl/commands/global"
"github.com/shimmeringbee/zcl/commands/local/basic"
Expand Down Expand Up @@ -469,7 +470,7 @@ func Test_enumerateDevice_updateCapabilitiesOnDevice(t *testing.T) {
t.Run("adds a new capability from rules output", func(t *testing.T) {
mdm := &mockDeviceManager{}
defer mdm.AssertExpectations(t)
ed := enumerateDevice{logger: logwrap.New(discard.Discard()), capabilityFactory: factory.Create, dm: mdm}
ed := enumerateDevice{logger: logwrap.New(discard.Discard()), capabilityFactory: factory.Create, dm: mdm, gw: &gateway{section: memory.New()}}
d := &device{m: &sync.RWMutex{}, deviceId: 1, capabilities: map[da.Capability]implcaps.ZDACapability{}}

id := inventoryDevice{
Expand Down Expand Up @@ -509,7 +510,8 @@ func Test_enumerateDevice_updateCapabilitiesOnDevice(t *testing.T) {
ed := enumerateDevice{logger: logwrap.New(discard.Discard()), capabilityFactory: factory.Create, dm: mdm}
opi := generic.NewProductInformation()
d := &device{m: &sync.RWMutex{}, deviceId: 1, capabilities: map[da.Capability]implcaps.ZDACapability{capabilities.ProductInformationFlag: opi}}
_, _ = opi.Attach(context.Background(), d, implcaps.Enumeration, map[string]interface{}{
opi.Init(d, memory.New())
_, _ = opi.Enumerate(context.Background(), map[string]interface{}{
"Name": "NEXUS-6",
})

Expand Down Expand Up @@ -548,7 +550,8 @@ func Test_enumerateDevice_updateCapabilitiesOnDevice(t *testing.T) {
ed := enumerateDevice{logger: logwrap.New(discard.Discard()), capabilityFactory: factory.Create, dm: mdm}
opi := generic.NewProductInformation()
d := &device{m: &sync.RWMutex{}, deviceId: 1, capabilities: map[da.Capability]implcaps.ZDACapability{capabilities.ProductInformationFlag: opi}}
_, _ = opi.Attach(context.Background(), d, implcaps.Enumeration, map[string]interface{}{
opi.Init(d, memory.New())
_, _ = opi.Enumerate(context.Background(), map[string]interface{}{
"Name": "NEXUS-6",
})
d.capabilities[capabilities.ProductInformationFlag] = opi
Expand Down
7 changes: 6 additions & 1 deletion gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/shimmeringbee/da"
"github.com/shimmeringbee/da/capabilities"
"github.com/shimmeringbee/logwrap"
"github.com/shimmeringbee/persistence"
"github.com/shimmeringbee/zcl"
"github.com/shimmeringbee/zcl/commands/global"
"github.com/shimmeringbee/zcl/communicator"
Expand All @@ -19,7 +20,7 @@ import (

const DefaultGatewayHomeAutomationEndpoint = zigbee.Endpoint(0x01)

func New(baseCtx context.Context, p zigbee.Provider, r ruleExecutor) da.Gateway {
func New(baseCtx context.Context, s persistence.Section, p zigbee.Provider, r ruleExecutor) da.Gateway {
ctx, cancel := context.WithCancel(baseCtx)

zclCommandRegistry := zcl.NewCommandRegistry()
Expand All @@ -39,6 +40,8 @@ func New(baseCtx context.Context, p zigbee.Provider, r ruleExecutor) da.Gateway
nodeLock: &sync.RWMutex{},
node: make(map[zigbee.IEEEAddress]*node),

section: s,

callbacks: callbacks.Create(),
ruleExecutor: r,

Expand Down Expand Up @@ -83,6 +86,8 @@ type gateway struct {
nodeLock *sync.RWMutex
node map[zigbee.IEEEAddress]*node

section persistence.Section

callbacks callbacks.AdderCaller
ruleExecutor ruleExecutor

Expand Down
3 changes: 2 additions & 1 deletion gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/shimmeringbee/da/capabilities"
"github.com/shimmeringbee/logwrap"
"github.com/shimmeringbee/logwrap/impl/discard"
"github.com/shimmeringbee/persistence/impl/memory"
"github.com/shimmeringbee/zigbee"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand All @@ -23,7 +24,7 @@ func newTestGateway() (*gateway, *zigbee.MockProvider, *mock.Call, func(*testing

mRE := mp.On("ReadEvent", mock.Anything).Return(nil, context.Canceled).Maybe()

gw := New(context.Background(), mp, nil)
gw := New(context.Background(), memory.New(), mp, nil)

gw.(*gateway).WithLogWrapLogger(logwrap.New(discard.Discard()))

Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
module github.com/shimmeringbee/zda

go 1.22
go 1.22.0

require (
github.com/antonmedv/expr v1.15.5
github.com/shimmeringbee/callbacks v0.0.0-20221001135028-b85b5f89d5d6
github.com/shimmeringbee/da v0.0.0-20240114214251-b3b74f3b1894
github.com/shimmeringbee/logwrap v0.1.3
github.com/shimmeringbee/persistence v0.0.0-20240318205009-b8100e7c3887
github.com/shimmeringbee/retry v0.0.0-20221006193055-2ce01bf139c2
github.com/shimmeringbee/zcl v0.0.0-20221006205348-732be4c63285
github.com/shimmeringbee/zigbee v0.0.0-20221016122511-6c2328db0d94
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
golang.org/x/sync v0.5.0
)

Expand All @@ -19,6 +20,6 @@ require (
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shimmeringbee/bytecodec v0.0.0-20210228205504-1e9e0677347b // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/stretchr/objx v0.5.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
54 changes: 8 additions & 46 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/antonmedv/expr v1.9.0 h1:j4HI3NHEdgDnN9p6oI6Ndr0G5QryMY0FNxT4ONrFDGU=
github.com/antonmedv/expr v1.9.0/go.mod h1:5qsM3oLGDND7sDmQGDXHkYfkjYMUX14qsgqmHhwGEk8=
github.com/antonmedv/expr v1.9.1-0.20221112134005-497c6bd01cf8 h1:ANpbgpXXI0gdonf79fgpVou3hdgtvGmN2F1qLvY6F/w=
github.com/antonmedv/expr v1.9.1-0.20221112134005-497c6bd01cf8/go.mod h1:FPC8iWArxls7axbVLsW+kpg1mz29A1b2M6jt+hZfDkU=
github.com/antonmedv/expr v1.15.5 h1:y0Iz3cEwmpRz5/r3w4qQR0MfIqJGdGM1zbhD/v0G5Vg=
github.com/antonmedv/expr v1.15.5/go.mod h1:0E/6TxnOlRNp81GMzX9QfDPAmHo2Phg00y4JUv1ihsE=
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/tview v0.0.0-20200219210816-cd38d7432498/go.mod h1:6lkG1x+13OShEf0EaOCaTQYyB7d5nSbb181KtjlS+84=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/sanity-io/litter v1.2.0/go.mod h1:JF6pZUFgu2Q0sBZ+HSV35P8TVPI1TTzEwyu9FXAw2W4=
github.com/shimmeringbee/bytecodec v0.0.0-20201107142444-94bb5c0baaee/go.mod h1:WYnxfxTJ45UQ+xeAuuTSIalcEepgP8Rb7T/OhCaDdgo=
github.com/shimmeringbee/bytecodec v0.0.0-20210228205504-1e9e0677347b h1:8q7X6JQwYKjnl+Absfv9m+LbDSBBllqTDDKzmtZ1ybY=
github.com/shimmeringbee/bytecodec v0.0.0-20210228205504-1e9e0677347b/go.mod h1:WYnxfxTJ45UQ+xeAuuTSIalcEepgP8Rb7T/OhCaDdgo=
github.com/shimmeringbee/callbacks v0.0.0-20221001135028-b85b5f89d5d6 h1:A1t2A3OrXEEdZqMw+zmwe6r7LbeZWP256es5TY9/k3Y=
github.com/shimmeringbee/callbacks v0.0.0-20221001135028-b85b5f89d5d6/go.mod h1:1AzT3lP4dAEaqWDdWsldhRtcl0+jyCGcZaBTHTjtA9w=
github.com/shimmeringbee/da v0.0.0-20231129204318-63ea834a97fd h1:C82d6yE2DWdS0PEQlyL/yIhn4Ozzf5mQWX0TbrQ4kno=
github.com/shimmeringbee/da v0.0.0-20231129204318-63ea834a97fd/go.mod h1:6Oji+jlg9NfJrlAcsGduhcQVffH7v57IRHguPF1JmgA=
github.com/shimmeringbee/da v0.0.0-20231129212915-774722ece3c7 h1:0ihLdGeXyzpNHbKKNQWjmS7ZpQy6cCiEH8Hz+gqvSh4=
github.com/shimmeringbee/da v0.0.0-20231129212915-774722ece3c7/go.mod h1:jUKTa353LvJT3TAdwtmfGEbcxkYbG58h0gbASRf0FIs=
github.com/shimmeringbee/da v0.0.0-20231216201234-d76445766fbb h1:7ShcdpIoUb7z7/rW/lTXBgWhjjsHsry9dBIL9vpQXFg=
github.com/shimmeringbee/da v0.0.0-20231216201234-d76445766fbb/go.mod h1:jUKTa353LvJT3TAdwtmfGEbcxkYbG58h0gbASRf0FIs=
github.com/shimmeringbee/da v0.0.0-20240114214251-b3b74f3b1894 h1:Q7vXhYoHVeh3hFHlAFL1VPbPlhVDwvCnKjtu8VPbUz4=
github.com/shimmeringbee/da v0.0.0-20240114214251-b3b74f3b1894/go.mod h1:jUKTa353LvJT3TAdwtmfGEbcxkYbG58h0gbASRf0FIs=
github.com/shimmeringbee/logwrap v0.1.3 h1:1PqPGdgbeQxACQqc6RUWERn7EnpA1jbiHzXVYFa7q2A=
github.com/shimmeringbee/logwrap v0.1.3/go.mod h1:NBAcZCUl6aFOGnWTs8m67EUAmWFZXRhoRQf5nknY8W0=
github.com/shimmeringbee/persistence v0.0.0-20240311211126-71abe465da0c h1:pxfug23MwBm39D6JKXoykQRSCwUNlr4FbsKqphh/qjU=
github.com/shimmeringbee/persistence v0.0.0-20240311211126-71abe465da0c/go.mod h1:dKKEj8uVcBM/CArQRE4yLw5DFRfAAzabI7mbgD1ZLeI=
github.com/shimmeringbee/persistence v0.0.0-20240318205009-b8100e7c3887 h1:hr+n6DzP37QYzCf3FN8bcuPoNSsbQe3u4QztdHjOq3I=
github.com/shimmeringbee/persistence v0.0.0-20240318205009-b8100e7c3887/go.mod h1:dKKEj8uVcBM/CArQRE4yLw5DFRfAAzabI7mbgD1ZLeI=
github.com/shimmeringbee/retry v0.0.0-20221006193055-2ce01bf139c2 h1:HxpPz7w7SxVf1GmcM5oTK1JK64TGpK1UflweYRSOwC4=
github.com/shimmeringbee/retry v0.0.0-20221006193055-2ce01bf139c2/go.mod h1:KYvVq5b7/BSSlWng+AKB5jwNGpc0D7eg8ySWrdPAlms=
github.com/shimmeringbee/zcl v0.0.0-20221006205348-732be4c63285 h1:GGO7oZJf4q7fA5yimMnfHjgOqq6WJOQX9DSBzxm0xBM=
Expand All @@ -49,38 +32,17 @@ github.com/shimmeringbee/zigbee v0.0.0-20221016122511-6c2328db0d94/go.mod h1:GMA
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 h1:cu5kTvlzcw1Q5S9f5ip1/cpiB4nXvw1XYzFPGgzLUOY=
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
37 changes: 24 additions & 13 deletions implcaps/generic/product_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"fmt"
"github.com/shimmeringbee/da"
"github.com/shimmeringbee/da/capabilities"
"github.com/shimmeringbee/persistence"
"github.com/shimmeringbee/zda/implcaps"
"sync"
)

type ProductInformation struct {
s persistence.Section
m *sync.RWMutex
pi *capabilities.ProductInfo
}
Expand All @@ -22,6 +24,23 @@ func (g *ProductInformation) ImplName() string {
return "GenericProductInformation"
}

func (g *ProductInformation) Init(_ da.Device, section persistence.Section) {
g.s = section
}

func (g *ProductInformation) Load(_ context.Context) (bool, error) {
g.m.Lock()
defer g.m.Unlock()

g.pi = &capabilities.ProductInfo{}
g.pi.Name, _ = g.s.String("Name")
g.pi.Manufacturer, _ = g.s.String("Manufacturer")
g.pi.Version, _ = g.s.String("Version")
g.pi.Serial, _ = g.s.String("Serial")

return true, nil
}

func (g *ProductInformation) Capability() da.Capability {
return capabilities.ProductInformationFlag
}
Expand All @@ -30,7 +49,7 @@ func (g *ProductInformation) Name() string {
return capabilities.StandardNames[capabilities.ProductInformationFlag]
}

func (g *ProductInformation) Attach(_ context.Context, _ da.Device, _ implcaps.AttachType, m map[string]interface{}) (bool, error) {
func (g *ProductInformation) Enumerate(_ context.Context, m map[string]interface{}) (bool, error) {
g.m.Lock()
defer g.m.Unlock()

Expand All @@ -45,12 +64,16 @@ func (g *ProductInformation) Attach(_ context.Context, _ da.Device, _ implcaps.A
switch k {
case "Name":
newPI.Name = stringV
_ = g.s.Set("Name", stringV)
case "Manufacturer":
newPI.Manufacturer = stringV
_ = g.s.Set("Manufacturer", stringV)
case "Version":
newPI.Version = stringV
_ = g.s.Set("Version", stringV)
case "Serial":
newPI.Serial = stringV
_ = g.s.Set("Serial", stringV)
}
}

Expand All @@ -62,18 +85,6 @@ func (g *ProductInformation) Detach(_ context.Context, _ implcaps.DetachType) er
return nil
}

func (g *ProductInformation) State() map[string]interface{} {
g.m.RLock()
defer g.m.RUnlock()

return map[string]interface{}{
"Name": g.pi.Name,
"Serial": g.pi.Serial,
"Manufacturer": g.pi.Manufacturer,
"Version": g.pi.Version,
}
}

func (g *ProductInformation) Get(_ context.Context) (capabilities.ProductInfo, error) {
g.m.RLock()
defer g.m.RUnlock()
Expand Down
Loading

0 comments on commit 9faae42

Please sign in to comment.