aliyun-iot-device-sdk
Advanced tools
Comparing version
@@ -21,3 +21,5 @@ 'use strict'; | ||
createDebug = _require.createDebug, | ||
mqttMatch = _require.mqttMatch; | ||
mqttMatch = _require.mqttMatch, | ||
getIP = _require.getIP, | ||
getPlatform = _require.getPlatform; | ||
@@ -40,3 +42,4 @@ var debug = createDebug('device'); | ||
EVENT_POST_TOPIC: '/sys/%s/%s/thing/event/%s/post', | ||
EVENT_POST_REPLY_TOPIC: '/sys/%s/%s/thing/event/%s/post_reply' | ||
EVENT_POST_REPLY_TOPIC: '/sys/%s/%s/thing/event/%s/post_reply', | ||
REPORT_SDK_INFO_TOPIC: '/sys/%s/%s/thing/deviceinfo/update' | ||
}; | ||
@@ -55,55 +58,5 @@ var tlsPrefix = ['tls://', 'mqtts://', 'wss://']; | ||
if (typeof config.productKey === 'undefined') { | ||
throw new Error('productKey should not be empty'); | ||
} | ||
if (typeof config.deviceName === 'undefined') { | ||
throw new Error('deviceName should not be empty'); | ||
} | ||
if (typeof config.deviceSecret === 'undefined') { | ||
throw new Error('deviceSecret should not be empty'); | ||
} | ||
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; | ||
} | ||
_this.productKey = config.productKey; | ||
_this.deviceName = config.deviceName; | ||
_this.deviceSecret = config.deviceSecret; | ||
_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'; | ||
_this._props = {}; | ||
_this.postPropsTopic = util.format(TOPICS.PROPERTY_POST_TOPIC, _this.productKey, _this.deviceName); | ||
_this.postPropsReplyTopic = util.format(TOPICS.PROPERTY_POST_REPLY_TOPIC, _this.productKey, _this.deviceName); | ||
_this.postEventTopic = util.format(TOPICS.EVENT_POST_TOPIC, _this.productKey, _this.deviceName); | ||
_this.postEventReplyTopic = util.format(TOPICS.EVENT_POST_REPLY_TOPIC, _this.productKey, _this.deviceName); | ||
_this.serviceTopic = util.format(TOPICS.SERVICE_TOPIC, _this.productKey, _this.deviceName); | ||
_this.paramsSetting(config); //初始化输入参数 | ||
_this.configchecking(); //验证三元组 | ||
// 初始 connect', 'error', 'close', 'reconnect', 'offline', 'message' 订阅 | ||
_this.onReady = _this.createOnReady(); | ||
@@ -131,2 +84,13 @@ _this.subTopicAndOnMessage = _this.createSubTopicAndOnMessage(); | ||
callbacks = []; | ||
//上报sdk版本信息 | ||
var networkInfo = 'WiFi|' + getIP(); | ||
var sdkInfo = { | ||
"domain": "SYSTEM", | ||
'SYS_SDK_LANGUAGE': 'NodeJS', | ||
'SYS_LP_SDK_VERSION': '0.3.0', | ||
'SYS_SDK_IF_INFO': networkInfo, | ||
'SYS_SDK_PLATFORM': getPlatform() | ||
}; | ||
_this2.publish(util.format(TOPICS.REPORT_SDK_INFO_TOPIC, _this2.productKey, _this2.deviceName), JSON.stringify(sdkInfo)); | ||
// console.log('publish sdk info',sdkInfo); | ||
}; | ||
@@ -287,3 +251,3 @@ this._mqttClient = this.createClient(this.config); | ||
return { | ||
clientId: this.clientId + '|securemode=' + this._securemode + ',\n signmethod=hmac' + this.signAlgorithm + ',\n timestamp=' + this.timestamp + '|', | ||
clientId: this.clientId + '|securemode=' + this.securemode + ',\n signmethod=hmac' + this.signAlgorithm + ',\n timestamp=' + this.timestamp + '|', | ||
username: this.deviceName + '&' + this.productKey, | ||
@@ -293,3 +257,2 @@ password: hmacSign(this.signAlgorithm, this.deviceSecret, 'clientId' + this.clientId + 'deviceName' + this.deviceName + 'productKey' + this.productKey + 'timestamp' + this.timestamp), | ||
clean: this.clean | ||
}; | ||
@@ -374,2 +337,99 @@ } | ||
} | ||
}, { | ||
key: 'paramsSetting', | ||
value: function paramsSetting(config) { | ||
var _this5 = this; | ||
// brokerUrlsecuremode&brokerPort | ||
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": | ||
_this5.productKey = config[originKey];break; | ||
case "devicename": | ||
_this5.deviceName = config[originKey];break; | ||
case "devicesecret": | ||
_this5.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 | ||
this.postPropsTopic = util.format(TOPICS.PROPERTY_POST_TOPIC, this.productKey, this.deviceName); | ||
this.postPropsReplyTopic = util.format(TOPICS.PROPERTY_POST_REPLY_TOPIC, this.productKey, this.deviceName); | ||
this.postEventTopic = util.format(TOPICS.EVENT_POST_TOPIC, this.productKey, this.deviceName); | ||
this.postEventReplyTopic = util.format(TOPICS.EVENT_POST_REPLY_TOPIC, this.productKey, this.deviceName); | ||
this.serviceTopic = util.format(TOPICS.SERVICE_TOPIC, this.productKey, this.deviceName); | ||
} | ||
}, { | ||
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'); | ||
} | ||
} | ||
}]); | ||
@@ -376,0 +436,0 @@ |
@@ -6,2 +6,3 @@ 'use strict'; | ||
var crypto = require('crypto'); | ||
var os = require('os'); | ||
@@ -68,2 +69,21 @@ function hmacSign(type, secret, content) { | ||
function getIP() { | ||
var ifaces = os.networkInterfaces(); | ||
var ip = ""; | ||
for (var dev in ifaces) { | ||
ifaces[dev].forEach(function (details) { | ||
if (details.family == 'IPv4' && dev === 'en0') { | ||
ip = details.address; | ||
} | ||
}); | ||
} | ||
return ip; | ||
} | ||
function getPlatform() { | ||
return os.platform(); | ||
} | ||
exports.getPlatform = getPlatform; | ||
exports.getIP = getIP; | ||
exports.hmacSign = hmacSign; | ||
@@ -70,0 +90,0 @@ exports.mqttMatch = mqttMatch; |
@@ -6,2 +6,5 @@ 0.3.0版本更新 | ||
0.3.1版本更新 | ||
1:productKey,deviceName,deviceSecret大小写经常容易忽略,新版本支持忽略大小写,也可以连接上 | ||
{ | ||
"name": "aliyun-iot-device-sdk", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "aliyun iot device sdk", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -10,10 +10,12 @@ const { hmacSign } = require('../lib/utils'); | ||
exports.aliyunDeviceDevice = { | ||
exports.aliyunDevice1 = { | ||
productKey: 'a1aq9sQk2JE', | ||
deviceName: 'sdk_device1', | ||
deviceSecret: 'shvvZGkq4mM641WUhpfJpyInVeJhAH2y', | ||
}; | ||
exports.aliyunGateway1 = { | ||
productKey: 'a1BOOa9HG6Z', | ||
deviceName: 'sdk_gateway1', | ||
deviceSecret: 'foepBtCT0Jl6x640KKQCPeYi9Kv8NdSV' | ||
}; | ||
exports.airBox = { | ||
@@ -20,0 +22,0 @@ productKey: 'a', |
const aliyunIot = require('../'); | ||
const fixtures = require('./fixtures'); | ||
describe('test aliyunIot', () => { | ||
test('device should be ok', done => { | ||
const device = aliyunIot.device(fixtures.lightDevice); | ||
describe('test connectability with linkPlatform ', () => { | ||
//设备可连接行测试 | ||
test('device connectability should be ok', done => { | ||
const device = aliyunIot.device(fixtures.aliyunDevice1); | ||
device.on('connect', () => { | ||
@@ -13,4 +14,5 @@ expect(device.clientId).toContain('&'); | ||
}); | ||
test('gateway should be ok', done => { | ||
const device = aliyunIot.gateway(fixtures.lightDevice); | ||
//网关可连接行测试 | ||
test('gateway connectability should be ok', done => { | ||
const device = aliyunIot.gateway(fixtures.aliyunGateway1); | ||
device.on('connect', () => { | ||
@@ -22,2 +24,30 @@ expect(device.clientId).toContain('&'); | ||
}); | ||
//设备三元组大小写可以忽略 | ||
test('device pk,dn,ds should be ignore case ', done => { | ||
const device = aliyunIot.device({ | ||
ProdUCtKey: 'a1aq9sQk2JE', | ||
deViceName: 'sdk_device1', | ||
deVicEsecret: 'shvvZGkq4mM641WUhpfJpyInVeJhAH2y', | ||
}); | ||
device.on('connect', () => { | ||
expect(device.clientId).toContain('&'); | ||
device.end(); | ||
done(); | ||
}); | ||
}); | ||
//网关三元组大小写可以忽略 | ||
test('device pk,dn,ds should be ignore case ', done => { | ||
const gateway = aliyunIot.gateway({ | ||
ProdUCtKey: 'a1BOOa9HG6Z', | ||
deViceName: 'sdk_gateway1', | ||
deVicEsecret: 'foepBtCT0Jl6x640KKQCPeYi9Kv8NdSV' | ||
}); | ||
gateway.on('connect', () => { | ||
expect(gateway.clientId).toContain('&'); | ||
gateway.end(); | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -113,3 +113,24 @@ const Buffer = require('buffer').Buffer; | ||
}); | ||
// 数据上报 | ||
// test('publish data should ok', done => { | ||
// const device = aliyunIot.device({ | ||
// ...fixtures.aliyunDevice1 | ||
// }); | ||
// device.subscribe(`${device.productKey}/${device.deviceName}/get`); | ||
// device.on('message', (topic, payload) => { | ||
// console.log(topic, payload.toString()); | ||
// device.end(); | ||
// done(); | ||
// }); | ||
// device.on('connect', () => { | ||
// device.publish(`/${device.productKey}/${device.deviceName}/user/pub`, 'aaa'); | ||
// }); | ||
// }); | ||
}); | ||
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
560654
1.26%23
4.55%3178
4.95%5
-28.57%