Skip to content
This repository was archived by the owner on Feb 18, 2026. It is now read-only.

Commit d77faa8

Browse files
committed
reuse the done modifications
1 parent 45bcc54 commit d77faa8

3 files changed

Lines changed: 222 additions & 108 deletions

File tree

models/opc-helper.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* Created by Thomas on 27.08.2015.
3+
*/
4+
var assert = require("better-assert");
5+
var _ = require("underscore");
6+
7+
/**
8+
* Helper Class
9+
* @type {{checkAndCorrectNodeId: Function, modifiyNodeArray: Function, addAtributeId: Function, concatNodesAndResults: Function}}
10+
*/
11+
var helper = {
12+
/**
13+
* Check NodeId and adds / removes stuff if necessary
14+
*
15+
* @accepts 'MI5.Queue.Queue0.', 'ns=4;s=MI5.Queue.Queue0'
16+
* @param baseNode
17+
* @returns {String}
18+
*/
19+
checkAndCorrectNodeId : function(baseNode) {
20+
// add . at the end if missing
21+
if (baseNode.slice(-1) == '.') {
22+
baseNode = baseNode.slice(0, -1);
23+
}
24+
25+
// check for namespace and node-identifier
26+
if (baseNode.slice(0, 7) != 'ns=4;s=') {
27+
baseNode = 'ns=4;s=' + baseNode;
28+
}
29+
30+
return baseNode;
31+
},
32+
33+
/**
34+
* Adds node-opcua specific nodes values: 'node' --> {nodeId: node}
35+
*
36+
* @param string
37+
* @returns object
38+
*/
39+
modifiyNodeArray : function(node) {
40+
return {
41+
nodeId : node
42+
};
43+
},
44+
45+
/**
46+
* Add attributeId to node-object array:
47+
* {nodeId: node} ==> {nodeId: node, attributeId: 13}
48+
*
49+
* @param node
50+
* @returns {number}
51+
*/
52+
addAtributeId : function(node){
53+
node.attributeId = 13;
54+
return node;
55+
},
56+
57+
/**
58+
* Combines nodes and results to one data array with the structure:
59+
* [{"nodeId":"MI5.Module1101.Output.SkillOutput.SkillOutput0.Busy",
60+
* "value":0}, {...}, {...}]
61+
*
62+
* @param nodes :
63+
* nodeId
64+
* @param results :
65+
* value
66+
* @returns {Array}
67+
*/
68+
concatNodesAndResults : function(nodes, results) {
69+
var output = new Array;
70+
for (var i = 0; i <= nodes.length; i++) {
71+
if (nodes[i] != undefined && results[i] != undefined) {
72+
// Check for BadNodeId (value: null, then statusCode)
73+
// console.log(nodes[i]);
74+
// console.log(results[i]);
75+
if (_.isEmpty(results[i].value)) {
76+
output[i] = {
77+
nodeId : nodes[i].nodeId.value,
78+
value : '',
79+
error : results[i].statusCode.description
80+
};
81+
} else {
82+
output[i] = {
83+
nodeId : nodes[i].nodeId.value,
84+
value : results[i].value.value
85+
};
86+
}
87+
}
88+
}
89+
return output;
90+
},
91+
92+
/**
93+
* returns Node-OPCUA Datatype according to normal datatype.
94+
*
95+
* e.g.: value
96+
* <scalar> the value to write (e.g. "hallo", 1, 23, 2.5, true)
97+
*
98+
* @param type
99+
* <string> corresponding type (e.g. String, Int16, Int32, Float,
100+
* Boolean)
101+
* @return <nodeData>
102+
*/
103+
convertDataType : function(type) {
104+
assert(typeof type === 'string');
105+
106+
var nodeopcua = require('node-opcua');
107+
108+
// Match Datatypes:
109+
if (type === 'String') {
110+
type = nodeopcua.DataType.String;
111+
} else if (type === 'Double') {
112+
type = nodeopcua.DataType.Double
113+
} else if (type === 'Float') {
114+
type = nodeopcua.DataType.Float
115+
} else if (type === 'Int16') {
116+
type = nodeopcua.DataType.Int16
117+
} else if (type === 'UInt16') {
118+
type = nodeopcua.DataType.UInt16
119+
} else if (type === 'Boolean') {
120+
type = nodeopcua.DataType.Boolean
121+
} else {
122+
console.log('Datatype is not supported by simpleOpcua Model');
123+
assert(false); // TODO: nicer way
124+
}
125+
126+
return type;
127+
},
128+
129+
};
130+
exports.helper = helper;

models/opc.js

Lines changed: 39 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
* Created by Thomas on 26.08.2015.
33
*/
44

