Commit 401d65872d38e64751dd21d047d32ad4f3fd5e8b
Merge branch '20230418' into 'master_dev'
refactor: 场景联动的设备输出,兼容TCP协议。 See merge request yunteng/thingskit!177
Showing
2 changed files
with
63 additions
and
58 deletions
... | ... | @@ -161,4 +161,11 @@ public interface FastIotConstants { |
161 | 161 | /** 租户下的用户 */ |
162 | 162 | public static final int IS_CUSTOMER_USER = 3; |
163 | 163 | } |
164 | + class Rpc{ | |
165 | + | |
166 | + /**RPC方法名*/ | |
167 | + public static String METHOD_NAME = "method"; | |
168 | + /**RPC参数*/ | |
169 | + public static String PARAMS_NAME = "params"; | |
170 | + } | |
164 | 171 | } | ... | ... |
... | ... | @@ -28,8 +28,6 @@ import org.thingsboard.server.dao.yunteng.mapper.*; |
28 | 28 | import org.thingsboard.server.dao.yunteng.service.*; |
29 | 29 | |
30 | 30 | import java.util.*; |
31 | -import java.util.concurrent.Executors; | |
32 | -import java.util.concurrent.TimeUnit; | |
33 | 31 | import java.util.stream.Collectors; |
34 | 32 | |
35 | 33 | /** @Description 场景联动业务实现层 @Author cxy @Date 2021/11/25 11:22 */ |
... | ... | @@ -69,6 +67,19 @@ public class TkSceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageM |
69 | 67 | sceneLinkage.copyToDTO(sceneLinkageDTO); |
70 | 68 | } |
71 | 69 | |
70 | + updateScene(sceneLinkageDTO, tenantId, customerId, sceneLinkage); | |
71 | + | |
72 | + return sceneLinkageDTO; | |
73 | + } | |
74 | + | |
75 | + /** | |
76 | + * 更新场景联动附加信息,触发器、执行条件、动作 | |
77 | + * @param sceneLinkageDTO 场景联动表单数据 | |
78 | + * @param tenantId 租户ID | |
79 | + * @param customerId 客户ID | |
80 | + * @param sceneLinkage 场景联动主表实体 | |
81 | + */ | |
82 | + private void updateScene(SceneLinkageDTO sceneLinkageDTO, String tenantId, String customerId, TkSceneLinkageEntity sceneLinkage) { | |
72 | 83 | String organizationId = sceneLinkage.getOrganizationId(); |
73 | 84 | List<DeviceDTO> organizationDevices = findDeviceList(organizationId, tenantId, customerId,new ArrayList<>()); |
74 | 85 | |
... | ... | @@ -80,8 +91,6 @@ public class TkSceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageM |
80 | 91 | updateTrigger(sceneLinkageDTO, tbDeviceIds); |
81 | 92 | updateDoCondition(sceneLinkageDTO, tbDeviceIds); |
82 | 93 | updateDoAction(sceneLinkageDTO, tbDeviceIds); |
83 | - | |
84 | - return sceneLinkageDTO; | |
85 | 94 | } |
86 | 95 | |
87 | 96 | /** |
... | ... | @@ -135,17 +144,7 @@ public class TkSceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageM |
135 | 144 | if (!sceneLinkage.getCreator().equals(currentUserId)) { |
136 | 145 | throw new TkDataValidationException("你不是此场景的创建者"); |
137 | 146 | } |
138 | - String organizationId = sceneLinkage.getOrganizationId(); | |
139 | - List<DeviceDTO> organizationDevices = findDeviceList(organizationId, tenantId, customerId,new ArrayList<>()); | |
140 | - | |
141 | - List<String> tbDeviceIds = new ArrayList<>(); | |
142 | - for (DeviceDTO item : organizationDevices) { | |
143 | - tbDeviceIds.add(item.getTbDeviceId()); | |
144 | - } | |
145 | - | |
146 | - updateTrigger(sceneLinkageDTO, tbDeviceIds); | |
147 | - updateDoCondition(sceneLinkageDTO, tbDeviceIds); | |
148 | - updateDoAction(sceneLinkageDTO, tbDeviceIds); | |
147 | + updateScene(sceneLinkageDTO, tenantId, customerId, sceneLinkage); | |
149 | 148 | |
150 | 149 | sceneLinkageDTO.copyToEntity(sceneLinkage); |
151 | 150 | sceneLinkage.setTenantId(tenantId); |
... | ... | @@ -183,7 +182,7 @@ public class TkSceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageM |
183 | 182 | } |
184 | 183 | |
185 | 184 | // 先删除触发器 |
186 | - int result = triggerMapper.delete( | |
185 | + triggerMapper.delete( | |
187 | 186 | new QueryWrapper<TkTriggerEntity>() |
188 | 187 | .lambda() |
189 | 188 | .eq( |
... | ... | @@ -220,18 +219,7 @@ public class TkSceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageM |
220 | 219 | continue; |
221 | 220 | } |
222 | 221 | List<String> deviceIds = action.getDeviceId(); |
223 | - if (ScopeEnum.PART.equals(action.getEntityType())) { | |
224 | - if (deviceIds == null || deviceIds.isEmpty()) { | |
225 | - throw new TkDataValidationException(ErrorMessage.DEVICE_LOSED.getMessage()); | |
226 | - } else { | |
227 | - for (String item : deviceIds) { | |
228 | - if (!tbDeviceIds.contains(item)) { | |
229 | - throw new TkDataValidationException( | |
230 | - ErrorMessage.ORGANIZATION_DEVICE_NOT_MATCHED_IN_ACTION.getMessage()); | |
231 | - } | |
232 | - } | |
233 | - } | |
234 | - } | |
222 | + validateRpcDevice(tbDeviceIds, deviceIds, action.getEntityType()); | |
235 | 223 | } |
236 | 224 | } |
237 | 225 | |
... | ... | @@ -251,13 +239,19 @@ public class TkSceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageM |
251 | 239 | doActionDTO.setTenantId(sceneLinkageDTO.getTenantId()); |
252 | 240 | doActionDTO.setSceneLinkageId(sceneLinkageDTO.getId()); |
253 | 241 | if (ActionTypeEnum.DEVICE_OUT.equals(doActionDTO.getOutTarget())) { |
254 | - ObjectNode doContext = JacksonUtil.newObjectNode(); | |
255 | - doContext.put("method", "methodThingskit"); | |
256 | - doContext.put("params", doActionDTO.getDoContext()); | |
242 | + JsonNode inputContext = doActionDTO.getDoContext().get(FastIotConstants.Rpc.PARAMS_NAME); | |
243 | + ObjectNode outputContext = JacksonUtil.newObjectNode(); | |
244 | + outputContext.put(FastIotConstants.Rpc.METHOD_NAME, "methodThingskit"); | |
245 | + if(inputContext.isTextual()){ | |
246 | + outputContext.put(FastIotConstants.Rpc.PARAMS_NAME,inputContext.asText()); | |
247 | + }else{ | |
248 | + outputContext.set(FastIotConstants.Rpc.PARAMS_NAME, inputContext); | |
249 | + } | |
250 | + | |
257 | 251 | ObjectNode addtionalInfo = JacksonUtil.newObjectNode(); |
258 | 252 | addtionalInfo.put(ModelConstants.TablePropertyMapping.COMMAND_TYPE, doActionDTO.getCommandType()); |
259 | - doContext.put(DataConstants.ADDITIONAL_INFO, addtionalInfo); | |
260 | - doActionDTO.setDoContext(doContext); | |
253 | + outputContext.set(DataConstants.ADDITIONAL_INFO, addtionalInfo); | |
254 | + doActionDTO.setDoContext(outputContext); | |
261 | 255 | } |
262 | 256 | return doActionDTO.getEntity(TkDoActionEntity.class); |
263 | 257 | }) |
... | ... | @@ -267,6 +261,27 @@ public class TkSceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageM |
267 | 261 | } |
268 | 262 | |
269 | 263 | /** |
264 | + * 验证设备输出的目标设备是否合法有效 | |
265 | + * @param tbDeviceIds 场景联动所属组织的全部设备ID | |
266 | + * @param deviceIds 场景联动选择的设备ID | |
267 | + * @param entityType 设备输出类型 | |
268 | + */ | |
269 | + private void validateRpcDevice(List<String> tbDeviceIds, List<String> deviceIds, ScopeEnum entityType) { | |
270 | + if (ScopeEnum.PART.equals(entityType)) { | |
271 | + if (deviceIds == null || deviceIds.isEmpty()) { | |
272 | + throw new TkDataValidationException(ErrorMessage.DEVICE_LOSED.getMessage()); | |
273 | + } else { | |
274 | + for (String item : deviceIds) { | |
275 | + if (!tbDeviceIds.contains(item)) { | |
276 | + throw new TkDataValidationException( | |
277 | + ErrorMessage.ORGANIZATION_DEVICE_NOT_MATCHED_IN_ACTION.getMessage()); | |
278 | + } | |
279 | + } | |
280 | + } | |
281 | + } | |
282 | + } | |
283 | + | |
284 | + /** | |
270 | 285 | * 修改执行动作 |
271 | 286 | * |
272 | 287 | * @param sceneLinkageDTO 场景联动信息 |
... | ... | @@ -280,18 +295,7 @@ public class TkSceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageM |
280 | 295 | continue; |
281 | 296 | } |
282 | 297 | List<String> deviceIds = condition.getEntityId(); |
283 | - if (ScopeEnum.PART.equals(condition.getEntityType())) { | |
284 | - if (deviceIds == null || deviceIds.isEmpty()) { | |
285 | - throw new TkDataValidationException(ErrorMessage.DEVICE_LOSED.getMessage()); | |
286 | - } else { | |
287 | - for (String item : deviceIds) { | |
288 | - if (!tbDeviceIds.contains(item)) { | |
289 | - throw new TkDataValidationException( | |
290 | - ErrorMessage.ORGANIZATION_DEVICE_NOT_MATCHED_IN_ACTION.getMessage()); | |
291 | - } | |
292 | - } | |
293 | - } | |
294 | - } | |
298 | + validateRpcDevice(tbDeviceIds, deviceIds, condition.getEntityType()); | |
295 | 299 | } |
296 | 300 | } |
297 | 301 | |
... | ... | @@ -392,7 +396,7 @@ public class TkSceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageM |
392 | 396 | // 查询该组织的所有子类 |
393 | 397 | List<String> orgIds = |
394 | 398 | organizationMapper.findOrganizationTreeList(tenantId, organizationFilter).stream() |
395 | - .map(organization -> organization.getId()) | |
399 | + .map(BaseDTO::getId) | |
396 | 400 | .collect(Collectors.toList()); |
397 | 401 | // 拿到当前组织ids所包含的设备集合 |
398 | 402 | if (orgIds.isEmpty()) { |
... | ... | @@ -439,7 +443,6 @@ public class TkSceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageM |
439 | 443 | * @param tenantId 租户主键 |
440 | 444 | * @param customerId 客户主键 |
441 | 445 | * @param state 是否禁用场景联动,true标识禁用,false标识启用。 |
442 | - * @return | |
443 | 446 | */ |
444 | 447 | @Override |
445 | 448 | public JsonNode getRuleNodeConfig( |
... | ... | @@ -471,7 +474,7 @@ public class TkSceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageM |
471 | 474 | sceneOrg.put(currentSceneId, self.getOrganizationId()); |
472 | 475 | } |
473 | 476 | |
474 | - if (enableIds.size() <= 0) { | |
477 | + if (enableIds.size() == 0) { | |
475 | 478 | return null; |
476 | 479 | } |
477 | 480 | |
... | ... | @@ -507,13 +510,13 @@ public class TkSceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageM |
507 | 510 | } |
508 | 511 | |
509 | 512 | List<TkDeviceProfileEntity> profiles = profileMapper.selectList(new LambdaQueryWrapper<TkDeviceProfileEntity>().eq(TkDeviceProfileEntity::getTenantId,tenantId)); |
510 | - Map<String, String> projectes = new HashMap<>(); | |
511 | - profiles.stream().forEach(f->projectes.put(f.getTbProfileId(),f.getId())); | |
513 | + Map<String, String> projects = new HashMap<>(); | |
514 | + profiles.forEach(f->projects.put(f.getTbProfileId(),f.getId())); | |
512 | 515 | |
513 | 516 | Map<String, Map> engineConfig = new HashMap<>(); |
514 | 517 | engineConfig.put("scenes", matchedDevices); |
515 | 518 | engineConfig.put("project", matchedProjectes); |
516 | - engineConfig.put("profile", projectes); | |
519 | + engineConfig.put("profile", projects); | |
517 | 520 | engineConfig.put("names", sceneInform); |
518 | 521 | engineConfig.put("orgs", sceneOrg); |
519 | 522 | |
... | ... | @@ -536,11 +539,7 @@ List<TkDeviceProfileEntity> profiles = profileMapper.selectList(new LambdaQueryW |
536 | 539 | if (!scenes.contains(scenId)) { |
537 | 540 | scenes.add(scenId); |
538 | 541 | } |
539 | - if (scenes.isEmpty()) { | |
540 | - resultMap.remove(deviceId); | |
541 | - } else { | |
542 | - resultMap.put(deviceId, scenes); | |
543 | - } | |
542 | + resultMap.put(deviceId, scenes); | |
544 | 543 | } |
545 | 544 | } |
546 | 545 | |
... | ... | @@ -551,8 +550,7 @@ List<TkDeviceProfileEntity> profiles = profileMapper.selectList(new LambdaQueryW |
551 | 550 | // 遍历组织id |
552 | 551 | List<String> queryOrganizationIds = new ArrayList<>(); |
553 | 552 | organizationDTOS.forEach(item -> queryOrganizationIds.add(item.getId())); |
554 | - Set<String> set = new HashSet<>(); | |
555 | - set.addAll(queryOrganizationIds); | |
553 | + Set<String> set = new HashSet<>(queryOrganizationIds); | |
556 | 554 | return new ArrayList<>(set); |
557 | 555 | } |
558 | 556 | } | ... | ... |