Search in sources :

Example 16 with Network

use of org.traccar.model.Network in project traccar by tananaev.

the class TotemProtocolDecoder method decode3.

private boolean decode3(Position position, Parser parser) {
    if (parser.hasNext()) {
        position.set(Position.KEY_ALARM, decodeAlarm123(Short.parseShort(parser.next(), 16)));
    }
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
    position.set(Position.PREFIX_IO + 1, parser.next());
    position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.1);
    position.set(Position.KEY_POWER, parser.nextDouble(0));
    position.set(Position.PREFIX_ADC + 1, parser.next());
    position.set(Position.PREFIX_ADC + 2, parser.next());
    position.set(Position.PREFIX_TEMP + 1, parser.next());
    position.set(Position.PREFIX_TEMP + 2, parser.next());
    position.setNetwork(new Network(CellTower.fromLacCid(parser.nextHexInt(0), parser.nextHexInt(0))));
    position.setValid(parser.next().equals("A"));
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    position.setCourse(parser.nextDouble(0));
    position.setSpeed(parser.nextDouble(0));
    position.set(Position.KEY_PDOP, parser.nextDouble());
    position.set(Position.KEY_ODOMETER, parser.nextInt(0) * 1000);
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    return true;
}
Also used : Network(org.traccar.model.Network)

Example 17 with Network

use of org.traccar.model.Network in project traccar by tananaev.

the class UlbotechProtocolDecoder method decodeBinary.

private Object decodeBinary(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
    // header
    buf.readUnsignedByte();
    // version
    buf.readUnsignedByte();
    // type
    buf.readUnsignedByte();
    String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    if (deviceSession.getTimeZone() == null) {
        deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId()));
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    long seconds = buf.readUnsignedInt() & 0x7fffffffL;
    // 2000-01-01 00:00
    seconds += 946684800L;
    seconds -= deviceSession.getTimeZone().getRawOffset() / 1000;
    Date time = new Date(seconds * 1000);
    boolean hasLocation = false;
    while (buf.readableBytes() > 3) {
        int type = buf.readUnsignedByte();
        int length = type == DATA_CANBUS ? buf.readUnsignedShort() : buf.readUnsignedByte();
        switch(type) {
            case DATA_GPS:
                hasLocation = true;
                position.setValid(true);
                position.setLatitude(buf.readInt() / 1000000.0);
                position.setLongitude(buf.readInt() / 1000000.0);
                position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
                position.setCourse(buf.readUnsignedShort());
                position.set(Position.KEY_HDOP, buf.readUnsignedShort());
                break;
            case DATA_LBS:
                if (length == 11) {
                    position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedInt(), -buf.readUnsignedByte())));
                } else {
                    position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), -buf.readUnsignedByte())));
                }
                if (length > 9 && length != 11) {
                    buf.skipBytes(length - 9);
                }
                break;
            case DATA_STATUS:
                int status = buf.readUnsignedShort();
                position.set(Position.KEY_IGNITION, BitUtil.check(status, 9));
                position.set(Position.KEY_STATUS, status);
                position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedShort()));
                break;
            case DATA_ODOMETER:
                position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
                break;
            case DATA_ADC:
                decodeAdc(position, buf, length);
                break;
            case DATA_GEOFENCE:
                position.set("geofenceIn", buf.readUnsignedInt());
                position.set("geofenceAlarm", buf.readUnsignedInt());
                break;
            case DATA_OBD2:
                decodeObd(position, buf, length);
                break;
            case DATA_FUEL:
                position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedInt() / 10000.0);
                break;
            case DATA_OBD2_ALARM:
                decodeObd(position, buf, length);
                break;
            case DATA_HARSH_DRIVER:
                decodeDriverBehavior(position, buf);
                break;
            case DATA_CANBUS:
                position.set("can", ChannelBuffers.hexDump(buf.readBytes(length)));
                break;
            case DATA_J1708:
                decodeJ1708(position, buf, length);
                break;
            case DATA_VIN:
                position.set(Position.KEY_VIN, buf.readBytes(length).toString(StandardCharsets.US_ASCII));
                break;
            case DATA_RFID:
                position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readBytes(length - 1).toString(StandardCharsets.US_ASCII));
                position.set("authorized", buf.readUnsignedByte() != 0);
                break;
            case DATA_EVENT:
                position.set(Position.KEY_EVENT, buf.readUnsignedByte());
                if (length > 1) {
                    position.set("eventMask", buf.readUnsignedInt());
                }
                break;
            default:
                buf.skipBytes(length);
                break;
        }
    }
    if (!hasLocation) {
        getLastLocation(position, time);
    } else {
        position.setTime(time);
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) Date(java.util.Date)

Example 18 with Network

use of org.traccar.model.Network in project traccar by tananaev.

