Search in sources :

Example 36 with Network

use of org.traccar.model.Network in project traccar by tananaev.

the class MeitrackProtocolDecoder method decodeRegular.

private Position decodeRegular(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
    Parser parser = new Parser(PATTERN, buf.toString(StandardCharsets.US_ASCII));
    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());
    int event = parser.nextInt(0);
    position.set(Position.KEY_EVENT, event);
    position.set(Position.KEY_ALARM, decodeAlarm(event));
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setTime(parser.nextDateTime());
    position.setValid(parser.next().equals("A"));
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    int rssi = parser.nextInt(0);
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_HDOP, parser.nextDouble());
    position.setAltitude(parser.nextDouble(0));
    position.set(Position.KEY_ODOMETER, parser.nextInt(0));
    position.set("runtime", parser.next());
    position.setNetwork(new Network(CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextHexInt(0), parser.nextHexInt(0), rssi)));
    position.set(Position.KEY_STATUS, parser.next());
    for (int i = 1; i <= 3; i++) {
        if (parser.hasNext()) {
            position.set(Position.PREFIX_ADC + i, parser.nextHexInt(0));
        }
    }
    String deviceModel = Context.getIdentityManager().getById(deviceSession.getDeviceId()).getModel();
    if (deviceModel == null) {
        deviceModel = "";
    }
    switch(deviceModel.toUpperCase()) {
        case "MVT340":
        case "MVT380":
            position.set(Position.KEY_BATTERY, parser.nextHexInt(0) * 3.0 * 2.0 / 1024.0);
            position.set(Position.KEY_POWER, parser.nextHexInt(0) * 3.0 * 16.0 / 1024.0);
            break;
        case "MT90":
            position.set(Position.KEY_BATTERY, parser.nextHexInt(0) * 3.3 * 2.0 / 4096.0);
            position.set(Position.KEY_POWER, parser.nextHexInt(0));
            break;
        case "T1":
        case "T3":
        case "MVT100":
        case "MVT600":
        case "MVT800":
        case "TC68":
        case "TC68S":
            position.set(Position.KEY_BATTERY, parser.nextHexInt(0) * 3.3 * 2.0 / 4096.0);
            position.set(Position.KEY_POWER, parser.nextHexInt(0) * 3.3 * 16.0 / 4096.0);
            break;
        case "T311":
        case "T322X":
        case "T333":
        case "T355":
            position.set(Position.KEY_BATTERY, parser.nextHexInt(0) / 100.0);
            position.set(Position.KEY_POWER, parser.nextHexInt(0) / 100.0);
            break;
        default:
            position.set(Position.KEY_BATTERY, parser.nextHexInt(0));
            position.set(Position.KEY_POWER, parser.nextHexInt(0));
            break;
    }
    String eventData = parser.next();
    if (eventData != null && !eventData.isEmpty()) {
        switch(event) {
            case 37:
                position.set(Position.KEY_DRIVER_UNIQUE_ID, eventData);
                break;
            default:
                position.set("eventData", eventData);
                break;
        }
    }
    int protocol = parser.nextInt(0);
    if (parser.hasNext()) {
        String fuel = parser.next();
        position.set(Position.KEY_FUEL_LEVEL, Integer.parseInt(fuel.substring(0, 2), 16) + Integer.parseInt(fuel.substring(2), 16) * 0.01);
    }
    if (parser.hasNext()) {
        for (String temp : parser.next().split("\\|")) {
            int index = Integer.parseInt(temp.substring(0, 2), 16);
            if (protocol >= 3) {
                double value = (short) Integer.parseInt(temp.substring(2), 16);
                position.set(Position.PREFIX_TEMP + index, value * 0.01);
            } else {
                double value = Byte.parseByte(temp.substring(2, 4), 16);
                value += (value < 0 ? -0.01 : 0.01) * Integer.parseInt(temp.substring(4), 16);
                position.set(Position.PREFIX_TEMP + index, value);
            }
        }
    }
    if (parser.hasNext(2)) {
        // count
        parser.nextInt();
        decodeDataFields(position, parser.next().split(","));
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) Parser(org.traccar.helper.Parser)

Example 37 with Network

use of org.traccar.model.Network in project traccar by tananaev.

the class MeitrackProtocolDecoder method decodeBinaryC.

