Search in sources :

Example 31 with DateBuilder

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

the class NavisProtocolDecoder method parsePosition.

private ParseResult parsePosition(DeviceSession deviceSession, ChannelBuffer buf) {
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    int format;
    if (buf.getUnsignedByte(buf.readerIndex()) == 0) {
        format = buf.readUnsignedShort();
    } else {
        format = buf.readUnsignedByte();
    }
    position.set("format", format);
    long index = buf.readUnsignedInt();
    position.set(Position.KEY_INDEX, index);
    position.set(Position.KEY_EVENT, buf.readUnsignedShort());
    // event time
    buf.skipBytes(6);
    short armedStatus = buf.readUnsignedByte();
    position.set(Position.KEY_ARMED, armedStatus & 0x7F);
    if (BitUtil.check(armedStatus, 7)) {
        position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
    }
    position.set(Position.KEY_STATUS, buf.readUnsignedByte());
    position.set(Position.KEY_RSSI, buf.readUnsignedByte());
    if (isFormat(format, F10, F20, F30)) {
        position.set(Position.KEY_OUTPUT, buf.readUnsignedShort());
    } else if (isFormat(format, F40, F50, F51, F52)) {
        position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());
    }
    if (isFormat(format, F10, F20, F30, F40)) {
        position.set(Position.KEY_INPUT, buf.readUnsignedShort());
    } else if (isFormat(format, F50, F51, F52)) {
        position.set(Position.KEY_INPUT, buf.readUnsignedByte());
    }
    position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
    position.set(Position.KEY_BATTERY, buf.readUnsignedShort());
    if (isFormat(format, F10, F20, F30)) {
        position.set(Position.PREFIX_TEMP + 1, buf.readShort());
    }
    if (isFormat(format, F10, F20, F50, F52)) {
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());
    }
    // Impulse counters
    if (isFormat(format, F20, F50, F51, F52)) {
        buf.readUnsignedInt();
        buf.readUnsignedInt();
    }
    if (isFormat(format, F20, F50, F51, F52)) {
        int locationStatus = buf.readUnsignedByte();
        position.setValid(BitUtil.check(locationStatus, 1));
        DateBuilder dateBuilder = new DateBuilder().setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte() + 1, buf.readUnsignedByte());
        position.setTime(dateBuilder.getDate());
        position.setLatitude(buf.readFloat() / Math.PI * 180);
        position.setLongitude(buf.readFloat() / Math.PI * 180);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloat()));
        position.setCourse(buf.readUnsignedShort());
        position.set(Position.KEY_ODOMETER, buf.readFloat() * 1000);
        position.set(Position.KEY_DISTANCE, buf.readFloat());
        // Segment times
        buf.readUnsignedShort();
        buf.readUnsignedShort();
    }
    // Other
    if (isFormat(format, F51, F52)) {
        buf.readUnsignedShort();
        buf.readByte();
        buf.readUnsignedShort();
        buf.readUnsignedShort();
        buf.readByte();
        buf.readUnsignedShort();
        buf.readUnsignedShort();
        buf.readByte();
        buf.readUnsignedShort();
    }
    // Four temperature sensors
    if (isFormat(format, F40, F52)) {
        buf.readByte();
        buf.readByte();
        buf.readByte();
        buf.readByte();
    }
    return new ParseResult(index, position);
}
Also used : Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder)

Example 32 with DateBuilder

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

the class NoranProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // length
    buf.readUnsignedShort();
    int type = buf.readUnsignedShort();
    if (type == MSG_SHAKE_HAND && channel != null) {
        ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 13);
        response.writeBytes(ChannelBuffers.copiedBuffer(ByteOrder.LITTLE_ENDIAN, "\r\n*KW", StandardCharsets.US_ASCII));
        response.writeByte(0);
        response.writeShort(response.capacity());
        response.writeShort(MSG_SHAKE_HAND_RESPONSE);
        // status
        response.writeByte(1);
        response.writeBytes(ChannelBuffers.copiedBuffer(ByteOrder.LITTLE_ENDIAN, "\r\n", StandardCharsets.US_ASCII));
        channel.write(response, remoteAddress);
    } else if (type == MSG_UPLOAD_POSITION || type == MSG_UPLOAD_POSITION_NEW || type == MSG_CONTROL_RESPONSE || type == MSG_ALARM) {
        boolean newFormat = false;
        if (type == MSG_UPLOAD_POSITION && buf.readableBytes() == 48 || type == MSG_ALARM && buf.readableBytes() == 48 || type == MSG_CONTROL_RESPONSE && buf.readableBytes() == 57) {
            newFormat = true;
        }
        Position position = new Position(getProtocolName());
        if (type == MSG_CONTROL_RESPONSE) {
            // GIS ip
            buf.readUnsignedInt();
            // GIS port
            buf.readUnsignedInt();
        }
        position.setValid(BitUtil.check(buf.readUnsignedByte(), 0));
        short alarm = buf.readUnsignedByte();
        switch(alarm) {
            case 1:
                position.set(Position.KEY_ALARM, Position.ALARM_SOS);
                break;
            case 2:
                position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED);
                break;
            case 3:
                position.set(Position.KEY_ALARM, Position.ALARM_GEOFENCE_EXIT);
                break;
            case 9:
                position.set(Position.KEY_ALARM, Position.ALARM_POWER_OFF);
                break;
            default:
                break;
        }
        if (newFormat) {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedInt()));
            position.setCourse(buf.readFloat());
        } else {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
            position.setCourse(buf.readUnsignedShort());
        }
        position.setLongitude(buf.readFloat());
        position.setLatitude(buf.readFloat());
        if (!newFormat) {
            long timeValue = buf.readUnsignedInt();
            DateBuilder dateBuilder = new DateBuilder().setYear((int) BitUtil.from(timeValue, 26)).setMonth((int) BitUtil.between(timeValue, 22, 26)).setDay((int) BitUtil.between(timeValue, 17, 22)).setHour((int) BitUtil.between(timeValue, 12, 17)).setMinute((int) BitUtil.between(timeValue, 6, 12)).setSecond((int) BitUtil.to(timeValue, 6));
            position.setTime(dateBuilder.getDate());
        }
        ChannelBuffer rawId;
        if (newFormat) {
            rawId = buf.readBytes(12);
        } else {
            rawId = buf.readBytes(11);
        }
        String id = rawId.toString(StandardCharsets.US_ASCII).replaceAll("[^\\p{Print}]", "");
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());
        if (newFormat) {
            DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            position.setTime(dateFormat.parse(buf.readBytes(17).toString(StandardCharsets.US_ASCII)));
            buf.readByte();
        }
        if (!newFormat) {
            position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
            position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte());
        } else if (type == MSG_UPLOAD_POSITION_NEW) {
            position.set(Position.PREFIX_TEMP + 1, buf.readShort());
            position.set(Position.KEY_ODOMETER, buf.readFloat());
        }
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 33 with DateBuilder

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

