|
@@ -4,8 +4,11 @@ import (
|
|
|
"fmt"
|
|
"fmt"
|
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap"
|
|
|
"net"
|
|
"net"
|
|
|
|
|
+ "regexp"
|
|
|
"server/dao"
|
|
"server/dao"
|
|
|
"server/global"
|
|
"server/global"
|
|
|
|
|
+ "strconv"
|
|
|
|
|
+ "strings"
|
|
|
"time"
|
|
"time"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
@@ -14,10 +17,12 @@ var (
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type Device struct {
|
|
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" { //默认一分钟发一次心跳
|
|
if data[2:11] == "heartbeat" { //默认一分钟发一次心跳
|
|
|
s.LastTime = time.Now()
|
|
s.LastTime = time.Now()
|
|
|
if !s.IsLogin {
|
|
if !s.IsLogin {
|
|
|
|
|
+ s.Conn.Write([]byte("{'trans':'on'}")) //开启上传状态
|
|
|
screen, _ := dao.QueryScreensBySn(data[20:44])
|
|
screen, _ := dao.QueryScreensBySn(data[20:44])
|
|
|
s.Info = screen
|
|
s.Info = screen
|
|
|
s.IsLogin = true
|
|
s.IsLogin = true
|
|
|
Devices[data[20:44]] = s
|
|
Devices[data[20:44]] = s
|
|
|
_ = dao.UpdateScreensStatusBySn(s.Info.Sn, 1)
|
|
_ = dao.UpdateScreensStatusBySn(s.Info.Sn, 1)
|
|
|
} //更新上次发送心跳时间
|
|
} //更新上次发送心跳时间
|
|
|
|
|
+ Devices[data[20:44]].Conn = s.Conn
|
|
|
global.GVA_LOG.Info("设备心跳", zap.String("ScreensName", s.Info.ScreensName))
|
|
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() {
|
|
func IsOnline() {
|
|
|
- t := time.NewTicker(1 * time.Minute) //每分钟
|
|
|
|
|
|
|
+ t := time.NewTicker(5 * time.Second) //每分钟
|
|
|
for {
|
|
for {
|
|
|
select {
|
|
select {
|
|
|
case <-t.C:
|
|
case <-t.C:
|
|
|
for _, device := range Devices {
|
|
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() {
|
|
if time.Now().Add(-2*time.Minute).After(device.LastTime) || device.LastTime.IsZero() {
|
|
|
state := 0
|
|
state := 0
|