فهرست منبع

开始编写yfkj_handler.go,对齐公司私有测报灯MQTT后端协议

niujiuru 4 هفته پیش
والد
کامیت
d75ecf9549
4فایلهای تغییر یافته به همراه34 افزوده شده و 11 حذف شده
  1. 14 6
      mqtt_reporter/client.go
  2. 3 3
      mqtt_reporter/photo.go
  3. 1 1
      mqtt_reporter/reporter.go
  4. 16 1
      mqtt_reporter/yfkj_handler.go

+ 14 - 6
mqtt_reporter/client.go

@@ -35,9 +35,9 @@ type ClientConfig struct {
 	ClientID     string //////////////////// 客户端的唯一标识
 	Username     string //////////////////// 登录服务器的用户
 	Password     string //////////////////// 登录服务器的密码
+	Qos          byte   //////////////////// 传输消息质量等级
 	WillTopic    string //////////////////// 遗嘱消息发送主题
 	WillPayload  string //////////////////// 遗嘱消息发送内容
-	WillQos      byte   //////////////////// 遗嘱消息质量等级
 	WillRetained bool   //////////////////// 遗嘱消息是否保留
 }
 
@@ -46,6 +46,8 @@ type subscription struct {
 	handler mqtt.MessageHandler //////////// 订阅主题消息处理
 }
 
+type MessageHandler func(client *Client, msg mqtt.Message)
+
 type Client struct {
 	client  mqtt.Client
 	config  ClientConfig
@@ -77,11 +79,11 @@ func NewClient(config ClientConfig, ctx context.Context) (*Client, error) {
 		SetOrderMatters(false)
 
 	if config.WillTopic != "" { //////////////////////////////////////// 设置遗嘱消息 /////////
-		if config.WillQos != MqttQos0 && config.WillQos != MqttQos1 && config.WillQos != MqttQos2 {
+		if config.Qos != MqttQos0 && config.Qos != MqttQos1 && config.Qos != MqttQos2 {
 			cancel()
 			return nil, ErrInvalidQoS
 		}
-		opts = opts.SetWill(config.WillTopic, config.WillPayload, config.WillQos, config.WillRetained)
+		opts = opts.SetWill(config.WillTopic, config.WillPayload, config.Qos, config.WillRetained)
 	}
 
 	opts.OnConnect = func(c mqtt.Client) {
@@ -264,7 +266,7 @@ func (c *Client) Publish(topic string, qos byte, retained bool, payload any) err
 消息回调需支持并发调用,并尽快返回,尽量避免耗时或阻塞操作, 此类类任务应交由独立的goroutine或通
 过消息队列来处理,避免阻塞mqtt底层的消息处理。
 */
-func (c *Client) Subscribe(topic string, qos byte, handler mqtt.MessageHandler) error {
+func (c *Client) Subscribe(topic string, qos byte, handler MessageHandler) error {
 	if topic == "" {
 		return ErrTopicEmpty
 	}
@@ -283,12 +285,18 @@ func (c *Client) Subscribe(topic string, qos byte, handler mqtt.MessageHandler)
 		c.mu.Unlock()
 		return err
 	}
-	c.subs[topic] = subscription{qos: qos, handler: handler} // 注意: 断线时也允许先登记订阅
+
+	wrappedHandler := func(_ mqtt.Client, msg mqtt.Message) {
+		handler(c, msg)
+	}
+
+	c.subs[topic] = subscription{qos: qos, handler: wrappedHandler}
 	if !c.client.IsConnected() {
 		c.mu.Unlock()
 		return nil
 	}
-	token := c.client.Subscribe(topic, qos, handler)
+
+	token := c.client.Subscribe(topic, qos, wrappedHandler)
 	c.mu.Unlock()
 
 	return c.waitToken(token)

+ 3 - 3
mqtt_reporter/photo.go

@@ -59,10 +59,10 @@ func (r *MQTTReporter) uploadPhotoToImagServers(imgFile string, isBackfill /* 
 	defer ftpclient.FileUploader.Unlock()
 
 	action := ""
-	if isBackfill {
-		action = "向图片服务器补录照片"
-	} else {
+	if !isBackfill {
 		action = "向图片服务器上传照片"
+	} else {
+		action = "向图片服务器补录照片"
 	}
 
 	needImagBackfill := false

+ 1 - 1
mqtt_reporter/reporter.go

@@ -120,9 +120,9 @@ func (r *MQTTReporter) initYFKJClient(server MQTTServer, devID string) error {
 		ClientID:     devID,
 		Username:     server.Username,
 		Password:     server.Password,
+		Qos:          qos,
 		WillTopic:    server.Topics.Offline,
 		WillPayload:  fmt.Sprintf(`{"cmd":"offline","ext":{"imei":"%s"}}`, devID),
-		WillQos:      qos,
 		WillRetained: false,
 	}
 

+ 16 - 1
mqtt_reporter/yfkj_handler.go

@@ -2,7 +2,22 @@ package mqtt_reporter
 
 import (
 	mqtt "github.com/eclipse/paho.mqtt.golang"
+	"hnyfkj.com.cn/rtu/linux/baseapp"
 )
 
-func yfkj_handleMessage(client mqtt.Client, msg mqtt.Message) {
+func yfkj_handleMessage(client *Client, msg mqtt.Message) {
+	go func() {
+		if msg.Retained() { ///// 收到历史缓存消息, 先通知服务端清除掉(空载荷 + Retain=1), 然后再处理
+			if err := client.Publish(msg.Topic(), client.config.Qos, true, []byte{}); err != nil {
+				baseapp.Logger.Errorf("[%s] MQTT服务器 %s 清除主题 %s 的Retained消息失败: %v!!",
+					MODULE_NAME, client.config.BrokerName, msg.Topic(), err)
+				return
+			}
+		}
+
+		yfkj_execLocalFunc(client, msg)
+	}()
+}
+
+func yfkj_execLocalFunc(client *Client, msg mqtt.Message) {
 }