完善自我更新逻辑
This commit is contained in:
+21
-13
@@ -2,41 +2,49 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sentinel/pkg/utils"
|
||||
"time"
|
||||
|
||||
"sentinel/pkg/config"
|
||||
"sentinel/pkg/device"
|
||||
"sentinel/pkg/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
deviceID := device.GetDeviceID()
|
||||
log.Init(utils.Log_file_dic) // 初始化日志目录
|
||||
log.Info("Device id: " + deviceID) // 第一次启动记录
|
||||
banner := `
|
||||
==========================================================================
|
||||
_______ _______ _ __________________ _ _______ _
|
||||
( ____ \( ____ \( ( /|\__ __/\__ __/( ( /|( ____ \( \
|
||||
| (_____ | (__ | \ | | | | | | | \ | || (__ | |
|
||||
(_____ )| __) | (\ \) | | | | | | (\ \) || __) | |
|
||||
/\____) || (____/\| ) \ | | | ___) (___| ) \ || (____/\| (____/\
|
||||
\_______)(_______/|/ )_) )_( \_______/|/ )_)(_______/(_______/
|
||||
==========================================================================
|
||||
`
|
||||
|
||||
broker := fmt.Sprintf("tls://%s:%d", utils.MQTT_HOST, utils.MQTT_PORT)
|
||||
username := deviceID
|
||||
password := utils.PASSWORD
|
||||
fmt.Println(banner)
|
||||
deviceID := device.GetDeviceID()
|
||||
log.Init(config.Log_file_dic) // 初始化日志目录
|
||||
log.Info("Device id: " + deviceID) // 第一次启动记录
|
||||
log.Println("版本号: ", config.APP_VERSION) // 第一次启动记录
|
||||
|
||||
var mqttSvc *MQTTService
|
||||
firstFail := true // 标记是否第一次失败
|
||||
for {
|
||||
mqttSvc = NewMQTTService(broker, username, username, password, 60)
|
||||
mqttSvc = NewMQTTService(config.MQTT_BROKER, deviceID, deviceID, config.PASSWORD, 60)
|
||||
err := mqttSvc.Connect()
|
||||
if err != nil {
|
||||
if firstFail {
|
||||
log.Error("物联网服务连接失败,请先注册设备. DeviceID: " + deviceID + " ")
|
||||
log.Error("物联网服务连接失败,如未注册设备,请先注册: " + deviceID)
|
||||
firstFail = false
|
||||
}
|
||||
time.Sleep(5 * time.Second) // 5秒后重试
|
||||
time.Sleep(3 * time.Second) // 5秒后重试
|
||||
continue
|
||||
}
|
||||
log.Info("物联网服务已启动")
|
||||
break
|
||||
}
|
||||
defer mqttSvc.Close()
|
||||
|
||||
biz := NewBusinessService(mqttSvc, utils.PROJECT, utils.DEVICE_TPYE, deviceID)
|
||||
biz := NewBusinessService(mqttSvc, deviceID)
|
||||
for {
|
||||
// MQTT业务
|
||||
err := biz.Start()
|
||||
@@ -47,7 +55,7 @@ func main() {
|
||||
continue
|
||||
}
|
||||
// 个人业务
|
||||
test()
|
||||
//test()
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user