Skip to content

Commit

Permalink
Add ZCL communicator, clean up from integrating device enumeration.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwood committed Dec 31, 2023
1 parent 3a08987 commit ab2842d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
19 changes: 14 additions & 5 deletions gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"github.com/shimmeringbee/da"
"github.com/shimmeringbee/da/capabilities"
"github.com/shimmeringbee/logwrap"
"github.com/shimmeringbee/zcl"
"github.com/shimmeringbee/zcl/commands/global"
"github.com/shimmeringbee/zcl/communicator"
"github.com/shimmeringbee/zda/implcaps/factory"
"github.com/shimmeringbee/zda/rules"
"github.com/shimmeringbee/zigbee"
Expand All @@ -19,8 +22,12 @@ const DefaultGatewayHomeAutomationEndpoint = zigbee.Endpoint(0x01)
func New(baseCtx context.Context, p zigbee.Provider, r ruleExecutor) da.Gateway {
ctx, cancel := context.WithCancel(baseCtx)

zclCommandRegistry := zcl.NewCommandRegistry()
global.Register(zclCommandRegistry)

gw := &gateway{
provider: p,
provider: p,
zclCommunicator: communicator.NewCommunicator(p, zclCommandRegistry),

selfDevice: gatewayDevice{
dd: &deviceDiscovery{},
Expand All @@ -38,12 +45,14 @@ func New(baseCtx context.Context, p zigbee.Provider, r ruleExecutor) da.Gateway
events: make(chan interface{}, 0xffff),
}

gw.WithGoLogger(log.New(os.Stderr, "", log.LstdFlags))

gw.ed = &enumerateDevice{
gw: gw,
dm: gw,
logger: logwrap.Logger{},
logger: gw.logger,
nq: gw.provider,
zclReadFn: nil,
zclReadFn: gw.zclCommunicator.Global().ReadAttributes,
capabilityFactory: factory.Create,
}

Expand All @@ -53,7 +62,6 @@ func New(baseCtx context.Context, p zigbee.Provider, r ruleExecutor) da.Gateway

gw.callbacks.Add(gw.ed.onNodeJoin)

gw.WithGoLogger(log.New(os.Stderr, "", log.LstdFlags))
return gw
}

Expand All @@ -62,7 +70,8 @@ type ruleExecutor interface {
}

type gateway struct {
provider zigbee.Provider
provider zigbee.Provider
zclCommunicator *communicator.Communicator

logger logwrap.Logger
ctx context.Context
Expand Down
2 changes: 2 additions & 0 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/shimmeringbee/zigbee"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"io"
"testing"
)

Expand Down Expand Up @@ -84,6 +85,7 @@ func Test_gateway_Devices(t *testing.T) {
defer stop(t)

mp.On("RegisterAdapterEndpoint", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
mp.On("QueryNodeDescription", mock.Anything, mock.Anything).Return(zigbee.NodeDescription{}, io.EOF).Maybe()

err := gw.Start(nil)
assert.NoError(t, err)
Expand Down
5 changes: 4 additions & 1 deletion provider_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@ func (g *gateway) receiveNodeLeaveEvent(e zigbee.NodeLeaveEvent) {
}

func (g *gateway) receiveNodeIncomingMessageEvent(e zigbee.NodeIncomingMessageEvent) {

if err := g.zclCommunicator.ProcessIncomingMessage(e); err != nil {
g.logger.LogWarn(g.ctx, "ZCL communicator failed to process incoming message.", logwrap.Datum("IEEEAddress", e.IEEEAddress.String()), logwrap.Err(err))
return
}
}
8 changes: 7 additions & 1 deletion provider_loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import (
"github.com/shimmeringbee/logwrap/impl/discard"
"github.com/shimmeringbee/zigbee"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"io"
"testing"
)

func Test_gateway_receiveNodeJoinEvent(t *testing.T) {
t.Run("node join event will add the new node to the node table, and introduce a base device", func(t *testing.T) {
g := New(context.Background(), nil, nil).(*gateway)
mp := &zigbee.MockProvider{}
mp.On("QueryNodeDescription", mock.Anything, mock.Anything).Return(zigbee.NodeDescription{}, io.EOF).Maybe()
defer mp.AssertExpectations(t)

g := New(context.Background(), mp, nil).(*gateway)
g.WithLogWrapLogger(logwrap.New(discard.Discard()))
addr := zigbee.GenerateLocalAdministeredIEEEAddress()

Expand Down

0 comments on commit ab2842d

Please sign in to comment.