-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_connect.go
50 lines (48 loc) · 1.49 KB
/
run_connect.go
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
package IOTMQTTClient
import (
mqtt "github.com/eclipse/paho.mqtt.golang"
BaseConfig "github.com/fotomxq/weeekj_core/v5/base/config"
CoreLog "github.com/fotomxq/weeekj_core/v5/core/log"
)
func runConnect() {
//捕捉异常
defer func() {
if r := recover(); r != nil {
CoreLog.Error("iot device run error, ", r)
}
}()
mqttServerURL, err := BaseConfig.GetDataString("MQTTServerURLTCP")
//绕过url为空的时候,如测试或特殊业务场景,不需要mqtt服务的
if err != nil {
CoreLog.Error("iot mqtt, get mqtt config server url, ", err)
return
}
if mqttServerURL == "" {
//CoreLog.Error("iot mqtt, get mqtt config server url, ", err)
return
}
mqttServerUsername, err := BaseConfig.GetDataString("MQTTServerUsername")
if err != nil {
CoreLog.Error("iot mqtt, get mqtt config server username, ", err)
return
}
mqttServerPassword, err := BaseConfig.GetDataString("MQTTServerPassword")
if err != nil {
CoreLog.Error("iot mqtt, get mqtt config server password, ", err)
return
}
mqttClient.NeedDisConnectOnIsConnect = false
if err := mqttClient.Init(mqttServerURL, mqttServerUsername, mqttServerPassword, mqttPrefix); err != nil {
CoreLog.Error("iot mqtt, connect server, ", err)
return
}
mqttClient.Options.SetOnConnectHandler(func(client mqtt.Client) {
if err := initSub(); err != nil {
CoreLog.Error("iot mqtt, connect init sub, ", err)
}
})
if err = mqttClient.ConnectClient(); err != nil {
CoreLog.Error("iot mqtt, connect failed, ", err)
return
}
}