public void getEnergyDevicesHistoryValueWithTimeRange() {
        try {
            System.out.println('开始刷新电表======>');
            CmdbAssets elvCmdbAssets = new CmdbAssets();
            elvCmdbAssets.setType('电表');
            List<CmdbAssets> elvCmdbAssetsList = cmdbAssetsMapper.selectCmdbAssetsList(elvCmdbAssets);
            String[] stringTimeRange = getStringTimeRange();
            for (CmdbAssets cmdbAssets : elvCmdbAssetsList) {
                // 前2 和 前1 小时之间的数据
                String historyDataWithTimeRangeBefore = this.energyServer.getHistoryDataWithTimeRange(cmdbAssets.getDeviceCode().split(",")[6], stringTimeRange[0], stringTimeRange[1]);
                // 移除字符串中的换行符
                historyDataWithTimeRangeBefore = historyDataWithTimeRangeBefore.replaceAll("\\n", "");
                JSONObject jsonObjectHisDataBefore = JSONObject.parseObject(historyDataWithTimeRangeBefore);
                List<JSONObject> jsonObjectBeforeList = jsonObjectHisDataBefore.getJSONArray('data').toJavaList(JSONObject.class);

                // 当前和前1小时之间数据
                String historyDataWithTimeRangeNow = this.energyServer.getHistoryDataWithTimeRange(cmdbAssets.getDeviceCode().split(",")[6], stringTimeRange[1], stringTimeRange[2]);
                JSONObject jsonObjectHisNow = JSONObject.parseObject(historyDataWithTimeRangeNow);
                List<JSONObject> jsonObjectNowList = jsonObjectHisNow.getJSONArray('data').toJavaList(JSONObject.class);

                //TODO  jsonObjectBeforeList末和jsonObjectNowList末计算差值
                JSONObject jsonObjectBeforeLast = jsonObjectBeforeList.get(jsonObjectBeforeList.size() - 1);
                JSONObject jsonObjectNowLast = jsonObjectNowList.get(jsonObjectNowList.size() - 1);
                BigDecimal beforeLastValue = jsonObjectBeforeLast.getBigDecimal('value');
                BigDecimal nowLastValue = jsonObjectNowLast.getBigDecimal('value');
                // 相减并设置保留一位小数
                BigDecimal result = nowLastValue.subtract(beforeLastValue).setScale(1, BigDecimal.ROUND_HALF_UP);
                //TODO 调用接口新增能耗获取记录
                EnergyValueLog energyValueLog = new EnergyValueLog();
                energyValueLog.setDeviceId(cmdbAssets.getId());
                energyValueLog.setEnergyValue(result.toString());
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = format.parse(stringTimeRange[2]);
                energyValueLog.setGetTime(date);
                energyValueLogService.insertEnergyValueLog(energyValueLog);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println('开始刷新水表=====>');
        CmdbAssets waterCmdbAssets = new CmdbAssets();
        waterCmdbAssets.setType('水表');
        List<CmdbAssets> waterCmdbAssetsList = cmdbAssetsMapper.selectCmdbAssetsList(waterCmdbAssets);

    }

该代码片段的功能是:

  1. 从数据库中获取所有电表和水表设备信息。
  2. 针对每台设备,获取其在两个时间段内的历史数据。
  3. 计算两个时间段内数据的差值。
  4. 将计算结果存储到数据库中。

解决方法: 代码中使用 replaceAll("\\n", "") 移除字符串中的换行符,使其成为合法的 JSON 字符串。

[{"data":[{"time":"2023-08-29 08:18:41","value":5366.1}],"pointId":"C_1001_AV_0006"}]

这个 JSON 字符串包含了设备在某个时间点的数据,数据包含 timevalue 两个字段。

代码中使用 JSONObject.parseObject 方法将 JSON 字符串解析成 JSONObject 对象,然后从 JSONObject 对象中获取 data 字段,并将其转换为 List<JSONObject> 对象。

最后,代码中使用 BigDecimal 类计算两个时间段内数据的差值,并将结果存储到数据库中。

该代码片段是一个典型的能耗数据收集和处理流程,可以帮助用户了解设备的能耗情况。

获取能耗设备历史数据并计算差值

原文地址: https://www.cveoy.top/t/topic/qxxX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录