5-
//var config = require('./../config.js');
6-
var nodeopcua = require('node-opcua');
7-
8-
//var EventEmitter = require("events").EventEmitter;
9-
//var util = require("util");
10-
//var assert = require("assert");
11-
var _ = require("underscore");
12-
135
var async = require("async");
146
var Q = require("q");
157
//var md5 = require("md5");
@@ -30,6 +22,8 @@ exports.opcua = opcua;
3022
opcua.prototype.connect = function(endpointurl, callback){
3123
var self = this;
3224

25+
var nodeopcua = require('node-opcua');
26+
3327
async.series([ function(cb) {
3428
// only create Client, if there is none
3529
if (!self.client) {
@@ -193,95 +187,49 @@ opcua.prototype.readQ = function(node){
193187
};
194188

195189
//==================================================================================
196-
//========================= Helper Function ========================================
190+
//========================= Write Element ==========================================
197191
//==================================================================================
198192

199-
/**
200-
* Helper Class
201-
* @type {{checkAndCorrectNodeId: Function, modifiyNodeArray: Function, addAtributeId: Function, concatNodesAndResults: Function}}
202-
*/
203-
var helper = {
204-
/**
205-
* Check NodeId and adds / removes stuff if necessary
206-
*
207-
* @accepts 'MI5.Queue.Queue0.', 'ns=4;s=MI5.Queue.Queue0'
208-
* @param baseNode
209-
* @returns {String}
210-
*/
211-
checkAndCorrectNodeId : function(baseNode) {
212-
// add . at the end if missing
213-
if (baseNode.slice(-1) == '.') {
214-
baseNode = baseNode.slice(0, -1);
215-
}
193+
opcua.prototype.write = function(nodeId, value, type, callback){
194+
var self = this;
216195

217-
// check for namespace and node-identifier
218-
if (baseNode.slice(0, 7) != 'ns=4;s=') {
219-
baseNode = 'ns=4;s=' + baseNode;
220-
}
196+
var nodeopcua = require('node-opcua');
221197

222-
return baseNode;
223-
},
224-
225-
/**
226-
* Adds node-opcua specific nodes values: 'node' --> {nodeId: node}
227-
*
228-
* @param string
229-
* @returns object
230-
*/
231-
modifiyNodeArray : function(node) {
232-
return {
233-
nodeId : node
234-
};
235-
},
236-
237-
/**
238-
* Add attributeId to node-object array:
239-
* {nodeId: node} ==> {nodeId: node, attributeId: 13}
240-
*
241-
* @param node
242-
* @returns {number}
243-
*/
244-
addAtributeId : function(node){
245-
node.attributeId = 13;
246-
return node;
247-
},
248-
249-
/**
250-
* Combines nodes and results to one data array with the structure:
251-
* [{"nodeId":"MI5.Module1101.Output.SkillOutput.SkillOutput0.Busy",
252-
* "value":0}, {...}, {...}]
253-
*
254-
* @param nodes :
255-
* nodeId
256-
* @param results :
257-
* value
258-
* @returns {Array}
259-
*/
260-
concatNodesAndResults : function(nodes, results) {
261-
var output = new Array;
262-
for (var i = 0; i <= nodes.length; i++) {
263-
if (nodes[i] != undefined && results[i] != undefined) {
264-
// Check for BadNodeId (value: null, then statusCode)
265-
// console.log(nodes[i]);
266-
// console.log(results[i]);
267-
if (_.isEmpty(results[i].value)) {
268-
output[i] = {
269-
nodeId : nodes[i].nodeId.value,
270-
value : '',
271-
error : results[i].statusCode.description
272-
};
273-
} else {
274-
output[i] = {
275-
nodeId : nodes[i].nodeId.value,
276-
value : results[i].value.value
277-
};
278-
}
279-
}
280-
}
281-
return output;
282-
},
198+
var nodeData = {
199+
nodeId : helper.checkAndCorrectNodeId(nodeId),
200+
attributeId : 13,
201+
value : new nodeopcua.DataValue({
202+
value : new nodeopcua.Variant({
203+
dataType : helper.convertDataType(type),
204+
value : value
205+
})
206+
})
207+
};
283208

209+
self.session.write([nodeData], function(err, results){
210+
if(err){
211+
callback(err, null);
212+
}
213+
callback(err,results.pop());
214+
});
215+
}
284216

217+
opcua.prototype.writeQ = function(nodeId, value, type){
218+
var self = this;
285219

220+
return Q.promise(function(resolve, reject){
221+
self.write(nodeId, value, type, function(err, result){
222+
if(!err){
223+
resolve(result);
224+
} else {
225+
reject(err);
226+
}
227+
});
228+
})
286229
};
287230

231+
//==================================================================================
232+
//========================= Helper Function ========================================
233+
//==================================================================================
234+
235+
var helper = require('./opc-helper.js').helper;

0 commit comments

Comments
 (0)