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 } // 个人业务 test() break } // 主线程循环,可做心跳或状态上报 for { time.Sleep(10 * time.Second) } }