Commit 95b1345882381413bab872c38a57fec7f8fdd845
1 parent
5754c9a2
【修复】IoT 设备属性上报:兼容 identifier 大小写不一致;BOOL 类型支持字符串 / 数字
- saveDeviceProperty 改用忽略大小写匹配物模型,统一以 identifier 作为 properties / Redis 缓存的 key,
避免设备上报 key 与物模型 identifier 大小写不一致时被丢弃,并与 Controller 读取 Redis 时使用的 key 对齐
- BOOL 类型先按布尔语义解析再转 0/1,避免 Convert.toByte("true") 走 Byte.parseByte 抛 NumberFormatException
- 新增 IotDevicePropertyServiceImplTest,覆盖大小写匹配 / BOOL 各种入参形态
Showing
2 changed files
with
217 additions
and
9 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/property/IotDevicePropertyServiceImpl.java
| @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; | @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; | ||
| 4 | import cn.hutool.core.convert.Convert; | 4 | import cn.hutool.core.convert.Convert; |
| 5 | import cn.hutool.core.date.LocalDateTimeUtil; | 5 | import cn.hutool.core.date.LocalDateTimeUtil; |
| 6 | import cn.hutool.core.map.MapUtil; | 6 | import cn.hutool.core.map.MapUtil; |
| 7 | +import cn.hutool.core.util.ObjUtil; | ||
| 7 | import cn.hutool.core.util.StrUtil; | 8 | import cn.hutool.core.util.StrUtil; |
| 8 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | 9 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 9 | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | 10 | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; |
| @@ -149,26 +150,29 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService { | @@ -149,26 +150,29 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService { | ||
| 149 | List<IotThingModelDO> thingModels = thingModelService.getThingModelListByProductIdFromCache(device.getProductId()); | 150 | List<IotThingModelDO> thingModels = thingModelService.getThingModelListByProductIdFromCache(device.getProductId()); |
| 150 | Map<String, Object> properties = new HashMap<>(); | 151 | Map<String, Object> properties = new HashMap<>(); |
| 151 | params.forEach((key, value) -> { | 152 | params.forEach((key, value) -> { |
| 152 | - IotThingModelDO thingModel = CollUtil.findOne(thingModels, o -> o.getIdentifier().equals(key)); | 153 | + // 忽略大小写匹配物模型,避免设备上报的 key 与 identifier 大小写不一致导致丢失 |
| 154 | + IotThingModelDO thingModel = CollUtil.findOne(thingModels, | ||
| 155 | + o -> StrUtil.equalsIgnoreCase(o.getIdentifier(), (CharSequence) key)); | ||
| 153 | if (thingModel == null || thingModel.getProperty() == null) { | 156 | if (thingModel == null || thingModel.getProperty() == null) { |
| 154 | log.error("[saveDeviceProperty][消息({}) 的属性({}) 不存在]", message, key); | 157 | log.error("[saveDeviceProperty][消息({}) 的属性({}) 不存在]", message, key); |
| 155 | return; | 158 | return; |
| 156 | } | 159 | } |
| 160 | + String identifier = thingModel.getIdentifier(); // 统一以物模型 identifier 作为 key,避免大小写问题 | ||
| 157 | String dataType = thingModel.getProperty().getDataType(); | 161 | String dataType = thingModel.getProperty().getDataType(); |
| 158 | if (ObjectUtils.equalsAny(dataType, | 162 | if (ObjectUtils.equalsAny(dataType, |
| 159 | IotDataSpecsDataTypeEnum.STRUCT.getDataType(), IotDataSpecsDataTypeEnum.ARRAY.getDataType())) { | 163 | IotDataSpecsDataTypeEnum.STRUCT.getDataType(), IotDataSpecsDataTypeEnum.ARRAY.getDataType())) { |
| 160 | // 特殊:STRUCT 和 ARRAY 类型,在 TDengine 里,有没对应数据类型,只能通过 JSON 来存储 | 164 | // 特殊:STRUCT 和 ARRAY 类型,在 TDengine 里,有没对应数据类型,只能通过 JSON 来存储 |
| 161 | - properties.put((String) key, JsonUtils.toJsonString(value)); | 165 | + properties.put(identifier, JsonUtils.toJsonString(value)); |
| 162 | } else if (IotDataSpecsDataTypeEnum.INT.getDataType().equals(dataType)) { | 166 | } else if (IotDataSpecsDataTypeEnum.INT.getDataType().equals(dataType)) { |
| 163 | - properties.put((String) key, Convert.toInt(value)); | 167 | + properties.put(identifier, Convert.toInt(value)); |
| 164 | } else if (IotDataSpecsDataTypeEnum.FLOAT.getDataType().equals(dataType)) { | 168 | } else if (IotDataSpecsDataTypeEnum.FLOAT.getDataType().equals(dataType)) { |
| 165 | - properties.put((String) key, Convert.toFloat(value)); | 169 | + properties.put(identifier, Convert.toFloat(value)); |
| 166 | } else if (IotDataSpecsDataTypeEnum.DOUBLE.getDataType().equals(dataType)) { | 170 | } else if (IotDataSpecsDataTypeEnum.DOUBLE.getDataType().equals(dataType)) { |
| 167 | - properties.put((String) key, Convert.toDouble(value)); | ||
| 168 | - } else if (IotDataSpecsDataTypeEnum.BOOL.getDataType().equals(dataType)) { | ||
| 169 | - properties.put((String) key, Convert.toByte(value)); | ||
| 170 | - } else { | ||
| 171 | - properties.put((String) key, value); | 171 | + properties.put(identifier, Convert.toDouble(value)); |
| 172 | + } else if (IotDataSpecsDataTypeEnum.BOOL.getDataType().equals(dataType)) { | ||
| 173 | + properties.put(identifier, Convert.toBool(value, false) ? (byte) 1 : (byte) 0); | ||
| 174 | + } else { | ||
| 175 | + properties.put(identifier, value); | ||
| 172 | } | 176 | } |
| 173 | }); | 177 | }); |
| 174 | if (CollUtil.isEmpty(properties)) { | 178 | if (CollUtil.isEmpty(properties)) { |
yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/device/property/IotDevicePropertyServiceImplTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.service.device.property; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | ||
| 4 | +import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum; | ||
| 5 | +import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; | ||
| 6 | +import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; | ||
| 7 | +import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDevicePropertyDO; | ||
| 8 | +import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO; | ||
| 9 | +import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.model.ThingModelProperty; | ||
| 10 | +import cn.iocoder.yudao.module.iot.dal.redis.device.DevicePropertyRedisDAO; | ||
| 11 | +import cn.iocoder.yudao.module.iot.dal.tdengine.IotDevicePropertyMapper; | ||
| 12 | +import cn.iocoder.yudao.module.iot.enums.thingmodel.IotDataSpecsDataTypeEnum; | ||
| 13 | +import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService; | ||
| 14 | +import org.junit.jupiter.api.Test; | ||
| 15 | +import org.mockito.ArgumentCaptor; | ||
| 16 | +import org.mockito.InjectMocks; | ||
| 17 | +import org.mockito.Mock; | ||
| 18 | + | ||
| 19 | +import java.time.LocalDateTime; | ||
| 20 | +import java.util.HashMap; | ||
| 21 | +import java.util.Map; | ||
| 22 | + | ||
| 23 | +import static java.util.Collections.singletonList; | ||
| 24 | +import static org.junit.jupiter.api.Assertions.*; | ||
| 25 | +import static org.mockito.ArgumentMatchers.*; | ||
| 26 | +import static org.mockito.Mockito.*; | ||
| 27 | + | ||
| 28 | +/** | ||
| 29 | + * {@link IotDevicePropertyServiceImpl} 的单元测试 | ||
| 30 | + * | ||
| 31 | + * @author 芋道源码 | ||
| 32 | + */ | ||
| 33 | +public class IotDevicePropertyServiceImplTest extends BaseMockitoUnitTest { | ||
| 34 | + | ||
| 35 | + @InjectMocks | ||
| 36 | + private IotDevicePropertyServiceImpl service; | ||
| 37 | + | ||
| 38 | + @Mock | ||
| 39 | + private IotThingModelService thingModelService; | ||
| 40 | + @Mock | ||
| 41 | + private IotDevicePropertyMapper devicePropertyMapper; | ||
| 42 | + @Mock | ||
| 43 | + private DevicePropertyRedisDAO deviceDataRedisDAO; | ||
| 44 | + | ||
| 45 | + @Test | ||
| 46 | + public void testSaveDeviceProperty_identifierCaseInsensitive() { | ||
| 47 | + // 准备参数:物模型 identifier 是 "LightStatus",设备上报的 key 是 "LIGHTSTATUS"(全大写) | ||
| 48 | + IotDeviceDO device = buildDevice(); | ||
| 49 | + IotThingModelDO thingModel = buildThingModel("LightStatus", IotDataSpecsDataTypeEnum.INT.getDataType()); | ||
| 50 | + Map<String, Object> params = new HashMap<>(); | ||
| 51 | + params.put("LIGHTSTATUS", 100); | ||
| 52 | + IotDeviceMessage message = buildMessage(params); | ||
| 53 | + | ||
| 54 | + // mock 行为 | ||
| 55 | + when(thingModelService.getThingModelListByProductIdFromCache(device.getProductId())) | ||
| 56 | + .thenReturn(singletonList(thingModel)); | ||
| 57 | + | ||
| 58 | + // 调用 | ||
| 59 | + service.saveDeviceProperty(device, message); | ||
| 60 | + | ||
| 61 | + // 断言:properties 落库 / 入缓存时 key 应为物模型 identifier "LightStatus",而不是上报的 "LIGHTSTATUS" | ||
| 62 | + Map<String, Object> dbProperties = captureMapperInsertProperties(); | ||
| 63 | + assertTrue(dbProperties.containsKey("LightStatus")); | ||
| 64 | + assertFalse(dbProperties.containsKey("LIGHTSTATUS")); | ||
| 65 | + assertEquals(100, dbProperties.get("LightStatus")); | ||
| 66 | + | ||
| 67 | + Map<String, IotDevicePropertyDO> redisProperties = captureRedisPutAllProperties(device.getId()); | ||
| 68 | + assertTrue(redisProperties.containsKey("LightStatus")); | ||
| 69 | + assertFalse(redisProperties.containsKey("LIGHTSTATUS")); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + @Test | ||
| 73 | + public void testSaveDeviceProperty_identifierNotInThingModel() { | ||
| 74 | + // 准备参数:上报的 key 在物模型里完全不存在(连忽略大小写都匹配不到) | ||
| 75 | + IotDeviceDO device = buildDevice(); | ||
| 76 | + IotThingModelDO thingModel = buildThingModel("LightStatus", IotDataSpecsDataTypeEnum.INT.getDataType()); | ||
| 77 | + Map<String, Object> params = new HashMap<>(); | ||
| 78 | + params.put("UnknownProperty", 1); | ||
| 79 | + IotDeviceMessage message = buildMessage(params); | ||
| 80 | + | ||
| 81 | + // mock 行为 | ||
| 82 | + when(thingModelService.getThingModelListByProductIdFromCache(device.getProductId())) | ||
| 83 | + .thenReturn(singletonList(thingModel)); | ||
| 84 | + | ||
| 85 | + // 调用 | ||
| 86 | + service.saveDeviceProperty(device, message); | ||
| 87 | + | ||
| 88 | + // 断言:没有合法属性,不会写入 TDengine 与 Redis | ||
| 89 | + verify(devicePropertyMapper, never()).insert(any(), any(), anyLong()); | ||
| 90 | + verify(deviceDataRedisDAO, never()).putAll(anyLong(), any()); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + @Test | ||
| 94 | + public void testSaveDeviceProperty_boolFromBooleanTrue() { | ||
| 95 | + // 准备参数:物模型为 BOOL,设备上报原生 boolean true | ||
| 96 | + assertBoolValueConvertedToByte(true, (byte) 1); | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + @Test | ||
| 100 | + public void testSaveDeviceProperty_boolFromBooleanFalse() { | ||
| 101 | + // 准备参数:物模型为 BOOL,设备上报原生 boolean false | ||
| 102 | + assertBoolValueConvertedToByte(false, (byte) 0); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + @Test | ||
| 106 | + public void testSaveDeviceProperty_boolFromStringTrue() { | ||
| 107 | + // 准备参数:物模型为 BOOL,设备上报字符串 "true" | ||
| 108 | + assertBoolValueConvertedToByte("true", (byte) 1); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + @Test | ||
| 112 | + public void testSaveDeviceProperty_boolFromStringFalse() { | ||
| 113 | + // 准备参数:物模型为 BOOL,设备上报字符串 "false" | ||
| 114 | + assertBoolValueConvertedToByte("false", (byte) 0); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + @Test | ||
| 118 | + public void testSaveDeviceProperty_boolFromNumberOne() { | ||
| 119 | + // 准备参数:物模型为 BOOL,设备上报数字 1 | ||
| 120 | + assertBoolValueConvertedToByte(1, (byte) 1); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + @Test | ||
| 124 | + public void testSaveDeviceProperty_boolFromNumberZero() { | ||
| 125 | + // 准备参数:物模型为 BOOL,设备上报数字 0 | ||
| 126 | + assertBoolValueConvertedToByte(0, (byte) 0); | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + /** | ||
| 130 | + * 校验 BOOL 类型属性上报后,最终落到 properties Map 的值类型与数值 | ||
| 131 | + */ | ||
| 132 | + private void assertBoolValueConvertedToByte(Object reportedValue, byte expected) { | ||
| 133 | + // 准备参数 | ||
| 134 | + IotDeviceDO device = buildDevice(); | ||
| 135 | + IotThingModelDO thingModel = buildThingModel("PowerSwitch", IotDataSpecsDataTypeEnum.BOOL.getDataType()); | ||
| 136 | + Map<String, Object> params = new HashMap<>(); | ||
| 137 | + params.put("PowerSwitch", reportedValue); | ||
| 138 | + IotDeviceMessage message = buildMessage(params); | ||
| 139 | + | ||
| 140 | + // mock 行为 | ||
| 141 | + when(thingModelService.getThingModelListByProductIdFromCache(device.getProductId())) | ||
| 142 | + .thenReturn(singletonList(thingModel)); | ||
| 143 | + | ||
| 144 | + // 调用:不能抛异常 | ||
| 145 | + assertDoesNotThrow(() -> service.saveDeviceProperty(device, message)); | ||
| 146 | + | ||
| 147 | + // 断言:写入的 value 是 byte 类型,且值匹配 | ||
| 148 | + Map<String, Object> dbProperties = captureMapperInsertProperties(); | ||
| 149 | + Object actual = dbProperties.get("PowerSwitch"); | ||
| 150 | + assertTrue(actual instanceof Byte, "BOOL 属性应被转为 Byte 类型,实际为 " + (actual == null ? "null" : actual.getClass())); | ||
| 151 | + assertEquals(expected, actual); | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + // ========== 辅助方法 ========== | ||
| 155 | + | ||
| 156 | + /** | ||
| 157 | + * 构造一个最简 IotDeviceDO,只设置测试需要的 id 与 productId | ||
| 158 | + */ | ||
| 159 | + private IotDeviceDO buildDevice() { | ||
| 160 | + return IotDeviceDO.builder().id(1L).productId(2L).build(); | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + /** | ||
| 164 | + * 构造物模型;只填 saveDeviceProperty 链路用到的 identifier + property.dataType | ||
| 165 | + */ | ||
| 166 | + private IotThingModelDO buildThingModel(String identifier, String dataType) { | ||
| 167 | + ThingModelProperty property = new ThingModelProperty(); | ||
| 168 | + property.setIdentifier(identifier); | ||
| 169 | + property.setDataType(dataType); | ||
| 170 | + return IotThingModelDO.builder().identifier(identifier).property(property).build(); | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + /** | ||
| 174 | + * 构造一条属性上报消息 | ||
| 175 | + */ | ||
| 176 | + private IotDeviceMessage buildMessage(Map<String, Object> params) { | ||
| 177 | + IotDeviceMessage message = new IotDeviceMessage(); | ||
| 178 | + message.setMethod(IotDeviceMessageMethodEnum.PROPERTY_POST.getMethod()); | ||
| 179 | + message.setParams(params); | ||
| 180 | + message.setReportTime(LocalDateTime.now()); | ||
| 181 | + return message; | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | + /** | ||
| 185 | + * 抓取 mapper.insert 的 properties 入参 | ||
| 186 | + */ | ||
| 187 | + @SuppressWarnings("unchecked") | ||
| 188 | + private Map<String, Object> captureMapperInsertProperties() { | ||
| 189 | + ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class); | ||
| 190 | + verify(devicePropertyMapper).insert(any(IotDeviceDO.class), captor.capture(), anyLong()); | ||
| 191 | + return captor.getValue(); | ||
| 192 | + } | ||
| 193 | + | ||
| 194 | + /** | ||
| 195 | + * 抓取 redisDAO.putAll 的 properties 入参 | ||
| 196 | + */ | ||
| 197 | + @SuppressWarnings("unchecked") | ||
| 198 | + private Map<String, IotDevicePropertyDO> captureRedisPutAllProperties(Long deviceId) { | ||
| 199 | + ArgumentCaptor<Map<String, IotDevicePropertyDO>> captor = ArgumentCaptor.forClass(Map.class); | ||
| 200 | + verify(deviceDataRedisDAO).putAll(eq(deviceId), captor.capture()); | ||
| 201 | + return captor.getValue(); | ||
| 202 | + } | ||
| 203 | + | ||
| 204 | +} |