Search in sources :

Example 26 with Position

use of org.traccar.model.Position in project traccar by tananaev.

the class AdmProtocolDecoder method decodeData.

private Position decodeData(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf, int type) {
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }
    if (BitUtil.to(type, 2) == 0) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
        position.set(Position.KEY_INDEX, buf.readUnsignedShort());
        int status = buf.readUnsignedShort();
        position.set(Position.KEY_STATUS, status);
        position.setValid(!BitUtil.check(status, 5));
        position.setLatitude(buf.readFloat());
        position.setLongitude(buf.readFloat());
        position.setCourse(buf.readUnsignedShort() * 0.1);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort() * 0.1));
        position.set(Position.KEY_ACCELERATION, buf.readUnsignedByte() * 0.1);
        position.setAltitude(buf.readUnsignedShort());
        position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
        position.set(Position.KEY_SATELLITES, buf.readUnsignedByte() & 0x0f);
        position.setTime(new Date(buf.readUnsignedInt() * 1000));
        position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
        position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
        if (BitUtil.check(type, 2)) {
            // vib
            buf.readUnsignedByte();
            // vib_count
            buf.readUnsignedByte();
            int out = buf.readUnsignedByte();
            for (int i = 0; i <= 3; i++) {
                position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(out, i) ? 1 : 0);
            }
            // in_alarm
            buf.readUnsignedByte();
        }
        if (BitUtil.check(type, 3)) {
            for (int i = 1; i <= 6; i++) {
                position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort() * 0.001);
            }
        }
        if (BitUtil.check(type, 4)) {
            for (int i = 1; i <= 2; i++) {
                position.set(Position.PREFIX_COUNT + i, buf.readUnsignedInt());
            }
        }
        if (BitUtil.check(type, 5)) {
            for (int i = 1; i <= 3; i++) {
                // fuel level
                buf.readUnsignedShort();
            }
            for (int i = 1; i <= 3; i++) {
                position.set(Position.PREFIX_TEMP + i, buf.readUnsignedByte());
            }
        }
        if (BitUtil.check(type, 6)) {
            buf.skipBytes(buf.getUnsignedByte(buf.readerIndex()));
        }
        if (BitUtil.check(type, 7)) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
        }
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Date(java.util.Date)

Example 27 with Position

use of org.traccar.model.Position in project traccar by tananaev.

