Search in sources :

Example 46 with DeviceSession

use of org.traccar.DeviceSession in project traccar by tananaev.

the class UlbotechProtocolDecoder method decodeBinary.

private Object decodeBinary(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
    // header
    buf.readUnsignedByte();
    // version
    buf.readUnsignedByte();
    // type
    buf.readUnsignedByte();
    String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    if (deviceSession.getTimeZone() == null) {
        deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId()));
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    long seconds = buf.readUnsignedInt() & 0x7fffffffL;
    // 2000-01-01 00:00
    seconds += 946684800L;
    seconds -= deviceSession.getTimeZone().getRawOffset() / 1000;
    Date time = new Date(seconds * 1000);
    boolean hasLocation = false;
    while (buf.readableBytes() > 3) {
        int type = buf.readUnsignedByte();
        int length = type == DATA_CANBUS ? buf.readUnsignedShort() : buf.readUnsignedByte();
        switch(type) {
            case DATA_GPS:
                hasLocation = true;
                position.setValid(true);
                position.setLatitude(buf.readInt() / 1000000.0);
                position.setLongitude(buf.readInt() / 1000000.0);
                position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
                position.setCourse(buf.readUnsignedShort());
                position.set(Position.KEY_HDOP, buf.readUnsignedShort());
                break;
            case DATA_LBS:
                if (length == 11) {
                    position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedInt(), -buf.readUnsignedByte())));
                } else {
                    position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), -buf.readUnsignedByte())));
                }
                if (length > 9 && length != 11) {
                    buf.skipBytes(length - 9);
                }
                break;
            case DATA_STATUS:
                int status = buf.readUnsignedShort();
                position.set(Position.KEY_IGNITION, BitUtil.check(status, 9));
                position.set(Position.KEY_STATUS, status);
                position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedShort()));
                break;
            case DATA_ODOMETER:
                position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
                break;
            case DATA_ADC:
                decodeAdc(position, buf, length);
                break;
            case DATA_GEOFENCE:
                position.set("geofenceIn", buf.readUnsignedInt());
                position.set("geofenceAlarm", buf.readUnsignedInt());
                break;
            case DATA_OBD2:
                decodeObd(position, buf, length);
                break;
            case DATA_FUEL:
                position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedInt() / 10000.0);
                break;
            case DATA_OBD2_ALARM:
                decodeObd(position, buf, length);
                break;
            case DATA_HARSH_DRIVER:
                decodeDriverBehavior(position, buf);
                break;
            case DATA_CANBUS:
                position.set("can", ChannelBuffers.hexDump(buf.readBytes(length)));
                break;
            case DATA_J1708:
                decodeJ1708(position, buf, length);
                break;
            case DATA_VIN:
                position.set(Position.KEY_VIN, buf.readBytes(length).toString(StandardCharsets.US_ASCII));
                break;
            case DATA_RFID:
                position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readBytes(length - 1).toString(StandardCharsets.US_ASCII));
                position.set("authorized", buf.readUnsignedByte() != 0);
                break;
            case DATA_EVENT:
                position.set(Position.KEY_EVENT, buf.readUnsignedByte());
                if (length > 1) {
                    position.set("eventMask", buf.readUnsignedInt());
                }
                break;
            default:
                buf.skipBytes(length);
                break;
        }
    }
    if (!hasLocation) {
        getLastLocation(position, time);
    } else {
        position.setTime(time);
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) Date(java.util.Date)

Example 47 with DeviceSession

use of org.traccar.DeviceSession in project traccar by tananaev.

