Search in sources :

Example 11 with DeviceSession

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

the class BceProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    String imei = String.format("%015d", buf.readLong());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    List<Position> positions = new LinkedList<>();
    while (buf.readableBytes() > 1) {
        int dataEnd = buf.readUnsignedShort() + buf.readerIndex();
        int type = buf.readUnsignedByte();
        int confirmKey = buf.readUnsignedByte() & 0x7F;
        while (buf.readerIndex() < dataEnd) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());
            int structEnd = buf.readUnsignedByte() + buf.readerIndex();
            long time = buf.readUnsignedInt();
            if ((time & 0x0f) == DATA_TYPE) {
                time = time >> 4 << 1;
                // 01/01/2008
                time += 0x47798280;
                position.setTime(new Date(time * 1000));
                // Read masks
                int mask;
                List<Integer> masks = new LinkedList<>();
                do {
                    mask = buf.readUnsignedShort();
                    masks.add(mask);
                } while (BitUtil.check(mask, 15));
                mask = masks.get(0);
                if (BitUtil.check(mask, 0)) {
                    position.setValid(true);
                    position.setLongitude(buf.readFloat());
                    position.setLatitude(buf.readFloat());
                    position.setSpeed(buf.readUnsignedByte());
                    int gps = buf.readUnsignedByte();
                    position.set(Position.KEY_SATELLITES, gps & 0xf);
                    position.set(Position.KEY_HDOP, gps >> 4);
                    position.setCourse(buf.readUnsignedByte());
                    position.setAltitude(buf.readUnsignedShort());
                    position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
                }
                if (BitUtil.check(mask, 1)) {
                    position.set(Position.KEY_INPUT, buf.readUnsignedShort());
                }
                for (int i = 1; i <= 8; i++) {
                    if (BitUtil.check(mask, i + 1)) {
                        position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort());
                    }
                }
                if (BitUtil.check(mask, 10)) {
                    buf.skipBytes(4);
                }
                if (BitUtil.check(mask, 11)) {
                    buf.skipBytes(4);
                }
                if (BitUtil.check(mask, 12)) {
                    buf.skipBytes(2);
                }
                if (BitUtil.check(mask, 13)) {
                    buf.skipBytes(2);
                }
                if (BitUtil.check(mask, 14)) {
                    position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedByte(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedByte())));
                    buf.readUnsignedByte();
                }
                if (BitUtil.check(mask, 0)) {
                    positions.add(position);
                }
            }
            buf.readerIndex(structEnd);
        }
        // Send response
        if (type == MSG_ASYNC_STACK && channel != null) {
            ChannelBuffer response = ChannelBuffers.buffer(ByteOrder.LITTLE_ENDIAN, 8 + 2 + 2 + 1);
            response.writeLong(Long.parseLong(imei));
            response.writeShort(2);
            response.writeByte(MSG_STACK_COFIRM);
            response.writeByte(confirmKey);
            int checksum = 0;
            for (int i = 0; i < response.writerIndex(); i++) {
                checksum += response.getUnsignedByte(i);
            }
            response.writeByte(checksum);
            channel.write(response);
        }
    }
    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) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 12 with DeviceSession

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

the class BlackKiteProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // header
    buf.readUnsignedByte();
    int length = (buf.readUnsignedShort() & 0x7fff) + 3;
    List<Position> positions = new LinkedList<>();
    Set<Integer> tags = new HashSet<>();
    boolean hasLocation = false;
    Position position = new Position(getProtocolName());
    while (buf.readerIndex() < length) {
        // Check if new message started
        int tag = buf.readUnsignedByte();
        if (tags.contains(tag)) {
            if (hasLocation && position.getFixTime() != null) {
                positions.add(position);
            }
            tags.clear();
            hasLocation = false;
            position = new Position(getProtocolName());
        }
        tags.add(tag);
        switch(tag) {
            case TAG_IMEI:
                getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII));
                break;
            case TAG_DATE:
                position.setTime(new Date(buf.readUnsignedInt() * 1000));
                break;
            case TAG_COORDINATES:
                hasLocation = true;
                position.setValid((buf.readUnsignedByte() & 0xf0) == 0x00);
                position.setLatitude(buf.readInt() / 1000000.0);
                position.setLongitude(buf.readInt() / 1000000.0);
                break;
            case TAG_SPEED_COURSE:
                position.setSpeed(buf.readUnsignedShort() * 0.0539957);
                position.setCourse(buf.readUnsignedShort() * 0.1);
                break;
            case TAG_ALTITUDE:
                position.setAltitude(buf.readShort());
                break;
            case TAG_STATUS:
                int status = buf.readUnsignedShort();
                position.set(Position.KEY_IGNITION, BitUtil.check(status, 9));
                if (BitUtil.check(status, 15)) {
                    position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
                }
                position.set(Position.KEY_CHARGE, BitUtil.check(status, 2));
                break;
            case TAG_DIGITAL_INPUTS:
                int input = buf.readUnsignedShort();
                for (int i = 0; i < 16; i++) {
                    position.set(Position.PREFIX_IO + (i + 1), BitUtil.check(input, i));
                }
                break;
            case TAG_DIGITAL_OUTPUTS:
                int output = buf.readUnsignedShort();
                for (int i = 0; i < 16; i++) {
                    position.set(Position.PREFIX_IO + (i + 17), BitUtil.check(output, i));
                }
                break;
            case TAG_INPUT_VOLTAGE1:
                position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort() / 1000.0);
                break;
            case TAG_INPUT_VOLTAGE2:
                position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort() / 1000.0);
                break;
            case TAG_INPUT_VOLTAGE3:
                position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort() / 1000.0);
                break;
            case TAG_INPUT_VOLTAGE4:
                position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShort() / 1000.0);
                break;
            case TAG_XT1:
            case TAG_XT2:
            case TAG_XT3:
                buf.skipBytes(16);
                break;
            default:
                break;
        }
    }
    if (hasLocation && position.getFixTime() != null) {
        positions.add(position);
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }
    sendReply(channel, buf.readUnsignedShort());
    for (Position p : positions) {
        p.setDeviceId(deviceSession.getDeviceId());
    }
    if (positions.isEmpty()) {
        return null;
    }
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) LinkedList(java.util.LinkedList) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) HashSet(java.util.HashSet)

