Search in sources :

Example 96 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class TeltonikaProtocolDecoder method sendImageRequest.

private void sendImageRequest(Channel channel, SocketAddress remoteAddress, long id, int offset, int size) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeInt(0);
        response.writeShort(0);
        // length
        response.writeShort(19);
        response.writeByte(CODEC_12);
        // nod
        response.writeByte(1);
        // camera
        response.writeByte(0x0D);
        // payload length
        response.writeInt(11);
        // command
        response.writeByte(2);
        response.writeInt((int) id);
        response.writeInt(offset);
        response.writeShort(size);
        // nod
        response.writeByte(1);
        response.writeShort(0);
        response.writeShort(Checksum.crc16(Checksum.CRC16_IBM, response.nioBuffer(8, response.readableBytes() - 10)));
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 97 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class TeltonikaProtocolDecoder method parseData.

private List<Position> parseData(Channel channel, SocketAddress remoteAddress, ByteBuf buf, int locationPacketId, String... imei) {
    List<Position> positions = new LinkedList<>();
    if (!connectionless) {
        // data length
        buf.readUnsignedInt();
    }
    int codec = buf.readUnsignedByte();
    int count = buf.readUnsignedByte();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    for (int i = 0; i < count; i++) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.setValid(true);
        if (codec == CODEC_13) {
            // type
            buf.readUnsignedByte();
            int length = buf.readInt() - 4;
            getLastLocation(position, new Date(buf.readUnsignedInt() * 1000));
            if (isPrintable(buf, length)) {
                position.set(Position.KEY_RESULT, buf.readCharSequence(length, StandardCharsets.US_ASCII).toString().trim());
            } else {
                position.set(Position.KEY_RESULT, ByteBufUtil.hexDump(buf.readSlice(length)));
            }
        } else if (codec == CODEC_12) {
            decodeSerial(channel, remoteAddress, position, buf);
        } else {
            decodeLocation(position, buf, codec);
        }
        if (!position.getOutdated() || !position.getAttributes().isEmpty()) {
            positions.add(position);
        }
    }
    if (channel != null && codec != CODEC_12 && codec != CODEC_13) {
        ByteBuf response = Unpooled.buffer();
        if (connectionless) {
            response.writeShort(5);
            response.writeShort(0);
            response.writeByte(0x01);
            response.writeByte(locationPacketId);
            response.writeByte(count);
        } else {
            response.writeInt(count);
        }
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
    return positions.isEmpty() ? null : positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage) LinkedList(java.util.LinkedList) Date(java.util.Date)

Example 98 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class Tk102ProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, int type, ByteBuf dataSequence, ByteBuf content) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeByte('[');
        response.writeByte(type);
        response.writeBytes(dataSequence);
        response.writeByte(content.readableBytes());
        response.writeBytes(content);
        content.release();
        response.writeByte(']');
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 99 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class TlvProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, SocketAddress remoteAddress, String type, String... arguments) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeCharSequence(type, StandardCharsets.US_ASCII);
        for (String argument : arguments) {
            response.writeByte(argument.length());
            response.writeCharSequence(argument, StandardCharsets.US_ASCII);
        }
        response.writeByte(0);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 100 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class UlbotechProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    if (buf.getUnsignedByte(buf.readerIndex()) == 0xF8) {
        if (channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeByte(0xF8);
            response.writeByte(DATA_GPS);
            response.writeByte(0xFE);
            response.writeShort(buf.getShort(response.writerIndex() - 1 - 2));
            response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer(1, 4)));
            response.writeByte(0xF8);
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
        return decodeBinary(channel, remoteAddress, buf);
    } else {
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(Unpooled.copiedBuffer(String.format("*TS01,ACK:%04X#", Checksum.crc16(Checksum.CRC16_XMODEM, buf.nioBuffer(1, buf.writerIndex() - 2))), StandardCharsets.US_ASCII), remoteAddress));
        }
        return decodeText(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Aggregations

NetworkMessage (org.traccar.NetworkMessage)146 ByteBuf (io.netty.buffer.ByteBuf)93 DeviceSession (org.traccar.DeviceSession)67 Position (org.traccar.model.Position)67 Parser (org.traccar.helper.Parser)34 Date (java.util.Date)27 DateBuilder (org.traccar.helper.DateBuilder)20 LinkedList (java.util.LinkedList)18 Network (org.traccar.model.Network)16 SimpleDateFormat (java.text.SimpleDateFormat)9 WifiAccessPoint (org.traccar.model.WifiAccessPoint)8 DateFormat (java.text.DateFormat)6 List (java.util.List)5 DatagramChannel (io.netty.channel.socket.DatagramChannel)4 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)4 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)4 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 CellTower (org.traccar.model.CellTower)3 DatagramPacket (io.netty.channel.socket.DatagramPacket)2