Commit 061819f25b16275b2ef97915f821dbf66cd673dd
1 parent
1f8576f6
修改:IOT 优化根据属性列表,自动生成属性上报事件和属性设置、获取服务
Showing
1 changed file
with
51 additions
and
14 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java
| ... | ... | @@ -96,7 +96,6 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
| 99 | - * @ TODO 还要再优化 | |
| 100 | 99 | * 根据属性列表,自动生成属性上报事件和属性设置、获取服务 |
| 101 | 100 | */ |
| 102 | 101 | private void generateDefaultEventsAndServices(IotThinkModelFunctionSaveReqVO reqVO, IotThinkModelFunctionDO thinkModelFunction) { |
| ... | ... | @@ -106,28 +105,66 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe |
| 106 | 105 | properties = new ArrayList<>(); |
| 107 | 106 | } |
| 108 | 107 | |
| 109 | - // 生成属性上报事件 | |
| 110 | - List<ThingModelEvent> events = reqVO.getEvents() != null ? new ArrayList<>(reqVO.getEvents()) : new ArrayList<>(); | |
| 108 | + // 获取现有的事件和服务 | |
| 109 | + List<ThingModelEvent> existingEvents = reqVO.getEvents() != null ? new ArrayList<>(reqVO.getEvents()) : new ArrayList<>(); | |
| 110 | + List<ThingModelService> existingServices = reqVO.getServices() != null ? new ArrayList<>(reqVO.getServices()) : new ArrayList<>(); | |
| 111 | + | |
| 112 | + // 生成或更新属性上报事件 | |
| 111 | 113 | ThingModelEvent propertyPostEvent = generatePropertyPostEvent(properties); |
| 112 | - events.add(propertyPostEvent); | |
| 114 | + updateEventInList(existingEvents, propertyPostEvent); | |
| 113 | 115 | |
| 114 | - // 生成属性设置和获取服务 | |
| 115 | - List<ThingModelService> services = reqVO.getServices() != null ? new ArrayList<>(reqVO.getServices()) : new ArrayList<>(); | |
| 116 | + // 生成或更新属性设置和获取服务 | |
| 116 | 117 | ThingModelService propertySetService = generatePropertySetService(properties); |
| 117 | - if (propertySetService != null) { | |
| 118 | - services.add(propertySetService); | |
| 119 | - } | |
| 118 | + updateServiceInList(existingServices, propertySetService); | |
| 119 | + | |
| 120 | 120 | ThingModelService propertyGetService = generatePropertyGetService(properties); |
| 121 | - if (propertyGetService != null) { | |
| 122 | - services.add(propertyGetService); | |
| 123 | - } | |
| 121 | + updateServiceInList(existingServices, propertyGetService); | |
| 124 | 122 | |
| 125 | 123 | // 更新 thinkModelFunction 对象的 events 和 services 字段 |
| 126 | - thinkModelFunction.setEvents(JSONUtil.toJsonStr(events)); | |
| 127 | - thinkModelFunction.setServices(JSONUtil.toJsonStr(services)); | |
| 124 | + thinkModelFunction.setEvents(JSONUtil.toJsonStr(existingEvents)); | |
| 125 | + thinkModelFunction.setServices(JSONUtil.toJsonStr(existingServices)); | |
| 128 | 126 | } |
| 129 | 127 | |
| 130 | 128 | /** |
| 129 | + * 在事件列表中更新或添加事件 | |
| 130 | + */ | |
| 131 | + private void updateEventInList(List<ThingModelEvent> events, ThingModelEvent newEvent) { | |
| 132 | + if (newEvent == null) { | |
| 133 | + return; | |
| 134 | + } | |
| 135 | + for (int i = 0; i < events.size(); i++) { | |
| 136 | + ThingModelEvent event = events.get(i); | |
| 137 | + if (event.getIdentifier().equals(newEvent.getIdentifier())) { | |
| 138 | + // 更新已有的事件 | |
| 139 | + events.set(i, newEvent); | |
| 140 | + return; | |
| 141 | + } | |
| 142 | + } | |
| 143 | + // 如果不存在,则添加新的事件 | |
| 144 | + events.add(newEvent); | |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * 在服务列表中更新或添加服务 | |
| 149 | + */ | |
| 150 | + private void updateServiceInList(List<ThingModelService> services, ThingModelService newService) { | |
| 151 | + if (newService == null) { | |
| 152 | + return; | |
| 153 | + } | |
| 154 | + for (int i = 0; i < services.size(); i++) { | |
| 155 | + ThingModelService service = services.get(i); | |
| 156 | + if (service.getIdentifier().equals(newService.getIdentifier())) { | |
| 157 | + // 更新已有的服务 | |
| 158 | + services.set(i, newService); | |
| 159 | + return; | |
| 160 | + } | |
| 161 | + } | |
| 162 | + // 如果不存在,则添加新的服务 | |
| 163 | + services.add(newService); | |
| 164 | + } | |
| 165 | + | |
| 166 | + | |
| 167 | + /** | |
| 131 | 168 | * 生成属性上报事件 |
| 132 | 169 | */ |
| 133 | 170 | private ThingModelEvent generatePropertyPostEvent(List<ThingModelProperty> properties) { | ... | ... |