Search in sources :

Example 61 with DeviceSession

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

the class GlobalSatProtocolDecoder method decodeOriginal.

private Position decodeOriginal(Channel channel, SocketAddress remoteAddress, String sentence) {
    if (channel != null) {
        channel.write("ACK\r");
    }
    String format;
    if (sentence.startsWith("GSr")) {
        format = format0;
    } else if (sentence.startsWith("GSh")) {
        format = format1;
    } else {
        return null;
    }
    // Check that message contains required parameters
    if (!format.contains("B") || !format.contains("S") || !(format.contains("1") || format.contains("2") || format.contains("3")) || !(format.contains("6") || format.contains("7") || format.contains("8"))) {
        return null;
    }
    if (format.contains("*")) {
        format = format.substring(0, format.indexOf('*'));
        sentence = sentence.substring(0, sentence.indexOf('*'));
    }
    String[] values = sentence.split(",");
    Position position = new Position(getProtocolName());
    for (int formatIndex = 0, valueIndex = 1; formatIndex < format.length() && valueIndex < values.length; formatIndex++) {
        String value = values[valueIndex];
        switch(format.charAt(formatIndex)) {
            case 'S':
                DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
                if (deviceSession == null) {
                    return null;
                }
                position.setDeviceId(deviceSession.getDeviceId());
                break;
            case 'A':
                if (value.isEmpty()) {
                    position.setValid(false);
                } else {
                    position.setValid(Integer.parseInt(value) != 1);
                }
                break;
            case 'B':
                DateBuilder dateBuilder = new DateBuilder().setDay(Integer.parseInt(value.substring(0, 2))).setMonth(Integer.parseInt(value.substring(2, 4))).setYear(Integer.parseInt(value.substring(4)));
                value = values[++valueIndex];
                dateBuilder.setHour(Integer.parseInt(value.substring(0, 2))).setMinute(Integer.parseInt(value.substring(2, 4))).setSecond(Integer.parseInt(value.substring(4)));
                position.setTime(dateBuilder.getDate());
                break;
            case 'C':
                valueIndex += 1;
                break;
            case '1':
                double longitude = Double.parseDouble(value.substring(1));
                if (value.charAt(0) == 'W') {
                    longitude = -longitude;
                }
                position.setLongitude(longitude);
                break;
            case '2':
                longitude = Double.parseDouble(value.substring(4)) / 60;
                longitude += Integer.parseInt(value.substring(1, 4));
                if (value.charAt(0) == 'W') {
                    longitude = -longitude;
                }
                position.setLongitude(longitude);
                break;
            case '3':
                position.setLongitude(Double.parseDouble(value) * 0.000001);
                break;
            case '6':
                double latitude = Double.parseDouble(value.substring(1));
                if (value.charAt(0) == 'S') {
                    latitude = -latitude;
                }
                position.setLatitude(latitude);
                break;
            case '7':
                latitude = Double.parseDouble(value.substring(3)) / 60;
                latitude += Integer.parseInt(value.substring(1, 3));
                if (value.charAt(0) == 'S') {
                    latitude = -latitude;
                }
                position.setLatitude(latitude);
                break;
            case '8':
                position.setLatitude(Double.parseDouble(value) * 0.000001);
                break;
            case 'G':
                position.setAltitude(Double.parseDouble(value));
                break;
            case 'H':
                position.setSpeed(Double.parseDouble(value));
                break;
            case 'I':
                position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(value)));
                break;
            case 'J':
                position.setSpeed(UnitsConverter.knotsFromMph(Double.parseDouble(value)));
                break;
            case 'K':
                position.setCourse(Double.parseDouble(value));
                break;
            case 'N':
                if (value.endsWith("mV")) {
                    position.set(Position.KEY_BATTERY, Integer.parseInt(value.substring(0, value.length() - 2)) / 1000.0);
                } else {
                    position.set(Position.KEY_BATTERY_LEVEL, Integer.parseInt(value));
                }
                break;
            default:
                // Unsupported
                break;
        }
        valueIndex += 1;
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder)

Example 62 with DeviceSession

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

the class Gt06ProtocolDecoder method decodeExtended.

