Commit 1d88e163124880c9caa2a33b237261c431b6f882

Authored by 黄 x
1 parent 3e456bf3

feat: add query gateway devices api

... ... @@ -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.DeviceTransportType;
18 19 import org.thingsboard.server.common.data.EntityType;
19 20 import org.thingsboard.server.common.data.audit.ActionType;
20 21 import org.thingsboard.server.common.data.edge.EdgeEventActionType;
... ... @@ -347,10 +348,27 @@ public class TkDeviceController extends BaseController {
347 348 @ApiParam(value = "设备标签") @RequestParam(value = "deviceLabel", required = false)
348 349 String deviceLabel,
349 350 @ApiParam(value = "设备配置ID") @RequestParam(value = "deviceProfileId", required = false)
350   - String deviceProfileId)
  351 + String deviceProfileId)
351 352 throws ThingsboardException {
352 353 return tkdeviceService.findDevicesByDeviceTypeAndOrganizationId(
353   - deviceType, getCurrentUser().getCurrentTenantId(), organizationId, deviceLabel,deviceProfileId);
  354 + deviceType,
  355 + getCurrentUser().getCurrentTenantId(),
  356 + organizationId,
  357 + deviceLabel,
  358 + deviceProfileId);
  359 + }
  360 +
  361 + @GetMapping("/gateway/list")
  362 + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})")
  363 + @ApiOperation("获取满足条件的所有设备")
  364 + public List<DeviceDTO> getGatewayDevices(
  365 + @ApiParam(value = "组织ID") @RequestParam(value = "organizationId", required = false)
  366 + String organizationId,
  367 + @ApiParam(value = "传输类型") @RequestParam(value = "transportType", required = false)
  368 + DeviceTransportType transportType)
  369 + throws ThingsboardException {
  370 + return tkdeviceService.findDevicesByTransportTypeAndOrganizationId(
  371 + getCurrentUser().getCurrentTenantId(), organizationId, transportType);
354 372 }
355 373
356 374 @GetMapping("/list/master/{organizationId}")
... ...
... ... @@ -13,6 +13,7 @@ import org.springframework.stereotype.Service;
13 13 import org.springframework.transaction.annotation.Transactional;
14 14 import org.thingsboard.common.util.JacksonUtil;
15 15 import org.thingsboard.server.common.data.DeviceProfile;
  16 +import org.thingsboard.server.common.data.DeviceTransportType;
16 17 import org.thingsboard.server.common.data.id.EntityId;
17 18 import org.thingsboard.server.common.data.id.TenantId;
18 19 import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
... ... @@ -271,6 +272,38 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev
271 272 }
272 273
273 274 @Override
  275 + public List<DeviceDTO> findDevicesByTransportTypeAndOrganizationId(
  276 + String tenantId, String organizationId, DeviceTransportType transportType) {
  277 + List<String> orgIds = organizationAllIds(tenantId, organizationId);
  278 + if (orgIds.isEmpty()) {
  279 + throw new YtDataValidationException(ErrorMessage.ORGANIZATION_NOT_EXTIED.getMessage());
  280 + }
  281 + List<String> deviceProfileIds = null;
  282 + if (null != transportType) {
  283 + deviceProfileIds = tkProfileMapper.getDeviceProfileIds(tenantId, transportType);
  284 + }
  285 + List<TkDeviceEntity> entities =
  286 + baseMapper.selectList(
  287 + new LambdaQueryWrapper<TkDeviceEntity>()
  288 + .eq(TkDeviceEntity::getTenantId, tenantId)
  289 + .eq(TkDeviceEntity::getDeviceType,DeviceTypeEnum.GATEWAY)
  290 + .in(TkDeviceEntity::getOrganizationId, orgIds));
  291 + if(null != deviceProfileIds && !deviceProfileIds.isEmpty()){
  292 + List<TkDeviceEntity> result = new ArrayList<>();
  293 + for (TkDeviceEntity entity :entities){
  294 + if(deviceProfileIds.contains(entity.getDeviceProfileId())){
  295 + result.add(entity);
  296 + }
  297 + }
  298 + return result.stream().map(obj -> obj.getDTO(DeviceDTO.class)).collect(Collectors.toList());
  299 + }
  300 + if (null != entities && !entities.isEmpty()) {
  301 + return entities.stream().map(obj -> obj.getDTO(DeviceDTO.class)).collect(Collectors.toList());
  302 + }
  303 + return null;
  304 + }
  305 +
  306 + @Override
