From ec34d01f15c751307a860280a0ca065c34a35725 Mon Sep 17 00:00:00 2001 From: Gabrielle Poerwawinata Date: Mon, 26 Nov 2018 13:07:55 +0100 Subject: [PATCH 01/14] MQTT integration Enabling MQTT feature on oisp-sdk. Sending data is our current working function. Actuation will be developed soon. Addition files on api and lib directory. Mqtt connector was taken from oisp-mqtt-gw. This feature uses mqtt version 2.13.1. Signed-off-by: Gabrielle Poerwawinata --- api/index.js | 1 + api/mqtt/connector.js | 318 ++++++++++++++++++++++++++++++++ api/mqtt/index.js | 28 +++ lib/proxies/iot.control.mqtt.js | 58 ++++++ lib/proxies/iot.mqtt.js | 110 +++++++++++ package.json | 1 + 6 files changed, 516 insertions(+) create mode 100644 api/mqtt/connector.js create mode 100644 api/mqtt/index.js create mode 100644 lib/proxies/iot.control.mqtt.js create mode 100644 lib/proxies/iot.mqtt.js diff --git a/api/index.js b/api/index.js index 2e29eb0..fc0d691 100644 --- a/api/index.js +++ b/api/index.js @@ -28,6 +28,7 @@ module.exports = function(config) { module.rest = require('./rest')(config); module.ws = require('./ws'); + module.mqtt = require('./mqtt'); return module; }; diff --git a/api/mqtt/connector.js b/api/mqtt/connector.js new file mode 100644 index 0000000..13d8d87 --- /dev/null +++ b/api/mqtt/connector.js @@ -0,0 +1,318 @@ +/** +* Copyright (c) 2017 Intel Corporation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +"use strict"; +var mqtt = require('mqtt'); +var events = require('events'); +var errBuilder = require('../errorHandler/errorHandler').errBuilder; +var fs = require('fs') +function Broker(conf, logger) { + var me = this; + me.host = conf.host; + me.port = conf.port; + me.key = conf.key; + me.cert = conf.cert; + me.ca = conf.ca; + me.secure = conf.secure; + me.keepalive = conf.keepalive || 60; + me.crd = { + username: conf.username, + password: conf.password + }; + me.max_retries = conf.retries || 30; + me.messageHandler = []; + me.logger = logger; + me.topics = conf.topics; + me.pubArgs = { + qos: conf.qos || 1, + retain: conf.retain + }; + me.iamHealthyTopic = "server/{hash}/healthy"; + me.isLive = false; + me.pingActivate = true; + me.client = { + connected: false, + end: function() {} + }; + + function buildPath (path, data) { + var re = /{\w+}/; + var pathReplace = path; + if (Array.isArray(data)) { + data.forEach(function (value) { + pathReplace = pathReplace.replace(re, value); + }); + } else { + pathReplace = pathReplace.replace(re, data); + } + return pathReplace; + } + + me.setCredential = function (newCrd) { + me.crd = newCrd || me.crd; + me.credential = { + username: me.crd.username, + password: me.crd.password, + keepalive: me.keepalive + }; + }; + me.setCredential(); + me.checkConnection = function () { + var me = this; + var retries = 0; + var hash = new Date().getTime(); + var healthyTopic = buildPath(me.iamHealthyTopic, hash); + var receivingPing = function (topic, message) { + me.logger.info("receivingPing ", message); + me.isLive = true; + }; + var sendPing = function () { + retries = 0; + me.isLive = false; + waitForPong(); + var da = {'ping': new Date().getTime()}; + me.publish(healthyTopic, da); + }; + var waitForPong = function () { + if (!me.isLive) { + retries++; + me.logger.debug("Waiting for Pong # " + retries); + if (retries < 10) { + setTimeout(waitForPong, 1500); + } else { + me.logger.error('Pong Not Achieved, Require re Attach Listeners'); + me.unbind(healthyTopic); + me.emit("reconnect"); + } + return false; + } + me.logger.info('Pong Received'); + setTimeout(sendPing, 30000); + return true; + }; + function bindingPing () { + me.bind(healthyTopic, receivingPing, function () { + sendPing(); + }); + } + me.on('reconnect', function() { + me.logger.debug('Restarting Pinging '); + bindingPing(); + }); + bindingPing(); + return true; + }; + me.listen = function () { + me.client.on('message',function(topic, message) { + try { + message = JSON.parse(message); + } catch (e) { + me.logger.error('Invalid Message: %s', e); + return; + } + me.logger.info('STATUS: %s', topic, message); + me.onMessage(topic, message); + }); + if (me.pingActivate) { + me.checkConnection(); + } + }; + me.connect = function (done) { + var retries = 0; + try { + if ((me.client instanceof mqtt.MqttClient) === false) { + if (me.secure === false) { + me.logger.info("Non Secure Connection to "+ me.host + ":" + me.port); + //me.client = mqtt.createClient(me.port, me.host, me.credential); + me.client = mqtt.connect('mqtt://' + me.host + ':' + me.port, { + username: me.crd.username, + password: me.crd.password + }); + } else { + me.logger.debug("Trying with Secure Connection to" + me.host + ":" + me.port); + + me.client = mqtt.connect('mqtts://' + me.host + ":" + me.port, { + username: me.crd.username, + password: me.crd.password, + ca : fs.readFileSync(conf.ca), + rejectUnauthorized: false + }); + + me.client.on('error', function(err) { + console.log(err); + }); + //me.client = mqtt.createSecureClient(PORT, options); + + //me.client = mqtt.createSecureClient(me.port, me.host, options) + //me.client = mqtt.connect(options); + } + } + } catch(e) { + console.log(e) + logger.error(e) + logger.error("Error in connection ex: ", e); + done(errBuilder.build(errBuilder.Errors.Generic.Connection.Timeout)); + return; + } + function waitForConnection() { + if (!me.client.connected) { + retries++; + me.logger.info("Waiting for MQTTConnector to connect # " + retries); + if (retries < me.max_retries) { + setTimeout(waitForConnection, 1500); + } else { + me.logger.info('MQTTConnector: Error Connecting to '+ me.host + ':' + me.port); + done(errBuilder.build(errBuilder.Errors.Generic.Connection.MaxTriesReached)); + } + return false; + } + me.logger.info('MQTTConnector: Connection successful to ' + me.host + ':' + me.port); + me.listen(); + if (done) { + done(null); + } + return true; + } + waitForConnection(); + }; + me.disconnect = function () { + me.logger.info("Trying to disconnect "); + me.client.end(); + me.client = { + connected: false, + end: function() {} + }; + }; + me.attach = function (topic, handler) { + me.messageHandler.push({"t": topic, + "h": handler}); + }; + function tryPattern(pattern, text) { + var a = new RegExp(pattern); + return a.test(text); + } + me.dettach = function (topic) { + me.logger.debug('Filtering Topics ' + topic + ' from local dispatcher'); + me.messageHandler = me.messageHandler.filter(function (obj) { + return !tryPattern(obj.t, topic); + }); + }; + me.onMessage = function (topic, message) { + var i, + length = me.messageHandler.length; + /** + * Iterate over the messageHandler to match topic patter, + * and dispatch message to only proper handler + */ + for (i = 0; i < length; i++ ){ + var obj = me.messageHandler[i]; + if (tryPattern(obj.t, topic)) { + me.logger.info('Fired STATUS: ' + topic + JSON.stringify(message)); + obj.h(topic, message); + } + } + }; + me.bind = function (topic, handler, callback) { + /** + * since the bind and publish connect automatically, + * it is require to chain the callbacks + */ + var toCallBack = callback; + function connectCallback() { + me.logger.debug('Subscribing to: ' + topic); + me.client.subscribe(topic, {qos: 1}, function (err, granted) { + me.logger.debug("grant " + JSON.stringify(granted)); + var topicAsPattern = granted[0].topic.replace(/\+/g, "[^<>]*"); + me.logger.info("Topic Pattern :" + topicAsPattern); + me.attach(topicAsPattern, handler); + if (toCallBack) { + toCallBack(); + } + }); + } + if (!me.connected()) { + me.connect(function(err) { + if (!err) { + connectCallback(); + } else { + me.logger.error(err); + if (toCallBack) { + toCallBack(err); + } + } + }); + } else { + connectCallback(); + } + }; + me.unbind = function (topic, callback) { + me.logger.debug('Unbinding from Topic ' + topic); + me.client.unsubscribe(topic, function() { + me.logger.info('Unbound (UNSUBACK) of topic ' + topic); + }); + me.logger.debug('Filtering Topics ' + topic + ' from local dispatcher'); + me.dettach(topic); + if (callback) { + callback(); + } + }; + /** + * @description publish broadcast to an specifics topics, the message. + * @param topic to which will be broadcast the message. + * @param message that will be sent to topics + * @param args + */ + me.publish = function (topic, message, options, callback) { + if ("function" === typeof options) { + callback = options; + options = me.pubArgs; + } else { + options = options || me.pubArgs; + } + function publishCallback() { + me.logger.debug('Publishing : T => ' + topic + " MSG => " + JSON.stringify(message)); + me.client.publish(topic, JSON.stringify(message), options, callback); + } + if (!me.connected()) { + me.connect(function(err) { + if (!err) { + publishCallback(); + } else { + me.logger.error("The connectio has faild on publish " + err); + if (callback) { + callback(err); + } + } + }); + } else { + publishCallback(); + } + }; + me.connected = function () { + return me.client.connected; + }; +} + +Broker.prototype = new events.EventEmitter(); + +var broker = null; +module.exports.singleton = function (conf, logger) { + if (!broker) { + broker = new Broker(conf, logger); + } + return broker; +}; +module.exports.Broker = Broker; \ No newline at end of file diff --git a/api/mqtt/index.js b/api/mqtt/index.js new file mode 100644 index 0000000..5fbbe6b --- /dev/null +++ b/api/mqtt/index.js @@ -0,0 +1,28 @@ +/* +Copyright (c) 2014, Intel Corporation + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +"use strict"; +module.exports = { + connector: require('./connector') +}; diff --git a/lib/proxies/iot.control.mqtt.js b/lib/proxies/iot.control.mqtt.js new file mode 100644 index 0000000..3b39c11 --- /dev/null +++ b/lib/proxies/iot.control.mqtt.js @@ -0,0 +1,58 @@ +/* +Copyright (c) 2014, Intel Corporation + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +"use strict"; +var common = require('../common'), + Broker = require("../../api/mqtt/connector"); + + + +function IoTKitMQTTControl(conf) { + var me = this; + //me.logger = logger; + me.client = broker; + me.type = 'mqtt'; + me.topics = conf.connector[me.type].topic; + me.pubArgs = { + qos: 0, + retain: false + }; + //me.logger.debug(me.type.toUpperCase(), 'Control Proxy Created'); +} + +IoTKitMQTTControl.prototype.controlCommandListen = function (data, handlerCb, syncCall) { + var me = this; + var controlTopic = common.buildPath(me.topics.control_command, data.deviceId); + var handler = function (topic, message) { + //me.logger.debug('controlCommandListen Topic %s , Message Recv : %s', topic, message); + handlerCb(message); + }; + return me.client.bind(controlTopic, handler, syncCall); +}; + + +module.exports.init = function(conf, logger) { + var brokerConnector = Broker.singleton(conf.connector.mqtt, logger); + return new IoTKitMQTTControl(conf, logger, brokerConnector); +}; diff --git a/lib/proxies/iot.mqtt.js b/lib/proxies/iot.mqtt.js new file mode 100644 index 0000000..6ac0f75 --- /dev/null +++ b/lib/proxies/iot.mqtt.js @@ -0,0 +1,110 @@ +/* +Copyright (c) 2014, Intel Corporation + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +"use strict"; +var common = require('../common'), + Broker = require("../../api/mqtt/connector"); + +function IoTKitMQTTCloud(conf, broker) { + + var me = this; + me.client = broker; + me.type = 'mqtt'; + me.topics = conf.connector[me.type].topic; + me.pubArgs = { + qos: 1, + retain: false + }; + console.log("logger", me.logger) + +} + +IoTKitMQTTCloud.prototype.pullActuations = function (data, callback) { + var me = this; + //me.logger.error("Actuations pulling is not yet supported by MQTT protocol"); + callback(null); +}; + +IoTKitMQTTCloud.prototype.data = function (data, callback) { + var me = this; + delete data.deviceToken; + var topic = common.buildPath(me.topics.metric_topic, [data.accountId, data.did]); + //me.logger.debug("Metric doc: %j", data, {}); + delete data.gatewayId; + return me.client.publish(topic, data.convertToMQTTPayload(), me.pubArgs, function() { + return callback({status:0}); + }); +}; + +IoTKitMQTTCloud.prototype.disconnect = function () { + var me = this; + me.client.disconnect(); +}; + +IoTKitMQTTCloud.prototype.healthResponse = function (device, callback, syncCallback) { + var me = this; + var healthStatus = common.buildPath(me.topics.health_status, device); + var handler = function (topic, message) { + //me.logger.debug('Topic %s , Message Recv : %s', topic, message); + me.client.unbind(healthStatus); + callback(message); + }; + me.client.bind(healthStatus, handler, syncCallback); +}; + +IoTKitMQTTCloud.prototype.health = function (device, callback) { + var me = this; + //me.logger.info("Starting Health testing "); + me.healthResponse(device, callback, function (err) { + if (!err) { + var topic = common.buildPath(me.topics.health, device); + var data = { 'detail': 'mqtt'}; + me.client.publish(topic, data, me.pubArgs); + } else { + callback(); + } + + }); + +}; + +IoTKitMQTTCloud.prototype.setCredential = function (user, password) { + var me = this; + me.crd = { + username: user || '', + password: password || '' + }; + + me.client.setCredential(me.crd); +}; + +IoTKitMQTTCloud.prototype.getActualTime = function (callback) { + var me = this; + //me.logger.error('This option is not currently supported for MQTT protocol.'); + callback(null); +}; + +module.exports.init = function(conf) { + var brokerConnector = Broker.singleton(conf.connector.mqtt); + return new IoTKitMQTTCloud(conf, brokerConnector); +}; diff --git a/package.json b/package.json index 2ba10c6..64be13e 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "async": "^2.5.0", "request": "^2.83.0", "tunnel": "0.0.5", + "mqtt" : "^2.13.1", "websocket": "^1.0.24" } } From d2eaeffa3f02e118cbed74a75d4057d997ed0b8f Mon Sep 17 00:00:00 2001 From: Jamal El Youssefi Date: Mon, 4 Mar 2019 12:56:52 +0100 Subject: [PATCH 02/14] Enabling the binary encoding. - If the transmitted data contains a binary object it's cbor encoded and and the application/cbor content type is used. - Added cbor npm package into package.json file Signed-off-by: Jamal El Youssefi Signed-off-by: Marcel Wagner --- api/rest/data.def.js | 12 +++++++++++- api/rest/devices.def.js | 15 +++++++++++++-- lib/common.js | 16 ++++++++++++++++ lib/httpClient.js | 7 +++++++ package.json | 3 ++- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/api/rest/data.def.js b/api/rest/data.def.js index 3b1471c..68313c7 100644 --- a/api/rest/data.def.js +++ b/api/rest/data.def.js @@ -24,6 +24,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; +var cbor = require('cbor'); + module.exports = function(config) { var common = require('../../lib/common'); var api = require('./api'); @@ -32,11 +34,18 @@ module.exports = function(config) { var module = {}; function SubmitDataOption(data) { + var isBinary = common.isBinary(data.body); this.pathname = common.buildPath(api.data.SEND, data.deviceId); this.token = data.userToken; ConnectionOptions.call(this); this.method = 'POST'; - this.body = JSON.stringify(data.body); + if ( isBinary ) { + this.body = cbor.encode(data.body); + this.headers["Content-type"] = "application/cbor"; + } else { + this.body = JSON.stringify(data.body); + this.headers["Content-type"] = "application/json"; + } } SubmitDataOption.prototype = new ConnectionOptions(); SubmitDataOption.prototype.constructor = SubmitDataOption; @@ -48,6 +57,7 @@ module.exports = function(config) { this.token = data.userToken; ConnectionOptions.call(this); this.method = 'POST'; + this.encoding = null; this.body = JSON.stringify(data.body); } SearchDataOption.prototype = new ConnectionOptions(); diff --git a/api/rest/devices.def.js b/api/rest/devices.def.js index 15bca88..4f94d9c 100644 --- a/api/rest/devices.def.js +++ b/api/rest/devices.def.js @@ -24,6 +24,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; +var cbor = require('cbor'); + module.exports = function(config) { var common = require('../../lib/common'); var api = require('./api'); @@ -185,18 +187,27 @@ module.exports = function(config) { * @constructor */ function DeviceSubmitDataOption (data) { + var isBinary = common.isBinary(data.body); this.pathname = common.buildPath(api.submit.DATA, data.deviceId); ConnectionOptions.call(this); + var contentType = "application/json"; + if ( isBinary == true ) { + contentType = "application/cbor"; + } this.method = 'POST'; this.headers = { - "Content-type" : "application/json", + "Content-type" : contentType, "Authorization" : "Bearer " + data.deviceToken }; if (data.forwarded) { this.headers["forwarded"] = true; delete data.forwarded; } - this.body = JSON.stringify(data.body); + if ( isBinary ) { + this.body = cbor.encode(data.body); + } else { + this.body = JSON.stringify(data.body); + } } DeviceSubmitDataOption.prototype = new ConnectionOptions(); DeviceSubmitDataOption.prototype.constructor = DeviceSubmitDataOption; diff --git a/lib/common.js b/lib/common.js index fa9803f..f32fb00 100644 --- a/lib/common.js +++ b/lib/common.js @@ -49,3 +49,19 @@ module.exports.isAbsolutePath = function(location) { return path.resolve(location) === path.normalize(location); }; +module.exports.isBinary = function(object) { + if ( Buffer.isBuffer(object) ) { + return true; + } + var keys = Object.keys(object); + for(var index in keys) { + var key = keys[index]; + if(typeof object[key] == 'object') { + if( this.isBinary(object[key]) ) { + return true; + } + } + } + return false; +}; + diff --git a/lib/httpClient.js b/lib/httpClient.js index 9663033..78d52c1 100755 --- a/lib/httpClient.js +++ b/lib/httpClient.js @@ -31,6 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * HTTPS request */ var request = require('request'); +var cbor = require('cbor'); function processResponse(res, body, callback) { var data = null; @@ -42,6 +43,12 @@ function processResponse(res, body, callback) { } catch (e) { data = null; } + } else if (res.headers['content-type'] && res.headers['content-type'].indexOf('application/cbor') > -1) { + try { + data = cbor.decode(body); + } catch (e) { + data = null; + } } else { data = null; } diff --git a/package.json b/package.json index 64be13e..6ba28c0 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "grunt-mocha-istanbul": "^5.0.2", "istanbul": "^0.4.5", "mocha": "^4.0.1", - "rewire": "^2.5.2" + "rewire": "^2.5.2", + "cbor": "^4.1.5" }, "dependencies": { "async": "^2.5.0", From 15e53c93f3a6589682a84c22f1ed4da552c9a084 Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Thu, 14 Mar 2019 15:52:29 +0000 Subject: [PATCH 03/14] MQTT: Bugfixes and formatting fixes Removed logging from SDK as this should be handled in application code. Use more simple error handling in callbacks until a better solution is found. Fix formating issues so tests pass Signed-off-by: Scott Ware --- api/mqtt/connector.js | 101 ++++++++------------------------ lib/proxies/iot.control.mqtt.js | 11 ++-- lib/proxies/iot.mqtt.js | 13 +--- 3 files changed, 32 insertions(+), 93 deletions(-) diff --git a/api/mqtt/connector.js b/api/mqtt/connector.js index 13d8d87..1c27b1a 100644 --- a/api/mqtt/connector.js +++ b/api/mqtt/connector.js @@ -17,9 +17,10 @@ "use strict"; var mqtt = require('mqtt'); var events = require('events'); -var errBuilder = require('../errorHandler/errorHandler').errBuilder; var fs = require('fs') -function Broker(conf, logger) { +var common = require('../../lib/common'); + +function Broker(conf) { var me = this; me.host = conf.host; me.port = conf.port; @@ -34,7 +35,6 @@ function Broker(conf, logger) { }; me.max_retries = conf.retries || 30; me.messageHandler = []; - me.logger = logger; me.topics = conf.topics; me.pubArgs = { qos: conf.qos || 1, @@ -47,20 +47,6 @@ function Broker(conf, logger) { connected: false, end: function() {} }; - - function buildPath (path, data) { - var re = /{\w+}/; - var pathReplace = path; - if (Array.isArray(data)) { - data.forEach(function (value) { - pathReplace = pathReplace.replace(re, value); - }); - } else { - pathReplace = pathReplace.replace(re, data); - } - return pathReplace; - } - me.setCredential = function (newCrd) { me.crd = newCrd || me.crd; me.credential = { @@ -74,9 +60,8 @@ function Broker(conf, logger) { var me = this; var retries = 0; var hash = new Date().getTime(); - var healthyTopic = buildPath(me.iamHealthyTopic, hash); - var receivingPing = function (topic, message) { - me.logger.info("receivingPing ", message); + var healthyTopic = common.buildPath(me.iamHealthyTopic, hash); + var receivingPing = function () { me.isLive = true; }; var sendPing = function () { @@ -89,17 +74,14 @@ function Broker(conf, logger) { var waitForPong = function () { if (!me.isLive) { retries++; - me.logger.debug("Waiting for Pong # " + retries); if (retries < 10) { setTimeout(waitForPong, 1500); } else { - me.logger.error('Pong Not Achieved, Require re Attach Listeners'); me.unbind(healthyTopic); me.emit("reconnect"); } return false; } - me.logger.info('Pong Received'); setTimeout(sendPing, 30000); return true; }; @@ -109,7 +91,6 @@ function Broker(conf, logger) { }); } me.on('reconnect', function() { - me.logger.debug('Restarting Pinging '); bindingPing(); }); bindingPing(); @@ -120,10 +101,9 @@ function Broker(conf, logger) { try { message = JSON.parse(message); } catch (e) { - me.logger.error('Invalid Message: %s', e); return; } - me.logger.info('STATUS: %s', topic, message); + me.onMessage(topic, message); }); if (me.pingActivate) { @@ -133,53 +113,36 @@ function Broker(conf, logger) { me.connect = function (done) { var retries = 0; try { - if ((me.client instanceof mqtt.MqttClient) === false) { - if (me.secure === false) { - me.logger.info("Non Secure Connection to "+ me.host + ":" + me.port); - //me.client = mqtt.createClient(me.port, me.host, me.credential); - me.client = mqtt.connect('mqtt://' + me.host + ':' + me.port, { + if ((me.client instanceof mqtt.MqttClient) === false) { + if (me.secure === false) { + me.client = mqtt.connect('mqtt://' + me.host + ':' + me.port, { username: me.crd.username, password: me.crd.password }); - } else { - me.logger.debug("Trying with Secure Connection to" + me.host + ":" + me.port); - + } else { me.client = mqtt.connect('mqtts://' + me.host + ":" + me.port, { username: me.crd.username, password: me.crd.password, ca : fs.readFileSync(conf.ca), rejectUnauthorized: false }); - - me.client.on('error', function(err) { - console.log(err); - }); - //me.client = mqtt.createSecureClient(PORT, options); - - //me.client = mqtt.createSecureClient(me.port, me.host, options) - //me.client = mqtt.connect(options); } } } catch(e) { - console.log(e) - logger.error(e) - logger.error("Error in connection ex: ", e); - done(errBuilder.build(errBuilder.Errors.Generic.Connection.Timeout)); + done(new Error("Connection Error", 1002)); return; } function waitForConnection() { if (!me.client.connected) { retries++; - me.logger.info("Waiting for MQTTConnector to connect # " + retries); if (retries < me.max_retries) { setTimeout(waitForConnection, 1500); } else { - me.logger.info('MQTTConnector: Error Connecting to '+ me.host + ':' + me.port); - done(errBuilder.build(errBuilder.Errors.Generic.Connection.MaxTriesReached)); + done(new Error("Connection Error", 1001)); } return false; } - me.logger.info('MQTTConnector: Connection successful to ' + me.host + ':' + me.port); + me.listen(); if (done) { done(null); @@ -189,7 +152,6 @@ function Broker(conf, logger) { waitForConnection(); }; me.disconnect = function () { - me.logger.info("Trying to disconnect "); me.client.end(); me.client = { connected: false, @@ -205,10 +167,9 @@ function Broker(conf, logger) { return a.test(text); } me.dettach = function (topic) { - me.logger.debug('Filtering Topics ' + topic + ' from local dispatcher'); - me.messageHandler = me.messageHandler.filter(function (obj) { - return !tryPattern(obj.t, topic); - }); + me.messageHandler = me.messageHandler.filter(function (obj) { + return !tryPattern(obj.t, topic); + }); }; me.onMessage = function (topic, message) { var i, @@ -217,10 +178,9 @@ function Broker(conf, logger) { * Iterate over the messageHandler to match topic patter, * and dispatch message to only proper handler */ - for (i = 0; i < length; i++ ){ + for (i = 0; i < length; i++ ) { var obj = me.messageHandler[i]; if (tryPattern(obj.t, topic)) { - me.logger.info('Fired STATUS: ' + topic + JSON.stringify(message)); obj.h(topic, message); } } @@ -232,15 +192,12 @@ function Broker(conf, logger) { */ var toCallBack = callback; function connectCallback() { - me.logger.debug('Subscribing to: ' + topic); me.client.subscribe(topic, {qos: 1}, function (err, granted) { - me.logger.debug("grant " + JSON.stringify(granted)); var topicAsPattern = granted[0].topic.replace(/\+/g, "[^<>]*"); - me.logger.info("Topic Pattern :" + topicAsPattern); me.attach(topicAsPattern, handler); if (toCallBack) { toCallBack(); - } + } }); } if (!me.connected()) { @@ -248,7 +205,6 @@ function Broker(conf, logger) { if (!err) { connectCallback(); } else { - me.logger.error(err); if (toCallBack) { toCallBack(err); } @@ -259,15 +215,12 @@ function Broker(conf, logger) { } }; me.unbind = function (topic, callback) { - me.logger.debug('Unbinding from Topic ' + topic); me.client.unsubscribe(topic, function() { - me.logger.info('Unbound (UNSUBACK) of topic ' + topic); + me.dettach(topic); + if (callback) { + callback(); + } }); - me.logger.debug('Filtering Topics ' + topic + ' from local dispatcher'); - me.dettach(topic); - if (callback) { - callback(); - } }; /** * @description publish broadcast to an specifics topics, the message. @@ -283,7 +236,6 @@ function Broker(conf, logger) { options = options || me.pubArgs; } function publishCallback() { - me.logger.debug('Publishing : T => ' + topic + " MSG => " + JSON.stringify(message)); me.client.publish(topic, JSON.stringify(message), options, callback); } if (!me.connected()) { @@ -291,10 +243,9 @@ function Broker(conf, logger) { if (!err) { publishCallback(); } else { - me.logger.error("The connectio has faild on publish " + err); if (callback) { callback(err); - } + } } }); } else { @@ -309,10 +260,10 @@ function Broker(conf, logger) { Broker.prototype = new events.EventEmitter(); var broker = null; -module.exports.singleton = function (conf, logger) { +module.exports.singleton = function (conf) { if (!broker) { - broker = new Broker(conf, logger); + broker = new Broker(conf); } return broker; }; -module.exports.Broker = Broker; \ No newline at end of file +module.exports.Broker = Broker; diff --git a/lib/proxies/iot.control.mqtt.js b/lib/proxies/iot.control.mqtt.js index 3b39c11..83fa7bd 100644 --- a/lib/proxies/iot.control.mqtt.js +++ b/lib/proxies/iot.control.mqtt.js @@ -28,9 +28,8 @@ var common = require('../common'), -function IoTKitMQTTControl(conf) { +function IoTKitMQTTControl(conf, broker) { var me = this; - //me.logger = logger; me.client = broker; me.type = 'mqtt'; me.topics = conf.connector[me.type].topic; @@ -38,21 +37,19 @@ function IoTKitMQTTControl(conf) { qos: 0, retain: false }; - //me.logger.debug(me.type.toUpperCase(), 'Control Proxy Created'); } IoTKitMQTTControl.prototype.controlCommandListen = function (data, handlerCb, syncCall) { var me = this; var controlTopic = common.buildPath(me.topics.control_command, data.deviceId); var handler = function (topic, message) { - //me.logger.debug('controlCommandListen Topic %s , Message Recv : %s', topic, message); handlerCb(message); }; return me.client.bind(controlTopic, handler, syncCall); }; -module.exports.init = function(conf, logger) { - var brokerConnector = Broker.singleton(conf.connector.mqtt, logger); - return new IoTKitMQTTControl(conf, logger, brokerConnector); +module.exports.init = function(conf) { + var broker = Broker.singleton(conf.connector.mqtt); + return new IoTKitMQTTControl(conf, broker); }; diff --git a/lib/proxies/iot.mqtt.js b/lib/proxies/iot.mqtt.js index 6ac0f75..6aae09b 100644 --- a/lib/proxies/iot.mqtt.js +++ b/lib/proxies/iot.mqtt.js @@ -35,13 +35,9 @@ function IoTKitMQTTCloud(conf, broker) { qos: 1, retain: false }; - console.log("logger", me.logger) - } IoTKitMQTTCloud.prototype.pullActuations = function (data, callback) { - var me = this; - //me.logger.error("Actuations pulling is not yet supported by MQTT protocol"); callback(null); }; @@ -49,7 +45,6 @@ IoTKitMQTTCloud.prototype.data = function (data, callback) { var me = this; delete data.deviceToken; var topic = common.buildPath(me.topics.metric_topic, [data.accountId, data.did]); - //me.logger.debug("Metric doc: %j", data, {}); delete data.gatewayId; return me.client.publish(topic, data.convertToMQTTPayload(), me.pubArgs, function() { return callback({status:0}); @@ -65,7 +60,6 @@ IoTKitMQTTCloud.prototype.healthResponse = function (device, callback, syncCallb var me = this; var healthStatus = common.buildPath(me.topics.health_status, device); var handler = function (topic, message) { - //me.logger.debug('Topic %s , Message Recv : %s', topic, message); me.client.unbind(healthStatus); callback(message); }; @@ -74,7 +68,6 @@ IoTKitMQTTCloud.prototype.healthResponse = function (device, callback, syncCallb IoTKitMQTTCloud.prototype.health = function (device, callback) { var me = this; - //me.logger.info("Starting Health testing "); me.healthResponse(device, callback, function (err) { if (!err) { var topic = common.buildPath(me.topics.health, device); @@ -99,12 +92,10 @@ IoTKitMQTTCloud.prototype.setCredential = function (user, password) { }; IoTKitMQTTCloud.prototype.getActualTime = function (callback) { - var me = this; - //me.logger.error('This option is not currently supported for MQTT protocol.'); callback(null); }; module.exports.init = function(conf) { - var brokerConnector = Broker.singleton(conf.connector.mqtt); - return new IoTKitMQTTCloud(conf, brokerConnector); + var broker = Broker.singleton(conf.connector.mqtt); + return new IoTKitMQTTCloud(conf, broker); }; From 8565bd8ab2327a03724160a65163055b4e72a878 Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Thu, 21 Mar 2019 21:06:23 +0000 Subject: [PATCH 04/14] Bump version to 1.0.0beta3 Signed-off-by: Scott Ware --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6ba28c0..52c8d45 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@open-iot-service-platform/oisp-sdk-js", - "version": "1.0.0beta2", + "version": "1.0.0beta3", "description": "OISP SDK for Node.js", "main": "index.js", "scripts": { From 992bc6af17ec0809a3624a769c2ee3750170ed1a Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Wed, 17 Apr 2019 15:54:48 +0100 Subject: [PATCH 05/14] Replace cbor with borc Signed-off-by: Scott Ware --- api/rest/data.def.js | 2 +- api/rest/devices.def.js | 2 +- lib/httpClient.js | 2 +- package.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/api/rest/data.def.js b/api/rest/data.def.js index 68313c7..90cd1b5 100644 --- a/api/rest/data.def.js +++ b/api/rest/data.def.js @@ -24,7 +24,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -var cbor = require('cbor'); +var cbor = require('borc'); module.exports = function(config) { var common = require('../../lib/common'); diff --git a/api/rest/devices.def.js b/api/rest/devices.def.js index 4f94d9c..ffedbcf 100644 --- a/api/rest/devices.def.js +++ b/api/rest/devices.def.js @@ -24,7 +24,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -var cbor = require('cbor'); +var cbor = require('borc'); module.exports = function(config) { var common = require('../../lib/common'); diff --git a/lib/httpClient.js b/lib/httpClient.js index 78d52c1..54b8189 100755 --- a/lib/httpClient.js +++ b/lib/httpClient.js @@ -31,7 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * HTTPS request */ var request = require('request'); -var cbor = require('cbor'); +var cbor = require('borc'); function processResponse(res, body, callback) { var data = null; diff --git a/package.json b/package.json index 52c8d45..6eeeabf 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "istanbul": "^0.4.5", "mocha": "^4.0.1", "rewire": "^2.5.2", - "cbor": "^4.1.5" + "borc": "^2.1.0" }, "dependencies": { "async": "^2.5.0", From 9f9d8d78178f313d13453805467e6723b25a986c Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Wed, 17 Apr 2019 20:11:28 +0100 Subject: [PATCH 06/14] Move 'borc' dependency to the correct section Signed-off-by: Scott Ware --- package.json | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 6eeeabf..913a79a 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,14 @@ }, "author": "", "license": "ISC", + "dependencies": { + "async": "^2.5.0", + "borc": "^2.1.0", + "request": "^2.83.0", + "tunnel": "0.0.5", + "mqtt" : "^2.13.1", + "websocket": "^1.0.24" + }, "devDependencies": { "chai": "^4.1.2", "grunt": "^1.0.1", @@ -18,14 +26,6 @@ "grunt-mocha-istanbul": "^5.0.2", "istanbul": "^0.4.5", "mocha": "^4.0.1", - "rewire": "^2.5.2", - "borc": "^2.1.0" - }, - "dependencies": { - "async": "^2.5.0", - "request": "^2.83.0", - "tunnel": "0.0.5", - "mqtt" : "^2.13.1", - "websocket": "^1.0.24" + "rewire": "^2.5.2" } } From 556349593642e4d9297445e70116dbdabfa979fb Mon Sep 17 00:00:00 2001 From: Gabrielle Poerwawinata Date: Thu, 23 May 2019 16:32:42 +0200 Subject: [PATCH 07/14] removing certificate attribute in mqtts Signed-off-by: Gabrielle Poerwawinata line is removed and sign-off added --- api/mqtt/connector.js | 1 - 1 file changed, 1 deletion(-) diff --git a/api/mqtt/connector.js b/api/mqtt/connector.js index 1c27b1a..c778995 100644 --- a/api/mqtt/connector.js +++ b/api/mqtt/connector.js @@ -123,7 +123,6 @@ function Broker(conf) { me.client = mqtt.connect('mqtts://' + me.host + ":" + me.port, { username: me.crd.username, password: me.crd.password, - ca : fs.readFileSync(conf.ca), rejectUnauthorized: false }); } From b9d4556b1b9ab3fb60a3b648c75e8e8cb2e5c412 Mon Sep 17 00:00:00 2001 From: Marcel Wagner Date: Tue, 4 Jun 2019 23:28:55 +0200 Subject: [PATCH 08/14] SearchDataAdvanced REST API needs encoding = null setting to allow return of binary data. Closing Issue #21 Signed-off-by: Marcel Wagner --- api/rest/data.def.js | 1 + 1 file changed, 1 insertion(+) diff --git a/api/rest/data.def.js b/api/rest/data.def.js index 90cd1b5..4c7dbc0 100644 --- a/api/rest/data.def.js +++ b/api/rest/data.def.js @@ -70,6 +70,7 @@ module.exports = function(config) { this.token = data.userToken; ConnectionOptions.call(this); this.method = 'POST'; + this.encoding = null; this.body = JSON.stringify(data.body); } SearchDataAdvancedOption.prototype = new ConnectionOptions(); From 802a7b325b7b009b284bba22eb65dcacb97a6824 Mon Sep 17 00:00:00 2001 From: Gabrielle Poerwawinata Date: Wed, 26 Jun 2019 11:18:03 +0200 Subject: [PATCH 09/14] Include metric to sdk. This can be useful to enable data payload convertion to MQTT or REST without the presence of agent. Signed-off-by: Gabrielle Poerwawinata gabrielle.w.poerwawinata@intel.com --- lib/data/index.js | 25 ++++++++++ lib/data/metric.js | 115 +++++++++++++++++++++++++++++++++++++++++++++ lib/index.js | 1 + 3 files changed, 141 insertions(+) create mode 100644 lib/data/index.js create mode 100644 lib/data/metric.js diff --git a/lib/data/index.js b/lib/data/index.js new file mode 100644 index 0000000..11bc8b7 --- /dev/null +++ b/lib/data/index.js @@ -0,0 +1,25 @@ +/* +Copyright (c) 2014, Intel Corporation +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +"use strict"; +module.exports = { + metric: require('./metric') +}; \ No newline at end of file diff --git a/lib/data/metric.js b/lib/data/metric.js new file mode 100644 index 0000000..2a03399 --- /dev/null +++ b/lib/data/metric.js @@ -0,0 +1,115 @@ +/* +Copyright (c) 2014, Intel Corporation + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +"use strict"; + +/** + * Interface to manage the Metric Payload require by Advances Analytics. + * @constructor + */ +function Metric () { + this.accountId = null; + this.did = null; + this.on = Date.now(); + this.count = 0; + this.data = []; +} + +Metric.prototype.dataAsRoot = function (value) { + var cid = this.nameOfComponentId || "cid"; + var theValue = value.v || value.value || "0"; + var dataTemporal = { + "on": value.on || this.on, + "value": theValue.toString() //Conversion since JSON schema required. + }; + dataTemporal[cid] = value.componentId || value.cid || this.globalCid; + if (value.loc) { + dataTemporal.loc = value.loc; + } + if (value.attributes) { + dataTemporal.attributes = value.attributes; + } + this.data.push(dataTemporal); +}; + +Metric.prototype.dataAsArray = function (msg) { + var l = msg.data.length; + this.globalCid = msg.cid || this.componentId ; + for (var i = 0; i < l; i++) { + var value = msg.data[i]; + this.dataAsRoot(value); + } +}; + +Metric.prototype.dataProcess = function (datoArr) { + if (datoArr) { + if (Array.isArray(datoArr.data)) { + console.log("data as array") + this.dataAsArray(datoArr); + } else { + console.log("data as root") + this.dataAsRoot(datoArr); + } + } +}; + +Metric.prototype.set = function (data) { + this.accountId = data.accountId || data.domainId; + this.did = data.deviceId; + this.on = data.on || this.on; + this.data = []; + this.dataProcess(data); + this.count = this.data.length; +}; + +Metric.prototype.convertToMQTTPayload = function() { + this.dataProcess(); + this.count = this.data.length; + return this; +}; + +Metric.prototype.convertToRestPayload = function() { + this.nameOfComponentId = "componentId"; + this.dataProcess(); + /** + * Since the schema validation of Rest Interface if so hard + * it is removed the none require parameter + */ + this.data.forEach(function (ob) { + if (ob.cid) { + ob.componentId = ob.cid; + delete ob.cid; + } + }); + + delete this.did; + delete this.count; + delete this.nameOfComponentId; + delete this.globalCid; + return this; +}; + +module.exports.init = function () { + return Metric; +}; diff --git a/lib/index.js b/lib/index.js index 6a57693..1fe4e78 100644 --- a/lib/index.js +++ b/lib/index.js @@ -30,6 +30,7 @@ module.exports = function(config) { module.common = require('./common.js'); module.httpClient = require('./httpClient'); module.proxies = require('./proxies')(config); + module.data = require('./data'); return module; } From cd4e294c89498ead31ba928fc3fec4d21d939fcd Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Wed, 26 Jun 2019 12:48:08 +0100 Subject: [PATCH 10/14] Update dependencies to fix security vulnerabilities Signed-off-by: Scott Ware --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 913a79a..d929bc6 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,9 @@ "author": "", "license": "ISC", "dependencies": { - "async": "^2.5.0", + "async": "^2.6.2", "borc": "^2.1.0", - "request": "^2.83.0", + "request": "^2.88.0", "tunnel": "0.0.5", "mqtt" : "^2.13.1", "websocket": "^1.0.24" From 997141c91cedd2ffe015ed815877f49d6eaaf3bf Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Wed, 26 Jun 2019 12:48:33 +0100 Subject: [PATCH 11/14] Remove unused variable from mqtt connector Signed-off-by: Scott Ware --- api/mqtt/connector.js | 1 - 1 file changed, 1 deletion(-) diff --git a/api/mqtt/connector.js b/api/mqtt/connector.js index c778995..3e7147e 100644 --- a/api/mqtt/connector.js +++ b/api/mqtt/connector.js @@ -17,7 +17,6 @@ "use strict"; var mqtt = require('mqtt'); var events = require('events'); -var fs = require('fs') var common = require('../../lib/common'); function Broker(conf) { From 893304c5403c287b519ad13b6eec3de285d48023 Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Wed, 26 Jun 2019 12:49:32 +0100 Subject: [PATCH 12/14] Fix incorrect indent Signed-off-by: Scott Ware --- lib/data/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/data/index.js b/lib/data/index.js index 11bc8b7..a6e5112 100644 --- a/lib/data/index.js +++ b/lib/data/index.js @@ -21,5 +21,5 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; module.exports = { - metric: require('./metric') -}; \ No newline at end of file + metric: require('./metric') +}; From a7641ef3a323fff5fd61e429359ca2c7cb0d27c8 Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Wed, 26 Jun 2019 12:50:28 +0100 Subject: [PATCH 13/14] Remove console instances from metric Signed-off-by: Scott Ware --- lib/data/metric.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/data/metric.js b/lib/data/metric.js index 2a03399..65e2d08 100644 --- a/lib/data/metric.js +++ b/lib/data/metric.js @@ -65,10 +65,8 @@ Metric.prototype.dataAsArray = function (msg) { Metric.prototype.dataProcess = function (datoArr) { if (datoArr) { if (Array.isArray(datoArr.data)) { - console.log("data as array") this.dataAsArray(datoArr); } else { - console.log("data as root") this.dataAsRoot(datoArr); } } From 6cb5f81a0af837699f89b379d6239096c82d6b2a Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Fri, 28 Jun 2019 22:19:33 +0100 Subject: [PATCH 14/14] Bump version for 1.0.0 release Signed-off-by: Scott Ware --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d929bc6..015e162 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@open-iot-service-platform/oisp-sdk-js", - "version": "1.0.0beta3", + "version": "1.0.0", "description": "OISP SDK for Node.js", "main": "index.js", "scripts": {