Ejemplo n.º 1
0
  /**
   * 升级第4步 检查升级包是否传输完毕,首先客户端读取image_transferred_blocks_status(image
   * transfer对象的属性3)获取各个imageBlock是否已接收的bitmap,如发现有遗漏的block,则补发相应的block,直至所有的block都被表计接收;当(image_transferred_blocks/8)>
   * ImageBlockSize时,客户端需采用选择性读(选择性参数=entry_descriptor)来实现分组读取所有image_transferred_blocks的bitmap,如ImageBlockSize=100,image_transferred_blocks=1800,则客户端需要分三次来读取bitmap(第一次的entry_descriptor中From_entry=0,to_entry=99;第二次entry_descriptor中From_entry=100,to_entry=199;第三次entry_descriptor中From_entry=200,to_entry=00。Entry_descriptor的使用详见《DLMS/COSEM应用层标准》);
   *
   * @param req
   * @param dr
   */
  private void upgradeStep_04(DlmsRequest req, DlmsRequest dr) {
    setRequestParam(dr, new ObisDescription(18, "0.0.44.0.0.255", 3));
    dr.setOpType(DLMS_OP_TYPE.OP_GET);
    dr.removeAppendParam(DlmsUpgradeAssisant.CURRENT_RESSIUE_NUM);
    int i_maxSize = (Integer) dr.getAppendParam(DlmsUpgradeAssisant.MAX_SIZE);
    int blockCount = (Integer) dr.getAppendParam(DlmsUpgradeAssisant.BLOCK_COUNT);

    if (blockCount / 8
        > i_maxSize) { // 按照新的标准:如果(image_transferred_blocks/8)> ImageBlockSize,选择性读,否则,直接读

      int fromIndex = getRessiueBlocks(req, dr);

      int nextIndex = fromIndex + i_maxSize - 1; // 假如读3次的话[0,i_maxSize-1],[i_maxSize,199]
      if (nextIndex > (blockCount / 8)) { // 最后一次读,读完之后,转移到第4步
        nextIndex = 0;
        dr.setOperator(DlmsUpgradeAssisant.UPGRADE_04);
      } else { // 继续读
        dr.setOperator(DlmsUpgradeAssisant.UPGRADE_READMAP);
        dr.addAppendParam(DlmsUpgradeAssisant.CURRENT_INDEX, nextIndex + 1);
      }
      // 分多次读
      SelectiveAccessDescriptor sad = new SelectiveAccessDescriptor();
      sad.selectByIndex(fromIndex, nextIndex);
      dr.getParams()[0].accessSelector = 2;
      dr.getParams()[0].data.assignValue(sad.getParameter());
    } else { // 直接读,读完之后转移到第4步
      dr.setOperator(DlmsUpgradeAssisant.UPGRADE_04);
    }
  }
Ejemplo n.º 2
0
  /**
   * 升级第一步
   * 从表计获取表支持的ImageBlockSize(即表计支持每帧接收的升级包数据块的字节数,表计根据具体的通信信道给出合适的字节数),ImageBlockSize可通过读取image
   * transfer的属性2获取;
   *
   * @param req
   * @param dr
   * @return
   */
  private boolean upgradeStep_01(DlmsRequest req, DlmsRequest dr) {
    if (req.getParams()[0].resultCode != 0) return false;

    dr.setOpType(DLMS_OP_TYPE.OP_GET);
    dr.setOperator(DlmsUpgradeAssisant.UPGRADE_01);
    setRequestParam(dr, new ObisDescription(18, "0.0.44.0.0.255", 2)); // 读取一次发送的最大字节数

    return true;
  }
