List of usage examples for io.netty.buffer ByteBuf readUnsignedInt
public abstract long readUnsignedInt();
From source file:org.traccar.protocol.AtrackProtocolDecoder.java
License:Apache License
private void readBinaryCustomData(Position position, ByteBuf buf, String form) { CellTower cellTower = new CellTower(); String[] keys = form.substring(1).split("%"); for (String key : keys) { switch (key) { case "SA": position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); break; case "MV": position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.1); break; case "BV": position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.1); break; case "GQ": cellTower.setSignalStrength((int) buf.readUnsignedByte()); break; case "CE": cellTower.setCellId(buf.readUnsignedInt()); break; case "LC": cellTower.setLocationAreaCode(buf.readUnsignedShort()); break; case "CN": int combinedMobileCodes = (int) (buf.readUnsignedInt() % 100000); // cccnn cellTower.setMobileCountryCode(combinedMobileCodes / 100); cellTower.setMobileNetworkCode(combinedMobileCodes % 100); break; case "RL": buf.readUnsignedByte(); // rxlev break; case "PC": position.set(Position.PREFIX_COUNT + 1, buf.readUnsignedInt()); break; case "AT": position.setAltitude(buf.readUnsignedInt()); break; case "RP": position.set(Position.KEY_RPM, buf.readUnsignedShort()); break; case "GS": position.set(Position.KEY_RSSI, buf.readUnsignedByte()); break; case "DT": position.set(Position.KEY_ARCHIVE, buf.readUnsignedByte() == 1); break; case "VN": position.set(Position.KEY_VIN, readString(buf)); break; case "MF": buf.readUnsignedShort(); // mass air flow rate break; case "EL": buf.readUnsignedByte(); // engine load break; case "TR": position.set(Position.KEY_THROTTLE, buf.readUnsignedByte()); break; case "ET": position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedShort()); break; case "FL": position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte()); break; case "ML": buf.readUnsignedByte(); // mil status break; case "FC": position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedInt()); break; case "CI": readString(buf); // format string break; case "AV1": position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort()); break; case "NC": readString(buf); // gsm neighbor cell info break; case "SM": buf.readUnsignedShort(); // max speed between reports break; case "GL": readString(buf); // google link break; case "MA": readString(buf); // mac address break; case "PD": buf.readUnsignedByte(); // pending code status break; case "CD": readString(buf); // sim cid break; case "CM": buf.readLong(); // imsi break; case "GN": buf.skipBytes(60); // g sensor data break; case "GV": buf.skipBytes(6); // maximum g force break; case "ME": buf.readLong(); // imei break; case "IA": buf.readUnsignedByte(); // intake air temperature break; case "MP": buf.readUnsignedByte(); // manifold absolute pressure break; default:/* ww w . j a va 2 s . co m*/ break; } } if (cellTower.getMobileCountryCode() != null && cellTower.getMobileNetworkCode() != null && cellTower.getCellId() != null && cellTower.getCellId() != 0 && cellTower.getLocationAreaCode() != null) { position.setNetwork(new Network(cellTower)); } else if (cellTower.getSignalStrength() != null) { position.set(Position.KEY_RSSI, cellTower.getSignalStrength()); } }
From source file:org.traccar.protocol.AtrackProtocolDecoder.java
License:Apache License
private List<Position> decodeBinary(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { buf.skipBytes(2); // prefix buf.readUnsignedShort(); // checksum buf.readUnsignedShort(); // length int index = buf.readUnsignedShort(); long id = buf.readLong(); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(id)); if (deviceSession == null) { return null; }//from w w w . ja v a 2s. com sendResponse(channel, remoteAddress, id, index); List<Position> positions = new LinkedList<>(); while (buf.readableBytes() >= MIN_DATA_LENGTH) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); if (longDate) { DateBuilder dateBuilder = new DateBuilder() .setDate(buf.readUnsignedShort(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); position.setTime(dateBuilder.getDate()); buf.skipBytes(7 + 7); } else { position.setFixTime(new Date(buf.readUnsignedInt() * 1000)); position.setDeviceTime(new Date(buf.readUnsignedInt() * 1000)); buf.readUnsignedInt(); // send time } position.setValid(true); position.setLongitude(buf.readInt() * 0.000001); position.setLatitude(buf.readInt() * 0.000001); position.setCourse(buf.readUnsignedShort()); int type = buf.readUnsignedByte(); position.set(Position.KEY_TYPE, type); position.set(Position.KEY_ALARM, alarmMap.get(type)); position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 100); position.set(Position.KEY_HDOP, buf.readUnsignedShort() * 0.1); position.set(Position.KEY_INPUT, buf.readUnsignedByte()); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); position.set(Position.KEY_OUTPUT, buf.readUnsignedByte()); position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort() * 0.001); position.set(Position.KEY_DRIVER_UNIQUE_ID, readString(buf)); position.set(Position.PREFIX_TEMP + 1, buf.readShort() * 0.1); position.set(Position.PREFIX_TEMP + 2, buf.readShort() * 0.1); String message = readString(buf); if (message != null && !message.isEmpty()) { Pattern pattern = Pattern.compile("FULS:F=(\\p{XDigit}+) t=(\\p{XDigit}+) N=(\\p{XDigit}+)"); Matcher matcher = pattern.matcher(message); if (matcher.find()) { int value = Integer.parseInt(matcher.group(3), decimalFuel ? 10 : 16); position.set(Position.KEY_FUEL_LEVEL, value * 0.1); } else { position.set("message", message); } } if (custom) { String form = this.form; if (form == null) { form = readString(buf).trim().substring("%CI".length()); } readBinaryCustomData(position, buf, form); } positions.add(position); } return positions; }
From source file:org.traccar.protocol.Avl301ProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(1); // header int type = buf.readUnsignedByte(); buf.readUnsignedByte(); // length if (type == MSG_LOGIN) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, readImei(buf)); if (deviceSession == null) { sendResponse(channel, type); }/*w w w . j a v a 2 s .c o m*/ } else if (type == MSG_STATUS) { sendResponse(channel, type); } else if (type == MSG_GPS_LBS_STATUS) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); position.setTime(dateBuilder.getDate()); int gpsLength = buf.readUnsignedByte(); // gps len and sat position.set(Position.KEY_SATELLITES, gpsLength & 0xf); position.set(Position.KEY_SATELLITES_VISIBLE, buf.readUnsignedByte()); // satellites double latitude = buf.readUnsignedInt() / 600000.0; double longitude = buf.readUnsignedInt() / 600000.0; position.setSpeed(buf.readUnsignedByte()); int union = buf.readUnsignedShort(); // course and flags position.setCourse(union & 0x03FF); position.setValid((union & 0x1000) != 0); if ((union & 0x0400) != 0) { latitude = -latitude; } if ((union & 0x0800) != 0) { longitude = -longitude; } position.setLatitude(latitude); position.setLongitude(longitude); if ((union & 0x4000) != 0) { position.set("acc", (union & 0x8000) != 0); } position.setNetwork( new Network(CellTower.fromLacCid(buf.readUnsignedShort(), buf.readUnsignedMedium()))); position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); int flags = buf.readUnsignedByte(); position.set("acc", (flags & 0x2) != 0); // parse other flags position.set(Position.KEY_POWER, buf.readUnsignedByte()); position.set(Position.KEY_RSSI, buf.readUnsignedByte()); return position; } return null; }
From source file:org.traccar.protocol.CalAmpProtocolDecoder.java
License:Apache License
private Position decodePosition(DeviceSession deviceSession, int type, ByteBuf buf) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date(buf.readUnsignedInt() * 1000)); if (type != MSG_MINI_EVENT_REPORT) { buf.readUnsignedInt(); // fix time }/* w w w . j a v a 2 s .co m*/ position.setLatitude(buf.readInt() * 0.0000001); position.setLongitude(buf.readInt() * 0.0000001); if (type != MSG_MINI_EVENT_REPORT) { position.setAltitude(buf.readInt() * 0.01); position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedInt())); } position.setCourse(buf.readShort()); if (type == MSG_MINI_EVENT_REPORT) { position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); } if (type == MSG_MINI_EVENT_REPORT) { position.set(Position.KEY_SATELLITES, buf.getUnsignedByte(buf.readerIndex()) & 0xf); position.setValid((buf.readUnsignedByte() & 0x20) == 0); } else { position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); position.setValid((buf.readUnsignedByte() & 0x08) == 0); } if (type != MSG_MINI_EVENT_REPORT) { position.set("carrier", buf.readUnsignedShort()); position.set(Position.KEY_RSSI, buf.readShort()); } position.set("modem", buf.readUnsignedByte()); if (type != MSG_MINI_EVENT_REPORT) { position.set(Position.KEY_HDOP, buf.readUnsignedByte()); } int input = buf.readUnsignedByte(); position.set(Position.KEY_INPUT, input); position.set(Position.KEY_IGNITION, BitUtil.check(input, 0)); if (type != MSG_MINI_EVENT_REPORT) { position.set(Position.KEY_STATUS, buf.readUnsignedByte()); } if (type == MSG_EVENT_REPORT || type == MSG_MINI_EVENT_REPORT) { if (type != MSG_MINI_EVENT_REPORT) { buf.readUnsignedByte(); // event index } position.set(Position.KEY_EVENT, buf.readUnsignedByte()); } int accType = BitUtil.from(buf.getUnsignedByte(buf.readerIndex()), 6); int accCount = BitUtil.to(buf.readUnsignedByte(), 6); if (type != MSG_MINI_EVENT_REPORT) { position.set("append", buf.readUnsignedByte()); } if (accType == 1) { buf.readUnsignedInt(); // threshold buf.readUnsignedInt(); // mask } for (int i = 0; i < accCount; i++) { if (buf.readableBytes() >= 4) { position.set("acc" + i, buf.readUnsignedInt()); } } return position; }
From source file:org.traccar.protocol.ContinentalProtocolDecoder.java
License:Apache License
private double readCoordinate(ByteBuf buf, boolean extended) { long value = buf.readUnsignedInt(); if (extended ? (value & 0x08000000) != 0 : (value & 0x00800000) != 0) { value |= extended ? 0xF0000000 : 0xFF000000; }/*from ww w .j a va 2 s . com*/ return (int) value / (extended ? 360000.0 : 3600.0); }
From source file:org.traccar.protocol.ContinentalProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(2); // header buf.readUnsignedShort(); // length buf.readUnsignedByte(); // software version long serialNumber = buf.readUnsignedInt(); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(serialNumber)); if (deviceSession == null) { return null; }// w w w. ja v a2s .com buf.readUnsignedByte(); // product int type = buf.readUnsignedByte(); if (type == MSG_STATUS) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setFixTime(new Date(buf.readUnsignedInt() * 1000L)); boolean extended = buf.getUnsignedByte(buf.readerIndex()) != 0; position.setLatitude(readCoordinate(buf, extended)); position.setLongitude(readCoordinate(buf, extended)); position.setCourse(buf.readUnsignedShort()); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); position.setValid(buf.readUnsignedByte() > 0); position.setDeviceTime(new Date(buf.readUnsignedInt() * 1000L)); position.set(Position.KEY_EVENT, buf.readUnsignedShort()); int input = buf.readUnsignedShort(); position.set(Position.KEY_IGNITION, BitUtil.check(input, 0)); position.set(Position.KEY_INPUT, input); position.set(Position.KEY_OUTPUT, buf.readUnsignedShort()); position.set(Position.KEY_BATTERY, buf.readUnsignedByte()); position.set(Position.KEY_DEVICE_TEMP, buf.readByte()); buf.readUnsignedShort(); // reserved if (buf.readableBytes() > 4) { position.set(Position.KEY_ODOMETER, buf.readUnsignedInt()); } if (buf.readableBytes() > 4) { position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(buf.readUnsignedInt())); } return position; } return null; }
From source file:org.traccar.protocol.EelinkProtocolDecoder.java
License:Apache License
private Position decodeOld(DeviceSession deviceSession, ByteBuf buf, int type, int index) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_INDEX, index); position.setTime(new Date(buf.readUnsignedInt() * 1000)); position.setLatitude(buf.readInt() / 1800000.0); position.setLongitude(buf.readInt() / 1800000.0); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); position.setCourse(buf.readUnsignedShort()); position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedMedium()))); position.setValid((buf.readUnsignedByte() & 0x01) != 0); if (type == MSG_GPS) { if (buf.readableBytes() >= 2) { decodeStatus(position, buf.readUnsignedShort()); }/*from w w w . j av a 2 s.com*/ if (buf.readableBytes() >= 2 * 4) { position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001); position.set(Position.KEY_RSSI, buf.readUnsignedShort()); position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort()); position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort()); } } else if (type == MSG_ALARM) { position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte())); } else if (type == MSG_STATE) { int statusType = buf.readUnsignedByte(); position.set(Position.KEY_EVENT, statusType); if (statusType == 0x01 || statusType == 0x02 || statusType == 0x03) { buf.readUnsignedInt(); // device time if (buf.readableBytes() >= 2) { decodeStatus(position, buf.readUnsignedShort()); } } } return position; }
From source file:org.traccar.protocol.EelinkProtocolDecoder.java
License:Apache License
private Position decodeNew(DeviceSession deviceSession, ByteBuf buf, int type, int index) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_INDEX, index); position.setTime(new Date(buf.readUnsignedInt() * 1000)); int flags = buf.readUnsignedByte(); if (BitUtil.check(flags, 0)) { position.setLatitude(buf.readInt() / 1800000.0); position.setLongitude(buf.readInt() / 1800000.0); position.setAltitude(buf.readShort()); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); position.setCourse(buf.readUnsignedShort()); position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); } else {/*from ww w. jav a 2 s.c om*/ getLastLocation(position, position.getDeviceTime()); } if (BitUtil.check(flags, 1)) { position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedInt(), buf.readUnsignedByte()))); } if (BitUtil.check(flags, 2)) { buf.skipBytes(7); // bsid1 } if (BitUtil.check(flags, 3)) { buf.skipBytes(7); // bsid2 } if (BitUtil.check(flags, 4)) { buf.skipBytes(7); // bss0 } if (BitUtil.check(flags, 5)) { buf.skipBytes(7); // bss1 } if (BitUtil.check(flags, 6)) { buf.skipBytes(7); // bss2 } if (type == MSG_WARNING) { position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte())); } else if (type == MSG_REPORT) { buf.readUnsignedByte(); // report type } if (type == MSG_NORMAL || type == MSG_WARNING || type == MSG_REPORT) { int status = buf.readUnsignedShort(); position.setValid(BitUtil.check(status, 0)); if (BitUtil.check(status, 1)) { position.set(Position.KEY_IGNITION, BitUtil.check(status, 2)); } position.set(Position.KEY_STATUS, status); } if (type == MSG_NORMAL) { if (buf.readableBytes() >= 2) { position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001); } if (buf.readableBytes() >= 4) { position.set(Position.PREFIX_ADC + 0, buf.readUnsignedShort()); position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort()); } if (buf.readableBytes() >= 4) { position.set(Position.KEY_ODOMETER, buf.readUnsignedInt()); } if (buf.readableBytes() >= 4) { buf.readUnsignedShort(); // gsm counter buf.readUnsignedShort(); // gps counter } if (buf.readableBytes() >= 4) { position.set(Position.KEY_STEPS, buf.readUnsignedShort()); buf.readUnsignedShort(); // walking time } if (buf.readableBytes() >= 12) { position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedShort() / 256.0); position.set("humidity", buf.readUnsignedShort() * 0.1); position.set("illuminance", buf.readUnsignedInt() / 256.0); position.set("co2", buf.readUnsignedInt()); } if (buf.readableBytes() >= 2) { position.set(Position.PREFIX_TEMP + 2, buf.readShort() / 16.0); } } return position; }
From source file:org.traccar.protocol.EelinkProtocolDecoder.java
License:Apache License
private Position decodeResult(DeviceSession deviceSession, ByteBuf buf, int index) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_INDEX, index); buf.readUnsignedByte(); // type buf.readUnsignedInt(); // uid String sentence = buf.toString(StandardCharsets.UTF_8); Parser parser = new Parser(PATTERN, sentence); if (parser.matches()) { position.setValid(true);/* www . j av a 2 s .c o m*/ position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); position.setCourse(parser.nextDouble()); position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble())); position.setTime(parser.nextDateTime()); } else { getLastLocation(position, null); position.set(Position.KEY_RESULT, sentence); } return position; }
From source file:org.traccar.protocol.EelinkProtocolDecoder.java
License:Apache License
private Position decodeObd(DeviceSession deviceSession, ByteBuf buf, int index) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, new Date(buf.readUnsignedInt() * 1000)); while (buf.readableBytes() > 0) { int pid = buf.readUnsignedByte(); int value = buf.readInt(); switch (pid) { case 0x89: position.set(Position.KEY_FUEL_CONSUMPTION, value); break; case 0x8a: position.set(Position.KEY_ODOMETER, value * 1000L); break; case 0x8b: position.set(Position.KEY_FUEL_LEVEL, value / 10); break; default:// ww w. j ava2s . com break; } } return position; }