private Object decodeExtended(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) throws Exception {
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }
    if (deviceSession.getTimeZone() == null) {
        deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId()));
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    // length
    buf.readUnsignedShort();
    int type = buf.readUnsignedByte();
    if (type == MSG_STRING_INFO) {
        // server flag
        buf.readUnsignedInt();
        String data;
        if (buf.readUnsignedByte() == 1) {
            data = buf.readBytes(buf.readableBytes() - 6).toString(StandardCharsets.US_ASCII);
        } else {
            data = buf.readBytes(buf.readableBytes() - 6).toString(StandardCharsets.UTF_16BE);
        }
        if (decodeLocationString(position, data) == null) {
            getLastLocation(position, null);
            position.set(Position.KEY_RESULT, data);
        }
        return position;
    } else if (type == MSG_INFO) {
        int subType = buf.readUnsignedByte();
        getLastLocation(position, null);
        if (subType == 0x00) {
            position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01);
            return position;
        } else if (subType == 0x05) {
            int flags = buf.readUnsignedByte();
            position.set(Position.KEY_DOOR, BitUtil.check(flags, 0));
            position.set(Position.PREFIX_IO + 1, BitUtil.check(flags, 2));
            return position;
        } else if (subType == 0x0d) {
            buf.skipBytes(6);
            return decodeFuelData(position, buf.toString(buf.readerIndex(), buf.readableBytes() - 4 - 2, StandardCharsets.US_ASCII));
        }
    } else if (type == MSG_X1_PHOTO_DATA) {
        int pictureId = buf.readInt();
        ChannelBuffer photo = photos.get(pictureId);
        // offset
        buf.readUnsignedInt();
        buf.readBytes(photo, buf.readUnsignedShort());
        if (photo.writableBytes() > 0) {
            sendPhotoRequest(channel, pictureId);
        } else {
            Device device = Context.getDeviceManager().getById(deviceSession.getDeviceId());
            position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(device.getUniqueId(), photo, "jpg"));
            photos.remove(pictureId);
        }
    } else if (type == MSG_AZ735_GPS || type == MSG_AZ735_ALARM) {
        if (!decodeGps(position, buf, true, deviceSession.getTimeZone())) {
            getLastLocation(position, position.getDeviceTime());
        }
        if (decodeLbs(position, buf, true)) {
            position.set(Position.KEY_RSSI, buf.readUnsignedByte());
        }
        // additional cell towers
        buf.skipBytes(buf.readUnsignedByte());
        // wifi access point
        buf.skipBytes(buf.readUnsignedByte());
        int status = buf.readUnsignedByte();
        position.set(Position.KEY_STATUS, status);
        if (type == MSG_AZ735_ALARM) {
            switch(status) {
                case 0xA0:
                    position.set(Position.KEY_ARMED, true);
                    break;
                case 0xA1:
                    position.set(Position.KEY_ARMED, false);
                    break;
                case 0xA2:
                case 0xA3:
                    position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY);
                    break;
                case 0xA4:
                    position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
                    break;
                case 0xA5:
                    position.set(Position.KEY_ALARM, Position.ALARM_DOOR);
                    break;
                default:
                    break;
            }
        }
        // reserved extension
        buf.skipBytes(buf.readUnsignedByte());
        sendResponse(channel, true, type, buf.getShort(buf.writerIndex() - 6), null);
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Device(org.traccar.model.Device) WifiAccessPoint(org.traccar.model.WifiAccessPoint) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 63 with DeviceSession

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

the class Gt06ProtocolDecoder method decodeBasic.

