com.feingto.iot.common.service.mqtt.MessageConsumeService Maven / Gradle / Ivy
package com.feingto.iot.common.service.mqtt;
import com.feingto.iot.common.exception.ServiceException;
import com.feingto.iot.common.model.mqtt.SendMessage;
import com.feingto.iot.common.service.ResponseCallback;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.*;
import lombok.extern.slf4j.Slf4j;
/**
* 消息消费服务
*
* @author longfei
*/
@Slf4j
public class MessageConsumeService implements IMessageConsume {
private final ResponseCallback callback;
public MessageConsumeService(ResponseCallback callback) {
this.callback = callback;
}
@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("Connect failed", new ServiceException("用户名密码错误"));
break;
case CONNECTION_REFUSED_IDENTIFIER_REJECTED:
log.error("Connect failed", new ServiceException("clientId身份验证失败"));
break;
case CONNECTION_REFUSED_SERVER_UNAVAILABLE:
log.error("Connect failed", new ServiceException("服务不可用"));
break;
case CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION:
log.error("Connect failed", new ServiceException("mqtt版本不可用"));
break;
case CONNECTION_REFUSED_NOT_AUTHORIZED:
log.error("Connect failed", new ServiceException("未授权连接"));
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