the class AlematicsProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    Parser parser = new Parser(PATTERN, (String) msg);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.set(Position.KEY_TYPE, parser.nextInt());
    position.set(Position.KEY_INDEX, parser.nextInt());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.setFixTime(parser.nextDateTime());
    position.setDeviceTime(parser.nextDateTime());
    position.setValid(true);
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextInt(0)));
    position.setCourse(parser.nextInt(0));
    position.setAltitude(parser.nextInt(0));
    position.set(Position.KEY_HDOP, parser.nextDouble());
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    position.set(Position.KEY_INPUT, parser.nextInt());
    position.set(Position.KEY_OUTPUT, parser.nextInt());
    position.set(Position.PREFIX_ADC + 1, parser.nextDouble());
    position.set(Position.KEY_POWER, parser.nextDouble());
    position.set(Position.KEY_ODOMETER, parser.nextInt());
    if (parser.hasNext()) {
        position.set("text", parser.next());
    } else {
        decodeExtras(position, parser);
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 28 with Position

use of org.traccar.model.Position in project traccar by tananaev.

the class ApelProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    int type = buf.readUnsignedShort();
    boolean alarm = (type & 0x8000) != 0;
    type = type & 0x7FFF;
    // length
    buf.readUnsignedShort();
    if (alarm) {
        sendSimpleMessage(channel, MSG_ACK_ALARM);
    }
    if (type == MSG_TRACKER_ID) {
        // unsupported authentication type
        return null;
    }
    if (type == MSG_TRACKER_ID_EXT) {
        // id
        buf.readUnsignedInt();
        int length = buf.readUnsignedShort();
        buf.skipBytes(length);
        length = buf.readUnsignedShort();
        getDeviceSession(channel, remoteAddress, buf.readBytes(length).toString(StandardCharsets.US_ASCII));
    } else if (type == MSG_LAST_LOG_INDEX) {
        long index = buf.readUnsignedInt();
        if (index > 0) {
            newIndex = index;
            requestArchive(channel);
        }
    } else if (type == MSG_CURRENT_GPS_DATA || type == MSG_STATE_FULL_INFO_T104 || type == MSG_LOG_RECORDS) {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        List<Position> positions = new LinkedList<>();
        int recordCount = 1;
        if (type == MSG_LOG_RECORDS) {
            recordCount = buf.readUnsignedShort();
        }
        for (int j = 0; j < recordCount; j++) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());
            int subtype = type;
            if (type == MSG_LOG_RECORDS) {
                position.set(Position.KEY_ARCHIVE, true);
                lastIndex = buf.readUnsignedInt() + 1;
                position.set(Position.KEY_INDEX, lastIndex);
                subtype = buf.readUnsignedShort();
                if (subtype != MSG_CURRENT_GPS_DATA && subtype != MSG_STATE_FULL_INFO_T104) {
                    buf.skipBytes(buf.readUnsignedShort());
                    continue;
                }
                // length
                buf.readUnsignedShort();
            }
            position.setTime(new Date(buf.readUnsignedInt() * 1000));
            position.setLatitude(buf.readInt() * 180.0 / 0x7FFFFFFF);
            position.setLongitude(buf.readInt() * 180.0 / 0x7FFFFFFF);
            if (subtype == MSG_STATE_FULL_INFO_T104) {
                int speed = buf.readUnsignedByte();
                position.setValid(speed != 255);
                position.setSpeed(UnitsConverter.knotsFromKph(speed));
                position.set(Position.KEY_HDOP, buf.readByte());
            } else {
                int speed = buf.readShort();
                position.setValid(speed != -1);
                position.setSpeed(UnitsConverter.knotsFromKph(speed * 0.01));
            }
            position.setCourse(buf.readShort() * 0.01);
            position.setAltitude(buf.readShort());
            if (subtype == MSG_STATE_FULL_INFO_T104) {
                position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
                position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                position.set(Position.KEY_EVENT, buf.readUnsignedShort());
                position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
                position.set(Position.KEY_INPUT, buf.readUnsignedByte());
                position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());
                for (int i = 1; i <= 8; i++) {
                    position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort());
                }
                position.set(Position.PREFIX_COUNT + 1, buf.readUnsignedInt());
                position.set(Position.PREFIX_COUNT + 2, buf.readUnsignedInt());
                position.set(Position.PREFIX_COUNT + 3, buf.readUnsignedInt());
            }
            positions.add(position);
        }
        // crc
        buf.readUnsignedInt();
        if (type == MSG_LOG_RECORDS) {
            requestArchive(channel);
        } else {
            sendSimpleMessage(channel, MSG_REQUEST_LAST_LOG_INDEX);
        }
        return positions;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) LinkedList(java.util.LinkedList) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 29 with Position

use of org.traccar.model.Position in project traccar by tananaev.

the class BceProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    String imei = String.format("%015d", buf.readLong());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    List<Position> positions = new LinkedList<>();
    while (buf.readableBytes() > 1) {
        int dataEnd = buf.readUnsignedShort() + buf.readerIndex();
        int type = buf.readUnsignedByte();
        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.readUnsignedInt();
            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.readUnsignedShort();
                    masks.add(mask);
                } while (BitUtil.check(mask, 15));
                mask = masks.get(0);
                if (BitUtil.check(mask, 0)) {
                    position.setValid(true);
                    position.setLongitude(buf.readFloat());
                    position.setLatitude(buf.readFloat());
                    position.setSpeed(buf.readUnsignedByte());
                    int gps = buf.readUnsignedByte();
                    position.set(Position.KEY_SATELLITES, gps & 0xf);
                    position.set(Position.KEY_HDOP, gps >> 4);
                    position.setCourse(buf.readUnsignedByte());
                    position.setAltitude(buf.readUnsignedShort());
                    position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
                }
                if (BitUtil.check(mask, 1)) {
                    position.set(Position.KEY_INPUT, buf.readUnsignedShort());
                }
                for (int i = 1; i <= 8; i++) {
                    if (BitUtil.check(mask, i + 1)) {
                        position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort());
                    }
                }
                if (BitUtil.check(mask, 10)) {
                    buf.skipBytes(4);
                }
                if (BitUtil.check(mask, 11)) {
                    buf.skipBytes(4);
                }
                if (BitUtil.check(mask, 12)) {
                    buf.skipBytes(2);
                }
                if (BitUtil.check(mask, 13)) {
                    buf.skipBytes(2);
                }
                if (BitUtil.check(mask, 14)) {
                    position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedByte(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedByte())));
                    buf.readUnsignedByte();
                }
                if (BitUtil.check(mask, 0)) {
                    positions.add(position);
                }
            }
            buf.readerIndex(structEnd);
        }
        // Send response
        if (type == MSG_ASYNC_STACK && channel != null) {
            ChannelBuffer response = ChannelBuffers.buffer(ByteOrder.LITTLE_ENDIAN, 8 + 2 + 2 + 1);
            response.writeLong(Long.parseLong(imei));
            response.writeShort(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.write(response);
        }
    }
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) LinkedList(java.util.LinkedList) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 30 with Position

