Search in sources :

Example 16 with DateBuilder

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

the class GlobalSatProtocolDecoder method decodeOriginal.

private Position decodeOriginal(Channel channel, SocketAddress remoteAddress, String sentence) {
    if (channel != null) {
        channel.write("ACK\r");
    }
    String format;
    if (sentence.startsWith("GSr")) {
        format = format0;
    } else if (sentence.startsWith("GSh")) {
        format = format1;
    } else {
        return null;
    }
    // Check that message contains required parameters
    if (!format.contains("B") || !format.contains("S") || !(format.contains("1") || format.contains("2") || format.contains("3")) || !(format.contains("6") || format.contains("7") || format.contains("8"))) {
        return null;
    }
    if (format.contains("*")) {
        format = format.substring(0, format.indexOf('*'));
        sentence = sentence.substring(0, sentence.indexOf('*'));
    }
    String[] values = sentence.split(",");
    Position position = new Position(getProtocolName());
    for (int formatIndex = 0, valueIndex = 1; formatIndex < format.length() && valueIndex < values.length; formatIndex++) {
        String value = values[valueIndex];
        switch(format.charAt(formatIndex)) {
            case 'S':
                DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
                if (deviceSession == null) {
                    return null;
                }
                position.setDeviceId(deviceSession.getDeviceId());
                break;
            case 'A':
                if (value.isEmpty()) {
                    position.setValid(false);
                } else {
                    position.setValid(Integer.parseInt(value) != 1);
                }
                break;
            case 'B':
                DateBuilder dateBuilder = new DateBuilder().setDay(Integer.parseInt(value.substring(0, 2))).setMonth(Integer.parseInt(value.substring(2, 4))).setYear(Integer.parseInt(value.substring(4)));
                value = values[++valueIndex];
                dateBuilder.setHour(Integer.parseInt(value.substring(0, 2))).setMinute(Integer.parseInt(value.substring(2, 4))).setSecond(Integer.parseInt(value.substring(4)));
                position.setTime(dateBuilder.getDate());
                break;
            case 'C':
                valueIndex += 1;
                break;
            case '1':
                double longitude = Double.parseDouble(value.substring(1));
                if (value.charAt(0) == 'W') {
                    longitude = -longitude;
                }
                position.setLongitude(longitude);
                break;
            case '2':
                longitude = Double.parseDouble(value.substring(4)) / 60;
                longitude += Integer.parseInt(value.substring(1, 4));
                if (value.charAt(0) == 'W') {
                    longitude = -longitude;
                }
                position.setLongitude(longitude);
                break;
            case '3':
                position.setLongitude(Double.parseDouble(value) * 0.000001);
                break;
            case '6':
                double latitude = Double.parseDouble(value.substring(1));
                if (value.charAt(0) == 'S') {
                    latitude = -latitude;
                }
                position.setLatitude(latitude);
                break;
            case '7':
                latitude = Double.parseDouble(value.substring(3)) / 60;
                latitude += Integer.parseInt(value.substring(1, 3));
                if (value.charAt(0) == 'S') {
                    latitude = -latitude;
                }
                position.setLatitude(latitude);
                break;
            case '8':
                position.setLatitude(Double.parseDouble(value) * 0.000001);
                break;
            case 'G':
                position.setAltitude(Double.parseDouble(value));
                break;
            case 'H':
                position.setSpeed(Double.parseDouble(value));
                break;
            case 'I':
                position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(value)));
                break;
            case 'J':
                position.setSpeed(UnitsConverter.knotsFromMph(Double.parseDouble(value)));
                break;
            case 'K':
                position.setCourse(Double.parseDouble(value));
                break;
            case 'N':
                if (value.endsWith("mV")) {
                    position.set(Position.KEY_BATTERY, Integer.parseInt(value.substring(0, value.length() - 2)) / 1000.0);
                } else {
                    position.set(Position.KEY_BATTERY_LEVEL, Integer.parseInt(value));
                }
                break;
            default:
                // Unsupported
                break;
        }
        valueIndex += 1;
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder)

Example 17 with DateBuilder

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

the class Gt06ProtocolDecoder method decodeWifi.

private Object decodeWifi(ChannelBuffer buf, DeviceSession deviceSession) throws Exception {
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    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));
    getLastLocation(position, dateBuilder.getDate());
    Network network = new Network();
    int wifiCount = buf.getByte(2);
    for (int i = 0; i < wifiCount; i++) {
        String mac = String.format("%02x:%02x:%02x:%02x:%02x:%02x", buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        network.addWifiAccessPoint(WifiAccessPoint.from(mac, buf.readUnsignedByte()));
    }
    int cellCount = buf.readUnsignedByte();
    int mcc = buf.readUnsignedShort();
    int mnc = buf.readUnsignedByte();
    for (int i = 0; i < cellCount; i++) {
        network.addCellTower(CellTower.from(mcc, mnc, buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedByte()));
    }
    position.setNetwork(network);
    return position;
}
Also used : Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Network(org.traccar.model.Network) WifiAccessPoint(org.traccar.model.WifiAccessPoint)

Example 18 with DateBuilder

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

the class Gt06ProtocolDecoder method decodeBasicOther.

