Skip to content

Commit

Permalink
StatusCode now an object
Browse files Browse the repository at this point in the history
use ===
  • Loading branch information
erossignon committed Feb 23, 2014
1 parent 493afb1 commit c8eff27
Show file tree
Hide file tree
Showing 41 changed files with 4,190 additions and 560 deletions.
4 changes: 2 additions & 2 deletions bin/interactive_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ function completer(line) {
if (line.trim().indexOf("open")) {

completions = 'localhost <port>'.split(' ');
hits = completions.filter(function(c) { return c.indexOf(line) == 0 });
hits = completions.filter(function(c) { return c.indexOf(line) === 0 });
return [hits.length ? hits : completions, line]
} else {
completions = 'open close getEndpoints quit'.split(' ');
hits = completions.filter(function(c) { return c.indexOf(line) == 0 });
hits = completions.filter(function(c) { return c.indexOf(line) === 0 });
// show all completions if none found
return [hits.length ? hits : completions, line]
}
Expand Down
39 changes: 39 additions & 0 deletions bin/more.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* write a file to the console, preserving the Ansi color decoration
*/
var argv = require('optimist')
.usage('Usage: $0 <file>')
.argv;

var fs = require("fs");



function readLines(input, func) {
var remaining = '';

input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
while (index > -1) {
var line = remaining.substring(0, index);
remaining = remaining.substring(index + 1);
func(line);
index = remaining.indexOf('\n');
}
});

input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
});
}

function func(data) {
console.log(data);
}

var input = fs.createReadStream(argv["_"][0]);

readLines(input, func);
2 changes: 1 addition & 1 deletion bin/opcua_interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TrafficAnalyser.prototype.add = function(data)
}
var messageHeader = opcua.readMessageHeader(stream);