the class UproProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    if (buf.getByte(buf.readerIndex()) != '*') {
        throw new ParseException(null, 0);
    }
    int headerIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&');
    if (headerIndex < 0) {
        headerIndex = buf.writerIndex();
    }
    String header = buf.readBytes(headerIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII);
    Parser parser = new Parser(PATTERN_HEADER, header);
    if (!parser.matches()) {
        return null;
    }
    boolean reply = parser.next().equals("1");
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    String type = parser.next();
    String subtype = parser.next();
    if (reply && channel != null) {
        channel.write("*MG20Y" + type + subtype + "#");
    }
    while (buf.readable()) {
        // skip delimiter
        buf.readByte();
        byte dataType = buf.readByte();
        int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&');
        if (delimiterIndex < 0) {
            delimiterIndex = buf.writerIndex();
        }
        ChannelBuffer data = buf.readBytes(delimiterIndex - buf.readerIndex());
        switch(dataType) {
            case 'A':
                decodeLocation(position, data.toString(StandardCharsets.US_ASCII));
                break;
            case 'B':
                position.set(Position.KEY_STATUS, data.toString(StandardCharsets.US_ASCII));
                break;
            case 'C':
                long odometer = 0;
                while (data.readable()) {
                    odometer <<= 4;
                    odometer += data.readByte() - (byte) '0';
                }
                position.set(Position.KEY_ODOMETER, odometer * 2 * 1852 / 3600);
                break;
            case 'P':
                position.setNetwork(new Network(CellTower.from(Integer.parseInt(data.readBytes(4).toString(StandardCharsets.US_ASCII)), Integer.parseInt(data.readBytes(4).toString(StandardCharsets.US_ASCII)), Integer.parseInt(data.readBytes(4).toString(StandardCharsets.US_ASCII), 16), Integer.parseInt(data.readBytes(4).toString(StandardCharsets.US_ASCII), 16))));
                break;
            case 'Q':
                position.set("obd-pid", ChannelBuffers.hexDump(data));
                break;
            case 'R':
                position.set("odb-travel", ChannelBuffers.hexDump(data));
                break;
            case 'S':
                position.set("obd-traffic", ChannelBuffers.hexDump(data));
                break;
            default:
                break;
        }
    }
    if (position.getLatitude() != 0 && position.getLongitude() != 0) {
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) ParseException(java.text.ParseException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Parser(org.traccar.helper.Parser)

Example 48 with DeviceSession

use of org.traccar.DeviceSession in project traccar by tananaev.

the class V680ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    sentence = sentence.trim();
    if (sentence.length() == 16) {
        getDeviceSession(channel, remoteAddress, sentence.substring(1, sentence.length()));
    } else {
        Parser parser = new Parser(PATTERN, sentence);
        if (!parser.matches()) {
            return null;
        }
        Position position = new Position(getProtocolName());
        DeviceSession deviceSession;
        if (parser.hasNext()) {
            deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
        } else {
            deviceSession = getDeviceSession(channel, remoteAddress);
        }
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());
        position.set("user", parser.next());
        position.setValid(parser.nextInt(0) > 0);
        position.set("password", parser.next());
        position.set(Position.KEY_EVENT, parser.next());
        position.set("packet", parser.next());
        position.set("lbsData", parser.next());
        double lon = parser.nextDouble(0);
        boolean west = parser.next().equals("W");
        double lat = parser.nextDouble(0);
        boolean south = parser.next().equals("S");
        if (lat > 90 || lon > 180) {
            int lonDegrees = (int) (lon * 0.01);
            lon = (lon - lonDegrees * 100) / 60.0;
            lon += lonDegrees;
            int latDegrees = (int) (lat * 0.01);
            lat = (lat - latDegrees * 100) / 60.0;
            lat += latDegrees;
        }
        position.setLongitude(west ? -lon : lon);
        position.setLatitude(south ? -lat : lat);
        position.setSpeed(parser.nextDouble(0));
        position.setCourse(parser.nextDouble(0));
        int day = parser.nextInt(0);
        int month = parser.nextInt(0);
        if (day == 0 && month == 0) {
            // invalid date
            return null;
        }
        DateBuilder dateBuilder = new DateBuilder().setDate(parser.nextInt(0), month, day).setTime(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) Parser(org.traccar.helper.Parser)

Example 49 with DeviceSession

use of org.traccar.DeviceSession in project traccar by tananaev.

the class VtfmsProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    Parser parser = new Parser(PATTERN, (String) msg);
    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());
    position.set(Position.KEY_ALARM, decodeAlarm(parser.nextInt()));
    position.set(Position.KEY_RSSI, parser.nextInt());
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    position.setValid(parser.next().equals("A"));
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.HMS_DMY));
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    if (parser.hasNext()) {
        position.setCourse(parser.nextDouble(0));
    }
    if (parser.hasNext()) {
        String direction = parser.next();
        for (int i = 0; i < DIRECTIONS.length; i++) {
            if (direction.equals(DIRECTIONS[i])) {
                position.setCourse(i * 45.0);
                break;
            }
        }
    }
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
    position.set(Position.KEY_HOURS, parser.nextInt());
    position.set("idleHours", parser.nextInt());
    position.set(Position.KEY_ODOMETER, parser.nextInt() * 100);
    position.set(Position.KEY_CHARGE, parser.next().equals("1"));
    position.set(Position.KEY_POWER, parser.nextDouble());
    position.set(Position.KEY_FUEL_LEVEL, parser.nextInt());
    position.set(Position.PREFIX_ADC + 1, parser.nextDouble());
    position.set(Position.PREFIX_ADC + 2, parser.nextDouble());
    position.set(Position.PREFIX_IN + 1, parser.nextInt());
    position.set(Position.PREFIX_IN + 2, parser.nextInt());
    position.set(Position.PREFIX_IN + 3, parser.nextInt());
    position.set(Position.PREFIX_IN + 4, parser.nextInt());
    position.set(Position.PREFIX_OUT + 1, parser.nextInt());
    position.set(Position.PREFIX_OUT + 2, parser.nextInt());
    position.set(Position.PREFIX_OUT + 3, parser.nextInt());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 50 with DeviceSession

