Skip to content

Commit

Permalink
Add Datapoint interface (#73)
Browse files Browse the repository at this point in the history
* Include fmt.Stringer in the DatapointMeta interface

All types already implement `String() string` so including it gives callers the ability to call it.

* Add a combined Datapoint interface

This allows callers to use all the methods present on the types, not just
`Pack` and `Unpack`.
  • Loading branch information
annismckenzie authored Jan 7, 2024
1 parent 1d83afd commit 816b703
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 11 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 @@ -16,4 +18,13 @@ type DatapointValue interface {
type DatapointMeta interface {
// Unit returns the unit of this datapoint type or empty string if it doesn't have a unit.
Unit() string

// fmt.Stringer provides a string representation of the datapoint.
fmt.Stringer
}

// Datapoint represents a datapoint with both its value and metadata.
type Datapoint interface {
DatapointValue
DatapointMeta
}
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 816b703

Please sign in to comment.