Skip to content

Commit

Permalink
Merge pull request #26 from boschrexroth/bugfix/read_with_args
Browse files Browse the repository at this point in the history
fix: read with argument failed with error DL_TYPE_MISMATCH.
  • Loading branch information
Sebastian Krauskopf committed Sep 20, 2021
2 parents 713aacf + 6e561c3 commit 17b03c7
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 64 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -88,6 +88,7 @@ Any use of the source code and related documents of this repository in applicati
* 2021-06-29: 1.8.7 - fix: writing strings with non-ASCII symbols (e.g. 'ü') resulted in DL_TYPE_MISMATCH.
* 2021-07-04: 1.8.8 - fix: give each of the subscribe nodes his own counter to show in the status text.
fix: allow to make a create without arguments (msg.payload = null). E.g. for '/motion/axs/<axs_name>/cmd/reset'.
* 2021-09-20: 1.8.9 - fix: read with argument failed with error DL_TYPE_MISMATCH.

## About

Expand Down
120 changes: 63 additions & 57 deletions lib/CtrlxDatalayerV2.js
Expand Up @@ -233,76 +233,82 @@ class CtrlxDatalayer {
* @throws {CtrlxProblemError} Throws an error when device returns an error.
* @throws Throws different http errors when connection could not be established.
*/
static read(hostname, port, servername, authorization, path, data = undefined, type, timeout, callback) {
static read(hostname, port, servername, authorization, path, data = undefined, type, timeout, callback) {

let options = {
hostname: hostname,
servername: servername,
port: port,
method: 'GET',
headers: {
'Accept': 'application/json',
'Authorization': authorization,
'Connection': 'keep-alive'
},
agent: keepAliveAgent,
rejectUnauthorized: false // accept self-signed certificates
};

switch (type) {
case 'browse':
case 'metadata':
options.path = encodeURI('/automation/api/v2/nodes/' + path + '?type=' + type);
break;

case 'read':
default:
if (typeof data !== 'undefined') {
options.path = encodeURI('/automation/api/v2/nodes/' + path + '?data=') + encodeURIComponent(JSON.stringify(data));
} else {
options.path = encodeURI('/automation/api/v2/nodes/' + path);
}
}

let options = {
hostname: hostname,
servername: servername,
port: port,
path: encodeURI('/automation/api/v2/nodes/' + path + '?type=' + type),
method: (typeof data !== 'undefined') ? 'POST' : 'GET',
headers: {
'Accept': 'application/json',
'Authorization': authorization,
'Connection': 'keep-alive'
},
agent: keepAliveAgent,
rejectUnauthorized: false // accept self-signed certificates
};
if (timeout >= 0) {
options.timeout = timeout;
}

let postData;
if (typeof data !== 'undefined') {
postData = JSON.stringify(data);
options.headers['Content-Type'] = 'application/json';
options.headers['Content-Length'] = Buffer.byteLength(postData);
}
const req = https.request(options, (res) => {
let data = "";

if (timeout >= 0) {
options.timeout = timeout;
}
res.setEncoding('utf8');
res.on('data', function(chunk) {
data += chunk;
});

const req = https.request(options, (res) => {
let data = "";
res.on('end', function() {

res.setEncoding('utf8');
res.on('data', function(chunk) {
data += chunk;
});
// We expect 200 on success
if (res.statusCode !== 200) {
callback(CtrlxProblemError.fromHttpResponse(res, data));
return;
}

res.on('end', function() {
// Try to parse the data
let payload;
try {
payload = JSON.parse(data);
} catch (err) {
callback(err, null);
}

// We expect 200 on success
if (res.statusCode !== 200) {
callback(CtrlxProblemError.fromHttpResponse(res, data));
return;
}
// No error, return payload data.
callback(null, payload);
});
});

// Try to parse the data
let payload;
try {
payload = JSON.parse(data);
} catch (err) {
callback(err, null);
}
req.on('timeout', () => {
req.abort();
});

// No error, return payload data.
callback(null, payload);
req.on('error', (err) => {
callback(err);
});
});

req.on('timeout', () => {
req.abort();
});
req.end();
}

req.on('error', (err) => {
callback(err);
});

if (typeof data !== 'undefined') {
req.write(postData);
}
req.end();
}


/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-ctrlx-automation",
"version": "1.8.8",
"version": "1.8.9",
"description": "Node-RED nodes for ctrlX AUTOMATION",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion test/CtrlxCore.nodes.test.js
Expand Up @@ -182,7 +182,7 @@ describe('CtrlxCoreDataLayerNodes', function() {
let ctrlx = new CtrlxCore(getHostname(), getUsername(), getPassword());

ctrlx.logIn()
.then(() => ctrlx.datalayerRead('test/add', {arg1: 17, arg2: 5}) )
.then(() => ctrlx.datalayerRead('test/add', { 'type': 'object', 'value': {'arg1': 17, 'arg2': 5} }) )
.then((data) => {
expect(data.type).to.equal('uint32');
expect(data.value).to.equal(22);
Expand Down
2 changes: 1 addition & 1 deletion test/ctrlx-datalayer-request.test.js
Expand Up @@ -274,7 +274,7 @@ describe('ctrlx-datalayer-request', function() {
});

// @ts-ignore
n1.receive({ payload: {arg1: 17, arg2: 5} });
n1.receive({ payload: { type: 'object', value: {arg1: 17, arg2: 5} }});
});
});

Expand Down
16 changes: 12 additions & 4 deletions test/helper/CtrlxMockupV2.js
Expand Up @@ -286,15 +286,23 @@ class CtrlxMockupV2 {
//
// Builtin Data Mockups - Read with parameter
//
this.app.post('/automation/api/v2/nodes/test/add', authenticateJWT, (req, res) => {
let x1 = req.body.arg1;
let x2 = req.body.arg2;
this.app.get('/automation/api/v2/nodes/test/add', authenticateJWT, (req, res) => {
if (typeof req.query.data === 'undefined') {
res.statusCode = 405;
res.send();
return;
}

if (typeof x1 === 'undefined' || typeof x2 === 'undefined') {
let data = JSON.parse(req.query.data);
if (typeof data.value === 'undefined' || typeof data.value.arg1 === 'undefined' || typeof data.value.arg2 === 'undefined') {
res.statusCode = 405;
res.send();
return;
}

let x1 = data.value.arg1;
let x2 = data.value.arg2;

res.statusCode = 200;
res.json({
value: x1 + x2,
Expand Down
30 changes: 30 additions & 0 deletions test/helper/benchmark.js
Expand Up @@ -220,6 +220,35 @@ async function benchmarkSubscriptionSimple() {
}


/**
* For simple tests...
*/
async function test() {

try {

await ctrlx.logIn();

let fFieldbus = await ctrlx.datalayerRead(
'fieldbuses/ethercat/master/instances/ethercatmaster/device_access/slave_online_info',
{
"type": "object",
"value": {"request": {"addressType": "fixedphysical", "address": 1001}}
});

console.log(JSON.stringify(fFieldbus));

} catch(err) {
console.error('Housten we are in trouble: ' + err);
} finally {
await ctrlx.logOut();
}

console.log('DONE!');

}




exports.benchmarkSimple = benchmarkSimple;
Expand All @@ -231,3 +260,4 @@ exports.benchmarkSubscriptionSimple = benchmarkSubscriptionSimple;
//benchmarkSimpleAsync();
//benchmarkRequestsPerSecond()
//benchmarkSubscriptionSimple();
//test();

0 comments on commit 17b03c7

Please sign in to comment.