Procházet zdrojové kódy

离线在线状态调优

chengqian před 4 měsíci
rodič
revize
e582b320f1
5 změnil soubory, kde provedl 25 přidání a 9 odebrání
  1. binární
      build/smart_intersectionV2.0
  2. 1 2
      main.go
  3. 16 6
      service/deviceMgr.go
  4. 6 1
      service/mqtt_handle.go
  5. 2 0
      timer/task.go

binární
build/smart_intersectionV2.0


+ 1 - 2
main.go

@@ -2,7 +2,6 @@ package main
 
 import (
 	"smartIntersection_edge/service"
-	"smartIntersection_edge/timer"
 	"smartIntersection_edge/util/config"
 	"smartIntersection_edge/util/logger"
 )
@@ -20,7 +19,7 @@ func main() {
 	}
 
 	go service.ListenTcp() //监听tcp
-	go timer.IsOnline()    //检测在线
+	//go timer.IsOnline()    //检测在线
 
 	select {}
 }

+ 16 - 6
service/deviceMgr.go

@@ -58,21 +58,31 @@ func (s *Device) Process() {
 		if data[2:7] == "login" {
 			fmt.Println("登录 login*****")
 			s.Conn.Write([]byte("login:successful"))
+			//fmt.Println("data sn", data[16:40])
+			if !s.IsLogin {
+				topic := MqttService.GetTopic(data[16:40], TopicChanStatus) //登录成功后马上修改在线状态
+				err := MqttService.Publish(topic, "1")
+				if err != nil {
+					continue
+				}
+			}
 		}
 
 		if data[2:11] == "heartbeat" { //默认一分钟发一次心跳
 			s.LastTime = time.Now()
+			topic := MqttService.GetTopic(data[20:44], TopicReportTime)
+			err := MqttService.Publish(topic, s.LastTime.String())
+			if err != nil {
+				continue
+			}
 			if !s.IsLogin {
-				s.Conn.Write([]byte("{'trans':'on'}")) //开启上传状态
+				s.Conn.Write([]byte("{'trans':'on'}")) //开启上传状态 如果要关闭上传状态,把这一行注释即可
 				screens := FindScreenBySN(data[20:44])
 				s.Info = screens
 				s.IsLogin = true
 				Devices[data[20:44]] = s
-				topic := MqttService.GetTopic(s.Info.Sn, TopicChanStatus)
-				err := MqttService.Publish(topic, "1")
-				if err != nil {
-					continue
-				}
+				//topic := MqttService.GetTopic(s.Info.Sn, TopicChanStatus)
+				//err := MqttService.Publish(topic, "1")
 			}
 			Devices[data[20:44]].Conn = s.Conn
 			logger.Logger.Infof("%v 设备心跳", s.Info.ScreensName)

+ 6 - 1
service/mqtt_handle.go

@@ -84,6 +84,10 @@ func (o *MqttHandler) Handler() interface{} {
 			if n <= 0 && err != nil {
 				logger.Logger.Errorf("设备:%v操作失败,错误:%v", sn, err)
 			}
+
+		case TopicOffline:
+			//删除内存中的离线设备
+			delete(Devices, m.PayloadString())
 		}
 	}
 }
@@ -110,6 +114,7 @@ const (
 	TopicChanStatus = "chanStatus" //上报状态
 	TopicHighSpeed  = "highSpeed"  //超速时
 	TopicLowSpeed   = "lowSpeed"   //低速时
-
 	TopicSetControl = "setControl" //云台下发控制
+	TopicReportTime = "reportTime" //上报在线时间
+	TopicOffline    = "Offline"    //离线消息
 )

+ 2 - 0
timer/task.go

@@ -1,6 +1,7 @@
 package timer
 
 import (
+	"fmt"
 	"smartIntersection_edge/service"
 	"smartIntersection_edge/util/logger"
 	"time"
@@ -11,6 +12,7 @@ func IsOnline() {
 	for {
 		select {
 		case <-t.C:
+			fmt.Println("设备长度:", len(service.Devices))
 			for _, device := range service.Devices {
 				//符合条件
 				if time.Now().Add(-2*time.Minute).After(device.LastTime) || device.LastTime.IsZero() {