Search in sources :

Example 26 with DeviceSession

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

the class GranitProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("~"));
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession != null && indexTilde == -1) {
        String bufString = buf.toString(StandardCharsets.US_ASCII);
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.setTime(new Date());
        getLastLocation(position, new Date());
        position.setValid(false);
        position.set(Position.KEY_RESULT, bufString);
        return position;
    }
    if (buf.readableBytes() < HEADER_LENGTH) {
        return null;
    }
    String header = buf.readBytes(HEADER_LENGTH).toString(StandardCharsets.US_ASCII);
    if (header.equals("+RRCB~")) {
        // binary length 26
        buf.skipBytes(2);
        int deviceId = buf.readUnsignedShort();
        deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId));
        if (deviceSession == null) {
            return null;
        }
        long unixTime = buf.readUnsignedInt();
        if (channel != null) {
            sendResponseCurrent(channel, deviceId, unixTime);
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.setTime(new Date(unixTime * 1000));
        decodeStructure(buf, position);
        return position;
    } else if (header.equals("+DDAT~")) {
        // binary length
        buf.skipBytes(2);
        int deviceId = buf.readUnsignedShort();
        deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId));
        if (deviceSession == null) {
            return null;
        }
        byte format = buf.readByte();
        if (format != 4) {
            return null;
        }
        byte nblocks = buf.readByte();
        int packNum = buf.readUnsignedShort();
        if (channel != null) {
            sendResponseArchive(channel, deviceId, packNum);
        }
        List<Position> positions = new ArrayList<>();
        while (nblocks > 0) {
            nblocks--;
            long unixTime = buf.readUnsignedInt();
            int timeIncrement = buf.getUnsignedShort(buf.readerIndex() + 120);
            for (int i = 0; i < 6; i++) {
                if (buf.getUnsignedByte(buf.readerIndex()) != 0xFE) {
                    Position position = new Position(getProtocolName());
                    position.setDeviceId(deviceSession.getDeviceId());
                    position.setTime(new Date((unixTime + i * timeIncrement) * 1000));
                    decodeStructure(buf, position);
                    position.set(Position.KEY_ARCHIVE, true);
                    positions.add(position);
                } else {
                    // skip filled 0xFE structure
                    buf.skipBytes(20);
                }
            }
            // increment
            buf.skipBytes(2);
        }
        return positions;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) StringFinder(org.traccar.helper.StringFinder) ArrayList(java.util.ArrayList) List(java.util.List) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 27 with DeviceSession

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

the class StarLinkProtocolDecoder 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;
    }
    int type = parser.nextInt(0);
    if (type != MSG_EVENT_REPORT) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.setValid(true);
    position.set(Position.KEY_INDEX, parser.nextInt(0));
    String[] data = parser.next().split(",");
    Integer lac = null, cid = null;
    int event = 0;
    for (int i = 0; i < Math.min(data.length, dataTags.length); i++) {
        if (data[i].isEmpty()) {
            continue;
        }
        switch(dataTags[i]) {
            case "#EDT#":
                position.setDeviceTime(dateFormat.parse(data[i]));
                break;
            case "#EID#":
                event = Integer.parseInt(data[i]);
                position.set(Position.KEY_ALARM, decodeAlarm(event));
                position.set(Position.KEY_EVENT, event);
                break;
            case "#PDT#":
                position.setFixTime(dateFormat.parse(data[i]));
                break;
            case "#LAT#":
                position.setLatitude(parseCoordinate(data[i]));
                break;
            case "#LONG#":
                position.setLongitude(parseCoordinate(data[i]));
                break;
            case "#SPD#":
                position.setSpeed(Double.parseDouble(data[i]));
                break;
            case "#HEAD#":
                position.setCourse(Integer.parseInt(data[i]));
                break;
            case "#ODO#":
                position.set(Position.KEY_ODOMETER, Long.parseLong(data[i]) * 1000);
                break;
            case "#IN1#":
                position.set(Position.PREFIX_IN + 1, Integer.parseInt(data[i]));
                break;
            case "#IN2#":
                position.set(Position.PREFIX_IN + 2, Integer.parseInt(data[i]));
                break;
            case "#IN3#":
                position.set(Position.PREFIX_IN + 3, Integer.parseInt(data[i]));
                break;
            case "#IN4#":
                position.set(Position.PREFIX_IN + 4, Integer.parseInt(data[i]));
                break;
            case "#OUT1#":
                position.set(Position.PREFIX_OUT + 1, Integer.parseInt(data[i]));
                break;
            case "#OUT2#":
                position.set(Position.PREFIX_OUT + 2, Integer.parseInt(data[i]));
                break;
            case "#OUT3#":
                position.set(Position.PREFIX_OUT + 3, Integer.parseInt(data[i]));
                break;
            case "#OUT4#":
                position.set(Position.PREFIX_OUT + 4, Integer.parseInt(data[i]));
                break;
            case "#LAC#":
                if (!data[i].isEmpty()) {
                    lac = Integer.parseInt(data[i]);
                }
                break;
            case "#CID#":
                if (!data[i].isEmpty()) {
                    cid = Integer.parseInt(data[i]);
                }
                break;
            case "#VIN#":
                position.set(Position.KEY_POWER, Double.parseDouble(data[i]));
                break;
            case "#VBAT#":
                position.set(Position.KEY_BATTERY, Double.parseDouble(data[i]));
                break;
            case "#DEST#":
                position.set("destination", data[i]);
                break;
            case "#IGN#":
                position.set(Position.KEY_IGNITION, data[i].equals("1"));
                break;
            case "#ENG#":
                position.set("engine", data[i].equals("1"));
                break;
            default:
                break;
        }
    }
    if (position.getFixTime() == null) {
        getLastLocation(position, null);
    }
    if (lac != null && cid != null) {
        position.setNetwork(new Network(CellTower.fromLacCid(lac, cid)));
    }
    if (event == 20) {
        String rfid = data[data.length - 1];
        if (rfid.matches("0+")) {
            rfid = data[data.length - 2];
        }
        position.set(Position.KEY_DRIVER_UNIQUE_ID, rfid);
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) Parser(org.traccar.helper.Parser)

