Commit 6c095b29e7992bef56f867d600b926cec8d5a5d6

Authored by 黄 x
1 parent 32fa2879

fix: [DEFECT-847] validator DeviceCredentials failed with delete TBDevice

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