Commit a0a5212312b5df65732c4be701b3f17fe53baabe

Authored by 芯火源
2 parents 2e97eb9b 10b3fc5b

Merge branch 'master_dev' into 20230614

... ... @@ -3,16 +3,15 @@ package org.thingsboard.server.controller.yunteng;
3 3 import io.swagger.annotations.Api;
4 4 import io.swagger.annotations.ApiOperation;
5 5 import lombok.RequiredArgsConstructor;
  6 +import org.apache.commons.lang3.StringUtils;
6 7 import org.springframework.http.ResponseEntity;
7 8 import org.springframework.security.access.prepost.PreAuthorize;
8   -import org.springframework.web.bind.annotation.GetMapping;
9   -import org.springframework.web.bind.annotation.RequestMapping;
10   -import org.springframework.web.bind.annotation.RequestParam;
11   -import org.springframework.web.bind.annotation.RestController;
  9 +import org.springframework.web.bind.annotation.*;
12 10 import org.thingsboard.server.common.data.EntityType;
13 11 import org.thingsboard.server.common.data.alarm.AlarmSeverity;
14 12 import org.thingsboard.server.common.data.alarm.AlarmStatus;
15 13 import org.thingsboard.server.common.data.exception.ThingsboardException;
  14 +import org.thingsboard.server.common.data.yunteng.dto.ConfigurationAlarmPageDTO;
