Search in sources :

Example 1 with BitBuffer

use of org.traccar.helper.BitBuffer in project traccar by tananaev.

the class AisProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String[] sentences = ((String) msg).split("\\r\\n");
    List<Position> positions = new ArrayList<>();
    Map<Integer, BitBuffer> buffers = new HashMap<>();
    for (String sentence : sentences) {
        if (!sentence.isEmpty()) {
            Parser parser = new Parser(PATTERN, sentence);
            if (parser.matches()) {
                int count = parser.nextInt(0);
                int index = parser.nextInt(0);
                int id = parser.nextInt(0);
                Position position = null;
                if (count == 1) {
                    BitBuffer bits = new BitBuffer();
                    bits.writeEncoded(parser.next().getBytes(StandardCharsets.US_ASCII));
                    position = decodePayload(channel, remoteAddress, bits);
                } else {
                    BitBuffer bits = buffers.get(id);
                    if (bits == null) {
                        bits = new BitBuffer();
                        buffers.put(id, bits);
                    }
                    bits.writeEncoded(parser.next().getBytes(StandardCharsets.US_ASCII));
                    if (count == index) {
                        position = decodePayload(channel, remoteAddress, bits);
                        buffers.remove(id);
                    }
                }
                if (position != null) {
                    positions.add(position);
                }
            }
        }
    }
    return positions;
}
Also used : Position(org.traccar.model.Position) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BitBuffer(org.traccar.helper.BitBuffer) Parser(org.traccar.helper.Parser)

Example 2 with BitBuffer

use of org.traccar.helper.BitBuffer in project traccar by tananaev.

the class Gl200BinaryProtocolDecoder method decodeLocation.

private List<Position> decodeLocation(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
    List<Position> positions = new LinkedList<>();
    int type = buf.readUnsignedByte();
    // mask
    buf.readUnsignedInt();
    // length
    buf.readUnsignedShort();
    // device type
    buf.readUnsignedByte();
    // protocol version
    buf.readUnsignedShort();
    // firmware version
    buf.readUnsignedShort();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.format("%015d", buf.readLong()));
    if (deviceSession == null) {
        return null;
    }
    int battery = buf.readUnsignedByte();
    int power = buf.readUnsignedShort();
    if (type == MSG_RSP_GEO) {
        // reserved
        buf.readUnsignedByte();
        // reserved
        buf.readUnsignedByte();
    }
    // motion status
    buf.readUnsignedByte();
    int satellites = buf.readUnsignedByte();
    if (type != MSG_RSP_COMPRESSED) {
        // index
        buf.readUnsignedByte();
    }
    if (type == MSG_RSP_LCB) {
        // phone length
        buf.readUnsignedByte();
        for (int b = buf.readUnsignedByte(); ; b = buf.readUnsignedByte()) {
            if ((b & 0xf) == 0xf || (b & 0xf0) == 0xf0) {
                break;
            }
        }
    }
    if (type == MSG_RSP_COMPRESSED) {
        int count = buf.readUnsignedShort();
        BitBuffer bits;
        int speed = 0;
        int heading = 0;
        int latitude = 0;
        int longitude = 0;
        long time = 0;
        for (int i = 0; i < count; i++) {
            if (time > 0) {
                time += 1;
            }
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());
            switch(BitUtil.from(buf.getUnsignedByte(buf.readerIndex()), 8 - 2)) {
                case 1:
                    bits = new BitBuffer(buf.readBytes(3));
                    // point attribute
                    bits.readUnsigned(2);
                    // fix type
                    bits.readUnsigned(1);
                    speed = bits.readUnsigned(12);
                    heading = bits.readUnsigned(9);
                    longitude = buf.readInt();
                    latitude = buf.readInt();
                    if (time == 0) {
                        time = buf.readUnsignedInt();
                    }
                    break;
                case 2:
                    bits = new BitBuffer(buf.readBytes(5));
                    // point attribute
                    bits.readUnsigned(2);
                    // fix type
                    bits.readUnsigned(1);
                    speed += bits.readSigned(7);
                    heading += bits.readSigned(7);
                    longitude += bits.readSigned(12);
                    latitude += bits.readSigned(11);
                    break;
                default:
                    // invalid or same
                    buf.readUnsignedByte();
                    continue;
            }
            position.setValid(true);
            position.setTime(new Date(time * 1000));
            position.setSpeed(UnitsConverter.knotsFromKph(speed * 0.1));
            position.setCourse(heading);
            position.setLongitude(longitude * 0.000001);
            position.setLatitude(latitude * 0.000001);
            positions.add(position);
        }
    } else {
        int count = buf.readUnsignedByte();
        for (int i = 0; i < count; i++) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());
            position.set(Position.KEY_BATTERY_LEVEL, battery);
            position.set(Position.KEY_POWER, power);
            position.set(Position.KEY_SATELLITES, satellites);
            int hdop = buf.readUnsignedByte();
            position.setValid(hdop > 0);
            position.set(Position.KEY_HDOP, hdop);
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedMedium() * 0.1));
            position.setCourse(buf.readUnsignedShort());
            position.setAltitude(buf.readShort());
            position.setLongitude(buf.readInt() * 0.000001);
            position.setLatitude(buf.readInt() * 0.000001);
            position.setTime(decodeTime(buf));
            position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort())));
            // reserved
            buf.readUnsignedByte();
            positions.add(position);
        }
    }
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) LinkedList(java.util.LinkedList) BitBuffer(org.traccar.helper.BitBuffer) Date(java.util.Date)

