You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/10/15 18:26:37 UTC

[plc4x] branch feature/plc4go updated: - Fine-tuning of the API

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

cdutz pushed a commit to branch feature/plc4go
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/feature/plc4go by this push:
     new baeaa7e  - Fine-tuning of the API
baeaa7e is described below

commit baeaa7ee0df58f6016fde386dc74b1868d2662bc
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Thu Oct 15 20:26:30 2020 +0200

    - Fine-tuning of the API
---
 .../examples/read/hello_world_plc4go_read.go       | 48 +++++++++-------
 sandbox/plc4go/pkg/plc4go/connection.go            | 66 ++++++++++++++++++----
 sandbox/plc4go/pkg/plc4go/driver.go                | 12 ++++
 sandbox/plc4go/pkg/plc4go/driverManager.go         | 29 ++++++----
 .../plc4go/pkg/plc4go/model/plc_read_request.go    | 11 +++-
 sandbox/plc4go/pkg/plc4go/model/plc_request.go     |  1 -
 .../pkg/plc4go/model/plc_subscription_request.go   |  9 ++-
 .../pkg/plc4go/model/plc_unsubscription_request.go |  8 +++
 .../plc4go/pkg/plc4go/model/plc_write_request.go   | 12 +++-
 9 files changed, 148 insertions(+), 48 deletions(-)

diff --git a/sandbox/plc4go/examples/read/hello_world_plc4go_read.go b/sandbox/plc4go/examples/read/hello_world_plc4go_read.go
index 1c9fa96..1281c50 100644
--- a/sandbox/plc4go/examples/read/hello_world_plc4go_read.go
+++ b/sandbox/plc4go/examples/read/hello_world_plc4go_read.go
@@ -3,43 +3,51 @@ package read
 import (
 	"encoding/json"
 	"fmt"
-	"os"
 	"plc4x.apache.org/plc4go-modbus-driver/0.8.0/pkg/plc4go"
 )
 
