From 03bc0a1c6bc644766526435c0cbaff741e167994 Mon Sep 17 00:00:00 2001 From: Daniel Lohse Date: Sat, 4 Nov 2023 22:41:11 +0100 Subject: [PATCH] Add Datapoint interface I needed this in a project of mine. --- knx/dpt/types.go | 8 ++++++++ knx/dpt/types_registry.go | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/knx/dpt/types.go b/knx/dpt/types.go index 04de8c3..82e9ca0 100644 --- a/knx/dpt/types.go +++ b/knx/dpt/types.go @@ -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. @@ -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 +} diff --git a/knx/dpt/types_registry.go b/knx/dpt/types_registry.go index ac91880..3f266a9 100644 --- a/knx/dpt/types_registry.go +++ b/knx/dpt/types_registry.go @@ -9,7 +9,7 @@ import ( ) var ( - types = [...]DatapointValue{ + types = [...]Datapoint{ // 1.xxx new(DPT_1001), new(DPT_1002), @@ -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 }