zhuguifei
昨天 7941623b1fb108a154ea4270d7c7e2df6031f6e5
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<template>
    <view>
    </view>
</template>
 
<script>
    export default {
        name: 'MqttView',
        data() {
            return {
 
            }
        },
        onReady() {
 
 
        },
        destroyed() {
            console.error("MQTT组件销毁")
            this.$mqttTool.end().then(res => {
                console.error(res)
            })
        },
        methods: {
            initMqtt() {
                uni.getSystemInfo({
                    success: (res) => {
                        let deviceId = res.deviceId
                        if (!deviceId) {
                            deviceId = 'mobile-' + this.tenantId + '-' + Date.parse(new Date())
                        }
                        uni.setStorageSync(this.$constant.DEVICE_ID, 'mobile-' + this.tenantId + '-' + res
                            .deviceId);
                    }
                })
                this.startConnect();
            },
            /* 连接MQTT */
            async startConnect() {
                var _this = this
                const account = uni.getStorageSync('account');
                const deviceid = uni.getStorageSync(this.$constant.DEVICE_ID);
 
                if (!account) {
 
                    return false
                }
                let opts = {
                    // #ifdef H5
                    url: 'ws://' + this.$api.mqttBaseUrl + ':8083/mqtt',
                    // #endif
                    // #ifdef MP-WEIXIN
                    url: 'wxs://' + this.$api.mqttBaseUrl + ':8084/mqtt', // 微信小程序强制 WSS
                    // #endif
                    // #ifdef APP-PLUS
                    url: 'wx://' + this.$api.mqttBaseUrl + ':8083/mqtt', // Android/iOS 用普通 WebSocket
                    // #endif
                    clientId: deviceid,
                    // username: account.username,
                    // password: account.password,
                    //统一使用租户登录 TODO 添加多租户用户
                    username: 'tongjitang',
                    password: '123456'
                }
                if (!this.$mqttTool.client) {
                    this.$mqttTool.client = await this.$mqttTool.connect(opts);
                }
                //订阅查询设备状态返回数据
                this.$mqttTool.subscribe({
                    topic: this.$constant.SERVICE_DOWN + '/' + deviceid + '/#',
                    qos: 0
                }).then(res => {
                    console.error(res)
                })
 
                //订阅设备故障广播(广播不在乎客户端id,发送给租户下所有在线的设备)
                this.$mqttTool.subscribe({
                    topic: this.$constant.SERVICE_BROADCAST_TENANT_REAL_FAULT.replace('%s', this.tenantId),
                    qos: 0
                }).then(res => {
                    console.error(res)
                })
 
                //订阅设备实时故障(主动请求只发给请求设备)
                this.$mqttTool.subscribe({
                    topic: this.$constant.SERVICE_ONECE_TENANT_REAL_FAULT.replace('%s', deviceid),
                    qos: 0
                }).then(res => {
                    console.error(res)
                })
 
                //订阅干燥设备连接断开状态更新-租户内所有设备
                this.$mqttTool.subscribe({
                    topic: this.$constant.SERVICE_BROADCAST_TENANT_UPDATE_EQU_STATU.replace('%s', this
                        .tenantId),
                    qos: 0
                }).then(res => {
                    console.error(res)
                })
                //订阅查询干燥设备连接状态-单个设备
                this.$mqttTool.subscribe({
                    topic: this.$constant.SERVICE_RES_EQU_STATU.replace('%s', deviceid),
                    qos: 0
                }).then(res => {
                    console.error(res)
                })
 
                //订阅干燥设备实时数据
                this.$mqttTool.subscribe({
                    topic: this.$constant.SERVICE_BROADCAST_TENANT_REAL_DATA.replace('%s', this.tenantId),
                    qos: 0
                }).then(res => {
                    console.error(res)
                })
 
                //订阅发送指令返回结果
                // this.$mqttTool.subscribe({
                //     topic: this.$constant.SERVICE_RES_EQU_CMD,
                //     qos: 0
                // }).then(res => {
                //     console.error(res)
                // })
                // if (!client) {
                //     return false
                // }
                let that = this
                let client = this.$mqttTool.client
 
                client.on('connect', function(res) {
                    console.error('连接成功')
                    console.error(res)
                })
                client.on('reconnect', function(res) {
                    console.error('重新连接')
                    console.error(res)
                })
                client.on('error', function(res) {
                    console.info('连接错误')
                    console.error(res)
                })
                client.on('close', function(res) {
 
                    console.error('关闭成功')
                    console.error(res)
 
                })
                client.on('message', function(topic, message, buffer) {
                    uni.$emit(that.$constant.MQTT_TOPIC_MESSAGE, message);
                    //console.error("收到message(总):"+topic)
 
 
                })
            },
        },
        computed: {
            tenantId() {
                const userinfo = uni.getStorageSync('userinfo');
                const tenantid = userinfo.loginTenantId
                return tenantid;
            }
 
        }
    }
</script>
 
<style>
 
</style>