mqtt.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package admin
  2. import (
  3. "errors"
  4. "fmt"
  5. "regexp"
  6. "runtime"
  7. "server/utils/logger"
  8. "server/utils/mqtt"
  9. "server/utils/protocol"
  10. "strings"
  11. "sync"
  12. "time"
  13. )
  14. func InitMqtt() {
  15. MqttService = GetHandler()
  16. MqttService.SubscribeTopics()
  17. go MqttService.Handler()
  18. }
  19. var MqttService *MqttHandler
  20. var timeoutReg = regexp.MustCompile("Client .* has exceeded timeout")
  21. var connectReg = regexp.MustCompile(`New client connected from .* as .*\(`)
  22. var disconnectReg = regexp.MustCompile("Client mqttx_893e4b7d disconnected")
  23. type MqttHandler struct {
  24. queue *mqtt.MlQueue
  25. }
  26. var _handlerOnce sync.Once
  27. var _handlerSingle *MqttHandler
  28. func GetHandler() *MqttHandler {
  29. _handlerOnce.Do(func() {
  30. _handlerSingle = &MqttHandler{
  31. queue: mqtt.NewQueue(10000),
  32. }
  33. })
  34. return _handlerSingle
  35. }
  36. func (o *MqttHandler) SubscribeTopics() {
  37. mqtt.GetMQTTMgr().Subscribe("smart_tunnel/#", mqtt.AtLeastOnce, o.HandlerData)
  38. }
  39. func (o *MqttHandler) HandlerData(m mqtt.Message) {
  40. for {
  41. ok, cnt := o.queue.Put(&m)
  42. if ok {
  43. break
  44. } else {
  45. logger.Get().Error("HandlerData:查询队列失败,队列消息数量:%d", cnt)
  46. runtime.Gosched()
  47. }
  48. }
  49. }
  50. func (o *MqttHandler) Handler() interface{} {
  51. defer func() {
  52. if err := recover(); err != nil {
  53. go GetHandler().Handler()
  54. //logger.Logger.Errorf("MqttHandler.Handler:发生异常:%s", string(debug.Stack()))
  55. }
  56. }()
  57. for {
  58. msg, ok, quantity := o.queue.Get()
  59. if !ok {
  60. time.Sleep(10 * time.Millisecond)
  61. continue
  62. } else if quantity > 1000 {
  63. logger.Get().Warnf("数据队列累积过多,请注意优化,当前队列条数:%d", quantity)
  64. }
  65. m, ok := msg.(*mqtt.Message)
  66. if !ok {
  67. continue
  68. }
  69. _, topic, err := parseTopic(m.Topic())
  70. fmt.Println(m.Topic())
  71. fmt.Println(m.PayloadString())
  72. if err != nil {
  73. //logger.Logger.Errorf("parseTopic err")
  74. continue
  75. }
  76. switch topic {
  77. case protocol.TopicTunnelControlAck:
  78. }
  79. }
  80. }
  81. func (o *MqttHandler) Publish(topic string, data interface{}) error {
  82. return mqtt.GetMQTTMgr().Publish(topic, data, mqtt.AtLeastOnce)
  83. }
  84. func (o *MqttHandler) GetTopic(deviceSn, protocol string) string {
  85. return fmt.Sprintf("mini/%s/%s", deviceSn, protocol)
  86. }
  87. // parseTopic 获取设备SN, topic
  88. // "mini/*****/switch_control/ack"
  89. func parseTopic(topic string) (string, string, error) {
  90. strList := strings.Split(topic, "/")
  91. if len(strList) < 4 {
  92. return "", "", errors.New("不支持的topic")
  93. }
  94. topic = strings.Join(strList[2:], "/")
  95. return strList[1], topic, nil
  96. }