private Object decodeBasicOther(Channel channel, ChannelBuffer buf, DeviceSession deviceSession, int type, int dataLength) throws Exception {
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    if (type == MSG_LBS_MULTIPLE || type == MSG_LBS_EXTEND || type == MSG_LBS_WIFI || type == MSG_LBS_2 || type == MSG_WIFI_3) {
        boolean longFormat = type == MSG_LBS_2 || type == MSG_WIFI_3;
        DateBuilder dateBuilder = new DateBuilder(deviceSession.getTimeZone()).setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        getLastLocation(position, dateBuilder.getDate());
        int mcc = buf.readUnsignedShort();
        int mnc = BitUtil.check(mcc, 15) ? buf.readUnsignedShort() : buf.readUnsignedByte();
        Network network = new Network();
        for (int i = 0; i < 7; i++) {
            int lac = longFormat ? buf.readInt() : buf.readUnsignedShort();
            int cid = longFormat ? (int) buf.readLong() : buf.readUnsignedMedium();
            int rssi = -buf.readUnsignedByte();
            if (lac > 0) {
                network.addCellTower(CellTower.from(BitUtil.to(mcc, 15), mnc, lac, cid, rssi));
            }
        }
        // time leads
        buf.readUnsignedByte();
        if (type != MSG_LBS_MULTIPLE && type != MSG_LBS_2) {
            int wifiCount = buf.readUnsignedByte();
            for (int i = 0; i < wifiCount; i++) {
                String mac = ChannelBuffers.hexDump(buf.readBytes(6)).replaceAll("(..)", "$1:");
                network.addWifiAccessPoint(WifiAccessPoint.from(mac.substring(0, mac.length() - 1), buf.readUnsignedByte()));
            }
        }
        position.setNetwork(network);
    } else if (type == MSG_STRING) {
        getLastLocation(position, null);
        int commandLength = buf.readUnsignedByte();
        if (commandLength > 0) {
            // server flag (reserved)
            buf.readUnsignedByte();
            position.set(Position.KEY_RESULT, buf.readBytes(commandLength - 1).toString(StandardCharsets.US_ASCII));
        }
    } else if (isSupported(type)) {
        if (hasGps(type)) {
            decodeGps(position, buf, false, deviceSession.getTimeZone());
        } else {
            getLastLocation(position, null);
        }
        if (hasLbs(type)) {
            decodeLbs(position, buf, hasStatus(type));
        }
        if (hasStatus(type)) {
            decodeStatus(position, buf);
        }
        if (type == MSG_GPS_LBS_1 && buf.readableBytes() >= 4 + 6) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
        }
        if (type == MSG_GPS_LBS_2 && buf.readableBytes() >= 3 + 6) {
            position.set(Position.KEY_IGNITION, buf.readUnsignedByte() > 0);
            // reason
            position.set(Position.KEY_EVENT, buf.readUnsignedByte());
            position.set(Position.KEY_ARCHIVE, buf.readUnsignedByte() > 0);
        }
    } else {
        buf.skipBytes(dataLength);
        if (type != MSG_COMMAND_0 && type != MSG_COMMAND_1 && type != MSG_COMMAND_2) {
            sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null);
        }
        return null;
    }
    if (hasLanguage(type)) {
        buf.readUnsignedShort();
    }
    if (type == MSG_GPS_LBS_STATUS_3 || type == MSG_FENCE_MULTI) {
        position.set(Position.KEY_GEOFENCE, buf.readUnsignedByte());
    }
    sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null);
    return position;
}
Also used : Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Network(org.traccar.model.Network) WifiAccessPoint(org.traccar.model.WifiAccessPoint)

Example 19 with DateBuilder

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

the class H02ProtocolDecoder method decodeLbs.

private Position decodeLbs(String sentence, Channel channel, SocketAddress remoteAddress) {
    Parser parser = new Parser(PATTERN_NBR, 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));
    Network network = new Network();
    int mcc = parser.nextInt(0);
    int mnc = parser.nextInt(0);
    String[] cells = parser.next().split(",");
    for (int i = 0; i < cells.length / 3; i++) {
        network.addCellTower(CellTower.from(mcc, mnc, Integer.parseInt(cells[i * 3]), Integer.parseInt(cells[i * 3 + 1]), Integer.parseInt(cells[i * 3 + 2])));
    }
    position.setNetwork(network);
    dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    getLastLocation(position, dateBuilder.getDate());
    processStatus(position, parser.nextLong(16, 0));
    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 20 with DateBuilder

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

the class H02ProtocolDecoder method decodeBinary.

private Position decodeBinary(ChannelBuffer buf, Channel channel, SocketAddress remoteAddress) {
    Position position = new Position(getProtocolName());
    // marker
    buf.readByte();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ChannelBuffers.hexDump(buf.readBytes(5)));
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    DateBuilder dateBuilder = new DateBuilder().setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)).setSecond(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2)).setMonth(BcdUtil.readInteger(buf, 2)).setYear(BcdUtil.readInteger(buf, 2));
    position.setTime(dateBuilder.getDate());
    double latitude = readCoordinate(buf, false);
    position.set(Position.KEY_BATTERY_LEVEL, decodeBattery(buf.readUnsignedByte()));
    double longitude = readCoordinate(buf, true);
    int flags = buf.readUnsignedByte() & 0x0f;
    position.setValid((flags & 0x02) != 0);
    if ((flags & 0x04) == 0) {
        latitude = -latitude;
    }
    if ((flags & 0x08) == 0) {
        longitude = -longitude;
    }
    position.setLatitude(latitude);
    position.setLongitude(longitude);
    position.setSpeed(BcdUtil.readInteger(buf, 3));
    position.setCourse((buf.readUnsignedByte() & 0x0f) * 100.0 + BcdUtil.readInteger(buf, 2));
    processStatus(position, buf.readUnsignedInt());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder)

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