Search in sources :

Example 1 with DatagramChannel

use of org.jboss.netty.channel.socket.DatagramChannel in project traccar by tananaev.

the class T55ProtocolDecoder method decodeGprmc.

private Position decodeGprmc(DeviceSession deviceSession, String sentence, SocketAddress remoteAddress, Channel channel) {
    if (deviceSession != null && channel != null && !(channel instanceof DatagramChannel) && Context.getIdentityManager().lookupAttributeBoolean(deviceSession.getDeviceId(), getProtocolName() + ".ack", false, true)) {
        channel.write("OK1\r\n");
    }
    Parser parser = new Parser(PATTERN_GPRMC, sentence);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    if (deviceSession != 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());
    if (parser.hasNext(5)) {
        position.set(Position.KEY_SATELLITES, parser.nextInt());
        deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());
        position.set(Position.KEY_IGNITION, parser.hasNext() && parser.next().equals("1"));
        position.set(Position.KEY_FUEL_LEVEL, parser.nextInt(0));
        position.set(Position.KEY_BATTERY, parser.nextInt());
    }
    if (parser.hasNext()) {
        String[] parameters = parser.next().split(",");
        for (int i = 1; i < parameters.length; i++) {
            position.set(Position.PREFIX_IO + i, parameters[i]);
        }
    }
    if (deviceSession != null) {
        return position;
    } else {
        // save position
        this.position = position;
        return null;
    }
}
Also used : Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) DatagramChannel(org.jboss.netty.channel.socket.DatagramChannel) Parser(org.traccar.helper.Parser)

Aggregations

DatagramChannel (org.jboss.netty.channel.socket.DatagramChannel)1 DateBuilder (org.traccar.helper.DateBuilder)1 Parser (org.traccar.helper.Parser)1 Position (org.traccar.model.Position)1