-func main() {
+func main() int {
 	// Get a connection to a remote PLC
-	connection, err := plc4go.NewPlcDriverManager().GetConnectedConnection("s7://192.168.23.30")
-	if err != nil {
-		_ = fmt.Errorf("error connecting to PLC: %s", err.Error())
-		os.Exit(1)
+	crc := plc4go.NewPlcDriverManager().GetConnectedConnection("s7://192.168.23.30")
+
+	// Wait for the driver to connect (or not)
+	connectionResult := <-crc
+	if connectionResult.Err != nil {
+		_ = fmt.Errorf("error connecting to PLC: %s", connectionResult.Err.Error())
+		return 1
 	}
+	connection := connectionResult.Connection
+
+	// Make sure the connection is closed at the end
+	defer connection.Close()
 
 	// Prepare a read-request
-	rrb, err := connection.ReadRequestBuilder()
+	rrb := connection.ReadRequestBuilder()
+	rrb.AddField("output-field", "%Q0.0:BOOL")
+	rrb.AddField("input-field", "%I0.0:BOOL")
+	readRequest, err := rrb.Build()
 	if err != nil {
-		_ = fmt.Errorf("error getting read-request builder: %s", err.Error())
-		os.Exit(2)
+		_ = fmt.Errorf("error preparing read-request: %s", connectionResult.Err.Error())
+		return 2
 	}
-	rrb.AddField("output-field", "%Q0.0")
-	rrb.AddField("input-field", "I0.0")
-	readRequest := rrb.Build()
 
 	// Execute a read-request
-	rr, err := readRequest.Execute()
-	if err != nil {
-		_ = fmt.Errorf("error executing read-request: %s", err.Error())
-		os.Exit(2)
-	}
+	rrc := readRequest.Execute()
 
 	// Wait for the response to finish
-	readResponse := <-rr
+	rrr := <-rrc
+	if rrr.Err != nil {
+		_ = fmt.Errorf("error executing read-request: %s", rrr.Err.Error())
+		return 3
+	}
 
 	// Do something with the response
-	readResponseJson, err := json.Marshal(readResponse)
+	readResponseJson, err := json.Marshal(rrr.Response)
 	if err != nil {
 		_ = fmt.Errorf("error serializing read-response: %s", err.Error())
-		os.Exit(2)
+		return 4
 	}
 	fmt.Printf("Result: %s\n", string(readResponseJson))
+
+	return 0
 }
diff --git a/sandbox/plc4go/pkg/plc4go/connection.go b/sandbox/plc4go/pkg/plc4go/connection.go
index 994e5a0..d61a82f 100644
--- a/sandbox/plc4go/pkg/plc4go/connection.go
+++ b/sandbox/plc4go/pkg/plc4go/connection.go
@@ -20,26 +20,72 @@ package plc4go
 
 import "plc4x.apache.org/plc4go-modbus-driver/0.8.0/pkg/plc4go/model"
 
+type PlcConnectionConnectResult struct {
+	Connection PlcConnection
+	Err        error
+}
+
+func NewPlcConnectionConnectResult(connection PlcConnection, err error) PlcConnectionConnectResult {
+	return PlcConnectionConnectResult{
+		Connection: connection,
+		Err:        err,
+	}
+}
+
+type PlcConnectionCloseResult struct {
+	Connection PlcConnection
+	Err        error
+}
+
+func NewPlcConnectionCloseResult(connection PlcConnection, err error) PlcConnectionCloseResult {
+	return PlcConnectionCloseResult{
+		Connection: connection,
+		Err:        err,
+	}
+}
+
+type PlcConnectionPingResult struct {
+	Err error
+}
+
+func NewPlcConnectionPingResult(err error) PlcConnectionPingResult {
+	return PlcConnectionPingResult{
+		Err: err,
+	}
+}
+
+type PlcConnectionMetadataResult struct {
+	Metadata model.PlcConnectionMetadata
+	Err      error
+}
+
+func NewPlcConnectionMetadataResult(metadata model.PlcConnectionMetadata, err error) PlcConnectionMetadataResult {
+	return PlcConnectionMetadataResult{
+		Metadata: metadata,
+		Err:      err,
+	}
+}
+
 type PlcConnection interface {
 	// Initiate the connection to the PLC
-	Connect() <-chan error
+	Connect() <-chan PlcConnectionConnectResult
 	// Close the connection to the PLC (gracefully)
-	Close() <-chan error
+	Close() <-chan PlcConnectionCloseResult
 	// Checks if the connection is currently still connected
 	IsConnected() bool
 
-	// Get some metadata regarding the current connection
-	GetMetadata() (model.PlcConnectionMetadata, error)
-
 	// Executes a no-op operation to check if the current connection is still able to communicate
-	Ping() error
+	Ping() <-chan PlcConnectionPingResult
+
+	// Get some metadata regarding the current connection
+	GetMetadata() PlcConnectionMetadataResult
 
 	// Create a builder for assembling read-requests
-	ReadRequestBuilder() (model.PlcReadRequestBuilder, error)
+	ReadRequestBuilder() model.PlcReadRequestBuilder
 	// Create a builder for assembling write-requests
-	WriteRequestBuilder() (model.PlcWriteRequestBuilder, error)
+	WriteRequestBuilder() model.PlcWriteRequestBuilder
 	// Create a builder for assembling subscription-requests
-	SubscriptionRequestBuilder() (model.PlcSubscriptionRequestBuilder, error)
+	SubscriptionRequestBuilder() model.PlcSubscriptionRequestBuilder
 	// Create a builder for assembling unsubscription-requests
-	UnsubscriptionRequestBuilder() (model.PlcUnsubscriptionRequestBuilder, error)
+	UnsubscriptionRequestBuilder() model.PlcUnsubscriptionRequestBuilder
 }
diff --git a/sandbox/plc4go/pkg/plc4go/driver.go b/sandbox/plc4go/pkg/plc4go/driver.go
index 777087f..4693938 100644
--- a/sandbox/plc4go/pkg/plc4go/driver.go
+++ b/sandbox/plc4go/pkg/plc4go/driver.go
@@ -20,6 +20,18 @@ package plc4go
 
 import "plc4x.apache.org/plc4go-modbus-driver/0.8.0/pkg/plc4go/model"
 
+type PlcDriverResult struct {
+	Driver PlcDriver
+	Err    error
+}
+
+func NewPlcDriverResult(driver PlcDriver, err error) PlcDriverResult {
+	return PlcDriverResult{
+		Driver: driver,
+		Err:    err,
+	}
+}
+
 type PlcDriver interface {
 	// Get the short code used to identify this driver (As used in the connection string)
 	GetProtocolCode() string
diff --git a/sandbox/plc4go/pkg/plc4go/driverManager.go b/sandbox/plc4go/pkg/plc4go/driverManager.go
index 07db7e9..6756b82 100644
--- a/sandbox/plc4go/pkg/plc4go/driverManager.go
+++ b/sandbox/plc4go/pkg/plc4go/driverManager.go
@@ -35,7 +35,7 @@ type PlcDriverManager interface {
 	GetDriver(driverName string) (PlcDriver, error)
 
 	// Get a connection to a remote PLC for a given plc4x connection-string
-	GetConnectedConnection(connectionString string) (PlcConnection, error)
+	GetConnectedConnection(connectionString string) <-chan PlcConnectionConnectResult
 }
 
 func NewPlcDriverManager() PlcDriverManager {
@@ -68,24 +68,31 @@ func (m plcDriverManger) GetDriver(driverName string) (PlcDriver, error) {
 	return m.drivers[driverName], nil
 }
 
-func (m plcDriverManger) GetConnectedConnection(connectionString string) (PlcConnection, error) {
+func (m plcDriverManger) GetConnectedConnection(connectionString string) <-chan PlcConnectionConnectResult {
 	connectionUrl, err := url.Parse(connectionString)
 	if err != nil {
-		return nil, errors.New("Error parsing connection string: " + err.Error())
+		ch := make(chan PlcConnectionConnectResult)
+		ch <- NewPlcConnectionConnectResult(nil, errors.New("Error parsing connection string: "+err.Error()))
+		return ch
 	}
 	driverName := connectionUrl.Scheme
 	driver, err := m.GetDriver(driverName)
 	if err != nil {
-		return nil, errors.New("Error getting driver for connection string: " + err.Error())
+		ch := make(chan PlcConnectionConnectResult)
+		ch <- NewPlcConnectionConnectResult(nil, errors.New("Error getting driver for connection string: "+err.Error()))
+		return ch
 	}
 	connection, err := driver.GetConnection(connectionString)
 	if err != nil {
-		return nil, errors.New("Error connecting for connection string: " + err.Error())
+		ch := make(chan PlcConnectionConnectResult)
+		ch <- NewPlcConnectionConnectResult(nil, errors.New("Error connecting for connection string: "+err.Error()))
+		return ch
 	}
-	errConsumer := connection.Connect()
-	connectionErr := <-errConsumer
-	if connectionErr != nil {
-		return nil, connectionErr
-	}
-	return connection, nil
+	ch := make(chan PlcConnectionConnectResult)
+	go func() {
+		errConsumer := connection.Connect()
+		connectionErr := <-errConsumer
+		ch <- connectionErr
+	}()
+	return ch
 }
diff --git a/sandbox/plc4go/pkg/plc4go/model/plc_read_request.go b/sandbox/plc4go/pkg/plc4go/model/plc_read_request.go
index 13f5209..7e325a9 100644
--- a/sandbox/plc4go/pkg/plc4go/model/plc_read_request.go
+++ b/sandbox/plc4go/pkg/plc4go/model/plc_read_request.go
@@ -20,9 +20,16 @@ package model
 
 type PlcReadRequestBuilder interface {
 	AddField(name string, query string) *PlcReadRequestBuilder
-	Build() PlcReadRequest
+	Build() (PlcReadRequest, error)
+}
+
+type PlcReadRequestResult struct {
+	Request  PlcReadRequest
+	Response PlcReadResponse
+	Err      error
 }
 
 type PlcReadRequest interface {
-	Execute() (<-chan PlcReadResponse, error)
+	Execute() <-chan PlcReadRequestResult
+	PlcRequest
 }
diff --git a/sandbox/plc4go/pkg/plc4go/model/plc_request.go b/sandbox/plc4go/pkg/plc4go/model/plc_request.go
index 80705cf..76dede5 100644
--- a/sandbox/plc4go/pkg/plc4go/model/plc_request.go
+++ b/sandbox/plc4go/pkg/plc4go/model/plc_request.go
@@ -19,6 +19,5 @@
 package model
 
 type PlcRequest interface {
-	Execute() (<-chan PlcResponse, error)
 	PlcMessage
 }
diff --git a/sandbox/plc4go/pkg/plc4go/model/plc_subscription_request.go b/sandbox/plc4go/pkg/plc4go/model/plc_subscription_request.go
index 742762a..6b3c398 100644
--- a/sandbox/plc4go/pkg/plc4go/model/plc_subscription_request.go
+++ b/sandbox/plc4go/pkg/plc4go/model/plc_subscription_request.go
@@ -24,9 +24,16 @@ type PlcSubscriptionRequestBuilder interface {
 	AddCyclicItem(name string, query string, interval time.Duration)
 	AddChangeOfStateItem(name string, query string)
 	AddEventItem(name string, query string)
-	Build() PlcSubscriptionRequest
+	Build() (PlcSubscriptionRequest, error)
+}
+
+type PlcSubscriptionRequestResult struct {
+	Request  PlcSubscriptionRequest
+	Response PlcSubscriptionResponse
+	Err      error
 }
 
 type PlcSubscriptionRequest interface {
+	Execute() <-chan PlcSubscriptionRequestResult
 	PlcRequest
 }
diff --git a/sandbox/plc4go/pkg/plc4go/model/plc_unsubscription_request.go b/sandbox/plc4go/pkg/plc4go/model/plc_unsubscription_request.go
index 7395b35..609e1cd 100644
--- a/sandbox/plc4go/pkg/plc4go/model/plc_unsubscription_request.go
+++ b/sandbox/plc4go/pkg/plc4go/model/plc_unsubscription_request.go
@@ -19,8 +19,16 @@
 package model
 
 type PlcUnsubscriptionRequestBuilder interface {
+	// TODO: Implement
+}
+
+type PlcUnsubscriptionRequestResult struct {
+	Request  PlcUnsubscriptionRequest
+	Response PlcUnsubscriptionResponse
+	Err      error
 }
 
 type PlcUnsubscriptionRequest interface {
+	Execute() <-chan PlcUnsubscriptionRequestResult
 	PlcRequest
 }
diff --git a/sandbox/plc4go/pkg/plc4go/model/plc_write_request.go b/sandbox/plc4go/pkg/plc4go/model/plc_write_request.go
index d881f52..c632088 100644
--- a/sandbox/plc4go/pkg/plc4go/model/plc_write_request.go
+++ b/sandbox/plc4go/pkg/plc4go/model/plc_write_request.go
@@ -21,11 +21,17 @@ package model
 import "plc4x.apache.org/plc4go-modbus-driver/0.8.0/pkg/plc4go/values"
 
 type PlcWriteRequestBuilder interface {
-	Execute() (<-chan PlcWriteResponse, error)
 	AddField(name string, query string, value values.PlcValue)
-	Build() PlcWriteRequest
+	Build() (PlcWriteRequest, error)
+}
+
+type PlcWriteRequestResult struct {
+	Request  PlcWriteRequest
+	Response PlcWriteResponse
+	Err      error
 }
 
 type PlcWriteRequest interface {
-	Execute() (<-chan PlcWriteResponse, error)
+	Execute() <-chan PlcWriteRequestResult
+	PlcRequest
 }