274 307 public DeviceDTO checkDeviceByTenantIdAndDeviceId(String tenantId, String deviceId) {
275 308 if (StringUtils.isEmpty(tenantId) || StringUtils.isEmpty(deviceId)) {
276 309 throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
... ... @@ -351,6 +384,9 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev
351 384 @NotNull
352 385 private List<String> organizationAllIds(String tenantId, String organizationId) {
353 386 List<String> organizationIds = new ArrayList<>();
  387 + if (StringUtils.isEmpty(organizationId)) {
  388 + return organizationIds;
  389 + }
354 390 if (!StringUtils.isEmpty(organizationId)) {
355 391 organizationIds.add(organizationId);
356 392 }
... ...
... ... @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 4 import com.baomidou.mybatisplus.core.metadata.IPage;
5 5 import org.apache.ibatis.annotations.Mapper;
6 6 import org.apache.ibatis.annotations.Param;
  7 +import org.thingsboard.server.common.data.DeviceTransportType;
7 8 import org.thingsboard.server.common.data.yunteng.dto.*;
8 9 import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum;
9 10 import org.thingsboard.server.dao.yunteng.entities.TkDeviceProfileEntity;
... ... @@ -31,4 +32,8 @@ public interface TkDeviceProfileMapper extends BaseMapper<TkDeviceProfileEntity>
31 32 @Param("tenantId") String tenantId,
32 33 @Param("scriptId") String scriptId,
33 34 @Param("deviceType") DeviceTypeEnum deviceType);
  35 +
  36 + List<String> getDeviceProfileIds(
  37 + @Param("tenantId") String tenantId,
  38 + @Param("transportType") DeviceTransportType transportType);
34 39 }
... ...
1 1 package org.thingsboard.server.dao.yunteng.service;
2 2
3 3 import com.fasterxml.jackson.databind.JsonNode;
  4 +import org.thingsboard.server.common.data.DeviceTransportType;
4 5 import org.thingsboard.server.common.data.id.EntityId;
5 6 import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO;
6 7 import org.thingsboard.server.common.data.yunteng.dto.RelationDeviceDTO;
... ... @@ -26,10 +27,7 @@ public interface TkDeviceService extends BaseService<TkDeviceEntity> {
26 27
27 28 YtPageData<RelationDeviceDTO> pageRelation(Map<String, Object> queryMap);
28 29
29   - /**
30   - * 验证表单数据有效性
31   - *
32   - */
  30 + /** 验证表单数据有效性 */
33 31 void validateFormData(String currentTenantId, DeviceDTO ytDevice);
34 32
35 33 /**
... ... @@ -55,6 +53,8 @@ public interface TkDeviceService extends BaseService<TkDeviceEntity> {
55 53 String deviceLabel,
56 54 String deviceProfileId);
57 55
  56 + List<DeviceDTO> findDevicesByTransportTypeAndOrganizationId(
  57 + String tenantId, String organizationId, DeviceTransportType transportType);
58 58 /**
59 59 * 通过设备ID和租户ID判断该设备是否存在
60 60 *
... ...
... ... @@ -85,4 +85,12 @@
85 85 </if>
86 86 </where>
87 87 </select>
  88 +
  89 + <select id="getDeviceProfileIds" resultType="java.lang.String">
  90 + SELECT iot.id
  91 + FROM device_profile base
  92 + LEFT JOIN tk_device_profile iot ON iot.tb_profile_id = base.id::TEXT
  93 + WHERE iot.tenant_id = #{tenantId}
  94 + AND base.transport_type = #{transportType}
  95 + </select>
88 96 </mapper>
... ...