chengqian 5 mēneši atpakaļ
vecāks
revīzija
cf4c6d477c
4 mainītis faili ar 110 papildinājumiem un 8 dzēšanām
  1. 12 0
      server/dao/dev_event.go
  2. 2 3
      server/main.go
  3. 76 5
      server/service/tcp/deviceMgr.go
  4. 20 0
      server/validator_test.go

+ 12 - 0
server/dao/dev_event.go

@@ -0,0 +1,12 @@
+package dao
+
+type Event struct {
+	ID   uint   `json:"ID"`
+	Sn   string `json:"Sn"`   //雷达sn(实际上是LED的SN)
+	Type string `json:"Type"` //事件类型
+	Time string `json:"Time"` //触发事件的时间
+}
+
+func (Event) TableName() string {
+	return "dev_event"
+}

+ 2 - 3
server/main.go

@@ -6,7 +6,6 @@ import (
 	"server/core"
 	"server/global"
 	"server/initialize"
-	"server/service/tcp"
 )
 
 //go:generate go env -w GO111MODULE=on
@@ -36,7 +35,7 @@ func main() {
 		defer db.Close()
 	}
 	//tcp.InitDevices()
-	go tcp.ListenTcp()
-	go tcp.IsOnline()
+	//go tcp.ListenTcp()
+	//go tcp.IsOnline()
 	core.RunWindowsServer()
 }

+ 76 - 5
server/service/tcp/deviceMgr.go

@@ -4,8 +4,11 @@ import (
 	"fmt"
 	"go.uber.org/zap"
 	"net"
+	"regexp"
 	"server/dao"
 	"server/global"
+	"strconv"
+	"strings"
 	"time"
 )
 
@@ -14,10 +17,12 @@ var (
 )
 
 type Device struct {
-	Info     dao.Screens
-	Conn     net.Conn
-	IsLogin  bool
-	LastTime time.Time
+	Info         dao.Screens
+	Conn         net.Conn
+	IsLogin      bool
+	LastTime     time.Time
+	SumHighSpeed int
+	SumLowSpeed  int
 }
 
 // 实例所有设备
@@ -55,24 +60,90 @@ func (s *Device) Process() {
 		if data[2:11] == "heartbeat" { //默认一分钟发一次心跳
 			s.LastTime = time.Now()
 			if !s.IsLogin {
+				s.Conn.Write([]byte("{'trans':'on'}")) //开启上传状态
 				screen, _ := dao.QueryScreensBySn(data[20:44])
 				s.Info = screen
 				s.IsLogin = true
 				Devices[data[20:44]] = s
 				_ = dao.UpdateScreensStatusBySn(s.Info.Sn, 1)
 			} //更新上次发送心跳时间
+			Devices[data[20:44]].Conn = s.Conn
 			global.GVA_LOG.Info("设备心跳", zap.String("ScreensName", s.Info.ScreensName))
 		}
 
+		if strings.Contains(data, `"status":"highspeed"`) {
+			s.SumLowSpeed = 0
+			re := regexp.MustCompile(`"speed1":"(\d+)"`)
+			match := re.FindStringSubmatch(data)
+			if len(match) > 1 {
+				speed1, _ := strconv.Atoi(match[1])
+				if speed1 > 30 { //大于40认为超速
+					s.SumHighSpeed++
+					if s.SumHighSpeed >= 3 {
+						fmt.Println("********************************************************************超速了")
+					}
+				} else {
+					s.SumHighSpeed = 0
+				}
+			} else {
+				fmt.Println("Speed1 not found")
+				continue
+			}
+		}
+
+		//判断低速
+		if strings.Contains(data, `"status":"normalspeed"`) {
+			s.SumHighSpeed = 0
+			re := regexp.MustCompile(`"speed1":"(\d+)"`)
+			match := re.FindStringSubmatch(data)
+			if len(match) > 1 {
+				speed1, _ := strconv.Atoi(match[1])
+				if speed1 < 20 { //小于10认为低速
+					s.SumLowSpeed++
+
+					if s.SumLowSpeed >= 8 {
+						fmt.Println("********************************************************************低低速了")
+					}
+				} else {
+					s.SumLowSpeed = 0
+				}
+			} else {
+				fmt.Println("Speed1 not found")
+				continue
+			}
+		}
+
+		//if strings.Contains(data, `"status":"none"`) {
+		//	s.SumHighSpeed = 0
+		//	s.SumLowSpeed = 0
+		//}
+
 	}
 }
 
 func IsOnline() {
-	t := time.NewTicker(1 * time.Minute) //每分钟
+	t := time.NewTicker(5 * time.Second) //每分钟
 	for {
 		select {
 		case <-t.C:
 			for _, device := range Devices {
+				commond := `{
+  "setdiscontent0": {
+    "num": "5",
+    "effect": "11",
+    "speed": "3",
+    "stay": "5",
+    "total": "100",
+    "color": "1",
+    "content": "[24M]abc"
+  }
+}`
+				n, err := Devices[device.Info.Sn].Conn.Write([]byte(commond))
+				fmt.Println("我是n,", n)
+				if err != nil {
+					fmt.Println("err:", err)
+				}
+				fmt.Println("发送了。。。。。。。。。。。。。。。。。。。。。。")
 				//符合条件
 				if time.Now().Add(-2*time.Minute).After(device.LastTime) || device.LastTime.IsZero() {
 					state := 0

+ 20 - 0
server/validator_test.go

@@ -0,0 +1,20 @@
+package main
+
+import (
+	"fmt"
+	"regexp"
+	"testing"
+)
+
+func TestVerify(t *testing.T) {
+	// 输入的JSON字符串
+	data := `{"id":"071995171560000000c40808","status":"highspeed","speed1":"35","speed2":"0","realtime":"2025-6-18 10:11:25","audio":"[m1]���ѳ��٣�������","vol":"6","play":"run","dis_content":"[pic3]#[pic4]","workmode":"normal"}`
+
+	// 使用正则表达式提取 "speed1" 后面的数字
+	re := regexp.MustCompile(`"speed1":"(\d+)"`)
+	match := re.FindStringSubmatch(data)
+
+	speed1 := match[1]
+	fmt.Println("Speed1:", speed1)
+
+}