Skip to content

Commit 323fdd5

Browse files
committed
Add zigbee converters to persistence.
Not convinced these should be here, but either way it'll require an import of one module to another.
1 parent bb35128 commit 323fdd5

File tree

4 files changed

+148
-1
lines changed

4 files changed

+148
-1
lines changed

Diff for: converter/zigbee.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package converter
2+
3+
import (
4+
"github.com/shimmeringbee/persistence"
5+
"github.com/shimmeringbee/zcl"
6+
"github.com/shimmeringbee/zigbee"
7+
)
8+
9+
func AttributeIDEncoder(s persistence.Section, k string, v zcl.AttributeID) error {
10+
return s.Set(k, int64(v))
11+
}
12+
13+
func AttributeIDDecoder(s persistence.Section, k string) (zcl.AttributeID, bool) {
14+
if ev, found := s.Int(k); found {
15+
return zcl.AttributeID(ev), true
16+
} else {
17+
return 0, false
18+
}
19+
}
20+
21+
func AttributeDataTypeEncoder(s persistence.Section, k string, v zcl.AttributeDataType) error {
22+
return s.Set(k, int64(v))
23+
}
24+
25+
func AttributeDataTypeDecoder(s persistence.Section, k string) (zcl.AttributeDataType, bool) {
26+
if ev, found := s.Int(k); found {
27+
return zcl.AttributeDataType(ev), true
28+
} else {
29+
return 0, false
30+
}
31+
}
32+
33+
func ClusterIDEncoder(s persistence.Section, k string, v zigbee.ClusterID) error {
34+
return s.Set(k, int64(v))
35+
}
36+
37+
func ClusterIDDecoder(s persistence.Section, k string) (zigbee.ClusterID, bool) {
38+
if ev, found := s.Int(k); found {
39+
return zigbee.ClusterID(ev), true
40+
} else {
41+
return 0, false
42+
}
43+
}
44+
45+
func EndpointEncoder(s persistence.Section, k string, v zigbee.Endpoint) error {
46+
return s.Set(k, int64(v))
47+
}
48+
49+
func EndpointDecoder(s persistence.Section, k string) (zigbee.Endpoint, bool) {
50+
if ev, found := s.Int(k); found {
51+
return zigbee.Endpoint(ev), true
52+
} else {
53+
return 0, false
54+
}
55+
}

Diff for: converter/zigbee_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package converter
2+
3+
import (
4+
"github.com/shimmeringbee/persistence/impl/memory"
5+
"github.com/shimmeringbee/zcl"
6+
"github.com/shimmeringbee/zigbee"
7+
"github.com/stretchr/testify/assert"
8+
"testing"
9+
)
10+
11+
func TestClusterID(t *testing.T) {
12+
t.Run("stored and retrieved", func(t *testing.T) {
13+
s := memory.New()
14+
15+
expected := zigbee.ClusterID(1)
16+
17+
err := Store(s, Key, expected, ClusterIDEncoder)
18+
assert.NoError(t, err)
19+
20+
actual, found := Retrieve(s, Key, ClusterIDDecoder)
21+
assert.True(t, found)
22+
assert.Equal(t, expected, actual)
23+
})
24+
}
25+
26+
func TestEndpoint(t *testing.T) {
27+
t.Run("stored and retrieved", func(t *testing.T) {
28+
s := memory.New()
29+
30+
expected := zigbee.Endpoint(1)
31+
32+
err := Store(s, Key, expected, EndpointEncoder)
33+
assert.NoError(t, err)
34+
35+
actual, found := Retrieve(s, Key, EndpointDecoder)
36+
assert.True(t, found)
37+
assert.Equal(t, expected, actual)
38+
})
39+
}
40+
41+
func TestAttributeID(t *testing.T) {
42+
t.Run("stored and retrieved", func(t *testing.T) {
43+
s := memory.New()
44+
45+
expected := zcl.AttributeID(1)
46+
47+
err := Store(s, Key, expected, AttributeIDEncoder)
48+
assert.NoError(t, err)
49+
50+
actual, found := Retrieve(s, Key, AttributeIDDecoder)
51+
assert.True(t, found)
52+
assert.Equal(t, expected, actual)
53+
})
54+
}
55+
56+
func TestAttributeDataType(t *testing.T) {
57+
t.Run("stored and retrieved", func(t *testing.T) {
58+
s := memory.New()
59+
60+
expected := zcl.AttributeDataType(1)
61+
62+
err := Store(s, Key, expected, AttributeDataTypeEncoder)
63+
assert.NoError(t, err)
64+
65+
actual, found := Retrieve(s, Key, AttributeDataTypeDecoder)
66+
assert.True(t, found)
67+
assert.Equal(t, expected, actual)
68+
})
69+
}

Diff for: go.mod

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ module github.com/shimmeringbee/persistence
22

33
go 1.22.0
44

5-
require github.com/stretchr/testify v1.9.0
5+
require (
6+
github.com/shimmeringbee/zcl v0.0.0-20240509210644-817a66d91348
7+
github.com/stretchr/testify v1.9.0
8+
)
69

710
require (
811
github.com/davecgh/go-spew v1.1.1 // indirect
912
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
github.com/shimmeringbee/bytecodec v0.0.0-20201107142444-94bb5c0baaee // indirect
14+
github.com/shimmeringbee/zigbee v0.0.0-20201027194100-4e53cafc0f7a // indirect
15+
github.com/stretchr/objx v0.5.2 // indirect
1016
gopkg.in/yaml.v3 v3.0.1 // indirect
1117
)

Diff for: go.sum

+17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
23
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
34
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
45
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/shimmeringbee/bytecodec v0.0.0-20201107142444-94bb5c0baaee h1:LGPf3nQB0b+k/zxaSPqwcW1Zd3cBGcEqREwqT7CWmw8=
7+
github.com/shimmeringbee/bytecodec v0.0.0-20201107142444-94bb5c0baaee/go.mod h1:WYnxfxTJ45UQ+xeAuuTSIalcEepgP8Rb7T/OhCaDdgo=
8+
github.com/shimmeringbee/zcl v0.0.0-20240509210644-817a66d91348 h1:J0kBfQom8P2bXAychpcd8PN2qjvgxuqcJi3DktvTm+0=
9+
github.com/shimmeringbee/zcl v0.0.0-20240509210644-817a66d91348/go.mod h1:oy+15b56Rms5RnwY/Ik4NlXSYusoAObOIVjfieLRfjQ=
10+
github.com/shimmeringbee/zigbee v0.0.0-20201027194100-4e53cafc0f7a h1:PNZqpjc7ouHQ1MKqerPNNeLOcTsXWZ0ZF9avU8LG9Es=
11+
github.com/shimmeringbee/zigbee v0.0.0-20201027194100-4e53cafc0f7a/go.mod h1:GMA6rVpzvUK16cZwi8uW11JUTx8xUGOk5DbkXYWvm/8=
12+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
13+
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
14+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
15+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
16+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
17+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
18+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
519
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
620
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
721
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
822
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
23+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
24+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
25+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
926
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1027
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)