Prechádzať zdrojové kódy

在线状态bug优化

chengqian 4 mesiacov pred
rodič
commit
21697cb817

BIN
server/build/smart_intersectionV2.0


+ 32 - 2
server/initialize/timer.go

@@ -2,9 +2,12 @@ package initialize
 
 import (
 	"fmt"
-	"server/task"
-
 	"github.com/robfig/cron/v3"
+	"server/dao"
+	"server/service/devices"
+	"server/task"
+	"server/utils/protocol"
+	"time"
 
 	"server/global"
 )
@@ -34,4 +37,31 @@ func Timer() {
 		//	fmt.Println("add timer error:", err)
 		//}
 	}()
+
+	go IsOnline() //监测在线状态
+
+}
+
+func IsOnline() {
+	t := time.NewTicker(1 * time.Minute) //每分钟
+	for {
+		select {
+		case <-t.C:
+			for sn, device := range devices.DeviceOnlineTimes {
+				fmt.Printf("定时 %v", device)
+				fmt.Println("判断:", time.Now().Add(-2*time.Minute).After(*device))
+				//符合条件
+				if time.Now().Add(-2 * time.Minute).After(*device) {
+					err := dao.UpdateScreensStatusBySn(sn, 0)
+					if err != nil {
+						global.GVA_LOG.Error(fmt.Sprintf("修改屏幕状态失败:%s", err.Error()))
+					}
+					topic := devices.MqttService.GetTopic(sn, protocol.TopicOffline)
+					_ = devices.MqttService.Publish(topic, sn)
+					delete(devices.DeviceOnlineTimes, sn)
+					global.GVA_LOG.Info(fmt.Sprintf("设备【sn】%v 离线了", sn))
+				}
+			}
+		}
+	}
 }

+ 11 - 0
server/service/devices/mqtt.go

@@ -27,6 +27,7 @@ var MqttService *MqttHandler
 var timeoutReg = regexp.MustCompile("Client .* has exceeded timeout")
 var connectReg = regexp.MustCompile(`New client connected from .* as .*\(`)
 var disconnectReg = regexp.MustCompile("Client mqttx_893e4b7d disconnected")
+var DeviceOnlineTimes = make(map[string]*time.Time)
 
 type MqttHandler struct {
 	queue *mqtt.MlQueue
@@ -106,6 +107,16 @@ func (o *MqttHandler) Handler() interface{} {
 			if err != nil {
 				global.GVA_LOG.Error(fmt.Sprintf("修改屏幕状态失败:%s", err.Error()))
 			}
+		case protocol.TopicReportTime:
+			parts := strings.Split(m.PayloadString(), " m=")
+			cleanTimeStr := parts[0]
+			// 时间格式
+			layout := "2006-01-02 15:04:05.999999999 +0000 UTC"
+
+			// 解析时间
+			parsedTime, _ := time.Parse(layout, cleanTimeStr)
+			//fmt.Println("转换时间:", parsedTime)
+			DeviceOnlineTimes[sn] = &parsedTime
 		}
 
 	}

+ 2 - 1
server/utils/protocol/protocol.go

@@ -4,6 +4,7 @@ const (
 	TopicChanStatus = "chanStatus" //上报状态
 	TopicHighSpeed  = "highSpeed"  //超速时
 	TopicLowSpeed   = "lowSpeed"   //低速时
-
 	TopicSetControl = "setControl" //云台下发控制
+	TopicReportTime = "reportTime" //上报在线时间
+	TopicOffline    = "Offline"    //上报在线时间
 )