deviceMgr.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package tcp
  2. import (
  3. "fmt"
  4. "go.uber.org/zap"
  5. "golang.org/x/text/encoding/simplifiedchinese"
  6. "golang.org/x/text/transform"
  7. "io"
  8. "net"
  9. "regexp"
  10. "server/dao"
  11. "server/global"
  12. "server/service/devices"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. var (
  18. Devices = make(map[string]*Device) // 存储连接的全局 map
  19. )
  20. type Device struct {
  21. Info dao.Screens
  22. Conn net.Conn
  23. IsLogin bool
  24. LastTime time.Time
  25. SumHighSpeed int
  26. SumLowSpeed int
  27. }
  28. // 实例所有设备
  29. func InitDevices() {
  30. //// 将数据库中的设备全部离线
  31. //screens, _ := dao.QueryAllScreens()
  32. //for _, screen := range screens {
  33. // Devices[screen.Sn] = &Device{Info: screen}
  34. //}
  35. }
  36. func (s *Device) Start(conn net.Conn) {
  37. s.Conn = conn
  38. go s.Process()
  39. }
  40. func (s *Device) Process() {
  41. // 函数执行完之后关闭连接
  42. defer s.Conn.Close()
  43. for {
  44. buf := make([]byte, 256)
  45. // 将tcp连接读取到的数据读取到byte数组中, 返回读取到的byte的数目
  46. n, err := s.Conn.Read(buf)
  47. if err != nil {
  48. // 从客户端读取数据的过程中发生错误 这里如果没读到可以视为设备离线了
  49. global.GVA_LOG.Error("read err,dev offLine")
  50. break
  51. }
  52. data := string(buf[:n])
  53. fmt.Println("读取", string(buf[:n]))
  54. if data[2:7] == "login" {
  55. s.Conn.Write([]byte("login:successful"))
  56. }
  57. if data[2:11] == "heartbeat" { //默认一分钟发一次心跳
  58. s.LastTime = time.Now()
  59. if !s.IsLogin {
  60. s.Conn.Write([]byte("{'trans':'on'}")) //开启上传状态
  61. screen, _ := dao.QueryScreensBySn(data[20:44])
  62. s.Info = screen
  63. s.IsLogin = true
  64. Devices[data[20:44]] = s
  65. _ = dao.UpdateScreensStatusBySn(s.Info.Sn, 1)
  66. } //更新上次发送心跳时间
  67. Devices[data[20:44]].Conn = s.Conn
  68. global.GVA_LOG.Info("设备心跳", zap.String("ScreensName", s.Info.ScreensName))
  69. }
  70. if strings.Contains(data, `"status":"highspeed"`) {
  71. s.SumLowSpeed = 0
  72. re := regexp.MustCompile(`"speed1":"(\d+)"`)
  73. match := re.FindStringSubmatch(data)
  74. if len(match) > 1 {
  75. speed1, _ := strconv.Atoi(match[1])
  76. if speed1 > 30 { //大于40认为超速
  77. s.SumHighSpeed++
  78. if s.SumHighSpeed >= 3 {
  79. fmt.Println("********************************************************************超速了")
  80. }
  81. } else {
  82. s.SumHighSpeed = 0
  83. }
  84. } else {
  85. fmt.Println("Speed1 not found")
  86. continue
  87. }
  88. }
  89. //判断低速
  90. if strings.Contains(data, `"status":"normalspeed"`) {
  91. s.SumHighSpeed = 0
  92. re := regexp.MustCompile(`"speed1":"(\d+)"`)
  93. match := re.FindStringSubmatch(data)
  94. if len(match) > 1 {
  95. speed1, _ := strconv.Atoi(match[1])
  96. if speed1 < 20 { //小于10认为低速
  97. s.SumLowSpeed++
  98. if s.SumLowSpeed >= 8 {
  99. fmt.Println("********************************************************************低低速了")
  100. }
  101. } else {
  102. s.SumLowSpeed = 0
  103. }
  104. } else {
  105. fmt.Println("Speed1 not found")
  106. continue
  107. }
  108. }
  109. //if strings.Contains(data, `"status":"none"`) {
  110. // s.SumHighSpeed = 0
  111. // s.SumLowSpeed = 0
  112. //}
  113. }
  114. }
  115. func IsOnline() {
  116. t := time.NewTicker(5 * time.Second) //每分钟
  117. for {
  118. select {
  119. case <-t.C:
  120. topic := devices.MqttService.GetTopic("071995171560000000c40808", devices.TopicChanStatus)
  121. err := devices.MqttService.Publish(topic, "1")
  122. if err != nil {
  123. fmt.Println(err.Error())
  124. }
  125. fmt.Println("发送了。。。。。。。。。。。。。。。。。。。。。。")
  126. for _, device := range Devices {
  127. //commond := `{
  128. // "setdiscontent0":{
  129. // "num":"7",
  130. // "effect":"11",
  131. // "speed":"3",
  132. // "stay":"5",
  133. // "total":"100",
  134. // "color":"3",
  135. // "content":"[24M]哈哈哈哈"
  136. // }
  137. //}`
  138. //conyuyin := `{"audionumset0":{"num0":"21","num1":"[m0]前方路口,注意来车","num2":"[m1]您已超速,请减速","num3":"[m0]对向来车,请减速","num4":"[m0]双向来车,请减速"}}`
  139. //consy := ``
  140. //gb2312, _ := UTF8ToGB2312(conyuyin)
  141. //n, err := Devices[device.Info.Sn].Conn.Write(gb2312)
  142. //fmt.Println("我是n,", n)
  143. //if err != nil {
  144. // fmt.Println("err:", err)
  145. //}
  146. //fmt.Println("发送了。。。。。。。。。。。。。。。。。。。。。。")
  147. //符合条件
  148. if time.Now().Add(-2*time.Minute).After(device.LastTime) || device.LastTime.IsZero() {
  149. state := 0
  150. err := dao.UpdateScreensStatusBySn(device.Info.Sn, state)
  151. delete(Devices, device.Info.Sn)
  152. if err != nil {
  153. continue
  154. }
  155. device.Info.Status = 0
  156. global.GVA_LOG.Info("设备离线了", zap.String("ScreensName", device.Info.ScreensName))
  157. }
  158. }
  159. }
  160. }
  161. }
  162. func UTF8ToGB2312(s string) ([]byte, error) {
  163. reader := transform.NewReader(strings.NewReader(s), simplifiedchinese.GB18030.NewEncoder())
  164. return io.ReadAll(reader)
  165. }