deviceMgr.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package tcp
  2. import (
  3. "encoding/hex"
  4. "errors"
  5. "fmt"
  6. "go.uber.org/zap"
  7. "gorm.io/gorm"
  8. "net"
  9. Dev "server/dao/devices"
  10. deviceDao "server/dao/devices"
  11. promodel "server/model/common/devices"
  12. "server/model/common/request"
  13. "server/protocol"
  14. "server/utils/logger"
  15. "strings"
  16. "time"
  17. )
  18. var (
  19. devices = make(map[string]Device) // 存储连接的全局 map
  20. )
  21. type Device struct {
  22. info deviceDao.Screens
  23. conn net.Conn
  24. lastTime time.Time
  25. stopChan chan struct{} // 添加停止通道
  26. }
  27. func (s *Device) Start(conn net.Conn) {
  28. s.conn = conn
  29. s.stopChan = make(chan struct{})
  30. go s.Process()
  31. go s.Handle()
  32. }
  33. func (s *Device) Process() {
  34. // 函数执行完之后关闭连接
  35. defer s.conn.Close()
  36. for {
  37. buf := make([]byte, 256)
  38. // 将tcp连接读取到的数据读取到byte数组中, 返回读取到的byte的数目
  39. n, err := s.conn.Read(buf)
  40. if err != nil {
  41. // 从客户端读取数据的过程中发生错误 这里如果没读到可以视为设备离线了
  42. logger.Logger.Errorf("read from client failed [离线了], err:%v", err)
  43. break
  44. }
  45. data := hex.EncodeToString(buf[:n])
  46. if !strings.Contains(strings.ToLower(data), "fe5c4b89") {
  47. continue
  48. }
  49. switch data[16:18] {
  50. case "61":
  51. logger.Logger.Debug("登录")
  52. // fe5c4b89 2a000000 62 00000000 17000000 31 23 32303233(年) 3035(月) 3135(日) 3031(星期) 3038(时) 3334(分) 3135(秒) 23 303630(心跳包时间) 23 ffff
  53. buffer := protocol.AuthDataPack{}.AuthLogin()
  54. // 通过sn 查设备是否存在 不存在return 存在就保存con 和 info
  55. screens, err := deviceDao.QueryScreenBySn(data[34:50])
  56. if errors.Is(err, gorm.ErrRecordNotFound) {
  57. logger.Logger.Errorf("Process[case '61'] SN not found \n")
  58. break
  59. }
  60. s.info = screens
  61. devices[s.info.Sn] = *s
  62. s.conn.Write(buffer.Bytes())
  63. case "91":
  64. logger.Logger.Debug("心跳")
  65. //判断内存devices中是否存储了设备(连接)信息
  66. if _, exists := devices[data[34:50]]; !exists {
  67. //不存在
  68. screens, err := deviceDao.QueryScreenBySn(data[34:50])
  69. if errors.Is(err, gorm.ErrRecordNotFound) {
  70. logger.Logger.Errorf("Process[case '91'] SN not found \n")
  71. break
  72. }
  73. s.info = screens
  74. devices[s.info.Sn] = *s
  75. }
  76. if data[34:50] != s.info.Sn {
  77. continue
  78. }
  79. s.UpdateInfo(data[82:84], data[86:88], data[90:92])
  80. default:
  81. fmt.Println("读取:", data)
  82. }
  83. }
  84. }
  85. func (s *Device) Handle() {
  86. defer s.conn.Close()
  87. t2 := time.NewTicker(3 * time.Minute) //在线监测
  88. defer t2.Stop()
  89. for {
  90. select {
  91. case <-t2.C:
  92. state := request.DeviceStatus{
  93. Status: 1,
  94. PlayStatus: 1,
  95. DisplayStatus: 1,
  96. SourceStatus: 1,
  97. }
  98. if time.Now().Add(-2*time.Minute).After(s.lastTime) || s.lastTime.IsZero() {
  99. //离线
  100. state.Status = 0
  101. state.PlayStatus = 0
  102. state.DisplayStatus = 0
  103. state.SourceStatus = 0
  104. err := Dev.UpdateScreensStatus(s.info.ScreensCode, state)
  105. if err != nil {
  106. logger.Logger.Error("[Handle] UpdateScreensStatus err", zap.Error(err))
  107. continue
  108. }
  109. s.conn.Close()
  110. close(s.stopChan) // 通知协程停止
  111. delete(devices, s.info.Sn) //从内存中移除连接
  112. return // 结束 Handle 协程
  113. } else {
  114. logger.Logger.Debugf("%s在线", s.info.ScreensCode)
  115. }
  116. //修改数据库状态
  117. err := Dev.UpdateScreensStatus(s.info.ScreensCode, state)
  118. if err != nil {
  119. logger.Logger.Error("[Handle] UpdateScreensStatus err", zap.Error(err))
  120. continue
  121. }
  122. break
  123. default:
  124. continue
  125. }
  126. }
  127. }
  128. func (s *Device) UpdateInfo(playStatus, displayStatus, sourceStatus string) {
  129. play, display, source := 0, 0, 0
  130. if playStatus == "31" {
  131. play = 1
  132. }
  133. if displayStatus == "30" {
  134. display = 1
  135. }
  136. if sourceStatus == "30" {
  137. source = 1
  138. }
  139. s.info.Status = 1
  140. s.info.PlayStatus = play
  141. s.info.DisplayStatus = display
  142. s.info.SourceStatus = source
  143. s.lastTime = time.Now()
  144. }
  145. func (s *Device) SwitchScreen(onOff int) error {
  146. pack := protocol.SwitchDataPack{Type: 0x52} //熄屏
  147. if onOff == 1 {
  148. pack = protocol.SwitchDataPack{Type: 0x51} //亮屏
  149. }
  150. buf := pack.SwitchScreens() //获取开关屏指令
  151. _, err := s.conn.Write(buf.Bytes())
  152. if err != nil {
  153. logger.Logger.Errorf("SwitchScreen write failed, err:%v", err)
  154. return err
  155. }
  156. return nil
  157. }
  158. func (s *Device) SendInternalCode(content []promodel.InternalCodeContent) error {
  159. pack := protocol.InternalCodeDataPack{}
  160. //获取要写入连接的字节数组
  161. buf := pack.SendInternalCode(content)
  162. _, err := s.conn.Write(buf.Bytes())
  163. if err != nil {
  164. logger.Logger.Errorf("SendInternalCode write failed, err:%v", err)
  165. return err
  166. }
  167. return nil
  168. }
  169. func (s *Device) VoiceBroad(broad string) error {
  170. pack := protocol.VoiceBroadDataPack{}
  171. buf := pack.VoiceBroad(broad)
  172. _, err := s.conn.Write(buf.Bytes())
  173. if err != nil {
  174. logger.Logger.Errorf("VoiceBroad write failed, err:%v", err)
  175. return err
  176. }
  177. return nil
  178. }
  179. func SwitchScreen(sn string, onOff int) error {
  180. device := devices[sn]
  181. err := device.SwitchScreen(onOff)
  182. if err != nil {
  183. return err
  184. }
  185. return nil
  186. }
  187. func SendInternalCode(sn string, content []promodel.InternalCodeContent) error {
  188. device := devices[sn]
  189. err := device.SendInternalCode(content)
  190. if err != nil {
  191. return err
  192. }
  193. return nil
  194. }
  195. func VoiceBroad(sn string, broad string) error {
  196. device := devices[sn]
  197. err := device.VoiceBroad(broad)
  198. if err != nil {
  199. return err
  200. }
  201. return nil
  202. }