Search in sources :

Example 6 with DateBuilder

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

the class Gps103ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    // Send response #1
    if (sentence.contains("##")) {
        if (channel != null) {
            channel.write("LOAD", remoteAddress);
            Parser handshakeParser = new Parser(PATTERN_HANDSHAKE, sentence);
            if (handshakeParser.matches()) {
                getDeviceSession(channel, remoteAddress, handshakeParser.next());
            }
        }
        return null;
    }
    // Send response #2
    if (!sentence.isEmpty() && Character.isDigit(sentence.charAt(0))) {
        if (channel != null) {
            channel.write("ON", remoteAddress);
        }
        int start = sentence.indexOf("imei:");
        if (start >= 0) {
            sentence = sentence.substring(start);
        } else {
            return null;
        }
    }
    Position position = new Position(getProtocolName());
    Parser parser = new Parser(PATTERN_NETWORK, sentence);
    if (parser.matches()) {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, null);
        position.setNetwork(new Network(CellTower.fromLacCid(parser.nextHexInt(0), parser.nextHexInt(0))));
        return position;
    }
    parser = new Parser(PATTERN_OBD, sentence);
    if (parser.matches()) {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, parser.nextDateTime());
        position.set(Position.KEY_ODOMETER, parser.nextInt(0));
        // instant fuel consumption
        parser.nextDouble(0);
        position.set(Position.KEY_FUEL_CONSUMPTION, parser.nextDouble(0));
        position.set(Position.KEY_HOURS, parser.nextInt());
        position.set(Position.KEY_OBD_SPEED, parser.nextInt(0));
        position.set(Position.KEY_ENGINE_LOAD, parser.next());
        position.set(Position.KEY_COOLANT_TEMP, parser.nextInt());
        position.set(Position.KEY_THROTTLE, parser.next());
        position.set(Position.KEY_RPM, parser.nextInt(0));
        position.set(Position.KEY_BATTERY, parser.nextDouble(0));
        position.set(Position.KEY_DTCS, parser.next().replace(',', ' ').trim());
        return position;
    }
    parser = new Parser(PATTERN, sentence);
    if (!parser.matches()) {
        return null;
    }
    String imei = parser.next();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    String alarm = parser.next();
    position.set(Position.KEY_ALARM, decodeAlarm(alarm));
    if (alarm.equals("help me")) {
        if (channel != null) {
            channel.write("**,imei:" + imei + ",E;", remoteAddress);
        }
    } else if (alarm.equals("acc on")) {
        position.set(Position.KEY_IGNITION, true);
    } else if (alarm.equals("acc off")) {
        position.set(Position.KEY_IGNITION, false);
    } else if (alarm.startsWith("T:")) {
        position.set(Position.PREFIX_TEMP + 1, alarm.substring(2));
    } else if (alarm.startsWith("oil ")) {
        position.set("oil", alarm.substring(4));
    } else if (!position.getAttributes().containsKey(Position.KEY_ALARM) && !alarm.equals("tracker")) {
        position.set(Position.KEY_EVENT, alarm);
    }
    DateBuilder dateBuilder = new DateBuilder().setDate(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    int localHours = parser.nextInt(0);
    int localMinutes = parser.nextInt(0);
    String rfid = parser.next();
    if (alarm.equals("rfid")) {
        position.set(Position.KEY_DRIVER_UNIQUE_ID, rfid);
    }
    String utcHours = parser.next();
    String utcMinutes = parser.next();
    dateBuilder.setTime(localHours, localMinutes, parser.nextInt(0));
    // Timezone calculation
    if (utcHours != null && utcMinutes != null) {
        int deltaMinutes = (localHours - Integer.parseInt(utcHours)) * 60;
        deltaMinutes += localMinutes - Integer.parseInt(utcMinutes);
        if (deltaMinutes <= -12 * 60) {
            deltaMinutes += 24 * 60;
        } else if (deltaMinutes > 12 * 60) {
            deltaMinutes -= 24 * 60;
        }
        dateBuilder.addMinute(-deltaMinutes);
    }
    position.setTime(dateBuilder.getDate());
    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_HEM));
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_HEM));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.setAltitude(parser.nextDouble(0));
    for (int i = 1; i <= 5; i++) {
        position.set(Position.PREFIX_IO + i, parser.next());
    }
    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 7 with DateBuilder

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