the class OigoProtocolDecoder method decodeMgMessage.

private Position decodeMgMessage(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
    // tag
    buf.readUnsignedByte();
    int flags = buf.getUnsignedByte(buf.readerIndex());
    DeviceSession deviceSession;
    if (BitUtil.check(flags, 6)) {
        // flags
        buf.readUnsignedByte();
        deviceSession = getDeviceSession(channel, remoteAddress);
    } else {
        String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
        deviceSession = getDeviceSession(channel, remoteAddress, imei);
    }
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    // imsi
    buf.skipBytes(8);
    int date = buf.readUnsignedShort();
    DateBuilder dateBuilder = new DateBuilder().setDate(2010 + BitUtil.from(date, 12), BitUtil.between(date, 8, 12), BitUtil.to(date, 8)).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), 0);
    position.setValid(true);
    position.setLatitude(convertCoordinate(buf.readInt()));
    position.setLongitude(convertCoordinate(buf.readInt()));
    position.setAltitude(UnitsConverter.metersFromFeet(buf.readShort()));
    position.setCourse(buf.readUnsignedShort());
    position.setSpeed(UnitsConverter.knotsFromMph(buf.readUnsignedByte()));
    position.set(Position.KEY_POWER, buf.readUnsignedByte() * 0.1);
    position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
    dateBuilder.setSecond(buf.readUnsignedByte());
    position.setTime(dateBuilder.getDate());
    position.set(Position.KEY_RSSI, buf.readUnsignedByte());
    int index = buf.readUnsignedByte();
    position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
    position.set(Position.KEY_ODOMETER, (long) (buf.readUnsignedInt() * 1609.34));
    if (channel != null && BitUtil.check(flags, 7)) {
        ChannelBuffer response = ChannelBuffers.dynamicBuffer();
        response.writeByte(MSG_ACKNOWLEDGEMENT);
        response.writeByte(index);
        response.writeByte(0x00);
        channel.write(response, remoteAddress);
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 34 with DateBuilder

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

the class PricolProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // header
    buf.readUnsignedByte();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, buf.readBytes(7).toString(StandardCharsets.US_ASCII));
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.set("eventType", buf.readUnsignedByte());
    position.set("packetVersion", buf.readUnsignedByte());
    position.set(Position.KEY_STATUS, buf.readUnsignedByte());
    position.set(Position.KEY_RSSI, buf.readUnsignedByte());
    position.set(Position.KEY_GPS, buf.readUnsignedByte());
    position.setTime(new DateBuilder().setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).getDate());
    position.setValid(true);
    double lat = buf.getUnsignedShort(buf.readerIndex()) / 100;
    lat += (buf.readUnsignedShort() % 100 * 10000 + buf.readUnsignedShort()) / 600000.0;
    position.setLatitude(buf.readUnsignedByte() == 'S' ? -lat : lat);
    double lon = buf.getUnsignedMedium(buf.readerIndex()) / 100;
    lon += (buf.readUnsignedMedium() % 100 * 10000 + buf.readUnsignedShort()) / 600000.0;
    position.setLongitude(buf.readUnsignedByte() == 'W' ? -lon : lon);
    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
    position.set(Position.KEY_INPUT, buf.readUnsignedShort());
    position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());
    position.set("analogAlerts", buf.readUnsignedByte());
    position.set("customAlertTypes", buf.readUnsignedShort());
    for (int i = 1; i <= 5; i++) {
        position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort());
    }
    position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium());
    position.set(Position.KEY_RPM, buf.readUnsignedShort());
    if (channel != null) {
        channel.write(ChannelBuffers.copiedBuffer("ACK", StandardCharsets.US_ASCII), remoteAddress);
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 35 with DateBuilder

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

the class Pt3000ProtocolDecoder 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());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    position.setTime(dateBuilder.getDate());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Parser(org.traccar.helper.Parser)

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