the class UproProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    if (buf.getByte(buf.readerIndex()) != '*') {
        throw new ParseException(null, 0);
    }
    int headerIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&');
    if (headerIndex < 0) {
        headerIndex = buf.writerIndex();
    }
    String header = buf.readBytes(headerIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII);
    Parser parser = new Parser(PATTERN_HEADER, header);
    if (!parser.matches()) {
        return null;
    }
    boolean reply = parser.next().equals("1");
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    String type = parser.next();
    String subtype = parser.next();
    if (reply && channel != null) {
        channel.write("*MG20Y" + type + subtype + "#");
    }
    while (buf.readable()) {
        // skip delimiter
        buf.readByte();
        byte dataType = buf.readByte();
        int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&');
        if (delimiterIndex < 0) {
            delimiterIndex = buf.writerIndex();
        }
        ChannelBuffer data = buf.readBytes(delimiterIndex - buf.readerIndex());
        switch(dataType) {
            case 'A':
                decodeLocation(position, data.toString(StandardCharsets.US_ASCII));
                break;
            case 'B':
                position.set(Position.KEY_STATUS, data.toString(StandardCharsets.US_ASCII));
                break;
            case 'C':
                long odometer = 0;
                while (data.readable()) {
                    odometer <<= 4;
                    odometer += data.readByte() - (byte) '0';
                }
                position.set(Position.KEY_ODOMETER, odometer * 2 * 1852 / 3600);
                break;
            case 'P':
                position.setNetwork(new Network(CellTower.from(Integer.parseInt(data.readBytes(4).toString(StandardCharsets.US_ASCII)), Integer.parseInt(data.readBytes(4).toString(StandardCharsets.US_ASCII)), Integer.parseInt(data.readBytes(4).toString(StandardCharsets.US_ASCII), 16), Integer.parseInt(data.readBytes(4).toString(StandardCharsets.US_ASCII), 16))));
                break;
            case 'Q':
                position.set("obd-pid", ChannelBuffers.hexDump(data));
                break;
            case 'R':
                position.set("odb-travel", ChannelBuffers.hexDump(data));
                break;
            case 'S':
                position.set("obd-traffic", ChannelBuffers.hexDump(data));
                break;
            default:
                break;
        }
    }
    if (position.getLatitude() != 0 && position.getLongitude() != 0) {
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) ParseException(java.text.ParseException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Parser(org.traccar.helper.Parser)

Example 19 with Network

use of org.traccar.model.Network in project traccar by tananaev.

the class TrvProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    String id = sentence.startsWith("TRV") ? sentence.substring(0, 3) : sentence.substring(0, 2);
    String type = sentence.substring(id.length(), id.length() + 4);
    if (channel != null) {
        String responseHeader = id + (char) (type.charAt(0) + 1) + type.substring(1);
        if (type.equals("AP00") && id.equals("IW")) {
            String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            channel.write(responseHeader + "," + time + ",0#");
        } else if (type.equals("AP14")) {
            channel.write(responseHeader + ",0.000,0.000#");
        } else {
            channel.write(responseHeader + "#");
        }
    }
    if (type.equals("AP00")) {
        getDeviceSession(channel, remoteAddress, sentence.substring(id.length() + type.length()));
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }
    if (type.equals("CP01")) {
        Parser parser = new Parser(PATTERN_HEATRBEAT, sentence);
        if (!parser.matches()) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, null);
        decodeCommon(position, parser);
        if (parser.hasNext(3)) {
            position.set(Position.KEY_BLOCKED, decodeOptionalValue(parser, 2));
            position.set(Position.KEY_CHARGE, decodeOptionalValue(parser, 1));
            position.set(Position.KEY_MOTION, decodeOptionalValue(parser, 1));
        }
        return position;
    } else if (type.equals("AP01") || type.equals("AP10")) {
        Parser parser = new Parser(PATTERN, sentence);
        if (!parser.matches()) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        DateBuilder dateBuilder = new DateBuilder().setDate(parser.nextInt(), parser.nextInt(), parser.nextInt());
        position.setValid(parser.next().equals("A"));
        position.setLatitude(parser.nextCoordinate());
        position.setLongitude(parser.nextCoordinate());
        position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
        dateBuilder.setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
        position.setTime(dateBuilder.getDate());
        position.setCourse(parser.nextDouble());
        decodeCommon(position, parser);
        position.setNetwork(new Network(CellTower.from(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt())));
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Network(org.traccar.model.Network) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Parser(org.traccar.helper.Parser)

Example 20 with Network

use of org.traccar.model.Network in project traccar by tananaev.

the class Tt8850ProtocolDecoder 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(true);
    position.setAccuracy(parser.nextInt(0));
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
    position.setCourse(parser.nextDouble(0));
    position.setAltitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setLatitude(parser.nextDouble(0));
    position.setTime(parser.nextDateTime());
    if (parser.hasNext(4)) {
        position.setNetwork(new Network(CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextHexInt(0), parser.nextHexInt(0))));
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) Parser(org.traccar.helper.Parser)

Aggregations

Network (org.traccar.model.Network)58 Position (org.traccar.model.Position)49 DeviceSession (org.traccar.DeviceSession)37 Parser (org.traccar.helper.Parser)25 DateBuilder (org.traccar.helper.DateBuilder)16 Date (java.util.Date)12 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)12 WifiAccessPoint (org.traccar.model.WifiAccessPoint)10 CellTower (org.traccar.model.CellTower)7 LinkedList (java.util.LinkedList)6 SimpleDateFormat (java.text.SimpleDateFormat)3 DateFormat (java.text.DateFormat)2 List (java.util.List)2 BitBuffer (org.traccar.helper.BitBuffer)2 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 Map (java.util.Map)1 TimeZone (java.util.TimeZone)1 Matcher (java.util.regex.Matcher)1 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)1