the class T55ProtocolDecoder method decodeGpgga.

private Position decodeGpgga(DeviceSession deviceSession, String sentence) {
    Parser parser = new Parser(PATTERN_GPGGA, sentence);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    DateBuilder dateBuilder = new DateBuilder().setCurrentDate().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    position.setTime(dateBuilder.getDate());
    position.setValid(true);
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    return position;
}
Also used : Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Parser(org.traccar.helper.Parser)

Example 8 with DateBuilder

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

the class T800xProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    buf.skipBytes(2);
    int type = buf.readUnsignedByte();
    // length
    buf.readUnsignedShort();
    int index = buf.readUnsignedShort();
    ChannelBuffer imei = buf.readBytes(8);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ChannelBuffers.hexDump(imei).substring(1));
    if (deviceSession == null) {
        return null;
    }
    if (type == MSG_LOGIN || type == MSG_ALARM || type == MSG_HEARTBEAT) {
        sendResponse(channel, type, imei);
    }
    if (type == MSG_GPS || type == MSG_ALARM) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.set(Position.KEY_INDEX, index);
        // acc on interval
        buf.readUnsignedShort();
        // acc off interval
        buf.readUnsignedShort();
        // angle compensation
        buf.readUnsignedByte();
        // distance compensation
        buf.readUnsignedShort();
        // speed alarm
        buf.readUnsignedShort();
        int locationStatus = buf.readUnsignedByte();
        // gsensor manager status
        buf.readUnsignedByte();
        // other flags
        buf.readUnsignedByte();
        // heartbeat
        buf.readUnsignedByte();
        // relay status
        buf.readUnsignedByte();
        // drag alarm setting
        buf.readUnsignedShort();
        int io = buf.readUnsignedShort();
        position.set(Position.KEY_IGNITION, BitUtil.check(io, 14));
        position.set("ac", BitUtil.check(io, 13));
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());
        position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte()));
        // reserved
        buf.readUnsignedByte();
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
        int battery = BcdUtil.readInteger(buf, 2);
        if (battery == 0) {
            battery = 100;
        }
        position.set(Position.KEY_BATTERY, battery);
        DateBuilder dateBuilder = new DateBuilder().setYear(BcdUtil.readInteger(buf, 2)).setMonth(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2)).setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)).setSecond(BcdUtil.readInteger(buf, 2));
        if (BitUtil.check(locationStatus, 6)) {
            position.setValid(!BitUtil.check(locationStatus, 7));
            position.setTime(dateBuilder.getDate());
            position.setAltitude(readSwappedFloat(buf));
            position.setLongitude(readSwappedFloat(buf));
            position.setLatitude(readSwappedFloat(buf));
            position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4) * 0.1));
            position.setCourse(buf.readUnsignedShort());
        } else {
            getLastLocation(position, dateBuilder.getDate());
            byte[] array = new byte[16];
            buf.readBytes(array);
            ChannelBuffer swapped = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, array);
            position.setNetwork(new Network(CellTower.from(swapped.readUnsignedShort(), swapped.readUnsignedShort(), swapped.readUnsignedShort(), swapped.readUnsignedShort())));
        // two more cell towers
        }
        return position;
    }
    return null;
}
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 9 with DateBuilder

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

