chzbconcentratormgr.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package main
  2. import (
  3. "runtime"
  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 _ChzbConcentratorMgrOnce sync.Once
  13. var _ChzbConcentratorMgrSingle *ChzbConcentratorMgr
  14. func GetChzbConcentratorMgr() *ChzbConcentratorMgr {
  15. _ChzbConcentratorMgrOnce.Do(func() {
  16. _ChzbConcentratorMgrSingle = &ChzbConcentratorMgr{
  17. queue: util.NewQueue(10000),
  18. mapChzbConcentrator: make(map[string]*ChZigbeeConcentrator),
  19. }
  20. })
  21. return _ChzbConcentratorMgrSingle
  22. }
  23. type ChzbConcentratorMgr struct {
  24. queue *util.MlQueue
  25. mapChzbConcentrator map[string]*ChZigbeeConcentrator
  26. }
  27. func (o *ChzbConcentratorMgr) SubscribeTopics() {
  28. GetMQTTMgr().Subscribe(GetVagueTopic(protocol.DT_CONCENTRATOR, protocol.TP_CHZB_DATA), mqtt.AtMostOnce, o.HandlerData)
  29. GetMQTTMgr().Subscribe(GetVagueTopic(protocol.DT_CONCENTRATOR, protocol.TP_CHZB_SET_WAITTIME_ACK), mqtt.AtMostOnce, o.HandlerData)
  30. GetMQTTMgr().Subscribe(GetVagueTopic(protocol.DT_CONCENTRATOR, protocol.TP_CHZB_SET_SWITCH_ACK), mqtt.AtMostOnce, o.HandlerData)
  31. GetMQTTMgr().Subscribe(GetVagueTopic(protocol.DT_CONCENTRATOR, protocol.TP_CHZB_SET_RECOVERY_AUTO_ACK), mqtt.AtMostOnce, o.HandlerData)
  32. GetMQTTMgr().Subscribe(GetVagueTopic(protocol.DT_CONCENTRATOR, protocol.TP_CHZB_SET_ONOFFTIME_ACK), mqtt.AtMostOnce, o.HandlerData)
  33. GetMQTTMgr().Subscribe(GetVagueTopic(protocol.DT_CONCENTRATOR, protocol.TP_CHZB_QUERY_ONOFFTIME_ACK), mqtt.AtMostOnce, o.HandlerData)
  34. GetMQTTMgr().Subscribe(GetVagueTopic(protocol.DT_CONCENTRATOR, protocol.TP_CHZB_SET_UPDATE_LAMP_ACK), mqtt.AtMostOnce, o.HandlerData)
  35. GetMQTTMgr().Subscribe(GetVagueTopic(protocol.DT_CONCENTRATOR, protocol.TP_CHZB_QUERY_LAMP), mqtt.AtMostOnce, o.HandlerData)
  36. GetMQTTMgr().Subscribe(GetVagueTopic(protocol.DT_CONCENTRATOR, protocol.TP_CHZB_QUERY_TIME_ACK), mqtt.AtMostOnce, o.HandlerData)
  37. GetMQTTMgr().Subscribe(GetVagueTopic(protocol.DT_CONCENTRATOR, protocol.TP_CHZB_SET_BROADCASTTIME_ACK), mqtt.AtMostOnce, o.HandlerData)
  38. GetMQTTMgr().Subscribe(GetVagueTopic(protocol.DT_CONCENTRATOR, protocol.TP_CHZB_ALARM), mqtt.AtMostOnce, o.HandlerData)
  39. }
  40. func (o *ChzbConcentratorMgr) HandlerData(m mqtt.Message) {
  41. for {
  42. ok, cnt := o.queue.Put(&m)
  43. if ok {
  44. break
  45. } else {
  46. logrus.Errorf("ChzbConcentratorMgr.HandlerData:查询队列失败,队列消息数量:%d", cnt)
  47. runtime.Gosched()
  48. }
  49. }
  50. }
  51. func (o *ChzbConcentratorMgr) Handler(args ...interface{}) interface{} {
  52. defer func() {
  53. if err := recover(); err != nil {
  54. time.Sleep(time.Second)
  55. gopool.Add(o.Handler, args)
  56. logrus.Errorf("ChzbConcentratorMgr.Handler发生异常:%v", err)
  57. logrus.Errorf("ChzbConcentratorMgr.Handler发生异常,堆栈信息:%s", string(debug.Stack()))
  58. }
  59. }()
  60. for {
  61. msg, ok, quantity := o.queue.Get()
  62. if !ok {
  63. time.Sleep(10 * time.Millisecond)
  64. continue
  65. } else if quantity > 1000 {
  66. logrus.Warnf("ChzbConcentratorMgr.Handler:数据队列累积过多,请注意优化,当前队列条数:%d", quantity)
  67. }
  68. m, ok := msg.(*mqtt.Message)
  69. if !ok {
  70. continue
  71. }
  72. Tenant, _, DID, _, err := ParseTopic(m.Topic())
  73. if err != nil {
  74. continue
  75. }
  76. pzl, ok := o.mapChzbConcentrator[DID]
  77. if !ok {
  78. pzl = NewChZigbeeConcentrator(Tenant, DID)
  79. pzl.Start()
  80. o.mapChzbConcentrator[DID] = pzl
  81. }
  82. pzl.PutMessage(m)
  83. }
  84. }