优化APP设备列表查询速度

This commit is contained in:
磷叶 2024-11-06 17:59:11 +08:00
parent 41eef23439
commit 791a919280
3 changed files with 93 additions and 68 deletions

View File

@ -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; // 时间

View File

@ -33,21 +33,29 @@ public class IotConverterImpl implements IotConverter {
List<CurrentDatastream> ds1 = data1 == null ? data2.getDatastreams() : data1.getDatastreams();
List<CurrentDatastream> 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<CurrentDatastream> ds1, List<CurrentDatastream> ds2) {
private IotDeviceInfo toIotDeviceInfo(List<CurrentDatastream> ds1, List<CurrentDatastream> 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

View File

@ -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<String, List<DeviceVO>> 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<String, List<DeviceVO>> entry : group.entrySet()) {
List<DeviceVO> deviceList = entry.getValue();
// 总用电量
if (deviceInfo.getW() != null) {
// 若有数据点中的值大于数据库中的值则更新
if (deviceInfo.getW().compareTo(device.getTotalElectriQuantity()) >= 0) {
device.setTotalElectriQuantity(deviceInfo.getW());
List<String> macList = CollectionUtils.map(deviceList, DeviceVO::getMac);
macList.addAll(CollectionUtils.map(deviceList, DeviceVO::getMac2));
List<IotDeviceInfo> 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());
}
}
}
}