the class Tk103ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    if (channel != null) {
        String id = sentence.substring(1, 13);
        String type = sentence.substring(13, 17);
        if (type.equals("BP00")) {
            channel.write("(" + id + "AP01HSO)");
            return null;
        } else if (type.equals("BP05")) {
            channel.write("(" + id + "AP05)");
        }
    }
    if (sentence.contains("ZC20")) {
        return decodeBattery(channel, remoteAddress, sentence);
    } else if (sentence.contains("BZ00")) {
        return decodeNetwork(channel, remoteAddress, sentence);
    } else if (sentence.contains("ZC03")) {
        return decodeCommandResult(channel, remoteAddress, sentence);
    } else if (sentence.contains("DW5")) {
        return decodeLbsWifi(channel, remoteAddress, 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());
    boolean alternative = parser.next() != null;
    decodeType(position, parser.next(), parser.next());
    DateBuilder dateBuilder = new DateBuilder();
    if (alternative) {
        dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    } else {
        dateBuilder.setDate(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(convertSpeed(parser.nextDouble(0), "kmh"));
    dateBuilder.setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    position.setTime(dateBuilder.getDate());
    position.setCourse(parser.nextDouble(0));
    if (parser.hasNext(6)) {
        position.set(Position.KEY_CHARGE, parser.nextInt() == 0);
        position.set(Position.KEY_IGNITION, parser.nextInt() == 1);
        int mask1 = parser.nextHexInt();
        position.set(Position.PREFIX_IN + 2, BitUtil.check(mask1, 0) ? 1 : 0);
        position.set("panic", BitUtil.check(mask1, 1) ? 1 : 0);
        position.set(Position.PREFIX_OUT + 2, BitUtil.check(mask1, 2) ? 1 : 0);
        if (decodeLow || BitUtil.check(mask1, 3)) {
            position.set(Position.KEY_BLOCKED, BitUtil.check(mask1, 3) ? 1 : 0);
        }
        int mask2 = parser.nextHexInt();
        for (int i = 0; i < 3; i++) {
            if (decodeLow || BitUtil.check(mask2, i)) {
                position.set("hs" + (3 - i), BitUtil.check(mask2, i) ? 1 : 0);
            }
        }
        if (decodeLow || BitUtil.check(mask2, 3)) {
            position.set(Position.KEY_DOOR, BitUtil.check(mask2, 3) ? 1 : 0);
        }
        int mask3 = parser.nextHexInt();
        for (int i = 1; i <= 3; i++) {
            if (decodeLow || BitUtil.check(mask3, i)) {
                position.set("ls" + (3 - i + 1), BitUtil.check(mask3, i) ? 1 : 0);
            }
        }
        position.set(Position.KEY_FUEL_LEVEL, parser.nextHexInt());
    }
    if (parser.hasNext()) {
        position.set(Position.KEY_ODOMETER, parser.nextLong(16, 0));
    }
    if (parser.hasNext()) {
        position.set(Position.PREFIX_TEMP + 1, parser.nextDouble(0));
    }
    if (parser.hasNext()) {
        position.setAltitude(parser.nextDouble(0));
    }
    if (parser.hasNext()) {
        position.set(Position.KEY_SATELLITES_VISIBLE, parser.nextInt(0));
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) WifiAccessPoint(org.traccar.model.WifiAccessPoint) Parser(org.traccar.helper.Parser)

Example 10 with DateBuilder

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

the class Tk102ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // header
    buf.skipBytes(1);
    int type = buf.readUnsignedByte();
    ChannelBuffer dataSequence = buf.readBytes(10);
    int length = buf.readUnsignedByte();
    if (type == MSG_LOGIN_REQUEST || type == MSG_LOGIN_REQUEST_2) {
        ChannelBuffer data = buf.readBytes(length);
        String id;
        if (type == MSG_LOGIN_REQUEST) {
            id = data.toString(StandardCharsets.US_ASCII);
        } else {
            id = data.copy(1, 15).toString(StandardCharsets.US_ASCII);
        }
        if (getDeviceSession(channel, remoteAddress, id) != null) {
            ChannelBuffer response = ChannelBuffers.dynamicBuffer();
            response.writeByte(MODE_GPRS);
            response.writeBytes(data);
            sendResponse(channel, MSG_LOGIN_RESPONSE, dataSequence, response);
        }
    } else if (type == MSG_HEARTBEAT_REQUEST) {
        sendResponse(channel, MSG_HEARTBEAT_RESPONSE, dataSequence, buf.readBytes(length));
    } else {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        Parser parser = new Parser(PATTERN, buf.readBytes(length).toString(StandardCharsets.US_ASCII));
        if (!parser.matches()) {
            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));
        position.setValid(parser.next().equals("A"));
        position.setLatitude(parser.nextCoordinate());
        position.setLongitude(parser.nextCoordinate());
        position.setSpeed(parser.nextDouble(0));
        dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
        position.setTime(dateBuilder.getDate());
        return position;
    }
    return null;
}
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)

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