Ejemplo n.º 3
0
  public void readAlarm(
      DlmsEventProcessor processor,
      DlmsObisItem[] params,
      DlmsRequest req,
      DlmsContext context,
      String dateTime) {

    boolean isIranTime = DlmsConstant.getInstance().isIranTime;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    if (isIranTime && !isOldProtocol(context.subProtocol)) { // 伊朗老表上报时间是公历,这里就不要将伊朗历转为公历了。
      dateTime = DateConvert.iranToGregorian(dateTime);
    }
    Date occurTime = null;
    try {
      occurTime = sdf.parse(dateTime);
      req.addAppendParam("EventOccurTime", occurTime);
      req.addAppendParam("OBIS", params[0].obisString);
    } catch (ParseException e) {
    }
    req.setOperator("EVENT_READING_AUTO");
    req.setOpType(DLMS_OP_TYPE.OP_GET);
    params[0].accessSelector = 1;
    params[0].classId = 7;
    params[0].attributeId = 2;
    req.setMeterId(context.meterId);
    SelectiveAccessDescriptor sad = new SelectiveAccessDescriptor();
    String fromTime = sdf.format(occurTime.getTime() - 1 * 1000 * 60);
    String now = sdf.format(occurTime.getTime() + 1 * 1000 * 60);
    DlmsAlarmStatus alarmStatus =
        DlmsAlarmManager.getInstance().getAlarmStatus(context.meterId, params[0].obisString);
    if (alarmStatus != null) {
      // -------------------包含最大读事件间隔--------------------------------------------------
      //			Calendar rc = Calendar.getInstance();
      //			rc.setTime(alarmStatus.getLastReportTime());
      //			Calendar fc = Calendar.getInstance();
      //			fc.setTime(occurTime);
      //			rc.add(Calendar.HOUR, DlmsConstant.getInstance().alarmTimeReadInterval);
      //			if(!rc.before(fc)){//读事件的间隔不能太长。
      //				fromTime=sdf.format(alarmStatus.getLastReportTime());
      //			}
      // -------------------不包含最大读事件间隔------------------------------------------------
      Calendar fc = Calendar.getInstance();
      fc.setTime(occurTime);
      fromTime = sdf.format(alarmStatus.getLastReportTime());
    }
    // 事件上报的帧里,含有时间,可以利用这个时间进行抄读
    fromTime = isIranTime ? DateConvert.gregorianToIran(fromTime) : fromTime;
    now = isIranTime ? DateConvert.gregorianToIran(now) : now;
    sad.selectByPeriodOfTime(fromTime, now, isOldProtocol(context.subProtocol));
    params[0].data.assignValue(sad.getParameter());
    req.setSubprotocol(context.subProtocol);
    req.setParams(params);
    processor.postWebRequest(req, null);
  }
Ejemplo n.º 4
0
 /**
  * 升级使能
  *
  * @param req
  */
 public void upgradeEnable(DlmsRequest req) {
   // TODO:存储升级信息 状态为正在升级
   req.setOperator(DlmsUpgradeAssisant.UPGRADE_00);
   req.setOpType(DLMS_OP_TYPE.OP_SET);
   DlmsObisItem param = req.getParams()[0];
   param.classId = 18;
   param.attributeId = 5;
   param.obisString = "0.0.44.0.0.255";
   DlmsData data = new DlmsData();
   data.setBool(true);
   param.data = data;
 }
Ejemplo n.º 5
0
  /**
   * 写升级模式.目前要求至少支持两种模式:0x01升级模式一为针对载波、RF等通信方式,imageBlockSize固定为64字节;0x02升级模式二为针对RS485、GPRS等通信方式,imageBlockSize固定为192字节。
   *
   * @param req
   * @param dr
   * @return
   */
  private boolean updateStep_01_01(DlmsRequest req, DlmsRequest dr) {

    if (req.getParams()[0].resultCode != 0) return false;

    dr.setOpType(DLMS_OP_TYPE.OP_SET);
    dr.setOperator(DlmsUpgradeAssisant.UPGRADE_01_01);
    DlmsData data = new DlmsData();
    data.setUnsigned(0x02);
    setRequestParam(dr, new ObisDescription(1, "0.0.44.128.0.255", 2));
    dr.getParams()[0].data = data;

    return true;
  }
Ejemplo n.º 6
0
  /**
   * DLMS升级第三步 传输升级包文件,客户端按照Step 1中获取的imageBlockSize分帧传送升级包文件,升级包的传输通过action操作image
   * transfer对象的方法2完成;
   *
   * @param req
   * @param dr
   */
  private boolean upgradeStep_03(DlmsRequest req, DlmsRequest dr) {

    if (req.getParams()[0].resultCode != 0) return false;

    dr.setOperator(DlmsUpgradeAssisant.UPGRADE_03);
    dr.setOpType(DLMS_OP_TYPE.OP_ACTION);
    int i_maxSize = (Integer) dr.getAppendParam(DlmsUpgradeAssisant.MAX_SIZE);
    byte[] content = (byte[]) dr.getAppendParam(DlmsUpgradeAssisant.UPGRADE_CONTENT);
    int currentBlockNum = 0;
    if (!dr.containsKey(DlmsUpgradeAssisant.CURRENT_BLOCK_NUM)) currentBlockNum = 0;
    else currentBlockNum = (Integer) dr.getAppendParam(DlmsUpgradeAssisant.CURRENT_BLOCK_NUM) + 1;
    dr.addAppendParam(DlmsUpgradeAssisant.CURRENT_BLOCK_NUM, currentBlockNum);
    if (tracer.isEnabled()) {
      tracer.trace(
          "MeterId:"
              + dr.getMeterId()
              + ",BlockCount:"
              + (Integer) dr.getAppendParam(DlmsUpgradeAssisant.BLOCK_COUNT)
              + ",CurrentBlockNum:"
              + currentBlockNum);
    }

    if (currentBlockNum == (Integer) dr.getAppendParam(DlmsUpgradeAssisant.BLOCK_COUNT)
        || (content.length % i_maxSize == 0
            && currentBlockNum + 1
                == (Integer) dr.getAppendParam(DlmsUpgradeAssisant.BLOCK_COUNT))) {
      // 当前发送是最后一帧
      dr.addAppendParam(DlmsUpgradeAssisant.IS_TRANSFER_FINISHED, true);
    }
    byte[] currentSendBlock = getCurUpgradeBlock(content, currentBlockNum, i_maxSize);
    String logicAddr = req.getMeterId();
    int tn = 0;
    if (req.getRelayParam() != null) {
      logicAddr = req.getRelayParam().getDcLogicalAddress();
      tn = req.getRelayParam().getMeasurePoint();
    }
    if (tracer.isEnabled())
      tracer.trace(
          "meterId:"
              + logicAddr
              + ",tn:"
              + tn
              + ",currentBlockNum:"
              + currentBlockNum
              + ",blockContent:"
              + HexDump.toHex(currentSendBlock));
    blockTransferRequest(dr, currentBlockNum, currentSendBlock);
    return true;
  }
Ejemplo n.º 7
0
 /**
  * 软件升级断点续传初始化
  *
  * @param req
  */
 public void upgradeReissueInit(DlmsRequest req) {
   // 软件升级补发
   req.setOperator(DlmsUpgradeAssisant.UPGRADE_03);
   byte[] content = DlmsUpgradeAssisant.getInstance().getFtpFileContent(req);
   Integer currentBlockNum = (Integer) req.getAppendParam(DlmsUpgradeAssisant.CURRENT_BLOCK_NUM);
   Integer i_maxSize = (Integer) req.getAppendParam(DlmsUpgradeAssisant.MAX_SIZE);
   byte[] currentBlock =
       DlmsUpgradeHandler.getInstance().getCurUpgradeBlock(content, currentBlockNum, i_maxSize);
   int blockCount = content.length / i_maxSize;
   req.addAppendParam(DlmsUpgradeAssisant.BLOCK_COUNT, blockCount);
   if (currentBlockNum == blockCount
       || content.length % i_maxSize == 0 && currentBlockNum - 1 == blockCount) {
     req.addAppendParam(DlmsUpgradeAssisant.IS_TRANSFER_FINISHED, true);
   }
   DlmsUpgradeHandler.getInstance().blockTransferRequest(req, currentBlockNum, currentBlock);
   req.addAppendParam(DlmsUpgradeAssisant.UPGRADE_CONTENT, content);
   req.setOpType(DLMS_OP_TYPE.OP_ACTION);
 }
Ejemplo n.º 8
0
  /**
   * 补发漏点块
   *
   * @param req
   * @param dr
   */
  @SuppressWarnings("unchecked")
  private void reissueBlock(DlmsRequest dr) {
    dr.setOperator(DlmsUpgradeAssisant.UPGRADE_RESSIUE);
    dr.setOpType(DLMS_OP_TYPE.OP_ACTION);
    int i_maxSize = (Integer) dr.getAppendParam(DlmsUpgradeAssisant.MAX_SIZE);
    byte[] content = (byte[]) dr.getAppendParam(DlmsUpgradeAssisant.UPGRADE_CONTENT);

    // 获得补发列表
    List<Integer> reissueList = (List<Integer>) dr.getAppendParam(DlmsUpgradeAssisant.REISSUE_LIST);

    // 从列表头开始发送
    Integer reissueBlockNum = reissueList.remove(0);
    dr.addAppendParam(DlmsUpgradeAssisant.CURRENT_RESSIUE_NUM, reissueBlockNum);
    dr.addAppendParam(DlmsUpgradeAssisant.REISSUE_LIST, reissueList);
    // 如果是最后一个
    byte[] reissueBlock = getCurUpgradeBlock(content, reissueBlockNum, i_maxSize);
    if (reissueList.size() == 0) {
      dr.addAppendParam(DlmsUpgradeAssisant.IS_TRANSFER_FINISHED, true);
    }
    blockTransferRequest(dr, reissueBlockNum, reissueBlock);
  }
