Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMahdi Ben Alaya2018-02-26 12:32:08 +0000
committerMahdi Ben Alaya2018-02-26 12:32:08 +0000
commit29c2f47e7614beab5d6658cc76173d01b93da5a8 (patch)
tree667087be0c87aea1f1ea569176ba821c32272d7f
parent7e02c052456cbd628a388519f7a2fb744deec252 (diff)
downloadorg.eclipse.om2m-29c2f47e7614beab5d6658cc76173d01b93da5a8.tar.gz
org.eclipse.om2m-29c2f47e7614beab5d6658cc76173d01b93da5a8.tar.xz
org.eclipse.om2m-29c2f47e7614beab5d6658cc76173d01b93da5a8.zip
Add oneM2M javascript client samples
-rw-r--r--org.eclipse.om2m.client.js/onem2m-actuator.js198
-rw-r--r--org.eclipse.om2m.client.js/onem2m-monitor-async.js163
-rw-r--r--org.eclipse.om2m.client.js/onem2m-monitor-sync.js138
-rw-r--r--org.eclipse.om2m.client.js/onem2m-sensor.js136
4 files changed, 635 insertions, 0 deletions
diff --git a/org.eclipse.om2m.client.js/onem2m-actuator.js b/org.eclipse.om2m.client.js/onem2m-actuator.js
new file mode 100644
index 00000000..a00b0671
--- /dev/null
+++ b/org.eclipse.om2m.client.js/onem2m-actuator.js
@@ -0,0 +1,198 @@
+/*******************************************************************************
+ * Copyright (c) 2016- 2017 SENSINOV (www.sensinov.com)
+ * 41 Rue de la découverte 31676 Labège - France
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+var express = require('express');
+var path = require('path');
+var bodyParser = require('body-parser');
+var request = require('request');
+var app = express();
+app.use(bodyParser.json());
+
+app.listen(4000, function () {
+ console.log('AE Actuator listening on port 4000!');
+});
+
+app.post('/', function (req, res) {
+ console.log("\n◀◀◀◀◀")
+ console.log(req.body);
+ var content = req.body["m2m:sgn"].nev.rep["m2m:cin"].con;
+ console.log("Actuator switched to "+content);
+ res.sendStatus(200);
+});
+
+createAE();
+function createAE(){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-actuator";
+ var method = "POST";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name";
+ var resourceType=2;
+ var requestId = "123456";
+ var representation = {
+ "m2m:ae":{
+ "rn":"myactuator",
+ "api":"app.company.com",
+ "rr":"true",
+ "poa":["http://127.0.0.1:4000/"]
+ }
+ };
+
+ console.log(method+" "+uri);
+ console.log(representation);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json;ty="+resourceType
+ },
+ json: representation
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+ createContainer();
+ }
+ });
+}
+
+
+function createContainer(){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-actuator";
+ var method = "POST";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name/myactuator";
+ var resourceType=3;
+ var requestId = "123456";
+ var representation = {
+ "m2m:cnt":{
+ "rn":"switch",
+ "mni":100
+
+ }
+ };
+
+ console.log(method+" "+uri);
+ console.log(representation);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json;ty="+resourceType
+ },
+ json: representation
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+ createContentInstance();
+ }
+ });
+}
+
+
+function createContentInstance(){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-actuator";
+ var method = "POST";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name/myactuator/switch";
+ var resourceType=4;
+ var requestId = "123456";
+ var representation = {
+ "m2m:cin":{
+ "con": false
+ }
+ };
+
+ console.log(method+" "+uri);
+ console.log(representation);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json;ty="+resourceType
+ },
+ json: representation
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+ }
+ });
+}
+
+
+function createSubscription(){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-monitor-async";
+ var method = "POST";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name/mysensor/luminosity";
+ var resourceType=23;
+ var requestId = "123456";
+ var representation = {
+ "m2m:sub": {
+ "rn": "subTest",
+ "nu": ["/server/Cae-actuator"],
+ "nct": 2,
+ "enc": {
+ "net": 3
+ }
+ }
+ };
+
+ console.log(method+" "+uri);
+ console.log(representation);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json;ty="+resourceType
+ },
+ json: representation
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+ }
+ });
+}
+
+
diff --git a/org.eclipse.om2m.client.js/onem2m-monitor-async.js b/org.eclipse.om2m.client.js/onem2m-monitor-async.js
new file mode 100644
index 00000000..f2fce5f4
--- /dev/null
+++ b/org.eclipse.om2m.client.js/onem2m-monitor-async.js
@@ -0,0 +1,163 @@
+/*******************************************************************************
+ * Copyright (c) 2016- 2017 SENSINOV (www.sensinov.com)
+ * 41 Rue de la découverte 31676 Labège - France
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+var express = require('express');
+var path = require('path');
+var bodyParser = require('body-parser');
+var request = require('request');
+var app = express();
+app.use(bodyParser.json());
+
+app.listen(3000, function () {
+ console.log('AE Monitor listening on port 3000!');
+});
+
+app.post('/', function (req, res) {
+ console.log("\n◀◀◀◀◀")
+ console.log(req.body);
+
+ var content = req.body["m2m:sgn"].nev.rep["m2m:cin"].con;
+ console.log("Receieved luminosity: "+content);
+ if(content>5){
+ console.log("High luminosity => Switch lamp OFF");
+ createContenInstance(false);
+ }else{
+ console.log("Low luminosity => Switch lamp ON");
+ createContenInstance(true)
+ }
+ res.sendStatus(200);
+});
+
+createAE();
+function createAE(){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-monitor-async";
+ var method = "POST";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name";
+ var resourceType=2;
+ var requestId = "123456";
+ var representation = {
+ "m2m:ae":{
+ "rn":"mymonitor-async",
+ "api":"app.company.com",
+ "rr":"true",
+ "poa":["http://127.0.0.1:3000/"]
+ }
+ };
+
+ console.log(method+" "+uri);
+ console.log(representation);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json;ty="+resourceType
+ },
+ json: representation
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+ createSubscription();
+ }
+ });
+}
+
+
+function createSubscription(){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-monitor-async";
+ var method = "POST";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name/mysensor/luminosity";
+ var resourceType=23;
+ var requestId = "123456";
+ var representation = {
+ "m2m:sub": {
+ "rn": "subMonitor",
+ "nu": ["/server/Cae-monitor-async"],
+ "nct": 2,
+ "enc": {
+ "net": 3
+ }
+ }
+ };
+
+ console.log(method+" "+uri);
+ console.log(representation);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json;ty="+resourceType
+ },
+ json: representation
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+ }
+ });
+}
+
+function createContenInstance(value){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-monitor-async";
+ var method = "POST";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name/myactuator/switch";
+ var resourceType=4;
+ var requestId = "123456";
+ var representation = {
+ "m2m:cin":{
+ "con": value
+ }
+ };
+
+ console.log(method+" "+uri);
+ console.log(representation);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json;ty="+resourceType
+ },
+ json: representation
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+ }
+ });
+}
+
+
diff --git a/org.eclipse.om2m.client.js/onem2m-monitor-sync.js b/org.eclipse.om2m.client.js/onem2m-monitor-sync.js
new file mode 100644
index 00000000..cae557c2
--- /dev/null
+++ b/org.eclipse.om2m.client.js/onem2m-monitor-sync.js
@@ -0,0 +1,138 @@
+/*******************************************************************************
+ * Copyright (c) 2016- 2017 SENSINOV (www.sensinov.com)
+ * 41 Rue de la découverte 31676 Labège - France
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+var bodyParser = require('body-parser');
+var request = require('request');
+
+createAE();
+
+function createAE(){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-monitor-sync";
+ var method = "POST";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name";
+ var resourceType=2;
+ var requestId = "123456";
+ var representation = {
+ "m2m:ae":{
+ "rn":"mymonitor-sync",
+ "api":"app.company.com",
+ "rr":"false"
+ }
+ };
+
+ console.log(method+" "+uri);
+ console.log(representation);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json;ty="+resourceType
+ },
+ json: representation
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+
+ setInterval(function() {
+ retrieveContentInstance();
+ }, 5000);
+ }
+ });
+}
+
+
+function retrieveContentInstance(){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-monitor-sync";
+ var method = "GET";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name/mysensor/luminosity/la";
+ var requestId = "123456";
+
+ console.log(method+" "+uri);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json"
+ }
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+ var jsonBody = JSON.parse(body);
+ var value = jsonBody["m2m:cin"].con;
+ console.log("Receieved luminosity: "+value);
+
+ if(value>5){
+ console.log("High luminosity => Switch lamp OFF");
+ createContentInstance(false);
+ }else{
+ console.log("Low luminosity => Switch lamp ON");
+ createContentInstance(true);
+ }
+ }
+ });
+}
+
+function createContentInstance(value){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-monitor-sync";
+ var method = "POST";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name/myactuator/switch";
+ var resourceType=4;
+ var requestId = "123456";
+ var representation = {
+ "m2m:cin":{
+ "con": value
+ }
+ };
+
+ console.log(method+" "+uri);
+ console.log(representation);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json;ty="+resourceType
+ },
+ json: representation
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+ }
+ });
+}
diff --git a/org.eclipse.om2m.client.js/onem2m-sensor.js b/org.eclipse.om2m.client.js/onem2m-sensor.js
new file mode 100644
index 00000000..f846e4c3
--- /dev/null
+++ b/org.eclipse.om2m.client.js/onem2m-sensor.js
@@ -0,0 +1,136 @@
+/*******************************************************************************
+ * Copyright (c) 2016- 2017 SENSINOV (www.sensinov.com)
+ * 41 Rue de la découverte 31676 Labège - France
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+var express = require('express');
+var request = require('request');
+
+createAE();
+function createAE(){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-sensor";
+ var method = "POST";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name";
+ var resourceType=2;
+ var requestId = "123456";
+ var representation = {
+ "m2m:ae":{
+ "rn":"mysensor",
+ "api":"app.company.com",
+ "rr":"false"
+ }
+ };
+
+ console.log(method+" "+uri);
+ console.log(representation);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json;ty="+resourceType
+ },
+ json: representation
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+ createContainer();
+ }
+ });
+}
+
+
+function createContainer(){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-sensor";
+ var method = "POST";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name/mysensor";
+ var resourceType=3;
+ var requestId = "123456";
+ var representation = {
+ "m2m:cnt":{
+ "rn":"luminosity",
+ "mni":100
+
+ }
+ };
+
+ console.log(method+" "+uri);
+ console.log(representation);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json;ty="+resourceType
+ },
+ json: representation
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+ setInterval(function() {
+ createContentInstance();
+ }, 5000);
+ }
+ });
+}
+
+function createContentInstance(){
+ console.log("\n▶▶▶▶▶");
+ var originator = "Cae-sensor";
+ var method = "POST";
+ var uri= "http://127.0.0.1:8080/~/in-cse/in-name/mysensor/luminosity";
+ var resourceType=4;
+ var requestId = "123456";
+ var representation = {
+ "m2m:cin":{
+ "con": Math.floor(Math.random()*10)
+ }
+ };
+
+ console.log(method+" "+uri);
+ console.log(representation);
+
+ var options = {
+ uri: uri,
+ method: method,
+ headers: {
+ "X-M2M-Origin": originator,
+ "X-M2M-RI": requestId,
+ "Content-Type": "application/json;ty="+resourceType
+ },
+ json: representation
+ };
+
+ request(options, function (error, response, body) {
+ console.log("◀◀◀◀◀");
+ if(error){
+ console.log(error);
+ }else{
+ console.log(response.statusCode);
+ console.log(body);
+ }
+ });
+}

Back to the top