Skip to content

Commit

Permalink
Changed the format of "lwm2m request" node's output message: (#44)
Browse files Browse the repository at this point in the history
* Changed the format of "lwm2m request" node's output message:
*Now the node outputs uuid, resource's path, status code and resource's value.

* Changed documentation of message structure
  • Loading branch information
mrNedas authored May 11, 2018
1 parent e72c33b commit cac8a50
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
26 changes: 15 additions & 11 deletions v1/lwm2m_request.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,27 @@
<p>If "read" is chosen variable type can also be choisen in which data will be represented.</p>
<p>Write request choise also adds input value type choise.</p>
<p>Input triggers the node to perform requests. It's value is equal to write request argument when performing write requests. For read and execute requests input message should not be empty or value should not be represented as "false".</p>
<p>Read output message structure:</p>
<p>Output message structure:</p>
<ul>
<li>msg.payload : <ul style="list-style-type:none">
<li>data: <ul style="list-style-type:none">
<li>uuid: <ul style="list-style-type:none">
<li>
<i>Value which was read</i>
<i>Endpoint's identification</i>
</li>
</ul></li>
</ul>
</li>
</ul>
<p>Write and execute output message structure:</p>
<ul>
<li>msg.payload : <ul style="list-style-type:none">
<li>code: <ul style="list-style-type:none">
<li>path: <ul style="list-style-type:none">
<li>
<i>LwM2M resource's path</i>
</li>
</ul></li>
<li>statusCode: <ul style="list-style-type:none">
<li>
<i>HTTP status code</i>
</li>
</ul></li>
<li>value: <ul style="list-style-type:none">
<li>
<i>Response status code</i>
<i>Value of the resource</i>
</li>
</ul></li>
</ul>
Expand Down
48 changes: 29 additions & 19 deletions v1/lwm2m_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ module.exports = function (RED) {
value: resourceValue,
});

node.device.write(node.resourcePath, (statusCode) => {
node.device.write(node.resourcePath, (statusCode, payload) => {
const msg = {};
msg.payload = {};
msg.payload.code = {};
msg.payload.code[node.resourcePath] = statusCode;
msg.payload.uuid = node.name;
msg.payload.path = node.resourcePath;
msg.payload.statusCode = statusCode;
msg.payload.value = payload;
node.send(msg);
}, tlvBuffer)
.catch((err) => {
Expand All @@ -108,8 +110,6 @@ module.exports = function (RED) {
node.device.read(node.resourcePath, (statusCode, payload) => {
const buffer = Buffer.from(payload, 'base64');
const msg = {};
msg.payload = {};
msg.payload.data = {};

switch (node.resourceType) {
case 'integer':
Expand All @@ -128,7 +128,11 @@ module.exports = function (RED) {
identifier: resourceIdentifier,
});

msg.payload.data[node.resourcePath] = decodedResource.value;
msg.payload = {};
msg.payload.uuid = node.name;
msg.payload.path = node.resourcePath;
msg.payload.statusCode = statusCode;
msg.payload.value = decodedResource.value;

node.send(msg);
}).catch((err) => {
Expand All @@ -148,19 +152,25 @@ module.exports = function (RED) {

case 'execute': {
node.on('input', () => {
node.device.execute(node.resourcePath, (statusCode) => {
const msg = {};
msg.payload = {};
msg.payload.code = {};
msg.payload.code[node.resourcePath] = statusCode;
node.send(msg);
}).catch((err) => {
if (typeof err === 'number') {
node.error(`Error code: ${err}`);
} else {
node.error(err);
}
});
if (node.resourcePath.split('/').length === 4) {
node.device.execute(node.resourcePath, (statusCode, payload) => {
const msg = {};
msg.payload = {};
msg.payload.uuid = node.name;
msg.payload.path = node.resourcePath;
msg.payload.statusCode = statusCode;
msg.payload.value = payload;
node.send(msg);
}).catch((err) => {
if (typeof err === 'number') {
node.error(`Error code: ${err}`);
} else {
node.error(err);
}
});
} else {
node.error('Invalid path to resource. Must be "/object/instance/resource", e.g., "/1/0/3".');
}
});

break;
Expand Down

0 comments on commit cac8a50

Please sign in to comment.