Search in sources :

Example 16 with DeviceSession

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

the class DishaProtocolDecoder 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.setValid(parser.next().equals("A"));
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.HMS_DMY));
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    position.set(Position.KEY_HDOP, parser.nextDouble());
    position.set(Position.KEY_RSSI, parser.nextDouble());
    position.set(Position.KEY_CHARGE, parser.nextInt(0) == 2);
    position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt(0));
    position.set(Position.PREFIX_ADC + 1, parser.nextInt(0));
    position.set(Position.PREFIX_ADC + 2, parser.nextInt(0));
    position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000);
    position.set(Position.KEY_INPUT, parser.next());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 17 with DeviceSession

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

the class DmtProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // header
    buf.skipBytes(2);
    int type = buf.readUnsignedByte();
    int length = buf.readUnsignedShort();
    if (type == MSG_HELLO) {
        // device serial number
        buf.readUnsignedInt();
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII));
        ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
        if (length == 51) {
            // reserved
            response.writeByte(0);
            // reserved
            response.writeInt(0);
        } else {
            response.writeInt((int) ((System.currentTimeMillis() - 1356998400000L) / 1000));
            // flags
            response.writeInt(deviceSession != null ? 0 : 1);
        }
        sendResponse(channel, MSG_HELLO_RESPONSE, response);
    } else if (type == MSG_COMMIT) {
        ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
        // flags (success)
        response.writeByte(1);
        sendResponse(channel, MSG_COMMIT_RESPONSE, response);
    } else if (type == MSG_CANNED_REQUEST_1) {
        ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
        response.writeBytes(new byte[12]);
        sendResponse(channel, MSG_CANNED_RESPONSE_1, response);
    } else if (type == MSG_CANNED_REQUEST_2) {
        sendResponse(channel, MSG_CANNED_RESPONSE_2, null);
    } else if (type == MSG_DATA_RECORD_64) {
        return decodeFixed64(channel, remoteAddress, buf);
    } else if (type == MSG_DATA_RECORD) {
        return decodeStandard(channel, remoteAddress, buf);
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 18 with DeviceSession

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

the class DmtProtocolDecoder method decodeStandard.

private List<Position> decodeStandard(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }
    List<Position> positions = new LinkedList<>();
    while (buf.readable()) {
        int recordEnd = buf.readerIndex() + buf.readUnsignedShort();
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.set(Position.KEY_INDEX, buf.readUnsignedInt());
        // since 1 Jan 2013
        position.setDeviceTime(new Date(1356998400000L + buf.readUnsignedInt() * 1000));
        position.set(Position.KEY_EVENT, buf.readUnsignedByte());
        while (buf.readerIndex() < recordEnd) {
            int fieldId = buf.readUnsignedByte();
            int fieldLength = buf.readUnsignedByte();
            int fieldEnd = buf.readerIndex() + (fieldLength == 255 ? buf.readUnsignedShort() : fieldLength);
            if (fieldId == 0) {
                position.setFixTime(new Date(1356998400000L + buf.readUnsignedInt() * 1000));
                position.setLatitude(buf.readInt() * 0.0000001);
                position.setLongitude(buf.readInt() * 0.0000001);
                position.setAltitude(buf.readShort());
                position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShort()));
                // speed accuracy
                buf.readUnsignedByte();
                position.setCourse(buf.readUnsignedByte() * 2);
                position.set(Position.KEY_PDOP, buf.readUnsignedByte() * 0.1);
                position.setAccuracy(buf.readUnsignedByte());
                position.setValid(buf.readUnsignedByte() != 0);
            } else if (fieldId == 2) {
                int input = buf.readInt();
                int output = buf.readUnsignedShort();
                int status = buf.readUnsignedShort();
                position.set(Position.KEY_IGNITION, BitUtil.check(input, 0));
                position.set(Position.KEY_INPUT, input);
                position.set(Position.KEY_OUTPUT, output);
                position.set(Position.KEY_STATUS, status);
            } else if (fieldId == 6) {
                while (buf.readerIndex() < fieldEnd) {
                    switch(buf.readUnsignedByte()) {
                        case 1:
                            position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
                            break;
                        case 2:
                            position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01);
                            break;
                        case 3:
                            position.set(Position.KEY_DEVICE_TEMP, buf.readShort() * 0.01);
                            break;
                        case 4:
                            position.set(Position.KEY_RSSI, buf.readUnsignedShort());
                            break;
                        case 5:
                            position.set("solarPower", buf.readUnsignedShort() * 0.001);
                            break;
                        default:
                            break;
                    }
                }
            }
            buf.readerIndex(fieldEnd);
        }
        if (position.getFixTime() == null) {
            getLastLocation(position, position.getDeviceTime());
        }
        positions.add(position);
    }
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) LinkedList(java.util.LinkedList) Date(java.util.Date)

