Skip to content

Commit

Permalink
add opcua write node
Browse files Browse the repository at this point in the history
  • Loading branch information
matteo-fantin committed Jul 12, 2024
1 parent 4749793 commit 8f4f1fb
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The first stable version will start from `1.0.0`
## Features

- [x] Read
- [ ] Write
- [x] Write
- [x] Browse
- [x] Check connection status
- [x] Subscription
Expand Down
2 changes: 1 addition & 1 deletion core.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function updateClientConnectionStatus(connectionId, status) {
console.debug(existingClient.applicationName + ":client has lost connection");
break;
}

core.eventEmitter.emit('client_connection_state', connectionId, status);
existingClient['clientState'] = status;
}

Expand Down
13 changes: 4 additions & 9 deletions opcua-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = function (RED) {

RED.nodes.createNode(this, args);
const opcuaclientnode = RED.nodes.getNode(args.client);
const existingClient = core.opcClients[opcuaclientnode.connectionId];

var node = this;

Expand All @@ -15,27 +16,21 @@ module.exports = function (RED) {

// Read Input Arg node
node.on('input', function (msg) {
const existingClient = core.opcClients[opcuaclientnode.connectionId];
if(!existingClient){
node.error("OPC UA Client not defined");
return;
}

if(existingClient.clientState == "reconnecting") return;
if(existingClient.clientState == "disconnected") return;

// Override nodeId from incoming node if not defined on read node
if (!args.nodeId && msg.nodeId) node.nodeId = msg.nodeId;

readNode(existingClient);
readNode();
});

async function readNode(opcClient) {
async function readNode() {
const nodeToRead = {
nodeId: node.nodeId,
attributeId: opcua.AttributeIds.Value
};
const dataValue = await opcClient.session.read(nodeToRead);
const dataValue = await existingClient.session.read(nodeToRead);
const dataValueString = JSON.stringify(dataValue);
const dataValueObj = JSON.parse(dataValueString);
node.send({ payload: dataValueObj });
Expand Down
27 changes: 27 additions & 0 deletions opcua-write.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script type="text/javascript">
RED.nodes.registerType('opcua-write', {
category: 'opcuax',
color: '#a6bbcf',
defaults: {
client: { value: "", required: true, type: "opcua-client" },
nodeid: { required: true },
},
inputs: 1,
outputs: 0,
icon: "opcua-logo.png",
label: function () {
return this.defaults.client.name;
}
});
</script>

<script type="text/html" data-template-name="opcua-write">
<div class="form-row">
<label for="node-input-client"><i class="fa fa-plug"></i> Client</label>
<input type="text" id="node-input-client" placeholder="">
</div>
<div class="form-row">
<label for="node-input-nodeid"><i class="fa fa-link"></i> NodeId</label>
<input type="text" id="node-input-nodeid" placeholder="ns=*;i=*, ns=*;s=*">
</div>
</script>
48 changes: 48 additions & 0 deletions opcua-write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// See example https://github.com/node-opcua/node-opcua/blob/af235f19e9e353fa748c11009b1300f76026fb28/packages/node-opcua-service-write/test/test_service_write.js
module.exports = function (RED) {

var core = require('./core');
var opcua = require('node-opcua');

function opcUaWriteNode(args) {

RED.nodes.createNode(this, args);
const opcuaclientnode = RED.nodes.getNode(args.client);
const existingClient = core.opcClients[opcuaclientnode.connectionId];

var node = this;

node.name = args.name;
node.nodeId = args.nodeid;

node.on('input', function (msg) {

if(existingClient.clientState == "reconnecting") return;
if(existingClient.clientState == "disconnected") return;

// Override nodeId from incoming node if not defined on read node
//if (!args.nodeId && msg.nodeId) node.nodeId = msg.nodeId;

const value = msg.payload.value;

write(value);
});

async function write(requestValue) {
const dataType = await existingClient.session.getBuiltInDataType(node.nodeId);
const resultStatus = await existingClient.session.write({
nodeId: node.nodeId,
attributeId: opcua.AttributeIds.Value,
value: new opcua.DataValue({
value: new opcua.Variant({
dataType: dataType,
value: requestValue
})
})
});
node.debug(resultStatus.description);
}
}

RED.nodes.registerType("opcua-write", opcUaWriteNode);
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "node-red-opcua-x",
"version": "0.6.0",
"version": "0.7.0",
"license": "MIT",
"author": {
"name": "Teo230",
"email": "teeassassin230@gmail.com"
"email": "m.f.tim@live.it"
},
"homepage": "https://github.com/Teo230/node-red-opcua-x",
"repository": {
Expand All @@ -21,7 +21,8 @@
"opcua-status": "opcua-status.js",
"opcua-read": "opcua-read.js",
"opcua-browse": "opcua-browse.js",
"opcua-subscription": "opcua-subscription.js"
"opcua-subscription": "opcua-subscription.js",
"opcua-write": "opcua-write.js"
}
},
"keywords": [
Expand Down

0 comments on commit 8f4f1fb

Please sign in to comment.