Example 3 with BitBuffer

use of org.traccar.helper.BitBuffer in project traccar by tananaev.

the class Jt600ProtocolDecoder method decodeBinary.

private List<Position> decodeBinary(ChannelBuffer buf, Channel channel, SocketAddress remoteAddress) {
    List<Position> positions = new LinkedList<>();
    // header
    buf.readByte();
    boolean longFormat = buf.getUnsignedByte(buf.readerIndex()) == 0x75;
    String id = String.valueOf(Long.parseLong(ChannelBuffers.hexDump(buf.readBytes(5))));
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }
    int protocolVersion = 0;
    if (longFormat) {
        protocolVersion = buf.readUnsignedByte();
    }
    int version = BitUtil.from(buf.readUnsignedByte(), 4);
    // length
    buf.readUnsignedShort();
    while (buf.readableBytes() > 1) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        DateBuilder dateBuilder = new DateBuilder().setDay(BcdUtil.readInteger(buf, 2)).setMonth(BcdUtil.readInteger(buf, 2)).setYear(BcdUtil.readInteger(buf, 2)).setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)).setSecond(BcdUtil.readInteger(buf, 2));
        position.setTime(dateBuilder.getDate());
        double latitude = convertCoordinate(BcdUtil.readInteger(buf, 8));
        double longitude = convertCoordinate(BcdUtil.readInteger(buf, 9));
        byte flags = buf.readByte();
        position.setValid((flags & 0x1) == 0x1);
        if ((flags & 0x2) == 0) {
            latitude = -latitude;
        }
        position.setLatitude(latitude);
        if ((flags & 0x4) == 0) {
            longitude = -longitude;
        }
        position.setLongitude(longitude);
        position.setSpeed(BcdUtil.readInteger(buf, 2));
        position.setCourse(buf.readUnsignedByte() * 2.0);
        if (longFormat) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
            // vehicle id combined
            buf.readUnsignedInt();
            position.set(Position.KEY_STATUS, buf.readUnsignedShort());
            int battery = buf.readUnsignedByte();
            if (battery == 0xff) {
                position.set(Position.KEY_CHARGE, true);
            } else {
                position.set(Position.KEY_BATTERY_LEVEL, battery);
            }
            CellTower cellTower = CellTower.fromCidLac(buf.readUnsignedShort(), buf.readUnsignedShort());
            cellTower.setSignalStrength((int) buf.readUnsignedByte());
            position.setNetwork(new Network(cellTower));
            if (protocolVersion == 0x17) {
                // geofence id
                buf.readUnsignedByte();
                // reserved
                buf.skipBytes(3);
            }
        } else if (version == 1) {
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
            position.set(Position.KEY_POWER, buf.readUnsignedByte());
            // other flags and sensors
            buf.readByte();
            position.setAltitude(buf.readUnsignedShort());
            int cid = buf.readUnsignedShort();
            int lac = buf.readUnsignedShort();
            int rssi = buf.readUnsignedByte();
            if (cid != 0 && lac != 0) {
                CellTower cellTower = CellTower.fromCidLac(cid, lac);
                cellTower.setSignalStrength(rssi);
                position.setNetwork(new Network(cellTower));
            } else {
                position.set(Position.KEY_RSSI, rssi);
            }
        } else if (version == 2) {
            int fuel = buf.readUnsignedByte() << 8;
            position.set(Position.KEY_STATUS, buf.readUnsignedInt());
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
            fuel += buf.readUnsignedByte();
            position.set(Position.KEY_FUEL_LEVEL, fuel);
        } else if (version == 3) {
            BitBuffer bitBuffer = new BitBuffer(buf);
            position.set("fuel1", bitBuffer.readUnsigned(12));
            position.set("fuel2", bitBuffer.readUnsigned(12));
            position.set("fuel3", bitBuffer.readUnsigned(12));
            position.set(Position.KEY_ODOMETER, bitBuffer.readUnsigned(20) * 1000);
            int status = bitBuffer.readUnsigned(24);
            position.set(Position.KEY_IGNITION, BitUtil.check(status, 0));
            position.set(Position.KEY_STATUS, status);
        }
        positions.add(position);
    }
    // index
    buf.readUnsignedByte();
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) CellTower(org.traccar.model.CellTower) LinkedList(java.util.LinkedList) Network(org.traccar.model.Network) BitBuffer(org.traccar.helper.BitBuffer)

Aggregations

BitBuffer (org.traccar.helper.BitBuffer)3 Position (org.traccar.model.Position)3 LinkedList (java.util.LinkedList)2 DeviceSession (org.traccar.DeviceSession)2 Network (org.traccar.model.Network)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 DateBuilder (org.traccar.helper.DateBuilder)1 Parser (org.traccar.helper.Parser)1 CellTower (org.traccar.model.CellTower)1