Merge remote-tracking branch 'origin/master' into dev

# Conflicts:
#	smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotReceiveServiceImpl.java
#	smart-switch-service/src/main/java/com/ruoyi/iot/service/impl/IotServiceImpl.java
This commit is contained in:
墨大叔 2024-09-29 10:35:59 +08:00
commit 3960f10abf
7 changed files with 139 additions and 85 deletions

View File

@ -1,15 +1,7 @@
package com.ruoyi.iot.domain; 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 lombok.Data;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
@ -21,60 +13,4 @@ public class CurrentDeviceData {
private String id; private String id;
private String title; private String title;
private List<CurrentDatastream> datastreams; private List<CurrentDatastream> 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;
}
} }

View File

@ -16,10 +16,6 @@ import java.util.Date;
@Data @Data
@Builder @Builder
public class IotDeviceInfo { public class IotDeviceInfo {
private String id; // iot设备id
private String mac; // 设备mac
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date at; // 时间 private Date at; // 时间

View File

@ -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);
}

View File

@ -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<CurrentDatastream> ds1 = data1 == null ? data2.getDatastreams() : data1.getDatastreams();
List<CurrentDatastream> 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<CurrentDatastream> ds1, List<CurrentDatastream> 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;
}
}

View File

@ -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.RedisLock;
import com.ruoyi.common.core.redis.enums.RedisLockKey; import com.ruoyi.common.core.redis.enums.RedisLockKey;
import com.ruoyi.common.utils.NumberUtils; 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.domain.response.CommandResponse;
import com.ruoyi.iot.enums.ReceiveStatus; import com.ruoyi.iot.enums.ReceiveStatus;
import com.ruoyi.iot.enums.ReceiveType; 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.Device;
import com.ruoyi.ss.device.domain.enums.DeviceOutageWay; import com.ruoyi.ss.device.domain.enums.DeviceOutageWay;
import com.ruoyi.ss.device.domain.vo.DeviceVO; 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.suit.domain.enums.SuitFeeType;
import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery; import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillStatus; 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.domain.vo.TransactionBillVO;
import com.ruoyi.ss.transactionBill.service.TransactionAssembler; import com.ruoyi.ss.transactionBill.service.TransactionAssembler;
import com.ruoyi.ss.transactionBill.service.TransactionBillService; import com.ruoyi.ss.transactionBill.service.TransactionBillService;
@ -38,7 +39,7 @@ import java.util.List;
*/ */
@Service @Service
@Slf4j @Slf4j
public class IotReceiveServiceImpl implements IotReceiveService{ public class IotReceiveServiceImpl implements IotReceiveService {
@Autowired @Autowired
private DeviceService deviceService; private DeviceService deviceService;

View File

@ -1,4 +1,4 @@
package com.ruoyi.iot.service; package com.ruoyi.iot.service.impl;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject; 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.domain.response.HistoryDataPointResponse;
import com.ruoyi.iot.enums.IotHttpStatus; import com.ruoyi.iot.enums.IotHttpStatus;
import com.ruoyi.iot.interfaces.IotDevice; 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.enums.DeviceOnlineStatus;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -58,6 +61,9 @@ public class IotServiceImpl implements IotService {
@Value(value = "${debug}") @Value(value = "${debug}")
private Boolean debug; private Boolean debug;
@Autowired
private IotConverter iotConverter;
// 查询OneNet设备在线状态 // 查询OneNet设备在线状态
@Override @Override
public DeviceOnlineStatus getOnlineStatus(String deviceName, String productId) { public DeviceOnlineStatus getOnlineStatus(String deviceName, String productId) {
@ -318,31 +324,26 @@ public class IotServiceImpl implements IotService {
@Override @Override
public IotDeviceInfo getDeviceInfo(String deviceName, String productId) { public IotDeviceInfo getDeviceInfo(String deviceName, String productId) {
if (StringUtils.isBlank(deviceName)) { if (StringUtils.isAnyBlank(deviceName, productId)) {
return null; return null;
} }
CurrentDeviceData currentDataPoint = getCurrentDataPoint(deviceName, productId); CurrentDeviceData currentDataPoint = getCurrentDataPoint(deviceName, productId);
if (currentDataPoint == null) { if (currentDataPoint == null) {
return null; return null;
} }
return currentDataPoint.parseDeviceInfo(); return iotConverter.toIotDeviceInfo(currentDataPoint, null);
} }
@Override @Override
public IotDeviceInfo getDeviceInfo(IotDevice device) { public IotDeviceInfo getDeviceInfo(IotDevice device) {
IotDeviceInfo info = null;
if (device == null || StringUtils.isBlank(device.getProductId())) { if (device == null || StringUtils.isBlank(device.getProductId())) {
return info; return null;
} }
if (StringUtils.hasText(device.iotMac1())) { CurrentDeviceData data1 = getCurrentDataPoint(device.getMac1(), device.getProductId());
info = getDeviceInfo(device.iotMac1(), device.getProductId()); CurrentDeviceData data2 = getCurrentDataPoint(device.getMac2(), device.getProductId());
}
if (info == null && StringUtils.hasText(device.iotMac2())) {
info = getDeviceInfo(device.iotMac2(), device.getProductId());
}
return info; return iotConverter.toIotDeviceInfo(data1, data2);
} }
@Override @Override
@ -355,7 +356,7 @@ public class IotServiceImpl implements IotService {
return Collections.emptyList(); return Collections.emptyList();
} }
return dataList.stream() return dataList.stream()
.map(CurrentDeviceData::parseDeviceInfo) .map(item -> iotConverter.toIotDeviceInfo(item, null))
.filter(Objects::nonNull) .filter(Objects::nonNull)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

View File

@ -145,7 +145,7 @@ public class SmMeterReadingRecordServiceImpl implements ISmMeterReadingRecordSer
// 更新数据 // 更新数据
for (DeviceVO device : list) { for (DeviceVO device : list) {
try { try {
if (StringUtils.isAnyBlank(device.getMac(), device.getModelProductId())) { if (device == null) {
continue; continue;
} }
IotDeviceInfo deviceInfo = iotService.getDeviceInfo(device); IotDeviceInfo deviceInfo = iotService.getDeviceInfo(device);