use of org.traccar.model.Position in project traccar by tananaev.

the class BlackKiteProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // header
    buf.readUnsignedByte();
    int length = (buf.readUnsignedShort() & 0x7fff) + 3;
    List<Position> positions = new LinkedList<>();
    Set<Integer> tags = new HashSet<>();
    boolean hasLocation = false;
    Position position = new Position(getProtocolName());
    while (buf.readerIndex() < length) {
        // Check if new message started
        int tag = buf.readUnsignedByte();
        if (tags.contains(tag)) {
            if (hasLocation && position.getFixTime() != null) {
                positions.add(position);
            }
            tags.clear();
            hasLocation = false;
            position = new Position(getProtocolName());
        }
        tags.add(tag);
        switch(tag) {
            case TAG_IMEI:
                getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII));
                break;
            case TAG_DATE:
                position.setTime(new Date(buf.readUnsignedInt() * 1000));
                break;
            case TAG_COORDINATES:
                hasLocation = true;
                position.setValid((buf.readUnsignedByte() & 0xf0) == 0x00);
                position.setLatitude(buf.readInt() / 1000000.0);
                position.setLongitude(buf.readInt() / 1000000.0);
                break;
            case TAG_SPEED_COURSE:
                position.setSpeed(buf.readUnsignedShort() * 0.0539957);
                position.setCourse(buf.readUnsignedShort() * 0.1);
                break;
            case TAG_ALTITUDE:
                position.setAltitude(buf.readShort());
                break;
            case TAG_STATUS:
                int status = buf.readUnsignedShort();
                position.set(Position.KEY_IGNITION, BitUtil.check(status, 9));
                if (BitUtil.check(status, 15)) {
                    position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
                }
                position.set(Position.KEY_CHARGE, BitUtil.check(status, 2));
                break;
            case TAG_DIGITAL_INPUTS:
                int input = buf.readUnsignedShort();
                for (int i = 0; i < 16; i++) {
                    position.set(Position.PREFIX_IO + (i + 1), BitUtil.check(input, i));
                }
                break;
            case TAG_DIGITAL_OUTPUTS:
                int output = buf.readUnsignedShort();
                for (int i = 0; i < 16; i++) {
                    position.set(Position.PREFIX_IO + (i + 17), BitUtil.check(output, i));
                }
                break;
            case TAG_INPUT_VOLTAGE1:
                position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort() / 1000.0);
                break;
            case TAG_INPUT_VOLTAGE2:
                position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort() / 1000.0);
                break;
            case TAG_INPUT_VOLTAGE3:
                position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort() / 1000.0);
                break;
            case TAG_INPUT_VOLTAGE4:
                position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShort() / 1000.0);
                break;
            case TAG_XT1:
            case TAG_XT2:
            case TAG_XT3:
                buf.skipBytes(16);
                break;
            default:
                break;
        }
    }
    if (hasLocation && position.getFixTime() != null) {
        positions.add(position);
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }
    sendReply(channel, buf.readUnsignedShort());
    for (Position p : positions) {
        p.setDeviceId(deviceSession.getDeviceId());
    }
    if (positions.isEmpty()) {
        return null;
    }
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) LinkedList(java.util.LinkedList) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) HashSet(java.util.HashSet)

Aggregations

Position (org.traccar.model.Position)301 DeviceSession (org.traccar.DeviceSession)188 Parser (org.traccar.helper.Parser)129 DateBuilder (org.traccar.helper.DateBuilder)70 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)62 Network (org.traccar.model.Network)49 Date (java.util.Date)47 LinkedList (java.util.LinkedList)37 Event (org.traccar.model.Event)25 Test (org.junit.Test)22 BaseTest (org.traccar.BaseTest)16 WifiAccessPoint (org.traccar.model.WifiAccessPoint)16 SimpleDateFormat (java.text.SimpleDateFormat)13 List (java.util.List)11 TripsConfig (org.traccar.reports.model.TripsConfig)11 DateFormat (java.text.DateFormat)10 Device (org.traccar.model.Device)10 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)9 DeviceState (org.traccar.model.DeviceState)9 StopReport (org.traccar.reports.model.StopReport)9