|
@@ -10,21 +10,29 @@
|
|
|
*/
|
|
|
package com.ruoyi.ems.handle;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.huashe.common.exception.Assert;
|
|
|
import com.huashe.common.utils.DateUtils;
|
|
|
import com.ruoyi.ems.config.InDoorEnergyConfig;
|
|
|
import com.ruoyi.ems.core.InDoorEnergyTemplate;
|
|
|
import com.ruoyi.ems.domain.ElecMeterH;
|
|
|
import com.ruoyi.ems.domain.EmsDevice;
|
|
|
+import com.ruoyi.ems.domain.EmsObjAbility;
|
|
|
import com.ruoyi.ems.domain.FdEnergyPriceConfig;
|
|
|
import com.ruoyi.ems.domain.MeterDevice;
|
|
|
import com.ruoyi.ems.domain.WaterMeterH;
|
|
|
import com.ruoyi.ems.model.AbilityPayload;
|
|
|
import com.ruoyi.ems.model.CallResponse;
|
|
|
+import com.ruoyi.ems.model.ObjAttrTableItem;
|
|
|
import com.ruoyi.ems.model.Price;
|
|
|
import com.ruoyi.ems.model.QueryDevice;
|
|
|
import com.ruoyi.ems.model.idenergy.CodesVal;
|
|
|
import com.ruoyi.ems.model.idenergy.CodesValReq;
|
|
|
+import com.ruoyi.ems.domain.EmsObjAttrValue;
|
|
|
+import com.ruoyi.ems.service.IEmsObjAttrValueService;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -32,9 +40,12 @@ import org.springframework.stereotype.Service;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -54,6 +65,9 @@ public class InDoorEnergyHandler extends BaseMeterDevHandler {
|
|
|
private static final String MODE_CODE = "M_W2_SM_INDOOR_ENERGY";
|
|
|
|
|
|
@Resource
|
|
|
+ private IEmsObjAttrValueService objAttrValueService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
private InDoorEnergyConfig config;
|
|
|
|
|
|
@Override
|
|
@@ -63,7 +77,44 @@ public class InDoorEnergyHandler extends BaseMeterDevHandler {
|
|
|
|
|
|
@Override
|
|
|
public CallResponse<Void> call(AbilityPayload abilityParam) {
|
|
|
- return null;
|
|
|
+ CallResponse<Void> callResponse = null;
|
|
|
+
|
|
|
+ if (StringUtils.equals("MeterReadingGw", abilityParam.getAbilityKey())) {
|
|
|
+ // 查询当前设备的接口属性值
|
|
|
+ List<EmsObjAttrValue> attrValues = objAttrValueService.selectByObjCode(MODE_CODE,
|
|
|
+ abilityParam.getObjCode());
|
|
|
+ attrValues = attrValues.stream()
|
|
|
+ // 过滤条件:attrKey以"interface"开头
|
|
|
+ .filter(attr -> StringUtils.startsWith(attr.getAttrKey(), "interface"))
|
|
|
+ // 收集为新的List
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ Set<String> pointIdSet = getPointIds(attrValues);
|
|
|
+ CodesValReq req = new CodesValReq(pointIdSet);
|
|
|
+
|
|
|
+ // 调用能耗数据接口获取实时数据
|
|
|
+ InDoorEnergyTemplate template = new InDoorEnergyTemplate(config.getUrl());
|
|
|
+ String callRes = template.getCodesVal(req);
|
|
|
+ JSONObject resJson = JSONObject.parseObject(callRes);
|
|
|
+ Assert.isTrue(StringUtils.equals(resJson.getString("code"), "200"), resJson.getInteger("code"),
|
|
|
+ resJson.getString("error"));
|
|
|
+ EmsObjAbility ability = new EmsObjAbility();
|
|
|
+ ability.setModelCode(MODE_CODE);
|
|
|
+ ability.setAbilityKey("MeterReadingGw");
|
|
|
+ int callStatus = StringUtils.equals(resJson.getString("code"), "200") ? 0 : 2;
|
|
|
+ saveCallLog(abilityParam.getObjCode(), ability, callStatus, JSON.toJSONString(req), callRes);
|
|
|
+
|
|
|
+ String dataStr = resJson.getString("ResultPointObjArr");
|
|
|
+ List<CodesVal> retList = JSONObject.parseArray(dataStr, CodesVal.class);
|
|
|
+ // 更新设备属性
|
|
|
+ updateDeviceAttrList(attrValues, retList);
|
|
|
+ callResponse = new CallResponse<>(0, "成功");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ callResponse = new CallResponse<>(-1, "不支持的能力key:" + abilityParam.getAbilityKey());
|
|
|
+ }
|
|
|
+
|
|
|
+ return callResponse;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -86,42 +137,83 @@ public class InDoorEnergyHandler extends BaseMeterDevHandler {
|
|
|
int cnt = 0;
|
|
|
|
|
|
try {
|
|
|
- List<MeterDevice> deviceList = getMeterDeviceList();
|
|
|
+ // 获取所有能源设备列表
|
|
|
+ List<EmsDevice> deviceList = getDeviceList();
|
|
|
+ List<MeterDevice> meterDeviceList = getMeterDeviceList();
|
|
|
|
|
|
+ // 遍历每个能源设备
|
|
|
if (CollectionUtils.isNotEmpty(deviceList)) {
|
|
|
- List<String> deviceCodes = deviceList.stream().map(MeterDevice::getDeviceCode) // 提取deviceCode属性
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- CodesValReq req = new CodesValReq(deviceCodes);
|
|
|
+ // 调用能耗数据接口获取实时数据
|
|
|
InDoorEnergyTemplate template = new InDoorEnergyTemplate(config.getUrl());
|
|
|
- List<CodesVal> retList = template.getCodesVal(req);
|
|
|
-
|
|
|
- // 电表过滤
|
|
|
- List<MeterDevice> elecDevs = deviceList.stream()
|
|
|
- .filter(device -> device.getMeterCls() != null && device.getMeterCls() == 45)
|
|
|
- .collect(Collectors.toList());
|
|
|
|
|
|
- // 电表抄表数据保存
|
|
|
- if (CollectionUtils.isNotEmpty(elecDevs)) {
|
|
|
- cnt += saveElecMeterReading(elecDevs, retList);
|
|
|
+ for (EmsDevice emsDevice : deviceList) {
|
|
|
+ cnt += meterDevHourProd(template, emsDevice, meterDeviceList);
|
|
|
}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ log.error("能耗数据抄报异常", e);
|
|
|
+ }
|
|
|
|
|
|
- // 水表过滤
|
|
|
- List<MeterDevice> waterDevs = deviceList.stream()
|
|
|
- .filter(device -> device.getMeterCls() != null && device.getMeterCls() == 70)
|
|
|
- .collect(Collectors.toList());
|
|
|
+ return cnt;
|
|
|
+ }
|
|
|
|
|
|
- // 水表抄表数据保存
|
|
|
- if (CollectionUtils.isNotEmpty(waterDevs)) {
|
|
|
- cnt += saveWaterMeterReading(waterDevs, retList);
|
|
|
- }
|
|
|
+ private int meterDevHourProd(InDoorEnergyTemplate template, EmsDevice emsDevice,
|
|
|
+ List<MeterDevice> meterDeviceList) {
|
|
|
+ int cnt = 0;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 查询当前设备的接口属性值
|
|
|
+ List<EmsObjAttrValue> attrValues = objAttrValueService.selectByObjCode(MODE_CODE,
|
|
|
+ emsDevice.getDeviceCode());
|
|
|
+ attrValues = attrValues.stream()
|
|
|
+ // 过滤条件:attrKey以"interface"开头
|
|
|
+ .filter(attr -> StringUtils.startsWith(attr.getAttrKey(), "interface"))
|
|
|
+ // 收集为新的List
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ Set<String> pointIdSet = getPointIds(attrValues);
|
|
|
+ CodesValReq req = new CodesValReq(pointIdSet);
|
|
|
+
|
|
|
+ // 调用能耗数据接口获取实时数据
|
|
|
+ String callRes = template.getCodesVal(req);
|
|
|
+ JSONObject resJson = JSONObject.parseObject(callRes);
|
|
|
+ Assert.isTrue(StringUtils.equals(resJson.getString("code"), "200"), resJson.getInteger("code"),
|
|
|
+ resJson.getString("error"));
|
|
|
+ EmsObjAbility ability = new EmsObjAbility();
|
|
|
+ ability.setModelCode(MODE_CODE);
|
|
|
+ ability.setAbilityKey("MeterReadingGw");
|
|
|
+ int callStatus = StringUtils.equals(resJson.getString("code"), "200") ? 0 : 2;
|
|
|
+ saveCallLog(emsDevice.getDeviceCode(), ability, callStatus, JSON.toJSONString(req), callRes);
|
|
|
+
|
|
|
+ String dataStr = resJson.getString("ResultPointObjArr");
|
|
|
+ List<CodesVal> retList = JSONObject.parseArray(dataStr, CodesVal.class);
|
|
|
+
|
|
|
+ // 电表过滤
|
|
|
+ List<MeterDevice> elecDevs = meterDeviceList.stream()
|
|
|
+ .filter(device -> pointIdSet.contains(device.getDeviceCode()) && 45 == device.getMeterCls())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 电表抄表数据保存
|
|
|
+ if (CollectionUtils.isNotEmpty(elecDevs)) {
|
|
|
+ cnt += saveElecMeterReading(elecDevs, retList);
|
|
|
+ }
|
|
|
|
|
|
- // 更新设备属性
|
|
|
- updateDeviceAttr(retList);
|
|
|
+ // 水表过滤
|
|
|
+ List<MeterDevice> waterDevs = meterDeviceList.stream()
|
|
|
+ .filter(device -> pointIdSet.contains(device.getDeviceCode()) && 70 == device.getMeterCls())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 水表抄表数据保存
|
|
|
+ if (CollectionUtils.isNotEmpty(waterDevs)) {
|
|
|
+ cnt += saveWaterMeterReading(waterDevs, retList);
|
|
|
}
|
|
|
+
|
|
|
+ // 更新设备属性
|
|
|
+ updateDeviceAttrList(attrValues, retList);
|
|
|
}
|
|
|
catch (Exception e) {
|
|
|
- log.error("能耗数据抄报异常", e);
|
|
|
+ log.error("meterDevHourProd error! deviceCode:{}", emsDevice.getDeviceCode(), e);
|
|
|
}
|
|
|
|
|
|
return cnt;
|
|
@@ -198,6 +290,63 @@ public class InDoorEnergyHandler extends BaseMeterDevHandler {
|
|
|
return CollectionUtils.isNotEmpty(meterHList) ? waterMeterHService.insertBatch(meterHList) : 0;
|
|
|
}
|
|
|
|
|
|
- private void updateDeviceAttr(List<CodesVal> retList) {
|
|
|
+ private void updateDeviceAttrList(List<EmsObjAttrValue> attrValues, List<CodesVal> retList) {
|
|
|
+ try {
|
|
|
+ Map<String, CodesVal> codesValMap = retList.stream()
|
|
|
+ .collect(Collectors.toMap(CodesVal::getPointId, Function.identity()));
|
|
|
+
|
|
|
+ for (EmsObjAttrValue objAttr : attrValues) {
|
|
|
+ // 更新列表中的值
|
|
|
+ boolean updated = false;
|
|
|
+ String attrValue = objAttr.getAttrValue();
|
|
|
+ List<ObjAttrTableItem> tableItems = JSON.parseArray(attrValue, ObjAttrTableItem.class);
|
|
|
+
|
|
|
+ if (CollectionUtils.isNotEmpty(tableItems)) {
|
|
|
+ Map<String, ObjAttrTableItem> itemMap = tableItems.stream()
|
|
|
+ .collect(Collectors.toMap(ObjAttrTableItem::getKey, Function.identity()));
|
|
|
+
|
|
|
+ for (Map.Entry<String, ObjAttrTableItem> itemEntry : itemMap.entrySet()) {
|
|
|
+ String itemKey = itemEntry.getKey();
|
|
|
+ ObjAttrTableItem item = itemEntry.getValue();
|
|
|
+
|
|
|
+ if (codesValMap.containsKey(itemKey)) {
|
|
|
+ updated = true;
|
|
|
+ CodesVal codesVal = codesValMap.get(itemKey);
|
|
|
+ item.setValue(codesVal.getValue());
|
|
|
+ item.setUpdateTime(codesVal.getTime());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有更新,则保存回数据库
|
|
|
+ if (updated) {
|
|
|
+ attrValue = JSON.toJSONString(tableItems);
|
|
|
+ objAttr.setAttrValue(attrValue);
|
|
|
+ objAttrValueService.updateObjAttrValue(objAttr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ log.error("更新设备属性异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Set<String> getPointIds(List<EmsObjAttrValue> attrValues) {
|
|
|
+ Set<String> pointIds = new HashSet<>();
|
|
|
+
|
|
|
+ if (CollectionUtils.isNotEmpty(attrValues)) {
|
|
|
+ for (EmsObjAttrValue objAttr : attrValues) {
|
|
|
+ String attrValue = objAttr.getAttrValue();
|
|
|
+ List<ObjAttrTableItem> tableItems = JSON.parseArray(attrValue, ObjAttrTableItem.class);
|
|
|
+
|
|
|
+ if (CollectionUtils.isNotEmpty(tableItems)) {
|
|
|
+ List<String> tmpList = tableItems.stream().map(ObjAttrTableItem::getKey)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ pointIds.addAll(tmpList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return pointIds;
|
|
|
}
|
|
|
}
|