Skip to content

Commit

Permalink
update status badge
Browse files Browse the repository at this point in the history
  • Loading branch information
Teo230 committed Jul 3, 2024
1 parent 1bc56b3 commit 65ee76d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 49 deletions.
4 changes: 3 additions & 1 deletion core.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*/
var core = core || { opcClient: {} }
core.opcua = core.opcua || require('node-opcua')
core.storage = core.storage || require('node-persist')
core.opcClient = core.opcClient || new core.opcua.OPCUAClient();
core.opcSession = core.opcSession || null
core.opcClientStatus = core.opcClientStatus || "disconnected"

core.createOpcUaClient = function(name){
// caused the connect method to fail after one single unsuccessful retry
Expand All @@ -35,11 +35,13 @@ core.createOpcUaClient = function(name){

core.connect = async function(host){
await core.opcClient.connect(host);
core.opcClientStatus = "connected";
core.opcSession = await core.opcClient.createSession();
}

core.close = async function(){
await core.opcSession.close();
core.opcClientStatus = "disconnected";
core.opcSession = null;
}

Expand Down
36 changes: 21 additions & 15 deletions opcua-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module.exports = function (RED) {

let node = this;

//Initialize persist storage
core.storage.init({dir: `${RED.settings.userDir}/cache/node-red-opcua-x`});

node.name = args.name; //unique name identifier
node.host = args.host; //opc.tcp

Expand All @@ -29,17 +26,16 @@ module.exports = function (RED) {
async function connect() {
try {

opcClient.on("abort", () => node.debug("client has aborted"));
opcClient.on("close", () => node.debug("client has closed"));
opcClient.on("connection_reestablished", () => sendClientConnectionStatus(true));
opcClient.on("connection_lost", () => sendClientConnectionStatus(false));
opcClient.on("start_reconnection", () => node.debug("client is trying to reconnect"));
opcClient.on("after_reconnection", () => node.debug("client start reconnection"));
opcClient.on("abort", () => sendClientConnectionStatus("disconnected"));
opcClient.on("close", () => sendClientConnectionStatus("disconnected"));
opcClient.on("connection_reestablished", () => sendClientConnectionStatus("connected"));
opcClient.on("connection_lost", () => sendClientConnectionStatus("disconnected"));
opcClient.on("start_reconnection", () => sendClientConnectionStatus("reconnecting"));
opcClient.on("after_reconnection", () => sendClientConnectionStatus("reconnecting"));

await core.connect(node.host);
sendClientConnectionStatus(true);

core.opcSession.on("session_closed", () => node.debug("session has closed"));
core.opcSession.on("session_closed", () => sendClientConnectionStatus("disconnected"));
// core.opcSession.on("keepalive", () => node.debug("session keepalive"));
// core.opcSession.on("keepalive_failure", () => node.debug("session keepalive failure"));

Expand All @@ -48,10 +44,20 @@ module.exports = function (RED) {
}
}

function sendClientConnectionStatus(connected) {
if (connected) node.debug("client has reconnected");
else node.debug("client has lost connection");
core.storage.setItem("client-connected", connected);
function sendClientConnectionStatus(status) {
switch(status){
case "connected":
node.debug("client has reconnected");
break;
case "reconnecting":
node.debug("client is trying to reconnect");
break
case "disconnected":
node.debug("client has lost connection");
break;
}

core.opcClientStatus = status;
}

async function onNodeClosed(done){
Expand Down
5 changes: 5 additions & 0 deletions opcua-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ module.exports = function (RED) {
// Read Input Arg node
node.on('input', function (msg) {

if(!core.opcSession){
node.error("Session not defined");
return;
}

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

Expand Down
35 changes: 18 additions & 17 deletions opcua-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@ module.exports = function (RED) {

RED.nodes.createNode(this, config);

const storage = core.storage;
let node = this;
let state = false;

setInterval(checkServerConnection, 5000);

function checkServerConnection() {
if (storage === null || storage === undefined) return;

storage.getItem("client-connected").then((value) => {
if (state === value) return;
state = value;

if(state){
node.status({fill:"green",shape:"dot",text:"connected"});
}else{
node.status({fill:"red",shape:"ring",text:"disconnected"});
}

var msg = { payload: state ? 'connected' : 'disconnected' };

node.send(msg);
});
if (state === core.opcClientStatus) return;
state = core.opcClientStatus;

switch(state){
case "connected":
node.status({ fill: "green", shape: "dot", text: "connected" });
break;
case "reconnecting":
node.status({ fill: "yellow", shape: "ring", text: "reconnecting" });
break;
case "disconnected":
default:
node.status({ fill: "red", shape: "ring", text: "disconnected" });
}

var msg = { payload: state };

node.send(msg);
}

}
Expand Down
16 changes: 3 additions & 13 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-opcua-x",
"version": "0.1.0",
"version": "0.2.0",
"node-red": {
"nodes": {
"opcua-client": "opcua-client.js",
Expand All @@ -10,7 +10,6 @@
}
},
"dependencies": {
"node-opcua": "^2.127.1",
"node-persist": "^4.0.1"
"node-opcua": "^2.127.1"
}
}

0 comments on commit 65ee76d

Please sign in to comment.