diff --git a/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotService.java b/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotService.java index b8ed7096..2e41cbab 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotService.java +++ b/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotService.java @@ -6,7 +6,6 @@ import com.ruoyi.iot.domain.IotDeviceInfo; import com.ruoyi.iot.domain.response.CommandResponse; import com.ruoyi.iot.interfaces.IotDevice; import com.ruoyi.ss.device.domain.enums.DeviceOnlineStatus; -import com.ruoyi.ss.device.domain.vo.DeviceVO; import java.math.BigDecimal; import java.util.List; @@ -88,10 +87,10 @@ public interface IotService { /** * 获取设备信息列表,并转换为IotDeviceInfo * - * @param deviceNames 设备名称列表 + * @param deviceList 设备名称列表 * @param productId */ - List getDeviceInfo(List deviceNames, String productId); + List getDeviceInfo(List deviceList, String productId); /** * 注册设备 diff --git a/smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotServiceImpl.java index c3c743fe..02052404 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotServiceImpl.java @@ -7,6 +7,7 @@ import com.ruoyi.common.constant.IotConstants; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.ServiceUtil; import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.collection.CollectionUtils; import com.ruoyi.common.utils.http.HttpUtils; import com.ruoyi.common.utils.oneNet.Token; import com.ruoyi.iot.domain.*; @@ -19,13 +20,14 @@ 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 com.ruoyi.ss.device.domain.vo.DeviceVO; 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; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -127,7 +129,11 @@ public class IotServiceImpl implements IotService { } } catch (Exception e) { log.info("mac1通电失败,尝试用mac2通电"); - result = this.open(device.iotMac2(), device.getProductId()); + if (!StringUtils.isAnyBlank(device.iotMac2(), device.getProductId())) { + result = this.open(device.iotMac2(), device.getProductId()); + } else { + throw e; + } } return result; @@ -136,6 +142,9 @@ public class IotServiceImpl implements IotService { // 断电 @Override public boolean close(String deviceName, String productId) { + if (StringUtils.isAnyBlank(deviceName, productId)) { + return false; + } CommandResponse response = sendCommand(deviceName, IotConstants.COMMAND_CLOSE, productId); IotHttpStatus status = IotHttpStatus.convertByCode(response.getCode()); if (!IotHttpStatus.SUCCESS.equals(status)) { @@ -160,7 +169,11 @@ public class IotServiceImpl implements IotService { } } catch (Exception e) { log.info("mac1断电失败:{},尝试用mac2断电", e.getMessage()); - result = this.close(device.iotMac2(), device.getProductId()); + if (!StringUtils.isAnyBlank(device.iotMac2(), device.getProductId())) { + result = this.close(device.iotMac2(), device.getProductId()); + } else { + throw e; + } } return result; @@ -372,18 +385,31 @@ public class IotServiceImpl implements IotService { } @Override - public List getDeviceInfo(List deviceNames, String productId) { - if (CollectionUtils.isEmpty(deviceNames)) { + public List getDeviceInfo(List deviceList, String productId) { + if (CollectionUtils.isEmpty(deviceList)) { return Collections.emptyList(); } - List dataList = getCurrentDataPoint(deviceNames, productId); + + // 获取数据点列表 + List macList = CollectionUtils.map(deviceList, IotDevice::iotMac1); + macList.addAll(CollectionUtils.map(deviceList, IotDevice::iotMac2)); + List dataList = getCurrentDataPoint(macList, productId); if (CollectionUtils.isEmpty(dataList)) { return Collections.emptyList(); } - return dataList.stream() - .map(item -> iotConverter.toIotDeviceInfo(item, null)) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + + // 构造返回数据 + List result = new ArrayList<>(); + for (IotDevice device : deviceList) { + CurrentDeviceData data1 = dataList.stream().filter(item -> device.iotMac1().equals(item.getTitle())).findFirst().orElse(null); + CurrentDeviceData data2 = dataList.stream().filter(item -> device.iotMac2().equals(item.getTitle())).findFirst().orElse(null); + IotDeviceInfo iotDeviceInfo = iotConverter.toIotDeviceInfo(data1, data2); + if (iotDeviceInfo != null) { + result.add(iotDeviceInfo); + } + } + + return result; } @Override diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/impl/DeviceServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/impl/DeviceServiceImpl.java index 2b2c5dc5..c8822e43 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/impl/DeviceServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/impl/DeviceServiceImpl.java @@ -1031,12 +1031,8 @@ public class DeviceServiceImpl implements DeviceService Map> group = list.stream().collect(Collectors.groupingBy(DeviceVO::getModelProductId)); for (Map.Entry> entry : group.entrySet()) { - List deviceList = entry.getValue(); - List macList = CollectionUtils.map(deviceList, DeviceVO::getMac); - macList.addAll(CollectionUtils.map(deviceList, DeviceVO::getMac2)); - - List iotList = iotService.getDeviceInfo(macList, entry.getKey()); + List iotList = iotService.getDeviceInfo(entry.getValue(), entry.getKey()); for (DeviceVO device : list) { try {