...
|
...
|
@@ -57,540 +57,545 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant. |
57
|
57
|
@Api(tags = {"设备管理"})
|
58
|
58
|
@Slf4j
|
59
|
59
|
public class TkDeviceController extends BaseController {
|
60
|
|
- private final TkDeviceService tkdeviceService;
|
61
|
|
- private final TkDeviceProfileService ytDeviceProfileService;
|
62
|
|
- private final TkAlarmInfoService tkAlarmInfoService ;
|
63
|
|
- private final TbAlarmService tbAlarmService;
|
64
|
|
- private final TbDeviceService tbDeviceService;
|
65
|
|
- private final TbEntityRelationService tbEntityRelationService;
|
66
|
|
- private final TkOrganizationService organizationService;
|
67
|
|
- private final CacheUtils cacheUtils;
|
68
|
|
- private final TkVideoChannelService tkVideoChannelService;
|
69
|
|
- private final TkVideoService tkVideoService;
|
70
|
|
- @PostMapping("/batch/update")
|
71
|
|
- @ApiOperation("批量修改设备")
|
72
|
|
- @PreAuthorize(
|
73
|
|
- "@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:post','api:yt:device:update'})")
|
74
|
|
- public ResponseEntity<String> batchUpdateDevice(
|
75
|
|
- @Validated(AddGroup.class) @RequestBody List<DeviceDTO> deviceDTO,String orgId) {
|
76
|
|
- deviceDTO.forEach(i ->{
|
77
|
|
- i.setOrganizationId(orgId);
|
|
60
|
+ private final TkDeviceService tkdeviceService;
|
|
61
|
+ private final TkDeviceProfileService ytDeviceProfileService;
|
|
62
|
+ private final TkAlarmInfoService tkAlarmInfoService;
|
|
63
|
+ private final TbAlarmService tbAlarmService;
|
|
64
|
+ private final TbDeviceService tbDeviceService;
|
|
65
|
+ private final TbEntityRelationService tbEntityRelationService;
|
|
66
|
+ private final TkOrganizationService organizationService;
|
|
67
|
+ private final CacheUtils cacheUtils;
|
|
68
|
+ private final TkVideoChannelService tkVideoChannelService;
|
|
69
|
+ private final TkVideoService tkVideoService;
|
|
70
|
+
|
|
71
|
+ @PostMapping("/batch/update")
|
|
72
|
+ @ApiOperation("批量修改设备")
|
|
73
|
+ @PreAuthorize(
|
|
74
|
+ "@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:post','api:yt:device:update'})")
|
|
75
|
+ public ResponseEntity<String> batchUpdateDevice(
|
|
76
|
+ @Validated(AddGroup.class) @RequestBody List<DeviceDTO> deviceDTO, String orgId) {
|
|
77
|
+ deviceDTO.forEach(i -> {
|
|
78
|
+ i.setOrganizationId(orgId);
|
|
79
|
+ try {
|
|
80
|
+ saveDevice(i);
|
|
81
|
+ } catch (Exception e) {
|
|
82
|
+ e.printStackTrace();
|
|
83
|
+ }
|
|
84
|
+ });
|
|
85
|
+ return ResponseEntity.ok(MessageUtils.message("update.success"));
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ @PostMapping
|
|
89
|
+ @ApiOperation("创建|编辑")
|
|
90
|
+ @PreAuthorize(
|
|
91
|
+ "@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:post','api:yt:device:update'})")
|
|
92
|
+ public ResponseEntity<DeviceDTO> saveDevice(
|
|
93
|
+ @Validated(AddGroup.class) @RequestBody DeviceDTO deviceDTO)
|
|
94
|
+ throws ThingsboardException, ExecutionException, InterruptedException {
|
|
95
|
+ return saveDeviceInterface(deviceDTO);
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ public ResponseEntity<DeviceDTO> saveDeviceInterface(DeviceDTO deviceDTO) throws ThingsboardException, ExecutionException, InterruptedException {
|
|
99
|
+ tkdeviceService.validateFormData(getCurrentUser().getTenantId(), deviceDTO);
|
|
100
|
+
|
|
101
|
+ /** 网关是否有效 */
|
|
102
|
+ String gatewayId = deviceDTO.getGatewayId();
|
|
103
|
+ Device gateWay = null;
|
|
104
|
+ if (StringUtils.isNotEmpty(gatewayId)) {
|
|
105
|
+ gateWay = checkDeviceId(DeviceId.fromString(gatewayId), Operation.READ);
|
|
106
|
+ if (null == gateWay) {
|
|
107
|
+ throw new TkDataValidationException(
|
|
108
|
+ MessageUtils.message(ErrorMessage.DEVICE_NOT_EXISTENCE_IN_TENANT.getI18nCode()));
|
|
109
|
+ }
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ boolean created = null == deviceDTO.getId();
|
|
113
|
+ Device savedDevice = null;
|
78
|
114
|
try {
|
79
|
|
- saveDevice(i);
|
|
115
|
+ /**
|
|
116
|
+ * 子设备编辑业务逻辑: 设备地址码必须同时设置到附加信息字段内。 1、新增或编辑网关和直连设备 2、新增网关子设备 3、编辑网关子设备时,关联关系已存在(未切换网关)
|
|
117
|
+ * 4、编辑网关子设备时,关联关系不存在(切换网关) 5、编辑网关子设备时,修改其它设备信息,例如:设备名称等。
|
|
118
|
+ */
|
|
119
|
+ Device tbDevice = tkdeviceService.buildTbDeviceFromDeviceDTO(getCurrentUser().getTenantId(), deviceDTO);
|
|
120
|
+ savedDevice = updateTbDevice(tbDevice, deviceDTO.getDeviceToken(), created);
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+ if (deviceDTO.getDeviceType().equals(DeviceTypeEnum.SENSOR)
|
|
124
|
+ && gatewayId != null) {
|
|
125
|
+ boolean relationNotMatched = true;
|
|
126
|
+
|
|
127
|
+ DeviceId slaveId = savedDevice.getId();
|
|
128
|
+ List<EntityRelationInfo> relations =
|
|
129
|
+ relationService.findInfoByTo(getTenantId(), slaveId, RelationTypeGroup.COMMON).get();
|
|
130
|
+
|
|
131
|
+ for (EntityRelationInfo relationInfo : relations) {
|
|
132
|
+ if (!FastIotConstants.Relation.relationType.equals(relationInfo.getType())) {
|
|
133
|
+ continue;
|
|
134
|
+ }
|
|
135
|
+ if (relationInfo.getFrom().equals(gateWay.getId())) {
|
|
136
|
+ relationNotMatched = false;
|
|
137
|
+ } else {
|
|
138
|
+ relationService.deleteRelation(getTenantId(), relationInfo);
|
|
139
|
+ }
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ if (relationNotMatched) {
|
|
143
|
+ addRelation(getTenantId(), gateWay.getId(), slaveId);
|
|
144
|
+ }
|
|
145
|
+ }
|
80
|
146
|
} catch (Exception e) {
|
81
|
|
- e.printStackTrace();
|
|
147
|
+ throw new TkDataValidationException(e.getMessage());
|
|
148
|
+
|
82
|
149
|
}
|
83
|
|
- });
|
84
|
|
- return ResponseEntity.ok(MessageUtils.message("update.success"));
|
85
|
|
- }
|
86
|
|
-
|
87
|
|
- @PostMapping
|
88
|
|
- @ApiOperation("创建|编辑")
|
89
|
|
- @PreAuthorize(
|
90
|
|
- "@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:post','api:yt:device:update'})")
|
91
|
|
- public ResponseEntity<DeviceDTO> saveDevice(
|
92
|
|
- @Validated(AddGroup.class) @RequestBody DeviceDTO deviceDTO)
|
93
|
|
- throws ThingsboardException, ExecutionException, InterruptedException {
|
94
|
|
- tkdeviceService.validateFormData(getCurrentUser().getTenantId(), deviceDTO);
|
95
|
|
-
|
96
|
|
- /** 网关是否有效 */
|
97
|
|
- String gatewayId = deviceDTO.getGatewayId();
|
98
|
|
- Device gateWay = null;
|
99
|
|
- if (StringUtils.isNotEmpty(gatewayId)) {
|
100
|
|
- gateWay = checkDeviceId(DeviceId.fromString(gatewayId), Operation.READ);
|
101
|
|
- if (null == gateWay) {
|
102
|
|
- throw new TkDataValidationException(
|
103
|
|
- MessageUtils.message(ErrorMessage.DEVICE_NOT_EXISTENCE_IN_TENANT.getI18nCode()));
|
104
|
|
- }
|
|
150
|
+ CopyUtils.copyProperties(savedDevice, deviceDTO);
|
|
151
|
+ return ResponseEntity.ok(deviceDTO);
|
105
|
152
|
}
|
106
|
153
|
|
107
|
|
- boolean created = null ==deviceDTO.getId();
|
108
|
|
- Device savedDevice = null;
|
109
|
|
- try {
|
110
|
|
- /**
|
111
|
|
- * 子设备编辑业务逻辑: 设备地址码必须同时设置到附加信息字段内。 1、新增或编辑网关和直连设备 2、新增网关子设备 3、编辑网关子设备时,关联关系已存在(未切换网关)
|
112
|
|
- * 4、编辑网关子设备时,关联关系不存在(切换网关) 5、编辑网关子设备时,修改其它设备信息,例如:设备名称等。
|
113
|
|
- */
|
114
|
|
- Device tbDevice = tkdeviceService.buildTbDeviceFromDeviceDTO(getCurrentUser().getTenantId(), deviceDTO);
|
115
|
|
- savedDevice = updateTbDevice(tbDevice, deviceDTO.getDeviceToken(), created);
|
116
|
|
-
|
117
|
|
-
|
118
|
|
- if (deviceDTO.getDeviceType().equals(DeviceTypeEnum.SENSOR)
|
119
|
|
- && gatewayId != null) {
|
120
|
|
- boolean relationNotMatched = true;
|
121
|
|
-
|
122
|
|
- DeviceId slaveId = savedDevice.getId();
|
123
|
|
- List<EntityRelationInfo> relations =
|
124
|
|
- relationService.findInfoByTo(getTenantId(), slaveId, RelationTypeGroup.COMMON).get();
|
125
|
|
-
|
126
|
|
- for (EntityRelationInfo relationInfo : relations) {
|
127
|
|
- if (!FastIotConstants.Relation.relationType.equals(relationInfo.getType())) {
|
128
|
|
- continue;
|
129
|
|
- }
|
130
|
|
- if (relationInfo.getFrom().equals(gateWay.getId())) {
|
131
|
|
- relationNotMatched = false;
|
132
|
|
- } else {
|
133
|
|
- relationService.deleteRelation(getTenantId(), relationInfo);
|
134
|
|
- }
|
|
154
|
+ @GetMapping({"sn"})
|
|
155
|
+ @ApiOperation("自动生成设备SN")
|
|
156
|
+ public ResponseEntity<String> generate() {
|
|
157
|
+ String result = tkdeviceService.generateSn();
|
|
158
|
+ return ResponseEntity.ok(result);
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+ public Device updateTbDevice(
|
|
163
|
+ Device tbDevice, TkCredentialsDto formCredentials, boolean created)
|
|
164
|
+ throws Exception {
|
|
165
|
+ Device oldDevice = null;
|
|
166
|
+ if (!created) {
|
|
167
|
+ oldDevice = checkDeviceId(tbDevice.getId(), Operation.WRITE);
|
|
168
|
+ Optional.ofNullable(oldDevice).ifPresent(db -> {
|
|
169
|
+ if (!db.getDeviceProfileId().equals(tbDevice.getDeviceProfileId())) {
|
|
170
|
+ //更改了产品,需将原产品的缓存置为无效
|
|
171
|
+ cacheUtils.invalidate(FastIotConstants.CacheConfigKey.SCENE_REACT, tbDevice.getTenantId().toString() + "," + db.getDeviceProfileId().toString());
|
|
172
|
+ }
|
|
173
|
+ if (null != db.getDeviceInfo()) {
|
|
174
|
+ Optional.ofNullable(db.getDeviceInfo().get(FastIotConstants.DeviceAdditional.SIP)).ifPresent(sip -> {
|
|
175
|
+ ObjectNode additional = (ObjectNode) tbDevice.getDeviceInfo();
|
|
176
|
+ additional.set(FastIotConstants.DeviceAdditional.SIP, sip);
|
|
177
|
+ });
|
|
178
|
+ }
|
|
179
|
+ });
|
|
180
|
+ } else {
|
|
181
|
+ tbDevice.setAlarmStatus(0);
|
|
182
|
+ checkEntity(null, tbDevice, Resource.DEVICE);
|
|
183
|
+ }
|
|
184
|
+
|
|
185
|
+ Device savedDevice = tbDeviceService.save(tbDevice, oldDevice, null, getCurrentUser());
|
|
186
|
+ DeviceId tbDeviceId = savedDevice.getId();
|
|
187
|
+ DeviceCredentials deviceCredentials;
|
|
188
|
+ if (formCredentials != null && formCredentials.getCredentialsType() != null) {
|
|
189
|
+ deviceCredentials =
|
|
190
|
+ checkNotNull(
|
|
191
|
+ deviceCredentialsService.findDeviceCredentialsByDeviceId(
|
|
192
|
+ getCurrentUser().getTenantId(), tbDeviceId));
|
|
193
|
+ deviceCredentials.setCredentialsId(formCredentials.getCredentialsId());
|
|
194
|
+ deviceCredentials.setCredentialsType(formCredentials.getCredentialsType());
|
|
195
|
+ deviceCredentials.setCredentialsValue(formCredentials.getCredentialsValue());
|
|
196
|
+
|
|
197
|
+ checkNotNull(deviceCredentials);
|
|
198
|
+ Device device = checkDeviceId(deviceCredentials.getDeviceId(), Operation.WRITE_CREDENTIALS);
|
|
199
|
+ tbDeviceService.updateDeviceCredentials(device, deviceCredentials, getCurrentUser());
|
|
200
|
+ }
|
|
201
|
+ //如果是网关设备,且网关修改了其组织
|
|
202
|
+ if (oldDevice != null && savedDevice.getDeviceType().equals(DeviceTypeEnum.GATEWAY) && !oldDevice.getOrganizationId().equals(savedDevice.getOrganizationId())) {
|
|
203
|
+ updateSensorDeviceOrganization(savedDevice.getTenantId(), savedDevice.getId(), savedDevice.getOrganizationId());
|
135
|
204
|
}
|
|
205
|
+ return savedDevice;
|
|
206
|
+ }
|
136
|
207
|
|
137
|
|
- if (relationNotMatched) {
|
138
|
|
- addRelation(getTenantId(), gateWay.getId(), slaveId);
|
|
208
|
+ private void updateSensorDeviceOrganization(TenantId tenantId, DeviceId gatewayId, String newOrganizationId) throws ThingsboardException {
|
|
209
|
+ //查询新组织下的所有组织
|
|
210
|
+ List<String> allOrganization = organizationService.organizationAllIds(tenantId.toString(), newOrganizationId);
|
|
211
|
+ SecurityUser loginUser = getCurrentUser();
|
|
212
|
+ //网关子设备没在该网关新组织下,则同步更新为网关的新组织
|
|
213
|
+ if (null != allOrganization && !allOrganization.isEmpty()) {
|
|
214
|
+ List<Device> list = tkdeviceService.findDevicesByGatewaySwitchOrgId(tenantId, newOrganizationId, gatewayId);
|
|
215
|
+ if (!list.isEmpty()) {
|
|
216
|
+ list.forEach(obj -> {
|
|
217
|
+ Device newObj = new Device(obj);
|
|
218
|
+ newObj.setOrganizationId(newOrganizationId);
|
|
219
|
+ try {
|
|
220
|
+ tbDeviceService.save(newObj, obj, null, loginUser);
|
|
221
|
+ } catch (Exception e) {
|
|
222
|
+ throw new RuntimeException(e);
|
|
223
|
+ }
|
|
224
|
+ });
|
|
225
|
+ }
|
139
|
226
|
}
|
140
|
|
- }
|
141
|
|
- } catch (Exception e) {
|
142
|
|
- throw new TkDataValidationException(e.getMessage());
|
|
227
|
+ }
|
143
|
228
|
|
|
229
|
+ @GetMapping("{id}")
|
|
230
|
+ @ApiOperation("详情")
|
|
231
|
+ @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{'api:yt:device:get'})")
|
|
232
|
+ public ResponseEntity<DeviceDTO> getDevice(@PathVariable("id") String id)
|
|
233
|
+ throws ThingsboardException {
|
|
234
|
+ return ResponseEntity.of(tkdeviceService.getDevice(getCurrentUser().getTenantId().getId(), UUID.fromString(id)));
|
144
|
235
|
}
|
145
|
|
- CopyUtils.copyProperties(savedDevice,deviceDTO);
|
146
|
|
- return ResponseEntity.ok(deviceDTO);
|
147
|
|
- }
|
148
|
|
-
|
149
|
|
- @GetMapping({"sn"})
|
150
|
|
- @ApiOperation("自动生成设备SN")
|
151
|
|
- public ResponseEntity<String> generate() {
|
152
|
|
- String result = tkdeviceService.generateSn();
|
153
|
|
- return ResponseEntity.ok(result);
|
154
|
|
- }
|
155
|
|
-
|
156
|
|
-
|
157
|
|
- public Device updateTbDevice(
|
158
|
|
- Device tbDevice, TkCredentialsDto formCredentials, boolean created)
|
159
|
|
- throws Exception {
|
160
|
|
- Device oldDevice = null;
|
161
|
|
- if (!created) {
|
162
|
|
- oldDevice = checkDeviceId(tbDevice.getId(), Operation.WRITE);
|
163
|
|
- Optional.ofNullable(oldDevice).ifPresent(db->{
|
164
|
|
- if(!db.getDeviceProfileId().equals(tbDevice.getDeviceProfileId())){
|
165
|
|
- //更改了产品,需将原产品的缓存置为无效
|
166
|
|
- cacheUtils.invalidate(FastIotConstants.CacheConfigKey.SCENE_REACT,tbDevice.getTenantId().toString()+","+db.getDeviceProfileId().toString());
|
|
236
|
+
|
|
237
|
+ @GetMapping("/public/{id}")
|
|
238
|
+ @ApiOperation("Public客户调用公开设备详情")
|
|
239
|
+ @PreAuthorize("hasAnyAuthority('CUSTOMER_USER')")
|
|
240
|
+ public ResponseEntity<DeviceDTO> getPublicDevice(@PathVariable("id") String id)
|
|
241
|
+ throws ThingsboardException {
|
|
242
|
+ return ResponseEntity.of(tkdeviceService.getDevice(getCurrentUser().getTenantId().getId(), UUID.fromString(id)));
|
|
243
|
+ }
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+ @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER','OPENAPI_USER')")
|
|
247
|
+ @PostMapping(path = {"/page"})
|
|
248
|
+ @ApiOperation("查询")
|
|
249
|
+ public TkPageData<DeviceDTO> getPage(
|
|
250
|
+ @RequestBody Map<String, Object> queryMap)
|
|
251
|
+ throws ThingsboardException {
|
|
252
|
+ return pageDevice((Integer) queryMap.get("pageSize"), (Integer) queryMap.get("page"), queryMap);
|
|
253
|
+ }
|
|
254
|
+
|
|
255
|
+ @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER','OPENAPI_USER')")
|
|
256
|
+ @PostMapping(params = {PAGE_SIZE, PAGE})
|
|
257
|
+ @ApiOperation("查询")
|
|
258
|
+ public TkPageData<DeviceDTO> pageDevice(
|
|
259
|
+ @RequestParam(PAGE_SIZE) int pageSize,
|
|
260
|
+ @RequestParam(PAGE) int page,
|
|
261
|
+ @RequestBody Map<String, Object> queryMap)
|
|
262
|
+ throws ThingsboardException {
|
|
263
|
+ queryMap.put(PAGE, page);
|
|
264
|
+ queryMap.put(PAGE_SIZE, pageSize);
|
|
265
|
+ // 如果当前用户是客户
|
|
266
|
+ if (getCurrentUser().isCustomerUser()) {
|
|
267
|
+ queryMap.put("customerId", getCurrentUser().getCustomerId().getId());
|
|
268
|
+ }
|
|
269
|
+ //如果当前用户是普通租户或者openapi用戶
|
|
270
|
+ if (getCurrentUser().isPtCommonTenant() || getCurrentUser().isOpenApiUser()) {
|
|
271
|
+ List<String> organizationIds = commonTenantOrganizationAllIds();
|
|
272
|
+ if (null != organizationIds && organizationIds.size() > 0) {
|
|
273
|
+ queryMap.put("organizationIds", organizationIds);
|
|
274
|
+ }
|
|
275
|
+ }
|
|
276
|
+ DeviceState deviceState =
|
|
277
|
+ null != queryMap.get("deviceState")
|
|
278
|
+ && StringUtils.isNotEmpty(queryMap.get("deviceState").toString())
|
|
279
|
+ ? DeviceState.valueOf(queryMap.get("deviceState").toString())
|
|
280
|
+ : null;
|
|
281
|
+ if (deviceState != null) {
|
|
282
|
+ queryMap.put("deviceState", deviceState.name());
|
167
|
283
|
}
|
168
|
|
- if(null !=db.getDeviceInfo()){
|
169
|
|
- Optional.ofNullable(db.getDeviceInfo().get(FastIotConstants.DeviceAdditional.SIP)).ifPresent(sip ->{
|
170
|
|
- ObjectNode additional = (ObjectNode) tbDevice.getDeviceInfo();
|
171
|
|
- additional.set(FastIotConstants.DeviceAdditional.SIP,sip);
|
172
|
|
- });
|
|
284
|
+ queryMap.put("userId", getCurrentUser().getId().toString());
|
|
285
|
+ return tkdeviceService.page(getCurrentUser().getCurrentTenantId(), queryMap);
|
|
286
|
+ }
|
|
287
|
+
|
|
288
|
+ @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
|
289
|
+ @GetMapping(
|
|
290
|
+ path = {"/relation"},
|
|
291
|
+ params = {PAGE_SIZE, PAGE})
|
|
292
|
+ @ApiOperation("子设备查询")
|
|
293
|
+ public TkPageData<RelationDeviceDTO> pageRelationDevice(
|
|
294
|
+ @RequestParam(PAGE_SIZE) int pageSize,
|
|
295
|
+ @RequestParam(PAGE) int page,
|
|
296
|
+ @RequestParam(value = "name", required = false) String name,
|
|
297
|
+ @RequestParam(value = "deviceState", required = false) DeviceState deviceState,
|
|
298
|
+ @RequestParam(value = "fromId") String fromId,
|
|
299
|
+ @RequestParam(value = ORDER_FILED, required = false) String orderBy,
|
|
300
|
+ @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType)
|
|
301
|
+ throws ThingsboardException {
|
|
302
|
+ HashMap<String, Object> queryMap = new HashMap<>();
|
|
303
|
+ queryMap.put(PAGE_SIZE, pageSize);
|
|
304
|
+ queryMap.put(PAGE, page);
|
|
305
|
+ queryMap.put(ORDER_FILED, orderBy);
|
|
306
|
+ queryMap.put("name", name);
|
|
307
|
+ queryMap.put("fromId", UUID.fromString(fromId));
|
|
308
|
+ queryMap.put("tenantId", UUID.fromString(getCurrentUser().getCurrentTenantId()));
|
|
309
|
+ if (deviceState != null) {
|
|
310
|
+ queryMap.put("deviceState", deviceState.name());
|
|
311
|
+ }
|
|
312
|
+ if (orderType != null) {
|
|
313
|
+ queryMap.put(ORDER_TYPE, orderType.name());
|
|
314
|
+ }
|
|
315
|
+ // 如果当前用户是客户
|
|
316
|
+ if (getCurrentUser().isCustomerUser()) {
|
|
317
|
+ queryMap.put("customerId", getCurrentUser().getCustomerId().getId());
|
173
|
318
|
}
|
174
|
|
- });
|
175
|
|
- } else {
|
176
|
|
- tbDevice.setAlarmStatus(0);
|
177
|
|
- checkEntity(null, tbDevice, Resource.DEVICE);
|
|
319
|
+ //如果当前用户是普通租户
|
|
320
|
+ if (getCurrentUser().isPtCommonTenant()) {
|
|
321
|
+ List<String> organizationIds = commonTenantOrganizationAllIds();
|
|
322
|
+ if (null != organizationIds && organizationIds.size() > 0) {
|
|
323
|
+ queryMap.put("organizationIds", organizationIds);
|
|
324
|
+ }
|
|
325
|
+ }
|
|
326
|
+ return tkdeviceService.pageRelation(queryMap);
|
|
327
|
+ }
|
|
328
|
+
|
|
329
|
+ @PostMapping("/import")
|
|
330
|
+ @ApiOperation("导入配置")
|
|
331
|
+ @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:import'})")
|
|
332
|
+ public ResponseEntity<String> importDeviceProfile() {
|
|
333
|
+ // TODO 实现的业务功能
|
|
334
|
+ return ResponseEntity.ok("");
|
178
|
335
|
}
|
179
|
336
|
|
180
|
|
- Device savedDevice = tbDeviceService.save(tbDevice, oldDevice, null, getCurrentUser());
|
181
|
|
- DeviceId tbDeviceId = savedDevice.getId();
|
182
|
|
- DeviceCredentials deviceCredentials;
|
183
|
|
- if (formCredentials != null && formCredentials.getCredentialsType() != null) {
|
184
|
|
- deviceCredentials =
|
185
|
|
- checkNotNull(
|
186
|
|
- deviceCredentialsService.findDeviceCredentialsByDeviceId(
|
187
|
|
- getCurrentUser().getTenantId(), tbDeviceId));
|
188
|
|
- deviceCredentials.setCredentialsId(formCredentials.getCredentialsId());
|
189
|
|
- deviceCredentials.setCredentialsType(formCredentials.getCredentialsType());
|
190
|
|
- deviceCredentials.setCredentialsValue(formCredentials.getCredentialsValue());
|
191
|
|
-
|
192
|
|
- checkNotNull(deviceCredentials);
|
193
|
|
- Device device = checkDeviceId(deviceCredentials.getDeviceId(), Operation.WRITE_CREDENTIALS);
|
194
|
|
- tbDeviceService.updateDeviceCredentials(device, deviceCredentials, getCurrentUser());
|
195
|
|
- }
|
196
|
|
- //如果是网关设备,且网关修改了其组织
|
197
|
|
- if(oldDevice !=null && savedDevice.getDeviceType().equals(DeviceTypeEnum.GATEWAY) && !oldDevice.getOrganizationId().equals(savedDevice.getOrganizationId())){
|
198
|
|
- updateSensorDeviceOrganization(savedDevice.getTenantId(),savedDevice.getId(),savedDevice.getOrganizationId());
|
199
|
|
- }
|
200
|
|
- return savedDevice;
|
201
|
|
- }
|
202
|
|
- private void updateSensorDeviceOrganization(TenantId tenantId,DeviceId gatewayId,String newOrganizationId) throws ThingsboardException {
|
203
|
|
- //查询新组织下的所有组织
|
204
|
|
- List<String> allOrganization = organizationService.organizationAllIds(tenantId.toString(),newOrganizationId);
|
205
|
|
- SecurityUser loginUser = getCurrentUser();
|
206
|
|
- //网关子设备没在该网关新组织下,则同步更新为网关的新组织
|
207
|
|
- if(null != allOrganization && !allOrganization.isEmpty()){
|
208
|
|
- List<Device> list = tkdeviceService.findDevicesByGatewaySwitchOrgId(tenantId,newOrganizationId,gatewayId);
|
209
|
|
- if(!list.isEmpty()){
|
210
|
|
- list.forEach(obj->{
|
211
|
|
- Device newObj = new Device(obj);
|
212
|
|
- newObj.setOrganizationId(newOrganizationId);
|
213
|
|
- try {
|
214
|
|
- tbDeviceService.save(newObj, obj, null, loginUser);
|
215
|
|
- } catch (Exception e) {
|
216
|
|
- throw new RuntimeException(e);
|
217
|
|
- }
|
|
337
|
+ @PostMapping("/export")
|
|
338
|
+ @ApiOperation("导出")
|
|
339
|
+ @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:export'})")
|
|
340
|
+ public ResponseEntity<String> exportDeviceProfile() {
|
|
341
|
+ // TODO 实现的业务功能
|
|
342
|
+ return ResponseEntity.ok("");
|
|
343
|
+ }
|
|
344
|
+
|
|
345
|
+ @DeleteMapping
|
|
346
|
+ @ApiOperation("删除")
|
|
347
|
+ @Transactional
|
|
348
|
+ @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:delete'})")
|
|
349
|
+ public void deleteDevices(@Validated({DeleteGroup.class}) @RequestBody DeleteDTO deleteDTO)
|
|
350
|
+ throws ThingsboardException {
|
|
351
|
+ String currentTenantId = getCurrentUser().getCurrentTenantId();
|
|
352
|
+ List<String> tbIds = tkdeviceService.findTbDeviceIdAndCheckForDelete(currentTenantId, deleteDTO.getIds());
|
|
353
|
+ if (null != tbIds) {
|
|
354
|
+ for (String id : tbIds) {
|
|
355
|
+ deleteTbDevice(UUID.fromString(id));
|
|
356
|
+ deleteAlarm(id);
|
|
357
|
+ }
|
|
358
|
+ tkVideoChannelService.clearDeviceChannelByDeviceIds(currentTenantId, tbIds);
|
|
359
|
+ tkVideoService.deleteGBT28181VideosByDeviceIds(currentTenantId, tbIds);
|
|
360
|
+ }
|
|
361
|
+ }
|
|
362
|
+
|
|
363
|
+ private void deleteAlarm(String id) throws ThingsboardException {
|
|
364
|
+ //根据设备的tbId查询当前设备的所有告警
|
|
365
|
+ List<String> alarmIds = tkAlarmInfoService.getByTbDeviceId(getTenantId().toString(), id);
|
|
366
|
+ if (alarmIds.isEmpty()) {
|
|
367
|
+ return;
|
|
368
|
+ }
|
|
369
|
+ alarmIds.forEach(strAlarmId -> {
|
|
370
|
+ //此处参考AlarmController中的deleteAlarm方法
|
|
371
|
+ try {
|
|
372
|
+ AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
|
|
373
|
+ Alarm alarm = checkAlarmId(alarmId, Operation.WRITE);
|
|
374
|
+ tbAlarmService.delete(alarm, getCurrentUser());
|
|
375
|
+ } catch (Exception e) {
|
|
376
|
+ e.printStackTrace();
|
|
377
|
+ }
|
218
|
378
|
});
|
219
|
|
- }
|
220
|
379
|
}
|
221
|
|
- }
|
222
|
|
-
|
223
|
|
- @GetMapping("{id}")
|
224
|
|
- @ApiOperation("详情")
|
225
|
|
- @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{'api:yt:device:get'})")
|
226
|
|
- public ResponseEntity<DeviceDTO> getDevice(@PathVariable("id") String id)
|
227
|
|
- throws ThingsboardException {
|
228
|
|
- return ResponseEntity.of(tkdeviceService.getDevice(getCurrentUser().getTenantId().getId(), UUID.fromString(id)));
|
229
|
|
- }
|
230
|
|
-
|
231
|
|
- @GetMapping("/public/{id}")
|
232
|
|
- @ApiOperation("Public客户调用公开设备详情")
|
233
|
|
- @PreAuthorize("hasAnyAuthority('CUSTOMER_USER')")
|
234
|
|
- public ResponseEntity<DeviceDTO> getPublicDevice(@PathVariable("id") String id)
|
235
|
|
- throws ThingsboardException {
|
236
|
|
- return ResponseEntity.of(tkdeviceService.getDevice(getCurrentUser().getTenantId().getId(), UUID.fromString(id)));
|
237
|
|
- }
|
238
|
|
-
|
239
|
|
-
|
240
|
|
-
|
241
|
|
- @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER','OPENAPI_USER')")
|
242
|
|
- @PostMapping(path = {"/page"})
|
243
|
|
- @ApiOperation("查询")
|
244
|
|
- public TkPageData<DeviceDTO> getPage(
|
245
|
|
- @RequestBody Map<String, Object> queryMap)
|
246
|
|
- throws ThingsboardException {
|
247
|
|
- return pageDevice((Integer) queryMap.get("pageSize"),(Integer) queryMap.get("page"),queryMap);
|
248
|
|
- }
|
249
|
|
-
|
250
|
|
- @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER','OPENAPI_USER')")
|
251
|
|
- @PostMapping(params = {PAGE_SIZE, PAGE})
|
252
|
|
- @ApiOperation("查询")
|
253
|
|
- public TkPageData<DeviceDTO> pageDevice(
|
254
|
|
- @RequestParam(PAGE_SIZE) int pageSize,
|
255
|
|
- @RequestParam(PAGE) int page,
|
256
|
|
- @RequestBody Map<String, Object> queryMap)
|
257
|
|
- throws ThingsboardException {
|
258
|
|
- queryMap.put(PAGE, page);
|
259
|
|
- queryMap.put(PAGE_SIZE, pageSize);
|
260
|
|
- // 如果当前用户是客户
|
261
|
|
- if (getCurrentUser().isCustomerUser()) {
|
262
|
|
- queryMap.put("customerId", getCurrentUser().getCustomerId().getId());
|
|
380
|
+
|
|
381
|
+ private void deleteTbDevice(UUID id) throws ThingsboardException {
|
|
382
|
+ DeviceId deviceId = new DeviceId(id);
|
|
383
|
+
|
|
384
|
+ Device device = checkDeviceId(deviceId, Operation.DELETE);
|
|
385
|
+
|
|
386
|
+ tbDeviceService.delete(device, getCurrentUser());
|
263
|
387
|
}
|
264
|
|
- //如果当前用户是普通租户或者openapi用戶
|
265
|
|
- if (getCurrentUser().isPtCommonTenant()||getCurrentUser().isOpenApiUser()) {
|
266
|
|
- List<String> organizationIds =commonTenantOrganizationAllIds();
|
267
|
|
- if(null!=organizationIds&&organizationIds.size()>0){
|
268
|
|
- queryMap.put("organizationIds", organizationIds);
|
269
|
|
- }
|
|
388
|
+
|
|
389
|
+ @GetMapping("/list")
|
|
390
|
+ @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
|
391
|
+ @ApiOperation("获取满足条件的所有设备")
|
|
392
|
+ public List<DeviceDTO> getDevices(
|
|
393
|
+ @ApiParam(value = "设备类型") @RequestParam(value = "deviceType", required = false) DeviceTypeEnum deviceType,
|
|
394
|
+ @ApiParam(value = "组织ID") @RequestParam(value = "organizationId", required = false) String organizationId,
|
|
395
|
+ @ApiParam(value = "设备标签") @RequestParam(value = "deviceLabel", required = false) String deviceLabel,
|
|
396
|
+ @ApiParam(value = "设备配置ID") @RequestParam(value = "deviceProfileId", required = false) String deviceProfileId,
|
|
397
|
+ @ApiParam(value = "传输协议类型") @RequestParam(value = "transportType", required = false) TransportTypeEnum transportType,
|
|
398
|
+ @ApiParam(value = "是否场景联动调用,是true") @RequestParam(value = "isSceneLinkage", required = false) Boolean isSceneLinkage,
|
|
399
|
+ @ApiParam(value = "是否边缘分配设备调用,是true") @RequestParam(value = "isEdgeDistribution", required = false) Boolean isEdgeDistribution,
|
|
400
|
+ @ApiParam(value = "edgeId") @RequestParam(value = "edgeId", required = false) String edgeId,
|
|
401
|
+ @ApiParam(value = "排除边端设备是true") @RequestParam(value = "isExcludeEdge", required = false) Boolean isExcludeEdge,
|
|
402
|
+ @ApiParam(value = "排除云端设备是true") @RequestParam(value = "isExcludeCloud", required = false) Boolean isExcludeCloud
|
|
403
|
+ )
|
|
404
|
+ throws ThingsboardException {
|
|
405
|
+ if (isExcludeEdge == null) {
|
|
406
|
+ isExcludeEdge = false;
|
|
407
|
+ }
|
|
408
|
+ if (isExcludeCloud == null) {
|
|
409
|
+ isExcludeCloud = false;
|
|
410
|
+ }
|
|
411
|
+ return tkdeviceService.findDevicesByDeviceTypeAndOrganizationId(
|
|
412
|
+ deviceType,
|
|
413
|
+ getCurrentUser().getTenantId().getId(),
|
|
414
|
+ organizationId,
|
|
415
|
+ deviceLabel,
|
|
416
|
+ StringUtils.isEmpty(deviceProfileId) ? null : UUID.fromString(deviceProfileId),
|
|
417
|
+ transportType,
|
|
418
|
+ isSceneLinkage,
|
|
419
|
+ isEdgeDistribution,
|
|
420
|
+ StringUtils.isEmpty(edgeId) ? null : UUID.fromString(edgeId),
|
|
421
|
+ isExcludeEdge,
|
|
422
|
+ isExcludeCloud
|
|
423
|
+ );
|
270
|
424
|
}
|
271
|
|
- DeviceState deviceState =
|
272
|
|
- null != queryMap.get("deviceState")
|
273
|
|
- && StringUtils.isNotEmpty(queryMap.get("deviceState").toString())
|
274
|
|
- ? DeviceState.valueOf(queryMap.get("deviceState").toString())
|
275
|
|
- : null;
|
276
|
|
- if (deviceState != null) {
|
277
|
|
- queryMap.put("deviceState", deviceState.name());
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+ @PostMapping("/getListByDeviceProfileIds")
|
|
428
|
+ @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
|
429
|
+ @ApiOperation("获取多种产品下的设备")
|
|
430
|
+ public List<DeviceDTO> getListByDeviceProfileIds(
|
|
431
|
+ @RequestBody(required = false) List<UUID> deviceProfileIds,
|
|
432
|
+ String organizationId)
|
|
433
|
+ throws ThingsboardException {
|
|
434
|
+ if (deviceProfileIds == null || deviceProfileIds.isEmpty()) {
|
|
435
|
+ throw new TkDataValidationException(MessageUtils.message(ErrorMessage.GET_DEVICE_LIST_ERROR.getI18nCode()));
|
|
436
|
+ }
|
|
437
|
+ return tkdeviceService.findDevicesByOrganizationIds(
|
|
438
|
+ getCurrentUser().getTenantId().getId(),
|
|
439
|
+ organizationId,
|
|
440
|
+ deviceProfileIds);
|
278
|
441
|
}
|
279
|
|
- queryMap.put("userId", getCurrentUser().getId().toString());
|
280
|
|
- return tkdeviceService.page(getCurrentUser().getCurrentTenantId(), queryMap);
|
281
|
|
- }
|
282
|
|
-
|
283
|
|
- @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
284
|
|
- @GetMapping(
|
285
|
|
- path = {"/relation"},
|
286
|
|
- params = {PAGE_SIZE, PAGE})
|
287
|
|
- @ApiOperation("子设备查询")
|
288
|
|
- public TkPageData<RelationDeviceDTO> pageRelationDevice(
|
289
|
|
- @RequestParam(PAGE_SIZE) int pageSize,
|
290
|
|
- @RequestParam(PAGE) int page,
|
291
|
|
- @RequestParam(value = "name", required = false) String name,
|
292
|
|
- @RequestParam(value = "deviceState", required = false) DeviceState deviceState,
|
293
|
|
- @RequestParam(value = "fromId") String fromId,
|
294
|
|
- @RequestParam(value = ORDER_FILED, required = false) String orderBy,
|
295
|
|
- @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType)
|
296
|
|
- throws ThingsboardException {
|
297
|
|
- HashMap<String, Object> queryMap = new HashMap<>();
|
298
|
|
- queryMap.put(PAGE_SIZE, pageSize);
|
299
|
|
- queryMap.put(PAGE, page);
|
300
|
|
- queryMap.put(ORDER_FILED, orderBy);
|
301
|
|
- queryMap.put("name", name);
|
302
|
|
- queryMap.put("fromId", UUID.fromString(fromId));
|
303
|
|
- queryMap.put("tenantId", UUID.fromString(getCurrentUser().getCurrentTenantId()));
|
304
|
|
- if (deviceState != null) {
|
305
|
|
- queryMap.put("deviceState", deviceState.name());
|
|
442
|
+
|
|
443
|
+ @GetMapping("/gateway/list")
|
|
444
|
+ @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
|
445
|
+ @ApiOperation("获取满足条件的所有设备")
|
|
446
|
+ public List<DeviceDTO> getGatewayDevices(
|
|
447
|
+ @ApiParam(value = "组织ID")
|
|
448
|
+ String organizationId,
|
|
449
|
+ @ApiParam(value = "网关id")
|
|
450
|
+ String gatewayId,
|
|
451
|
+ @ApiParam(value = "传输类型") @RequestParam(value = "transportType", required = false)
|
|
452
|
+ DeviceTransportType transportType)
|
|
453
|
+ throws ThingsboardException {
|
|
454
|
+ List<String> orgIds = null;
|
|
455
|
+ if (getCurrentUser().isPtCommonTenant()) {
|
|
456
|
+ orgIds = commonTenantOrganizationAllIds();
|
|
457
|
+ }
|
|
458
|
+ return tkdeviceService.findDevicesByTransportTypeAndOrganizationId(
|
|
459
|
+ getCurrentUser().getTenantId().getId(), orgIds, organizationId, transportType, gatewayId);
|
306
|
460
|
}
|
307
|
|
- if (orderType != null) {
|
308
|
|
- queryMap.put(ORDER_TYPE, orderType.name());
|
|
461
|
+
|
|
462
|
+ @GetMapping("/list/master/{organizationId}")
|
|
463
|
+ @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
|
464
|
+ @ApiOperation("主设备列表")
|
|
465
|
+ public List<SelectItemDTO> getMasterDevices(
|
|
466
|
+ @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId,
|
|
467
|
+ @ApiParam(value = "设备配置ID") @RequestParam(value = "deviceProfileId", required = false)
|
|
468
|
+ String deviceProfileId)
|
|
469
|
+ throws ThingsboardException {
|
|
470
|
+ return tkdeviceService.findMasterDevices(
|
|
471
|
+ getCurrentUser().getTenantId().getId(),
|
|
472
|
+ getCurrentUser().isCustomerUser() ? getCurrentUser().getCustomerId().getId() : null,
|
|
473
|
+ organizationId,
|
|
474
|
+ UUID.fromString(deviceProfileId));
|
309
|
475
|
}
|
310
|
|
- // 如果当前用户是客户
|
311
|
|
- if (getCurrentUser().isCustomerUser()) {
|
312
|
|
- queryMap.put("customerId", getCurrentUser().getCustomerId().getId());
|
|
476
|
+
|
|
477
|
+ @GetMapping("/list/slave/{organizationId}")
|
|
478
|
+ @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
|
479
|
+ @ApiOperation("从设备列表")
|
|
480
|
+ public List<SelectItemDTO> getSlaveDevices(
|
|
481
|
+ @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId,
|
|
482
|
+ @ApiParam(value = "主设备ID") @RequestParam(value = "masterId", required = false)
|
|
483
|
+ String masterId)
|
|
484
|
+ throws ThingsboardException {
|
|
485
|
+ return tkdeviceService.findSlaveDevices(
|
|
486
|
+ UUID.fromString(masterId),
|
|
487
|
+ getCurrentUser().getTenantId().getId(),
|
|
488
|
+ getCurrentUser().isCustomerUser() ? getCurrentUser().getCustomerId().getId() : null,
|
|
489
|
+ organizationId);
|
313
|
490
|
}
|
314
|
|
- //如果当前用户是普通租户
|
315
|
|
- if (getCurrentUser().isPtCommonTenant()) {
|
316
|
|
- List<String> organizationIds =commonTenantOrganizationAllIds();
|
317
|
|
- if(null!=organizationIds&&organizationIds.size()>0){
|
318
|
|
- queryMap.put("organizationIds", organizationIds);
|
319
|
|
- }
|
|
491
|
+
|
|
492
|
+ @GetMapping("/keys/{organizationId}")
|
|
493
|
+ @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
|
494
|
+ @ApiOperation("设备遥测指标名称")
|
|
495
|
+ public List<String> listKeys(
|
|
496
|
+ @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId,
|
|
497
|
+ @ApiParam(value = "设备ID") @RequestParam(value = "deviceIds", required = false)
|
|
498
|
+ List<UUID> deviceIds)
|
|
499
|
+ throws ThingsboardException {
|
|
500
|
+ return tkdeviceService.findDeviceKeys(
|
|
501
|
+ getCurrentUser().getTenantId().getId(),
|
|
502
|
+ getCurrentUser().isCustomerUser() ? getCurrentUser().getCustomerId().getId() : null,
|
|
503
|
+ organizationId,
|
|
504
|
+ deviceIds);
|
320
|
505
|
}
|
321
|
|
- return tkdeviceService.pageRelation(queryMap);
|
322
|
|
- }
|
323
|
|
-
|
324
|
|
- @PostMapping("/import")
|
325
|
|
- @ApiOperation("导入配置")
|
326
|
|
- @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:import'})")
|
327
|
|
- public ResponseEntity<String> importDeviceProfile() {
|
328
|
|
- // TODO 实现的业务功能
|
329
|
|
- return ResponseEntity.ok("");
|
330
|
|
- }
|
331
|
|
-
|
332
|
|
- @PostMapping("/export")
|
333
|
|
- @ApiOperation("导出")
|
334
|
|
- @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:export'})")
|
335
|
|
- public ResponseEntity<String> exportDeviceProfile() {
|
336
|
|
- // TODO 实现的业务功能
|
337
|
|
- return ResponseEntity.ok("");
|
338
|
|
- }
|
339
|
|
-
|
340
|
|
- @DeleteMapping
|
341
|
|
- @ApiOperation("删除")
|
342
|
|
- @Transactional
|
343
|
|
- @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:delete'})")
|
344
|
|
- public void deleteDevices(@Validated({DeleteGroup.class}) @RequestBody DeleteDTO deleteDTO)
|
345
|
|
- throws ThingsboardException {
|
346
|
|
- String currentTenantId = getCurrentUser().getCurrentTenantId();
|
347
|
|
- List<String> tbIds = tkdeviceService.findTbDeviceIdAndCheckForDelete(currentTenantId, deleteDTO.getIds());
|
348
|
|
- if(null !=tbIds){
|
349
|
|
- for (String id : tbIds) {
|
350
|
|
- deleteTbDevice(UUID.fromString(id));
|
351
|
|
- deleteAlarm(id);
|
352
|
|
- }
|
353
|
|
- tkVideoChannelService.clearDeviceChannelByDeviceIds(currentTenantId,tbIds);
|
354
|
|
- tkVideoService.deleteGBT28181VideosByDeviceIds(currentTenantId,tbIds);
|
|
506
|
+
|
|
507
|
+ @GetMapping("/gateway/{tbDeviceId}")
|
|
508
|
+ @ApiOperation("获取网关设备")
|
|
509
|
+ public DeviceDTO findGateWayDeviceByTbDeviceId(
|
|
510
|
+ @ApiParam(value = "tb设备Id") @PathVariable("tbDeviceId") String tbDeviceId)
|
|
511
|
+ throws ThingsboardException {
|
|
512
|
+ List<DeviceDTO> list =
|
|
513
|
+ tkdeviceService.findGateWayDeviceByTbDeviceId(
|
|
514
|
+ getCurrentUser().getTenantId().getId(), UUID.fromString(tbDeviceId));
|
|
515
|
+ return null != list && list.size() > FastIotConstants.MagicNumber.ZERO
|
|
516
|
+ ? list.get(FastIotConstants.MagicNumber.ZERO)
|
|
517
|
+ : null;
|
355
|
518
|
}
|
356
|
|
- }
|
357
|
|
-
|
358
|
|
- private void deleteAlarm(String id) throws ThingsboardException {
|
359
|
|
- //根据设备的tbId查询当前设备的所有告警
|
360
|
|
- List<String> alarmIds = tkAlarmInfoService.getByTbDeviceId(getTenantId().toString(),id);
|
361
|
|
- if(alarmIds.isEmpty()){
|
362
|
|
- return;
|
363
|
|
- }
|
364
|
|
- alarmIds.forEach(strAlarmId -> {
|
365
|
|
- //此处参考AlarmController中的deleteAlarm方法
|
366
|
|
- try {
|
367
|
|
- AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
|
368
|
|
- Alarm alarm = checkAlarmId(alarmId, Operation.WRITE);
|
369
|
|
- tbAlarmService.delete(alarm, getCurrentUser());
|
370
|
|
- } catch (Exception e) {
|
371
|
|
- e.printStackTrace();
|
372
|
|
- }
|
373
|
|
- });
|
374
|
|
- }
|
375
|
|
- private void deleteTbDevice(UUID id) throws ThingsboardException {
|
376
|
|
- DeviceId deviceId = new DeviceId(id);
|
377
|
|
-
|
378
|
|
- Device device = checkDeviceId(deviceId, Operation.DELETE);
|
379
|
|
-
|
380
|
|
- tbDeviceService.delete(device, getCurrentUser());
|
381
|
|
- }
|
382
|
|
-
|
383
|
|
- @GetMapping("/list")
|
384
|
|
- @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
385
|
|
- @ApiOperation("获取满足条件的所有设备")
|
386
|
|
- public List<DeviceDTO> getDevices(
|
387
|
|
- @ApiParam(value = "设备类型") @RequestParam(value = "deviceType", required = false) DeviceTypeEnum deviceType,
|
388
|
|
- @ApiParam(value = "组织ID") @RequestParam(value = "organizationId", required = false) String organizationId,
|
389
|
|
- @ApiParam(value = "设备标签") @RequestParam(value = "deviceLabel", required = false) String deviceLabel,
|
390
|
|
- @ApiParam(value = "设备配置ID") @RequestParam(value = "deviceProfileId", required = false) String deviceProfileId,
|
391
|
|
- @ApiParam(value = "传输协议类型") @RequestParam(value = "transportType", required = false) TransportTypeEnum transportType,
|
392
|
|
- @ApiParam(value = "是否场景联动调用,是true") @RequestParam(value = "isSceneLinkage", required = false) Boolean isSceneLinkage,
|
393
|
|
- @ApiParam(value = "是否边缘分配设备调用,是true") @RequestParam(value = "isEdgeDistribution", required = false) Boolean isEdgeDistribution,
|
394
|
|
- @ApiParam(value = "edgeId") @RequestParam(value = "edgeId", required = false) String edgeId,
|
395
|
|
- @ApiParam(value = "排除边端设备是true") @RequestParam(value = "isExcludeEdge", required = false) Boolean isExcludeEdge,
|
396
|
|
- @ApiParam(value = "排除云端设备是true") @RequestParam(value = "isExcludeCloud", required = false) Boolean isExcludeCloud
|
397
|
|
- )
|
398
|
|
- throws ThingsboardException {
|
399
|
|
- if (isExcludeEdge == null) {
|
400
|
|
- isExcludeEdge = false;
|
|
519
|
+
|
|
520
|
+ @GetMapping("/get_subset/{tbDeviceId}")
|
|
521
|
+ @ApiOperation("获取网关子设备信息")
|
|
522
|
+ public DeviceDTO getSubsetDeviceByTbDeviceId(
|
|
523
|
+ @ApiParam(value = "tb设备Id") @PathVariable("tbDeviceId") String tbDeviceId)
|
|
524
|
+ throws ThingsboardException {
|
|
525
|
+ return tkdeviceService.getSubsetDeviceByTbDeviceId(
|
|
526
|
+ getCurrentUser().getCurrentTenantId(), tbDeviceId);
|
401
|
527
|
}
|
402
|
|
- if (isExcludeCloud == null) {
|
403
|
|
- isExcludeCloud = false;
|
|
528
|
+
|
|
529
|
+ @GetMapping("/device/relation")
|
|
530
|
+ public String getDeviceRelation(
|
|
531
|
+ @ApiParam(value = "网关子设备:true 非网关子设备:false") @RequestParam(value = "isSlave") boolean isSlave,
|
|
532
|
+ @ApiParam(value = "设备ID") @RequestParam(value = "deviceId") String deviceId) {
|
|
533
|
+ return tkdeviceService.getDeviceRelation(isSlave, UUID.fromString(deviceId));
|
404
|
534
|
}
|
405
|
|
- return tkdeviceService.findDevicesByDeviceTypeAndOrganizationId(
|
406
|
|
- deviceType,
|
407
|
|
- getCurrentUser().getTenantId().getId(),
|
408
|
|
- organizationId,
|
409
|
|
- deviceLabel,
|
410
|
|
- StringUtils.isEmpty(deviceProfileId) ? null : UUID.fromString(deviceProfileId),
|
411
|
|
- transportType,
|
412
|
|
- isSceneLinkage,
|
413
|
|
- isEdgeDistribution,
|
414
|
|
- StringUtils.isEmpty(edgeId) ? null : UUID.fromString(edgeId),
|
415
|
|
- isExcludeEdge,
|
416
|
|
- isExcludeCloud
|
417
|
|
- );
|
418
|
|
- }
|
419
|
|
-
|
420
|
|
-
|
421
|
|
- @PostMapping("/getListByDeviceProfileIds")
|
422
|
|
- @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
423
|
|
- @ApiOperation("获取多种产品下的设备")
|
424
|
|
- public List<DeviceDTO> getListByDeviceProfileIds(
|
425
|
|
- @RequestBody (required = false)List<UUID> deviceProfileIds,
|
426
|
|
- String organizationId)
|
427
|
|
- throws ThingsboardException {
|
428
|
|
- if(deviceProfileIds==null||deviceProfileIds.isEmpty()){
|
429
|
|
- throw new TkDataValidationException(MessageUtils.message(ErrorMessage.GET_DEVICE_LIST_ERROR.getI18nCode()));
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+ /**
|
|
538
|
+ * 添加关联关系
|
|
539
|
+ *
|
|
540
|
+ * @param tenantId 租户ID
|
|
541
|
+ * @param gateWayTbDeviceId 网关设备Tb的设备ID
|
|
542
|
+ * @param deviceId 设备的tbDeviceId
|
|
543
|
+ */
|
|
544
|
+ private void addRelation(TenantId tenantId, DeviceId gateWayTbDeviceId, DeviceId deviceId)
|
|
545
|
+ throws ThingsboardException {
|
|
546
|
+ EntityRelation relation = new EntityRelation();
|
|
547
|
+ relation.setFrom(gateWayTbDeviceId);
|
|
548
|
+ relation.setTypeGroup(RelationTypeGroup.COMMON);
|
|
549
|
+ relation.setTo(deviceId);
|
|
550
|
+ relation.setType(FastIotConstants.Relation.relationType);
|
|
551
|
+ checkCanCreateRelation(relation.getTo());
|
|
552
|
+ if (relation.getTypeGroup() == null) {
|
|
553
|
+ relation.setTypeGroup(RelationTypeGroup.COMMON);
|
|
554
|
+ }
|
|
555
|
+
|
|
556
|
+ tbEntityRelationService.save(getTenantId(), getCurrentUser().getCustomerId(), relation, getCurrentUser());
|
430
|
557
|
}
|
431
|
|
- return tkdeviceService.findDevicesByOrganizationIds(
|
432
|
|
- getCurrentUser().getTenantId().getId(),
|
433
|
|
- organizationId,
|
434
|
|
- deviceProfileIds);
|
435
|
|
- }
|
436
|
|
-
|
437
|
|
- @GetMapping("/gateway/list")
|
438
|
|
- @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
439
|
|
- @ApiOperation("获取满足条件的所有设备")
|
440
|
|
- public List<DeviceDTO> getGatewayDevices(
|
441
|
|
- @ApiParam(value = "组织ID")
|
442
|
|
- String organizationId,
|
443
|
|
- @ApiParam(value = "网关id")
|
444
|
|
- String gatewayId,
|
445
|
|
- @ApiParam(value = "传输类型") @RequestParam(value = "transportType", required = false)
|
446
|
|
- DeviceTransportType transportType)
|
447
|
|
- throws ThingsboardException {
|
448
|
|
- List<String> orgIds = null;
|
449
|
|
- if(getCurrentUser().isPtCommonTenant()){
|
450
|
|
- orgIds= commonTenantOrganizationAllIds();
|
|
558
|
+
|
|
559
|
+ @GetMapping({"used/{id}"})
|
|
560
|
+ @ApiOperation("设备是否被占用")
|
|
561
|
+ public ResponseResult<Boolean> otherUsing(@PathVariable("id") String deviceId)
|
|
562
|
+ throws ThingsboardException {
|
|
563
|
+ String str = tkdeviceService.otherUsing(deviceId, getCurrentUser().getCurrentTenantId());
|
|
564
|
+ ResponseResult result = ResponseResult.success(StringUtils.isEmpty(str) ? true : false);
|
|
565
|
+ result.setMessage(str);
|
|
566
|
+ return result;
|
451
|
567
|
}
|
452
|
|
- return tkdeviceService.findDevicesByTransportTypeAndOrganizationId(
|
453
|
|
- getCurrentUser().getTenantId().getId(), orgIds,organizationId, transportType,gatewayId);
|
454
|
|
- }
|
455
|
|
-
|
456
|
|
- @GetMapping("/list/master/{organizationId}")
|
457
|
|
- @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
458
|
|
- @ApiOperation("主设备列表")
|
459
|
|
- public List<SelectItemDTO> getMasterDevices(
|
460
|
|
- @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId,
|
461
|
|
- @ApiParam(value = "设备配置ID") @RequestParam(value = "deviceProfileId", required = false)
|
462
|
|
- String deviceProfileId)
|
463
|
|
- throws ThingsboardException {
|
464
|
|
- return tkdeviceService.findMasterDevices(
|
465
|
|
- getCurrentUser().getTenantId().getId(),
|
466
|
|
- getCurrentUser().isCustomerUser() ? getCurrentUser().getCustomerId().getId() : null,
|
467
|
|
- organizationId,
|
468
|
|
- UUID.fromString(deviceProfileId));
|
469
|
|
- }
|
470
|
|
-
|
471
|
|
- @GetMapping("/list/slave/{organizationId}")
|
472
|
|
- @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
473
|
|
- @ApiOperation("从设备列表")
|
474
|
|
- public List<SelectItemDTO> getSlaveDevices(
|
475
|
|
- @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId,
|
476
|
|
- @ApiParam(value = "主设备ID") @RequestParam(value = "masterId", required = false)
|
477
|
|
- String masterId)
|
478
|
|
- throws ThingsboardException {
|
479
|
|
- return tkdeviceService.findSlaveDevices(
|
480
|
|
- UUID.fromString(masterId),
|
481
|
|
- getCurrentUser().getTenantId().getId(),
|
482
|
|
- getCurrentUser().isCustomerUser() ? getCurrentUser().getCustomerId().getId() : null,
|
483
|
|
- organizationId);
|
484
|
|
- }
|
485
|
|
-
|
486
|
|
- @GetMapping("/keys/{organizationId}")
|
487
|
|
- @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')")
|
488
|
|
- @ApiOperation("设备遥测指标名称")
|
489
|
|
- public List<String> listKeys(
|
490
|
|
- @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId,
|
491
|
|
- @ApiParam(value = "设备ID") @RequestParam(value = "deviceIds", required = false)
|
492
|
|
- List<UUID> deviceIds)
|
493
|
|
- throws ThingsboardException {
|
494
|
|
- return tkdeviceService.findDeviceKeys(
|
495
|
|
- getCurrentUser().getTenantId().getId(),
|
496
|
|
- getCurrentUser().isCustomerUser() ? getCurrentUser().getCustomerId().getId() : null,
|
497
|
|
- organizationId,
|
498
|
|
- deviceIds);
|
499
|
|
- }
|
500
|
|
-
|
501
|
|
- @GetMapping("/gateway/{tbDeviceId}")
|
502
|
|
- @ApiOperation("获取网关设备")
|
503
|
|
- public DeviceDTO findGateWayDeviceByTbDeviceId(
|
504
|
|
- @ApiParam(value = "tb设备Id") @PathVariable("tbDeviceId") String tbDeviceId)
|
505
|
|
- throws ThingsboardException {
|
506
|
|
- List<DeviceDTO> list =
|
507
|
|
- tkdeviceService.findGateWayDeviceByTbDeviceId(
|
508
|
|
- getCurrentUser().getTenantId().getId(), UUID.fromString(tbDeviceId));
|
509
|
|
- return null != list && list.size() > FastIotConstants.MagicNumber.ZERO
|
510
|
|
- ? list.get(FastIotConstants.MagicNumber.ZERO)
|
511
|
|
- : null;
|
512
|
|
- }
|
513
|
|
-
|
514
|
|
- @GetMapping("/get_subset/{tbDeviceId}")
|
515
|
|
- @ApiOperation("获取网关子设备信息")
|
516
|
|
- public DeviceDTO getSubsetDeviceByTbDeviceId(
|
517
|
|
- @ApiParam(value = "tb设备Id") @PathVariable("tbDeviceId") String tbDeviceId)
|
518
|
|
- throws ThingsboardException {
|
519
|
|
- return tkdeviceService.getSubsetDeviceByTbDeviceId(
|
520
|
|
- getCurrentUser().getCurrentTenantId(), tbDeviceId);
|
521
|
|
- }
|
522
|
|
-
|
523
|
|
- @GetMapping("/device/relation")
|
524
|
|
- public String getDeviceRelation(
|
525
|
|
- @ApiParam(value = "网关子设备:true 非网关子设备:false") @RequestParam(value = "isSlave") boolean isSlave,
|
526
|
|
- @ApiParam(value = "设备ID") @RequestParam(value = "deviceId") String deviceId) {
|
527
|
|
- return tkdeviceService.getDeviceRelation(isSlave, UUID.fromString(deviceId));
|
528
|
|
- }
|
529
|
|
-
|
530
|
|
-
|
531
|
|
-
|
532
|
|
- /**
|
533
|
|
- * 添加关联关系
|
534
|
|
- *
|
535
|
|
- * @param tenantId 租户ID
|
536
|
|
- * @param gateWayTbDeviceId 网关设备Tb的设备ID
|
537
|
|
- * @param deviceId 设备的tbDeviceId
|
538
|
|
- */
|
539
|
|
- private void addRelation(TenantId tenantId, DeviceId gateWayTbDeviceId, DeviceId deviceId)
|
540
|
|
- throws ThingsboardException {
|
541
|
|
- EntityRelation relation = new EntityRelation();
|
542
|
|
- relation.setFrom(gateWayTbDeviceId);
|
543
|
|
- relation.setTypeGroup(RelationTypeGroup.COMMON);
|
544
|
|
- relation.setTo(deviceId);
|
545
|
|
- relation.setType(FastIotConstants.Relation.relationType);
|
546
|
|
- checkCanCreateRelation(relation.getTo());
|
547
|
|
- if (relation.getTypeGroup() == null) {
|
548
|
|
- relation.setTypeGroup(RelationTypeGroup.COMMON);
|
|
568
|
+
|
|
569
|
+ @GetMapping({"/attributes/{deviceProfileId}"})
|
|
570
|
+ @ApiOperation("获取设备的属性")
|
|
571
|
+ public ResponseEntity<JsonNode> getDeviceAttributes(
|
|
572
|
+ @PathVariable("deviceProfileId") String deviceProfileId,
|
|
573
|
+ @RequestParam(value = "dataType", required = false) DataTypeEnum dataType)
|
|
574
|
+ throws ThingsboardException {
|
|
575
|
+ String tenantId = getCurrentUser().getCurrentTenantId();
|
|
576
|
+ DeviceProfileDTO dto = ytDeviceProfileService.findDeviceProfileById(tenantId, deviceProfileId);
|
|
577
|
+ if (null == dto) {
|
|
578
|
+ throw new TkDataValidationException(MessageUtils.message(ErrorMessage.INVALID_PARAMETER.getI18nCode()));
|
|
579
|
+ }
|
|
580
|
+ return ResponseEntity.ok(
|
|
581
|
+ tkdeviceService.getDeviceAttributes(deviceProfileId, tenantId, dataType));
|
549
|
582
|
}
|
550
|
583
|
|
551
|
|
- tbEntityRelationService.save(getTenantId(), getCurrentUser().getCustomerId(), relation, getCurrentUser());
|
552
|
|
- }
|
553
|
|
-
|
554
|
|
- @GetMapping({"used/{id}"})
|
555
|
|
- @ApiOperation("设备是否被占用")
|
556
|
|
- public ResponseResult<Boolean> otherUsing(@PathVariable("id") String deviceId)
|
557
|
|
- throws ThingsboardException {
|
558
|
|
- String str = tkdeviceService.otherUsing(deviceId, getCurrentUser().getCurrentTenantId());
|
559
|
|
- ResponseResult result = ResponseResult.success(StringUtils.isEmpty(str) ? true : false);
|
560
|
|
- result.setMessage(str);
|
561
|
|
- return result;
|
562
|
|
- }
|
563
|
|
-
|
564
|
|
- @GetMapping({"/attributes/{deviceProfileId}"})
|
565
|
|
- @ApiOperation("获取设备的属性")
|
566
|
|
- public ResponseEntity<JsonNode> getDeviceAttributes(
|
567
|
|
- @PathVariable("deviceProfileId") String deviceProfileId,
|
568
|
|
- @RequestParam(value = "dataType", required = false) DataTypeEnum dataType)
|
569
|
|
- throws ThingsboardException {
|
570
|
|
- String tenantId = getCurrentUser().getCurrentTenantId();
|
571
|
|
- DeviceProfileDTO dto = ytDeviceProfileService.findDeviceProfileById(tenantId, deviceProfileId);
|
572
|
|
- if (null == dto) {
|
573
|
|
- throw new TkDataValidationException(MessageUtils.message(ErrorMessage.INVALID_PARAMETER.getI18nCode()));
|
|
584
|
+ @PostMapping("get/devices")
|
|
585
|
+ @ApiOperation("通过设备ID列表获取设备信息")
|
|
586
|
+ public ResponseResult<List<DeviceDTO>> findDeviceInfoByTbDeviceIds(
|
|
587
|
+ @RequestBody List<String> tbDeviceIds) throws ThingsboardException {
|
|
588
|
+ return ResponseResult.success(
|
|
589
|
+ tkdeviceService.findDeviceInfoByTbDeviceIds(
|
|
590
|
+ tbDeviceIds, getCurrentUser().getCurrentTenantId()));
|
574
|
591
|
}
|
575
|
|
- return ResponseEntity.ok(
|
576
|
|
- tkdeviceService.getDeviceAttributes(deviceProfileId, tenantId, dataType));
|
577
|
|
- }
|
578
|
|
-
|
579
|
|
- @PostMapping("get/devices")
|
580
|
|
- @ApiOperation("通过设备ID列表获取设备信息")
|
581
|
|
- public ResponseResult<List<DeviceDTO>> findDeviceInfoByTbDeviceIds(
|
582
|
|
- @RequestBody List<String> tbDeviceIds) throws ThingsboardException {
|
583
|
|
- return ResponseResult.success(
|
584
|
|
- tkdeviceService.findDeviceInfoByTbDeviceIds(
|
585
|
|
- tbDeviceIds, getCurrentUser().getCurrentTenantId()));
|
586
|
|
- }
|
587
|
|
-
|
588
|
|
- private void checkCanCreateRelation(EntityId entityId) throws ThingsboardException {
|
589
|
|
- SecurityUser currentUser = getCurrentUser();
|
590
|
|
- var isTenantAdminAndRelateToSelf = currentUser.isTenantAdmin() && currentUser.getTenantId().equals(entityId);
|
591
|
|
- if (!isTenantAdminAndRelateToSelf) {
|
592
|
|
- checkEntityId(entityId, Operation.WRITE);
|
|
592
|
+
|
|
593
|
+ private void checkCanCreateRelation(EntityId entityId) throws ThingsboardException {
|
|
594
|
+ SecurityUser currentUser = getCurrentUser();
|
|
595
|
+ var isTenantAdminAndRelateToSelf = currentUser.isTenantAdmin() && currentUser.getTenantId().equals(entityId);
|
|
596
|
+ if (!isTenantAdminAndRelateToSelf) {
|
|
597
|
+ checkEntityId(entityId, Operation.WRITE);
|
|
598
|
+ }
|
593
|
599
|
}
|
594
|
|
- }
|
595
|
600
|
|
596
|
601
|
} |
...
|
...
|
|