private List<Position> decodeBinaryC(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
    List<Position> positions = new LinkedList<>();
    String flag = buf.toString(2, 1, StandardCharsets.US_ASCII);
    int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',');
    String imei = buf.toString(index + 1, 15, StandardCharsets.US_ASCII);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    buf.skipBytes(index + 1 + 15 + 1 + 3 + 1 + 2 + 2 + 4);
    while (buf.readableBytes() >= 0x34) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.set(Position.KEY_EVENT, buf.readUnsignedByte());
        position.setLatitude(buf.readInt() * 0.000001);
        position.setLongitude(buf.readInt() * 0.000001);
        // 946684800 = 2000-01-01
        position.setTime(new Date((946684800 + buf.readUnsignedInt()) * 1000));
        position.setValid(buf.readUnsignedByte() == 1);
        position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
        int rssi = buf.readUnsignedByte();
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
        position.setCourse(buf.readUnsignedShort());
        position.set(Position.KEY_HDOP, buf.readUnsignedShort() * 0.1);
        position.setAltitude(buf.readUnsignedShort());
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
        position.set("runtime", buf.readUnsignedInt());
        position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), rssi)));
        position.set(Position.KEY_STATUS, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
        position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.01);
        position.set(Position.KEY_POWER, buf.readUnsignedShort());
        // geo-fence
        buf.readUnsignedInt();
        positions.add(position);
    }
    if (channel != null) {
        StringBuilder command = new StringBuilder("@@");
        command.append(flag).append(27 + positions.size() / 10).append(",");
        command.append(imei).append(",CCC,").append(positions.size()).append("*");
        int checksum = 0;
        for (int i = 0; i < command.length(); i += 1) {
            checksum += command.charAt(i);
        }
        command.append(String.format("%02x", checksum & 0xff).toUpperCase());
        command.append("\r\n");
        // delete processed data
        channel.write(command.toString());
    }
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) LinkedList(java.util.LinkedList) Date(java.util.Date)

Example 38 with Network

use of org.traccar.model.Network in project traccar by tananaev.

the class OsmAndProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    HttpRequest request = (HttpRequest) msg;
    QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
    Map<String, List<String>> params = decoder.getParameters();
    if (params.isEmpty()) {
        decoder = new QueryStringDecoder(request.getContent().toString(StandardCharsets.US_ASCII), false);
        params = decoder.getParameters();
    }
    Position position = new Position(getProtocolName());
    position.setValid(true);
    Network network = new Network();
    for (Map.Entry<String, List<String>> entry : params.entrySet()) {
        for (String value : entry.getValue()) {
            switch(entry.getKey()) {
                case "id":
                case "deviceid":
                    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
                    if (deviceSession == null) {
                        sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
                        return null;
                    }
                    position.setDeviceId(deviceSession.getDeviceId());
                    break;
                case "valid":
                    position.setValid(Boolean.parseBoolean(value));
                    break;
                case "timestamp":
                    try {
                        long timestamp = Long.parseLong(value);
                        if (timestamp < Integer.MAX_VALUE) {
                            timestamp *= 1000;
                        }
                        position.setTime(new Date(timestamp));
                    } catch (NumberFormatException error) {
                        if (value.contains("T")) {
                            position.setTime(new Date(ISODateTimeFormat.dateTimeParser().parseMillis(value)));
                        } else {
                            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            position.setTime(dateFormat.parse(value));
                        }
                    }
                    break;
                case "lat":
                    position.setLatitude(Double.parseDouble(value));
                    break;
                case "lon":
                    position.setLongitude(Double.parseDouble(value));
                    break;
                case "location":
                    String[] location = value.split(",");
                    position.setLatitude(Double.parseDouble(location[0]));
                    position.setLongitude(Double.parseDouble(location[1]));
                    break;
                case "cell":
                    String[] cell = value.split(",");
                    if (cell.length > 4) {
                        network.addCellTower(CellTower.from(Integer.parseInt(cell[0]), Integer.parseInt(cell[1]), Integer.parseInt(cell[2]), Integer.parseInt(cell[3]), Integer.parseInt(cell[4])));
                    } else {
                        network.addCellTower(CellTower.from(Integer.parseInt(cell[0]), Integer.parseInt(cell[1]), Integer.parseInt(cell[2]), Integer.parseInt(cell[3])));
                    }
                    break;
                case "wifi":
                    String[] wifi = value.split(",");
                    network.addWifiAccessPoint(WifiAccessPoint.from(wifi[0].replace('-', ':'), Integer.parseInt(wifi[1])));
                    break;
                case "speed":
                    position.setSpeed(convertSpeed(Double.parseDouble(value), "kn"));
                    break;
                case "bearing":
                case "heading":
                    position.setCourse(Double.parseDouble(value));
                    break;
                case "altitude":
                    position.setAltitude(Double.parseDouble(value));
                    break;
                case "accuracy":
                    position.setAccuracy(Double.parseDouble(value));
                    break;
                case "hdop":
                    position.set(Position.KEY_HDOP, Double.parseDouble(value));
                    break;
                case "batt":
                    position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(value));
                    break;
                case "driverUniqueId":
                    position.set(Position.KEY_DRIVER_UNIQUE_ID, value);
                    break;
                default:
                    try {
                        position.set(entry.getKey(), Double.parseDouble(value));
                    } catch (NumberFormatException e) {
                        switch(value) {
                            case "true":
                                position.set(entry.getKey(), true);
                                break;
                            case "false":
                                position.set(entry.getKey(), false);
                                break;
                            default:
                                position.set(entry.getKey(), value);
                                break;
                        }
                    }
                    break;
            }
        }
    }
    if (position.getFixTime() == null) {
        position.setTime(new Date());
    }
    if (network.getCellTowers() != null || network.getWifiAccessPoints() != null) {
        position.setNetwork(network);
    }
    if (position.getLatitude() == 0 && position.getLongitude() == 0) {
        getLastLocation(position, position.getDeviceTime());
    }
    if (position.getDeviceId() != 0) {
        sendResponse(channel, HttpResponseStatus.OK);
        return position;
    } else {
        sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
        return null;
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Date(java.util.Date) QueryStringDecoder(org.jboss.netty.handler.codec.http.QueryStringDecoder) Network(org.traccar.model.Network) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) List(java.util.List) Map(java.util.Map) SimpleDateFormat(java.text.SimpleDateFormat)

