From 791a9192800de58008156bb5eeeda36312e7fa2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A3=B7=E5=8F=B6?= <14103883+leaf-phos@user.noreply.gitee.com> Date: Wed, 6 Nov 2024 17:59:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96APP=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=9F=A5=E8=AF=A2=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/iot/domain/IotDeviceInfo.java | 2 + .../iot/service/impl/IotConverterImpl.java | 14 +- .../service/impl/DeviceServiceImpl.java | 145 ++++++++++-------- 3 files changed, 93 insertions(+), 68 deletions(-) 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 c44f2477..31ecea1e 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,6 +16,8 @@ import java.util.Date; @Data @Builder public class IotDeviceInfo { + private String 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/impl/IotConverterImpl.java b/smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotConverterImpl.java index e438cd55..a232756b 100644 --- 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 @@ -33,21 +33,29 @@ public class IotConverterImpl implements IotConverter { List ds1 = data1 == null ? data2.getDatastreams() : data1.getDatastreams(); List ds2 = data2 == null ? data1.getDatastreams() : data2.getDatastreams(); + String mac = null; + try { + mac = data1.getTitle(); + } catch (Exception e) { + mac = data2.getTitle(); + } + if (CollectionUtils.isNotEmptyElement(ds1)) { - return this.toIotDeviceInfo(ds1, ds2); + return this.toIotDeviceInfo(ds1, ds2, mac); } else if (CollectionUtils.isNotEmptyElement(ds2)) { - return this.toIotDeviceInfo(ds2, ds1); + return this.toIotDeviceInfo(ds2, ds1, mac); } return null; } - private IotDeviceInfo toIotDeviceInfo(List ds1, List ds2) { + private IotDeviceInfo toIotDeviceInfo(List ds1, List ds2, String mac) { if (CollectionUtils.isEmptyElement(ds1) && CollectionUtils.isEmptyElement(ds2)) { return null; } IotDeviceInfo device = IotDeviceInfo.newDefaultInstance(); + device.setMac(mac); for (CurrentDatastream stream : ds1) { String id = stream.getId(); // 数据点ID 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 d6f3d1ef..1d3fb9ea 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 @@ -1029,82 +1029,97 @@ public class DeviceServiceImpl implements DeviceService // 拼接正在使用中的订单数量 deviceAssembler.assembleUsingBillCount(list); - for (DeviceVO device : list) { - try { - if (StringUtils.isAnyBlank(device.getMac(), device.getModelProductId())) { - continue; - } - // 获取数据点信息 - IotDeviceInfo deviceInfo = iotService.getDeviceInfo(device); + // todo 列表根据产品ID分组 + Map> group = list.stream().collect(Collectors.groupingBy(DeviceVO::getModelProductId)); - // 更新设备信息 - device.setDeviceId(device.getDeviceId()); - if (deviceInfo != null) { - device.setLastPullTime(deviceInfo.getAt()); - device.setPowerStatus(deviceInfo.getS()); - device.setRemainTime(deviceInfo.getTime()); - device.setRealTimePower(deviceInfo.getP()); - device.setVoltage(deviceInfo.getV()); - device.setElectricity(deviceInfo.getA()); - device.setVersion(deviceInfo.getVersion()); + for (Map.Entry> entry : group.entrySet()) { + List deviceList = entry.getValue(); - // 总用电量 - if (deviceInfo.getW() != null) { - // 若有数据点中的值大于数据库中的值,则更新 - if (deviceInfo.getW().compareTo(device.getTotalElectriQuantity()) >= 0) { - device.setTotalElectriQuantity(deviceInfo.getW()); + List macList = CollectionUtils.map(deviceList, DeviceVO::getMac); + macList.addAll(CollectionUtils.map(deviceList, DeviceVO::getMac2)); + + List iotList = iotService.getDeviceInfo(macList, entry.getKey()); + + for (DeviceVO device : list) { + try { + if (StringUtils.isAnyBlank(device.getMac(), device.getModelProductId())) { + continue; + } + // 获取数据点信息 +// IotDeviceInfo deviceInfo = iotService.getDeviceInfo(device); + IotDeviceInfo deviceInfo = iotList.stream() + .filter(item -> Objects.equals(device.getMac(), item.getMac()) || Objects.equals(device.getMac2(), item.getMac())) + .findFirst().orElse(null); + + // 更新设备信息 + device.setDeviceId(device.getDeviceId()); + if (deviceInfo != null) { + device.setLastPullTime(deviceInfo.getAt()); + device.setPowerStatus(deviceInfo.getS()); + device.setRemainTime(deviceInfo.getTime()); + device.setRealTimePower(deviceInfo.getP()); + device.setVoltage(deviceInfo.getV()); + device.setElectricity(deviceInfo.getA()); + device.setVersion(deviceInfo.getVersion()); + + // 总用电量 + if (deviceInfo.getW() != null) { + // 若有数据点中的值大于数据库中的值,则更新 + if (deviceInfo.getW().compareTo(device.getTotalElectriQuantity()) >= 0) { + device.setTotalElectriQuantity(deviceInfo.getW()); + } + // 若数据点小于数据库中的值,则发命令恢复物联网设备的总用电量 + else { + scheduledExecutorService.schedule(() -> { + iotService.setTotalEle(device, device.getTotalElectriQuantity().multiply(new BigDecimal(1000))); + }, 0, TimeUnit.SECONDS); + } } - // 若数据点小于数据库中的值,则发命令恢复物联网设备的总用电量 - else { - scheduledExecutorService.schedule(() -> { - iotService.setTotalEle(device, device.getTotalElectriQuantity().multiply(new BigDecimal(1000))); - }, 0, TimeUnit.SECONDS); + + // 是否有WIFI + if (ModelTag.hasTag(device.getModelTags(), ModelTag.WIFI)) { + device.setWifi(deviceInfo.getWifi()); + } + + // 是否有电量 + if (ModelTag.hasTag(device.getModelTags(), ModelTag.ELE)) { + device.setSurplusEle(deviceInfo.getM()); + } else { + device.setSurplusEle(BigDecimal.ZERO); } } - // 是否有WIFI - if (ModelTag.hasTag(device.getModelTags(), ModelTag.WIFI)) { - device.setWifi(deviceInfo.getWifi()); - } - - // 是否有电量 - if (ModelTag.hasTag(device.getModelTags(), ModelTag.ELE)) { - device.setSurplusEle(deviceInfo.getM()); - } else { - device.setSurplusEle(BigDecimal.ZERO); - } - } - - // 判断设备是否正在使用 - // 设备过期时间 > 当前时间,则正在使用 + // 判断设备是否正在使用 + // 设备过期时间 > 当前时间,则正在使用 // boolean hasTime = device.getExpireTime() != null && device.getExpireTime().isAfter(now); // // 若当前设备有电量,则正在使用 // boolean hasEle = device.getSurplusEle() != null && device.getSurplusEle().compareTo(BigDecimal.ZERO) > 0; - // 若开关开启,则正在使用 - boolean hasOpen = DevicePowerStatus.ON.getStatus().equals(device.getPowerStatus()); - // 若设备有正在使用中的订单,则正在使用 - boolean hasUsingBill = device.getUsingBillCount() != null && device.getUsingBillCount() > 0; - if (hasOpen || hasUsingBill) { - device.setStatus(DeviceStatus.USING.getStatus()); - } else { - device.setStatus(DeviceStatus.NORMAL.getStatus()); - } + // 若开关开启,则正在使用 + boolean hasOpen = DevicePowerStatus.ON.getStatus().equals(device.getPowerStatus()); + // 若设备有正在使用中的订单,则正在使用 + boolean hasUsingBill = device.getUsingBillCount() != null && device.getUsingBillCount() > 0; + if (hasOpen || hasUsingBill) { + device.setStatus(DeviceStatus.USING.getStatus()); + } else { + device.setStatus(DeviceStatus.NORMAL.getStatus()); + } - // 是否在线 - String onlineStatus1 = iotService.getOnlineStatus(device.getMac(), device.getProductId()).getStatus(); - String onlineStatus2 = iotService.getOnlineStatus(device.getMac2(), device.getProductId()).getStatus(); - device.setOnlineStatus1(onlineStatus1); - device.setOnlineStatus2(onlineStatus2); - if (DeviceOnlineStatus.ONLINE.getStatus().equals(onlineStatus1) || DeviceOnlineStatus.ONLINE.getStatus().equals(onlineStatus2)) { - device.setOnlineStatus(DeviceOnlineStatus.ONLINE.getStatus()); - device.setLastOnlineTime(now); - } else { - device.setOnlineStatus(DeviceOnlineStatus.OFFLINE.getStatus()); - } + // 是否在线 + String onlineStatus1 = iotService.getOnlineStatus(device.getMac(), device.getProductId()).getStatus(); + String onlineStatus2 = iotService.getOnlineStatus(device.getMac2(), device.getProductId()).getStatus(); + device.setOnlineStatus1(onlineStatus1); + device.setOnlineStatus2(onlineStatus2); + if (DeviceOnlineStatus.ONLINE.getStatus().equals(onlineStatus1) || DeviceOnlineStatus.ONLINE.getStatus().equals(onlineStatus2)) { + device.setOnlineStatus(DeviceOnlineStatus.ONLINE.getStatus()); + device.setLastOnlineTime(now); + } else { + device.setOnlineStatus(DeviceOnlineStatus.OFFLINE.getStatus()); + } - this.updateIotInfo(device); - } catch (Exception e) { - log.error("更新设备信息失败,deviceId:{}", device.getDeviceId(), e); + this.updateIotInfo(device); + } catch (Exception e) { + log.error("更新设备信息失败,deviceId:{}, {}", device.getDeviceId(), e.getMessage()); + } } } }