Ejemplo n.º 9
0
  /**
   * 升级初始化
   *
   * @param req
   * @param dr
   * @param i_maxSize
   */
  private void upgradeInit(DlmsRequest req, DlmsRequest dr, int i_maxSize) {
    dr.removeAppendParam(DlmsUpgradeAssisant.CURRENT_BLOCK_NUM);
    dr.addAppendParam(DlmsUpgradeAssisant.MAX_SIZE, i_maxSize);
    dr.setOpType(DLMS_OP_TYPE.OP_ACTION);
    setRequestParam(dr, new ObisDescription(18, "0.0.44.0.0.255", 1)); // 初始化
    DlmsData data = new DlmsData();
    DlmsData[] datas = new DlmsData[2];
    byte[] content = DlmsUpgradeAssisant.getInstance().getFtpFileContent(req);
    dr.addAppendParam(DlmsUpgradeAssisant.UPGRADE_CONTENT, content);
    dr.addAppendParam(DlmsUpgradeAssisant.BLOCK_COUNT, content.length / i_maxSize); // 块数

    byte[] fileHead = fileHeadConstruct(req);
    dr.addAppendParam(DlmsUpgradeAssisant.FILE_HEAD, HexDump.toHex(fileHead));
    datas[0] = new DlmsData(); // image_identifier
    datas[0].setOctetString(fileHead);
    datas[1] = new DlmsData(); // image_szie
    datas[1].setDoubleLongUnsigned(content.length);
    ASN1SequenceOf struct = new ASN1SequenceOf(datas);
    data.setStructure(struct);
    dr.getParams()[0].data = data;
    dr.setOperator(DlmsUpgradeAssisant.UPGRADE_02);
  }
Ejemplo n.º 10
0
  /**
   * step7 写入执行时间 新版本软件生效,通过action操作image transfer对象的方法4激活之前步骤中传输的升级包文件。 软件升级时间的格式为array[1] {
   * structure include { time; octet-string[4] date; octet-string[5] } }
   *
   * @param req
   * @param dr
   */
  private void upgradeStep_07(DlmsRequest req, DlmsRequest dr) {
    // 写执行时间
    String strEffcDate = (String) dr.getAppendParam(DlmsUpgradeAssisant.EFFECTIVE_TIME);
    String[] dateTime = strEffcDate.split(" ");
    DlmsData[] effcDate = new DlmsData[2];
    effcDate[0] = new DlmsData();
    effcDate[0].setDlmsTime(dateTime[1]);
    effcDate[1] = new DlmsData();
    effcDate[1].setDlmsDate(dateTime[0]);
    DlmsData[] array = new DlmsData[] {new DlmsData()};
    ASN1SequenceOf struct = new ASN1SequenceOf(effcDate);
    array[0].setStructure(struct);
    DlmsData data = new DlmsData();
    try {
      data.setArray(array);
    } catch (IOException e) {
      log.error(StringUtil.getExceptionDetailInfo(e));
    }

    setRequestParam(dr, new ObisDescription(22, "0.0.15.0.2.255", 4));
    dr.getParams()[0].data = data;
    dr.setOpType(DLMS_OP_TYPE.OP_SET);
    dr.setOperator(DlmsUpgradeAssisant.UPGRADE_07);
  }