Example 39 with Network

use of org.traccar.model.Network in project traccar by tananaev.

the class RoboTrackProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    int type = buf.readUnsignedByte();
    if (type == MSG_ID) {
        // name
        buf.skipBytes(16);
        String imei = buf.readBytes(15).toString(StandardCharsets.US_ASCII);
        if (getDeviceSession(channel, remoteAddress, imei) != null && channel != null) {
            ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
            response.writeByte(MSG_ACK);
            // success
            response.writeByte(0x01);
            // checksum
            response.writeByte(0x66);
            channel.write(response);
        }
    } else if (type == MSG_GPS || type == MSG_GSM) {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.setDeviceTime(new Date(buf.readUnsignedInt() * 1000));
        if (type == MSG_GPS) {
            position.setValid(true);
            position.setFixTime(position.getDeviceTime());
            position.setLatitude(buf.readInt() * 0.000001);
            position.setLongitude(buf.readInt() * 0.000001);
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readByte()));
        } else {
            getLastLocation(position, position.getDeviceTime());
            position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort())));
            // reserved
            buf.readUnsignedByte();
        }
        int value = buf.readUnsignedByte();
        position.set(Position.KEY_SATELLITES, BitUtil.to(value, 4));
        position.set(Position.KEY_RSSI, BitUtil.between(value, 4, 7));
        position.set(Position.KEY_MOTION, BitUtil.check(value, 7));
        value = buf.readUnsignedByte();
        position.set(Position.KEY_CHARGE, BitUtil.check(value, 0));
        for (int i = 1; i <= 4; i++) {
            position.set(Position.PREFIX_IN + i, BitUtil.check(value, i));
        }
        position.set(Position.KEY_BATTERY_LEVEL, BitUtil.from(value, 5) * 100 / 7);
        position.set(Position.KEY_DEVICE_TEMP, buf.readByte());
        for (int i = 1; i <= 3; i++) {
            position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort());
        }
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 40 with Network

use of org.traccar.model.Network in project traccar by tananaev.

the class CityeasyProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // header
    buf.skipBytes(2);
    // length
    buf.readUnsignedShort();
    String imei = ChannelBuffers.hexDump(buf.readBytes(7));
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei, imei + Checksum.luhn(Long.parseLong(imei)));
    if (deviceSession == null) {
        return null;
    }
    int type = buf.readUnsignedShort();
    if (type == MSG_LOCATION_REPORT || type == MSG_LOCATION_REQUEST) {
        String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 8, StandardCharsets.US_ASCII);
        Parser parser = new Parser(PATTERN, sentence);
        if (!parser.matches()) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        if (parser.hasNext(15)) {
            position.setTime(parser.nextDateTime());
            position.setValid(parser.next().equals("A"));
            position.set(Position.KEY_SATELLITES, parser.nextInt());
            position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
            position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
            position.setSpeed(parser.nextDouble(0));
            position.set(Position.KEY_HDOP, parser.nextDouble(0));
            position.setAltitude(parser.nextDouble(0));
        } else {
            getLastLocation(position, null);
        }
        position.setNetwork(new Network(CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0), parser.nextInt(0))));
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Parser(org.traccar.helper.Parser)

Aggregations

Network (org.traccar.model.Network)58 Position (org.traccar.model.Position)49 DeviceSession (org.traccar.DeviceSession)37 Parser (org.traccar.helper.Parser)25 DateBuilder (org.traccar.helper.DateBuilder)16 Date (java.util.Date)12 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)12 WifiAccessPoint (org.traccar.model.WifiAccessPoint)10 CellTower (org.traccar.model.CellTower)7 LinkedList (java.util.LinkedList)6 SimpleDateFormat (java.text.SimpleDateFormat)3 DateFormat (java.text.DateFormat)2 List (java.util.List)2 BitBuffer (org.traccar.helper.BitBuffer)2 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 Map (java.util.Map)1 TimeZone (java.util.TimeZone)1 Matcher (java.util.regex.Matcher)1 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)1