Commit 64fefaa6305027762e4836ae06dae3ebac961429
1 parent
061819f2
修改:IOT 序列化报错问题处理、请求示例修改
Showing
3 changed files
with
57 additions
and
20 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.http
| ... | ... | @@ -146,9 +146,9 @@ tenant-id: {{adminTenentId}} |
| 146 | 146 | Authorization: Bearer {{token}} |
| 147 | 147 | |
| 148 | 148 | { |
| 149 | - "id": 1, | |
| 150 | - "productId": 1001, | |
| 151 | - "productKey": "smart-sensor-001", | |
| 149 | + "id": 3, | |
| 150 | + "productId": 1002, | |
| 151 | + "productKey": "smart-sensor-002", | |
| 152 | 152 | "properties": [ |
| 153 | 153 | { |
| 154 | 154 | "identifier": "Temperature", |
| ... | ... | @@ -158,8 +158,8 @@ Authorization: Bearer {{token}} |
| 158 | 158 | "dataType": { |
| 159 | 159 | "type": "float", |
| 160 | 160 | "specs": { |
| 161 | - "min": -40.0, | |
| 162 | - "max": 125.0, | |
| 161 | + "min": -100.0, | |
| 162 | + "max": 200.0, | |
| 163 | 163 | "step": 0.1, |
| 164 | 164 | "unit": "℃" |
| 165 | 165 | } |
| ... | ... | @@ -229,7 +229,8 @@ Authorization: Bearer {{token}} |
| 229 | 229 | "name": "重启设备", |
| 230 | 230 | "callType": "async", |
| 231 | 231 | "inputData": [], |
| 232 | - "description": "远程重启设备" | |
| 232 | + "description": "远程重启设备", | |
| 233 | + "method": "thing.service.reboot" | |
| 233 | 234 | }, |
| 234 | 235 | { |
| 235 | 236 | "identifier": "SetThreshold", |
| ... | ... | @@ -251,7 +252,8 @@ Authorization: Bearer {{token}} |
| 251 | 252 | "description": "报警温度阈值" |
| 252 | 253 | } |
| 253 | 254 | ], |
| 254 | - "description": "设置设备的温度报警阈值" | |
| 255 | + "description": "设置设备的温度报警阈值", | |
| 256 | + "method": "thing.service.setThreshold" | |
| 255 | 257 | } |
| 256 | 258 | ], |
| 257 | 259 | "events": [ |
| ... | ... | @@ -272,13 +274,14 @@ Authorization: Bearer {{token}} |
| 272 | 274 | "description": "触发报警时的温度值" |
| 273 | 275 | } |
| 274 | 276 | ], |
| 275 | - "description": "当温度超过阈值时触发高温报警事件" | |
| 277 | + "description": "当温度超过阈值时触发高温报警事件", | |
| 278 | + "method": "thing.event.highTemperatureAlert" | |
| 276 | 279 | } |
| 277 | 280 | ] |
| 278 | 281 | } |
| 279 | 282 | |
| 280 | 283 | |
| 281 | 284 | ### 请求 /iot/think-model-function/get-by-product-key 接口 => 成功 |
| 282 | -GET {{baseUrl}}/iot/think-model-function/get-by-product-key?productKey=123456 | |
| 285 | +GET {{baseUrl}}/iot/think-model-function/get-by-product-key?productKey=smart-sensor-002 | |
| 283 | 286 | tenant-id: {{adminTenentId}} |
| 284 | 287 | Authorization: Bearer {{token}} |
| 285 | 288 | \ No newline at end of file | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/convert/thinkmodelfunction/IotThinkModelFunctionConvert.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.convert.thinkmodelfunction; |
| 2 | 2 | |
| 3 | -import cn.hutool.json.JSONUtil; | |
| 4 | 3 | import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelEvent; |
| 5 | 4 | import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelProperty; |
| 6 | 5 | import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelService; |
| 7 | 6 | import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionRespVO; |
| 8 | 7 | import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionSaveReqVO; |
| 9 | 8 | import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO; |
| 9 | +import com.fasterxml.jackson.core.JsonProcessingException; | |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
| 10 | 11 | import org.mapstruct.Mapper; |
| 11 | 12 | import org.mapstruct.Mapping; |
| 12 | 13 | import org.mapstruct.factory.Mappers; |
| ... | ... | @@ -19,6 +20,8 @@ public interface IotThinkModelFunctionConvert { |
| 19 | 20 | |
| 20 | 21 | IotThinkModelFunctionConvert INSTANCE = Mappers.getMapper(IotThinkModelFunctionConvert.class); |
| 21 | 22 | |
| 23 | + ObjectMapper objectMapper = new ObjectMapper(); | |
| 24 | + | |
| 22 | 25 | // 将 SaveReqVO 转换为 DO 对象,处理 properties, services, events 字段 |
| 23 | 26 | @Mapping(target = "properties", expression = "java(convertPropertiesToJson(bean.getProperties()))") |
| 24 | 27 | @Mapping(target = "services", expression = "java(convertServicesToJson(bean.getServices()))") |
| ... | ... | @@ -26,15 +29,27 @@ public interface IotThinkModelFunctionConvert { |
| 26 | 29 | IotThinkModelFunctionDO convert(IotThinkModelFunctionSaveReqVO bean); |
| 27 | 30 | |
| 28 | 31 | default String convertPropertiesToJson(List<ThingModelProperty> properties) { |
| 29 | - return properties != null ? JSONUtil.toJsonStr(properties) : "[]"; | |
| 32 | + try { | |
| 33 | + return properties != null ? objectMapper.writeValueAsString(properties) : "[]"; | |
| 34 | + } catch (JsonProcessingException e) { | |
| 35 | + throw new RuntimeException("序列化 properties 时发生错误", e); | |
| 36 | + } | |
| 30 | 37 | } |
| 31 | 38 | |
| 32 | 39 | default String convertServicesToJson(List<ThingModelService> services) { |
| 33 | - return services != null ? JSONUtil.toJsonStr(services) : "[]"; | |
| 40 | + try { | |
| 41 | + return services != null ? objectMapper.writeValueAsString(services) : "[]"; | |
| 42 | + } catch (JsonProcessingException e) { | |
| 43 | + throw new RuntimeException("序列化 services 时发生错误", e); | |
| 44 | + } | |
| 34 | 45 | } |
| 35 | 46 | |
| 36 | 47 | default String convertEventsToJson(List<ThingModelEvent> events) { |
| 37 | - return events != null ? JSONUtil.toJsonStr(events) : "[]"; | |
| 48 | + try { | |
| 49 | + return events != null ? objectMapper.writeValueAsString(events) : "[]"; | |
| 50 | + } catch (JsonProcessingException e) { | |
| 51 | + throw new RuntimeException("序列化 events 时发生错误", e); | |
| 52 | + } | |
| 38 | 53 | } |
| 39 | 54 | |
| 40 | 55 | // 将 DO 转换为 RespVO 对象,处理 properties, services, events 字段 |
| ... | ... | @@ -44,18 +59,29 @@ public interface IotThinkModelFunctionConvert { |
| 44 | 59 | IotThinkModelFunctionRespVO convert(IotThinkModelFunctionDO bean); |
| 45 | 60 | |
| 46 | 61 | default List<ThingModelProperty> convertJsonToProperties(String propertiesJson) { |
| 47 | - return propertiesJson != null ? JSONUtil.toList(propertiesJson, ThingModelProperty.class) : new ArrayList<>(); | |
| 62 | + try { | |
| 63 | + return propertiesJson != null ? objectMapper.readValue(propertiesJson, objectMapper.getTypeFactory().constructCollectionType(List.class, ThingModelProperty.class)) : new ArrayList<>(); | |
| 64 | + } catch (JsonProcessingException e) { | |
| 65 | + throw new RuntimeException("反序列化 properties 时发生错误", e); | |
| 66 | + } | |
| 48 | 67 | } |
| 49 | 68 | |
| 50 | 69 | default List<ThingModelService> convertJsonToServices(String servicesJson) { |
| 51 | - return servicesJson != null ? JSONUtil.toList(servicesJson, ThingModelService.class) : new ArrayList<>(); | |
| 70 | + try { | |
| 71 | + return servicesJson != null ? objectMapper.readValue(servicesJson, objectMapper.getTypeFactory().constructCollectionType(List.class, ThingModelService.class)) : new ArrayList<>(); | |
| 72 | + } catch (JsonProcessingException e) { | |
| 73 | + throw new RuntimeException("反序列化 services 时发生错误", e); | |
| 74 | + } | |
| 52 | 75 | } |
| 53 | 76 | |
| 54 | 77 | default List<ThingModelEvent> convertJsonToEvents(String eventsJson) { |
| 55 | - return eventsJson != null ? JSONUtil.toList(eventsJson, ThingModelEvent.class) : new ArrayList<>(); | |
| 78 | + try { | |
| 79 | + return eventsJson != null ? objectMapper.readValue(eventsJson, objectMapper.getTypeFactory().constructCollectionType(List.class, ThingModelEvent.class)) : new ArrayList<>(); | |
| 80 | + } catch (JsonProcessingException e) { | |
| 81 | + throw new RuntimeException("反序列化 events 时发生错误", e); | |
| 82 | + } | |
| 56 | 83 | } |
| 57 | 84 | |
| 58 | 85 | // 批量转换 DO 列表到 RespVO 列表 |
| 59 | 86 | List<IotThinkModelFunctionRespVO> convertList(List<IotThinkModelFunctionDO> list); |
| 60 | -} | |
| 61 | - | |
| 87 | +} | |
| 62 | 88 | \ No newline at end of file | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java
| ... | ... | @@ -6,6 +6,8 @@ import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThi |
| 6 | 6 | import cn.iocoder.yudao.module.iot.convert.thinkmodelfunction.IotThinkModelFunctionConvert; |
| 7 | 7 | import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO; |
| 8 | 8 | import cn.iocoder.yudao.module.iot.dal.mysql.thinkmodelfunction.IotThinkModelFunctionMapper; |
| 9 | +import com.fasterxml.jackson.core.JsonProcessingException; | |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
| 9 | 11 | import jakarta.annotation.Resource; |
| 10 | 12 | import lombok.extern.slf4j.Slf4j; |
| 11 | 13 | import org.springframework.stereotype.Service; |
| ... | ... | @@ -27,6 +29,8 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe |
| 27 | 29 | @Resource |
| 28 | 30 | private IotThinkModelFunctionMapper thinkModelFunctionMapper; |
| 29 | 31 | |
| 32 | + private ObjectMapper objectMapper = new ObjectMapper(); | |
| 33 | + | |
| 30 | 34 | @Override |
| 31 | 35 | public Long createThinkModelFunction(IotThinkModelFunctionSaveReqVO createReqVO) { |
| 32 | 36 | log.info("创建物模型,参数:{}", createReqVO); |
| ... | ... | @@ -121,8 +125,12 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe |
| 121 | 125 | updateServiceInList(existingServices, propertyGetService); |
| 122 | 126 | |
| 123 | 127 | // 更新 thinkModelFunction 对象的 events 和 services 字段 |
| 124 | - thinkModelFunction.setEvents(JSONUtil.toJsonStr(existingEvents)); | |
| 125 | - thinkModelFunction.setServices(JSONUtil.toJsonStr(existingServices)); | |
| 128 | + try { | |
| 129 | + thinkModelFunction.setEvents(objectMapper.writeValueAsString(existingEvents)); | |
| 130 | + thinkModelFunction.setServices(objectMapper.writeValueAsString(existingServices)); | |
| 131 | + } catch (JsonProcessingException e) { | |
| 132 | + throw new RuntimeException("序列化事件和服务时发生错误", e); | |
| 133 | + } | |
| 126 | 134 | } |
| 127 | 135 | |
| 128 | 136 | /** | ... | ... |