package main

import (
	"sync"

	"lc/common/mqtt"
)

//海蓝mqtt连接
var _hlMqttMgrOnce sync.Once
var _hlMqttMgrSingle *HlMqttMgr

func GetHlMqttMgr() *HlMqttMgr {
	_hlMqttMgrOnce.Do(func() {
		_hlMqttMgrSingle = _newHlMqttMgr()
	})
	return _hlMqttMgrSingle
}

type HlMqttMgr struct {
	Cloud *MqttClient
}

func _newHlMqttMgr() *HlMqttMgr {
	return &HlMqttMgr{
		Cloud: NewMqttClient(MqttconfigHl.Mqtt_Server,
			MqttconfigHl.Mqtt_Id,
			MqttconfigHl.Mqtt_User,
			MqttconfigHl.Mqtt_Password,
			3000, &EmptyMqttOnline{}),
	}

}

func (o *HlMqttMgr) Subscribe(topic string, qos mqtt.QOS, handler mqtt.MessageHandler) {
	o.Cloud.Handle(topic, handler)
	err := o.Cloud.Subscribe(topic, qos)
	if err != nil {
		return
	}
}

func (o *HlMqttMgr) UnSubscribe(topic string) {
	err := o.Cloud.Unsubscribe(topic)
	if err != nil {
		return
	}
}

func (o *HlMqttMgr) Publish(topic string, payload string, qos mqtt.QOS) error {
	return o.Cloud.PublishString(topic, payload, qos)
}
