Skip to content

Commit

Permalink
Add zigbee converters to persistence.
Browse files Browse the repository at this point in the history
Not convinced these should be here, but either way it'll require an import of one module to another.
  • Loading branch information
melair committed May 21, 2024
1 parent bb35128 commit 323fdd5
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
55 changes: 55 additions & 0 deletions converter/zigbee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package converter

import (
"github.com/shimmeringbee/persistence"
"github.com/shimmeringbee/zcl"
"github.com/shimmeringbee/zigbee"
)

func AttributeIDEncoder(s persistence.Section, k string, v zcl.AttributeID) error {
return s.Set(k, int64(v))
}

func AttributeIDDecoder(s persistence.Section, k string) (zcl.AttributeID, bool) {
if ev, found := s.Int(k); found {
return zcl.AttributeID(ev), true
} else {
return 0, false
}
}

func AttributeDataTypeEncoder(s persistence.Section, k string, v zcl.AttributeDataType) error {
return s.Set(k, int64(v))
}

func AttributeDataTypeDecoder(s persistence.Section, k string) (zcl.AttributeDataType, bool) {
if ev, found := s.Int(k); found {
return zcl.AttributeDataType(ev), true
} else {
return 0, false
}
}

func ClusterIDEncoder(s persistence.Section, k string, v zigbee.ClusterID) error {
return s.Set(k, int64(v))
}

func ClusterIDDecoder(s persistence.Section, k string) (zigbee.ClusterID, bool) {
if ev, found := s.Int(k); found {
return zigbee.ClusterID(ev), true
} else {
return 0, false
}
}

func EndpointEncoder(s persistence.Section, k string, v zigbee.Endpoint) error {
return s.Set(k, int64(v))
}

func EndpointDecoder(s persistence.Section, k string) (zigbee.Endpoint, bool) {
if ev, found := s.Int(k); found {
return zigbee.Endpoint(ev), true
} else {
return 0, false
}
}
69 changes: 69 additions & 0 deletions converter/zigbee_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package converter

import (
"github.com/shimmeringbee/persistence/impl/memory"
"github.com/shimmeringbee/zcl"
"github.com/shimmeringbee/zigbee"
"github.com/stretchr/testify/assert"
"testing"
)

func TestClusterID(t *testing.T) {
t.Run("stored and retrieved", func(t *testing.T) {
s := memory.New()

expected := zigbee.ClusterID(1)

err := Store(s, Key, expected, ClusterIDEncoder)
assert.NoError(t, err)

actual, found := Retrieve(s, Key, ClusterIDDecoder)
assert.True(t, found)
assert.Equal(t, expected, actual)
})
}

func TestEndpoint(t *testing.T) {
t.Run("stored and retrieved", func(t *testing.T) {
s := memory.New()

expected := zigbee.Endpoint(1)

err := Store(s, Key, expected, EndpointEncoder)
assert.NoError(t, err)

actual, found := Retrieve(s, Key, EndpointDecoder)
assert.True(t, found)
assert.Equal(t, expected, actual)
})
}

func TestAttributeID(t *testing.T) {
t.Run("stored and retrieved", func(t *testing.T) {
s := memory.New()

expected := zcl.AttributeID(1)

err := Store(s, Key, expected, AttributeIDEncoder)
assert.NoError(t, err)

actual, found := Retrieve(s, Key, AttributeIDDecoder)
assert.True(t, found)
assert.Equal(t, expected, actual)
})
}

func TestAttributeDataType(t *testing.T) {
t.Run("stored and retrieved", func(t *testing.T) {
s := memory.New()

expected := zcl.AttributeDataType(1)

err := Store(s, Key, expected, AttributeDataTypeEncoder)
assert.NoError(t, err)

actual, found := Retrieve(s, Key, AttributeDataTypeDecoder)
assert.True(t, found)
assert.Equal(t, expected, actual)
})
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ module github.com/shimmeringbee/persistence

go 1.22.0

require github.com/stretchr/testify v1.9.0
require (
github.com/shimmeringbee/zcl v0.0.0-20240509210644-817a66d91348
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shimmeringbee/bytecodec v0.0.0-20201107142444-94bb5c0baaee // indirect
github.com/shimmeringbee/zigbee v0.0.0-20201027194100-4e53cafc0f7a // indirect
github.com/stretchr/objx v0.5.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shimmeringbee/bytecodec v0.0.0-20201107142444-94bb5c0baaee h1:LGPf3nQB0b+k/zxaSPqwcW1Zd3cBGcEqREwqT7CWmw8=
github.com/shimmeringbee/bytecodec v0.0.0-20201107142444-94bb5c0baaee/go.mod h1:WYnxfxTJ45UQ+xeAuuTSIalcEepgP8Rb7T/OhCaDdgo=
github.com/shimmeringbee/zcl v0.0.0-20240509210644-817a66d91348 h1:J0kBfQom8P2bXAychpcd8PN2qjvgxuqcJi3DktvTm+0=
github.com/shimmeringbee/zcl v0.0.0-20240509210644-817a66d91348/go.mod h1:oy+15b56Rms5RnwY/Ik4NlXSYusoAObOIVjfieLRfjQ=
github.com/shimmeringbee/zigbee v0.0.0-20201027194100-4e53cafc0f7a h1:PNZqpjc7ouHQ1MKqerPNNeLOcTsXWZ0ZF9avU8LG9Es=
github.com/shimmeringbee/zigbee v0.0.0-20201027194100-4e53cafc0f7a/go.mod h1:GMA6rVpzvUK16cZwi8uW11JUTx8xUGOk5DbkXYWvm/8=
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.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.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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=
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=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 323fdd5

Please sign in to comment.