-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.js
124 lines (119 loc) · 4.26 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//app.js
var mDeviceClouds = require('/utils/devicesClouds.js');
var util = require('/utils/mqtt/util.js')
var {
Client,
Message
} = require('/utils/mqtt/paho-mqtt.js')
App({
data: {
logged: false,
takeSession: false,
requestResult: '',
client: null
},
setOnMessageArrived: function(onMessageArrived) {
if (typeof onMessageArrived === 'function') {
this.data.onMessageArrived = onMessageArrived
}
},
setOnConnectionLost: function(onConnectionLost) {
if (typeof onConnectionLost === 'function') {
this.data.onConnectionLost = onConnectionLost
}
},
doConnect: function() {
var that = this;
if (that.data.client && that.data.client.isConnected()) {
console.log('不要重复连接');
return
}
var client = new Client(this.globalData.server_domain, this.globalData.clientId());
client.connect({
userName: this.globalData.userName,
password: this.globalData.password,
useSSL: true,
reconnect: true, //设置断线重连,默认是断线不重连
cleanSession: true,
keepAliveInterval: this.globalData.keepAliveInterval,
onFailure: function(errorCode) {
mDeviceClouds.notifyConnectEvent(false)
//console.log("connect failed code:" + errorCode.errorCode)
//console.log("connect failed message:" + errorCode.errorMessage)
},
onSuccess: function() {
that.data.client = client
client.onMessageArrived = function(msg) {
if (typeof that.data.onMessageArrived === 'function') {
return that.data.onMessageArrived(msg)
}
// console.log("onMessageArrived topic:" + msg.destinationName)
// console.log("onMessageArrived payload:" + msg.payloadString)
mDeviceClouds.notifyDeviceStatusEvent(msg.destinationName, msg.payloadString)
}
client.onConnectionLost = function(responseObject) {
if (typeof that.data.onConnectionLost === 'function') {
return that.data.onConnectionLost(responseObject)
}
if (responseObject.errorCode !== 0) {
//console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
//console.log("connect success..")
//连接成功mqtt服务器回调
mDeviceClouds.notifyConnectEvent(true)
}
});
},
onLaunch: function() {
//延迟链接,以防后面的收不到链接成功回调
var that = this
setTimeout(function() {
that.doConnect();
}, 800)
// 订阅某个设备推送状态函数:参数 device对象
mDeviceClouds.listenSubDeviceTopicEvent(true, function(device) {
var client = that.data.client;
if (client && client.isConnected()) {
//console.log('订阅成功');
return client.subscribe(device, {
qos: 1
});
}
//console.log('订阅失败');
})
// 发送消息给设备
mDeviceClouds.listenWriteDeviceEvent(true, function(device, message, qos = 1, retained = false) {
var client = that.data.client;
if (client && client.isConnected()) {
var message = new Message(message);
message.destinationName = device;
message.qos = qos;
message.retained = retained;
console.log('发送ok');
return client.send(message);
}
})
},
globalData: {
//连接的域名:注意格式,不要带端口号
server_domain: "wss://7qfp623.mqtt.iot.gz.baidubce.com/mqtt",
//心跳
keepAliveInterval: 60,
//本工程的链接mqtt服务器的名字和密码
userName: "7qfp623/wechatclient",
password: "YS92fo65tMWPeJSx",
//请保持唯一,一旦多个客户端用相同的clientID连接服务器就会挤掉之前的链接,后者先得。
clientId: function() {
// var len = 16; //长度
// var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
// var maxPos = $chars.length;
// var pwd = 'WC_';
// for (let i = 0; i < len; i++) {
// pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
// }
// return pwd;
return "DeviceId-3qi2sszsg8";
}, //因为百度云限制clientId,如果你的服务器不限制,根据需求你用随机数也可以的。参考上面的随机数获取方法即可!
}
})