You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2022/09/06 12:21:15 UTC

[plc4x] branch develop updated: feat(plc4go): added GetSource to PlcSubscriptionEvent

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 0e01d3793 feat(plc4go): added GetSource to PlcSubscriptionEvent
0e01d3793 is described below

commit 0e01d379308048d845631fc5ba93ed300e2b3744
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Sep 6 14:21:07 2022 +0200

    feat(plc4go): added GetSource to PlcSubscriptionEvent
---
 plc4go/internal/cbus/SubscriptionEvent.go       |  7 +++----
 plc4go/internal/knxnetip/SubscriptionEvent.go   |  7 +++----
 plc4go/pkg/api/model/plc_subscription.go        |  9 +++++++++
 plc4go/spi/model/DefaultPlcSubscriptionEvent.go | 24 +++++++++++++++++-------
 4 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/plc4go/internal/cbus/SubscriptionEvent.go b/plc4go/internal/cbus/SubscriptionEvent.go
index b72238cea..bc6a092d4 100644
--- a/plc4go/internal/cbus/SubscriptionEvent.go
+++ b/plc4go/internal/cbus/SubscriptionEvent.go
@@ -34,10 +34,9 @@ type SubscriptionEvent struct {
 func NewSubscriptionEvent(fields map[string]apiModel.PlcField, types map[string]internalMode.SubscriptionType,
 	intervals map[string]time.Duration, responseCodes map[string]apiModel.PlcResponseCode,
 	address map[string]string, values map[string]values.PlcValue) SubscriptionEvent {
-	return SubscriptionEvent{
-		address:                     address,
-		DefaultPlcSubscriptionEvent: internalMode.NewDefaultPlcSubscriptionEvent(fields, types, intervals, responseCodes, values),
-	}
+	subscriptionEvent := SubscriptionEvent{address: address}
+	subscriptionEvent.DefaultPlcSubscriptionEvent = internalMode.NewDefaultPlcSubscriptionEvent(subscriptionEvent, fields, types, intervals, responseCodes, values)
+	return subscriptionEvent
 }
 
 func (m SubscriptionEvent) GetAddress(name string) string {
diff --git a/plc4go/internal/knxnetip/SubscriptionEvent.go b/plc4go/internal/knxnetip/SubscriptionEvent.go
index 159302922..488870f04 100644
--- a/plc4go/internal/knxnetip/SubscriptionEvent.go
+++ b/plc4go/internal/knxnetip/SubscriptionEvent.go
@@ -36,10 +36,9 @@ type SubscriptionEvent struct {
 func NewSubscriptionEvent(fields map[string]apiModel.PlcField, types map[string]internalMode.SubscriptionType,
 	intervals map[string]time.Duration, responseCodes map[string]apiModel.PlcResponseCode,
 	addresses map[string][]byte, values map[string]values.PlcValue) SubscriptionEvent {
-	return SubscriptionEvent{
-		addresses:                   addresses,
-		DefaultPlcSubscriptionEvent: internalMode.NewDefaultPlcSubscriptionEvent(fields, types, intervals, responseCodes, values),
-	}
+	subscriptionEvent := SubscriptionEvent{addresses: addresses}
+	subscriptionEvent.DefaultPlcSubscriptionEvent = internalMode.NewDefaultPlcSubscriptionEvent(subscriptionEvent, fields, types, intervals, responseCodes, values)
+	return subscriptionEvent
 }
 
 // GetAddress Decode the binary data in the address according to the field requested
diff --git a/plc4go/pkg/api/model/plc_subscription.go b/plc4go/pkg/api/model/plc_subscription.go
index b981c9924..95b9774ae 100644
--- a/plc4go/pkg/api/model/plc_subscription.go
+++ b/plc4go/pkg/api/model/plc_subscription.go
@@ -27,9 +27,18 @@ import (
 
 type PlcSubscriptionEvent interface {
 	PlcResponse
+	// GetFieldNames returns all field name which can be found in this event
 	GetFieldNames() []string
+	// GetResponseCode returns the PlcResponseCode for a field
 	GetResponseCode(name string) PlcResponseCode
+	// GetAddress returns the address for an event. This is meant to for reading or writing one item.
+	// Sometimes there are fields which can't be directly addressed (e.g. only through a broadcast).
+	// In that case (if applicable) the GetSource contains the source information about the sending device.
 	GetAddress(name string) string
+	// GetSource returns usually the same as GetAddress in case when the address contains information about the source.
+	// If we have a field which is not directly addressable (see doc for GetAddress) the source is useful to identify the device.
+	GetSource(name string) string
+	// GetValue returns the field value for a named field.
 	GetValue(name string) values.PlcValue
 }
 
diff --git a/plc4go/spi/model/DefaultPlcSubscriptionEvent.go b/plc4go/spi/model/DefaultPlcSubscriptionEvent.go
index 7e64f848d..81dd600de 100644
--- a/plc4go/spi/model/DefaultPlcSubscriptionEvent.go
+++ b/plc4go/spi/model/DefaultPlcSubscriptionEvent.go
@@ -28,21 +28,27 @@ import (
 
 type DefaultPlcSubscriptionEvent struct {
 	DefaultResponse
+	DefaultPlcSubscriptionEventRequirements
 	fields    map[string]model.PlcField
 	types     map[string]SubscriptionType
 	intervals map[string]time.Duration
 	values    map[string]values.PlcValue
 }
 
-func NewDefaultPlcSubscriptionEvent(fields map[string]model.PlcField, types map[string]SubscriptionType,
+type DefaultPlcSubscriptionEventRequirements interface {
+	GetAddress(name string) string
+}
+
+func NewDefaultPlcSubscriptionEvent(defaultPlcSubscriptionEventRequirements DefaultPlcSubscriptionEventRequirements, fields map[string]model.PlcField, types map[string]SubscriptionType,
 	intervals map[string]time.Duration, responseCodes map[string]model.PlcResponseCode,
 	values map[string]values.PlcValue) DefaultPlcSubscriptionEvent {
 	return DefaultPlcSubscriptionEvent{
-		DefaultResponse: NewDefaultResponse(responseCodes),
-		fields:          fields,
-		types:           types,
-		intervals:       intervals,
-		values:          values,
+		DefaultResponse:                         NewDefaultResponse(responseCodes),
+		DefaultPlcSubscriptionEventRequirements: defaultPlcSubscriptionEventRequirements,
+		fields:                                  fields,
+		types:                                   types,
+		intervals:                               intervals,
+		values:                                  values,
 	}
 }
 
@@ -67,7 +73,11 @@ func (m DefaultPlcSubscriptionEvent) GetInterval(name string) time.Duration {
 }
 
 func (m DefaultPlcSubscriptionEvent) GetAddress(name string) string {
-	panic("GetAddress not implemented")
+	return m.DefaultPlcSubscriptionEventRequirements.GetAddress(name)
+}
+
+func (m DefaultPlcSubscriptionEvent) GetSource(name string) string {
+	return m.GetAddress(name)
 }
 
 func (m DefaultPlcSubscriptionEvent) GetValue(name string) values.PlcValue {