Skip to content

Commit

Permalink
Add Datapoint interface
Browse files Browse the repository at this point in the history
I needed this in a project of mine.
  • Loading branch information
annismckenzie committed Nov 4, 2023
1 parent 1d83afd commit 03bc0a1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 8 additions & 0 deletions knx/dpt/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package dpt

import "fmt"

// A DatapointValue is a value of a datapoint.
type DatapointValue interface {
// Pack the datapoint to a byte array.
Expand All @@ -17,3 +19,9 @@ type DatapointMeta interface {
// Unit returns the unit of this datapoint type or empty string if it doesn't have a unit.
Unit() string
}

type Datapoint interface {
DatapointValue
DatapointMeta
fmt.Stringer
}
6 changes: 3 additions & 3 deletions knx/dpt/types_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
types = [...]DatapointValue{
types = [...]Datapoint{
// 1.xxx
new(DPT_1001),
new(DPT_1002),
Expand Down Expand Up @@ -248,14 +248,14 @@ func ListSupportedTypes() []string {
}

// Produce creates a new instance of the given datapoint-type name e.g. "1.001".
func Produce(name string) (d DatapointValue, ok bool) {
func Produce(name string) (d Datapoint, ok bool) {
// Setup the registry
setup()

// Lookup the given type and create a new instance of that type
x, ok := registry[name]
if ok {
d = reflect.New(x).Interface().(DatapointValue)
d = reflect.New(x).Interface().(Datapoint)
}
return d, ok
}

0 comments on commit 03bc0a1

Please sign in to comment.