@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ChannelBuffer buf = (ChannelBuffer) msg; // Find type Integer index = ChannelBufferTools.find(buf, 0, buf.readableBytes(), ","); index = ChannelBufferTools.find(buf, index + 1, buf.readableBytes(), ","); String type = buf.toString(index + 1, 3, Charset.defaultCharset()); if (type.equals("CCC")) { return decodeBinaryMessage(channel, buf); } else { return decodeRegularMessage(channel, buf); } }
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ChannelBuffer buf = (ChannelBuffer) msg; buf.skipBytes(2); // header int type = buf.readUnsignedByte(); buf.readUnsignedShort(); // size if (type == MSG_ON_DEMAND || type == MSG_POSITION_UPLOAD || type == MSG_POSITION_REUPLOAD || type == MSG_ALARM || type == MSG_REPLY || type == MSG_PERIPHERAL) { // Create new position Position position = new Position(); position.setProtocol(getProtocolName()); // Device identification if (!identify(readSerialNumber(buf), channel)) { return null; } position.setDeviceId(getDeviceId()); // Date and time Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); time.clear(); time.set(Calendar.YEAR, 2000 + ChannelBufferTools.readHexInteger(buf, 2)); time.set(Calendar.MONTH, ChannelBufferTools.readHexInteger(buf, 2) - 1); time.set(Calendar.DAY_OF_MONTH, ChannelBufferTools.readHexInteger(buf, 2)); time.set(Calendar.HOUR_OF_DAY, ChannelBufferTools.readHexInteger(buf, 2)); time.set(Calendar.MINUTE, ChannelBufferTools.readHexInteger(buf, 2)); time.set(Calendar.SECOND, ChannelBufferTools.readHexInteger(buf, 2)); position.setTime(time.getTime()); // Location position.setLatitude(ChannelBufferTools.readCoordinate(buf)); position.setLongitude(ChannelBufferTools.readCoordinate(buf)); position.setSpeed(UnitsConverter.knotsFromKph(ChannelBufferTools.readHexInteger(buf, 4))); position.setCourse(ChannelBufferTools.readHexInteger(buf, 4)); // Flags int flags = buf.readUnsignedByte(); position.setValid((flags & 0x80) != 0); if (type == MSG_ALARM) { buf.skipBytes(2); } else { // Odometer position.set(Event.KEY_ODOMETER, buf.readUnsignedMedium()); // Status buf.skipBytes(4); // Other buf.skipBytes(8); } // TODO: parse extra data return position; } else if (type == MSG_LOGIN && channel != null) { buf.skipBytes(4); // serial number buf.readByte(); // reserved ChannelBuffer response = ChannelBuffers.dynamicBuffer(); response.writeByte(0x29); response.writeByte(0x29); // header response.writeByte(MSG_CONFIRMATION); response.writeShort(5); // size response.writeByte(buf.readUnsignedByte()); response.writeByte(type); response.writeByte(0); // reserved response.writeByte(Crc.xorChecksum(response.toByteBuffer())); response.writeByte(0x0D); // ending channel.write(response); } return null; }
private Position decodeNormalMessage( ChannelBuffer buf, Channel channel, SocketAddress remoteAddress) { Position position = new Position(); position.setProtocol(getProtocolName()); buf.readByte(); // header String id = String.valueOf(Long.parseLong(ChannelBuffers.hexDump(buf.readBytes(5)))); if (!identify(id, channel, remoteAddress)) { return null; } position.setDeviceId(getDeviceId()); int version = ChannelBufferTools.readHexInteger(buf, 1); buf.readUnsignedByte(); // type buf.readBytes(2); // length DateBuilder dateBuilder = new DateBuilder() .setDay(ChannelBufferTools.readHexInteger(buf, 2)) .setMonth(ChannelBufferTools.readHexInteger(buf, 2)) .setYear(ChannelBufferTools.readHexInteger(buf, 2)) .setHour(ChannelBufferTools.readHexInteger(buf, 2)) .setMinute(ChannelBufferTools.readHexInteger(buf, 2)) .setSecond(ChannelBufferTools.readHexInteger(buf, 2)); position.setTime(dateBuilder.getDate()); double latitude = convertCoordinate(ChannelBufferTools.readHexInteger(buf, 8)); double longitude = convertCoordinate(ChannelBufferTools.readHexInteger(buf, 9)); byte flags = buf.readByte(); position.setValid((flags & 0x1) == 0x1); if ((flags & 0x2) == 0) { latitude = -latitude; } position.setLatitude(latitude); if ((flags & 0x4) == 0) { longitude = -longitude; } position.setLongitude(longitude); position.setSpeed(ChannelBufferTools.readHexInteger(buf, 2)); position.setCourse(buf.readUnsignedByte() * 2.0); if (version == 1) { position.set(Event.KEY_SATELLITES, buf.readUnsignedByte()); position.set(Event.KEY_POWER, buf.readUnsignedByte()); buf.readByte(); // other flags and sensors position.setAltitude(buf.readUnsignedShort()); int cid = buf.readUnsignedShort(); int lac = buf.readUnsignedShort(); if (cid != 0 && lac != 0) { position.set(Event.KEY_CID, cid); position.set(Event.KEY_LAC, lac); } position.set(Event.KEY_GSM, buf.readUnsignedByte()); } else if (version == 2) { int fuel = buf.readUnsignedByte() << 8; position.set(Event.KEY_STATUS, buf.readUnsignedInt()); position.set(Event.KEY_ODOMETER, buf.readUnsignedInt()); fuel += buf.readUnsignedByte(); position.set(Event.KEY_FUEL, fuel); } return position; }
private List<Position> decodeBinaryMessage(Channel channel, ChannelBuffer buf) { List<Position> positions = new LinkedList<>(); String flag = buf.toString(2, 1, Charset.defaultCharset()); int index = ChannelBufferTools.find(buf, 0, buf.readableBytes(), ","); // Identification String imei = buf.toString(index + 1, 15, Charset.defaultCharset()); if (!identify(imei, channel)) { return null; } buf.skipBytes(index + 1 + 15 + 1 + 3 + 1 + 2 + 2 + 4); while (buf.readableBytes() >= 0x34) { Position position = new Position(); position.setProtocol(getProtocolName()); position.setDeviceId(getDeviceId()); // Event position.set(Event.KEY_EVENT, buf.readUnsignedByte()); // Location position.setLatitude(buf.readInt() * 0.000001); position.setLongitude(buf.readInt() * 0.000001); // Time (946684800 - timestamp for 2000-01-01) position.setTime(new Date((946684800 + buf.readUnsignedInt()) * 1000)); // Validity position.setValid(buf.readUnsignedByte() == 1); // Satellites position.set(Event.KEY_SATELLITES, buf.readUnsignedByte()); // GSM Signal position.set(Event.KEY_GSM, buf.readUnsignedByte()); // Speed position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); // Course position.setCourse(buf.readUnsignedShort()); // HDOP position.set(Event.KEY_HDOP, buf.readUnsignedShort() * 0.1); // Altitude position.setAltitude(buf.readUnsignedShort()); // Other position.set(Event.KEY_ODOMETER, buf.readUnsignedInt()); position.set("runtime", buf.readUnsignedInt()); position.set( Event.KEY_CELL, buf.readUnsignedShort() + "|" + buf.readUnsignedShort() + "|" + buf.readUnsignedShort() + "|" + buf.readUnsignedShort()); position.set(Event.KEY_STATUS, buf.readUnsignedShort()); // ADC position.set(Event.PREFIX_ADC + 1, buf.readUnsignedShort()); position.set(Event.KEY_BATTERY, buf.readUnsignedShort() * 0.01); position.set(Event.KEY_POWER, buf.readUnsignedShort()); buf.readUnsignedInt(); // geo-fence positions.add(position); } // Delete recorded data if (channel != null) { StringBuilder command = new StringBuilder("@@"); command.append(flag).append(27 + positions.size() / 10).append(","); command.append(imei).append(",CCC,").append(positions.size()).append("*"); int checksum = 0; for (int i = 0; i < command.length(); i += 1) checksum += command.charAt(i); command.append(String.format("%02x", checksum & 0xff).toUpperCase()); command.append("\r\n"); channel.write(command.toString()); } return positions; }
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ChannelBuffer buf = (ChannelBuffer) msg; buf.readUnsignedByte(); // protocol buf.readUnsignedShort(); // length int index = buf.readUnsignedByte() >> 3; if (channel != null) { ChannelBuffer response = ChannelBuffers.copiedBuffer("^" + index, Charset.defaultCharset()); channel.write(response, remoteAddress); } String id = String.valueOf(buf.readUnsignedInt()); if (!identify(id, channel, remoteAddress)) { return null; } List<Position> positions = new LinkedList<>(); while (buf.readableBytes() > 2) { Position position = new Position(); position.setProtocol(getProtocolName()); position.setDeviceId(getDeviceId()); int end = buf.readerIndex() + buf.readUnsignedByte(); position.setTime(new Date(buf.readUnsignedInt() * 1000)); int flags = buf.readUnsignedByte(); position.set(Event.KEY_SATELLITES, BitUtil.range(flags, 2)); position.setValid(BitUtil.range(flags, 0, 2) > 0); // Latitude double lat = buf.readUnsignedMedium(); lat = lat * -180 / 16777216 + 90; position.setLatitude(lat); // Longitude double lon = buf.readUnsignedMedium(); lon = lon * 360 / 16777216 - 180; position.setLongitude(lon); // Status flags = buf.readUnsignedByte(); position.set(Event.KEY_IGNITION, BitUtil.check(flags, 0)); position.set(Event.KEY_GSM, BitUtil.range(flags, 2, 3)); position.setCourse((BitUtil.range(flags, 5) * 45 + 180) % 360); // Speed int speed = buf.readUnsignedByte(); if (speed < 250) { position.setSpeed(UnitsConverter.knotsFromKph(speed)); } while (buf.readerIndex() < end) { int type = buf.readUnsignedByte(); int length = buf.readUnsignedByte(); if (length == 255) { length += buf.readUnsignedByte(); } switch (type) { case 2: position.set(Event.KEY_ODOMETER, buf.readUnsignedMedium()); break; case 5: position.set(Event.KEY_INPUT, buf.readUnsignedByte()); break; case 6: { int n = buf.readUnsignedByte() >> 4; if (n < 2) { position.set(Event.PREFIX_ADC + n, readSwappedFloat(buf)); } else { position.set("di" + (n - 2), readSwappedFloat(buf)); } } break; case 7: { int alarm = buf.readUnsignedByte(); buf.readUnsignedByte(); if (BitUtil.check(alarm, 5)) { position.set(Event.KEY_ALARM, BitUtil.range(alarm, 0, 4)); } } break; case 8: position.set("antihijack", buf.readUnsignedByte()); break; case 9: position.set("authorized", ChannelBufferTools.readHexString(buf, 16)); break; case 10: position.set("unauthorized", ChannelBufferTools.readHexString(buf, 16)); break; case 24: { Set<Integer> temps = new LinkedHashSet<>(); int temp = buf.readUnsignedByte(); for (int i = 3; i >= 0; i--) { int n = (temp >> (2 * i)) & 0x03; if (!temps.contains(n)) { temps.add(n); } } for (int n : temps) { position.set(Event.PREFIX_TEMP + n, buf.readUnsignedByte()); } } break; case 28: position.set("weight", buf.readUnsignedShort()); buf.readUnsignedByte(); break; case 90: position.set(Event.KEY_POWER, readSwappedFloat(buf)); break; case 101: position.set(Event.KEY_OBD_SPEED, buf.readUnsignedByte()); break; case 102: position.set(Event.KEY_RPM, buf.readUnsignedByte() * 50); break; case 107: { int fuel = buf.readUnsignedShort(); switch (fuel >> 14) { case 1: position.set(Event.KEY_FUEL, (fuel & 0x3fff) * 0.4 + "%"); break; case 2: position.set(Event.KEY_FUEL, (fuel & 0x3fff) * 0.5 + " l"); break; case 3: position.set(Event.KEY_FUEL, (fuel & 0x3fff) * -0.5 + " l"); break; } } break; case 108: position.set(Event.KEY_OBD_ODOMETER, buf.readUnsignedInt() * 5); break; case 150: position.set("door", buf.readUnsignedByte()); break; default: buf.skipBytes(length); break; } } positions.add(position); } return positions; }