Search in sources :

Example 6 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project rocketmq-externals by apache.

the class MessageDispatcher method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!(msg instanceof MqttMessage)) {
        return;
    }
    Client client = clientManager.get(ctx.channel());
    if (client == null) {
        client = new MqttClient();
        client.setCtx(ctx);
        clientManager.put(ctx.channel(), client);
    }
    MqttMessage mqttMessage = (MqttMessage) msg;
    Message message = MessageUtil.getMessage(mqttMessage);
    message.setClient(client);
    dispatch(message);
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) Message(org.apache.rocketmq.iot.common.data.Message) MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) Client(org.apache.rocketmq.iot.connection.client.Client) MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient)

Example 7 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project traccar by tananaev.

the class IotmProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    if (msg instanceof MqttConnectMessage) {
        MqttConnectMessage message = (MqttConnectMessage) msg;
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, message.payload().clientIdentifier());
        MqttConnectReturnCode returnCode = deviceSession != null ? MqttConnectReturnCode.CONNECTION_ACCEPTED : MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED;
        MqttMessage response = MqttMessageBuilders.connAck().returnCode(returnCode).build();
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
    } else if (msg instanceof MqttSubscribeMessage) {
        MqttSubscribeMessage message = (MqttSubscribeMessage) msg;
        MqttMessage response = MqttMessageBuilders.subAck().packetId((short) message.variableHeader().messageId()).build();
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
    } else if (msg instanceof MqttPublishMessage) {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        List<Position> positions = new LinkedList<>();
        MqttPublishMessage message = (MqttPublishMessage) msg;
        ByteBuf buf = message.payload();
        // structure version
        buf.readUnsignedByte();
        while (buf.readableBytes() > 1) {
            int type = buf.readUnsignedByte();
            int length = buf.readUnsignedShortLE();
            ByteBuf record = buf.readSlice(length);
            if (type == 1) {
                Position position = new Position(getProtocolName());
                position.setDeviceId(deviceSession.getDeviceId());
                position.setTime(new Date(record.readUnsignedIntLE() * 1000));
                while (record.readableBytes() > 0) {
                    int sensorType = record.readUnsignedByte();
                    int sensorId = record.readUnsignedShortLE();
                    if (sensorType == 14) {
                        position.setValid(true);
                        position.setLatitude(record.readFloatLE());
                        position.setLongitude(record.readFloatLE());
                        position.setSpeed(UnitsConverter.knotsFromKph(record.readUnsignedShortLE()));
                        position.set(Position.KEY_HDOP, record.readUnsignedByte());
                        position.set(Position.KEY_SATELLITES, record.readUnsignedByte());
                        position.setCourse(record.readUnsignedShortLE());
                        position.setAltitude(record.readShortLE());
                    } else {
                        if (sensorType == 3) {
                            continue;
                        }
                        decodeSensor(position, record, sensorType, sensorId);
                    }
                }
                positions.add(position);
            } else if (type == 3) {
                Position position = new Position(getProtocolName());
                position.setDeviceId(deviceSession.getDeviceId());
                getLastLocation(position, new Date(record.readUnsignedIntLE() * 1000));
                // function identifier
                record.readUnsignedByte();
                position.set(Position.KEY_EVENT, record.readUnsignedByte());
                positions.add(position);
            }
        }
        // checksum
        buf.readUnsignedByte();
        MqttMessage response = MqttMessageBuilders.pubAck().packetId((short) message.variableHeader().packetId()).build();
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
        return positions.isEmpty() ? null : positions;
    }
    return null;
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttSubscribeMessage(io.netty.handler.codec.mqtt.MqttSubscribeMessage) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) ByteBuf(io.netty.buffer.ByteBuf) LinkedList(java.util.LinkedList) Date(java.util.Date) MqttConnectReturnCode(io.netty.handler.codec.mqtt.MqttConnectReturnCode) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) NetworkMessage(org.traccar.NetworkMessage)

Example 8 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project thingsboard by thingsboard.

the class MqttPingHandler method sendPingReq.

private void sendPingReq(Channel channel) {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PINGREQ, false, MqttQoS.AT_MOST_ONCE, false, 0);
    channel.writeAndFlush(new MqttMessage(fixedHeader));
    if (this.pingRespTimeout != null) {
        this.pingRespTimeout = channel.eventLoop().schedule(() -> {
            MqttFixedHeader fixedHeader2 = new MqttFixedHeader(MqttMessageType.DISCONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0);
            channel.writeAndFlush(new MqttMessage(fixedHeader2)).addListener(ChannelFutureListener.CLOSE);
        // TODO: what do when the connection is closed ?
        }, this.keepaliveSeconds, TimeUnit.SECONDS);
    }
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader)

Example 9 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project thingsboard by thingsboard.

the class MqttTransportHandler method createUnSubAckMessage.

private MqttMessage createUnSubAckMessage(int msgId) {
    MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(UNSUBACK, false, AT_MOST_ONCE, false, 0);
    MqttMessageIdVariableHeader mqttMessageIdVariableHeader = MqttMessageIdVariableHeader.from(msgId);
    return new MqttMessage(mqttFixedHeader, mqttMessageIdVariableHeader);
}
Also used : MqttMessageIdVariableHeader(io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader)

Example 10 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project thingsboard by thingsboard.

the class MqttTransportHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    log.trace("[{}] Processing msg: {}", sessionId, msg);
    if (address == null) {
        address = getAddress(ctx);
    }
    try {
        if (msg instanceof MqttMessage) {
            MqttMessage message = (MqttMessage) msg;
            if (message.decoderResult().isSuccess()) {
                processMqttMsg(ctx, message);
            } else {
                log.error("[{}] Message decoding failed: {}", sessionId, message.decoderResult().cause().getMessage());
                ctx.close();
            }
        } else {
            log.debug("[{}] Received non mqtt message: {}", sessionId, msg.getClass().getSimpleName());
            ctx.close();
        }
    } finally {
        ReferenceCountUtil.safeRelease(msg);
    }
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage)

Aggregations

MqttMessage (io.netty.handler.codec.mqtt.MqttMessage)20 MqttFixedHeader (io.netty.handler.codec.mqtt.MqttFixedHeader)10 MqttPublishMessage (io.netty.handler.codec.mqtt.MqttPublishMessage)6 Client (org.apache.rocketmq.iot.connection.client.Client)4 MqttConnAckMessage (io.netty.handler.codec.mqtt.MqttConnAckMessage)3 MqttConnectMessage (io.netty.handler.codec.mqtt.MqttConnectMessage)3 MqttPubAckMessage (io.netty.handler.codec.mqtt.MqttPubAckMessage)2 MqttSubscribeMessage (io.netty.handler.codec.mqtt.MqttSubscribeMessage)2 Message (org.apache.rocketmq.iot.common.data.Message)2 MqttClient (org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient)2 Test (org.junit.Test)2 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)2 JsonParseException (com.google.gson.JsonParseException)1 ByteBuf (io.netty.buffer.ByteBuf)1 MqttConnAckVariableHeader (io.netty.handler.codec.mqtt.MqttConnAckVariableHeader)1 MqttConnectReturnCode (io.netty.handler.codec.mqtt.MqttConnectReturnCode)1 MqttMessageIdVariableHeader (io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader)1 MqttPublishVariableHeader (io.netty.handler.codec.mqtt.MqttPublishVariableHeader)1 MqttSubAckMessage (io.netty.handler.codec.mqtt.MqttSubAckMessage)1 MqttUnsubAckMessage (io.netty.handler.codec.mqtt.MqttUnsubAckMessage)1