Skip to content

Instantly share code, notes, and snippets.

@erossignon
Created November 20, 2016 10:03
Show Gist options
  • Save erossignon/8d80ae05379a4b4c5fcbf83458432e70 to your computer and use it in GitHub Desktop.
Save erossignon/8d80ae05379a4b4c5fcbf83458432e70 to your computer and use it in GitHub Desktop.
How to determine dataType of a variable
/*global require,console,setTimeout */
var opcua = require("node-opcua");
var async = require("async");
var assert = require("assert");
var client = new opcua.OPCUAClient();
var endpointUrl = "opc.tcp://" + require("os").hostname() + ":26543/UA/Server";
///var myNodeId = "ns=411;s=Scalar_Simulation_Double";
var myNodeId = "ns=411;s=Scalar_Simulation_ImageGIF";
function with_session(the_session,done) {
var dataType = null;
async.series([
function(callback) {
var max_age = 0;
var nodes_to_read = [
{
nodeId: myNodeId,
attributeId: opcua.AttributeIds.DataType
}
];
the_session.read(nodes_to_read, max_age, function(err,nodes_to_read,dataValues) {
if (!err) {
console.log("\n - the dataType Id of the variable you want to read is ".cyan , dataValues[0].value.value.toString());
dataType = dataValues[0].value.value;
assert(dataType instanceof opcua.NodeId);
if (dataType.value <=25) {
// see ./node-opcua/schemas/DataType_enum.js
// we have a wellknown DataType
var dt = opcua.DataType.get(dataType.value);
console.log(" you must use dataType:".cyan + "opcua.DataType."+dt.toString()+ " in your variant".cyan);
} else {
// this is more complicated here => you need to walk up the hierachie of dataType until you reach
// a "well known" datatype.
return findBasicDataType(the_session,dataType,function(err,wellKnownDataType){
console.log(" you must use dataType:".cyan + "opcua.DataType."+wellKnownDataType.toString()+ " in your variant".cyan);
callback();
});
}
}
callback(err);
});
},
// read the name of the dataType
function(callback){
console.log("you can also find the browse Name of your type");
var max_age = 0;
var nodes_to_read = [
{
nodeId: dataType,
attributeId: opcua.AttributeIds.BrowseName
}
];
the_session.read(nodes_to_read, max_age, function(err,nodes_to_read,dataValues) {
if (!err) {
console.log(" result" , dataValues[0].value.toString());
dataType = dataValues[0].value.value;
assert(dataType instanceof opcua.QualifiedName);
}
callback(err);
});
},
],done);
}
function withOPCUAConnection(inner_function,callback) {
var the_session, the_subscription;
async.series([
// step 1 : connect to
function(callback) {
client.connect(endpointUrl,function (err) {
if(err) {
console.log(" cannot connect to endpoint :" , endpointUrl );
} else {
console.log("connected !");
}
callback(err);
});
},
// step 2 : createSession
function(callback) {
client.createSession( function(err,session) {
if(!err) {
the_session = session;
}
callback(err);
});
},
function(callback) {
inner_function(the_session,function(err) {
callback();
});
},
// close session
function(callback) {
the_session.close(function(err){
if(err) {
console.log("session closed failed ?");
}
callback();
});
}
],
function(err) {
if (err) {
console.log(" failure ",err);
} else {
console.log("done!");
}
client.disconnect(function(){});
}) ;
};
withOPCUAConnection(with_session);
var ReferenceTypeIds = opcua.ReferenceTypeIds;
var resultMask = opcua.browse_service.makeResultMask("ReferenceType | IsForward | BrowseName | NodeClass | TypeDefinition");
// Walk u
function findBasicDataType(session,dataTypeId,callback) {
if (dataTypeId.value <=25) {
// see ./node-opcua/schemas/DataType_enum.js
// we have a wellknown DataType
console.log(" " + dataTypeId.toString().cyan + " is a well known DataType".cyan);
var dataType = opcua.DataType.get(dataTypeId.value);
callback(null,dataType);
} else {
console.log(" " + dataTypeId.toString().cyan + " is not well known DataType, let's find the supertype".cyan);
// let's browse for the SuperType of this object
var nodeToBrowse = new opcua.browse_service.BrowseDescription({
// BrowseDescription
referenceTypeId: opcua.makeNodeId(ReferenceTypeIds.HasSubtype),
includeSubtypes: false,
browseDirection: opcua.browse_service.BrowseDirection.Inverse,
nodeId: dataTypeId,
resultMask: resultMask
});
//xx console.log(" nodetoBrowse", nodeToBrowse.toString());
session.browse([nodeToBrowse],function(err,results) {
var result = results[0];
if (err) return callback(err);
// xx console.log(result.toString());
var baseDataType = result.references[0].nodeId;
var name = result.references[0].browseName.toString();
console.log(" - the super type is " + baseDataType.toString() + " " + name);
return findBasicDataType(session,baseDataType,callback);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment