Search in sources :

Example 71 with DateBuilder

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

the class UlbotechProtocolDecoder method decodeText.

private Object decodeText(Channel channel, SocketAddress remoteAddress, String sentence) {
    Parser parser = new Parser(PATTERN, sentence);
    if (!parser.matches()) {
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)).setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    getLastLocation(position, dateBuilder.getDate());
    position.set(Position.KEY_RESULT, parser.next());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Parser(org.traccar.helper.Parser)

Example 72 with DateBuilder

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

the class TzoneProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // header
    buf.skipBytes(2);
    // length
    buf.readUnsignedShort();
    if (buf.readUnsignedShort() != 0x2424) {
        return null;
    }
    int hardware = buf.readUnsignedShort();
    long firmware = buf.readUnsignedInt();
    String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.set(Position.KEY_VERSION_HW, hardware);
    position.set(Position.KEY_VERSION_FW, firmware);
    position.setDeviceTime(new DateBuilder().setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).getDate());
    // GPS info
    int blockLength = buf.readUnsignedShort();
    int blockEnd = buf.readerIndex() + blockLength;
    if (blockLength < 22) {
        return null;
    }
    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
    double lat;
    double lon;
    if (hardware == 0x10A || hardware == 0x10B) {
        lat = buf.readUnsignedInt() / 600000.0;
        lon = buf.readUnsignedInt() / 600000.0;
    } else {
        lat = buf.readUnsignedInt() / 100000.0 / 60.0;
        lon = buf.readUnsignedInt() / 100000.0 / 60.0;
    }
    position.setFixTime(new DateBuilder().setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).getDate());
    position.setSpeed(buf.readUnsignedShort() * 0.01);
    position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium());
    int flags = buf.readUnsignedShort();
    position.setCourse(BitUtil.to(flags, 9));
    if (!BitUtil.check(flags, 10)) {
        lat = -lat;
    }
    position.setLatitude(lat);
    if (BitUtil.check(flags, 9)) {
        lon = -lon;
    }
    position.setLongitude(lon);
    position.setValid(BitUtil.check(flags, 11));
    buf.readerIndex(blockEnd);
    // LBS info
    blockLength = buf.readUnsignedShort();
    blockEnd = buf.readerIndex() + blockLength;
    if (blockLength > 0 && (hardware == 0x10A || hardware == 0x10B)) {
        position.setNetwork(new Network(CellTower.fromLacCid(buf.readUnsignedShort(), buf.readUnsignedShort())));
    }
    buf.readerIndex(blockEnd);
    // Status info
    blockLength = buf.readUnsignedShort();
    blockEnd = buf.readerIndex() + blockLength;
    if (blockLength >= 13) {
        position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte()));
        position.set("terminalInfo", buf.readUnsignedByte());
        int status = buf.readUnsignedByte();
        position.set(Position.PREFIX_OUT + 1, BitUtil.check(status, 0));
        position.set(Position.PREFIX_OUT + 2, BitUtil.check(status, 1));
        status = buf.readUnsignedByte();
        position.set(Position.PREFIX_IN + 1, BitUtil.check(status, 4));
        if (BitUtil.check(status, 0)) {
            position.set(Position.KEY_ALARM, Position.ALARM_SOS);
        }
        position.set(Position.KEY_RSSI, buf.readUnsignedByte());
        position.set("gsmStatus", buf.readUnsignedByte());
        position.set(Position.KEY_BATTERY, buf.readUnsignedShort());
        position.set(Position.KEY_POWER, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());
    }
    if (blockLength >= 15) {
        position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedShort());
    }
    buf.readerIndex(blockEnd);
    if (hardware == 0x10A || hardware == 0x10B) {
        decodeCards(position, buf);
        // temperature
        buf.skipBytes(buf.readUnsignedShort());
        // lock
        buf.skipBytes(buf.readUnsignedShort());
        decodePassengers(position, buf);
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Network(org.traccar.model.Network) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 73 with DateBuilder

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

the class AstraProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    if (channel != null) {
        channel.write(ChannelBuffers.wrappedBuffer(new byte[] { 0x06 }), remoteAddress);
    }
    // protocol
    buf.readUnsignedByte();
    // length
    buf.readUnsignedShort();
    String imei = String.format("%08d", buf.readUnsignedInt()) + String.format("%07d", buf.readUnsignedMedium());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    List<Position> positions = new LinkedList<>();
    while (buf.readableBytes() > 2) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        // index
        buf.readUnsignedByte();
        position.setValid(true);
        position.setLatitude(buf.readInt() * 0.000001);
        position.setLongitude(buf.readInt() * 0.000001);
        DateBuilder dateBuilder = new DateBuilder().setDate(1980, 1, 6).addMillis(buf.readUnsignedInt() * 1000L);
        position.setTime(dateBuilder.getDate());
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte() * 2));
        position.setCourse(buf.readUnsignedByte() * 2);
        int reason = buf.readUnsignedMedium();
        position.set(Position.KEY_EVENT, reason);
        int status = buf.readUnsignedShort();
        position.set(Position.KEY_STATUS, status);
        position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedByte());
        position.set(Position.KEY_BATTERY, buf.readUnsignedByte());
        position.set(Position.KEY_POWER, buf.readUnsignedByte());
        // max journey speed
        buf.readUnsignedByte();
        // accelerometer
        buf.skipBytes(6);
        position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedShort());
        // journey idle time
        buf.readUnsignedShort();
        position.setAltitude(buf.readUnsignedByte() * 20);
        int quality = buf.readUnsignedByte();
        position.set(Position.KEY_SATELLITES, quality & 0xf);
        position.set(Position.KEY_RSSI, quality >> 4);
        // geofence events
        buf.readUnsignedByte();
        if (BitUtil.check(status, 8)) {
            position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readBytes(7).toString(StandardCharsets.US_ASCII));
            position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 1000);
            position.set(Position.KEY_HOURS, buf.readUnsignedShort());
        }
        if (BitUtil.check(status, 6)) {
            Log.warning("Extension data is not supported");
            return position;
        }
        positions.add(position);
    }
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) LinkedList(java.util.LinkedList) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 74 with DateBuilder

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

the class AtrackProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    if (buf.getUnsignedShort(buf.readerIndex()) == 0xfe02) {
        if (channel != null) {
            // keep-alive message
            channel.write(buf, remoteAddress);
        }
        return null;
    } else if (buf.getByte(buf.readerIndex()) == '$') {
        return decodeString(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII).trim());
    }
    // prefix
    buf.skipBytes(2);
    // checksum
    buf.readUnsignedShort();
    // length
    buf.readUnsignedShort();
    int index = buf.readUnsignedShort();
    long id = buf.readLong();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(id));
    if (deviceSession == null) {
        return null;
    }
    sendResponse(channel, remoteAddress, id, index);
    List<Position> positions = new LinkedList<>();
    while (buf.readableBytes() >= MIN_DATA_LENGTH) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        if (longDate) {
            DateBuilder dateBuilder = new DateBuilder().setDate(buf.readUnsignedShort(), buf.readUnsignedByte(), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
            position.setTime(dateBuilder.getDate());
            buf.skipBytes(7 + 7);
        } else {
            position.setFixTime(new Date(buf.readUnsignedInt() * 1000));
            position.setDeviceTime(new Date(buf.readUnsignedInt() * 1000));
            // send time
            buf.readUnsignedInt();
        }
        position.setValid(true);
        position.setLongitude(buf.readInt() * 0.000001);
        position.setLatitude(buf.readInt() * 0.000001);
        position.setCourse(buf.readUnsignedShort());
        int type = buf.readUnsignedByte();
        position.set(Position.KEY_TYPE, type);
        position.set(Position.KEY_ALARM, alarmMap.get(type));
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 100);
        position.set(Position.KEY_HDOP, buf.readUnsignedShort() * 0.1);
        position.set(Position.KEY_INPUT, buf.readUnsignedByte());
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
        position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort() * 0.001);
        position.set(Position.KEY_DRIVER_UNIQUE_ID, readString(buf));
        position.set(Position.PREFIX_TEMP + 1, buf.readShort() * 0.1);
        position.set(Position.PREFIX_TEMP + 2, buf.readShort() * 0.1);
        String message = readString(buf);
        if (message != null && !message.isEmpty()) {
            Pattern pattern = Pattern.compile("FULS:F=(\\p{XDigit}+) t=(\\p{XDigit}+) N=(\\p{XDigit}+)");
            Matcher matcher = pattern.matcher(message);
            if (matcher.find()) {
                int value = Integer.parseInt(matcher.group(3), decimalFuel ? 10 : 16);
                position.set(Position.KEY_FUEL_LEVEL, value * 0.1);
            } else {
                position.set("message", message);
            }
        }
        if (custom) {
            String form = this.form;
            if (form == null) {
                form = readString(buf).substring("%CI".length());
            }
            readCustomData(position, buf, form);
        }
        positions.add(position);
    }
    return positions;
}
Also used : Pattern(java.util.regex.Pattern) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Matcher(java.util.regex.Matcher) LinkedList(java.util.LinkedList) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 75 with DateBuilder

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

the class AutoFonProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    int type = buf.readUnsignedByte();
    if (type == MSG_LOGIN || type == MSG_45_LOGIN) {
        if (type == MSG_LOGIN) {
            // hardware version
            buf.readUnsignedByte();
            // software version
            buf.readUnsignedByte();
        }
        String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
        if (deviceSession != null && channel != null) {
            ChannelBuffer response = ChannelBuffers.dynamicBuffer();
            response.writeBytes("resp_crc=".getBytes(StandardCharsets.US_ASCII));
            response.writeByte(buf.getByte(buf.writerIndex() - 1));
            channel.write(response);
        }
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }
    if (type == MSG_LOCATION) {
        return decodePosition(deviceSession, buf, false);
    } else if (type == MSG_HISTORY) {
        int count = buf.readUnsignedByte() & 0x0f;
        // total count
        buf.readUnsignedShort();
        List<Position> positions = new LinkedList<>();
        for (int i = 0; i < count; i++) {
            positions.add(decodePosition(deviceSession, buf, true));
        }
        return positions;
    } else if (type == MSG_45_LOCATION) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        short status = buf.readUnsignedByte();
        if (BitUtil.check(status, 7)) {
            position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
        }
        position.set(Position.KEY_BATTERY, BitUtil.to(status, 7));
        // remaining time
        buf.skipBytes(2);
        position.set(Position.PREFIX_TEMP + 1, buf.readByte());
        // timer (interval and units)
        buf.skipBytes(2);
        // mode
        buf.readByte();
        // gprs sending interval
        buf.readByte();
        // mcc, mnc, lac, cid
        buf.skipBytes(6);
        int valid = buf.readUnsignedByte();
        position.setValid(BitUtil.from(valid, 6) != 0);
        position.set(Position.KEY_SATELLITES, BitUtil.from(valid, 6));
        int time = buf.readUnsignedMedium();
        int date = buf.readUnsignedMedium();
        DateBuilder dateBuilder = new DateBuilder().setTime(time / 10000, time / 100 % 100, time % 100).setDateReverse(date / 10000, date / 100 % 100, date % 100);
        position.setTime(dateBuilder.getDate());
        position.setLatitude(convertCoordinate(buf.readUnsignedByte(), buf.readUnsignedMedium()));
        position.setLongitude(convertCoordinate(buf.readUnsignedByte(), buf.readUnsignedMedium()));
        position.setSpeed(buf.readUnsignedByte());
        position.setCourse(buf.readUnsignedShort());
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) List(java.util.List) LinkedList(java.util.LinkedList) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

DateBuilder (org.traccar.helper.DateBuilder)75 Position (org.traccar.model.Position)70 DeviceSession (org.traccar.DeviceSession)63 Parser (org.traccar.helper.Parser)41 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)26 Network (org.traccar.model.Network)16 LinkedList (java.util.LinkedList)9 Date (java.util.Date)5 WifiAccessPoint (org.traccar.model.WifiAccessPoint)5 List (java.util.List)3 Pattern (java.util.regex.Pattern)3 SimpleDateFormat (java.text.SimpleDateFormat)2 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)2 QueryStringDecoder (org.jboss.netty.handler.codec.http.QueryStringDecoder)2 CellTower (org.traccar.model.CellTower)2 DateFormat (java.text.DateFormat)1 Calendar (java.util.Calendar)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 ChannelBufferIndexFinder (org.jboss.netty.buffer.ChannelBufferIndexFinder)1