Ejemplo n.º 1
0
  @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;
  }
Ejemplo n.º 2
0
  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;
  }