Example 13 with DeviceSession

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

the class CalAmpProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    if (BitUtil.check(buf.getByte(buf.readerIndex()), 7)) {
        int content = buf.readUnsignedByte();
        if (BitUtil.check(content, 0)) {
            String id = ChannelBuffers.hexDump(buf.readBytes(buf.readUnsignedByte()));
            getDeviceSession(channel, remoteAddress, id);
        }
        if (BitUtil.check(content, 1)) {
            // identifier type
            buf.skipBytes(buf.readUnsignedByte());
        }
        if (BitUtil.check(content, 2)) {
            // authentication
            buf.skipBytes(buf.readUnsignedByte());
        }
        if (BitUtil.check(content, 3)) {
            // routing
            buf.skipBytes(buf.readUnsignedByte());
        }
        if (BitUtil.check(content, 4)) {
            // forwarding
            buf.skipBytes(buf.readUnsignedByte());
        }
        if (BitUtil.check(content, 5)) {
            // response redirection
            buf.skipBytes(buf.readUnsignedByte());
        }
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }
    int service = buf.readUnsignedByte();
    int type = buf.readUnsignedByte();
    int index = buf.readUnsignedShort();
    if (service == SERVICE_ACKNOWLEDGED) {
        sendResponse(channel, remoteAddress, type, index, 0);
    }
    if (type == MSG_EVENT_REPORT || type == MSG_LOCATE_REPORT || type == MSG_MINI_EVENT_REPORT) {
        return decodePosition(deviceSession, type, buf);
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 14 with DeviceSession

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

the class CarTrackProtocolDecoder 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());
    DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), 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(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    position.setTime(dateBuilder.getDate());
    position.set(Position.PREFIX_IO + 1, parser.next());
    String odometer = parser.next();
    odometer = odometer.replace(":", "A");
    odometer = odometer.replace(";", "B");
    odometer = odometer.replace("<", "C");
    odometer = odometer.replace("=", "D");
    odometer = odometer.replace(">", "E");
    odometer = odometer.replace("?", "F");
    position.set(Position.KEY_ODOMETER, Integer.parseInt(odometer, 16));
    // there is no meaningful alarms
    parser.next();
    position.set(Position.PREFIX_ADC + 1, parser.next());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Parser(org.traccar.helper.Parser)

Example 15 with DeviceSession

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

the class CarcellProtocolDecoder 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());
    position.set(Position.KEY_ARCHIVE, parser.next().equals("%"));
    position.setValid(true);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    if (parser.hasNext(8)) {
        position.setLatitude(parser.nextCoordinate(CoordinateFormat.HEM_DEG_MIN_MIN));
        position.setLongitude(parser.nextCoordinate(CoordinateFormat.HEM_DEG_MIN_MIN));
    }
    if (parser.hasNext(4)) {
        position.setLatitude(parser.nextCoordinate(CoordinateFormat.HEM_DEG));
        position.setLongitude(parser.nextCoordinate(CoordinateFormat.HEM_DEG));
    }
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextInt(0)));
    position.setCourse(parser.nextInt(0));
    if (parser.hasNext(3)) {
        position.set("x", parser.nextInt(0));
        position.set("y", parser.nextInt(0));
        position.set("z", parser.nextInt(0));
    }
    if (parser.hasNext(1)) {
        position.set(Position.KEY_ACCELERATION, parser.nextInt(0));
    }
    Double internalBattery = (parser.nextDouble(0) + 100d) * 0.0294d;
    position.set(Position.KEY_BATTERY, internalBattery);
    position.set(Position.KEY_RSSI, parser.nextInt(0));
    position.set("jamming", parser.next().equals("1"));
    position.set(Position.KEY_GPS, parser.nextInt(0));
    position.set("clockType", parser.next());
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
    position.set("blocked", parser.next().equals("1"));
    position.set(Position.KEY_IGNITION, parser.next().equals("1"));
    if (parser.hasNext(4)) {
        position.set("cloned", parser.next().equals("1"));
        // panic button status
        parser.next();
        String painelStatus = parser.next();
        if (painelStatus.equals("1")) {
            position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
        }
        position.set("painel", painelStatus.equals("2"));
        Double mainVoltage = parser.nextDouble(0) / 100d;
        position.set(Position.KEY_POWER, mainVoltage);
    }
    if (parser.hasNext(5)) {
        position.set("timeUntilDelivery", parser.nextInt(0));
        // panic button status
        parser.next();
        position.set(Position.KEY_INPUT, parser.next());
        Double mainVoltage = parser.nextDouble(0) / 100d;
        position.set(Position.KEY_POWER, mainVoltage);
        position.set("iccid", parser.next());
    }
    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