All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feingto.iot.client.service.MqttMessageService Maven / Gradle / Ivy

There is a newer version: 2.3.3.RELEASE
Show newest version
package com.feingto.iot.client.service;

import com.feingto.iot.common.model.mqtt.MqttConnectOptions;
import com.feingto.iot.common.model.mqtt.SendMessage;
import com.feingto.iot.common.service.ResponseCallback;
import com.feingto.iot.common.service.mqtt.MessageRequest;
import com.feingto.iot.common.service.mqtt.MessageResponse;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.*;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * MQTT 消息服务
 *
 * @author longfei
 */
@Slf4j
public class MqttMessageService implements IMqttMessage {
    @Setter
    private ResponseCallback callback;

    @Override
    public void connect(Channel channel, MqttConnectOptions options) {
        MessageRequest.connect(channel, options);
    }

    @Override
    public void connack(MqttConnAckMessage msg) {
        MqttConnAckVariableHeader mqttConnAckVariableHeader = msg.variableHeader();
        switch (mqttConnAckVariableHeader.connectReturnCode()) {
            case CONNECTION_ACCEPTED:
                break;
            case CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD:
                log.error("用户名密码错误");
                break;
            case CONNECTION_REFUSED_IDENTIFIER_REJECTED:
                log.error("clientId身份验证失败");
                break;
            case CONNECTION_REFUSED_SERVER_UNAVAILABLE:
                log.error("服务不可用");
                break;
            case CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION:
                log.error("mqtt版本不可用");
                break;
            case CONNECTION_REFUSED_NOT_AUTHORIZED:
                log.error("未授权连接");
                break;
        }
    }

    @Override
    public void publish(Channel channel, MqttMessage msg) {
        MqttFixedHeader fixedHeader = msg.fixedHeader();
        MqttPublishMessage message = (MqttPublishMessage) msg;
        MqttPublishVariableHeader variableHeader = message.variableHeader();
        if (callback != null) {
            callback.onResponse(SendMessage.newInstance(message));
        }

        switch (fixedHeader.qosLevel()) {
            case AT_MOST_ONCE:
                break;
            case AT_LEAST_ONCE:
                MessageResponse.puback(channel, MqttMessageType.PUBACK, MqttQoS.AT_MOST_ONCE, variableHeader.packetId());
                break;
            case EXACTLY_ONCE:
                MessageResponse.puback(channel, MqttMessageType.PUBREC, MqttQoS.AT_LEAST_ONCE, variableHeader.packetId());
                break;
        }
    }

    @Override
    public void puback(Channel channel, MqttMessage msg) {
    }

    @Override
    public void pubrec(Channel channel, MqttMessage msg) {
        MqttMessageIdVariableHeader variableHeader = (MqttMessageIdVariableHeader) msg.variableHeader();
        MessageResponse.pubrel(channel, variableHeader.messageId());
    }

    @Override
    public void pubrel(Channel channel, MqttMessage msg) {
        MqttMessageIdVariableHeader variableHeader = (MqttMessageIdVariableHeader) msg.variableHeader();
        MessageResponse.pubcomp(channel, variableHeader.messageId());
    }

    @Override
    public void pubcomp(Channel channel, MqttMessage msg) {
    }

    @Override
    public void suback(Channel channel, MqttSubAckMessage msg) {
    }

    @Override
    public void unsubBack(Channel channel, MqttMessage msg) {
    }

    @Override
    public void pingreq(Channel channel) {
        MessageRequest.pingreq(channel);
    }

    @Override
    public void disconnect(Channel channel) {
        MessageRequest.disconnect(channel);
    }

    @Override
    public void exceptionCaught(Throwable cause) {
        cause.printStackTrace();
        if (callback != null) {
            callback.onFailure(cause);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy