diff --git a/smart-switch-service/src/main/java/com/ruoyi/iot/domain/CurrentDeviceData.java b/smart-switch-service/src/main/java/com/ruoyi/iot/domain/CurrentDeviceData.java index fcc66cfd..bf0f7e50 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/iot/domain/CurrentDeviceData.java +++ b/smart-switch-service/src/main/java/com/ruoyi/iot/domain/CurrentDeviceData.java @@ -1,15 +1,7 @@ package com.ruoyi.iot.domain; -import com.ruoyi.common.constant.IotConstants; -import com.ruoyi.common.utils.NumberUtils; -import com.ruoyi.common.utils.collection.CollectionUtils; -import com.ruoyi.iot.constants.ReceiveConstants; -import com.ruoyi.ss.device.domain.enums.DeviceOutageWay; import lombok.Data; -import java.math.BigDecimal; -import java.math.RoundingMode; -import java.util.Date; import java.util.List; /** @@ -21,60 +13,4 @@ public class CurrentDeviceData { private String id; private String title; private List datastreams; - - /** - * 转换为设备信息 - * @return - */ - public IotDeviceInfo parseDeviceInfo() { - if (CollectionUtils.isEmptyElement(datastreams)) { - return null; - } - - IotDeviceInfo device = IotDeviceInfo.newDefaultInstance(); - device.setMac(title); - device.setId(id); - - for (CurrentDatastream stream : datastreams) { - String value = stream.getValue().toString(); - - switch (stream.getId()) { - case ReceiveConstants.DS_V: - device.setV(NumberUtils.nonNullDecimal(value)); - break; - case ReceiveConstants.DS_P: - device.setP(NumberUtils.nonNullDecimal(value)); - break; - case ReceiveConstants.DS_A: - device.setA(NumberUtils.nonNullDecimal(value)); - break; - case ReceiveConstants.DS_W: - device.setW(NumberUtils.nonNullDecimal(value).divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP)); - break; - case ReceiveConstants.DS_S: - device.setS(Integer.valueOf(value).toString()); - break; - case ReceiveConstants.DS_M: - device.setM(NumberUtils.nonNullDecimal(value).divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP)); - break; - case ReceiveConstants.DS_SET: - DeviceOutageWay deviceOutageWay = DeviceOutageWay.parse(value); - device.setSet(deviceOutageWay.getValue()); - break; - case ReceiveConstants.DS_TIME: - device.setTime(NumberUtils.nonNullDecimal(value)); - device.setAt(stream.getAt()); - break; - case ReceiveConstants.DS_FW: - device.setModel(value); - break; - case ReceiveConstants.DS_SSID: - device.setWifi(value); - break; - default: - break; - } - } - return device; - } } diff --git a/smart-switch-service/src/main/java/com/ruoyi/iot/domain/IotDeviceInfo.java b/smart-switch-service/src/main/java/com/ruoyi/iot/domain/IotDeviceInfo.java index 3bff42bf..966c2b0b 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/iot/domain/IotDeviceInfo.java +++ b/smart-switch-service/src/main/java/com/ruoyi/iot/domain/IotDeviceInfo.java @@ -16,10 +16,6 @@ import java.util.Date; @Data @Builder public class IotDeviceInfo { - private String id; // iot设备id - - private String mac; // 设备mac - @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date at; // 时间 diff --git a/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotConverter.java b/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotConverter.java new file mode 100644 index 00000000..d58e6750 --- /dev/null +++ b/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotConverter.java @@ -0,0 +1,17 @@ +package com.ruoyi.iot.service; + +import com.ruoyi.iot.domain.CurrentDatastream; +import com.ruoyi.iot.domain.CurrentDeviceData; +import com.ruoyi.iot.domain.IotDeviceInfo; + +import java.util.List; + +/** + * @author wjh + * 2024/9/29 + */ +public interface IotConverter { + + IotDeviceInfo toIotDeviceInfo(CurrentDeviceData data1, CurrentDeviceData data2); + +} diff --git a/smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotConverterImpl.java b/smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotConverterImpl.java new file mode 100644 index 00000000..c28da10e --- /dev/null +++ b/smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotConverterImpl.java @@ -0,0 +1,103 @@ +package com.ruoyi.iot.service.impl; + +import com.ruoyi.common.utils.NumberUtils; +import com.ruoyi.common.utils.collection.CollectionUtils; +import com.ruoyi.iot.constants.ReceiveConstants; +import com.ruoyi.iot.domain.CurrentDatastream; +import com.ruoyi.iot.domain.CurrentDeviceData; +import com.ruoyi.iot.domain.IotDeviceInfo; +import com.ruoyi.iot.service.IotConverter; +import com.ruoyi.ss.device.domain.enums.DeviceOutageWay; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Objects; + +/** + * @author wjh + * 2024/9/29 + */ +@Service +public class IotConverterImpl implements IotConverter { + @Override + public IotDeviceInfo toIotDeviceInfo(CurrentDeviceData data1, CurrentDeviceData data2) { + if (data1 == null && data2 == null) { + return null; + } + + // 获取数据点,让不为空的排在前面 + List ds1 = data1 == null ? data2.getDatastreams() : data1.getDatastreams(); + List ds2 = data2 == null ? data1.getDatastreams() : data2.getDatastreams(); + + if (CollectionUtils.isNotEmptyElement(ds1)) { + return this.toIotDeviceInfo(ds1, ds2); + } else if (CollectionUtils.isNotEmptyElement(ds2)) { + return this.toIotDeviceInfo(ds2, ds1); + } + + return null; + } + + private IotDeviceInfo toIotDeviceInfo(List ds1, List ds2) { + if (CollectionUtils.isEmptyElement(ds1) && CollectionUtils.isEmptyElement(ds2)) { + return null; + } + + IotDeviceInfo device = IotDeviceInfo.newDefaultInstance(); + + for (CurrentDatastream stream : ds1) { + String id = stream.getId(); // 数据点ID + String value = stream.getValue().toString(); // 数据点取值,取最新的值 + Date at = stream.getAt(); + if (ds2 != null) { + CurrentDatastream stream2 = ds2.stream().filter(item -> Objects.equals(item.getId(), stream.getId())).findFirst().orElse(null); + if (stream2 != null && stream2.getAt().getTime() > stream.getAt().getTime()) { + value = stream2.getValue().toString(); + at = stream2.getAt(); + } + } + + switch (id) { + case ReceiveConstants.DS_V: + device.setV(NumberUtils.nonNullDecimal(value)); + break; + case ReceiveConstants.DS_P: + device.setP(NumberUtils.nonNullDecimal(value)); + break; + case ReceiveConstants.DS_A: + device.setA(NumberUtils.nonNullDecimal(value)); + break; + case ReceiveConstants.DS_W: + device.setW(NumberUtils.nonNullDecimal(value).divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP)); + break; + case ReceiveConstants.DS_S: + device.setS(Integer.valueOf(value).toString()); + break; + case ReceiveConstants.DS_M: + device.setM(NumberUtils.nonNullDecimal(value).divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP)); + break; + case ReceiveConstants.DS_SET: + DeviceOutageWay deviceOutageWay = DeviceOutageWay.parse(value); + device.setSet(deviceOutageWay.getValue()); + break; + case ReceiveConstants.DS_TIME: + device.setTime(NumberUtils.nonNullDecimal(value)); + device.setAt(at); + break; + case ReceiveConstants.DS_FW: + device.setModel(value); + break; + case ReceiveConstants.DS_SSID: + device.setWifi(value); + break; + default: + break; + } + } + return device; + } +} diff --git a/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotReceiveServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotReceiveServiceImpl.java similarity index 97% rename from smart-switch-service/src/main/java/com/ruoyi/iot/service/IotReceiveServiceImpl.java rename to smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotReceiveServiceImpl.java index ac067414..3655ae4e 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotReceiveServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotReceiveServiceImpl.java @@ -1,4 +1,4 @@ -package com.ruoyi.iot.service; +package com.ruoyi.iot.service.impl; import com.ruoyi.common.core.redis.RedisLock; import com.ruoyi.common.core.redis.enums.RedisLockKey; import com.ruoyi.common.utils.NumberUtils; @@ -11,6 +11,8 @@ import com.ruoyi.iot.domain.ReceiveMsg; import com.ruoyi.iot.domain.response.CommandResponse; import com.ruoyi.iot.enums.ReceiveStatus; import com.ruoyi.iot.enums.ReceiveType; +import com.ruoyi.iot.service.IotReceiveService; +import com.ruoyi.iot.service.IotService; import com.ruoyi.ss.device.domain.Device; import com.ruoyi.ss.device.domain.enums.DeviceOutageWay; import com.ruoyi.ss.device.domain.vo.DeviceVO; @@ -18,7 +20,6 @@ import com.ruoyi.ss.device.service.DeviceService; import com.ruoyi.ss.suit.domain.enums.SuitFeeType; import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery; import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillStatus; -import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillType; import com.ruoyi.ss.transactionBill.domain.vo.TransactionBillVO; import com.ruoyi.ss.transactionBill.service.TransactionAssembler; import com.ruoyi.ss.transactionBill.service.TransactionBillService; @@ -38,7 +39,7 @@ import java.util.List; */ @Service @Slf4j -public class IotReceiveServiceImpl implements IotReceiveService{ +public class IotReceiveServiceImpl implements IotReceiveService { @Autowired private DeviceService deviceService; diff --git a/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotServiceImpl.java similarity index 95% rename from smart-switch-service/src/main/java/com/ruoyi/iot/service/IotServiceImpl.java rename to smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotServiceImpl.java index f9ff8102..797a9cba 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotServiceImpl.java @@ -1,4 +1,4 @@ -package com.ruoyi.iot.service; +package com.ruoyi.iot.service.impl; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; @@ -16,8 +16,11 @@ import com.ruoyi.iot.domain.response.DetailResponse; import com.ruoyi.iot.domain.response.HistoryDataPointResponse; import com.ruoyi.iot.enums.IotHttpStatus; import com.ruoyi.iot.interfaces.IotDevice; +import com.ruoyi.iot.service.IotConverter; +import com.ruoyi.iot.service.IotService; import com.ruoyi.ss.device.domain.enums.DeviceOnlineStatus; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -58,6 +61,9 @@ public class IotServiceImpl implements IotService { @Value(value = "${debug}") private Boolean debug; + @Autowired + private IotConverter iotConverter; + // 查询OneNet设备在线状态 @Override public DeviceOnlineStatus getOnlineStatus(String deviceName, String productId) { @@ -318,31 +324,26 @@ public class IotServiceImpl implements IotService { @Override public IotDeviceInfo getDeviceInfo(String deviceName, String productId) { - if (StringUtils.isBlank(deviceName)) { + if (StringUtils.isAnyBlank(deviceName, productId)) { return null; } CurrentDeviceData currentDataPoint = getCurrentDataPoint(deviceName, productId); if (currentDataPoint == null) { return null; } - return currentDataPoint.parseDeviceInfo(); + return iotConverter.toIotDeviceInfo(currentDataPoint, null); } @Override public IotDeviceInfo getDeviceInfo(IotDevice device) { - IotDeviceInfo info = null; if (device == null || StringUtils.isBlank(device.getProductId())) { - return info; + return null; } - if (StringUtils.hasText(device.iotMac1())) { - info = getDeviceInfo(device.iotMac1(), device.getProductId()); - } - if (info == null && StringUtils.hasText(device.iotMac2())) { - info = getDeviceInfo(device.iotMac2(), device.getProductId()); - } + CurrentDeviceData data1 = getCurrentDataPoint(device.getMac1(), device.getProductId()); + CurrentDeviceData data2 = getCurrentDataPoint(device.getMac2(), device.getProductId()); - return info; + return iotConverter.toIotDeviceInfo(data1, data2); } @Override @@ -355,7 +356,7 @@ public class IotServiceImpl implements IotService { return Collections.emptyList(); } return dataList.stream() - .map(CurrentDeviceData::parseDeviceInfo) + .map(item -> iotConverter.toIotDeviceInfo(item, null)) .filter(Objects::nonNull) .collect(Collectors.toList()); } diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/meterReadingRecord/service/SmMeterReadingRecordServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/meterReadingRecord/service/SmMeterReadingRecordServiceImpl.java index f9c498e4..4a934fa4 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/meterReadingRecord/service/SmMeterReadingRecordServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/meterReadingRecord/service/SmMeterReadingRecordServiceImpl.java @@ -145,7 +145,7 @@ public class SmMeterReadingRecordServiceImpl implements ISmMeterReadingRecordSer // 更新数据 for (DeviceVO device : list) { try { - if (StringUtils.isAnyBlank(device.getMac(), device.getModelProductId())) { + if (device == null) { continue; } IotDeviceInfo deviceInfo = iotService.getDeviceInfo(device);