use of org.traccar.DeviceSession in project traccar by tananaev.

the class TrvProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    String id = sentence.startsWith("TRV") ? sentence.substring(0, 3) : sentence.substring(0, 2);
    String type = sentence.substring(id.length(), id.length() + 4);
    if (channel != null) {
        String responseHeader = id + (char) (type.charAt(0) + 1) + type.substring(1);
        if (type.equals("AP00") && id.equals("IW")) {
            String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            channel.write(responseHeader + "," + time + ",0#");
        } else if (type.equals("AP14")) {
            channel.write(responseHeader + ",0.000,0.000#");
        } else {
            channel.write(responseHeader + "#");
        }
    }
    if (type.equals("AP00")) {
        getDeviceSession(channel, remoteAddress, sentence.substring(id.length() + type.length()));
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }
    if (type.equals("CP01")) {
        Parser parser = new Parser(PATTERN_HEATRBEAT, sentence);
        if (!parser.matches()) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, null);
        decodeCommon(position, parser);
        if (parser.hasNext(3)) {
            position.set(Position.KEY_BLOCKED, decodeOptionalValue(parser, 2));
            position.set(Position.KEY_CHARGE, decodeOptionalValue(parser, 1));
            position.set(Position.KEY_MOTION, decodeOptionalValue(parser, 1));
        }
        return position;
    } else if (type.equals("AP01") || type.equals("AP10")) {
        Parser parser = new Parser(PATTERN, sentence);
        if (!parser.matches()) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        DateBuilder dateBuilder = new DateBuilder().setDate(parser.nextInt(), parser.nextInt(), parser.nextInt());
        position.setValid(parser.next().equals("A"));
        position.setLatitude(parser.nextCoordinate());
        position.setLongitude(parser.nextCoordinate());
        position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
        dateBuilder.setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
        position.setTime(dateBuilder.getDate());
        position.setCourse(parser.nextDouble());
        decodeCommon(position, parser);
        position.setNetwork(new Network(CellTower.from(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt())));
        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) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Parser(org.traccar.helper.Parser)

Aggregations

DeviceSession (org.traccar.DeviceSession)197 Position (org.traccar.model.Position)188 Parser (org.traccar.helper.Parser)110 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)64 DateBuilder (org.traccar.helper.DateBuilder)63 Date (java.util.Date)37 Network (org.traccar.model.Network)37 LinkedList (java.util.LinkedList)30 SimpleDateFormat (java.text.SimpleDateFormat)10 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)10 List (java.util.List)9 WifiAccessPoint (org.traccar.model.WifiAccessPoint)9 DateFormat (java.text.DateFormat)8 Pattern (java.util.regex.Pattern)6 StringReader (java.io.StringReader)4 JsonObject (javax.json.JsonObject)4 QueryStringDecoder (org.jboss.netty.handler.codec.http.QueryStringDecoder)4 Matcher (java.util.regex.Matcher)3 Calendar (java.util.Calendar)2 HashSet (java.util.HashSet)2