59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"sentinel/pkg/utils"
|
|
"time"
|
|
|
|
"sentinel/pkg/device"
|
|
"sentinel/pkg/log"
|
|
)
|
|
|
|
func main() {
|
|
deviceID := device.GetDeviceID()
|
|
log.Init(utils.Log_file_dic) // 初始化日志目录
|
|
log.Info("Device id: " + deviceID) // 第一次启动记录
|
|
|
|
broker := fmt.Sprintf("tls://%s:%d", utils.MQTT_HOST, utils.MQTT_PORT)
|
|
username := deviceID
|
|
password := utils.PASSWORD
|
|
|
|
var mqttSvc *MQTTService
|
|
firstFail := true // 标记是否第一次失败
|
|
for {
|
|
mqttSvc = NewMQTTService(broker, username, username, password, 60)
|
|
err := mqttSvc.Connect()
|
|
if err != nil {
|
|
if firstFail {
|
|
log.Error("物联网服务连接失败,请先注册设备. DeviceID: " + deviceID + " ")
|
|
firstFail = false
|
|
}
|
|
time.Sleep(5 * time.Second) // 5秒后重试
|
|
continue
|
|
}
|
|
log.Info("物联网服务已启动")
|
|
break
|
|
}
|
|
defer mqttSvc.Close()
|
|
|
|
biz := NewBusinessService(mqttSvc, utils.PROJECT, utils.DEVICE_TPYE, 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
|
|
}
|
|
// 个人业务
|
|
test()
|
|
break
|
|
}
|
|
|
|
// 主线程循环,可做心跳或状态上报
|
|
for {
|
|
time.Sleep(10 * time.Second)
|
|
}
|
|
}
|