router.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package mqtt_api
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
  4. "sync"
  5. )
  6. type Handler func(c *Context)
  7. type Router struct {
  8. Group string
  9. mqttClient *MqttClient
  10. contextsMutex *sync.Mutex
  11. contexts []*Context
  12. globalHandlers []Handler
  13. topicHandlersMutex *sync.Mutex
  14. topicHandlers map[string][]Handler
  15. }
  16. func NewRouter(group string, globalHandlers []Handler) *Router {
  17. return &Router{
  18. Group: group,
  19. mqttClient: nil,
  20. contextsMutex: &sync.Mutex{},
  21. contexts: make([]*Context, 0),
  22. globalHandlers: globalHandlers,
  23. topicHandlersMutex: &sync.Mutex{},
  24. topicHandlers: make(map[string][]Handler),
  25. }
  26. }
  27. func DestroyRouter(router *Router) {
  28. if router == nil {
  29. return
  30. }
  31. router.contextsMutex.Lock()
  32. for _, context := range router.contexts {
  33. destroyContext(context)
  34. }
  35. router.contexts = nil
  36. router.contextsMutex.Unlock()
  37. router = nil
  38. }
  39. func (router *Router) Start(mqttOptions *MqttClientOptions) error {
  40. mqttClient, err := newMqttClient(mqttOptions, func(client *MqttClient) {
  41. router.subscribeTopics(client)
  42. }, func(client *MqttClient) {
  43. router.unsubscribeTopics(client)
  44. })
  45. if err != nil {
  46. return err
  47. }
  48. err = mqttClient.connect()
  49. if err != nil {
  50. return err
  51. }
  52. router.mqttClient = mqttClient
  53. return nil
  54. }
  55. func (router *Router) Finish() {
  56. if router.mqttClient == nil {
  57. return
  58. }
  59. router.unsubscribeTopics(router.mqttClient)
  60. router.mqttClient.disconnect()
  61. destroyMqttClient(router.mqttClient)
  62. router.mqttClient = nil
  63. }
  64. func (router *Router) AddTopic(topic string, handlers ...Handler) error {
  65. added := router.addTopicHandlers(topic, handlers...)
  66. if !added {
  67. return nil
  68. }
  69. if router.mqttClient == nil {
  70. return nil
  71. }
  72. err := router.mqttClient.subscribe(topic, func(topic string, data []byte) {})
  73. if err != nil {
  74. return err
  75. }
  76. return nil
  77. }
  78. func (router *Router) subscribeTopics(client *MqttClient) {
  79. router.rangeTopicHandlers(func(topic string, handlers []Handler) {
  80. err := client.subscribe(topic, func(topic string, data []byte) {
  81. c, err := newContext(router.mqttClient, topic, data, handlers)
  82. if err != nil {
  83. logger.GetInstance().Error(err)
  84. return
  85. }
  86. c.Next()
  87. })
  88. if err != nil {
  89. logger.GetInstance().Error(err)
  90. return
  91. }
  92. })
  93. }
  94. func (router *Router) unsubscribeTopics(client *MqttClient) {
  95. router.rangeTopicHandlers(func(topic string, handlers []Handler) {
  96. err := client.unsubscribe(topic)
  97. if err != nil {
  98. logger.GetInstance().Error(err)
  99. return
  100. }
  101. })
  102. }
  103. func (router *Router) addTopicHandlers(topic string, handler ...Handler) bool {
  104. router.topicHandlersMutex.Lock()
  105. defer router.topicHandlersMutex.Unlock()
  106. if router.topicHandlers[topic] != nil && len(router.topicHandlers[topic]) > 0 {
  107. return false
  108. }
  109. router.topicHandlers[topic] = append(router.globalHandlers, handler...)
  110. return true
  111. }
  112. func (router *Router) removeTopicHandlers(topic string) {
  113. router.topicHandlersMutex.Lock()
  114. defer router.topicHandlersMutex.Unlock()
  115. if router.topicHandlers[topic] == nil || len(router.topicHandlers[topic]) == 0 {
  116. return
  117. }
  118. delete(router.topicHandlers, topic)
  119. }
  120. func (router *Router) rangeTopicHandlers(callback func(topic string, handlers []Handler)) {
  121. router.topicHandlersMutex.Lock()
  122. defer router.topicHandlersMutex.Unlock()
  123. for topic, topicHandlers := range router.topicHandlers {
  124. callback(topic, topicHandlers)
  125. }
  126. }