@tier0/node-red-contrib-opcda-client 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +89 -0
- package/images/opcda_read.png +0 -0
- package/images/opcda_server.png +0 -0
- package/images/opcda_write.png +0 -0
- package/opcda/opcda-read.html +145 -0
- package/opcda/opcda-read.js +350 -0
- package/opcda/opcda-server.html +143 -0
- package/opcda/opcda-server.js +91 -0
- package/opcda/opcda-write.html +32 -0
- package/opcda/opcda-write.js +290 -0
- package/package.json +45 -0
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
module.exports = function(RED) {
|
|
2
|
+
const opcda = require('node-opc-da');
|
|
3
|
+
const { OPCServer } = opcda;
|
|
4
|
+
const { ComServer, Session, Clsid, ComString} = opcda.dcom;
|
|
5
|
+
|
|
6
|
+
const errorCode = {
|
|
7
|
+
0x80040154 : "Clsid is not found.",
|
|
8
|
+
0x00000005 : "Access denied. Username and/or password might be wrong.",
|
|
9
|
+
0xC0040006 : "The Items AccessRights do not allow the operation.",
|
|
10
|
+
0xC0040004 : "The server cannot convert the data between the specified format/ requested data type and the canonical data type.",
|
|
11
|
+
0xC004000C : "Duplicate name not allowed.",
|
|
12
|
+
0xC0040010 : "The server's configuration file is an invalid format.",
|
|
13
|
+
0xC0040009 : "The filter string was not valid",
|
|
14
|
+
0xC0040001 : "The value of the handle is invalid. Note: a client should never pass an invalid handle to a server. If this error occurs, it is due to a programming error in the client or possibly in the server.",
|
|
15
|
+
0xC0040008 : "The item ID doesn't conform to the server's syntax.",
|
|
16
|
+
0xC0040203 : "The passed property ID is not valid for the item.",
|
|
17
|
+
0xC0040011 : "Requested Object (e.g. a public group) was not found.",
|
|
18
|
+
0xC0040005 : "The requested operation cannot be done on a public group.",
|
|
19
|
+
0xC004000B : "The value was out of range.",
|
|
20
|
+
0xC0040007 : "The item ID is not defined in the server address space (on add or validate) or no longer exists in the server address space (for read or write).",
|
|
21
|
+
0xC004000A : "The item's access path is not known to the server.",
|
|
22
|
+
0x0004000E : "A value passed to WRITE was accepted but the output was clamped.",
|
|
23
|
+
0x0004000F : "The operation cannot be performed because the object is being referenced.",
|
|
24
|
+
0x0004000D : "The server does not support the requested data rate but will use the closest available rate.",
|
|
25
|
+
0x00000061 : "Clsid syntax is invalid"
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function resolveError(e) {
|
|
29
|
+
if (errorCode[e]) return errorCode[e];
|
|
30
|
+
if (typeof e === 'number') return `DCOM error code: 0x${(e >>> 0).toString(16).toUpperCase()}`;
|
|
31
|
+
if (e instanceof Error) return e.message || e.toString();
|
|
32
|
+
if (typeof e === 'string') return e;
|
|
33
|
+
try { return JSON.stringify(e); } catch (_) { return String(e); }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const itemTypes = {
|
|
37
|
+
"double" : opcda.dcom.Types.DOUBLE,
|
|
38
|
+
"short" : opcda.dcom.Types.SHORT,
|
|
39
|
+
"integer" : opcda.dcom.Types.INTEGER,
|
|
40
|
+
"float" : opcda.dcom.Types.FLOAT,
|
|
41
|
+
"byte" : opcda.dcom.Types.BYTE,
|
|
42
|
+
"long" : opcda.dcom.Types.LONG,
|
|
43
|
+
"boolean" : opcda.dcom.Types.BOOLEAN,
|
|
44
|
+
"uuid" : opcda.dcom.Types.UUID,
|
|
45
|
+
"string" : opcda.dcom.Types.COMSTRING,
|
|
46
|
+
"char" : opcda.dcom.Types.CHARACTER,
|
|
47
|
+
"date" : opcda.dcom.Types.DATE,
|
|
48
|
+
"currency" : opcda.dcom.Types.CURRENCY,
|
|
49
|
+
"array" : opcda.dcom.Types.ARRAY
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
function OPCDAWrite(config) {
|
|
53
|
+
RED.nodes.createNode(this,config);
|
|
54
|
+
let node = this;
|
|
55
|
+
|
|
56
|
+
let server = RED.nodes.getNode(config.server);
|
|
57
|
+
|
|
58
|
+
node.opcItemMgr = null;
|
|
59
|
+
node.opcSyncIO = null;
|
|
60
|
+
node.opcGroup = null;
|
|
61
|
+
|
|
62
|
+
let clientHandle = 0;
|
|
63
|
+
|
|
64
|
+
let serverHandles = {};
|
|
65
|
+
|
|
66
|
+
node.isConnected = false;
|
|
67
|
+
node.isWriting = false;
|
|
68
|
+
|
|
69
|
+
if(!server){
|
|
70
|
+
node.error("Please select a server.");
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!server.credentials) {
|
|
75
|
+
node.error("Failed to load credentials!");
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
node.updateStatus = function(status){
|
|
80
|
+
switch(status){
|
|
81
|
+
case "disconnected":
|
|
82
|
+
node.status({fill:"red",shape:"ring",text:"Disconnected"});
|
|
83
|
+
break;
|
|
84
|
+
case "connecting":
|
|
85
|
+
node.status({fill:"yellow",shape:"ring",text:"Connecting"});
|
|
86
|
+
break;
|
|
87
|
+
case "ready":
|
|
88
|
+
node.status({fill:"green",shape:"ring",text:"Ready"});
|
|
89
|
+
break;
|
|
90
|
+
case "writing":
|
|
91
|
+
node.status({fill:"blue",shape:"ring",text:"Writing"});
|
|
92
|
+
break;
|
|
93
|
+
case "error":
|
|
94
|
+
node.status({fill:"red",shape:"ring",text:"Error"});
|
|
95
|
+
break;
|
|
96
|
+
case "mismatch":
|
|
97
|
+
node.status({fill:"yellow",shape:"ring",text:"Mismatch"});
|
|
98
|
+
break;
|
|
99
|
+
default:
|
|
100
|
+
node.status({fill:"grey",shape:"ring",text:"Unknown"});
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
node.init = function(){
|
|
106
|
+
return new Promise(async function(resolve, reject){
|
|
107
|
+
if(!node.isConnected){
|
|
108
|
+
|
|
109
|
+
try{
|
|
110
|
+
node.updateStatus('connecting');
|
|
111
|
+
var timeout = parseInt(server.config.timeout);
|
|
112
|
+
var comSession = new Session();
|
|
113
|
+
comSession = comSession.createSession(server.config.domain, server.credentials.username, server.credentials.password);
|
|
114
|
+
comSession.setGlobalSocketTimeout(timeout);
|
|
115
|
+
|
|
116
|
+
node.tout = setTimeout(function(){
|
|
117
|
+
node.updateStatus("timeout");
|
|
118
|
+
reject("Connection Timeout");
|
|
119
|
+
}, timeout);
|
|
120
|
+
|
|
121
|
+
node.comServer = new ComServer(new Clsid(server.config.clsid), server.config.address, comSession);
|
|
122
|
+
await node.comServer.init();
|
|
123
|
+
|
|
124
|
+
var comObject = await node.comServer.createInstance();
|
|
125
|
+
node.opcServer = new OPCServer();
|
|
126
|
+
await node.opcServer.init(comObject);
|
|
127
|
+
|
|
128
|
+
clearTimeout(node.tout);
|
|
129
|
+
|
|
130
|
+
serverHandles = [];
|
|
131
|
+
clientHandles = [];
|
|
132
|
+
node.opcGroup = await node.opcServer.addGroup(config.id, null);
|
|
133
|
+
node.opcItemMgr = await node.opcGroup.getItemManager();
|
|
134
|
+
node.opcSyncIO = await node.opcGroup.getSyncIO();
|
|
135
|
+
|
|
136
|
+
node.isConnected = true;
|
|
137
|
+
|
|
138
|
+
node.updateStatus('ready');
|
|
139
|
+
|
|
140
|
+
resolve();
|
|
141
|
+
}
|
|
142
|
+
catch(e){
|
|
143
|
+
reject(e);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
node.destroy = function(){
|
|
150
|
+
return new Promise(async function(resolve){
|
|
151
|
+
try{
|
|
152
|
+
node.isConnected = false;
|
|
153
|
+
|
|
154
|
+
if (node.opcSyncIO) {
|
|
155
|
+
await node.opcSyncIO.end();
|
|
156
|
+
node.opcSyncIO = null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (node.opcItemMgr) {
|
|
160
|
+
await node.opcItemMgr.end();
|
|
161
|
+
node.opcItemMgr = null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (node.opcGroup) {
|
|
165
|
+
await node.opcGroup.end();
|
|
166
|
+
node.opcGroup = null;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if(node.opcServer){
|
|
170
|
+
node.opcServer.end();
|
|
171
|
+
node.opcServer = null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if(node.comServer){
|
|
175
|
+
node.comServer.closeStub();
|
|
176
|
+
node.comServer = null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
resolve();
|
|
180
|
+
}
|
|
181
|
+
catch(e){
|
|
182
|
+
reject(e);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function writeGroup(itemValues){
|
|
188
|
+
|
|
189
|
+
try{
|
|
190
|
+
node.isWriting = true;
|
|
191
|
+
node.updateStatus("writing");
|
|
192
|
+
|
|
193
|
+
var objects = [];
|
|
194
|
+
for(itemValue of itemValues){
|
|
195
|
+
if(!(itemValue.itemID in serverHandles)){
|
|
196
|
+
clientHandle++;
|
|
197
|
+
var item = [{itemID: itemValue.itemID, clientHandle: clientHandle}];
|
|
198
|
+
var addedItem = await node.opcItemMgr.add(item);
|
|
199
|
+
|
|
200
|
+
if ((addedItem[0])[0] !== 0) {
|
|
201
|
+
node.warn(`Error adding item '${item[0].itemID}'`);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
else {
|
|
205
|
+
serverHandles[itemValue.itemID] = (addedItem[0])[1].serverHandle;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
var object = {
|
|
210
|
+
value: itemValue.type == 'string' ? new ComString(itemValue.value, null) : itemValue.value,
|
|
211
|
+
handle: serverHandles[itemValue.itemID],
|
|
212
|
+
type: itemTypes[itemValue.type]
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
objects.push(object);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
await node.opcSyncIO.write(objects);
|
|
219
|
+
|
|
220
|
+
var msg = { payload: true };
|
|
221
|
+
node.send(msg);
|
|
222
|
+
|
|
223
|
+
node.updateStatus("ready");
|
|
224
|
+
}
|
|
225
|
+
catch(e){
|
|
226
|
+
node.error("opcda-error", e.message);
|
|
227
|
+
node.updateStatus('error');
|
|
228
|
+
|
|
229
|
+
node.reconnect();
|
|
230
|
+
|
|
231
|
+
var msg = { payload: false };
|
|
232
|
+
node.send(msg);
|
|
233
|
+
}
|
|
234
|
+
finally{
|
|
235
|
+
node.isWriting = false;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
node.isReconnecting = false;
|
|
240
|
+
node.reconnect = async function(){
|
|
241
|
+
try{
|
|
242
|
+
if(!node.isReconnecting){
|
|
243
|
+
node.isReconnecting = true;
|
|
244
|
+
await node.destroy();
|
|
245
|
+
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
246
|
+
await node.init();
|
|
247
|
+
node.isReconnecting = false;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
node.comServer.on('disconnected',async function(){
|
|
251
|
+
node.isConnected = false;
|
|
252
|
+
node.updateStatus('disconnected');
|
|
253
|
+
await node.reconnect();
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
catch(e){
|
|
257
|
+
node.isReconnecting = false;
|
|
258
|
+
var msg = resolveError(e);
|
|
259
|
+
node.updateStatus('error');
|
|
260
|
+
node.error(`OPC DA connection error: ${msg}`);
|
|
261
|
+
switch(e) {
|
|
262
|
+
case 0x00000005:
|
|
263
|
+
case 0xC0040010:
|
|
264
|
+
case 0x80040154:
|
|
265
|
+
case 0x00000061:
|
|
266
|
+
return;
|
|
267
|
+
default:
|
|
268
|
+
await node.reconnect();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
node.reconnect();
|
|
274
|
+
|
|
275
|
+
node.on('input', function(msg){
|
|
276
|
+
if(node.isConnected && !node.isWriting){
|
|
277
|
+
writeGroup(msg.payload);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
node.on('close', function(done){
|
|
282
|
+
node.status({});
|
|
283
|
+
node.destroy().then(function(){
|
|
284
|
+
done();
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
RED.nodes.registerType("tier0-opcda-write",OPCDAWrite);
|
|
290
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tier0/node-red-contrib-opcda-client",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Node-RED OPC DA Reading and Writing Node",
|
|
5
|
+
"node-red": {
|
|
6
|
+
"nodes": {
|
|
7
|
+
"tier0-opc-read": "opcda/opcda-read.js",
|
|
8
|
+
"tier0-opc-write": "opcda/opcda-write.js",
|
|
9
|
+
"tier0-opc-server": "opcda/opcda-server.js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"opcda",
|
|
13
|
+
"plc",
|
|
14
|
+
"industry4.0",
|
|
15
|
+
"dcom",
|
|
16
|
+
"node-red",
|
|
17
|
+
"opc-da",
|
|
18
|
+
"automation",
|
|
19
|
+
"i4.0"
|
|
20
|
+
]
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"opcda",
|
|
24
|
+
"plc",
|
|
25
|
+
"industry4.0",
|
|
26
|
+
"dcom",
|
|
27
|
+
"opc-da",
|
|
28
|
+
"node-red",
|
|
29
|
+
"automation",
|
|
30
|
+
"i4.0"
|
|
31
|
+
],
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/FREEZONEX/opcda-client.git"
|
|
35
|
+
},
|
|
36
|
+
"author": "FREEZONEX",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/FREEZONEX/opcda-client/issues"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://github.com/FREEZONEX/opcda-client#readme",
|
|
41
|
+
"license": "Apache-2.0",
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"node-opc-da": "^1.0.6"
|
|
44
|
+
}
|
|
45
|
+
}
|