Developing Custom Connectors
For the following custom connector development example, we provide a sample base repository on GitHub. We recommend that you clone the base repository and follow the custom connector development guide below.
Sample base repository:
This base repository has the following structure:
service.yaml
is the sample service commissioning file.CustomConnection.js
andCustomEndpoint.js
contain the basic connection and data handling methods.CustomConnection.json
andCustomEndpoint.json
are the JSON Schemas that contain the configuration parameters.
Methods for Custom Connectors
To implement a custom connector, you need to implement certain methods. The most common methods are Connection
and DataHandle
.
Connection Methods
Connection methods establish connections to machines and handle disconnect and reconnect scenarios. Your protocol connection must typically implement the following Connection
methods:
handleConnect
handleDisconnect
handleReconnect
DataHandle Methods
To handle the data, implement the following DataHandle
methods:
handleRead
handleWrite
handleSubscribe
/handleUnsubscribe
(optional, otherwise a cron-based polling ofread()
is provided)
Implementing Connection Methods
To update the process with the correct state, the Connection
methods handleConnect
, handleDisconnect
, and handleReconnect
must call the appropriate transition methods of the base class.
The image below shows the different state machines and how they relate to the handle methods:
Example: Your handleConnect
method will probably start a network operation that receives a callback method that is called when the operation completes successfully or unsuccessfully. In this callback, you must call this.connectDone
or this.connectFailed
to trigger the correct state transition so that all other hooks and event listeners follow up correctly.
Connection Methods With and Without a Disconnect Method
You need to consider the following Connection
methods:
Connection methods that require both a connect and a disconnect method.
Connection methods that require only a connect method, even when reconnecting.
Connect and Disconnect
To create variables and objects on connection, add them to your
handleConnect
method.To delete variables and objects on disconnection or before a new connection attempt, add them to your
handleDisconnect
method.To delete variables and objects first and then recreate them when reconnecting, add them to your
handleReconnect
method.It is recommended that you add a private method to your class that creates the variables and objects, another private method that deletes them, and call them via the
handleConnect
,handleDisconnect
, andhandleReconnect
methods.
Connect without Disconnect
To create variables and objects that are static, add them to the
constructor
of your derived implementation class.
Reconnecting
Depending on what your client protocol library does, it can be sufficient to listen to all error events from your client, pass them to the connection state machine by calling e.g. this.connectLost
, and implement the handleReconnect
method with correct deletion and recreation of your connection objects. However, pay attention to what your client library is doing. The reconnect mechanism of the connection state machine will just start calling handleReconnect
with an increasing delay (as configured by the connectionStrategy section in the user parameters).
Code Example for Implementing Connection Methods
The following code example uses the host and port parameters to establish a TCP connection in the CustomConnection.js
file. First, you specify the host and port, then you define your handleConnect
method, which calls the createConnection
method and opens a TCP connection.
You can call the different states with the following methods:
connectFailed
reconnectFailed
connectDone
Implementing DataHandle Methods
There are two types of DataHandle
methods:
handleRead
/handleWrite
for single-message data (similar to POST and GET behavior)handleSubscribe
/handleUnsubscribe
for data subscriptions (similar to MQTT behavior)
If you implement data subscription in your client library, you must implement the handleSubscribe
/handleUnsubscribe
method pair. Again, this is optional because we can achieve similar behavior by calling the handleRead
on schedule. Those receive the address
object from the configuration file as first parameter, which in turn was defined in the JSON Schema (CustomEndpoint.json
).
Your actual protocol addresses are in the properties of this object according to your JSON Schema. The second argument is the JavaScript callback method which must be called on each data reception, i.e. just like an event listener method. In order to properly unregister that method, your handleUnsubscribe method will receive the same callback method argument so that you can properly unregister it if needed.
Code Example for Implementing DataHandling Methods (1)
For single-message data read and write, you need to implement handleRead
/handleWrite
, which similarly receives the address as defined in the JSON Schema (CustomEndpoint.json
) and the callback that needs to be called with the result.
Code Example for Implementing DataHandling Methods (2)
Specifying Custom Configuration Parameters
The set of all available parameters for each class is described using a JSON Schema. The following code example contains JSON Schemas for all properties (connection parameters) that you can configure in all Connectware resources.
The JSON Schema descriptions are stored in files next to the JavaScript files but with a .json
suffix. For example, for Connection.js
, the properties are described in the file Connection.json
file right next to the JavaScript file. For each derived protocol connection, there must also be JSON Schema files for connection and endpoint.
Related Links
Code Example for Specifying Custom Configuration Parameters
For the derived connection classes, you must implement a static getCustomSchema method, as shown in the following code example:
The JSON Schemas of the derived classes will contain the properties that are specific to that particular connection.
We recommend to add descriptions to all properties.
The object with all properties from the JSON Schema will be passed into the constructor in the base classes, which is then available as this._connection
member variable (or this._address
in the endpoint) during runtime.
Last updated
Was this helpful?