if (messageHeader.msgType == "ERR") {
if (messageHeader.msgType === "ERR") {

var err = new s.TCPErrorMessage();
err.decode(stream);
Expand Down
2 changes: 1 addition & 1 deletion bin/simple_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function browseTree(root, nodeId, callback) {
root[node.browseName.name].typeDefinition =node.typeDefinition.toString();
root[node.browseName.name].nodes = {};

if (node.typeDefinition.value == folderTypeNodeId.value || node.typeDefinition.value == 2000 || node.typeDefinition.value == 62 ) {
if (node.typeDefinition.value === folderTypeNodeId.value || node.typeDefinition.value === 2000 || node.typeDefinition.value === 62 ) {

if (node.isForward) {
//xx console.log(" appending : " + node.nodeId.displayText());
Expand Down
60 changes: 36 additions & 24 deletions code_gen/generate_status_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,65 @@ var fs = require("fs");
var csv = require("csv");
var sprintf = require("sprintf").sprintf;



// see OPC-UA Part 6 , A2
var codeMap= {};
csv().from.stream(fs.createReadStream(__dirname+'/StatusCodes.csv')).to.array(function(data)
{
data.forEach(function(e){
var codeName = e[0];
codeMap[codeName] = parseInt(e[1]);
});
//xx console.log(data);
var codeMap = {};
csv().from.stream(fs.createReadStream(__dirname + '/StatusCodes.csv')).to.array(function (data) {
data.forEach(function (e) {
var codeName = e[0];
codeMap[codeName] = parseInt(e[1]);
});
//xx console.log(data);

parseStatusCodeXML();
console.log("codeMap" , codeMap);
parseStatusCodeXML();

});

function parseStatusCodeXML() {

var xmlFile = "./code_gen/UA_StatusCodes.xml";
var xmlFile = __dirname + "/UA_StatusCodes.xml";

var parser = new xml.Parser();

var obj = {};
var outFile = fs.createWriteStream("lib/opcua_status_code.js");
var outFile = fs.createWriteStream(__dirname + "/../lib/raw_status_codes.js");

outFile.write("// this file has been automatically generated\n");

outFile.write(" exports.StatusCodes = { \n");
parser.on('startElement',function(name,attrs) {
var obj ;
if ( name == "opc:Constant") {

outFile.write(" Good: { name:'Good', value: 0, description:'No Error' }\n");

parser.on('startElement', function (name, attrs) {

if (name == "opc:Constant") {
var cstName = attrs.Name;
if (cstName in codeMap) {
obj = { name:cstName,value:codeMap[cstName]};
obj = { name: cstName, value: codeMap[cstName]};
} else {
console.log("cannot find",cstName);
console.log("cannot find", cstName);
}
} else if ( name == "opc:Documentation") {
} else if (name == "opc:Documentation") {

parser.once("text",function(txt) {
parser.once("text", function (txt) {
obj.description = txt;
});
}
});
parser.on("endElement",function(name){
if ( name === "opc:TypeDictionary") {
outFile.write("};\n");
} else if ( name === "opc:Constant") {
outFile.write(sprintf(" %40s: { name: %40s , value: %6d ,description: \"%s\"}, \n",obj.name,"'"+obj.name+"'",obj.value, obj.description));
}

var sep = ",";
parser.on("endElement", function (name) {

if (name === "opc:TypeDictionary") {
outFile.write("};\n");
} else if (name === "opc:Constant") {
outFile.write(
sprintf("%1s %40s: { name: %40s , value: %6d ,description: \"%s\"}\n",
sep, obj.name, "'" + obj.name + "'", obj.value, obj.description));
sep = ",";
}
});

parser.write(fs.readFileSync(xmlFile));
Expand Down
2 changes: 1 addition & 1 deletion lib/chunk_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ChunkManager.prototype.write = function (buffer, length) {
while (l > 0) {
assert(length - input_cursor !== 0);

if (this.cursor == 0) {
if (this.cursor === 0) {
// let the client to write some stuff at the start of the chunk
if (!this._in_before_chunk) {
this._in_before_chunk = true;
Expand Down
2 changes: 2 additions & 0 deletions lib/client/client_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ OPCUAClientBase.prototype.__defineGetter__("transactionInProgress" ,function() {
*/
OPCUAClientBase.prototype.disconnect = function(callback) {

assert(_.isFunction(callback));

if (this._secureChannel) {
this._secureChannel.close(callback);
this._secureChannel = null;
Expand Down
4 changes: 2 additions & 2 deletions lib/client/client_secure_channel_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ _ResponseReceiver.prototype._handle_response= function(message_chunk) {
debugLog(hexDump(message_chunk).blue.bold);
debugLog(messageHeaderToString(message_chunk));

if (this._expectedMsgTypes.indexOf(msgType) == -1) {
if (this._expectedMsgTypes.indexOf(msgType) === -1) {
// invalid message type received
var errMessage ="the incoming messageChunk with msgType " + msgType + " is invalid ! expecting "+ this._expectedMsgTypes;
console.log(("ERROR ").red +errMessage);
Expand Down Expand Up @@ -345,7 +345,7 @@ ClientSecureChannelLayer.prototype._performMessageTransaction = function(msgType
return;
}
assert(self._transport, " must have a valid transport");
assert(msgType.length == 3);
assert(msgType.length === 3);
assert(responseClass);
assert(_.isFunction(callback));

Expand Down
10 changes: 6 additions & 4 deletions lib/datavalue.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var factories = require("./factories");
var ec = require("./encode_decode");
var set_flag = require("./utils").set_flag;
var check_flag = require("./utils").check_flag;

var StatusCodes = require("./opcua_status_code").StatusCode;

var DataValueEncodingByte_Schema = {
name:"DataValue_EncodingByte",
Expand Down Expand Up @@ -51,7 +51,7 @@ var DataValue_Schema = {
id: factories.next_available_id(),
fields: [
{ name:"value", fieldType:"Variant" , defaultValue: null },
{ name:"statusCode", fieldType:"StatusCode", defaultValue: 0x000 },
{ name:"statusCode", fieldType:"StatusCode", defaultValue: null },
{ name:"sourceTimestamp", fieldType:"DateTime" , defaultValue: null },
{ name:"sourcePicoseconds", fieldType:"UInt16" , defaultValue: 0 },
{ name:"serverTimestamp", fieldType:"DateTime" , defaultValue: null },
Expand All @@ -71,7 +71,8 @@ var DataValue_Schema = {

// write statusCode
if (check_flag(encoding_mask,DataValueEncodingByte.StatusCode)) {
ec.encodeUInt32(dataValue.statusCode,stream);
//ec.encodeUInt32(dataValue.statusCode.value,stream);
ec.encodeStatusCode(dataValue.statusCode,stream);
}
// write sourceTimestamp
if (check_flag(encoding_mask,DataValueEncodingByte.SourceTimestamp)) {
Expand Down Expand Up @@ -99,7 +100,8 @@ var DataValue_Schema = {
}
// read statusCode
if (check_flag(encoding_mask,DataValueEncodingByte.StatusCode)) {
dataValue.statusCode = ec.decodeUInt32(stream);
dataValue.statusCode = ec.decodeStatusCode(stream);

}
// read sourceTimestamp
if (check_flag(encoding_mask,DataValueEncodingByte.SourceTimestamp)) {
Expand Down
12 changes: 8 additions & 4 deletions lib/encode_decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var check_flag = require("./utils").check_flag;
exports.decodeUAString = function (stream) {
var value;
var length = stream.readInteger();
if (length == -1) {
if (length === -1) {
value = undefined;
} else {
value = stream._buffer.toString('binary', stream.length, stream.length + length);
Expand Down Expand Up @@ -52,6 +52,7 @@ exports.decodeUInt16 = function (stream) {


exports.encodeInt16 = function (value, stream) {
assert(_.isFinite(value));
stream.writeInt16(value);
};

Expand All @@ -60,6 +61,7 @@ exports.decodeInt16 = function (stream) {
};

exports.encodeInt32 = function (value, stream) {
assert(_.isFinite(value));
stream.writeInteger(value);
};

Expand Down Expand Up @@ -210,7 +212,7 @@ exports.encodeDateTime = function(date, stream) {
stream.writeUInt32(lo);
stream.writeUInt32(hi);

assert( date.toString() == bn_hundredNanoSecondFrom1601ToDate(hi,lo).toString());
assert( date.toString() === bn_hundredNanoSecondFrom1601ToDate(hi,lo).toString());
};

exports.decodeDateTime = function(stream) {
Expand Down Expand Up @@ -364,7 +366,7 @@ exports.encodeNodeId = function(nodeId,stream)
ec.encodeByteString(nodeId.value,stream);
break;
default:
assert( encoding_byte == EnumNodeIdEncoding.Guid.value , " encoding_byte = " + encoding_byte);
assert( encoding_byte === EnumNodeIdEncoding.Guid.value , " encoding_byte = " + encoding_byte);
stream.writeInt16(nodeId.namespace);
ec.encodeGUID(nodeId.value,stream);
break;
Expand Down Expand Up @@ -397,7 +399,7 @@ var _decodeNodeId = function(encoding_byte,stream) {
value = ec.decodeByteString(stream);
break;
default:
assert( encoding_byte == EnumNodeIdEncoding.Guid.value ," encoding_byte = " + encoding_byte.toString(16));
assert( encoding_byte === EnumNodeIdEncoding.Guid.value ," encoding_byte = " + encoding_byte.toString(16));
namespace = stream.readInt16();
value=ec.decodeGUID(stream);
assert(is_guid(value));
Expand Down Expand Up @@ -463,3 +465,5 @@ exports.decodeByteString = function(stream) {
return stream.readByteStream();
};

exports.decodeStatusCode = require("./opcua_status_code").decodeStatusCode;
exports.encodeStatusCode = require("./opcua_status_code").encodeStatusCode;
25 changes: 19 additions & 6 deletions lib/factories.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ var _ = require("underscore");
var hexDump = require("./utils").hexDump;

var objectNodeIds = require("./opcua_node_ids").Object;
var sc = require("./opcua_status_code");
assert(sc.StatusCodes.Good.value==0);


factories = {};
_enumerations = {};
var factories = {};
var _enumerations = {};


coerceNodeId = require("./nodeid").coerceNodeId;
var coerceNodeId = require("./nodeid").coerceNodeId;


var _defaultType = [
Expand Down Expand Up @@ -47,6 +48,18 @@ var _defaultType = [
{ name: "NodeId", encode: ec.encodeNodeId, decode: ec.decodeNodeId, defaultValue: ec.makeNodeId , coerce: coerceNodeId },
{ name: "ExpandedNodeId", encode: ec.encodeExpandedNodeId, decode: ec.decodeExpandedNodeId, defaultValue: ec.makeExpandedNodeId },

//The StatusCode is a 32-bit unsigned integer. The top 16 bits represent the numeric value of the
//code that shall be used for detecting specific errors or conditions. The bottom 16 bits are bit flags
//that contain additional information but do not affect the meaning of the StatusCode.
// 7.33 Part 4 - P 143
{
name:"StatusCode",
encode: sc.encodeStatusCode,
decode: sc.decodeStatusCode,
defaultValue: sc.StatusCodes.Good
},


{ name: "ByteString", encode: ec.encodeByteString, decode: ec.decodeByteString, defaultValue: function () {
return new Buffer(0);
} },
Expand Down Expand Up @@ -79,7 +92,7 @@ function decodeExtensionObject(stream) {
var nodeId = ec.decodeNodeId(stream);
var encodingType = stream.readByte();
var length = stream.readUInt32();
if (nodeId.value == 0 || encodingType == 0) {
if (nodeId.value === 0 || encodingType === 0) {
return null;
}
var object = exports.constructObject(nodeId);
Expand Down Expand Up @@ -494,7 +507,7 @@ var _exploreObject = function (kind_of_field, self, field, extra, data) {
data.lines.push(r(padding + fieldName, 30) + " " + r(fieldType, 15));
data.lines.push(_hexDump);
} else {
if (fieldType == "IntegerId" || fieldType == "UInt32") {
if (fieldType === "IntegerId" || fieldType === "UInt32") {
value = "" + value + " 0x" + value.toString(16);
}
str = r(padding + fieldName, 30) + " " + r(fieldType, 15) + " " + value;
Expand Down
2 changes: 1 addition & 1 deletion lib/guid.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ function is_guid(value) {
exports.is_guid = is_guid;

exports.isValidGUID = function (guid) {
assert(guid.length == 36);
assert(guid.length === 36);
return is_guid(guid);
};

0 comments on commit c8eff27

Please sign in to comment.