修改:IOT 序列化报错问题处理、请求示例修改

This commit is contained in:
安浩浩 2024-09-14 08:59:55 +08:00
parent 061819f25b
commit 64fefaa630
3 changed files with 57 additions and 20 deletions

View File

@ -146,9 +146,9 @@ tenant-id: {{adminTenentId}}
Authorization: Bearer {{token}}
{
"id": 1,
"productId": 1001,
"productKey": "smart-sensor-001",
"id": 3,
"productId": 1002,
"productKey": "smart-sensor-002",
"properties": [
{
"identifier": "Temperature",
@ -158,8 +158,8 @@ Authorization: Bearer {{token}}
"dataType": {
"type": "float",
"specs": {
"min": -40.0,
"max": 125.0,
"min": -100.0,
"max": 200.0,
"step": 0.1,
"unit": "℃"
}
@ -229,7 +229,8 @@ Authorization: Bearer {{token}}
"name": "重启设备",
"callType": "async",
"inputData": [],
"description": "远程重启设备"
"description": "远程重启设备",
"method": "thing.service.reboot"
},
{
"identifier": "SetThreshold",
@ -251,7 +252,8 @@ Authorization: Bearer {{token}}
"description": "报警温度阈值"
}
],
"description": "设置设备的温度报警阈值"
"description": "设置设备的温度报警阈值",
"method": "thing.service.setThreshold"
}
],
"events": [
@ -272,13 +274,14 @@ Authorization: Bearer {{token}}
"description": "触发报警时的温度值"
}
],
"description": "当温度超过阈值时触发高温报警事件"
"description": "当温度超过阈值时触发高温报警事件",
"method": "thing.event.highTemperatureAlert"
}
]
}
### 请求 /iot/think-model-function/get-by-product-key 接口 => 成功
GET {{baseUrl}}/iot/think-model-function/get-by-product-key?productKey=123456
GET {{baseUrl}}/iot/think-model-function/get-by-product-key?productKey=smart-sensor-002
tenant-id: {{adminTenentId}}
Authorization: Bearer {{token}}

View File

@ -1,12 +1,13 @@
package cn.iocoder.yudao.module.iot.convert.thinkmodelfunction;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelEvent;
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelProperty;
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelService;
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
@ -19,6 +20,8 @@ public interface IotThinkModelFunctionConvert {
IotThinkModelFunctionConvert INSTANCE = Mappers.getMapper(IotThinkModelFunctionConvert.class);
ObjectMapper objectMapper = new ObjectMapper();
// SaveReqVO 转换为 DO 对象处理 properties, services, events 字段
@Mapping(target = "properties", expression = "java(convertPropertiesToJson(bean.getProperties()))")
@Mapping(target = "services", expression = "java(convertServicesToJson(bean.getServices()))")
@ -26,15 +29,27 @@ public interface IotThinkModelFunctionConvert {
IotThinkModelFunctionDO convert(IotThinkModelFunctionSaveReqVO bean);
default String convertPropertiesToJson(List<ThingModelProperty> properties) {
return properties != null ? JSONUtil.toJsonStr(properties) : "[]";
try {
return properties != null ? objectMapper.writeValueAsString(properties) : "[]";
} catch (JsonProcessingException e) {
throw new RuntimeException("序列化 properties 时发生错误", e);
}
}
default String convertServicesToJson(List<ThingModelService> services) {
return services != null ? JSONUtil.toJsonStr(services) : "[]";
try {
return services != null ? objectMapper.writeValueAsString(services) : "[]";
} catch (JsonProcessingException e) {
throw new RuntimeException("序列化 services 时发生错误", e);
}
}
default String convertEventsToJson(List<ThingModelEvent> events) {
return events != null ? JSONUtil.toJsonStr(events) : "[]";
try {
return events != null ? objectMapper.writeValueAsString(events) : "[]";
} catch (JsonProcessingException e) {
throw new RuntimeException("序列化 events 时发生错误", e);
}
}
// DO 转换为 RespVO 对象处理 properties, services, events 字段
@ -44,18 +59,29 @@ public interface IotThinkModelFunctionConvert {
IotThinkModelFunctionRespVO convert(IotThinkModelFunctionDO bean);
default List<ThingModelProperty> convertJsonToProperties(String propertiesJson) {
return propertiesJson != null ? JSONUtil.toList(propertiesJson, ThingModelProperty.class) : new ArrayList<>();
try {
return propertiesJson != null ? objectMapper.readValue(propertiesJson, objectMapper.getTypeFactory().constructCollectionType(List.class, ThingModelProperty.class)) : new ArrayList<>();
} catch (JsonProcessingException e) {
throw new RuntimeException("反序列化 properties 时发生错误", e);
}
}
default List<ThingModelService> convertJsonToServices(String servicesJson) {
return servicesJson != null ? JSONUtil.toList(servicesJson, ThingModelService.class) : new ArrayList<>();
try {
return servicesJson != null ? objectMapper.readValue(servicesJson, objectMapper.getTypeFactory().constructCollectionType(List.class, ThingModelService.class)) : new ArrayList<>();
} catch (JsonProcessingException e) {
throw new RuntimeException("反序列化 services 时发生错误", e);
}
}
default List<ThingModelEvent> convertJsonToEvents(String eventsJson) {
return eventsJson != null ? JSONUtil.toList(eventsJson, ThingModelEvent.class) : new ArrayList<>();
try {
return eventsJson != null ? objectMapper.readValue(eventsJson, objectMapper.getTypeFactory().constructCollectionType(List.class, ThingModelEvent.class)) : new ArrayList<>();
} catch (JsonProcessingException e) {
throw new RuntimeException("反序列化 events 时发生错误", e);
}
}
// 批量转换 DO 列表到 RespVO 列表
List<IotThinkModelFunctionRespVO> convertList(List<IotThinkModelFunctionDO> list);
}
}

View File

@ -6,6 +6,8 @@ import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThi
import cn.iocoder.yudao.module.iot.convert.thinkmodelfunction.IotThinkModelFunctionConvert;
import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO;
import cn.iocoder.yudao.module.iot.dal.mysql.thinkmodelfunction.IotThinkModelFunctionMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@ -27,6 +29,8 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
@Resource
private IotThinkModelFunctionMapper thinkModelFunctionMapper;
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public Long createThinkModelFunction(IotThinkModelFunctionSaveReqVO createReqVO) {
log.info("创建物模型,参数:{}", createReqVO);
@ -121,8 +125,12 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
updateServiceInList(existingServices, propertyGetService);
// 更新 thinkModelFunction 对象的 events services 字段
thinkModelFunction.setEvents(JSONUtil.toJsonStr(existingEvents));
thinkModelFunction.setServices(JSONUtil.toJsonStr(existingServices));
try {
thinkModelFunction.setEvents(objectMapper.writeValueAsString(existingEvents));
thinkModelFunction.setServices(objectMapper.writeValueAsString(existingServices));
} catch (JsonProcessingException e) {
throw new RuntimeException("序列化事件和服务时发生错误", e);
}
}
/**