@Override
  protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg)
      throws Exception {

    String sentence = (String) msg;

    String type = sentence.substring(3, 7);
    if (channel != null) {
      channel.write("B" + type.substring(1)); // response
    }

    if (type.equals("AP00")) {
      identify(sentence.substring(7), channel, remoteAddress);
      return null;
    }

    if (!hasDeviceId()) {
      return null;
    }

    if (type.equals("AP01") || type.equals("AP10")) {

      Parser parser = new Parser(PATTERN, sentence);
      if (!parser.matches()) {
        return null;
      }

      Position position = new Position();
      position.setProtocol(getProtocolName());
      position.setDeviceId(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());

      position.set(Event.KEY_GSM, parser.nextInt());
      position.set(Event.KEY_SATELLITES, parser.nextInt());
      position.set(Event.KEY_BATTERY, parser.nextInt());

      int acc = parser.nextInt();
      if (acc != 0) {
        position.set(Event.KEY_IGNITION, acc == 1);
      }

      position.set(Event.KEY_MCC, parser.nextInt());
      position.set(Event.KEY_MNC, parser.nextInt());
      position.set(Event.KEY_LAC, parser.nextInt());
      position.set(Event.KEY_CID, parser.nextInt());

      return position;
    }

    return null;
  }