Ejemplo n.º 11
0
  /**
   * 收到请求,升级处理
   *
   * @param req
   * @return
   */
  public boolean upgradeProcesser(DlmsRequest req) {
    String logicAddress = "";
    int measurePoint = 0;
    if (req.getRelayParam() != null) {
      RelayParam relayParam = req.getRelayParam();
      logicAddress = relayParam.getDcLogicalAddress();
      measurePoint = relayParam.getMeasurePoint();
    } else {
      logicAddress = req.getMeterId();
      measurePoint = 0;
    }
    try {

      List<UpgradeInfo> infos =
          dbService.getUpgradeInfo(
              new Long((String) req.getAppendParam(DlmsUpgradeAssisant.UPGRADE_ID)));
      if (infos.size() != 1) {
        log.error(
            "Upgrade Request Fail:Info.size="
                + infos.size()
                + ".MeterId="
                + logicAddress
                + ".Tn="
                + measurePoint);
        if (tracer.isEnabled())
          tracer.trace(
              "Upgrade Request Fail:Info.size="
                  + infos.size()
                  + ".MeterId="
                  + logicAddress
                  + ".Tn="
                  + measurePoint);
      }
      req.addAppendParam(DlmsUpgradeAssisant.UPGRADE_ID, infos.get(0).getSoftUpgradeID());

      UpgradeInfo info = infos.get(0);
      int status = info.getStatus();
      DlmsUpgradeAssisant.getInstance().addRequestAppendParams(info, req);

      String fileHead = (String) req.getAppendParam(DlmsUpgradeAssisant.FILE_HEAD);
      String fileType = fileHead.substring(0, 2);

      // 如果是给模块升级,设置dstAddr为  0x0070
      // 根据地址文件头信息获得升级状态
      // req.setDestAddr(0x0070);

      if ("02".equals(fileType)) { // 判断是否是对模块升级
        req.setDestAddr(DlmsMessage.DstAddrToModule);
      }

      boolean isRessiueSuccess = true;
      switch (status) {
        case UpgradeInfo.WAIT_UPGRADE:
          upgradeEnable(req);
          break;
        case UpgradeInfo.WAIT_UPGARDEINIT:
          upgradeInit(req, req, (Integer) req.getAppendParam(DlmsUpgradeAssisant.MAX_SIZE));
          break;
        case UpgradeInfo.UPGRADE_PAUSE:
          upgradeReissueInit(req);
          break;
        case UpgradeInfo.RESSIUE_PAUSE:
          req.addAppendParam(DlmsUpgradeAssisant.IS_TRANSFER_FINISHED, false);
          reissueBlock(req);
          break;
        case UpgradeInfo.CHECK_MAP_FAIL:
          req.setOperator(DlmsUpgradeAssisant.UPGRADE_RESSIUE);
          upgradeStep_04(req, req);
          break;
        case UpgradeInfo.READ_STATUS_FAIL:
          readTransferStatus(req);
          break;
        case UpgradeInfo.VERFIY_FILE_FAIL:
          upgradeStep_05(req, req);
          break;
        case UpgradeInfo.CHECK_FILE_FAIL:
          upgradeStep_06(req, req);
          break;
        case UpgradeInfo.SET_EFFECTTIME_FAIL:
          upgradeStep_07(req, req);
          break;
        default:
          isRessiueSuccess = false;
          log.error(
              "Upgrade File.Status:"
                  + status
                  + " .MeterId="
                  + logicAddress
                  + " .Tn="
                  + measurePoint);
          break;
      }
      if (isRessiueSuccess) {
        DlmsUpgradeAssisant.getInstance().updateUpgradeStatus(req, "01");
        return true;
      }
      return false;
    } catch (Exception e) {
      // 设置升级失败
      log.error("update stoped.", e);
      DlmsUpgradeAssisant.getInstance().updateUpgradeInfo(req, UpgradeInfo.FAIL); // 设置为失败
      DlmsUpgradeAssisant.getInstance().updateUpgradeStatus(req, "00");
      return false;
    }
  }
Ejemplo n.º 12
0
 /**
  * 第五步:验证升级包文件,客户端通过action操作image
  * transfer对象的方法3来触发表计对升级包文件进行验证,表计回复的结果可能为successs、temporary-failure或other-reason,如结果为other-reason则终止升级过程;如结果为success则表明验证成功,进入下一步;如结果为temporary-failure则延时片刻后获取image_transfer_status(读image
  * transfer对象的属性6),状态为image verification successful时表明验证成功,状态为image verification
  * failed时表明验证失败,状态为image verification
  * initiated时表明表计正在验证,需延时片刻再读取image_transfer_status;验证过程中表计需要计算升级包的散列值,并与image_identifier中散列值校验码或解密散列校验码密文进行比对,当不一致时,在步骤
  * image_verify时表计回复 other-reason表示升级包验证失败
  *
  * @param req
  * @param dr
  */
 private void upgradeStep_05(DlmsRequest req, DlmsRequest dr) {
   setRequestParam(dr, new ObisDescription(18, "0.0.44.0.0.255", 3));
   dr.setOpType(DLMS_OP_TYPE.OP_ACTION);
   dr.setOperator(DlmsUpgradeAssisant.UPGRADE_05);
 }
Ejemplo n.º 13
0
 /**
  * 读升级状态
  *
  * @param dr
  */
 private void readTransferStatus(DlmsRequest dr) {
   setRequestParam(dr, new ObisDescription(18, "0.0.44.0.0.255", 6));
   dr.setOpType(DLMS_OP_TYPE.OP_GET);
   dr.setOperator(DlmsUpgradeAssisant.UPGRADE_READ_STATUS);
 }