node-red-contrib-iiot-opcua
Version:
An Industrial IoT OPC UA toolbox contribution package for Node-RED based on node-opcua.
97 lines (94 loc) • 3.93 kB
JavaScript
/*
The BSD 3-Clause License
Copyright 2022 - DATATRONiQ GmbH (https://datatroniq.com)
Copyright (c) 2018-2022 Klaus Landsdorf (http://node-red.plus/)
Copyright 2015,2016 - Mika Karaila, Valmet Automation Inc. (node-red-contrib-opcua)
All rights reserved.
node-red-contrib-iiot-opcua
*/
'use strict';
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const opcua_iiot_core_1 = require("./core/opcua-iiot-core");
const opcua_iiot_core_connector_1 = require("./core/opcua-iiot-core-connector");
const underscore_1 = __importDefault(require("underscore"));
/**
* OPC UA node representation for Node-RED OPC UA IIoT nodes.
*
* @param RED
*/
module.exports = (RED) => {
// SOURCE-MAP-REQUIRED
function OPCUAIIoTNode(config) {
RED.nodes.createNode(this, config);
this.nodeId = config.nodeId;
this.datatype = config.datatype;
this.value = config.value;
this.topic = config.topic;
this.name = config.name;
this.injectType = config.injectType;
this.showErrors = config.showErrors;
let self = this;
self.iiot = {};
self.iiot.subscribed = false;
self.status({ fill: 'blue', shape: 'ring', text: 'new' });
self.toggleNodeStatusSymbol = () => {
self.iiot.subscribed = !self.iiot.subscribed;
if (self.injectType === 'listen') {
if (self.iiot.subscribed) {
self.status({ fill: 'blue', shape: 'dot', text: 'subscribed' });
}
else {
self.status({ fill: 'blue', shape: 'ring', text: 'not subscribed' });
}
}
else {
self.status({ fill: 'blue', shape: 'dot', text: 'injected' });
}
};
this.on('input', (msg) => {
self.toggleNodeStatusSymbol();
const topic = msg.topic || self.topic;
const payload = msg.payload;
const value = (payload === null || payload === void 0 ? void 0 : payload.value) ? payload.value : msg.payload;
const valuesToWrite = payload.valuesToWrite || [];
const addressSpaceItems = payload.addressSpaceItems || [];
if (self.injectType === 'write') {
addressSpaceItems.push({ name: self.name, nodeId: self.nodeId, datatypeName: self.datatype });
try {
if (typeof self.value !== "string") {
self.value = self.value.toString();
}
valuesToWrite.push((0, opcua_iiot_core_1.convertDataValueByDataType)((underscore_1.default.isEmpty(self.value)) ? value : self.value, self.datatype));
}
catch (err) {
opcua_iiot_core_connector_1.logger.internalDebugLog(err);
if (self.showErrors) {
this.error(err, msg);
}
}
}
else {
addressSpaceItems.push({ name: self.name, nodeId: self.nodeId, datatypeName: self.datatype });
}
const outputPayload = {
nodetype: "node",
injectType: self.injectType || payload.injectType,
addressSpaceItems,
valuesToWrite,
value,
};
const outputMessage = {
payload: outputPayload,
topic,
_msgid: msg._msgid
};
opcua_iiot_core_connector_1.logger.internalDebugLog('node msg stringified: ' + JSON.stringify(msg));
this.send(outputMessage);
});
}
RED.nodes.registerType('OPCUA-IIoT-Node', OPCUAIIoTNode);
};
//# sourceMappingURL=opcua-iiot-node.js.map