16 15 import org.thingsboard.server.common.data.yunteng.dto.SysDictDTO;
17 16 import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum;
18 17 import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum;
... ... @@ -46,11 +45,11 @@ public class TkAlarmInfoController extends BaseController {
46 45 public TkPageData<TkAlarmEntity> pageAlarmInfo(
47 46 @RequestParam(PAGE_SIZE) int pageSize,
48 47 @RequestParam(PAGE) int page,
49   - @RequestParam(value = "status", required = false) List<AlarmStatus> status,
  48 + @RequestParam(value = "status", required = false) AlarmStatus status,
50 49 @RequestParam(value = "alarmType", required = false) String alarmType,
51 50 @RequestParam(value = "severity", required = false) AlarmSeverity severity,
52 51 @RequestParam(value = "organizationId", required = false) String organizationId,
53   - @RequestParam(value = "deviceIds", required = false) List<String> deviceIds,
  52 + @RequestParam(value = "deviceId", required = false) String deviceId,
54 53 @RequestParam(value = "deviceName", required = false) String deviceName,
55 54 @RequestParam(value = "deviceType", required = false) DeviceTypeEnum deviceType,
56 55 @RequestParam(value = "startTime", required = false) Long startTime,
... ... @@ -63,7 +62,15 @@ public class TkAlarmInfoController extends BaseController {
63 62 pageFilter.put(PAGE, page);
64 63 pageFilter.put(ORDER_FILED, orderBy);
65 64 pageFilter.put(ORDER_TYPE, orderType);
66   -
  65 + List<String> deviceIds = null;
  66 + List<AlarmStatus> statuses = null;
  67 + if(!StringUtils.isEmpty(deviceId))
  68 + {
  69 + deviceIds = List.of(deviceId);
  70 + }
  71 + if(null !=status){
  72 + statuses = List.of(status);
  73 + }
67 74 UUID customerId = null;
68 75 if (getCurrentUser().isCustomerUser()) {
69 76 customerId = getCurrentUser().getCustomerId().getId();
... ... @@ -76,13 +83,53 @@ public class TkAlarmInfoController extends BaseController {
76 83 alarmType,
77 84 startTime,
78 85 endTime,
79   - status,
  86 + statuses,
80 87 deviceIds,
81 88 deviceName,
82 89 deviceType,
83 90 EntityType.DEVICE,
84 91 organizationId);
85 92 }
  93 + @ApiOperation(value = "组态告警查询")
  94 + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})")
  95 + @PostMapping("/configuration/page")
  96 + public TkPageData<TkAlarmEntity> pageAlarmInfoForConfiguration(@RequestBody ConfigurationAlarmPageDTO page) throws ThingsboardException {
  97 + Map<String, Object> pageFilter = new HashMap<>();
  98 + pageFilter.put(PAGE_SIZE, page.getPageSize());
  99 + pageFilter.put(PAGE, page.getPage());
  100 + pageFilter.put(ORDER_FILED, page.getOrderBy());
  101 + pageFilter.put(ORDER_TYPE, page.getOrderType());
  102 + UUID customerId = null;
  103 + if (getCurrentUser().isCustomerUser()) {
  104 + customerId = getCurrentUser().getCustomerId().getId();
  105 + }
  106 + AlarmSeverity severity = page.getSeverity();
  107 + AlarmStatus status = page.getStatus();
  108 + List<AlarmStatus> statuses = null;
  109 + if(null !=status){
  110 + statuses = List.of(status);
  111 + }
  112 + String alarmType = page.getAlarmType();
  113 + Long startTime = page.getStartTime();
  114 + Long endTime = page.getEndTime();
  115 + String deviceName = page.getDeviceName();
  116 + String organizationId = page.getOrganizationId();
  117 + DeviceTypeEnum deviceType = page.getDeviceType();
  118 + return alarmInfoService.alarmPage(
  119 + pageFilter,
  120 + getCurrentUser().getTenantId(),
  121 + customerId,
  122 + severity,
  123 + alarmType,
  124 + startTime,
  125 + endTime,
  126 + statuses,
  127 + page.getDeviceIds(),
  128 + deviceName,
  129 + deviceType,
  130 + EntityType.DEVICE,
  131 + organizationId);
  132 + }
86 133
87 134 @ApiOperation(value = "告警类型")
88 135 @GetMapping()
... ...
... ... @@ -33,7 +33,6 @@ import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum;
33 33 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
34 34 import org.thingsboard.server.common.msg.queue.ServiceQueue;
35 35 import org.thingsboard.server.controller.BaseController;
36   -import org.thingsboard.server.dao.yunteng.service.TkDeviceScriptService;
37 36 import org.thingsboard.server.dao.yunteng.service.TkDeviceProfileService;
38 37 import org.thingsboard.server.service.security.permission.Operation;
39 38
... ... @@ -48,8 +47,7 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.
48 47 @RequestMapping("api/yt/device_profile")
49 48 @Api(tags = {"设备配置管理"})
50 49 public class TkDeviceProfileController extends BaseController {
51   - private final TkDeviceProfileService ytDeviceProfileService;
52   - private final TkDeviceScriptService javaScriptService;
  50 + private final TkDeviceProfileService tkDeviceProfileService;
53 51
54 52 @PostMapping()
55 53 @PreAuthorize(
... ... @@ -67,7 +65,7 @@ public class TkDeviceProfileController extends BaseController {
67 65
68 66 DeviceProfile saveDeviceProfile = updateTbDeviceProfile(tbDeviceProfile, created);
69 67 deviceProfileDTO.setTbProfileId(saveDeviceProfile.getId().toString());
70   - ytDeviceProfileService.insertOrUpdate(deviceProfileDTO);
  68 + tkDeviceProfileService.insertOrUpdate(deviceProfileDTO);
71 69
72 70 return ResponseEntity.ok(deviceProfileDTO);
73 71 }
... ... @@ -131,7 +129,7 @@ public class TkDeviceProfileController extends BaseController {
131 129 public ResponseEntity<DeviceProfileDTO> getDevice(@PathVariable("id") String id)
132 130 throws ThingsboardException {
133 131 return ResponseEntity.ok(
134   - ytDeviceProfileService.getDeviceProfile(getCurrentUser().getCurrentTenantId(), id));
  132 + tkDeviceProfileService.getDeviceProfile(getCurrentUser().getCurrentTenantId(), id));
135 133 }
136 134
137 135 @GetMapping(params = {PAGE_SIZE, PAGE})
... ... @@ -154,7 +152,7 @@ public class TkDeviceProfileController extends BaseController {
154 152 queryMap.put("transportType", transportType);
155 153 queryMap.put(TENANT_ID, getCurrentUser().getCurrentTenantId());
156 154 queryMap.put(CUSTOMER_ID, getCurrentUser().getCustomerId().toString());
157   - return ytDeviceProfileService.page(queryMap, getCurrentUser().isTenantAdmin());
  155 + return tkDeviceProfileService.page(queryMap, getCurrentUser().isTenantAdmin());
158 156 }
159 157
160 158 @GetMapping("/me/list")
... ... @@ -167,10 +165,10 @@ public class TkDeviceProfileController extends BaseController {
167 165 List<DeviceProfileDTO> results;
168 166 String tenantId = getCurrentUser().getCurrentTenantId();
169 167 if (getCurrentUser().isTenantAdmin()) {
170   - results = ytDeviceProfileService.findDeviceProfile(tenantId, null,deviceType);
  168 + results = tkDeviceProfileService.findDeviceProfile(tenantId, null,deviceType);
171 169 } else {
172 170 results =
173   - ytDeviceProfileService.findCustomerDeviceProfiles(
  171 + tkDeviceProfileService.findCustomerDeviceProfiles(
174 172 tenantId, getCurrentUser().getCustomerId(),deviceType);
175 173 }
176 174
... ... @@ -183,18 +181,18 @@ public class TkDeviceProfileController extends BaseController {
183 181 public void deleteDeviceProfiles(@Validated({DeleteGroup.class}) @RequestBody DeleteDTO deleteDTO)
184 182 throws ThingsboardException {
185 183 String tenantId = getCurrentUser().getCurrentTenantId();
186   - ytDeviceProfileService.checkDeviceProfiles(tenantId, deleteDTO.getIds());
  184 + tkDeviceProfileService.checkDeviceProfiles(tenantId, deleteDTO.getIds());
187 185
188 186
189 187 for (String id : deleteDTO.getIds()) {
190 188 deleteTbDeviceProfile(id);
191 189 }
192   - ytDeviceProfileService.deleteDeviceProfiles(tenantId, deleteDTO.getIds());
  190 + tkDeviceProfileService.deleteDeviceProfiles(tenantId, deleteDTO.getIds());
193 191 }
194 192
195 193 private void deleteTbDeviceProfile(String profileId) throws ThingsboardException {
196 194 DeviceProfileDTO dto =
197   - ytDeviceProfileService.findDeviceProfileById(
  195 + tkDeviceProfileService.findDeviceProfileById(
198 196 getCurrentUser().getCurrentTenantId(), profileId);
199 197 if (null != dto) {
200 198 DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(dto.getTbProfileId()));
... ... @@ -248,7 +246,7 @@ public class TkDeviceProfileController extends BaseController {
248 246 DeviceProfile tbDeviceProfile = new DeviceProfile();
249 247 if (StringUtils.isNotBlank(deviceProfileDTO.getId())) {
250 248 DeviceProfileDTO findDeviceProfile =
251   - ytDeviceProfileService.findDeviceProfileById(
  249 + tkDeviceProfileService.findDeviceProfileById(
252 250 getCurrentUser().getCurrentTenantId(), deviceProfileDTO.getId());
253 251 if (StringUtils.isNotEmpty(findDeviceProfile.getTbProfileId())) {
254 252 UUID profileId = UUID.fromString(findDeviceProfile.getTbProfileId());
... ...
  1 +package org.thingsboard.server.common.data.yunteng.dto;
  2 +
  3 +import io.swagger.annotations.ApiModelProperty;
  4 +import lombok.Data;
  5 +import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum;
  6 +
  7 +import javax.validation.constraints.NotNull;
  8 +
  9 +@Data
  10 +public class BasePageDTO {
  11 + @ApiModelProperty(required = true)
  12 + @NotNull(message = "分页大小不能为空")
  13 + private int pageSize;
  14 +
  15 + @NotNull(message = "分页不能为空")
  16 + @ApiModelProperty(required = true)
  17 + private int page;
  18 +
  19 + private String orderBy;
  20 + private OrderTypeEnum orderType;
  21 +}
... ...
  1 +package org.thingsboard.server.common.data.yunteng.dto;
  2 +
  3 +import io.swagger.annotations.ApiModelProperty;
  4 +import lombok.Data;
  5 +import org.thingsboard.server.common.data.alarm.AlarmSeverity;
  6 +import org.thingsboard.server.common.data.alarm.AlarmStatus;
  7 +import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum;
  8 +
  9 +import javax.validation.constraints.NotEmpty;
  10 +import java.util.List;
  11 +
  12 +@Data
  13 +public class ConfigurationAlarmPageDTO extends BasePageDTO {
  14 + private AlarmStatus status;
  15 + private String alarmType;
  16 + private String organizationId;
  17 +
  18 + @ApiModelProperty(required = true)
  19 + @NotEmpty(message = "告警设备列表不能为空")
  20 + private List<String> deviceIds;
  21 +
  22 + private String deviceName;
  23 + private DeviceTypeEnum deviceType;
  24 + private Long startTime;
  25 + private Long endTime;
  26 + private AlarmSeverity severity;
  27 +}
... ...
... ... @@ -4,12 +4,15 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
4 4 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
5 5 import com.baomidou.mybatisplus.core.metadata.IPage;
6 6 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7 +import com.fasterxml.jackson.databind.JsonNode;
7 8 import lombok.RequiredArgsConstructor;
8 9 import lombok.extern.slf4j.Slf4j;
9 10 import org.apache.commons.lang3.StringUtils;
10 11 import org.springframework.stereotype.Service;
11 12 import org.springframework.transaction.annotation.Transactional;
12 13 import org.thingsboard.server.common.data.DeviceProfile;
  14 +import org.thingsboard.server.common.data.DeviceTransportType;
  15 +import org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration;
13 16 import org.thingsboard.server.common.data.id.CustomerId;
14 17 import org.thingsboard.server.common.data.id.TenantId;
15 18 import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
... ... @@ -20,6 +23,7 @@ import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
20 23 import org.thingsboard.server.common.data.yunteng.dto.*;
21 24 import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum;
22 25 import org.thingsboard.server.common.data.yunteng.enums.TransportTypeEnum;
  26 +import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil;
23 27 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
24 28 import org.thingsboard.server.dao.yunteng.entities.*;
25 29 import org.thingsboard.server.dao.yunteng.jpa.dao.TkJpaDeviceProfileDao;
... ... @@ -85,6 +89,19 @@ public class TkDeviceProfileServiceImpl
85 89 }
86 90
87 91 @Override
  92 + public List<DeviceProfileDTO> findDeviceProfileByScriptId(String tenantId, String scriptId) {
  93 + if(StringUtils.isEmpty(tenantId) || StringUtils.isEmpty(scriptId)){
  94 + throw new TkDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  95 + }
  96 + List<TkDeviceProfileEntity> entities = baseMapper.selectList(new LambdaQueryWrapper<TkDeviceProfileEntity>()
  97 + .eq(TkDeviceProfileEntity::getTenantId,tenantId).like(TkDeviceProfileEntity::getScriptId,scriptId));
  98 + if(null != entities && !entities.isEmpty()){
  99 + return entities.stream().map(entity->entity.getDTO(DeviceProfileDTO.class)).collect(Collectors.toList());
  100 + }
  101 + return null;
  102 + }
  103 +
  104 + @Override
88 105 @Transactional
89 106 public DeviceProfileDTO insertOrUpdate(DeviceProfileDTO deviceDTO) {
90 107 if (StringUtils.isBlank(deviceDTO.getId())) {
... ... @@ -97,6 +114,9 @@ public class TkDeviceProfileServiceImpl
97 114 private DeviceProfileDTO insert(DeviceProfileDTO deviceDTO) {
98 115
99 116 TkDeviceProfileEntity profile = new TkDeviceProfileEntity();
  117 + if (Objects.equals(deviceDTO.getTransportType(), DeviceTransportType.TCP.name())) {
  118 + setDeviceScriptId(profile,deviceDTO.getProfileData().getTransportConfiguration());
  119 + }
100 120 deviceDTO.copyToEntity(
101 121 profile,
102 122 ModelConstants.TablePropertyMapping.ACTIVE_TIME,
... ... @@ -113,6 +133,9 @@ public class TkDeviceProfileServiceImpl
113 133
114 134 private DeviceProfileDTO update(DeviceProfileDTO deviceDTO) {
115 135 TkDeviceProfileEntity device = new TkDeviceProfileEntity();
  136 + if (Objects.equals(deviceDTO.getTransportType(), DeviceTransportType.TCP.name())) {
  137 + setDeviceScriptId(device,deviceDTO.getProfileData().getTransportConfiguration());
  138 + }
116 139 deviceDTO.copyToEntity(
117 140 device,
118 141 ModelConstants.TablePropertyMapping.ACTIVE_TIME,
... ... @@ -127,6 +150,23 @@ public class TkDeviceProfileServiceImpl
127 150 baseMapper.updateById(device);
128 151 return device.getDTO(DeviceProfileDTO.class);
129 152 }
  153 + private void setDeviceScriptId(TkDeviceProfileEntity profile, DeviceProfileTransportConfiguration transportConfiguration)
  154 + {
  155 + JsonNode jsonNode = JacksonUtil.convertValue(transportConfiguration, JsonNode.class);
  156 + if(null != jsonNode)
  157 + {
  158 + String authScriptId = String.valueOf(jsonNode.get("authScriptId"));
  159 + String upScriptId = String.valueOf(jsonNode.get("upScriptId"));
  160 + List<String> list = List.of(authScriptId,upScriptId);
  161 + if(null != list && !list.isEmpty()){
  162 + String scriptId = list.get(FastIotConstants.MagicNumber.ZERO);
  163 + if(list.size()>FastIotConstants.MagicNumber.ONE){
  164 + scriptId = StringUtils.join(list,",");
  165 + }
  166 + profile.setScriptId(scriptId);
  167 + }
  168 + }
  169 + }
130 170
131 171 @Override
132 172 @Transactional
... ...
... ... @@ -97,6 +97,14 @@ public class TkDeviceScriptServiceImpl
97 97 if (!entity.getTenantId().equals(tenantId)) {
98 98 throw new TkDataValidationException(ErrorMessage.NOT_BELONG_CURRENT_TENANT.getMessage());
99 99 }
  100 + if(Objects.equals(status,StatusEnum.DISABLE.getIndex())){
  101 + List<DeviceProfileDTO> profileList = tkDeviceProfileService.findDeviceProfileByScriptId(tenantId,id);
  102 + if(null != profileList && !profileList.isEmpty()){
  103 +
  104 + throw new TkDataValidationException(String.format(ErrorMessage.CURRENT_DATA_IN_USE.getMessage(),
  105 + profileList.get(0).getName()));
  106 + }
  107 + }
100 108 entity.setStatus(status);
101 109 return baseMapper.updateById(entity) > FastIotConstants.MagicNumber.ZERO;
102 110 }
... ...
... ... @@ -53,4 +53,12 @@ public interface TkDeviceProfileService extends BaseService<TkDeviceProfileEntit
53 53 * @return 设备配置信息
54 54 */
55 55 DeviceProfileDTO findByTbDeviceProfileId(String tenantId, String tbProfileId);
  56 +
  57 + /**
  58 + * 根据脚本ID查询,使用的产品
  59 + * @param tenantId 租户ID
  60 + * @param scriptId 脚本ID
  61 + * @return 产品列表
  62 + */
  63 + List<DeviceProfileDTO> findDeviceProfileByScriptId(String tenantId,String scriptId);
56 64 }
... ...