优化APP设备列表查询速度
This commit is contained in:
parent
41eef23439
commit
791a919280
|
@ -16,6 +16,8 @@ import java.util.Date;
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
public class IotDeviceInfo {
|
public class IotDeviceInfo {
|
||||||
|
private String mac;
|
||||||
|
|
||||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
private Date at; // 时间
|
private Date at; // 时间
|
||||||
|
|
||||||
|
|
|
@ -33,21 +33,29 @@ public class IotConverterImpl implements IotConverter {
|
||||||
List<CurrentDatastream> ds1 = data1 == null ? data2.getDatastreams() : data1.getDatastreams();
|
List<CurrentDatastream> ds1 = data1 == null ? data2.getDatastreams() : data1.getDatastreams();
|
||||||
List<CurrentDatastream> ds2 = data2 == null ? data1.getDatastreams() : data2.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)) {
|
if (CollectionUtils.isNotEmptyElement(ds1)) {
|
||||||
return this.toIotDeviceInfo(ds1, ds2);
|
return this.toIotDeviceInfo(ds1, ds2, mac);
|
||||||
} else if (CollectionUtils.isNotEmptyElement(ds2)) {
|
} else if (CollectionUtils.isNotEmptyElement(ds2)) {
|
||||||
return this.toIotDeviceInfo(ds2, ds1);
|
return this.toIotDeviceInfo(ds2, ds1, mac);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
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)) {
|
if (CollectionUtils.isEmptyElement(ds1) && CollectionUtils.isEmptyElement(ds2)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
IotDeviceInfo device = IotDeviceInfo.newDefaultInstance();
|
IotDeviceInfo device = IotDeviceInfo.newDefaultInstance();
|
||||||
|
device.setMac(mac);
|
||||||
|
|
||||||
for (CurrentDatastream stream : ds1) {
|
for (CurrentDatastream stream : ds1) {
|
||||||
String id = stream.getId(); // 数据点ID
|
String id = stream.getId(); // 数据点ID
|
||||||
|
|
|
@ -1029,82 +1029,97 @@ public class DeviceServiceImpl implements DeviceService
|
||||||
// 拼接正在使用中的订单数量
|
// 拼接正在使用中的订单数量
|
||||||
deviceAssembler.assembleUsingBillCount(list);
|
deviceAssembler.assembleUsingBillCount(list);
|
||||||
|
|
||||||
for (DeviceVO device : list) {
|
// todo 列表根据产品ID分组
|
||||||
try {
|
Map<String, List<DeviceVO>> group = list.stream().collect(Collectors.groupingBy(DeviceVO::getModelProductId));
|
||||||
if (StringUtils.isAnyBlank(device.getMac(), device.getModelProductId())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// 获取数据点信息
|
|
||||||
IotDeviceInfo deviceInfo = iotService.getDeviceInfo(device);
|
|
||||||
|
|
||||||
// 更新设备信息
|
for (Map.Entry<String, List<DeviceVO>> entry : group.entrySet()) {
|
||||||
device.setDeviceId(device.getDeviceId());
|
List<DeviceVO> deviceList = entry.getValue();
|
||||||
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());
|
|
||||||
|
|
||||||
// 总用电量
|
List<String> macList = CollectionUtils.map(deviceList, DeviceVO::getMac);
|
||||||
if (deviceInfo.getW() != null) {
|
macList.addAll(CollectionUtils.map(deviceList, DeviceVO::getMac2));
|
||||||
// 若有数据点中的值大于数据库中的值,则更新
|
|
||||||
if (deviceInfo.getW().compareTo(device.getTotalElectriQuantity()) >= 0) {
|
List<IotDeviceInfo> iotList = iotService.getDeviceInfo(macList, entry.getKey());
|
||||||
device.setTotalElectriQuantity(deviceInfo.getW());
|
|
||||||
|
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 {
|
// 是否有WIFI
|
||||||
scheduledExecutorService.schedule(() -> {
|
if (ModelTag.hasTag(device.getModelTags(), ModelTag.WIFI)) {
|
||||||
iotService.setTotalEle(device, device.getTotalElectriQuantity().multiply(new BigDecimal(1000)));
|
device.setWifi(deviceInfo.getWifi());
|
||||||
}, 0, TimeUnit.SECONDS);
|
}
|
||||||
|
|
||||||
|
// 是否有电量
|
||||||
|
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 hasTime = device.getExpireTime() != null && device.getExpireTime().isAfter(now);
|
||||||
// // 若当前设备有电量,则正在使用
|
// // 若当前设备有电量,则正在使用
|
||||||
// boolean hasEle = device.getSurplusEle() != null && device.getSurplusEle().compareTo(BigDecimal.ZERO) > 0;
|
// boolean hasEle = device.getSurplusEle() != null && device.getSurplusEle().compareTo(BigDecimal.ZERO) > 0;
|
||||||
// 若开关开启,则正在使用
|
// 若开关开启,则正在使用
|
||||||
boolean hasOpen = DevicePowerStatus.ON.getStatus().equals(device.getPowerStatus());
|
boolean hasOpen = DevicePowerStatus.ON.getStatus().equals(device.getPowerStatus());
|
||||||
// 若设备有正在使用中的订单,则正在使用
|
// 若设备有正在使用中的订单,则正在使用
|
||||||
boolean hasUsingBill = device.getUsingBillCount() != null && device.getUsingBillCount() > 0;
|
boolean hasUsingBill = device.getUsingBillCount() != null && device.getUsingBillCount() > 0;
|
||||||
if (hasOpen || hasUsingBill) {
|
if (hasOpen || hasUsingBill) {
|
||||||
device.setStatus(DeviceStatus.USING.getStatus());
|
device.setStatus(DeviceStatus.USING.getStatus());
|
||||||
} else {
|
} else {
|
||||||
device.setStatus(DeviceStatus.NORMAL.getStatus());
|
device.setStatus(DeviceStatus.NORMAL.getStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 是否在线
|
// 是否在线
|
||||||
String onlineStatus1 = iotService.getOnlineStatus(device.getMac(), device.getProductId()).getStatus();
|
String onlineStatus1 = iotService.getOnlineStatus(device.getMac(), device.getProductId()).getStatus();
|
||||||
String onlineStatus2 = iotService.getOnlineStatus(device.getMac2(), device.getProductId()).getStatus();
|
String onlineStatus2 = iotService.getOnlineStatus(device.getMac2(), device.getProductId()).getStatus();
|
||||||
device.setOnlineStatus1(onlineStatus1);
|
device.setOnlineStatus1(onlineStatus1);
|
||||||
device.setOnlineStatus2(onlineStatus2);
|
device.setOnlineStatus2(onlineStatus2);
|
||||||
if (DeviceOnlineStatus.ONLINE.getStatus().equals(onlineStatus1) || DeviceOnlineStatus.ONLINE.getStatus().equals(onlineStatus2)) {
|
if (DeviceOnlineStatus.ONLINE.getStatus().equals(onlineStatus1) || DeviceOnlineStatus.ONLINE.getStatus().equals(onlineStatus2)) {
|
||||||
device.setOnlineStatus(DeviceOnlineStatus.ONLINE.getStatus());
|
device.setOnlineStatus(DeviceOnlineStatus.ONLINE.getStatus());
|
||||||
device.setLastOnlineTime(now);
|
device.setLastOnlineTime(now);
|
||||||
} else {
|
} else {
|
||||||
device.setOnlineStatus(DeviceOnlineStatus.OFFLINE.getStatus());
|
device.setOnlineStatus(DeviceOnlineStatus.OFFLINE.getStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
this.updateIotInfo(device);
|
this.updateIotInfo(device);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("更新设备信息失败,deviceId:{}", device.getDeviceId(), e);
|
log.error("更新设备信息失败,deviceId:{}, {}", device.getDeviceId(), e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user