-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathclient.go
More file actions
131 lines (103 loc) · 3.37 KB
/
client.go
File metadata and controls
131 lines (103 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package plc4xvalue
import (
"github.com/elastic/elastic-agent-libs/logp"
"github.com/apache/plc4x/plc4go/pkg/api"
"github.com/apache/plc4x/plc4go/pkg/api/drivers"
"github.com/apache/plc4x/plc4go/pkg/api/model"
"github.com/apache/plc4x/plc4go/pkg/api/values"
"errors"
"fmt"
)
type Client struct {
connection plc4go.PlcConnection
connectionResult plc4go.PlcConnectionConnectResult
config *MetricSet
connected bool
counter int
}
type ResponseObject struct {
node Node
value values.PlcValue
}
type Node struct {
ID string `config:"tag"`
Label string `config:"label"`
Name string
}
func (client *Client) connect() (bool, error) {
var err error
var config = client.config
//Implement connection logic
// Create a new instance of the PlcDriverManager
driverManager := plc4go.NewPlcDriverManager()
// Register the Drivers
drivers.RegisterModbusTcpDriver(driverManager)
drivers.RegisterAdsDriver(driverManager)
drivers.RegisterBacnetDriver(driverManager)
drivers.RegisterCBusDriver(driverManager)
drivers.RegisterEipDriver(driverManager)
drivers.RegisterKnxDriver(driverManager)
drivers.RegisterS7Driver(driverManager)
// Get a connection to a remote PLC
connectionRequestChanel := driverManager.GetConnection(config.Endpoint)
// Wait for the driver to connect (or not)
client.connectionResult = <-connectionRequestChanel
// Check if something went wrong
if client.connectionResult.GetErr() != nil {
fmt.Printf("Error connecting to PLC: %s", client.connectionResult.GetErr().Error())
return false, client.connectionResult.GetErr()
}
client.connection = client.connectionResult.GetConnection()
if !client.connection.IsConnected() {
return false, errors.New("The connection is not established")
}
client.connected = true
logp.Info("[PLC4x] Connection established")
return true, err
}
func (client *Client) read() ([]*ResponseObject, error) {
var retVal []*ResponseObject
logp.Info("[PLC4x] Start read node values")
// Prepare a read-request
for _, node := range client.config.Nodes {
var response ResponseObject
readRequest, err := client.connection.ReadRequestBuilder().
AddTagAddress("tag", node.ID).
Build()
if err != nil {
logp.Info("[PLC4x] Error preparing read-request")
return retVal, err
}
// Execute a read-request
rrc := readRequest.Execute()
// Wait for the response to finish
rrr := <-rrc
if rrr.GetErr() != nil {
logp.Info("[PLC4x] Error executing read-request: %s", rrr.GetErr().Error())
return retVal, rrr.GetErr()
}
// Do something with the response
if rrr.GetResponse().GetResponseCode("tag") != model.PlcResponseCode_OK {
fmt.Printf("error an non-ok return code: %s", rrr.GetResponse().GetResponseCode("tag").GetName())
return retVal, rrr.GetErr()
}
response.node = node
response.value = rrr.GetResponse().GetValue("tag")
fmt.Printf("Got result %f", response.value.GetFloat32())
retVal = append(retVal, &response)
}
return retVal, nil
}
func (client *Client) closeConnection() {
logp.Debug("Shutdown", "Will shutdown connection savely")
client.connected = false
//Fetch panic during shutdown. So that the beat can reconnect
defer func() {
if r := recover(); r != nil {
logp.Info("The connection was already closed / terminated")
}
}()
// Make sure the connection is closed at the end
client.connection.BlockingClose()
logp.Debug("Shutdown", "Shutdown successfully")
}