datamgr.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package main
  2. import (
  3. "context"
  4. "runtime/debug"
  5. "sync"
  6. "time"
  7. "github.com/sirupsen/logrus"
  8. "lc/common/mqtt"
  9. "lc/common/protocol"
  10. "lc/common/util"
  11. )
  12. var _onceITSDeviceMgr sync.Once
  13. var _singleITSDeviceMgr *ITSDeviceMgr
  14. func GetITSDeviceMgr() *ITSDeviceMgr {
  15. _onceITSDeviceMgr.Do(func() {
  16. ctx, cancel := context.WithCancel(context.Background())
  17. _singleITSDeviceMgr = &ITSDeviceMgr{
  18. Queue: util.NewQueue(2000),
  19. MapITSDevice: make(map[string]*ITSDevice),
  20. ctx: ctx,
  21. cancel: cancel,
  22. downQueue: util.NewQueue(100),
  23. mapTopicHandle: make(map[string]func(m mqtt.Message)),
  24. }
  25. })
  26. return _singleITSDeviceMgr
  27. }
  28. type ITSDeviceMgr struct {
  29. Queue *util.MlQueue //数据队列,来自SDK
  30. MapITSDevice map[string]*ITSDevice //TollgateID->ITSDevice
  31. logouts []string //退出登陆的userid
  32. logoutsmu sync.Mutex
  33. ctx context.Context
  34. cancel context.CancelFunc
  35. downQueue *util.MlQueue
  36. mapTopicHandle map[string]func(m mqtt.Message)
  37. }
  38. func (o *ITSDeviceMgr) LoadITSDevice() {
  39. for _, v := range itsConfig.Its {
  40. o.MapITSDevice[v.TollgateID] = NewITSDevice(v.TollgateID, v.ID, v.User, v.Password, v.IP, v.Port)
  41. }
  42. o.mapTopicHandle[GetTopic(protocol.DT_GATEWAY, appConfig.GID, protocol.TP_GW_ITS)] = o.HandleTpGwIts
  43. GetMQTTMgr().Subscribe(GetTopic(protocol.DT_GATEWAY, appConfig.GID, protocol.TP_GW_ITS), mqtt.AtMostOnce, o.HandleMessage, ToAll)
  44. }
  45. func (o *ITSDeviceMgr) HandleMessage(m mqtt.Message) {
  46. o.downQueue.Put(m)
  47. }
  48. // HandleTpGwIts 上报配置信息
  49. func (o *ITSDeviceMgr) HandleTpGwIts(m mqtt.Message) {
  50. var obj protocol.Pack_IDObject
  51. if err := obj.DeCode(m.PayloadString()); err != nil {
  52. return
  53. }
  54. if obj.Gid != appConfig.GID {
  55. return
  56. }
  57. var ret protocol.Pack_ITSDev
  58. if str, err := ret.EnCode(appConfig.GID, obj.Seq, &itsConfig); err == nil {
  59. GetMQTTMgr().Publish(GetTopic(protocol.DT_GATEWAY, appConfig.GID, protocol.TP_GW_ITS_ACK), str, 0, ToCloud)
  60. }
  61. }
  62. func (o *ITSDeviceMgr) CheckOnline(dev *ITSDevice, online uint8) {
  63. var obj protocol.Pack_IPCState
  64. if str, err := obj.EnCode(dev.CamID, appConfig.GID, GetNextUint64(), util.MlNow(), online); err == nil {
  65. GetMQTTMgr().Publish(GetTopic(protocol.DT_ITS, dev.CamID, protocol.TP_ITSDEV_STATE), str, mqtt.AtMostOnce, ToCloud)
  66. }
  67. }
  68. func (o *ITSDeviceMgr) CheckLogin() {
  69. for _, v := range o.MapITSDevice {
  70. if v.UserID != "" {
  71. o.CheckOnline(v, protocol.SUCCESS)
  72. continue
  73. }
  74. if err := v.Login(); err == nil {
  75. logrus.Infof("抓拍单元[%s]SDK登陆成功", v.CamID)
  76. if bdi, err := v.GetDeviceStatus(); err == nil {
  77. logrus.Infof("获取抓拍单元[%s]信息成功,信息内容:%v", v.CamID, bdi)
  78. } else {
  79. logrus.Errorf("获取抓拍单元[%s]信息失败:%s", v.CamID, err.Error())
  80. }
  81. if err := v.SetPicStreamDataCallback(); err != nil {
  82. logrus.Errorf("抓拍单元[%s]执行SetPicStreamDataCallback失败:%s", v.CamID, err.Error())
  83. }
  84. //if err := v.Reboot(); err != nil {
  85. // logrus.Errorf("设备[%s]重启失败:%s", err.Error())
  86. //}
  87. v.Reconn = 0
  88. o.CheckOnline(v, protocol.SUCCESS)
  89. } else {
  90. v.Reconn++
  91. o.CheckOnline(v, protocol.FAILED)
  92. logrus.Errorf("抓拍单元[%s]SDK登陆失败:%s,已重连%d次", v.CamID, err.Error(), v.Reconn)
  93. }
  94. }
  95. }
  96. func (o *ITSDeviceMgr) HandleLogout() {
  97. o.logoutsmu.Lock()
  98. defer o.logoutsmu.Unlock()
  99. for _, v := range o.logouts {
  100. for _, dev := range o.MapITSDevice {
  101. if v == dev.UserID {
  102. dev.StopMediaStream()
  103. dev.UserID = ""
  104. logrus.Warnf("%s logout.", dev.CamID)
  105. break
  106. }
  107. }
  108. }
  109. o.logouts = nil
  110. }
  111. func (o *ITSDeviceMgr) NotifyLogout(userid string) {
  112. o.logoutsmu.Lock()
  113. defer o.logoutsmu.Unlock()
  114. o.logouts = append(o.logouts, userid)
  115. }
  116. func (o *ITSDeviceMgr) PushData(data *tagVehicleInfo) {
  117. o.Queue.Put(data)
  118. }
  119. func (o *ITSDeviceMgr) HandleData(data *tagVehicleInfo) {
  120. s, ok := o.MapITSDevice[data.TollgateID]
  121. if ok {
  122. data.CamID = s.CamID
  123. s.ToStatic(data)
  124. }
  125. GetWebSvr().Update(data)
  126. if itsConfig.RemotePub > 0 {
  127. o.RemotePub(data)
  128. }
  129. }
  130. func (o *ITSDeviceMgr) SendData(t time.Time) {
  131. for _, v := range o.MapITSDevice {
  132. v.SendStatic(t)
  133. }
  134. }
  135. func (o *ITSDeviceMgr) RemotePub(data *tagVehicleInfo) {
  136. var obj protocol.Pack_VehicleSpeed
  137. vs := protocol.VehicleSpeed{
  138. Plate: data.VehiclePlate,
  139. Time: protocol.BJTime(data.PassTime),
  140. Type: data.VehicleType,
  141. Speed: data.VehicleSpeed,
  142. }
  143. if str, err := obj.EnCode(data.CamID, appConfig.GID, GetNextUint64(), &vs); err == nil {
  144. topic := GetTopic(protocol.DT_ITS, data.CamID, protocol.TP_ITS_VEHICLESPEED)
  145. GetMQTTMgr().Publish(topic, str, 0, ToCloud)
  146. }
  147. }
  148. func (o *ITSDeviceMgr) Handle(args ...interface{}) interface{} {
  149. defer func() {
  150. if err := recover(); err != nil {
  151. logrus.Errorf("ITSDeviceMgr.HandleData发生异常:%v", err)
  152. logrus.Errorf("ITSDeviceMgr.HandleData发生异常,堆栈信息:%s", string(debug.Stack()))
  153. gopool.Add(o.Handle, args)
  154. }
  155. }()
  156. o.CheckLogin()
  157. timer := time.NewTicker(1 * time.Minute)
  158. currHour := util.New(util.MlNow()).BeginningOfHour() //data time
  159. LastHour := currHour.Add(1 * time.Hour) //send time
  160. exit := false
  161. for {
  162. select {
  163. case <-o.ctx.Done():
  164. exit = true
  165. case <-timer.C:
  166. o.HandleLogout()
  167. o.CheckLogin()
  168. default:
  169. if util.MlNow().After(LastHour) {
  170. o.SendData(currHour)
  171. currHour = currHour.Add(1 * time.Hour)
  172. LastHour = currHour.Add(1 * time.Hour)
  173. }
  174. //从队列钟获取指令执行
  175. if m, ok, _ := o.downQueue.Get(); ok {
  176. if mm, ok := m.(mqtt.Message); ok {
  177. if fn, ok := o.mapTopicHandle[mm.Topic()]; ok {
  178. fn(mm)
  179. } else {
  180. logrus.Errorf("ITSDeviceMgr.Handle:不支持的主题:%s", mm.Topic())
  181. }
  182. }
  183. }
  184. if v, ok, _ := o.Queue.Get(); ok {
  185. if vi, ok := v.(*tagVehicleInfo); ok {
  186. o.HandleData(vi)
  187. }
  188. } else {
  189. if exit {
  190. logrus.Warnf("ITSDeviceMgr.HandleData退出,原因:%v", o.ctx.Err())
  191. return 0
  192. }
  193. time.Sleep(50 * time.Millisecond)
  194. }
  195. }
  196. }
  197. }