UNPKG

sensecap

Version:

## Install ``` npm install sensecap --save ```

133 lines (120 loc) 4.25 kB
var logger = require('../utils/loggerUtil') var mqtt = require('mqtt') function BaseMqtt(options) { // 连接选项 this.option = { clean: true, keepalive: 30, connectTimeout: 4000, // 超时时间 protocol: 'mqtt', port: 1883, password: options.accessKeySecret, reconnectPeriod: 10000, transformWsUrl: function (url, options, client) { logger.log('transformWsUrl: ' + url); return url; } } this.orgId = '0' this.isConnected = false; switch (options.region) { case 1: this.brokerUrl = 'http://sensecap-openstream.seeed.cn'; break; case 2: this.brokerUrl = 'http://sensecap-openstream.seeed.cc'; break; default: this.brokerUrl = 'http://sensecap-openstream.seeed.cn'; break; } } BaseMqtt.prototype = { connect: function (callback) { var that = this; this.option.clientId = 'org-' + this.orgId + '-nodejs'; this.option.username = 'org-' + this.orgId; if (this.isConnected) { callback(null); } else { this.client = mqtt.connect(this.brokerUrl, this.option); } this.client.on('connect', function () { logger.log('mqtt connect success!'); that.isConnected = true; callback(null) }); this.client.on('message', function (topic, message) { // logger.log('mqtt message===>'); // logger.log(topic); // logger.log(message.toString()); if (that.onMessageCallback) { that.onMessageCallback(null, topic, message.toString()); } }); this.client.on('error', function (error) { logger.log('mqtt connect fail!'); logger.error(error); that.isConnected = false; callback(error) }); this.client.on('reconnect', function () { logger.log('mqtt reconnect!'); }); this.client.on('offline', function () { logger.log('mqtt offline!'); that.isConnected = false; }); this.client.on('end', function () { logger.log('mqtt end!'); that.isConnected = false; }); this.client.on('close', function () { logger.log('mqtt close!'); that.isConnected = false; }); }, subscribeData: function (deviceEui, channel, measurementId, callback) { var topic = '/device_sensor_data/' + this.orgId + '/' + (deviceEui ? deviceEui : '+') + '/' + (channel ? channel : '+') + '/+/' + (measurementId ? measurementId : '+') logger.log('subscribe topic========: ' + topic) this.onMessageCallback = callback; this.client.subscribe(topic, { qos: 2, // 0 = 订阅建立时发送保留消息 1 = 订阅建立时,若该订阅当前不存在则发送保留消息 2 = 订阅建立时不要发送保留消息 rh: 2, }, function (err) { if (!err) { logger.log('mqtt subscribe success: ' + topic); } else { callback(err); } }) }, unSubscribe: function (deviceEui, channel, measurementId, callback) { var topic = '/device_sensor_data/' + this.orgId + '/' + (deviceEui ? deviceEui : '+') + '/' + (channel ? channel : '+') + '/+/' + (measurementId ? measurementId : '+') this.client.unsubscribe(topic, {}, function (err) { if (!err) { logger.log('unsubscribe success: ' + topic); callback(null, topic); } else { logger.log('err========'); logger.error(err); callback(err); } }) }, disconnect: function () { this.client.end(false, { reasonCode: 0 // 0:正常断开 }, function () { logger.log('mqtt disconnect success!'); }) }, setOrgId: function (orgId) { this.orgId = orgId }, getOrgId: function () { return this.orgId; }, }; module.exports = BaseMqtt;