Example 19 with DeviceSession

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

the class EasyTrackProtocolDecoder 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.set(Position.KEY_COMMAND, parser.next());
    position.setValid(parser.next().equals("A"));
    DateBuilder dateBuilder = new DateBuilder().setDate(parser.nextHexInt(0), parser.nextHexInt(0), parser.nextHexInt(0)).setTime(parser.nextHexInt(0), parser.nextHexInt(0), parser.nextHexInt(0));
    position.setTime(dateBuilder.getDate());
    if (BitUtil.check(parser.nextHexInt(0), 3)) {
        position.setLatitude(-parser.nextHexInt(0) / 600000.0);
    } else {
        position.setLatitude(parser.nextHexInt(0) / 600000.0);
    }
    if (BitUtil.check(parser.nextHexInt(0), 3)) {
        position.setLongitude(-parser.nextHexInt(0) / 600000.0);
    } else {
        position.setLongitude(parser.nextHexInt(0) / 600000.0);
    }
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextHexInt(0) / 100.0));
    position.setCourse(parser.nextHexInt(0) / 100.0);
    position.set(Position.KEY_STATUS, parser.next());
    position.set("signal", parser.next());
    position.set(Position.KEY_POWER, parser.nextDouble(0));
    position.set("oil", parser.nextHexInt(0));
    position.set(Position.KEY_ODOMETER, parser.nextHexInt(0) * 100);
    position.setAltitude(parser.nextDouble(0));
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Parser(org.traccar.helper.Parser)

Example 20 with DeviceSession

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

the class EelinkProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    String uniqueId = null;
    DeviceSession deviceSession;
    if (buf.getByte(0) == 'E' && buf.getByte(1) == 'L') {
        // udp header
        buf.skipBytes(2 + 2 + 2);
        uniqueId = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
        deviceSession = getDeviceSession(channel, remoteAddress, uniqueId);
    } else {
        deviceSession = getDeviceSession(channel, remoteAddress);
    }
    // header
    buf.skipBytes(2);
    int type = buf.readUnsignedByte();
    // length
    buf.readShort();
    int index = buf.readUnsignedShort();
    if (type != MSG_GPS && type != MSG_DATA) {
        sendResponse(channel, remoteAddress, uniqueId, type, index);
    }
    if (type == MSG_LOGIN) {
        if (deviceSession == null) {
            getDeviceSession(channel, remoteAddress, ChannelBuffers.hexDump(buf.readBytes(8)).substring(1));
        }
    } else {
        if (deviceSession == null) {
            return null;
        }
        if (type == MSG_GPS || type == MSG_ALARM || type == MSG_STATE || type == MSG_SMS) {
            return decodeOld(deviceSession, buf, type, index);
        } else if (type >= MSG_NORMAL && type <= MSG_OBD_CODE) {
            return decodeNew(deviceSession, buf, index);
        } else if (type == MSG_HEARTBEAT && buf.readableBytes() >= 2) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());
            getLastLocation(position, null);
            decodeStatus(position, buf.readUnsignedShort());
            return position;
        } else if (type == MSG_DOWNLINK) {
            return decodeResult(deviceSession, buf, index);
        }
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

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