@SuppressWarnings("unchecked")
  private int getRessiueBlocks(DlmsRequest req, DlmsRequest dr) {
    int offset = 0;
    List<Integer> ressiueBlocks = null;
    if (req.containsKey(DlmsUpgradeAssisant.REISSUE_LIST)) {
      ressiueBlocks = (List<Integer>) req.getAppendParam(DlmsUpgradeAssisant.REISSUE_LIST);
    } else {
      ressiueBlocks = new ArrayList<Integer>();
    }
    if (DlmsUpgradeAssisant.UPGRADE_03.equals(req.getOperator())
        || DlmsUpgradeAssisant.UPGRADE_RESSIUE.equals(req.getOperator())) {
      return offset;
    }
    ASN1BitString bitString = req.getParams()[0].resultData.getBitString();
    byte[] bitValue = bitString.getValue();
    // 获得有效位数
    int blockCount = (bitValue.length - 1) * 8 - bitValue[0];

    getRessiueBlocks(bitValue, blockCount, ressiueBlocks, offset + 1);
    if (req.containsKey(DlmsUpgradeAssisant.CURRENT_INDEX)) {
      offset = (Integer) req.getAppendParam(DlmsUpgradeAssisant.CURRENT_INDEX);
    } else {
      offset = 0;
    }
    dr.addAppendParam(DlmsUpgradeAssisant.REISSUE_LIST, ressiueBlocks);
    return offset;
  }
  public void handleUpgrade(DlmsEventProcessor processor, DlmsRequest req, DlmsContext context) {

    try {
      boolean isFinished = false; // 是否升级结束
      boolean isSuccess = true; // 命令是否成功

      DlmsRequest dr = new DlmsRequest();
      dr.setRelayParam(req.getRelayParam());
      dr.setMeterId(context.meterId);
      dr.addAllAppendParmas(req.getAllParam());
      dr.setDestAddr(req.getDestAddr());

      if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_00)) {
        isSuccess = updateStep_01_01(req, dr);
      } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_01_01)) {
        isSuccess = upgradeStep_01(req, dr);
      } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_01)) {
        isSuccess = upgradeStep_02(req, dr);
      } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_02)) {
        isSuccess = upgradeStep_03(req, dr);
      } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_03)
          || req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_RESSIUE)) {
        isSuccess = blockTransfering(req, dr);
      } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_READMAP)) {
        upgradeStep_04(req, dr);
      } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_04)) {
        if (checkBitMap(req, dr)) { // 如果没有漏点,进入第五步
          upgradeStep_05(req, dr);
        }
      } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_05)) { // 检测验证是否成功
        if (req.getParams()[0].resultCode == 2) { // 如果返回的是temporary-failure,休息一会再读
          try {
            Thread.sleep(sleepTimeStay06Step * 1000);
          } catch (InterruptedException e) {
          }
        }
        readTransferStatus(dr); // 这里无论返回什么,都去读升级状态
      } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_READ_STATUS)) {
        int verfiyFlg = req.getParams()[0].resultData.getEnum();
        if (verfiyFlg == 3) {
          upgradeStep_06(req, dr);
        } else if (verfiyFlg == 2) {
          try {
            Thread.sleep(sleepTimeStay06Step * 1000);
          } catch (InterruptedException e) {
          }
          readTransferStatus(dr);
        } else { // 除了上面两种情况,都是失败
          isSuccess = false;
        }
      } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_06)) {
        // 读到的数据与主站下发做比较
        // 如果成功,执行第7步
        {
          upgradeStep_07(req, dr);
        }
      } else if (req.getOperator().equals(DlmsUpgradeAssisant.UPGRADE_07)) {
        // 如果结束 设置finished=true
        if (req.getParams()[0].resultCode == 0) isFinished = true;
        else {
          isSuccess = false;
        }
      }

      // 查询待发送列表中,是否有要升级的请求,如果有,则当前请求判定为失败
      if (context.webReqList.size() > 0) {
        DlmsEvent evt = (DlmsEvent) context.webReqList.get(0);
        DlmsRequest request = (DlmsRequest) evt.getRequest();
        if (request.getOperator() != null && request.getOperator().contains("UPGRADE")) {
          isSuccess = false;
          log.error(
              "meterid="
                  + context.meterId
                  + ".New upgrade request is comming,current upgrade set fail");
        }
      }

      if (!isSuccess) {
        DlmsUpgradeAssisant.getInstance().updateUpgradeInfo(req, UpgradeInfo.FAIL); // 设置为失败
        DlmsUpgradeAssisant.getInstance().updateUpgradeStatus(req, "00");
        if (tracer.isEnabled())
          tracer.trace(
              "Upgrade Failed. MeterId=" + req.getMeterId() + ",Operator=" + req.getOperator());
        return;
      }
      if (!isFinished) {
        DlmsUpgradeAssisant.getInstance().onUpgradeSuccess(dr);
        processor.postWebRequest(dr, null);
      } else {
        // 收尾阶段
        req.addAppendParam(
            DlmsUpgradeAssisant.CURRENT_BLOCK_NUM,
            (Integer) req.getAppendParam(DlmsUpgradeAssisant.BLOCK_COUNT));
        DlmsUpgradeAssisant.getInstance().updateUpgradeInfo(req, UpgradeInfo.SUCCESS); // 设置为成功
        DlmsUpgradeAssisant.getInstance().updateUpgradeStatus(req, "02");
      }
    } catch (Exception e) {
      log.error(StringUtil.getExceptionDetailInfo(e));
      DlmsUpgradeAssisant.getInstance().updateUpgradeInfo(req, UpgradeInfo.FAIL); // 设置为失败
      DlmsUpgradeAssisant.getInstance().updateUpgradeStatus(req, "00");
    }
  }