Search in sources :

Example 66 with Position

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

the class MegastekProtocolDecoder method decodeNew.

private Position decodeNew(Channel channel, SocketAddress remoteAddress, String sentence) {
    Parser parser = new Parser(PATTERN_NEW, 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());
    if (parser.next().equals("S")) {
        position.set(Position.KEY_ARCHIVE, true);
    }
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    position.set(Position.KEY_SATELLITES, parser.nextInt(0));
    position.set(Position.KEY_HDOP, parser.nextDouble(0));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.setAltitude(parser.nextDouble(0));
    if (parser.hasNext()) {
        position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000);
    }
    position.setNetwork(new Network(CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextHexInt(0), parser.nextHexInt(0), parser.nextInt(0))));
    position.set(Position.KEY_INPUT, parser.nextBinInt(0));
    position.set(Position.KEY_OUTPUT, parser.nextBinInt(0));
    for (int i = 1; i <= 3; i++) {
        position.set(Position.PREFIX_ADC + i, parser.nextInt(0));
    }
    for (int i = 1; i <= 2; i++) {
        String adc = parser.next();
        if (adc != null) {
            position.set(Position.PREFIX_TEMP + i, Double.parseDouble(adc));
        }
    }
    position.set(Position.KEY_DRIVER_UNIQUE_ID, parser.next());
    String battery = parser.next();
    if (battery != null) {
        position.set(Position.KEY_BATTERY, Integer.parseInt(battery));
    }
    position.set(Position.KEY_ALARM, decodeAlarm(parser.next()));
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) Parser(org.traccar.helper.Parser)

Example 67 with Position

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

the class RecodaProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    int type = buf.readInt();
    // length
    buf.readUnsignedInt();
    if (type != MSG_HEARTBEAT) {
        // version
        buf.readUnsignedShort();
        // index
        buf.readUnsignedShort();
    }
    if (type == MSG_SIGNAL_LINK_REGISTRATION) {
        getDeviceSession(channel, remoteAddress, buf.readBytes(12).toString(StandardCharsets.US_ASCII));
    } else if (type == MSG_GPS_DATA) {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.setTime(new Date(buf.readLong()));
        int flags = buf.readUnsignedByte();
        if (BitUtil.check(flags, 0)) {
            // declination
            buf.readUnsignedShort();
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
            position.setLongitude(buf.readUnsignedByte() + buf.readUnsignedByte() / 60.0);
            position.setLatitude(buf.readUnsignedByte() + buf.readUnsignedByte() / 60.0);
            position.setLongitude(position.getLongitude() + buf.readUnsignedInt() / 3600.0);
            position.setLatitude(position.getLatitude() + buf.readUnsignedInt() / 3600.0);
            int status = buf.readUnsignedByte();
            position.setValid(BitUtil.check(status, 0));
            if (BitUtil.check(status, 1)) {
                position.setLongitude(-position.getLongitude());
            }
            if (!BitUtil.check(status, 2)) {
                position.setLatitude(-position.getLatitude());
            }
        } else {
            getLastLocation(position, position.getDeviceTime());
        }
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 68 with Position

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

the class RitiProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // header
    buf.skipBytes(2);
    Position position = new Position(getProtocolName());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(buf.readUnsignedShort()));
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.set("mode", buf.readUnsignedByte());
    position.set(Position.KEY_COMMAND, buf.readUnsignedByte());
    position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
    // status
    buf.skipBytes(5);
    // idleCount
    buf.readUnsignedShort();
    // idleTime in seconds
    buf.readUnsignedShort();
    position.set(Position.KEY_DISTANCE, buf.readUnsignedInt());
    position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt());
    // Parse GPRMC
    int end = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*');
    String gprmc = buf.toString(buf.readerIndex(), end - buf.readerIndex(), StandardCharsets.US_ASCII);
    Parser parser = new Parser(PATTERN, gprmc);
    if (!parser.matches()) {
        return null;
    }
    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) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Parser(org.traccar.helper.Parser)

Example 69 with Position

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

the class SanavProtocolDecoder 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)

Example 70 with Position

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

the class Pt502ProtocolDecoder 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());
    String type = parser.next();
    if (type.startsWith("PHO") && channel != null) {
        photo = new byte[Integer.parseInt(type.substring(3))];
        channel.write("#PHD0," + Math.min(photo.length, MAX_CHUNK_SIZE) + "\r\n");
    }
    position.set(Position.KEY_ALARM, decodeAlarm(type));
    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), 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());
    position.set(Position.KEY_INPUT, parser.next());
    position.set(Position.KEY_OUTPUT, parser.next());
    if (parser.hasNext()) {
        String[] values = parser.next().split(",");
        for (int i = 0; i < values.length; i++) {
            position.set(Position.PREFIX_ADC + (i + 1), Integer.parseInt(values[i], 16));
        }
    }
    position.set(Position.KEY_ODOMETER, parser.nextInt(0));
    position.set(Position.KEY_DRIVER_UNIQUE_ID, parser.next());
    if (parser.hasNext()) {
        int value = parser.nextHexInt(0);
        position.set(Position.KEY_BATTERY, value >> 8);
        position.set(Position.KEY_RSSI, (value >> 4) & 0xf);
        position.set(Position.KEY_SATELLITES, value & 0xf);
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Parser(org.traccar.helper.Parser)

Aggregations

Position (org.traccar.model.Position)831 DeviceSession (org.traccar.DeviceSession)505 Parser (org.traccar.helper.Parser)319 DateBuilder (org.traccar.helper.DateBuilder)179 Date (java.util.Date)154 Network (org.traccar.model.Network)143 LinkedList (java.util.LinkedList)112 ByteBuf (io.netty.buffer.ByteBuf)105 Event (org.traccar.model.Event)75 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)69 NetworkMessage (org.traccar.NetworkMessage)67 Test (org.junit.Test)56 WifiAccessPoint (org.traccar.model.WifiAccessPoint)54 BaseTest (org.traccar.BaseTest)40 SimpleDateFormat (java.text.SimpleDateFormat)38 Device (org.traccar.model.Device)29 DateFormat (java.text.DateFormat)27 DeviceState (org.traccar.model.DeviceState)27 List (java.util.List)25 TripsConfig (org.traccar.reports.model.TripsConfig)25