I have to develop a node.js app which can interact with another oneM2M server. In particular the node.js app need to exchange data using CoaP protocol, but I don't know how to do that in a oneM2M compliant manner.
I started my node.js project using this module: https://github.com/mcollina/node-coap but I need some support to create a CoaP client using oneM2M protocol.
Code samples are really appreciated.
According to TS-0008: CoAP Protocol Binding in chapter 6.2.1 Header there is the following oneM2M operations mapping with CoAP methods:
Operation CoAP Method
CREATE POST
RETRIEVE GET
UPDATE PUT
DELETE DELETE
NOTIFY POST
Then in chapter 6.2.2.4 Definition of New Options a new set of CoAP options is introduced, mapping oneM2M header parameters. Here the main HTTP header variables are listed with CoAP option equivalents:
Header HTTP variable CoAP Option
X-M2M-Origin 256
X-M2M-RI 257
oneM2M-TY 267
So here is a minimal node.js script to do a GET operation, that is to retrieve latest contentInstance in the container resource with path /<cseBase>/<AE>/<Container>
:
var coap = require('coap');
var options = {
host : '<hostname>',
port : 5683,
pathname : "/<cseBase>/<AE>/<Container>/la",
method : 'get',
confirmable : 'true',
options : {
'Accept' : 'application/json'
}
};
var bodyString = ''; // void string for GET operation
var responseBody = '';
var req = coap.request(options);
req.setOption("256", new Buffer("<origin>")); // X-M2M-Origin (mandatory)
req.setOption("257", new Buffer('123456')); // X-M2M-RI (mandatory)
req.on('response', function (res) {
res.on('data', function () {
responseBody += res.payload.toString();
});
res.on('end', function () {
if (res.code == '2.05') {
console.log('[coap] coap ready, request OK');
var obj = JSON.parse(responseBody);
console.log('[coap] responseBody', obj);
} else {
console.log('[coap] coap res.code='+res.code);
}
});
});
req.write(bodyString);
req.end();
And this is a minimal POST operation example, that is to create a contentInstance in the container resource with path /<cseBase>/<AE>/<Container>
:
var coap = require('coap');
var options = {
host : '<hostname>',
port : 5683,
pathname : "/<cseBase>/<AE>/<Container>",
method : 'post',
confirmable : 'true',
options : {
'Content-Format' : 'application/json'
}
};
var bodyString = new Buffer('{"m2m:cin":{ "con":{"temperature":33}}}');
var responseBody = '';
var req = coap.request(options);
req.setOption("256", new Buffer("<origin>")); // X-M2M-Origin (mandatory)
req.setOption("257", new Buffer('123456')); // X-M2M-RI (mandatory)
req.setOption("267", new Buffer([4])); // ty = 4, ContentInstance resource type
req.on('response', function (res) {
res.on('data', function () {
responseBody += res.payload.toString();
});
res.on('end', function () {
if (res.code == '2.05') {
console.log('[coap] coap ready, request OK');
var obj = JSON.parse(responseBody);
console.log('[coap] responseBody', obj);
} else {
console.log('[coap] coap res.code='+res.code);
}
});
});
req.write(bodyString);
req.end();