List of usage examples for io.netty.buffer ByteBuf readUnsignedMedium
public abstract int readUnsignedMedium();
From source file:org.traccar.protocol.AutoFonProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; int type = buf.readUnsignedByte(); if (type == MSG_LOGIN || type == MSG_45_LOGIN) { if (type == MSG_LOGIN) { buf.readUnsignedByte(); // hardware version buf.readUnsignedByte(); // software version }/* w w w.j a v a2 s. c om*/ String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession != null && channel != null) { ByteBuf response = Unpooled.buffer(); response.writeBytes("resp_crc=".getBytes(StandardCharsets.US_ASCII)); response.writeByte(buf.getByte(buf.writerIndex() - 1)); channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } return null; } DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; } if (type == MSG_LOCATION) { return decodePosition(deviceSession, buf, false); } else if (type == MSG_HISTORY) { int count = buf.readUnsignedByte() & 0x0f; buf.readUnsignedShort(); // total count List<Position> positions = new LinkedList<>(); for (int i = 0; i < count; i++) { positions.add(decodePosition(deviceSession, buf, true)); } return positions; } else if (type == MSG_45_LOCATION) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); short status = buf.readUnsignedByte(); if (BitUtil.check(status, 7)) { position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); } position.set(Position.KEY_BATTERY, BitUtil.to(status, 7)); buf.skipBytes(2); // remaining time position.set(Position.PREFIX_TEMP + 1, buf.readByte()); buf.skipBytes(2); // timer (interval and units) buf.readByte(); // mode buf.readByte(); // gprs sending interval buf.skipBytes(6); // mcc, mnc, lac, cid int valid = buf.readUnsignedByte(); position.setValid(BitUtil.from(valid, 6) != 0); position.set(Position.KEY_SATELLITES, BitUtil.from(valid, 6)); int time = buf.readUnsignedMedium(); int date = buf.readUnsignedMedium(); DateBuilder dateBuilder = new DateBuilder().setTime(time / 10000, time / 100 % 100, time % 100) .setDateReverse(date / 10000, date / 100 % 100, date % 100); position.setTime(dateBuilder.getDate()); position.setLatitude(convertCoordinate(buf.readUnsignedByte(), buf.readUnsignedMedium())); position.setLongitude(convertCoordinate(buf.readUnsignedByte(), buf.readUnsignedMedium())); position.setSpeed(buf.readUnsignedByte()); position.setCourse(buf.readUnsignedShort()); return position; } return null; }
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); }// ww 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.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()); }// w w w .j a va 2s . c om 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.Gl200BinaryProtocolDecoder.java
License:Apache License
private List<Position> decodeLocation(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { List<Position> positions = new LinkedList<>(); int type = buf.readUnsignedByte(); buf.readUnsignedInt(); // mask buf.readUnsignedShort(); // length buf.readUnsignedByte(); // device type buf.readUnsignedShort(); // protocol version buf.readUnsignedShort(); // firmware version DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.format("%015d", buf.readLong())); if (deviceSession == null) { return null; }//from w ww . ja v a 2s. co m int battery = buf.readUnsignedByte(); int power = buf.readUnsignedShort(); if (type == MSG_RSP_GEO) { buf.readUnsignedByte(); // reserved buf.readUnsignedByte(); // reserved } buf.readUnsignedByte(); // motion status int satellites = buf.readUnsignedByte(); if (type != MSG_RSP_COMPRESSED) { buf.readUnsignedByte(); // index } if (type == MSG_RSP_LCB) { buf.readUnsignedByte(); // phone length for (int b = buf.readUnsignedByte();; b = buf.readUnsignedByte()) { if ((b & 0xf) == 0xf || (b & 0xf0) == 0xf0) { break; } } } if (type == MSG_RSP_COMPRESSED) { int count = buf.readUnsignedShort(); BitBuffer bits; int speed = 0; int heading = 0; int latitude = 0; int longitude = 0; long time = 0; for (int i = 0; i < count; i++) { if (time > 0) { time += 1; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); switch (BitUtil.from(buf.getUnsignedByte(buf.readerIndex()), 8 - 2)) { case 1: bits = new BitBuffer(buf.readSlice(3)); bits.readUnsigned(2); // point attribute bits.readUnsigned(1); // fix type speed = bits.readUnsigned(12); heading = bits.readUnsigned(9); longitude = buf.readInt(); latitude = buf.readInt(); if (time == 0) { time = buf.readUnsignedInt(); } break; case 2: bits = new BitBuffer(buf.readSlice(5)); bits.readUnsigned(2); // point attribute bits.readUnsigned(1); // fix type speed += bits.readSigned(7); heading += bits.readSigned(7); longitude += bits.readSigned(12); latitude += bits.readSigned(11); break; default: buf.readUnsignedByte(); // invalid or same continue; } position.setValid(true); position.setTime(new Date(time * 1000)); position.setSpeed(UnitsConverter.knotsFromKph(speed * 0.1)); position.setCourse(heading); position.setLongitude(longitude * 0.000001); position.setLatitude(latitude * 0.000001); positions.add(position); } } else { int count = buf.readUnsignedByte(); for (int i = 0; i < count; i++) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_BATTERY_LEVEL, battery); position.set(Position.KEY_POWER, power); position.set(Position.KEY_SATELLITES, satellites); int hdop = buf.readUnsignedByte(); position.setValid(hdop > 0); position.set(Position.KEY_HDOP, hdop); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedMedium() * 0.1)); position.setCourse(buf.readUnsignedShort()); position.setAltitude(buf.readShort()); position.setLongitude(buf.readInt() * 0.000001); position.setLatitude(buf.readInt() * 0.000001); position.setTime(decodeTime(buf)); position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort()))); buf.readUnsignedByte(); // reserved positions.add(position); } } return positions; }
From source file:org.traccar.protocol.Gl200BinaryProtocolDecoder.java
License:Apache License
private Position decodeEvent(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { Position position = new Position(getProtocolName()); int type = buf.readUnsignedByte(); buf.readUnsignedInt(); // mask buf.readUnsignedShort(); // length buf.readUnsignedByte(); // device type buf.readUnsignedShort(); // protocol version position.set(Position.KEY_VERSION_FW, String.valueOf(buf.readUnsignedShort())); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.format("%015d", buf.readLong())); if (deviceSession == null) { return null; }/*w w w . jav a2 s .co m*/ position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_BATTERY_LEVEL, buf.readUnsignedByte()); position.set(Position.KEY_POWER, buf.readUnsignedShort()); buf.readUnsignedByte(); // motion status position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); switch (type) { case MSG_EVT_BPL: buf.readUnsignedShort(); // backup battery voltage break; case MSG_EVT_VGN: case MSG_EVT_VGF: buf.readUnsignedShort(); // reserved buf.readUnsignedByte(); // report type buf.readUnsignedInt(); // ignition duration break; case MSG_EVT_UPD: buf.readUnsignedShort(); // code buf.readUnsignedByte(); // retry break; case MSG_EVT_IDF: buf.readUnsignedInt(); // idling duration break; case MSG_EVT_GSS: buf.readUnsignedByte(); // gps signal status buf.readUnsignedInt(); // reserved break; case MSG_EVT_GES: buf.readUnsignedShort(); // trigger geo id buf.readUnsignedByte(); // trigger geo enable buf.readUnsignedByte(); // trigger mode buf.readUnsignedInt(); // radius buf.readUnsignedInt(); // check interval break; case MSG_EVT_GPJ: buf.readUnsignedByte(); // cw jamming value buf.readUnsignedByte(); // gps jamming state break; case MSG_EVT_RMD: buf.readUnsignedByte(); // roaming state break; case MSG_EVT_JDS: buf.readUnsignedByte(); // jamming state break; case MSG_EVT_CRA: buf.readUnsignedByte(); // crash counter break; case MSG_EVT_UPC: buf.readUnsignedByte(); // command id buf.readUnsignedShort(); // result break; default: break; } buf.readUnsignedByte(); // count int hdop = buf.readUnsignedByte(); position.setValid(hdop > 0); position.set(Position.KEY_HDOP, hdop); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedMedium() * 0.1)); position.setCourse(buf.readUnsignedShort()); position.setAltitude(buf.readShort()); position.setLongitude(buf.readInt() * 0.000001); position.setLatitude(buf.readInt() * 0.000001); position.setTime(decodeTime(buf)); position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort()))); buf.readUnsignedByte(); // reserved return position; }
From source file:org.traccar.protocol.GlobalstarProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { FullHttpRequest request = (FullHttpRequest) msg; Document document = documentBuilder.parse(new ByteBufferBackedInputStream(request.content().nioBuffer())); NodeList nodes = (NodeList) messageExpression.evaluate(document, XPathConstants.NODESET); List<Position> positions = new LinkedList<>(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, xPath.evaluate("esn", node)); if (deviceSession != null) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setValid(true);// w ww .ja v a 2s.c om position.setTime(new Date(Long.parseLong(xPath.evaluate("unixTime", node)) * 1000)); ByteBuf buf = Unpooled .wrappedBuffer(DataConverter.parseHex(xPath.evaluate("payload", node).substring(2))); buf.readUnsignedByte(); // flags position.setLatitude(buf.readUnsignedMedium() * 90.0 / (1 << 23)); if (position.getLatitude() > 90) { position.setLatitude(position.getLatitude() - 180); } position.setLongitude(buf.readUnsignedMedium() * 90.0 / (1 << 23)); if (position.getLongitude() > 180) { position.setLongitude(position.getLongitude() - 360); } buf.readUnsignedByte(); // status buf.readUnsignedByte(); // status positions.add(position); } } sendResponse(channel, HttpResponseStatus.OK); return positions; }
From source file:org.traccar.protocol.Gt06ProtocolDecoder.java
License:Apache License
private boolean decodeLbs(Position position, ByteBuf buf, boolean hasLength) { int length = 0; if (hasLength) { length = buf.readUnsignedByte(); if (length == 0) { return false; }/*ww w. j a v a 2s . co m*/ } int mcc = buf.readUnsignedShort(); int mnc = BitUtil.check(mcc, 15) ? buf.readUnsignedShort() : buf.readUnsignedByte(); position.setNetwork(new Network( CellTower.from(BitUtil.to(mcc, 15), mnc, buf.readUnsignedShort(), buf.readUnsignedMedium()))); if (length > 9) { buf.skipBytes(length - 9); } return true; }
From source file:org.traccar.protocol.Gt06ProtocolDecoder.java
License:Apache License
private Object decodeBasicOther(Channel channel, ByteBuf buf, DeviceSession deviceSession, int type, int dataLength) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); if (type == MSG_LBS_MULTIPLE || type == MSG_LBS_EXTEND || type == MSG_LBS_WIFI || type == MSG_LBS_2 || type == MSG_WIFI_3) {//from w ww. ja va2 s .co m boolean longFormat = type == MSG_LBS_2 || type == MSG_WIFI_3; DateBuilder dateBuilder = new DateBuilder(deviceSession.getTimeZone()) .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); getLastLocation(position, dateBuilder.getDate()); int mcc = buf.readUnsignedShort(); int mnc = BitUtil.check(mcc, 15) ? buf.readUnsignedShort() : buf.readUnsignedByte(); Network network = new Network(); for (int i = 0; i < 7; i++) { int lac = longFormat ? buf.readInt() : buf.readUnsignedShort(); int cid = longFormat ? (int) buf.readLong() : buf.readUnsignedMedium(); int rssi = -buf.readUnsignedByte(); if (lac > 0) { network.addCellTower(CellTower.from(BitUtil.to(mcc, 15), mnc, lac, cid, rssi)); } } buf.readUnsignedByte(); // time leads if (type != MSG_LBS_MULTIPLE && type != MSG_LBS_2) { int wifiCount = buf.readUnsignedByte(); for (int i = 0; i < wifiCount; i++) { String mac = ByteBufUtil.hexDump(buf.readSlice(6)).replaceAll("(..)", "$1:"); network.addWifiAccessPoint( WifiAccessPoint.from(mac.substring(0, mac.length() - 1), buf.readUnsignedByte())); } } position.setNetwork(network); } else if (type == MSG_STRING) { getLastLocation(position, null); int commandLength = buf.readUnsignedByte(); if (commandLength > 0) { buf.readUnsignedByte(); // server flag (reserved) position.set(Position.KEY_RESULT, buf.readSlice(commandLength - 1).toString(StandardCharsets.US_ASCII)); } } else if (isSupported(type)) { if (hasGps(type)) { decodeGps(position, buf, false, deviceSession.getTimeZone()); } else { getLastLocation(position, null); } if (hasLbs(type)) { decodeLbs(position, buf, hasStatus(type)); } if (hasStatus(type)) { decodeStatus(position, buf); } if (type == MSG_GPS_LBS_1 && buf.readableBytes() == 2 + 6) { int mask = buf.readUnsignedShort(); position.set(Position.KEY_IGNITION, BitUtil.check(mask, 8 + 7)); position.set(Position.PREFIX_IN + 2, BitUtil.check(mask, 8 + 6)); if (BitUtil.check(mask, 8 + 4)) { int value = BitUtil.to(mask, 8 + 1); if (BitUtil.check(mask, 8 + 1)) { value = -value; } position.set(Position.PREFIX_TEMP + 1, value); } else { int value = BitUtil.to(mask, 8 + 2); if (BitUtil.check(mask, 8 + 5)) { position.set(Position.PREFIX_ADC + 1, value); } else { position.set(Position.PREFIX_ADC + 1, value * 0.1); } } } if (type == MSG_GPS_LBS_1 && buf.readableBytes() == 4 + 6) { position.set(Position.KEY_ODOMETER, buf.readUnsignedInt()); } if (type == MSG_GPS_LBS_2 && buf.readableBytes() == 3 + 6) { position.set(Position.KEY_IGNITION, buf.readUnsignedByte() > 0); position.set(Position.KEY_EVENT, buf.readUnsignedByte()); // reason position.set(Position.KEY_ARCHIVE, buf.readUnsignedByte() > 0); } } else { if (dataLength > 0) { buf.skipBytes(dataLength); } if (type != MSG_COMMAND_0 && type != MSG_COMMAND_1 && type != MSG_COMMAND_2) { sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null); } return null; } if (hasLanguage(type)) { buf.readUnsignedShort(); } if (type == MSG_GPS_LBS_STATUS_3 || type == MSG_FENCE_MULTI) { position.set(Position.KEY_GEOFENCE, buf.readUnsignedByte()); } sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null); return position; }
From source file:org.traccar.protocol.KhdProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(2); // header int type = buf.readUnsignedByte(); buf.readUnsignedShort(); // size if (type == MSG_LOGIN || type == MSG_ADMIN_NUMBER || type == MSG_SEND_TEXT || type == MSG_SMS_ALARM_SWITCH || type == MSG_POSITION_REUPLOAD) { ByteBuf response = Unpooled.buffer(); response.writeByte(0x29);/* ww w.ja va 2s.co m*/ response.writeByte(0x29); // header response.writeByte(MSG_CONFIRMATION); response.writeShort(5); // size response.writeByte(buf.getByte(buf.writerIndex() - 2)); response.writeByte(type); response.writeByte(buf.writerIndex() > 9 ? buf.getByte(9) : 0); // 10th byte response.writeByte(Checksum.xor(response.nioBuffer())); response.writeByte(0x0D); // ending if (channel != null) { channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } } if (type == MSG_ON_DEMAND || type == MSG_POSITION_UPLOAD || type == MSG_POSITION_REUPLOAD || type == MSG_ALARM || type == MSG_REPLY || type == MSG_PERIPHERAL) { Position position = new Position(getProtocolName()); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, readSerialNumber(buf)); if (deviceSession == null) { return null; } position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder().setYear(BcdUtil.readInteger(buf, 2)) .setMonth(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2)) .setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)) .setSecond(BcdUtil.readInteger(buf, 2)); position.setTime(dateBuilder.getDate()); position.setLatitude(BcdUtil.readCoordinate(buf)); position.setLongitude(BcdUtil.readCoordinate(buf)); position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4))); position.setCourse(BcdUtil.readInteger(buf, 4)); position.setValid((buf.readUnsignedByte() & 0x80) != 0); if (type != MSG_ALARM) { position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium()); position.set(Position.KEY_STATUS, buf.readUnsignedInt()); position.set(Position.KEY_HDOP, buf.readUnsignedByte()); position.set(Position.KEY_VDOP, buf.readUnsignedByte()); position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); buf.skipBytes(5); // other location data if (type == MSG_PERIPHERAL) { buf.readUnsignedShort(); // data length int dataType = buf.readUnsignedByte(); buf.readUnsignedByte(); // content length switch (dataType) { case 0x01: position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte() * 100 + buf.readUnsignedByte()); break; case 0x02: position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedByte() * 100 + buf.readUnsignedByte()); break; default: break; } } } return position; } return null; }
From source file:org.traccar.protocol.ObdDongleProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(2); // header int index = buf.readUnsignedShort(); String imei = buf.readSlice(15).toString(StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession == null) { return null; }//w w w .jav a2 s . c o m int type = buf.readUnsignedByte(); buf.readUnsignedShort(); // data length if (type == MSG_TYPE_CONNECT) { ByteBuf response = Unpooled.buffer(); response.writeByte(1); response.writeShort(0); response.writeInt(0); sendResponse(channel, MSG_TYPE_CONNACK, index, imei, response); } else if (type == MSG_TYPE_PUBLISH) { int typeMajor = buf.readUnsignedByte(); int typeMinor = buf.readUnsignedByte(); buf.readUnsignedByte(); // event id Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date(buf.readUnsignedInt() * 1000)); int flags = buf.readUnsignedByte(); position.setValid(!BitUtil.check(flags, 6)); position.set(Position.KEY_SATELLITES, BitUtil.to(flags, 4)); double longitude = ((BitUtil.to(buf.readUnsignedShort(), 1) << 24) + buf.readUnsignedMedium()) * 0.00001; position.setLongitude(BitUtil.check(flags, 5) ? longitude : -longitude); double latitude = buf.readUnsignedMedium() * 0.00001; position.setLatitude(BitUtil.check(flags, 4) ? latitude : -latitude); int speedCourse = buf.readUnsignedMedium(); position.setSpeed(UnitsConverter.knotsFromMph(BitUtil.from(speedCourse, 10) * 0.1)); position.setCourse(BitUtil.to(speedCourse, 10)); ByteBuf response = Unpooled.buffer(); response.writeByte(typeMajor); response.writeByte(typeMinor); sendResponse(channel, MSG_TYPE_PUBACK, index, imei, response); return position; } return null; }