Search in sources :

Example 96 with DeviceSession

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

the class XexunProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    Pattern pattern = PATTERN_BASIC;
    if (full) {
        pattern = PATTERN_FULL;
    }
    Parser parser = new Parser(pattern, (String) msg);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    if (full) {
        position.set("serial", parser.next());
        position.set("number", parser.next());
    }
    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(convertSpeed(parser.nextDouble(0), "kn"));
    position.setCourse(parser.nextDouble(0));
    dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    position.setTime(dateBuilder.getDate());
    position.set("signal", parser.next());
    decodeStatus(position, parser.next());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    if (full) {
        position.set(Position.KEY_SATELLITES, parser.nextInt());
        position.setAltitude(parser.nextDouble(0));
        position.set(Position.KEY_POWER, parser.nextDouble(0));
    }
    return position;
}
Also used : Pattern(java.util.regex.Pattern) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Parser(org.traccar.helper.Parser)

Example 97 with DeviceSession

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

the class WatchProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // header
    buf.skipBytes(1);
    String manufacturer = buf.readBytes(2).toString(StandardCharsets.US_ASCII);
    // delimiter
    buf.skipBytes(1);
    int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*');
    String id = buf.readBytes(idIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }
    // delimiter
    buf.skipBytes(1);
    String index = null;
    int contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*');
    if (contentIndex + 5 < buf.writerIndex() && buf.getByte(contentIndex + 5) == '*' && buf.toString(contentIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) {
        int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex();
        index = buf.readBytes(indexLength).toString(StandardCharsets.US_ASCII);
        // delimiter
        buf.skipBytes(1);
    }
    // length
    buf.skipBytes(4);
    // delimiter
    buf.skipBytes(1);
    // ignore ending
    buf.writerIndex(buf.writerIndex() - 1);
    contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',');
    if (contentIndex < 0) {
        contentIndex = buf.writerIndex();
    }
    String type = buf.readBytes(contentIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII);
    if (contentIndex < buf.writerIndex()) {
        buf.readerIndex(contentIndex + 1);
    }
    if (type.equals("INIT")) {
        sendResponse(channel, manufacturer, id, index, "INIT,1");
    } else if (type.equals("LK")) {
        sendResponse(channel, manufacturer, id, index, "LK");
        if (buf.readable()) {
            String[] values = buf.toString(StandardCharsets.US_ASCII).split(",");
            if (values.length >= 3) {
                Position position = new Position(getProtocolName());
                position.setDeviceId(deviceSession.getDeviceId());
                getLastLocation(position, null);
                position.set(Position.KEY_BATTERY_LEVEL, Integer.parseInt(values[2]));
                return position;
            }
        }
    } else if (type.equals("UD") || type.equals("UD2") || type.equals("UD3") || type.equals("AL") || type.equals("WT")) {
        if (type.equals("AL")) {
            sendResponse(channel, manufacturer, id, index, "AL");
        }
        return decodePosition(deviceSession, buf.toString(StandardCharsets.US_ASCII));
    } else if (type.equals("TKQ")) {
        sendResponse(channel, manufacturer, id, index, "TKQ");
    } else if (type.equals("PULSE") || type.equals("heart") || type.equals("bphrt")) {
        if (buf.readable()) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());
            getLastLocation(position, new Date());
            String[] values = buf.toString(StandardCharsets.US_ASCII).split(",");
            int valueIndex = 0;
            if (type.equals("bphrt")) {
                position.set("pressureHigh", values[valueIndex++]);
                position.set("pressureLow", values[valueIndex++]);
            }
            position.set("pulse", values[valueIndex]);
            return position;
        }
    } else if (type.equals("img")) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, null);
        int timeIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',');
        buf.readerIndex(timeIndex + 12 + 2);
        position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(id, buf, "jpg"));
        return position;
    } else if (type.equals("TK")) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, null);
        position.set(Position.KEY_AUDIO, Context.getMediaManager().writeFile(id, buf, "amr"));
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) WifiAccessPoint(org.traccar.model.WifiAccessPoint) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 98 with DeviceSession

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

