deviceMgr.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package service
  2. import (
  3. "fmt"
  4. "golang.org/x/text/encoding/simplifiedchinese"
  5. "golang.org/x/text/transform"
  6. "io"
  7. "net"
  8. "regexp"
  9. "smartIntersection_edge/util/config"
  10. "smartIntersection_edge/util/logger"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. var (
  16. Devices = make(map[string]*Device)
  17. )
  18. type Device struct {
  19. Info *config.Screens
  20. Conn net.Conn
  21. IsLogin bool
  22. LastTime time.Time
  23. SumHighSpeed int
  24. SumLowSpeed int
  25. }
  26. // 实例所有设备
  27. func InitDevices() {
  28. //// 将数据库中的设备全部离线
  29. //screens, _ := dao.QueryAllScreens()
  30. //for _, screen := range screens {
  31. // Devices[screen.Sn] = &Device{Info: screen}
  32. //}
  33. }
  34. func (s *Device) Start(conn net.Conn) {
  35. s.Conn = conn
  36. go s.Process()
  37. }
  38. func (s *Device) Process() {
  39. // 函数执行完之后关闭连接
  40. defer s.Conn.Close()
  41. for {
  42. buf := make([]byte, 256)
  43. // 将tcp连接读取到的数据读取到byte数组中, 返回读取到的byte的数目
  44. n, err := s.Conn.Read(buf)
  45. if err != nil {
  46. // 从客户端读取数据的过程中发生错误 这里如果没读到可以视为设备离线了
  47. logger.Logger.Errorf("read err,dev offLine")
  48. break
  49. }
  50. data := string(buf[:n])
  51. //fmt.Println("读取", string(buf[:n]), time.Now())
  52. if data[2:7] == "login" {
  53. fmt.Println("登录 login*****")
  54. s.Conn.Write([]byte("login:successful"))
  55. //fmt.Println("data sn", data[16:40])
  56. if !s.IsLogin {
  57. topic := MqttService.GetTopic(data[16:40], TopicChanStatus) //登录成功后马上修改在线状态
  58. err := MqttService.Publish(topic, "1")
  59. if err != nil {
  60. continue
  61. }
  62. }
  63. }
  64. if data[2:11] == "heartbeat" { //默认一分钟发一次心跳
  65. s.LastTime = time.Now()
  66. topic := MqttService.GetTopic(data[20:44], TopicReportTime)
  67. err := MqttService.Publish(topic, s.LastTime.String())
  68. if err != nil {
  69. continue
  70. }
  71. if !s.IsLogin {
  72. s.Conn.Write([]byte("{'trans':'on'}")) //开启上传状态 如果要关闭上传状态,把这一行注释即可
  73. screens := FindScreenBySN(data[20:44])
  74. s.Info = screens
  75. s.IsLogin = true
  76. Devices[data[20:44]] = s
  77. //topic := MqttService.GetTopic(s.Info.Sn, TopicChanStatus)
  78. //err := MqttService.Publish(topic, "1")
  79. }
  80. Devices[data[20:44]].Conn = s.Conn
  81. logger.Logger.Infof("%v 设备心跳", s.Info.ScreensName)
  82. }
  83. //判断超速
  84. if strings.Contains(data, `"status":"highspeed"`) {
  85. s.SumLowSpeed = 0
  86. re := regexp.MustCompile(`"speed1":"(\d+)"`)
  87. match := re.FindStringSubmatch(data)
  88. if len(match) > 1 {
  89. speed1, _ := strconv.Atoi(match[1])
  90. if speed1 > 50 { //大于50认为超速
  91. s.SumHighSpeed++
  92. if s.SumHighSpeed >= 3 { //连续三次认为超速
  93. topic := MqttService.GetTopic(s.Info.Sn, TopicHighSpeed)
  94. MqttService.Publish(topic, time.Now().Format(time.RFC3339)) //上报当前超速时间
  95. }
  96. } else {
  97. s.SumHighSpeed = 0
  98. }
  99. } else {
  100. fmt.Println("Speed1 not found")
  101. continue
  102. }
  103. }
  104. //判断低速
  105. if strings.Contains(data, `"status":"normalspeed"`) {
  106. s.SumHighSpeed = 0
  107. re := regexp.MustCompile(`"speed1":"(\d+)"`)
  108. match := re.FindStringSubmatch(data)
  109. if len(match) > 1 {
  110. speed1, _ := strconv.Atoi(match[1])
  111. if speed1 < 10 { //小于10认为低速
  112. s.SumLowSpeed++
  113. if s.SumLowSpeed >= 6 { //连续8次认为低速
  114. topic := MqttService.GetTopic(s.Info.Sn, TopicLowSpeed)
  115. MqttService.Publish(topic, time.Now().Format(time.RFC3339)) //上报当前低速时间
  116. }
  117. } else {
  118. s.SumLowSpeed = 0
  119. }
  120. } else {
  121. fmt.Println("Speed1 not found")
  122. continue
  123. }
  124. }
  125. if strings.Contains(data, `"status":"none"`) {
  126. s.SumHighSpeed = 0
  127. s.SumLowSpeed = 0
  128. }
  129. }
  130. }
  131. func FindScreenBySN(sn string) *config.Screens {
  132. for _, screen := range config.DevConfig.Screens {
  133. if screen != nil && screen.Sn == sn {
  134. return screen
  135. }
  136. }
  137. return nil
  138. }
  139. func UTF8ToGB2312(s string) ([]byte, error) {
  140. reader := transform.NewReader(strings.NewReader(s), simplifiedchinese.GB18030.NewEncoder())
  141. return io.ReadAll(reader)
  142. }