lgq
2026-03-31 e491cdb48129752324c4e3764f99bd9203c56dec
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
//build:20240524
//用于简化mqtt组件微光通信协议的使用,把mqtt封装在这个worker里,使用者只需要订阅eventcenter的事件就可以监听mqtt
import log from './dxLogger.js'
import net from './dxNet.js'
import mqtt from './dxMqtt.js'
import dxMap from './dxMap.js'
import std from './dxStd.js'
import * as os from "os";
const map = dxMap.get('default')
const id = "{{id}}"
const options = map.get("__mqtt__run_init" + id)
let connected = false
function run() {
    mqtt.init(options.mqttAddr, options.clientId, options.username, options.password, options.prefix, options.qos, options.willTopic, options.willMessage, options.id)
    log.info('mqtt start......,id =', id)
    os.sleep(2000)//等待2秒
 
    __bus.on(mqtt.RECONNECT, (options) => {
        mqtt.destroy(options.id)
        mqtt.init(options.mqttAddr, options.clientId, options.username, options.password, options.prefix, options.qos, options.willTopic, options.willMessage, options.id)
    })
 
    std.setInterval(() => {
        try {
            if (mqtt.isConnected(options.id) && net.getStatus().connected) {
                if (!connected) {
                    _fireChange(true)
                    if (options.subs) {
                        mqtt.subscribes(options.subs, options.qos, options.id)
                    }
                }
            } else {
                if (connected) {
                    _fireChange(false)
                }
                // 重连
                mqtt.reconnect(options.willTopic, options.willMessage, options.id)
                os.sleep(2000)//重连后等待2秒
            }
        } catch (error) {
            log.error(error)
        }
    }, 3000)
    std.setInterval(() => {
        // 连接成功后进入消息监听
        if (connected) {
            if (!mqtt.msgIsEmpty(options.id)) {
                let msg = mqtt.receive(options.id)
                __bus.fire(mqtt.RECEIVE_MSG + options.id, msg)//bus.newworker的时候会import eventbus as __bus
            }
        }
    }, 10);
}
 
try {
    run()
} catch (error) {
    log.error(error)
}
 
function _fireChange(status) {
    __bus.fire(mqtt.CONNECTED_CHANGED + options.id, status ? 'connected' : 'disconnected')//bus.newworker的时候会import eventbus as __bus
    connected = status
}