'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _const = require('./const'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var _require = require('./utils'), hmacSign = _require.hmacSign; var util = require('util'); var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined' || typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope; var DEFAULT_REGION = 'cn-shanghai'; var BROKER_URL = '%s%s.iot-as-mqtt.%s.aliyuncs.com:%s/'; var tlsPrefix = ['tls://', 'mqtts://', 'wss://']; var Model = function () { function Model(config) { _classCallCheck(this, Model); //初始化参数 this.init(config); //参数校验合法 this.configchecking(); } // 获取通配订阅topic _createClass(Model, [{ key: 'getWildcardTopic', value: function getWildcardTopic() { return util.format("/sys/%s/%s/thing", this.productKey, this.deviceName); } // 获取三元组 }, { key: 'triple', value: function triple() { var productKey = this.productKey, deviceName = this.deviceName, deviceSecret = this.deviceSecret; return { productKey: productKey, deviceName: deviceName, deviceSecret: deviceSecret }; } }, { key: 'getPostEventMethod', value: function getPostEventMethod(eventName) { return util.format(_const.ALIYUN_BROKER_METHODS_TEMPLATE.POST_EVENT, eventName); } }, { key: 'getPostEvenTopic', value: function getPostEvenTopic(eventName) { return util.format(_const.ALIYUN_BROKER_TOPICS.EVENT_POST_TOPIC, this.productKey, this.deviceName, eventName); } }, { key: 'getPostEvenReplyTopic', value: function getPostEvenReplyTopic(eventName) { return util.format(_const.ALIYUN_BROKER_TOPICS.EVENT_POST_REPLY_TOPIC, this.productKey, this.deviceName, eventName); } }, { key: 'getServiceTopic', value: function getServiceTopic(serviceName) { var serviceNameReplace = serviceName; if (serviceName.indexOf('/') != -1) { serviceNameReplace = serviceName.replace('.', '/'); } return util.format(_const.ALIYUN_BROKER_TOPICS.SERVICE_TOPIC, this.productKey, this.deviceName, serviceNameReplace); } // getServiceRespTopic(serviceName) { // let serviceNameReplace = serviceName; // if (serviceName.indexOf('/') != -1) { // serviceNameReplace = serviceName.replace('.', '/') // } // return util.format(BROKER_TOPICS.SERVICE_RESP_TOPIC, this.productKey, this.deviceName, serviceNameReplace) // } //生成alink协议内容 }, { key: 'genAlinkContent', value: function genAlinkContent(method, params) { var id = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ""; var version = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '1.0'; var msg = { id: id, version: version, params: params, method: method }; return msg; } /** * 连接参数生成规则 * broker:${YourProductKey}.iot-as-mqtt.${Region}.aliyuncs.com:1883 * Region:https://help.aliyun.com/document_detail/40654.html?spm=a2c4g.11186623.2.14.6cba65fekp352N * ClientId: 表示客户端ID,建议使用设备的MAC地址或SN码,64字符内 ,clientId+"|securemode=3,signmethod=hmacsha1,timestamp=132323232|" * Username: deviceName+"&"+productKey * Password: sign_hmac(deviceSecret,content) * securemode:表示目前安全模式,可选值有2 (TLS直连模式)和3(TCP直连模式) * signmethod:签名算法,支持hmacmd5,hmacsha1,hmacsha256和 sha256,默认为hmacmd5。 * timestamp: 可选 */ }, { key: 'genConnectPrarms', value: function genConnectPrarms() { return { clientId: this.clientId + '|securemode=' + this.securemode + ',\n signmethod=hmac' + this.signAlgorithm + ',\n timestamp=' + this.timestamp + '|', username: this.deviceName + '&' + this.productKey, password: hmacSign(this.signAlgorithm, this.deviceSecret, 'clientId' + this.clientId + 'deviceName' + this.deviceName + 'productKey' + this.productKey + 'timestamp' + this.timestamp), keepalive: this.keepalive, clean: this.clean }; } }, { key: 'init', value: function init(config) { var _this = this; // 初始化连接参数 if (config.brokerUrl && tlsPrefix.some(function (prefix) { return config.brokerUrl.startsWith(prefix); }) || config.tls) { this.securemode = 2; this.tls = true; } else { this.securemode = 3; } if (isBrowser) { if (this.tls) { this.brokerProtocol = 'wss://'; } else { this.brokerProtocol = 'ws://'; } this.brokerPort = 443; } else { if (this.tls) { this.brokerProtocol = 'mqtts://'; } else { this.brokerProtocol = 'mqtt://'; } this.brokerPort = 1883; } if (config.brokerUrl && tlsPrefix.some(function (prefix) { return config.brokerUrl.startsWith(prefix); }) || config.tls) { this.securemode = 2; this.tls = true; } else { this.securemode = 3; } if (isBrowser) { if (this.tls) { this.brokerProtocol = 'wss://'; } else { this.brokerProtocol = 'ws://'; } this.brokerPort = 443; } else { if (this.tls) { this.brokerProtocol = 'mqtts://'; } else { this.brokerProtocol = 'mqtt://'; } this.brokerPort = 1883; } //三元组忽略大小写 Object.keys(config).forEach(function (originKey) { var key = originKey.toLowerCase(); switch (key) { case "productkey": _this.productKey = config[originKey]; break; case "devicename": _this.deviceName = config[originKey]; break; case "devicesecret": _this.deviceSecret = config[originKey]; break; } }); this.region = config.region || config.regionId; this.keepalive = config.keepalive || 60; //keepalive,默认60 this.clean = config.clean || false; //cleanSession配置选项 this.signAlgorithm = config.signAlgorithm || 'sha1'; this.config = config || {}; if (config.brokerUrl) { this.brokerUrl = config.brokerUrl; } else { this.brokerUrl = util.format(BROKER_URL, this.brokerProtocol, this.productKey, this.region || DEFAULT_REGION, this.brokerPort); } this.timestamp = Date.now(); this.clientId = config.clientId ? config.clientId + '_aliyun-iot-device-sdk-js' : this.productKey + '&' + this.deviceName + '_aliyun-iot-device-sdk-js'; /* 初始化topic */ //methods this.POST_PROPERY_METHOD = _const.ALIYUN_BROKER_METHODS_TEMPLATE.POST_PROPERY; // this.POST_EVENT = util.format(METHODS_TEMPLATE.POST_EVENT, eventName); this.POST_TAGS_METHOD = _const.ALIYUN_BROKER_METHODS_TEMPLATE.POST_TAGS; this.DELETE_TAGS_METHOD = _const.ALIYUN_BROKER_METHODS_TEMPLATE.DELETE_TAGS; this.GET_CONFIG_METHOD = _const.ALIYUN_BROKER_METHODS_TEMPLATE.GET_CONFIG; this.ADD_TOPO_METHOD = _const.ALIYUN_BROKER_METHODS_TEMPLATE.ADD_TOPO; this.DELETE_TOPO_METHOD = _const.ALIYUN_BROKER_METHODS_TEMPLATE.DELETE_TOPO; this.GET_TOPO_METHOD = _const.ALIYUN_BROKER_METHODS_TEMPLATE.GET_TOPO; this.SUBDEVICE_REGISTER_METHOD = _const.ALIYUN_BROKER_METHODS_TEMPLATE.SUBDEVICE_REGISTER; //topic var _formatWithPKDN = function _formatWithPKDN(template) { return util.format(template, _this.productKey, _this.deviceName); }; this.POST_PROPS_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.PROPERTY_POST_TOPIC); this.POST_PROPS_REPLY_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.PROPERTY_POST_REPLY_TOPIC); // this.POST_EVENT_TOPIC = _formatWithPKDN(BROKER_TOPICS.EVENT_POST_TOPIC) // this.POST_EVENT_REPLY_TOPIC = _formatWithPKDN(BROKER_TOPICS.EVENT_POST_REPLY_TOPIC) this.SERVICE_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.SERVICE_TOPIC); this.TAG_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.TAG_TOPIC); this.TAG_REPLY_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.TAG_REPLY_TOPIC); this.TAG_DELETE_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.TAG_DELETE_TOPIC); this.TAG_DELETE_REPLY_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.TAG_DELETE_REPLY_TOPIC); this.CONFIG_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.CONFIG_TOPIC); this.CONFIG_REPLY_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.CONFIG_REPLY_TOPIC); this.CONFIG_SUBSCRIBE_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.CONFIG_SUBSCRIBE_TOPIC); this.CONFIG_SUBSCRIBE_RESP_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.CONFIG_SUBSCRIBE_RESP_TOPIC); this.SHADOW_SUBSCRIBE_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.SHADOW_SUBSCRIBE_TOPIC); this.SHADOW_GET_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.SHADOW_GET_TOPIC); this.SHADOW_POST_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.SHADOW_POST_TOPIC); // gateway this.ADD_TOPO_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.ADD_TOPO_TOPIC); this.ADD_TOPO_REPLY_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.ADD_TOPO_REPLY_TOPIC); this.DELETE_TOPO_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.DELETE_TOPO_TOPIC); this.DELETE_TOPO_REPLY_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.DELETE_TOPO_REPLY_TOPIC); this.GET_TOPO_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.GET_TOPO_TOPIC); this.GET_TOPO_REPLY_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.GET_TOPO_REPLY_TOPIC); this.LOGIN_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.LOGIN_TOPIC); this.LOGIN_REPLY_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.LOGIN_REPLY_TOPIC); this.LOGOUT_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.LOGOUT_TOPIC); this.LOGOUT_REPLY_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.LOGOUT_REPLY_TOPIC); this.SUBDEVICE_REGISTER_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.SUBDEVICE_REGISTER_TOPIC); this.SUBDEVICE_REGISTER_REPLY_TOPIC = _formatWithPKDN(_const.ALIYUN_BROKER_TOPICS.SUBDEVICE_REGISTER_REPLY_TOPIC); } }, { key: 'configchecking', value: function configchecking() { if (typeof this.productKey === 'undefined') { throw new Error('productKey should not be empty'); } if (typeof this.deviceName === 'undefined') { throw new Error('deviceName should not be empty'); } if (typeof this.deviceSecret === 'undefined') { throw new Error('deviceSecret should not be empty'); } } }]); return Model; }(); exports.default = Model; module.exports = Model;