private Object decodeBasic(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) throws Exception {
    int length = buf.readUnsignedByte();
    int dataLength = length - 5;
    int type = buf.readUnsignedByte();
    DeviceSession deviceSession = null;
    if (type != MSG_LOGIN) {
        deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        if (deviceSession.getTimeZone() == null) {
            deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId()));
        }
    }
    if (type == MSG_LOGIN) {
        String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
        // type
        buf.readUnsignedShort();
        deviceSession = getDeviceSession(channel, remoteAddress, imei);
        if (deviceSession != null && deviceSession.getTimeZone() == null) {
            deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId()));
        }
        if (dataLength > 10) {
            int extensionBits = buf.readUnsignedShort();
            int hours = (extensionBits >> 4) / 100;
            int minutes = (extensionBits >> 4) % 100;
            int offset = (hours * 60 + minutes) * 60;
            if ((extensionBits & 0x8) != 0) {
                offset = -offset;
            }
            if (deviceSession != null) {
                TimeZone timeZone = deviceSession.getTimeZone();
                if (timeZone.getRawOffset() == 0) {
                    timeZone.setRawOffset(offset * 1000);
                    deviceSession.setTimeZone(timeZone);
                }
            }
        }
        if (deviceSession != null) {
            sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null);
        }
    } else if (type == MSG_HEARTBEAT) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, null);
        int status = buf.readUnsignedByte();
        position.set(Position.KEY_ARMED, BitUtil.check(status, 0));
        position.set(Position.KEY_IGNITION, BitUtil.check(status, 1));
        position.set(Position.KEY_CHARGE, BitUtil.check(status, 2));
        sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null);
        return position;
    } else if (type == MSG_ADDRESS_REQUEST) {
        String response = "NA&&NA&&0##";
        ChannelBuffer content = ChannelBuffers.dynamicBuffer();
        content.writeByte(response.length());
        content.writeInt(0);
        content.writeBytes(response.getBytes(StandardCharsets.US_ASCII));
        sendResponse(channel, true, MSG_ADDRESS_RESPONSE, 0, content);
    } else if (type == MSG_TIME_REQUEST) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        ChannelBuffer content = ChannelBuffers.dynamicBuffer();
        content.writeByte(calendar.get(Calendar.YEAR) - 2000);
        content.writeByte(calendar.get(Calendar.MONTH) + 1);
        content.writeByte(calendar.get(Calendar.DAY_OF_MONTH));
        content.writeByte(calendar.get(Calendar.HOUR_OF_DAY));
        content.writeByte(calendar.get(Calendar.MINUTE));
        content.writeByte(calendar.get(Calendar.SECOND));
        sendResponse(channel, false, MSG_TIME_REQUEST, 0, content);
    } else if (type == MSG_X1_GPS) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        // data and alarm
        buf.readUnsignedInt();
        decodeGps(position, buf, false, deviceSession.getTimeZone());
        // terminal info
        buf.readUnsignedShort();
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
        position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedByte(), buf.readUnsignedShort(), buf.readUnsignedInt())));
        return position;
    } else if (type == MSG_X1_PHOTO_INFO) {
        // time
        buf.skipBytes(6);
        // fix status
        buf.readUnsignedByte();
        // latitude
        buf.readUnsignedInt();
        // longitude
        buf.readUnsignedInt();
        // camera id
        buf.readUnsignedByte();
        // photo source
        buf.readUnsignedByte();
        // picture format
        buf.readUnsignedByte();
        ChannelBuffer photo = ChannelBuffers.buffer(buf.readInt());
        int pictureId = buf.readInt();
        photos.put(pictureId, photo);
        sendPhotoRequest(channel, pictureId);
    } else if (type == MSG_WIFI || type == MSG_WIFI_2) {
        return decodeWifi(buf, deviceSession);
    } else {
        return decodeBasicOther(channel, buf, deviceSession, type, dataLength);
    }
    return null;
}
Also used : TimeZone(java.util.TimeZone) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Calendar(java.util.Calendar) Network(org.traccar.model.Network) WifiAccessPoint(org.traccar.model.WifiAccessPoint) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 64 with DeviceSession

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

the class H02ProtocolDecoder method decodeLbs.

private Position decodeLbs(String sentence, Channel channel, SocketAddress remoteAddress) {
    Parser parser = new Parser(PATTERN_NBR, sentence);
    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());
    DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    Network network = new Network();
    int mcc = parser.nextInt(0);
    int mnc = parser.nextInt(0);
    String[] cells = parser.next().split(",");
    for (int i = 0; i < cells.length / 3; i++) {
        network.addCellTower(CellTower.from(mcc, mnc, Integer.parseInt(cells[i * 3]), Integer.parseInt(cells[i * 3 + 1]), Integer.parseInt(cells[i * 3 + 2])));
    }
    position.setNetwork(network);
    dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    getLastLocation(position, dateBuilder.getDate());
    processStatus(position, parser.nextLong(16, 0));
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Network(org.traccar.model.Network) Parser(org.traccar.helper.Parser)

Example 65 with DeviceSession

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

the class H02ProtocolDecoder method decodeBinary.

private Position decodeBinary(ChannelBuffer buf, Channel channel, SocketAddress remoteAddress) {
    Position position = new Position(getProtocolName());
    // marker
    buf.readByte();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ChannelBuffers.hexDump(buf.readBytes(5)));
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    DateBuilder dateBuilder = new DateBuilder().setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)).setSecond(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2)).setMonth(BcdUtil.readInteger(buf, 2)).setYear(BcdUtil.readInteger(buf, 2));
    position.setTime(dateBuilder.getDate());
    double latitude = readCoordinate(buf, false);
    position.set(Position.KEY_BATTERY_LEVEL, decodeBattery(buf.readUnsignedByte()));
    double longitude = readCoordinate(buf, true);
    int flags = buf.readUnsignedByte() & 0x0f;
    position.setValid((flags & 0x02) != 0);
    if ((flags & 0x04) == 0) {
        latitude = -latitude;
    }
    if ((flags & 0x08) == 0) {
        longitude = -longitude;
    }
    position.setLatitude(latitude);
    position.setLongitude(longitude);
    position.setSpeed(BcdUtil.readInteger(buf, 3));
    position.setCourse((buf.readUnsignedByte() & 0x0f) * 100.0 + BcdUtil.readInteger(buf, 2));
    processStatus(position, buf.readUnsignedInt());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder)

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