Search in sources :

Example 56 with Position

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

the class OigoProtocolDecoder method decodeArMessage.

private Position decodeArMessage(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
    // header
    buf.skipBytes(1);
    // length
    buf.readUnsignedShort();
    int type = buf.readUnsignedByte();
    int tag = buf.readUnsignedByte();
    DeviceSession deviceSession;
    switch(BitUtil.to(tag, 3)) {
        case 0:
            String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
            deviceSession = getDeviceSession(channel, remoteAddress, imei);
            break;
        case 1:
            buf.skipBytes(1);
            String meid = buf.readBytes(14).toString(StandardCharsets.US_ASCII);
            deviceSession = getDeviceSession(channel, remoteAddress, meid);
            break;
        default:
            deviceSession = getDeviceSession(channel, remoteAddress);
            break;
    }
    if (deviceSession == null || type != MSG_AR_LOCATION) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.set(Position.KEY_EVENT, buf.readUnsignedByte());
    int mask = buf.readInt();
    if (BitUtil.check(mask, 0)) {
        position.set(Position.KEY_INDEX, buf.readUnsignedShort());
    }
    if (BitUtil.check(mask, 1)) {
        int date = buf.readUnsignedByte();
        DateBuilder dateBuilder = new DateBuilder().setDate(BitUtil.between(date, 4, 8) + 2010, BitUtil.to(date, 4), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        position.setTime(dateBuilder.getDate());
    }
    if (BitUtil.check(mask, 2)) {
        // device time
        buf.skipBytes(5);
    }
    if (BitUtil.check(mask, 3)) {
        position.setLatitude(buf.readUnsignedInt() * 0.000001 - 90);
        position.setLongitude(buf.readUnsignedInt() * 0.000001 - 180.0);
    }
    if (BitUtil.check(mask, 4)) {
        int status = buf.readUnsignedByte();
        position.setValid(BitUtil.between(status, 4, 8) != 0);
        position.set(Position.KEY_SATELLITES, BitUtil.to(status, 4));
        position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
    }
    if (BitUtil.check(mask, 5)) {
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
    }
    if (BitUtil.check(mask, 6)) {
        position.setCourse(buf.readUnsignedShort());
    }
    if (BitUtil.check(mask, 7)) {
        position.setAltitude(buf.readShort());
    }
    if (BitUtil.check(mask, 8)) {
        position.set(Position.KEY_RSSI, buf.readUnsignedByte());
    }
    if (BitUtil.check(mask, 9)) {
        position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
    }
    if (BitUtil.check(mask, 10)) {
        position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
    }
    if (BitUtil.check(mask, 11)) {
        // gpio
        buf.skipBytes(2);
    }
    if (BitUtil.check(mask, 12)) {
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
    }
    if (BitUtil.check(mask, 13)) {
        // software version
        buf.skipBytes(6);
    }
    if (BitUtil.check(mask, 14)) {
        // hardware version
        buf.skipBytes(5);
    }
    if (BitUtil.check(mask, 15)) {
        // device config
        buf.readUnsignedShort();
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder)

Example 57 with Position

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

the class PathAwayProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    HttpRequest request = (HttpRequest) msg;
    QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, decoder.getParameters().get("UserName").get(0));
    if (deviceSession == null) {
        return null;
    }
    Parser parser = new Parser(PATTERN, decoder.getParameters().get("LOC").get(0));
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
    position.setValid(true);
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setAltitude(parser.nextDouble(0));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    if (channel != null) {
        HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        channel.write(response).addListener(ChannelFutureListener.CLOSE);
    }
    return position;
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) QueryStringDecoder(org.jboss.netty.handler.codec.http.QueryStringDecoder) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Parser(org.traccar.helper.Parser)

Example 58 with Position

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

the class L100ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // start marker
    buf.readUnsignedByte();
    // type
    buf.readUnsignedByte();
    String sentence = buf.readBytes(buf.readableBytes() - 2).toString(StandardCharsets.US_ASCII);
    Parser parser = new Parser(PATTERN, sentence);
    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), 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_STATUS, parser.next());
    position.set(Position.PREFIX_ADC + 1, parser.next());
    position.set(Position.KEY_ODOMETER, parser.nextDouble(0));
    position.set(Position.PREFIX_TEMP + 1, parser.nextDouble(0));
    position.set(Position.KEY_BATTERY, parser.nextDouble(0));
    int rssi = parser.nextInt(0);
    position.setNetwork(new Network(CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextHexInt(0), parser.nextHexInt(0), rssi)));
    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) Parser(org.traccar.helper.Parser)

Example 59 with Position

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

the class LaipacProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    if (sentence.startsWith("$ECHK") && channel != null) {
        // heartbeat
        channel.write(sentence + "\r\n");
        return null;
    }
    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));
    String status = parser.next();
    position.setValid(status.toUpperCase().equals("A"));
    position.set(Position.KEY_STATUS, status);
    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());
    String event = parser.next();
    position.set(Position.KEY_ALARM, decodeAlarm(event));
    position.set(Position.KEY_EVENT, event);
    position.set(Position.KEY_BATTERY, Double.parseDouble(parser.next().replaceAll("\\.", "")) * 0.001);
    position.set(Position.KEY_ODOMETER, parser.nextDouble());
    position.set(Position.KEY_GPS, parser.nextInt());
    position.set(Position.PREFIX_ADC + 1, parser.nextDouble() * 0.001);
    position.set(Position.PREFIX_ADC + 2, parser.nextDouble() * 0.001);
    Integer lac = parser.nextHexInt();
    Integer cid = parser.nextHexInt();
    Integer mcc = parser.nextInt();
    Integer mnc = parser.nextInt();
    if (lac != null && cid != null && mcc != null && mnc != null) {
        position.setNetwork(new Network(CellTower.from(mcc, mnc, lac, cid)));
    }
    String checksum = parser.next();
    if (channel != null) {
        if (event.equals("3")) {
            channel.write("$AVCFG,00000000,d*31\r\n");
        } else if (event.equals("X") || event.equals("4")) {
            channel.write("$AVCFG,00000000,x*2D\r\n");
        } else if (event.equals("Z")) {
            channel.write("$AVCFG,00000000,z*2F\r\n");
        } else if (Character.isLowerCase(status.charAt(0))) {
            String response = "$EAVACK," + event + "," + checksum;
            response += Checksum.nmea(response) + "\r\n";
            channel.write(response);
        }
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Network(org.traccar.model.Network) Parser(org.traccar.helper.Parser)

Example 60 with Position

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

the class M2cProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    // remove start symbol
    sentence = sentence.substring(1);
    List<Position> positions = new LinkedList<>();
    for (String line : sentence.split("\r\n")) {
        if (!line.isEmpty()) {
            Position position = decodePosition(channel, remoteAddress, line);
            if (position != null) {
                positions.add(position);
            }
        }
    }
    return positions;
}
Also used : Position(org.traccar.model.Position) LinkedList(java.util.LinkedList)

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