重新整理物联网服务,分离业务逻辑。
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"sentinel/pkg/config"
|
||||
"sentinel/pkg/device"
|
||||
"sentinel/pkg/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
banner := `
|
||||
==========================================================================
|
||||
|
||||
██████ █████ █████ ██████ ██ █████ ██████ ██ ██ ██████
|
||||
██ ██ ██ ██ ██ ████ ██ ██ ███ ██ ██
|
||||
█████ ██ ██ ██ ██ █████ ██ ██ ██ ██ █████ ████ ██ ██
|
||||
██ ██ ██ ██ ██ ██ ██████ ██ ██ ██ ██ ████ ██
|
||||
██████ █████ █████ ██████ ██ ██ █████ ██████ ██ ██ ██
|
||||
|
||||
==========================================================================
|
||||
`
|
||||
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(config.MQTT_BROKER, deviceID, deviceID, config.PASSWORD, 60)
|
||||
err := mqttSvc.Connect()
|
||||
if err != nil {
|
||||
if firstFail {
|
||||
log.Error("物联网服务连接失败,如未注册设备,请先注册: " + deviceID)
|
||||
firstFail = false
|
||||
}
|
||||
time.Sleep(3 * time.Second) // 5秒后重试
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
defer mqttSvc.Close()
|
||||
|
||||
biz := NewBusinessService(mqttSvc, deviceID)
|
||||
for {
|
||||
// MQTT业务
|
||||
err := biz.Start()
|
||||
if err != nil {
|
||||
log.Error("business service start failed: " + err.Error())
|
||||
fmt.Println("业务启动失败,5秒后重试...")
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// 主线程循环,可做心跳或状态上报
|
||||
for {
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user