Commit 6c095b29e7992bef56f867d600b926cec8d5a5d6
1 parent
32fa2879
fix: [DEFECT-847] validator DeviceCredentials failed with delete TBDevice
Showing
1 changed file
with
43 additions
and
28 deletions
... | ... | @@ -15,6 +15,7 @@ import org.springframework.validation.annotation.Validated; |
15 | 15 | import org.springframework.web.bind.annotation.*; |
16 | 16 | import org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg; |
17 | 17 | import org.thingsboard.server.common.data.Device; |
18 | +import org.thingsboard.server.common.data.EntityType; | |
18 | 19 | import org.thingsboard.server.common.data.audit.ActionType; |
19 | 20 | import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
20 | 21 | import org.thingsboard.server.common.data.exception.ThingsboardException; |
... | ... | @@ -36,9 +37,9 @@ import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum; |
36 | 37 | import org.thingsboard.server.common.data.yunteng.utils.tools.ResponseResult; |
37 | 38 | import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; |
38 | 39 | import org.thingsboard.server.controller.BaseController; |
39 | -import org.thingsboard.server.dao.device.DeviceService; | |
40 | 40 | import org.thingsboard.server.dao.yunteng.service.TkDeviceProfileService; |
41 | 41 | import org.thingsboard.server.dao.yunteng.service.TkDeviceService; |
42 | +import org.thingsboard.server.service.gateway_device.GatewayNotificationsService; | |
42 | 43 | import org.thingsboard.server.service.security.permission.Operation; |
43 | 44 | import org.thingsboard.server.service.security.permission.Resource; |
44 | 45 | |
... | ... | @@ -55,10 +56,10 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant. |
55 | 56 | @Api(tags = {"设备管理"}) |
56 | 57 | @Slf4j |
57 | 58 | public class TkDeviceController extends BaseController { |
58 | - private final TkDeviceService deviceService; | |
59 | - private final DeviceService tbDeviceService; | |
59 | + private final TkDeviceService tkdeviceService; | |
60 | 60 | private final ObjectMapper objectMapper; |
61 | 61 | private final TkDeviceProfileService ytDeviceProfileService; |
62 | + private final GatewayNotificationsService gatewayNotificationsService; | |
62 | 63 | |
63 | 64 | @PostMapping |
64 | 65 | @ApiOperation("创建|编辑") |
... | ... | @@ -68,14 +69,14 @@ public class TkDeviceController extends BaseController { |
68 | 69 | @Validated(AddGroup.class) @RequestBody DeviceDTO deviceDTO) |
69 | 70 | throws ThingsboardException, ExecutionException, InterruptedException { |
70 | 71 | String currentTenantId = getCurrentUser().getCurrentTenantId(); |
71 | - deviceService.validateFormData(currentTenantId, deviceDTO); | |
72 | + tkdeviceService.validateFormData(currentTenantId, deviceDTO); | |
72 | 73 | |
73 | 74 | /** 网关是否有效 */ |
74 | 75 | String gatewayId = deviceDTO.getGatewayId(); |
75 | 76 | DeviceDTO gateWay = null; |
76 | 77 | if (StringUtils.isNotEmpty(gatewayId)) { |
77 | 78 | gateWay = |
78 | - deviceService.checkDeviceByTenantIdAndDeviceId( | |
79 | + tkdeviceService.checkDeviceByTenantIdAndDeviceId( | |
79 | 80 | getCurrentUser().getCurrentTenantId(), gatewayId); |
80 | 81 | if (null == gateWay) { |
81 | 82 | throw new YtDataValidationException( |
... | ... | @@ -120,14 +121,14 @@ public class TkDeviceController extends BaseController { |
120 | 121 | } |
121 | 122 | |
122 | 123 | DeviceDTO newDeviceDTO = |
123 | - deviceService.insertOrUpdate(getCurrentUser().getCurrentTenantId(), deviceDTO); | |
124 | + tkdeviceService.insertOrUpdate(getCurrentUser().getCurrentTenantId(), deviceDTO); | |
124 | 125 | return ResponseEntity.ok(newDeviceDTO); |
125 | 126 | } |
126 | 127 | |
127 | 128 | @GetMapping({"sn"}) |
128 | 129 | @ApiOperation("自动生成设备SN") |
129 | 130 | public ResponseEntity<String> generate() { |
130 | - String result = deviceService.generateSn(); | |
131 | + String result = tkdeviceService.generateSn(); | |
131 | 132 | return ResponseEntity.ok(result); |
132 | 133 | } |
133 | 134 | |
... | ... | @@ -141,9 +142,10 @@ public class TkDeviceController extends BaseController { |
141 | 142 | checkEntity(null, tbDevice, Resource.DEVICE); |
142 | 143 | } |
143 | 144 | |
144 | - Device savedDevice = checkNotNull(tbDeviceService.saveDeviceWithAccessToken(tbDevice, null)); | |
145 | + Device savedDevice = checkNotNull(deviceService.saveDeviceWithAccessToken(tbDevice, null)); | |
145 | 146 | tbClusterService.onDeviceUpdated(savedDevice, oldDevice); |
146 | 147 | DeviceId tbDeviceId = savedDevice.getId(); |
148 | + DeviceCredentials deviceCredentials = null; | |
147 | 149 | try { |
148 | 150 | logEntityAction( |
149 | 151 | getCurrentUser(), |
... | ... | @@ -154,7 +156,7 @@ public class TkDeviceController extends BaseController { |
154 | 156 | null); |
155 | 157 | |
156 | 158 | if (formCredentials != null && formCredentials.getCredentialsType() != null) { |
157 | - DeviceCredentials deviceCredentials = | |
159 | + deviceCredentials = | |
158 | 160 | checkNotNull( |
159 | 161 | deviceCredentialsService.findDeviceCredentialsByDeviceId( |
160 | 162 | getCurrentUser().getTenantId(), tbDeviceId)); |
... | ... | @@ -182,8 +184,16 @@ public class TkDeviceController extends BaseController { |
182 | 184 | null, |
183 | 185 | deviceCredentials); |
184 | 186 | } |
185 | - } catch (ThingsboardException e) { | |
186 | - log.error("Failed to log entity action", e); | |
187 | + } catch (Exception e) { | |
188 | + logEntityAction( | |
189 | + emptyId(EntityType.DEVICE), | |
190 | + null, | |
191 | + null, | |
192 | + ActionType.CREDENTIALS_UPDATED, | |
193 | + e, | |
194 | + deviceCredentials); | |
195 | + deleteTbDevice(tbDeviceId.getId().toString()); | |
196 | + throw handleException(e); | |
187 | 197 | } |
188 | 198 | return tbDeviceId; |
189 | 199 | } |
... | ... | @@ -193,7 +203,7 @@ public class TkDeviceController extends BaseController { |
193 | 203 | @PreAuthorize("@check.checkPermissions({},{'api:yt:device:get'})") |
194 | 204 | public ResponseEntity<DeviceDTO> getDevice(@PathVariable("id") String id) |
195 | 205 | throws ThingsboardException { |
196 | - return ResponseEntity.of(deviceService.getDevice(getCurrentUser().getCurrentTenantId(), id)); | |
206 | + return ResponseEntity.of(tkdeviceService.getDevice(getCurrentUser().getCurrentTenantId(), id)); | |
197 | 207 | } |
198 | 208 | |
199 | 209 | @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})") |
... | ... | @@ -238,7 +248,7 @@ public class TkDeviceController extends BaseController { |
238 | 248 | if (getCurrentUser().isCustomerUser()) { |
239 | 249 | queryMap.put("customerId", getCurrentUser().getCustomerId().toString()); |
240 | 250 | } |
241 | - return deviceService.page(getCurrentUser().getCurrentTenantId(), queryMap); | |
251 | + return tkdeviceService.page(getCurrentUser().getCurrentTenantId(), queryMap); | |
242 | 252 | } |
243 | 253 | |
244 | 254 | @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})") |
... | ... | @@ -276,7 +286,7 @@ public class TkDeviceController extends BaseController { |
276 | 286 | if (getCurrentUser().isCustomerUser()) { |
277 | 287 | queryMap.put("customerId", getCurrentUser().getCustomerId().toString()); |
278 | 288 | } |
279 | - return deviceService.pageRelation(queryMap); | |
289 | + return tkdeviceService.pageRelation(queryMap); | |
280 | 290 | } |
281 | 291 | |
282 | 292 | @PostMapping("/import") |
... | ... | @@ -301,20 +311,23 @@ public class TkDeviceController extends BaseController { |
301 | 311 | public void deleteDevices(@Validated({DeleteGroup.class}) @RequestBody DeleteDTO deleteDTO) |
302 | 312 | throws ThingsboardException { |
303 | 313 | String currentTenantId = getCurrentUser().getCurrentTenantId(); |
304 | - List<String> tdIds = deviceService.findTbDeviceId(currentTenantId, deleteDTO.getIds()); | |
314 | + List<String> tdIds = tkdeviceService.findTbDeviceId(currentTenantId, deleteDTO.getIds()); | |
305 | 315 | for (String id : tdIds) { |
306 | 316 | deleteTbDevice(id); |
307 | 317 | } |
308 | - deviceService.deleteDevices(currentTenantId, deleteDTO.getIds()); | |
318 | + tkdeviceService.deleteDevices(currentTenantId, deleteDTO.getIds()); | |
309 | 319 | } |
310 | 320 | |
311 | 321 | private void deleteTbDevice(String id) throws ThingsboardException { |
312 | 322 | DeviceId deviceId = new DeviceId(toUUID(id)); |
323 | + | |
313 | 324 | Device device = checkDeviceId(deviceId, Operation.DELETE); |
314 | 325 | |
315 | 326 | List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), deviceId); |
316 | 327 | |
317 | - tbDeviceService.deleteDevice(getCurrentUser().getTenantId(), deviceId); | |
328 | + deviceService.deleteDevice(getCurrentUser().getTenantId(), deviceId); | |
329 | + | |
330 | + gatewayNotificationsService.onDeviceDeleted(device); | |
318 | 331 | |
319 | 332 | tbClusterService.onDeviceDeleted(device, null); |
320 | 333 | |
... | ... | @@ -333,7 +346,7 @@ public class TkDeviceController extends BaseController { |
333 | 346 | @ApiParam(value = "设备标签") @RequestParam(value = "deviceLabel", required = false) |
334 | 347 | String deviceLabel) |
335 | 348 | throws ThingsboardException { |
336 | - return deviceService.findDevicesByDeviceTypeAndOrganizationId( | |
349 | + return tkdeviceService.findDevicesByDeviceTypeAndOrganizationId( | |
337 | 350 | deviceType, getCurrentUser().getCurrentTenantId(), organizationId, deviceLabel); |
338 | 351 | } |
339 | 352 | |
... | ... | @@ -348,7 +361,7 @@ public class TkDeviceController extends BaseController { |
348 | 361 | @ApiParam(value = "设备标签") @RequestParam(value = "deviceLabel", required = false) |
349 | 362 | String deviceLabel) |
350 | 363 | throws ThingsboardException { |
351 | - return deviceService.findDevicesByDeviceTypeAndOrganizationId( | |
364 | + return tkdeviceService.findDevicesByDeviceTypeAndOrganizationId( | |
352 | 365 | deviceType, getCurrentUser().getCurrentTenantId(), organizationId, deviceLabel); |
353 | 366 | } |
354 | 367 | |
... | ... | @@ -358,12 +371,13 @@ public class TkDeviceController extends BaseController { |
358 | 371 | public List<SelectItemDTO> getMasterDevices( |
359 | 372 | @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId, |
360 | 373 | @ApiParam(value = "设备配置ID") @RequestParam(value = "deviceProfileId", required = false) |
361 | - String deviceProfileId) | |
374 | + String deviceProfileId) | |
362 | 375 | throws ThingsboardException { |
363 | - return deviceService.findMasterDevices( | |
376 | + return tkdeviceService.findMasterDevices( | |
364 | 377 | getCurrentUser().getCurrentTenantId(), |
365 | 378 | getCurrentUser().isCustomerUser() ? getCurrentUser().getCustomerId().toString() : null, |
366 | - organizationId,deviceProfileId); | |
379 | + organizationId, | |
380 | + deviceProfileId); | |
367 | 381 | } |
368 | 382 | |
369 | 383 | @GetMapping("/list/slave/{organizationId}") |
... | ... | @@ -374,7 +388,7 @@ public class TkDeviceController extends BaseController { |
374 | 388 | @ApiParam(value = "主设备ID") @RequestParam(value = "masterId", required = false) |
375 | 389 | String masterId) |
376 | 390 | throws ThingsboardException { |
377 | - return deviceService.findSlaveDevices( | |
391 | + return tkdeviceService.findSlaveDevices( | |
378 | 392 | masterId, |
379 | 393 | getCurrentUser().getCurrentTenantId(), |
380 | 394 | getCurrentUser().isCustomerUser() ? getCurrentUser().getCustomerId().toString() : null, |
... | ... | @@ -389,7 +403,7 @@ public class TkDeviceController extends BaseController { |
389 | 403 | @ApiParam(value = "设备ID") @RequestParam(value = "deviceIds", required = false) |
390 | 404 | List<String> deviceIds) |
391 | 405 | throws ThingsboardException { |
392 | - return deviceService.findDeviceKeys( | |
406 | + return tkdeviceService.findDeviceKeys( | |
393 | 407 | getCurrentUser().getCurrentTenantId(), |
394 | 408 | getCurrentUser().isCustomerUser() ? getCurrentUser().getCustomerId().toString() : null, |
395 | 409 | organizationId, |
... | ... | @@ -402,7 +416,7 @@ public class TkDeviceController extends BaseController { |
402 | 416 | @ApiParam(value = "tb设备Id") @PathVariable("tbDeviceId") String tbDeviceId) |
403 | 417 | throws ThingsboardException { |
404 | 418 | List<DeviceDTO> list = |
405 | - deviceService.findGateWayDeviceByTbDeviceId( | |
419 | + tkdeviceService.findGateWayDeviceByTbDeviceId( | |
406 | 420 | getCurrentUser().getCurrentTenantId(), tbDeviceId); |
407 | 421 | return null != list && list.size() > FastIotConstants.MagicNumber.ZERO |
408 | 422 | ? list.get(FastIotConstants.MagicNumber.ZERO) |
... | ... | @@ -414,7 +428,7 @@ public class TkDeviceController extends BaseController { |
414 | 428 | public DeviceDTO getSubsetDeviceByTbDeviceId( |
415 | 429 | @ApiParam(value = "tb设备Id") @PathVariable("tbDeviceId") String tbDeviceId) |
416 | 430 | throws ThingsboardException { |
417 | - return deviceService.getSubsetDeviceByTbDeviceId( | |
431 | + return tkdeviceService.getSubsetDeviceByTbDeviceId( | |
418 | 432 | getCurrentUser().getCurrentTenantId(), tbDeviceId); |
419 | 433 | } |
420 | 434 | |
... | ... | @@ -481,7 +495,7 @@ public class TkDeviceController extends BaseController { |
481 | 495 | @ApiOperation("设备是否被占用") |
482 | 496 | public ResponseResult<Boolean> otherUsing(@PathVariable("id") String deviceId) |
483 | 497 | throws ThingsboardException { |
484 | - String str = deviceService.otherUsing(deviceId, getCurrentUser().getCurrentTenantId()); | |
498 | + String str = tkdeviceService.otherUsing(deviceId, getCurrentUser().getCurrentTenantId()); | |
485 | 499 | ResponseResult result = ResponseResult.success(StringUtils.isEmpty(str) ? true : false); |
486 | 500 | result.setMessage(str); |
487 | 501 | return result; |
... | ... | @@ -498,6 +512,7 @@ public class TkDeviceController extends BaseController { |
498 | 512 | if (null == dto) { |
499 | 513 | throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); |
500 | 514 | } |
501 | - return ResponseEntity.ok(deviceService.getDeviceAttributes(deviceProfileId, tenantId,dataType)); | |
515 | + return ResponseEntity.ok( | |
516 | + tkdeviceService.getDeviceAttributes(deviceProfileId, tenantId, dataType)); | |
502 | 517 | } |
503 | 518 | } | ... | ... |