the class Xt013ProtocolDecoder 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;
    }
    Position position = new Position(getProtocolName());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime());
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
    position.setCourse(parser.nextDouble(0));
    position.setAltitude(parser.nextDouble(0));
    position.setValid(parser.next().equals("F"));
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    position.set(Position.KEY_RSSI, parser.nextDouble());
    position.set(Position.KEY_BATTERY, parser.nextDouble(0));
    position.set(Position.KEY_CHARGE, parser.next().equals("1"));
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 99 with DeviceSession

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

the class Xt2400ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    byte[] format = null;
    if (formats.size() > 1) {
        format = formats.get(buf.getUnsignedByte(buf.readerIndex()));
    } else if (!formats.isEmpty()) {
        format = formats.values().iterator().next();
    }
    if (format == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    for (byte tag : format) {
        switch(tag) {
            case 0x03:
                DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(buf.readUnsignedInt()));
                if (deviceSession == null) {
                    return null;
                }
                position.setDeviceId(deviceSession.getDeviceId());
                break;
            case 0x04:
                position.set(Position.KEY_EVENT, buf.readUnsignedByte());
                break;
            case 0x05:
                position.set(Position.KEY_INDEX, buf.readUnsignedShort());
                break;
            case 0x06:
                position.setTime(new Date(buf.readUnsignedInt() * 1000));
                break;
            case 0x07:
                position.setLatitude(buf.readInt() * 0.000001);
                break;
            case 0x08:
                position.setLongitude(buf.readInt() * 0.000001);
                break;
            case 0x09:
                position.setAltitude(buf.readShort() * 0.1);
                break;
            case 0x0a:
                position.setCourse(buf.readShort() * 0.1);
                break;
            case 0x0b:
                position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
                break;
            case 0x10:
                position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt());
                break;
            case 0x12:
                position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
                break;
            case 0x13:
                position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
                break;
            case 0x14:
                position.set(Position.KEY_RSSI, buf.readShort());
                break;
            case 0x16:
                position.set(Position.KEY_BATTERY, buf.readUnsignedByte() * 0.1);
                break;
            case 0x17:
                position.set(Position.KEY_POWER, buf.readUnsignedByte() * 0.1);
                break;
            case 0x57:
                position.set(Position.KEY_OBD_SPEED, UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
                break;
            case 0x65:
                position.set(Position.KEY_VIN, buf.readBytes(17).toString(StandardCharsets.US_ASCII));
                break;
            case 0x73:
                position.set(Position.KEY_VERSION_FW, buf.readBytes(16).toString(StandardCharsets.US_ASCII).trim());
                break;
            default:
                buf.skipBytes(getTagLength(tag));
                break;
        }
    }
    if (position.getLatitude() != 0 && position.getLongitude() != 0) {
        position.setValid(true);
    } else {
        getLastLocation(position, position.getDeviceTime());
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 100 with DeviceSession

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

the class YwtProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    // Synchronization
    if (sentence.startsWith("%SN") && channel != null) {
        int start = sentence.indexOf(':');
        int end = start;
        for (int i = 0; i < 4; i++) {
            end = sentence.indexOf(',', end + 1);
        }
        if (end == -1) {
            end = sentence.length();
        }
        channel.write("%AT+SN=" + sentence.substring(start, end));
        return null;
    }
    Parser parser = new Parser(PATTERN, sentence);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    String type = parser.next();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime());
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
    position.setAltitude(parser.nextDouble(0));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    int satellites = parser.nextInt(0);
    position.setValid(satellites >= 3);
    position.set(Position.KEY_SATELLITES, satellites);
    String reportId = parser.next();
    position.set(Position.KEY_STATUS, parser.next());
    // Send response
    if ((type.equals("KP") || type.equals("EP")) && channel != null) {
        channel.write("%AT+" + type + "=" + reportId + "\r\n");
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) 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