Example 28 with DeviceSession

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

the class TaipProtocolDecoder method decodeAttributes.

private Position decodeAttributes(Channel channel, SocketAddress remoteAddress, Position position, String[] attributes) {
    String uniqueId = null;
    DeviceSession deviceSession = null;
    String messageIndex = null;
    if (attributes != null) {
        for (String attribute : attributes) {
            int index = attribute.indexOf('=');
            if (index != -1) {
                String key = attribute.substring(0, index).toLowerCase();
                String value = attribute.substring(index + 1);
                switch(key) {
                    case "id":
                        uniqueId = value;
                        deviceSession = getDeviceSession(channel, remoteAddress, value);
                        if (deviceSession != null) {
                            position.setDeviceId(deviceSession.getDeviceId());
                        }
                        break;
                    case "io":
                        position.set(Position.KEY_IGNITION, BitUtil.check(value.charAt(0) - '0', 0));
                        position.set(Position.KEY_CHARGE, BitUtil.check(value.charAt(0) - '0', 1));
                        position.set(Position.KEY_OUTPUT, value.charAt(1) - '0');
                        position.set(Position.KEY_INPUT, value.charAt(2) - '0');
                        break;
                    case "ix":
                        position.set(Position.PREFIX_IO + 1, value);
                        break;
                    case "ad":
                        position.set(Position.PREFIX_ADC + 1, Integer.parseInt(value));
                        break;
                    case "sv":
                        position.set(Position.KEY_SATELLITES, Integer.parseInt(value));
                        break;
                    case "bl":
                        position.set(Position.KEY_BATTERY, Integer.parseInt(value) * 0.001);
                        break;
                    case "vo":
                        position.set(Position.KEY_ODOMETER, Long.parseLong(value));
                        break;
                    default:
                        position.set(key, value);
                        break;
                }
            } else if (attribute.startsWith("#")) {
                messageIndex = attribute;
            }
        }
    }
    if (deviceSession != null) {
        if (channel != null) {
            if (messageIndex != null) {
                String response = ">ACK;ID=" + uniqueId + ";" + messageIndex + ";*";
                response += String.format("%02X", Checksum.xor(response)) + "<";
                channel.write(response, remoteAddress);
            } else {
                channel.write(uniqueId, remoteAddress);
            }
        }
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession)

Example 29 with DeviceSession

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

the class SuntechProtocolDecoder method decode2356.

private Position decode2356(Channel channel, SocketAddress remoteAddress, String protocol, String[] values) throws ParseException {
    int index = 0;
    String type = values[index++].substring(5);
    if (!type.equals("STT") && !type.equals("EMG") && !type.equals("EVT") && !type.equals("ALT")) {
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, values[index++]);
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.set(Position.KEY_TYPE, type);
    if (protocol.equals("ST300") || protocol.equals("ST500") || protocol.equals("ST600")) {
        // model
        index += 1;
    }
    position.set(Position.KEY_VERSION_FW, values[index++]);
    DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHH:mm:ss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    position.setTime(dateFormat.parse(values[index++] + values[index++]));
    if (!protocol.equals("ST500")) {
        int cid = Integer.parseInt(values[index++], 16);
        if (protocol.equals("ST600")) {
            position.setNetwork(new Network(CellTower.from(Integer.parseInt(values[index++]), Integer.parseInt(values[index++]), Integer.parseInt(values[index++], 16), cid, Integer.parseInt(values[index++]))));
        }
    }
    position.setLatitude(Double.parseDouble(values[index++]));
    position.setLongitude(Double.parseDouble(values[index++]));
    position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(values[index++])));
    position.setCourse(Double.parseDouble(values[index++]));
    position.set(Position.KEY_SATELLITES, Integer.parseInt(values[index++]));
    position.setValid(values[index++].equals("1"));
    position.set(Position.KEY_ODOMETER, Integer.parseInt(values[index++]));
    position.set(Position.KEY_POWER, Double.parseDouble(values[index++]));
    String io = values[index++];
    if (io.length() == 6) {
        position.set(Position.KEY_IGNITION, io.charAt(0) == '1');
        position.set(Position.PREFIX_IN + 1, io.charAt(1) == '1');
        position.set(Position.PREFIX_IN + 2, io.charAt(2) == '1');
        position.set(Position.PREFIX_IN + 3, io.charAt(3) == '1');
        position.set(Position.PREFIX_OUT + 1, io.charAt(4) == '1');
        position.set(Position.PREFIX_OUT + 2, io.charAt(5) == '1');
    }
    switch(type) {
        case "STT":
            // mode
            index += 1;
            position.set(Position.KEY_INDEX, Integer.parseInt(values[index++]));
            break;
        case "EMG":
            position.set(Position.KEY_ALARM, decodeEmergency(Integer.parseInt(values[index++])));
            break;
        case "EVT":
            position.set(Position.KEY_EVENT, Integer.parseInt(values[index++]));
            break;
        case "ALT":
            position.set(Position.KEY_ALARM, decodeAlert(Integer.parseInt(values[index++])));
            break;
        default:
            break;
    }
    if (hbm) {
        if (index < values.length) {
            position.set(Position.KEY_HOURS, Integer.parseInt(values[index++]));
        }
        if (index < values.length) {
            position.set(Position.KEY_BATTERY, Double.parseDouble(values[index++]));
        }
        if (index < values.length && values[index++].equals("0")) {
            position.set(Position.KEY_ARCHIVE, true);
        }
        if (includeAdc) {
            position.set(Position.PREFIX_ADC + 1, Double.parseDouble(values[index++]));
            position.set(Position.PREFIX_ADC + 2, Double.parseDouble(values[index++]));
            position.set(Position.PREFIX_ADC + 3, Double.parseDouble(values[index++]));
        }
        if (values.length - index >= 2) {
            String driverUniqueId = values[index++];
            if (values[index++].equals("1") && !driverUniqueId.isEmpty()) {
                position.set(Position.KEY_DRIVER_UNIQUE_ID, driverUniqueId);
            }
        }
        if (includeTemp) {
            for (int i = 1; i <= 3; i++) {
                String temperature = values[index++];
                String value = temperature.substring(temperature.indexOf(':') + 1);
                if (!value.isEmpty()) {
                    position.set(Position.PREFIX_TEMP + i, Double.parseDouble(value));
                }
            }
        }
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Network(org.traccar.model.Network) SimpleDateFormat(java.text.SimpleDateFormat)

Example 30 with DeviceSession

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

the class SuntechProtocolDecoder method decodeUniversal.

private Position decodeUniversal(Channel channel, SocketAddress remoteAddress, String[] values) throws ParseException {
    int index = 0;
    String type = values[index++];
    if (!type.equals("STT")) {
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, values[index++]);
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.set(Position.KEY_TYPE, type);
    int mask = Integer.parseInt(values[index++], 16);
    if (BitUtil.check(mask, 1)) {
        // model
        index += 1;
    }
    if (BitUtil.check(mask, 2)) {
        position.set(Position.KEY_VERSION_FW, values[index++]);
    }
    if (BitUtil.check(mask, 3) && values[index++].equals("0")) {
        position.set(Position.KEY_ARCHIVE, true);
    }
    if (BitUtil.check(mask, 4) && BitUtil.check(mask, 5)) {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        position.setTime(dateFormat.parse(values[index++] + values[index++]));
    }
    if (BitUtil.check(mask, 6)) {
        // cell
        index += 1;
    }
    if (BitUtil.check(mask, 7)) {
        // mcc
        index += 1;
    }
    if (BitUtil.check(mask, 8)) {
        // mnc
        index += 1;
    }
    if (BitUtil.check(mask, 9)) {
        // lac
        index += 1;
    }
    if (BitUtil.check(mask, 10)) {
        position.set(Position.KEY_RSSI, Integer.parseInt(values[index++]));
    }
    if (BitUtil.check(mask, 11)) {
        position.setLatitude(Double.parseDouble(values[index++]));
    }
    if (BitUtil.check(mask, 12)) {
        position.setLongitude(Double.parseDouble(values[index++]));
    }
    if (BitUtil.check(mask, 13)) {
        position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(values[index++])));
    }
    if (BitUtil.check(mask, 14)) {
        position.setCourse(Double.parseDouble(values[index++]));
    }
    if (BitUtil.check(mask, 15)) {
        position.set(Position.KEY_SATELLITES, Integer.parseInt(values[index++]));
    }
    if (BitUtil.check(mask, 16)) {
        position.setValid(values[index++].equals("1"));
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat)

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