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-
135var async = require ( "async" ) ;
146var Q = require ( "q" ) ;
157//var md5 = require("md5");
@@ -30,6 +22,8 @@ exports.opcua = opcua;
3022opcua . 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