Пример #1
0
  @Override
  public void collectUpsStatus(String ipAaddress, Integer index, PeDevice peDevice) {

    SNMPUtil snmpUtil = new SNMPUtil();
    // 获取UPS采集的数据
    Map<String, String> resultMap = new HashMap<String, String>();
    try {
      resultMap = snmpUtil.walkByGetNext(ipAaddress, "1.3.6.1.4.1.34651.2.1.1");
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    if (resultMap.size() > 0) {
      String lastCollectIntervalStr =
          resultMap.get(UpsOidInfo.lastCollectTnterval.getOid() + "." + index);
      if (lastCollectIntervalStr != null) {
        Long lastCollectInterval = Long.parseLong(lastCollectIntervalStr.trim());
        UpsStatus upsStatus = new UpsStatus();
        if (lastCollectInterval < MAX_INTERVAL && lastCollectInterval >= 0) {

          upsStatus.setName(resultMap.get(UpsOidInfo.upsName.getOid() + "." + index));
          upsStatus.setInterfaceType(
              Integer.parseInt(
                  resultMap.get(UpsOidInfo.interfaceType.getOid() + "." + index).trim()));
          upsStatus.setCommunicationStatus(
              Integer.parseInt(
                  resultMap.get(UpsOidInfo.communicationStatus.getOid() + "." + index).trim()));
          upsStatus.setDischargePatterns(
              Integer.parseInt(
                  resultMap.get(UpsOidInfo.dischargePatterns.getOid() + "." + index).trim()));
          upsStatus.setUpsType(
              Integer.parseInt(resultMap.get(UpsOidInfo.upsType.getOid() + "." + index).trim()));
          upsStatus.setModelNumber(resultMap.get(UpsOidInfo.modelNumber));
          upsStatus.setBrand(resultMap.get(UpsOidInfo.brand));
          upsStatus.setVersionNumber(resultMap.get(UpsOidInfo.versionNumber));
          upsStatus.setRateVoltage(
              Double.parseDouble(
                      resultMap.get(UpsOidInfo.ratedVoltage.getOid() + "." + index).trim())
                  / 10);
          upsStatus.setRatedCurrent(
              Double.parseDouble(
                      resultMap.get(UpsOidInfo.ratedCurrent.getOid() + "." + index).trim())
                  / 10);
          upsStatus.setBatteryVoltage(
              Double.parseDouble(
                      resultMap.get(UpsOidInfo.batteryVoltage.getOid() + "." + index).trim())
                  / 10);
          upsStatus.setRatedFrequency(
              Double.parseDouble(
                      resultMap.get(UpsOidInfo.ratedFrequency.getOid() + "." + index).trim())
                  / 10);
          upsStatus.setPower(
              Double.parseDouble(resultMap.get(UpsOidInfo.power.getOid() + "." + index).trim())
                  / 10);

          Integer upsStatusResult =
              Integer.parseInt(resultMap.get(UpsOidInfo.upsStatus.getOid() + "." + index).trim());
          if (upsStatus.getRateVoltage() == 220) {
            byte singlePhaseByte = (byte) (upsStatusResult & 0xff); // 最低位
            // 单项电低8位
            String singlePhase = ConverToBitUtils.byteToBit(singlePhaseByte);
            upsStatus.setCityVoltageStatus(singlePhase.substring(0, 1));
            upsStatus.setBatteryVoltageStatus(singlePhase.substring(1, 2));
            upsStatus.setRunningStatus(singlePhase.substring(2, 3));
            upsStatus.setUpsStatus(singlePhase.substring(3, 4));
            upsStatus.setPatternsStatus(singlePhase.substring(4, 5));
            upsStatus.setTestStatus(singlePhase.substring(5, 6));
            upsStatus.setShutdownStatus(singlePhase.substring(6, 7));
            upsStatus.setBuzzerStatus(singlePhase.substring(7, 8));

          } else {
            upsStatus.setUpsStatus(
                resultMap.get(UpsOidInfo.upsStatus.getOid() + "." + index).trim());
          }
          upsStatus.setFrequency(
              Double.parseDouble(resultMap.get(UpsOidInfo.frequency.getOid() + "." + index).trim())
                  / 10);
          upsStatus.setInternalTemperature(
              Double.parseDouble(
                      resultMap.get(UpsOidInfo.internalTemperature.getOid() + "." + index).trim())
                  / 10);
          String byPassVoltage = null;
          String byPassVolage_v =
              resultMap.get(UpsOidInfo.bypassVoltage.getOid() + "." + index).trim();
          if (null != byPassVolage_v) {
            long v =
                Long.parseLong(
                    resultMap.get(UpsOidInfo.bypassVoltage.getOid() + "." + index).trim());
            if (v >= 0) {
              int a = (int) v & 0xffff;
              int b = (int) (v >> 16) & 0xffff;
              int c = (int) (v >> 32) & 0xffff;
              byPassVoltage =
                  ((double) a / 10 + "V/" + (double) b / 10 + "V/" + (double) c / 10 + "V");
            }
          }
          upsStatus.setBypassVoltage(byPassVoltage);
          upsStatus.setBypassFrequency(
              Double.parseDouble(
                      resultMap.get(UpsOidInfo.bypassFrequency.getOid() + "." + index).trim())
                  / 10);

          String inputVoltage_v =
              resultMap.get(UpsOidInfo.inputVoltage.getOid() + "." + index).trim();
          String inputVoltage = null;
          if (null != inputVoltage_v) {
            long v = Long.parseLong(inputVoltage_v.trim().trim());
            int a = (int) v & 0xffff;
            int b = (int) (v >> 16) & 0xffff;
            int c = (int) (v >> 32) & 0xffff;
            inputVoltage =
                ((double) a / 10 + "V/" + (double) b / 10 + "V/" + (double) c / 10 + "V");
          }

          upsStatus.setInputVoltage(inputVoltage);
          String outputVoltage_v =
              resultMap.get(UpsOidInfo.outputVoltage.getOid() + "." + index).trim();
          String outputVoltage = null;
          if (null != inputVoltage_v) {
            long v = Long.parseLong(outputVoltage_v.trim().trim());
            int a = (int) v & 0xffff;
            int b = (int) (v >> 16) & 0xffff;
            int c = (int) (v >> 32) & 0xffff;
            outputVoltage =
                ((double) a / 10 + "V/" + (double) b / 10 + "V/" + (double) c / 10 + "V");
          }
          upsStatus.setOutputVoltage(outputVoltage);
          upsStatus.setErrorVoltage(
              Double.parseDouble(
                      resultMap.get(UpsOidInfo.errorVoltage.getOid() + "." + index).trim())
                  / 10);
          String load_v = resultMap.get(UpsOidInfo.load.getOid() + "." + index).trim();
          String load = null;
          if (null != load_v) {
            long v = Long.parseLong(load_v.trim().trim());
            int a = (int) v & 0xffff;
            int b = (int) (v >> 16) & 0xffff;
            int c = (int) (v >> 32) & 0xffff;
            load = ((double) a / 10 + "%/" + (double) b / 10 + "%/" + (double) c / 10 + "%");
          }
          upsStatus.setUpsLoad(load);
          upsStatus.setOutputFrenquency(
              Double.parseDouble(
                      resultMap.get(UpsOidInfo.outputFrenquency.getOid() + "." + index).trim())
                  / 10);
          upsStatus.setSingleVoltage(
              Double.parseDouble(
                      resultMap.get(UpsOidInfo.singleVoltage.getOid() + "." + index).trim())
                  / 100);
          upsStatus.setTotalVoltage(
              Double.parseDouble(
                      resultMap.get(UpsOidInfo.totalVoltage.getOid() + "." + index).trim())
                  / 10);
          upsStatus.setElectricQuantity(
              Double.parseDouble(
                  resultMap.get(UpsOidInfo.electricQuantity.getOid() + "." + index).trim()));
          upsStatus.setPassCurrent(
              Double.parseDouble(
                      resultMap.get(UpsOidInfo.passCurrent.getOid() + "." + index).trim())
                  / 10);
          upsStatus.setRemainingTime(
              Integer.parseInt(
                  resultMap.get(UpsOidInfo.remainingTime.getOid() + "." + index).trim()));
          // testStatus为1表示没有放电
          upsStatus.setDischargeStatus(
              Integer.parseInt(
                  resultMap.get(UpsOidInfo.dischargeTest.getOid() + "." + index).trim()));
          UpsStatus newUpsStatus = findNewData(peDevice.getId());
          upsStatus.setCollectTime(DateUtils.parseDate(resultMap.get("collectTime")));
          // 如果采集到的当前ups处于放电中,并且上次采集的数据没有处于放电中,进行放电统计数据的保存
          if (newUpsStatus != null) {
            if (upsStatus.getDischargeStatus() != 0 && upsStatus.getUpsType() != 2) {

              DischargeStatistic dischargeStatistic = new DischargeStatistic();
              if (newUpsStatus.getDischargeStatus() == 0) {
                dischargeStatistic.setBatch(System.currentTimeMillis());
              } else {
                DischargeStatistic d =
                    dischargeStatisticDao.findBeginDateForNewData(peDevice.getId(), null, "desc");
                if (d != null) {
                  dischargeStatistic.setBatch(d.getBatch());
                } else {
                  dischargeStatistic.setBatch(System.currentTimeMillis());
                }
              }
              dischargeStatistic.setCollectTime(upsStatus.getCollectTime());
              // 放电开始的容量
              dischargeStatistic.setCapacity(upsStatus.getElectricQuantity());
              dischargeStatistic.setDeviceId(peDevice.getId());
              dischargeStatistic.setDeviceLoad(
                  Double.parseDouble(upsStatus.getUpsLoad().split("%/")[0]));

              dischargeStatisticDao.save(dischargeStatistic);
            }
            if (upsStatus.getRemainingTime() == 0 && upsStatus.getUpsType() != 2) {
              DischargeStatistic newData = null;
              if (upsStatus.getDischargeStatus() != 0) {
                DischargeStatistic d =
                    dischargeStatisticDao.findBeginDateForNewData(peDevice.getId(), null, "desc");
                newData =
                    dischargeStatisticDao.findBeforeDataByDeviceIdAndBatch(
                        peDevice.getId(), d.getBatch(), "desc");
              } else {
                newData =
                    dischargeStatisticDao.findBeginDateForNewData(peDevice.getId(), null, "desc");
              }
              if (newData != null) {
                // 计算剩余时间
                DischargeStatistic oldData =
                    dischargeStatisticDao.findBeginDateForNewData(
                        peDevice.getId(), newData.getBatch(), "asc");
                // 容量差
                Double differenceCapacity = oldData.getCapacity() - newData.getCapacity();
                // 时间差,按照分钟计算
                Long newTime = newData.getCollectTime().getTime();
                Long oldTime = oldData.getCollectTime().getTime();
                // 时间差
                Long differenceTime = (newTime - oldTime) / 1000 / 60;
                Double avg =
                    dischargeStatisticDao.findAvgLoadByDeviceIdAndBatch(
                        peDevice.getId(), newData.getBatch());
                // 消耗1%的容量需要的分钟
                Double unitTime = 0.0;
                if (differenceCapacity != 0L) {
                  unitTime = differenceTime / differenceCapacity;
                }
                Double remainingTime =
                    upsStatus.getElectricQuantity()
                        * unitTime
                        * avg
                        / (Double.parseDouble(upsStatus.getUpsLoad().split("%/")[0]));
                upsStatus.setRemainingTime(
                    Integer.parseInt(new DecimalFormat("0").format(remainingTime)));
              }
            }
          }
          upsStatus.setStatus(StatusEnum.NORMAL);
          upsStatus.setLinkState(1);

          upsStatus.setIdentifier(peDevice.getIdentifier());

          mongoDao.save(upsStatus);
          upsAlarmCondition(upsStatus);

        } else {
          upsStatus.setLinkState(-1);
          upsStatus.setCollectTime(new Date());

          upsStatus.setIdentifier(peDevice.getIdentifier());
          upsAlarmCondition(upsStatus);
        }
      }
    }
  }