mqtthandlerHL.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package controllers
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "github.com/astaxie/beego"
  7. "lc/common/mqtt"
  8. )
  9. //海蓝灯控的mqtt
  10. var _mqtthandleroncehl sync.Once
  11. var _mqtthandlersinglehl *MqttHandler
  12. func GetMqttHandlerHL() *MqttHandler {
  13. _mqtthandleroncehl.Do(func() {
  14. _mqtthandlersinglehl = NewMqttHandler(beego.AppConfig.String("hlmqtt.server"), beego.AppConfig.String("hlmqtt.user"),
  15. beego.AppConfig.String("hlmqtt.password"), 3000)
  16. })
  17. return _mqtthandlersinglehl
  18. }
  19. type MqttHandlerhl struct {
  20. mqtt *mqtt.Client
  21. mu sync.Mutex
  22. mapTopics map[string]mqtt.QOS
  23. Timeout uint //超时时间,毫秒为单位
  24. }
  25. func NewMqttHandlerhl(server, user, password string, timeout uint) *MqttHandlerhl {
  26. o := MqttHandlerhl{
  27. mapTopics: make(map[string]mqtt.QOS),
  28. Timeout: timeout,
  29. }
  30. client, err := mqtt.NewClient(mqtt.ClientOptions{
  31. Servers: []string{server},
  32. ClientID: beego.AppConfig.String("mqtt.id"),
  33. Username: user,
  34. Password: password,
  35. AutoReconnect: true,
  36. }, &o)
  37. if client == nil || err != nil {
  38. beego.Error("未配置 MQTT Server")
  39. panic("未配置 MQTT Server")
  40. return nil
  41. }
  42. o.mqtt = client
  43. ctx, cancel := o.Ctx()
  44. defer cancel()
  45. err = client.Connect(ctx)
  46. return &o
  47. }
  48. func (o *MqttHandlerhl) ConnectionLostHandler(err error) {
  49. beego.Error("MqttHandlerhl.ConnectionLostHandler:MQTT连接已经断开,原因:", err)
  50. }
  51. func (o *MqttHandlerhl) OnConnectHandler() {
  52. beego.Debug("MqttHandlerhl.OnConnectHandler:MQTT连接成功")
  53. //连接成功则订阅主题
  54. for k, v := range o.mapTopics {
  55. o.Subscribe(k, v)
  56. }
  57. }
  58. func (o *MqttHandlerhl) GetWill() (string, string) {
  59. return "", ""
  60. }
  61. func (o *MqttHandlerhl) Connect() error {
  62. if !o.mqtt.IsConnected() {
  63. ctx, cancel := o.Ctx()
  64. defer cancel()
  65. return o.mqtt.Connect(ctx)
  66. }
  67. return nil
  68. }
  69. func (o *MqttHandlerhl) IsConnected() bool {
  70. return o.mqtt.IsConnected()
  71. }
  72. func (o *MqttHandlerhl) Publish(topic string, payload []byte, qos mqtt.QOS) error {
  73. ctx, cancel := o.Ctx()
  74. defer cancel()
  75. return o.mqtt.Publish(ctx, topic, payload, qos)
  76. }
  77. func (o *MqttHandlerhl) PublishString(topic string, payload string, qos mqtt.QOS) error {
  78. ctx, cancel := o.Ctx()
  79. defer cancel()
  80. return o.mqtt.PublishString(ctx, topic, payload, qos)
  81. }
  82. func (o *MqttHandlerhl) PublishJSON(topic string, payload interface{}, qos mqtt.QOS) error {
  83. ctx, cancel := o.Ctx()
  84. defer cancel()
  85. return o.mqtt.PublishJSON(ctx, topic, payload, qos)
  86. }
  87. func (o *MqttHandlerhl) Subscribe(topic string, qos mqtt.QOS) error {
  88. o.mu.Lock()
  89. defer o.mu.Unlock()
  90. if _, ok := o.mapTopics[topic]; !ok {
  91. o.mapTopics[topic] = qos
  92. }
  93. ctx, cancel := o.Ctx()
  94. defer cancel()
  95. return o.mqtt.Subscribe(ctx, topic, qos)
  96. }
  97. func (o *MqttHandlerhl) Unsubscribe(ctx context.Context, topic string) error {
  98. o.mu.Lock()
  99. defer o.mu.Unlock()
  100. if _, ok := o.mapTopics[topic]; ok {
  101. delete(o.mapTopics, topic)
  102. }
  103. ctx, cancel := o.Ctx()
  104. defer cancel()
  105. return o.mqtt.Unsubscribe(ctx, topic)
  106. }
  107. func (o *MqttHandlerhl) Handle(topic string, handler mqtt.MessageHandler) mqtt.Route {
  108. return o.mqtt.Handle(topic, handler)
  109. }
  110. func (o *MqttHandlerhl) Ctx() (context.Context, context.CancelFunc) {
  111. return context.WithTimeout(context.Background(), time.Millisecond*time.Duration(o.Timeout))
  112. }