Search in sources :

Example 46 with NetworkMessage

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

the class At2000FrameDecoder method sendResponse.

private void sendResponse(Channel channel) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer(2 * BLOCK_LENGTH);
        response.writeByte(At2000ProtocolDecoder.MSG_ACKNOWLEDGEMENT);
        response.writeMedium(1);
        // success
        response.writeByte(0x00);
        response.writerIndex(2 * BLOCK_LENGTH);
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 47 with NetworkMessage

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

the class At2000ProtocolDecoder method sendRequest.

private static void sendRequest(Channel channel) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer(BLOCK_LENGTH);
        response.writeByte(MSG_TRACK_REQUEST);
        response.writeMedium(0);
        response.writerIndex(BLOCK_LENGTH);
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 48 with NetworkMessage

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

the class ApelProtocolDecoder method sendSimpleMessage.

private void sendSimpleMessage(Channel channel, short type) {
    ByteBuf request = Unpooled.buffer(8);
    request.writeShortLE(type);
    request.writeShortLE(0);
    request.writeIntLE(Checksum.crc32(request.nioBuffer(0, 4)));
    channel.writeAndFlush(new NetworkMessage(request, channel.remoteAddress()));
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 49 with NetworkMessage

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

the class ArmoliProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    char type = sentence.charAt(1);
    Position position = new Position(getProtocolName());
    DeviceSession deviceSession;
    if (type != 'M') {
        if (type == 'W') {
            deviceSession = getDeviceSession(channel, remoteAddress);
            if (deviceSession != null) {
                position.setDeviceId(deviceSession.getDeviceId());
                getLastLocation(position, null);
                position.set(Position.KEY_RESULT, sentence.substring(sentence.indexOf(',') + 1, sentence.length() - 1));
                return position;
            }
        } else if (channel != null && (type == 'Q' || type == 'L')) {
            channel.writeAndFlush(new NetworkMessage("[TX,];;", remoteAddress));
        }
        return null;
    }
    Parser parser = new Parser(PATTERN, (String) msg);
    if (!parser.matches()) {
        return null;
    }
    deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
    position.setValid(parser.nextInt() > 0);
    position.set(Position.KEY_SATELLITES, parser.nextHexInt());
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextHexInt()));
    position.setCourse(parser.nextInt());
    position.set(Position.PREFIX_ADC + 1, parser.nextHexInt() / 27.0 * 1000);
    position.set(Position.PREFIX_ADC + 1, parser.nextHexInt() / 27.0 * 1000);
    position.set(Position.KEY_STATUS, parser.nextHexInt());
    position.set("maxSpeed", parser.nextHexInt());
    position.set(Position.KEY_ODOMETER, parser.nextHexInt());
    if (parser.hasNext()) {
        position.set(Position.KEY_HDOP, parser.nextInt() * 0.1);
    }
    if (parser.hasNext()) {
        position.set("alarms", parser.next());
    }
    if (parser.hasNext()) {
        position.set(Position.KEY_G_SENSOR, parser.next());
    }
    if (parser.hasNext()) {
        position.set(Position.KEY_POWER, parser.nextHexInt() * 0.01);
    }
    if (parser.hasNext()) {
        position.set(Position.KEY_BATTERY, parser.nextHexInt() * 0.01);
    }
    if (parser.hasNext()) {
        position.set(Position.KEY_DRIVER_UNIQUE_ID, parser.next());
    }
    if (parser.hasNext()) {
        String[] values = parser.next().split(">");
        for (int i = 1; i < values.length; i++) {
            String value = values[i];
            position.add(ObdDecoder.decodeData(Integer.parseInt(value.substring(4, 6), 16), Long.parseLong(value.substring(6), 16), true));
        }
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

Example 50 with NetworkMessage

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

the class BceProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    String imei = String.format("%015d", buf.readLongLE());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    List<Position> positions = new LinkedList<>();
    while (buf.readableBytes() > 1) {
        int dataEnd = buf.readUnsignedShortLE() + buf.readerIndex();
        int type = buf.readUnsignedByte();
        if (type != MSG_ASYNC_STACK && type != MSG_TIME_TRIGGERED) {
            return null;
        }
        int confirmKey = buf.readUnsignedByte() & 0x7F;
        while (buf.readerIndex() < dataEnd) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());
            int structEnd = buf.readUnsignedByte() + buf.readerIndex();
            long time = buf.readUnsignedIntLE();
            if ((time & 0x0f) == DATA_TYPE) {
                time = time >> 4 << 1;
                // 01/01/2008
                time += 0x47798280;
                position.setTime(new Date(time * 1000));
                // Read masks
                int mask;
                List<Integer> masks = new LinkedList<>();
                do {
                    mask = buf.readUnsignedShortLE();
                    masks.add(mask);
                } while (BitUtil.check(mask, 15));
                mask = masks.get(0);
                decodeMask1(buf, mask, position);
                if (masks.size() >= 2) {
                    mask = masks.get(1);
                    decodeMask2(buf, mask, position);
                }
                if (masks.size() >= 3) {
                    mask = masks.get(2);
                    decodeMask3(buf, mask, position);
                }
                if (masks.size() >= 4) {
                    mask = masks.get(3);
                    decodeMask4(buf, mask, position);
                }
            }
            buf.readerIndex(structEnd);
            if (position.getValid()) {
                positions.add(position);
            } else if (!position.getAttributes().isEmpty()) {
                getLastLocation(position, null);
                positions.add(position);
            }
        }
        // Send response
        if (type == MSG_ASYNC_STACK && channel != null) {
            ByteBuf response = Unpooled.buffer(8 + 2 + 2 + 1);
            response.writeLongLE(Long.parseLong(imei));
            response.writeShortLE(2);
            response.writeByte(MSG_STACK_COFIRM);
            response.writeByte(confirmKey);
            int checksum = 0;
            for (int i = 0; i < response.writerIndex(); i++) {
                checksum += response.getUnsignedByte(i);
            }
            response.writeByte(checksum);
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
    }
    return 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)

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