Commit f1665c4ad009f63c8844343c3af608dd394dd3da
1 parent
36e53d4c
feat: add get subset device info by tbDeviceId
Showing
3 changed files
with
883 additions
and
811 deletions
@@ -55,415 +55,443 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant. | @@ -55,415 +55,443 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant. | ||
55 | @Api(tags = {"设备管理"}) | 55 | @Api(tags = {"设备管理"}) |
56 | @Slf4j | 56 | @Slf4j |
57 | public class YtDeviceController extends BaseController { | 57 | public class YtDeviceController extends BaseController { |
58 | - private final YtDeviceService deviceService; | ||
59 | - private final DeviceService tbDeviceService; | ||
60 | - String sePL = new Date().toString(); | ||
61 | - | ||
62 | - @PostMapping | ||
63 | - @ApiOperation("创建|编辑") | ||
64 | - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:post','api:yt:device:update'})") | ||
65 | - public ResponseEntity<DeviceDTO> saveDevice( | ||
66 | - @Validated(AddGroup.class) @RequestBody DeviceDTO deviceDTO) throws ThingsboardException, ExecutionException, InterruptedException { | ||
67 | - String currentTenantId = getCurrentUser().getCurrentTenantId(); | ||
68 | - boolean enable = deviceService.validateFormdata(currentTenantId, deviceDTO); | ||
69 | - if (!enable) { | ||
70 | - ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); | ||
71 | - } | ||
72 | - DeviceDTO newDeviceDTO = null; | ||
73 | - boolean isIncludeRelation = false; | ||
74 | - | ||
75 | - String gatewayId = deviceDTO.getGatewayId(); | ||
76 | - DeviceDTO gateWayDevice = null; | ||
77 | - if (StringUtils.isNotEmpty(gatewayId)) { | ||
78 | - gateWayDevice = | ||
79 | - deviceService.checkDeviceByTenantIdAndDeviceId( | ||
80 | - getCurrentUser().getCurrentTenantId(), gatewayId); | ||
81 | - | ||
82 | - // 第一步判断该网关设备是否存在于该租户下面 | ||
83 | - if (null == gateWayDevice) { | ||
84 | - throw new YtDataValidationException( | ||
85 | - ErrorMessage.DEVICE_NOT_EXISTENCE_IN_TENANT.getMessage()); | ||
86 | - } | ||
87 | - } | ||
88 | - | ||
89 | - /**子设备编辑时,TB中已经存在关联关系则值更新设备表信息*/ | ||
90 | - String selfTbDeviceIdStr = deviceDTO.getTbDeviceId(); | ||
91 | - if (selfTbDeviceIdStr != null | ||
92 | - && deviceDTO.getDeviceType().equals(DeviceTypeEnum.SENSOR) | ||
93 | - && StringUtils.isNotEmpty(gatewayId)) { | ||
94 | - | ||
95 | - | ||
96 | - // 第二步判断网关子设备是否已关联到网关设备 | ||
97 | - EntityId entityId = EntityIdFactory.getByTypeAndId("DEVICE", selfTbDeviceIdStr);//gateWayDevice.getTbDeviceId() | ||
98 | - List<EntityRelationInfo> list = | ||
99 | - relationService.findInfoByTo(getTenantId(), entityId, RelationTypeGroup.COMMON).get(); | ||
100 | - | ||
101 | - for (EntityRelationInfo entityRelationInfo : list) { | ||
102 | - if (entityRelationInfo.getTo().getId().equals(selfTbDeviceIdStr) | ||
103 | - && entityRelationInfo.getFrom().getId().equals(gatewayId)) { | ||
104 | - deviceDTO.setTbDeviceId(entityRelationInfo.getTo().toString()); | ||
105 | - newDeviceDTO = | ||
106 | - deviceService.insertOrUpdate(getCurrentUser().getCurrentTenantId(), deviceDTO); | ||
107 | - isIncludeRelation = true; | ||
108 | - break; | ||
109 | - } | ||
110 | - } | ||
111 | - } | ||
112 | - | ||
113 | - | ||
114 | - /**需要更新设备表和关联关系表*/ | ||
115 | - if (!isIncludeRelation) { | ||
116 | - Device tbDevice = buildTbDeviceFromDeviceDTO(getCurrentUser().getTenantId(), deviceDTO); | ||
117 | - | ||
118 | - DeviceId selfTbId = updateTbDevice(tbDevice, deviceDTO.getDeviceToken()); | ||
119 | - selfTbDeviceIdStr = selfTbId.getId().toString(); | ||
120 | - deviceDTO.setTbDeviceId(selfTbDeviceIdStr); | ||
121 | - newDeviceDTO = deviceService.insertOrUpdate(getCurrentUser().getCurrentTenantId(), deviceDTO); | ||
122 | - | ||
123 | - if (deviceDTO.getDeviceType().equals(DeviceTypeEnum.SENSOR) | ||
124 | - && StringUtils.isNotEmpty(selfTbDeviceIdStr)) { | ||
125 | - // 删除原來的关联关系 | ||
126 | - List<DeviceDTO> list = | ||
127 | - deviceService.findGateWayDeviceByTbDeviceId( | ||
128 | - getCurrentUser().getCurrentTenantId(), selfTbDeviceIdStr); | ||
129 | - if (null != list && list.size() > 0) { | ||
130 | - DeviceId form = new DeviceId(UUID.fromString(list.get(0).getTbDeviceId())); | ||
131 | - EntityRelation relation = | ||
132 | - new EntityRelation( | ||
133 | - form, selfTbId, FastIotConstants.Relation.relationType, RelationTypeGroup.COMMON); | ||
134 | - boolean found = | ||
135 | - relationService.deleteRelation( | ||
136 | - getTenantId(), | ||
137 | - form, | ||
138 | - selfTbId, | ||
139 | - FastIotConstants.Relation.relationType, | ||
140 | - RelationTypeGroup.COMMON); | ||
141 | - | ||
142 | - if (!found) { | ||
143 | - throw new ThingsboardException( | ||
144 | - "Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); | ||
145 | - } | ||
146 | - sendRelationNotificationMsg( | ||
147 | - getTenantId(), relation, EdgeEventActionType.RELATION_DELETED); | ||
148 | - } | ||
149 | - | ||
150 | - if (gateWayDevice != null) { | ||
151 | - addRelation(getTenantId(), gateWayDevice.getTbDeviceId(), selfTbDeviceIdStr); | ||
152 | - } | ||
153 | - } | ||
154 | - } | ||
155 | - return ResponseEntity.ok(newDeviceDTO); | 58 | + private final YtDeviceService deviceService; |
59 | + private final DeviceService tbDeviceService; | ||
60 | + private final ObjectMapper objectMapper; | ||
61 | + String sePL = new Date().toString(); | ||
62 | + | ||
63 | + @PostMapping | ||
64 | + @ApiOperation("创建|编辑") | ||
65 | + @PreAuthorize( | ||
66 | + "@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:post','api:yt:device:update'})") | ||
67 | + public ResponseEntity<DeviceDTO> saveDevice( | ||
68 | + @Validated(AddGroup.class) @RequestBody DeviceDTO deviceDTO) | ||
69 | + throws ThingsboardException, ExecutionException, InterruptedException { | ||
70 | + String currentTenantId = getCurrentUser().getCurrentTenantId(); | ||
71 | + boolean enable = deviceService.validateFormdata(currentTenantId, deviceDTO); | ||
72 | + if (!enable) { | ||
73 | + ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); | ||
156 | } | 74 | } |
157 | - | ||
158 | - @GetMapping({"sn"}) | ||
159 | - @ApiOperation("自动生成设备SN") | ||
160 | - public ResponseEntity<String> generate() { | ||
161 | - String result = deviceService.generateSn(); | ||
162 | - return ResponseEntity.ok(result); | 75 | + DeviceDTO newDeviceDTO = null; |
76 | + boolean isIncludeRelation = false; | ||
77 | + | ||
78 | + String gatewayId = deviceDTO.getGatewayId(); | ||
79 | + DeviceDTO gateWayDevice = null; | ||
80 | + if (StringUtils.isNotEmpty(gatewayId)) { | ||
81 | + gateWayDevice = | ||
82 | + deviceService.checkDeviceByTenantIdAndDeviceId( | ||
83 | + getCurrentUser().getCurrentTenantId(), gatewayId); | ||
84 | + | ||
85 | + // 第一步判断该网关设备是否存在于该租户下面 | ||
86 | + if (null == gateWayDevice) { | ||
87 | + throw new YtDataValidationException( | ||
88 | + ErrorMessage.DEVICE_NOT_EXISTENCE_IN_TENANT.getMessage()); | ||
89 | + } | ||
163 | } | 90 | } |
164 | 91 | ||
165 | - private DeviceId updateTbDevice(Device tbDevice, YtCredentialsDto formCredentials) | ||
166 | - throws ThingsboardException { | ||
167 | - Device oldDevice = null; | ||
168 | - boolean created = tbDevice.getId() == null; | ||
169 | - if (!created) { | ||
170 | - oldDevice = checkDeviceId(tbDevice.getId(), Operation.WRITE); | ||
171 | - } else { | ||
172 | - checkEntity(null, tbDevice, Resource.DEVICE); | ||
173 | - } | ||
174 | - | ||
175 | - Device savedDevice = checkNotNull(tbDeviceService.saveDeviceWithAccessToken(tbDevice, null)); | ||
176 | - tbClusterService.onDeviceUpdated(savedDevice, oldDevice); | ||
177 | - DeviceId tbDeviceId = savedDevice.getId(); | ||
178 | - try { | ||
179 | - logEntityAction( | ||
180 | - getCurrentUser(), | ||
181 | - savedDevice.getId(), | ||
182 | - savedDevice, | ||
183 | - savedDevice.getCustomerId(), | ||
184 | - created ? ActionType.ADDED : ActionType.UPDATED, | ||
185 | - null); | ||
186 | - | ||
187 | - if (formCredentials != null && formCredentials.getCredentialsType() != null) { | ||
188 | - DeviceCredentials deviceCredentials = | ||
189 | - checkNotNull( | ||
190 | - deviceCredentialsService.findDeviceCredentialsByDeviceId( | ||
191 | - getCurrentUser().getTenantId(), tbDeviceId)); | ||
192 | - deviceCredentials.setCredentialsId(formCredentials.getCredentialsId()); | ||
193 | - deviceCredentials.setCredentialsType(formCredentials.getCredentialsType()); | ||
194 | - deviceCredentials.setCredentialsValue(formCredentials.getCredentialsValue()); | ||
195 | - | ||
196 | - DeviceCredentials result = | ||
197 | - checkNotNull( | ||
198 | - deviceCredentialsService.updateDeviceCredentials( | ||
199 | - getCurrentUser().getTenantId(), deviceCredentials)); | ||
200 | - tbClusterService.pushMsgToCore( | ||
201 | - new DeviceCredentialsUpdateNotificationMsg( | ||
202 | - getCurrentUser().getTenantId(), deviceCredentials.getDeviceId(), result), | ||
203 | - null); | ||
204 | - | ||
205 | - sendEntityNotificationMsg( | ||
206 | - getTenantId(), tbDeviceId, EdgeEventActionType.CREDENTIALS_UPDATED); | ||
207 | - | ||
208 | - logEntityAction( | ||
209 | - tbDeviceId, | ||
210 | - savedDevice, | ||
211 | - savedDevice.getCustomerId(), | ||
212 | - ActionType.CREDENTIALS_UPDATED, | ||
213 | - null, | ||
214 | - deviceCredentials); | ||
215 | - } | ||
216 | - } catch (ThingsboardException e) { | ||
217 | - log.error("Failed to log entity action", e); | 92 | + /** 子设备编辑时,TB中已经存在关联关系则值更新设备表信息 */ |
93 | + String selfTbDeviceIdStr = deviceDTO.getTbDeviceId(); | ||
94 | + if (selfTbDeviceIdStr != null | ||
95 | + && deviceDTO.getDeviceType().equals(DeviceTypeEnum.SENSOR) | ||
96 | + && StringUtils.isNotEmpty(gatewayId)) { | ||
97 | + | ||
98 | + // 第二步判断网关子设备是否已关联到网关设备 | ||
99 | + EntityId entityId = | ||
100 | + EntityIdFactory.getByTypeAndId( | ||
101 | + "DEVICE", selfTbDeviceIdStr); // gateWayDevice.getTbDeviceId() | ||
102 | + List<EntityRelationInfo> list = | ||
103 | + relationService.findInfoByTo(getTenantId(), entityId, RelationTypeGroup.COMMON).get(); | ||
104 | + | ||
105 | + for (EntityRelationInfo entityRelationInfo : list) { | ||
106 | + if (entityRelationInfo.getTo().getId().equals(selfTbDeviceIdStr) | ||
107 | + && entityRelationInfo.getFrom().getId().equals(gatewayId)) { | ||
108 | + deviceDTO.setTbDeviceId(entityRelationInfo.getTo().toString()); | ||
109 | + newDeviceDTO = | ||
110 | + deviceService.insertOrUpdate(getCurrentUser().getCurrentTenantId(), deviceDTO); | ||
111 | + isIncludeRelation = true; | ||
112 | + break; | ||
218 | } | 113 | } |
219 | - return tbDeviceId; | 114 | + } |
220 | } | 115 | } |
221 | 116 | ||
222 | - @GetMapping("{id}") | ||
223 | - @ApiOperation("详情") | ||
224 | - @PreAuthorize("@check.checkPermissions({},{'api:yt:device:get'})") | ||
225 | - public ResponseEntity<DeviceDTO> getDevice(@PathVariable("id") String id) | ||
226 | - throws ThingsboardException { | ||
227 | - return ResponseEntity.of(deviceService.getDevice(getCurrentUser().getCurrentTenantId(), id)); | ||
228 | - } | 117 | + /** 需要更新设备表和关联关系表 */ |
118 | + if (!isIncludeRelation) { | ||
119 | + Device tbDevice = buildTbDeviceFromDeviceDTO(getCurrentUser().getTenantId(), deviceDTO); | ||
229 | 120 | ||
230 | - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})") | ||
231 | - @GetMapping(params = {PAGE_SIZE, PAGE}) | ||
232 | - @ApiOperation("查询") | ||
233 | - public YtPageData<DeviceDTO> pageDevice( | ||
234 | - @RequestParam(PAGE_SIZE) int pageSize, | ||
235 | - @RequestParam(PAGE) int page, | ||
236 | - @RequestParam(value = "name", required = false) String name, | ||
237 | - @RequestParam(value = "deviceState", required = false) DeviceState deviceState, | ||
238 | - @RequestParam(value = "deviceType", required = false) DeviceTypeEnum deviceType, | ||
239 | - @RequestParam(value = "organizationId", required = false) String organizationId, | ||
240 | - @RequestParam(value = "alarmStatus", required = false) Integer alarmStatus, | ||
241 | - @RequestParam(value = "profileId", required = false) String profileId, | ||
242 | - @RequestParam(value = ORDER_FILED, required = false) String orderBy, | ||
243 | - @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType) | ||
244 | - throws ThingsboardException { | ||
245 | - HashMap<String, Object> queryMap = new HashMap<>(); | ||
246 | - queryMap.put(PAGE_SIZE, pageSize); | ||
247 | - queryMap.put(PAGE, page); | ||
248 | - queryMap.put(ORDER_FILED, orderBy); | ||
249 | - queryMap.put("name", name); | ||
250 | - queryMap.put("alarmStatus", alarmStatus); | ||
251 | - queryMap.put("profileId", profileId); | ||
252 | - if (deviceState != null) { | ||
253 | - if (deviceState != DeviceState.INACTIVE) { | ||
254 | - queryMap.put("deviceState", deviceState == DeviceState.ONLINE); | ||
255 | - } else { | ||
256 | - queryMap.put("activeState", DeviceState.INACTIVE); | ||
257 | - } | ||
258 | - } | ||
259 | - if (deviceType != null) { | ||
260 | - queryMap.put("deviceType", deviceType.name()); | ||
261 | - } | ||
262 | - if (!StringUtils.isEmpty(organizationId)) { | ||
263 | - queryMap.put("organizationId", organizationId); | ||
264 | - } | ||
265 | - if (orderType != null) { | ||
266 | - queryMap.put(ORDER_TYPE, orderType.name()); | ||
267 | - } | ||
268 | - // 如果当前用户是客户 | ||
269 | - if (getCurrentUser().isCustomerUser()) { | ||
270 | - queryMap.put("customerId", getCurrentUser().getCustomerId().toString()); | ||
271 | - } | ||
272 | - return deviceService.page(getCurrentUser().getCurrentTenantId(), queryMap); | ||
273 | - } | 121 | + DeviceId selfTbId = updateTbDevice(tbDevice, deviceDTO.getDeviceToken()); |
122 | + selfTbDeviceIdStr = selfTbId.getId().toString(); | ||
123 | + deviceDTO.setTbDeviceId(selfTbDeviceIdStr); | ||
124 | + newDeviceDTO = deviceService.insertOrUpdate(getCurrentUser().getCurrentTenantId(), deviceDTO); | ||
274 | 125 | ||
275 | - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})") | ||
276 | - @GetMapping(path = {"/relation"}, params = {PAGE_SIZE, PAGE}) | ||
277 | - @ApiOperation("子设备查询") | ||
278 | - public YtPageData<RelationDeviceDTO> pageRelationDevice( | ||
279 | - @RequestParam(PAGE_SIZE) int pageSize, | ||
280 | - @RequestParam(PAGE) int page, | ||
281 | - @RequestParam(value = "name", required = false) String name, | ||
282 | - @RequestParam(value = "deviceState", required = false) DeviceState deviceState, | ||
283 | - @RequestParam(value = "fromId") String fromId, | ||
284 | - @RequestParam(value = ORDER_FILED, required = false) String orderBy, | ||
285 | - @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType) throws ThingsboardException { | ||
286 | - HashMap<String, Object> queryMap = new HashMap<>(); | ||
287 | - queryMap.put(PAGE_SIZE, pageSize); | ||
288 | - queryMap.put(PAGE, page); | ||
289 | - queryMap.put(ORDER_FILED, orderBy); | ||
290 | - queryMap.put("name", name); | ||
291 | - queryMap.put("fromId", fromId); | ||
292 | - queryMap.put("tenantId", getCurrentUser().getCurrentTenantId()); | ||
293 | - if (deviceState != null) { | ||
294 | - if (deviceState != DeviceState.INACTIVE) { | ||
295 | - queryMap.put("deviceState", deviceState == DeviceState.ONLINE); | ||
296 | - } else { | ||
297 | - queryMap.put("activeState", DeviceState.INACTIVE); | ||
298 | - } | ||
299 | - } | ||
300 | - if (orderType != null) { | ||
301 | - queryMap.put(ORDER_TYPE, orderType.name()); | 126 | + if (deviceDTO.getDeviceType().equals(DeviceTypeEnum.SENSOR) |
127 | + && StringUtils.isNotEmpty(selfTbDeviceIdStr)) { | ||
128 | + // 删除原來的关联关系 | ||
129 | + List<DeviceDTO> list = | ||
130 | + deviceService.findGateWayDeviceByTbDeviceId( | ||
131 | + getCurrentUser().getCurrentTenantId(), selfTbDeviceIdStr); | ||
132 | + if (null != list && list.size() > 0) { | ||
133 | + DeviceId form = new DeviceId(UUID.fromString(list.get(0).getTbDeviceId())); | ||
134 | + EntityRelation relation = | ||
135 | + new EntityRelation( | ||
136 | + form, selfTbId, FastIotConstants.Relation.relationType, RelationTypeGroup.COMMON); | ||
137 | + boolean found = | ||
138 | + relationService.deleteRelation( | ||
139 | + getTenantId(), | ||
140 | + form, | ||
141 | + selfTbId, | ||
142 | + FastIotConstants.Relation.relationType, | ||
143 | + RelationTypeGroup.COMMON); | ||
144 | + | ||
145 | + if (!found) { | ||
146 | + throw new ThingsboardException( | ||
147 | + "Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); | ||
148 | + } | ||
149 | + sendRelationNotificationMsg( | ||
150 | + getTenantId(), relation, EdgeEventActionType.RELATION_DELETED); | ||
302 | } | 151 | } |
303 | - // 如果当前用户是客户 | ||
304 | - if (getCurrentUser().isCustomerUser()) { | ||
305 | - queryMap.put("customerId", getCurrentUser().getCustomerId().toString()); | 152 | + |
153 | + if (gateWayDevice != null) { | ||
154 | + addRelation(getTenantId(), gateWayDevice.getTbDeviceId(), selfTbDeviceIdStr); | ||
306 | } | 155 | } |
307 | - return deviceService.pageRelation(queryMap); | 156 | + } |
308 | } | 157 | } |
309 | - | ||
310 | - @PostMapping("/import") | ||
311 | - @ApiOperation("导入配置") | ||
312 | - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:import'})") | ||
313 | - public ResponseEntity<String> importDeviceProfile() { | ||
314 | - // TODO 实现的业务功能 | ||
315 | - return ResponseEntity.ok(""); | 158 | + return ResponseEntity.ok(newDeviceDTO); |
159 | + } | ||
160 | + | ||
161 | + @GetMapping({"sn"}) | ||
162 | + @ApiOperation("自动生成设备SN") | ||
163 | + public ResponseEntity<String> generate() { | ||
164 | + String result = deviceService.generateSn(); | ||
165 | + return ResponseEntity.ok(result); | ||
166 | + } | ||
167 | + | ||
168 | + private DeviceId updateTbDevice(Device tbDevice, YtCredentialsDto formCredentials) | ||
169 | + throws ThingsboardException { | ||
170 | + Device oldDevice = null; | ||
171 | + boolean created = tbDevice.getId() == null; | ||
172 | + if (!created) { | ||
173 | + oldDevice = checkDeviceId(tbDevice.getId(), Operation.WRITE); | ||
174 | + } else { | ||
175 | + checkEntity(null, tbDevice, Resource.DEVICE); | ||
316 | } | 176 | } |
317 | 177 | ||
318 | - @PostMapping("/export") | ||
319 | - @ApiOperation("导出") | ||
320 | - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:export'})") | ||
321 | - public ResponseEntity<String> exportDeviceProfile() { | ||
322 | - // TODO 实现的业务功能 | ||
323 | - return ResponseEntity.ok(""); | 178 | + Device savedDevice = checkNotNull(tbDeviceService.saveDeviceWithAccessToken(tbDevice, null)); |
179 | + tbClusterService.onDeviceUpdated(savedDevice, oldDevice); | ||
180 | + DeviceId tbDeviceId = savedDevice.getId(); | ||
181 | + try { | ||
182 | + logEntityAction( | ||
183 | + getCurrentUser(), | ||
184 | + savedDevice.getId(), | ||
185 | + savedDevice, | ||
186 | + savedDevice.getCustomerId(), | ||
187 | + created ? ActionType.ADDED : ActionType.UPDATED, | ||
188 | + null); | ||
189 | + | ||
190 | + if (formCredentials != null && formCredentials.getCredentialsType() != null) { | ||
191 | + DeviceCredentials deviceCredentials = | ||
192 | + checkNotNull( | ||
193 | + deviceCredentialsService.findDeviceCredentialsByDeviceId( | ||
194 | + getCurrentUser().getTenantId(), tbDeviceId)); | ||
195 | + deviceCredentials.setCredentialsId(formCredentials.getCredentialsId()); | ||
196 | + deviceCredentials.setCredentialsType(formCredentials.getCredentialsType()); | ||
197 | + deviceCredentials.setCredentialsValue(formCredentials.getCredentialsValue()); | ||
198 | + | ||
199 | + DeviceCredentials result = | ||
200 | + checkNotNull( | ||
201 | + deviceCredentialsService.updateDeviceCredentials( | ||
202 | + getCurrentUser().getTenantId(), deviceCredentials)); | ||
203 | + tbClusterService.pushMsgToCore( | ||
204 | + new DeviceCredentialsUpdateNotificationMsg( | ||
205 | + getCurrentUser().getTenantId(), deviceCredentials.getDeviceId(), result), | ||
206 | + null); | ||
207 | + | ||
208 | + sendEntityNotificationMsg( | ||
209 | + getTenantId(), tbDeviceId, EdgeEventActionType.CREDENTIALS_UPDATED); | ||
210 | + | ||
211 | + logEntityAction( | ||
212 | + tbDeviceId, | ||
213 | + savedDevice, | ||
214 | + savedDevice.getCustomerId(), | ||
215 | + ActionType.CREDENTIALS_UPDATED, | ||
216 | + null, | ||
217 | + deviceCredentials); | ||
218 | + } | ||
219 | + } catch (ThingsboardException e) { | ||
220 | + log.error("Failed to log entity action", e); | ||
324 | } | 221 | } |
325 | - | ||
326 | - @DeleteMapping | ||
327 | - @ApiOperation("删除") | ||
328 | - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:delete'})") | ||
329 | - public void deleteDevices(@Validated({DeleteGroup.class}) @RequestBody DeleteDTO deleteDTO) | ||
330 | - throws ThingsboardException { | ||
331 | - String currentTenantId = getCurrentUser().getCurrentTenantId(); | ||
332 | - List<String> tdIds = deviceService.findTbDeviceId(currentTenantId, deleteDTO.getIds()); | ||
333 | - for (String id : tdIds) { | ||
334 | - deleteTbDevice(id); | ||
335 | - } | ||
336 | - deviceService.deleteDevices(currentTenantId, deleteDTO.getIds()); | 222 | + return tbDeviceId; |
223 | + } | ||
224 | + | ||
225 | + @GetMapping("{id}") | ||
226 | + @ApiOperation("详情") | ||
227 | + @PreAuthorize("@check.checkPermissions({},{'api:yt:device:get'})") | ||
228 | + public ResponseEntity<DeviceDTO> getDevice(@PathVariable("id") String id) | ||
229 | + throws ThingsboardException { | ||
230 | + return ResponseEntity.of(deviceService.getDevice(getCurrentUser().getCurrentTenantId(), id)); | ||
231 | + } | ||
232 | + | ||
233 | + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})") | ||
234 | + @GetMapping(params = {PAGE_SIZE, PAGE}) | ||
235 | + @ApiOperation("查询") | ||
236 | + public YtPageData<DeviceDTO> pageDevice( | ||
237 | + @RequestParam(PAGE_SIZE) int pageSize, | ||
238 | + @RequestParam(PAGE) int page, | ||
239 | + @RequestParam(value = "name", required = false) String name, | ||
240 | + @RequestParam(value = "deviceState", required = false) DeviceState deviceState, | ||
241 | + @RequestParam(value = "deviceType", required = false) DeviceTypeEnum deviceType, | ||
242 | + @RequestParam(value = "organizationId", required = false) String organizationId, | ||
243 | + @RequestParam(value = "alarmStatus", required = false) Integer alarmStatus, | ||
244 | + @RequestParam(value = "profileId", required = false) String profileId, | ||
245 | + @RequestParam(value = ORDER_FILED, required = false) String orderBy, | ||
246 | + @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType) | ||
247 | + throws ThingsboardException { | ||
248 | + HashMap<String, Object> queryMap = new HashMap<>(); | ||
249 | + queryMap.put(PAGE_SIZE, pageSize); | ||
250 | + queryMap.put(PAGE, page); | ||
251 | + queryMap.put(ORDER_FILED, orderBy); | ||
252 | + queryMap.put("name", name); | ||
253 | + queryMap.put("alarmStatus", alarmStatus); | ||
254 | + queryMap.put("profileId", profileId); | ||
255 | + if (deviceState != null) { | ||
256 | + if (deviceState != DeviceState.INACTIVE) { | ||
257 | + queryMap.put("deviceState", deviceState == DeviceState.ONLINE); | ||
258 | + } else { | ||
259 | + queryMap.put("activeState", DeviceState.INACTIVE); | ||
260 | + } | ||
337 | } | 261 | } |
338 | - | ||
339 | - private void deleteTbDevice(String id) throws ThingsboardException { | ||
340 | - DeviceId deviceId = new DeviceId(toUUID(id)); | ||
341 | - Device device = checkDeviceId(deviceId, Operation.DELETE); | ||
342 | - | ||
343 | - List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), deviceId); | ||
344 | - | ||
345 | - tbDeviceService.deleteDevice(getCurrentUser().getTenantId(), deviceId); | ||
346 | - | ||
347 | - tbClusterService.onDeviceDeleted(device, null); | ||
348 | - | ||
349 | - logEntityAction(deviceId, device, device.getCustomerId(), ActionType.DELETED, null, id); | ||
350 | - | ||
351 | - sendDeleteNotificationMsg(getTenantId(), deviceId, relatedEdgeIds); | 262 | + if (deviceType != null) { |
263 | + queryMap.put("deviceType", deviceType.name()); | ||
352 | } | 264 | } |
353 | - | ||
354 | - @GetMapping("/list/{deviceType}") | ||
355 | - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{})") | ||
356 | - @ApiOperation("获取该组织的所有设备") | ||
357 | - public List<DeviceDTO> getGatewayDevices( | ||
358 | - @ApiParam(value = "组织ID") @RequestParam("organizationId") String organizationId, | ||
359 | - @ApiParam(value = "设备类型") @PathVariable("deviceType") DeviceTypeEnum deviceType) throws ThingsboardException { | ||
360 | - return deviceService.findDevicesByDeviceTypeAndOrganizationId(deviceType,getCurrentUser().getCurrentTenantId(), organizationId); | 265 | + if (!StringUtils.isEmpty(organizationId)) { |
266 | + queryMap.put("organizationId", organizationId); | ||
361 | } | 267 | } |
362 | - | ||
363 | - @GetMapping("/list/master/{organizationId}") | ||
364 | - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})") | ||
365 | - @ApiOperation("主设备列表") | ||
366 | - public List<SelectItemDTO> getMasterDevices( | ||
367 | - @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId) throws ThingsboardException { | ||
368 | - return deviceService.findMasterDevices(getCurrentUser().getCurrentTenantId() | ||
369 | - ,getCurrentUser().isCustomerUser()?getCurrentUser().getCustomerId().toString():null | ||
370 | - , organizationId); | 268 | + if (orderType != null) { |
269 | + queryMap.put(ORDER_TYPE, orderType.name()); | ||
371 | } | 270 | } |
372 | - @GetMapping("/list/slave/{organizationId}") | ||
373 | - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})") | ||
374 | - @ApiOperation("从设备列表") | ||
375 | - public List<SelectItemDTO> getSlaveDevices( | ||
376 | - @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId, | ||
377 | - @ApiParam(value = "主设备ID") @RequestParam(value = "masterId",required = false) String masterId) throws ThingsboardException { | ||
378 | - return deviceService.findSlaveDevices(masterId,getCurrentUser().getCurrentTenantId() | ||
379 | - ,getCurrentUser().isCustomerUser()?getCurrentUser().getCurrentUserId():null | ||
380 | - , organizationId); | 271 | + // 如果当前用户是客户 |
272 | + if (getCurrentUser().isCustomerUser()) { | ||
273 | + queryMap.put("customerId", getCurrentUser().getCustomerId().toString()); | ||
381 | } | 274 | } |
382 | - | ||
383 | - @GetMapping("/keys/{organizationId}") | ||
384 | - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})") | ||
385 | - @ApiOperation("设备遥测指标名称") | ||
386 | - public List<String> listKeys( | ||
387 | - @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId, | ||
388 | - @ApiParam(value = "设备ID") @RequestParam(value = "deviceIds",required = false) List<String> deviceIds) throws ThingsboardException { | ||
389 | - return deviceService.findDeviceKeys(getCurrentUser().getCurrentTenantId() | ||
390 | - ,getCurrentUser().isCustomerUser()?getCurrentUser().getCustomerId().toString():null | ||
391 | - , organizationId,deviceIds); | 275 | + return deviceService.page(getCurrentUser().getCurrentTenantId(), queryMap); |
276 | + } | ||
277 | + | ||
278 | + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})") | ||
279 | + @GetMapping( | ||
280 | + path = {"/relation"}, | ||
281 | + params = {PAGE_SIZE, PAGE}) | ||
282 | + @ApiOperation("子设备查询") | ||
283 | + public YtPageData<RelationDeviceDTO> pageRelationDevice( | ||
284 | + @RequestParam(PAGE_SIZE) int pageSize, | ||
285 | + @RequestParam(PAGE) int page, | ||
286 | + @RequestParam(value = "name", required = false) String name, | ||
287 | + @RequestParam(value = "deviceState", required = false) DeviceState deviceState, | ||
288 | + @RequestParam(value = "fromId") String fromId, | ||
289 | + @RequestParam(value = ORDER_FILED, required = false) String orderBy, | ||
290 | + @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType) | ||
291 | + throws ThingsboardException { | ||
292 | + HashMap<String, Object> queryMap = new HashMap<>(); | ||
293 | + queryMap.put(PAGE_SIZE, pageSize); | ||
294 | + queryMap.put(PAGE, page); | ||
295 | + queryMap.put(ORDER_FILED, orderBy); | ||
296 | + queryMap.put("name", name); | ||
297 | + queryMap.put("fromId", fromId); | ||
298 | + queryMap.put("tenantId", getCurrentUser().getCurrentTenantId()); | ||
299 | + if (deviceState != null) { | ||
300 | + if (deviceState != DeviceState.INACTIVE) { | ||
301 | + queryMap.put("deviceState", deviceState == DeviceState.ONLINE); | ||
302 | + } else { | ||
303 | + queryMap.put("activeState", DeviceState.INACTIVE); | ||
304 | + } | ||
392 | } | 305 | } |
393 | - | ||
394 | - @GetMapping("/gateway/{tbDeviceId}") | ||
395 | - @ApiOperation("获取网关设备") | ||
396 | - public DeviceDTO findGateWayDeviceByTbDeviceId( | ||
397 | - @ApiParam(value = "tb设备Id") @PathVariable("tbDeviceId") String tbDeviceId) | ||
398 | - throws ThingsboardException { | ||
399 | - List<DeviceDTO> list = | ||
400 | - deviceService.findGateWayDeviceByTbDeviceId( | ||
401 | - getCurrentUser().getCurrentTenantId(), tbDeviceId); | ||
402 | - return null != list && list.size() > FastIotConstants.MagicNumber.ZERO | ||
403 | - ? list.get(FastIotConstants.MagicNumber.ZERO) | ||
404 | - : null; | 306 | + if (orderType != null) { |
307 | + queryMap.put(ORDER_TYPE, orderType.name()); | ||
405 | } | 308 | } |
406 | - | ||
407 | - private final ObjectMapper objectMapper; | ||
408 | - | ||
409 | - private Device buildTbDeviceFromDeviceDTO(TenantId tenantId, DeviceDTO deviceDTO) { | ||
410 | - Device tbDevice = new Device(); | ||
411 | - String deviceId = deviceDTO.getTbDeviceId(); | ||
412 | - if (StringUtils.isNotBlank(deviceId)) { | ||
413 | - DeviceId id = new DeviceId(UUID.fromString(deviceId)); | ||
414 | - tbDevice.setId(id); | ||
415 | - } | ||
416 | - ObjectNode additionalInfo = objectMapper.createObjectNode(); | ||
417 | - additionalInfo.put( | ||
418 | - "gateway", | ||
419 | - Optional.ofNullable(deviceDTO.getDeviceType()) | ||
420 | - .map(deviceType -> deviceType.equals(DeviceTypeEnum.GATEWAY)) | ||
421 | - .orElse(false)); | ||
422 | - additionalInfo.put( | ||
423 | - "description", | ||
424 | - Optional.ofNullable(deviceDTO.getDeviceInfo()) | ||
425 | - .map(deviceInfo -> deviceInfo.get("description")) | ||
426 | - .map(JsonNode::asText) | ||
427 | - .orElse("")); | ||
428 | - additionalInfo.put("overwriteActivityTime", false); | ||
429 | - | ||
430 | - DeviceProfileId deviceProfileId = | ||
431 | - new DeviceProfileId(UUID.fromString(deviceDTO.getProfileId())); | ||
432 | - | ||
433 | - tbDevice.setAdditionalInfo(additionalInfo); | ||
434 | - tbDevice.setCustomerId(null); | ||
435 | - tbDevice.setDeviceProfileId(deviceProfileId); | ||
436 | - tbDevice.setLabel(deviceDTO.getLabel()); | ||
437 | - tbDevice.setName(deviceDTO.getName()); | ||
438 | - tbDevice.setCreatedTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()); | ||
439 | - tbDevice.setTenantId(tenantId); | ||
440 | - return tbDevice; | 309 | + // 如果当前用户是客户 |
310 | + if (getCurrentUser().isCustomerUser()) { | ||
311 | + queryMap.put("customerId", getCurrentUser().getCustomerId().toString()); | ||
441 | } | 312 | } |
442 | - | ||
443 | - /** | ||
444 | - * 添加关联关系 | ||
445 | - * | ||
446 | - * @param tenantId 租户ID | ||
447 | - * @param gateWayTbDeviceId 网关设备Tb的设备ID | ||
448 | - * @param deviceId 设备的tbDeviceId | ||
449 | - */ | ||
450 | - private void addRelation(TenantId tenantId, String gateWayTbDeviceId, String deviceId) | ||
451 | - throws ThingsboardException { | ||
452 | - EntityRelation relation = new EntityRelation(); | ||
453 | - relation.setFrom(new DeviceId(UUID.fromString(gateWayTbDeviceId))); | ||
454 | - relation.setTypeGroup(RelationTypeGroup.COMMON); | ||
455 | - relation.setTo(new DeviceId(UUID.fromString(deviceId))); | ||
456 | - relation.setType(FastIotConstants.Relation.relationType); | ||
457 | - relationService.saveRelation(tenantId, relation); | ||
458 | - sendRelationNotificationMsg( | ||
459 | - getTenantId(), relation, EdgeEventActionType.RELATION_ADD_OR_UPDATE); | 313 | + return deviceService.pageRelation(queryMap); |
314 | + } | ||
315 | + | ||
316 | + @PostMapping("/import") | ||
317 | + @ApiOperation("导入配置") | ||
318 | + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:import'})") | ||
319 | + public ResponseEntity<String> importDeviceProfile() { | ||
320 | + // TODO 实现的业务功能 | ||
321 | + return ResponseEntity.ok(""); | ||
322 | + } | ||
323 | + | ||
324 | + @PostMapping("/export") | ||
325 | + @ApiOperation("导出") | ||
326 | + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:export'})") | ||
327 | + public ResponseEntity<String> exportDeviceProfile() { | ||
328 | + // TODO 实现的业务功能 | ||
329 | + return ResponseEntity.ok(""); | ||
330 | + } | ||
331 | + | ||
332 | + @DeleteMapping | ||
333 | + @ApiOperation("删除") | ||
334 | + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:device:delete'})") | ||
335 | + public void deleteDevices(@Validated({DeleteGroup.class}) @RequestBody DeleteDTO deleteDTO) | ||
336 | + throws ThingsboardException { | ||
337 | + String currentTenantId = getCurrentUser().getCurrentTenantId(); | ||
338 | + List<String> tdIds = deviceService.findTbDeviceId(currentTenantId, deleteDTO.getIds()); | ||
339 | + for (String id : tdIds) { | ||
340 | + deleteTbDevice(id); | ||
460 | } | 341 | } |
461 | - @GetMapping({"used/{id}"}) | ||
462 | - @ApiOperation("设备是否被占用") | ||
463 | - public ResponseResult<Boolean> otherUsing(@PathVariable("id") String deviceId) throws ThingsboardException { | ||
464 | - String str = deviceService.otherUsing(deviceId, getCurrentUser().getCurrentTenantId()); | ||
465 | - ResponseResult result =ResponseResult.success(StringUtils.isEmpty(str)?true:false); | ||
466 | - result.setMessage(str); | ||
467 | - return result; | 342 | + deviceService.deleteDevices(currentTenantId, deleteDTO.getIds()); |
343 | + } | ||
344 | + | ||
345 | + private void deleteTbDevice(String id) throws ThingsboardException { | ||
346 | + DeviceId deviceId = new DeviceId(toUUID(id)); | ||
347 | + Device device = checkDeviceId(deviceId, Operation.DELETE); | ||
348 | + | ||
349 | + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), deviceId); | ||
350 | + | ||
351 | + tbDeviceService.deleteDevice(getCurrentUser().getTenantId(), deviceId); | ||
352 | + | ||
353 | + tbClusterService.onDeviceDeleted(device, null); | ||
354 | + | ||
355 | + logEntityAction(deviceId, device, device.getCustomerId(), ActionType.DELETED, null, id); | ||
356 | + | ||
357 | + sendDeleteNotificationMsg(getTenantId(), deviceId, relatedEdgeIds); | ||
358 | + } | ||
359 | + | ||
360 | + @GetMapping("/list/{deviceType}") | ||
361 | + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{})") | ||
362 | + @ApiOperation("获取该组织的所有设备") | ||
363 | + public List<DeviceDTO> getGatewayDevices( | ||
364 | + @ApiParam(value = "组织ID") @RequestParam("organizationId") String organizationId, | ||
365 | + @ApiParam(value = "设备类型") @PathVariable("deviceType") DeviceTypeEnum deviceType) | ||
366 | + throws ThingsboardException { | ||
367 | + return deviceService.findDevicesByDeviceTypeAndOrganizationId( | ||
368 | + deviceType, getCurrentUser().getCurrentTenantId(), organizationId); | ||
369 | + } | ||
370 | + | ||
371 | + @GetMapping("/list/master/{organizationId}") | ||
372 | + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})") | ||
373 | + @ApiOperation("主设备列表") | ||
374 | + public List<SelectItemDTO> getMasterDevices( | ||
375 | + @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId) | ||
376 | + throws ThingsboardException { | ||
377 | + return deviceService.findMasterDevices( | ||
378 | + getCurrentUser().getCurrentTenantId(), | ||
379 | + getCurrentUser().isCustomerUser() ? getCurrentUser().getCustomerId().toString() : null, | ||
380 | + organizationId); | ||
381 | + } | ||
382 | + | ||
383 | + @GetMapping("/list/slave/{organizationId}") | ||
384 | + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})") | ||
385 | + @ApiOperation("从设备列表") | ||
386 | + public List<SelectItemDTO> getSlaveDevices( | ||
387 | + @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId, | ||
388 | + @ApiParam(value = "主设备ID") @RequestParam(value = "masterId", required = false) | ||
389 | + String masterId) | ||
390 | + throws ThingsboardException { | ||
391 | + return deviceService.findSlaveDevices( | ||
392 | + masterId, | ||
393 | + getCurrentUser().getCurrentTenantId(), | ||
394 | + getCurrentUser().isCustomerUser() ? getCurrentUser().getCurrentUserId() : null, | ||
395 | + organizationId); | ||
396 | + } | ||
397 | + | ||
398 | + @GetMapping("/keys/{organizationId}") | ||
399 | + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})") | ||
400 | + @ApiOperation("设备遥测指标名称") | ||
401 | + public List<String> listKeys( | ||
402 | + @ApiParam(value = "组织ID") @PathVariable("organizationId") String organizationId, | ||
403 | + @ApiParam(value = "设备ID") @RequestParam(value = "deviceIds", required = false) | ||
404 | + List<String> deviceIds) | ||
405 | + throws ThingsboardException { | ||
406 | + return deviceService.findDeviceKeys( | ||
407 | + getCurrentUser().getCurrentTenantId(), | ||
408 | + getCurrentUser().isCustomerUser() ? getCurrentUser().getCustomerId().toString() : null, | ||
409 | + organizationId, | ||
410 | + deviceIds); | ||
411 | + } | ||
412 | + | ||
413 | + @GetMapping("/gateway/{tbDeviceId}") | ||
414 | + @ApiOperation("获取网关设备") | ||
415 | + public DeviceDTO findGateWayDeviceByTbDeviceId( | ||
416 | + @ApiParam(value = "tb设备Id") @PathVariable("tbDeviceId") String tbDeviceId) | ||
417 | + throws ThingsboardException { | ||
418 | + List<DeviceDTO> list = | ||
419 | + deviceService.findGateWayDeviceByTbDeviceId( | ||
420 | + getCurrentUser().getCurrentTenantId(), tbDeviceId); | ||
421 | + return null != list && list.size() > FastIotConstants.MagicNumber.ZERO | ||
422 | + ? list.get(FastIotConstants.MagicNumber.ZERO) | ||
423 | + : null; | ||
424 | + } | ||
425 | + | ||
426 | + @GetMapping("/get_subset/{tbDeviceId}") | ||
427 | + @ApiOperation("获取网关子设备信息") | ||
428 | + public DeviceDTO getSubsetDeviceByTbDeviceId( | ||
429 | + @ApiParam(value = "tb设备Id") @PathVariable("tbDeviceId") String tbDeviceId) | ||
430 | + throws ThingsboardException { | ||
431 | + return deviceService.getSubsetDeviceByTbDeviceId( | ||
432 | + getCurrentUser().getCurrentTenantId(), tbDeviceId); | ||
433 | + } | ||
434 | + | ||
435 | + private Device buildTbDeviceFromDeviceDTO(TenantId tenantId, DeviceDTO deviceDTO) { | ||
436 | + Device tbDevice = new Device(); | ||
437 | + String deviceId = deviceDTO.getTbDeviceId(); | ||
438 | + if (StringUtils.isNotBlank(deviceId)) { | ||
439 | + DeviceId id = new DeviceId(UUID.fromString(deviceId)); | ||
440 | + tbDevice.setId(id); | ||
468 | } | 441 | } |
442 | + ObjectNode additionalInfo = objectMapper.createObjectNode(); | ||
443 | + additionalInfo.put( | ||
444 | + "gateway", | ||
445 | + Optional.ofNullable(deviceDTO.getDeviceType()) | ||
446 | + .map(deviceType -> deviceType.equals(DeviceTypeEnum.GATEWAY)) | ||
447 | + .orElse(false)); | ||
448 | + additionalInfo.put( | ||
449 | + "description", | ||
450 | + Optional.ofNullable(deviceDTO.getDeviceInfo()) | ||
451 | + .map(deviceInfo -> deviceInfo.get("description")) | ||
452 | + .map(JsonNode::asText) | ||
453 | + .orElse("")); | ||
454 | + additionalInfo.put("overwriteActivityTime", false); | ||
455 | + | ||
456 | + DeviceProfileId deviceProfileId = | ||
457 | + new DeviceProfileId(UUID.fromString(deviceDTO.getProfileId())); | ||
458 | + | ||
459 | + tbDevice.setAdditionalInfo(additionalInfo); | ||
460 | + tbDevice.setCustomerId(null); | ||
461 | + tbDevice.setDeviceProfileId(deviceProfileId); | ||
462 | + tbDevice.setLabel(deviceDTO.getLabel()); | ||
463 | + tbDevice.setName(deviceDTO.getName()); | ||
464 | + tbDevice.setCreatedTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()); | ||
465 | + tbDevice.setTenantId(tenantId); | ||
466 | + return tbDevice; | ||
467 | + } | ||
468 | + | ||
469 | + /** | ||
470 | + * 添加关联关系 | ||
471 | + * | ||
472 | + * @param tenantId 租户ID | ||
473 | + * @param gateWayTbDeviceId 网关设备Tb的设备ID | ||
474 | + * @param deviceId 设备的tbDeviceId | ||
475 | + */ | ||
476 | + private void addRelation(TenantId tenantId, String gateWayTbDeviceId, String deviceId) | ||
477 | + throws ThingsboardException { | ||
478 | + EntityRelation relation = new EntityRelation(); | ||
479 | + relation.setFrom(new DeviceId(UUID.fromString(gateWayTbDeviceId))); | ||
480 | + relation.setTypeGroup(RelationTypeGroup.COMMON); | ||
481 | + relation.setTo(new DeviceId(UUID.fromString(deviceId))); | ||
482 | + relation.setType(FastIotConstants.Relation.relationType); | ||
483 | + relationService.saveRelation(tenantId, relation); | ||
484 | + sendRelationNotificationMsg( | ||
485 | + getTenantId(), relation, EdgeEventActionType.RELATION_ADD_OR_UPDATE); | ||
486 | + } | ||
487 | + | ||
488 | + @GetMapping({"used/{id}"}) | ||
489 | + @ApiOperation("设备是否被占用") | ||
490 | + public ResponseResult<Boolean> otherUsing(@PathVariable("id") String deviceId) | ||
491 | + throws ThingsboardException { | ||
492 | + String str = deviceService.otherUsing(deviceId, getCurrentUser().getCurrentTenantId()); | ||
493 | + ResponseResult result = ResponseResult.success(StringUtils.isEmpty(str) ? true : false); | ||
494 | + result.setMessage(str); | ||
495 | + return result; | ||
496 | + } | ||
469 | } | 497 | } |
@@ -44,461 +44,497 @@ import java.util.stream.Collectors; | @@ -44,461 +44,497 @@ import java.util.stream.Collectors; | ||
44 | @RequiredArgsConstructor | 44 | @RequiredArgsConstructor |
45 | @Slf4j | 45 | @Slf4j |
46 | public class YtDeviceServiceImpl extends AbstractBaseService<DeviceMapper, YtDevice> | 46 | public class YtDeviceServiceImpl extends AbstractBaseService<DeviceMapper, YtDevice> |
47 | - implements YtDeviceService { | 47 | + implements YtDeviceService { |
48 | 48 | ||
49 | - private final DeviceProfileDao deviceProfileDao; | 49 | + private final DeviceProfileDao deviceProfileDao; |
50 | 50 | ||
51 | - private final OrganizationMapper ytOrganizationMapper; | ||
52 | - private final SceneLinkageMapper sceneLinkageMapper; | ||
53 | - private final TriggerMapper triggerMapper; | ||
54 | - private final DoConditionMapper conditionMapper; | ||
55 | - private final DoActionMapper actionMapper; | 51 | + private final OrganizationMapper ytOrganizationMapper; |
52 | + private final SceneLinkageMapper sceneLinkageMapper; | ||
53 | + private final TriggerMapper triggerMapper; | ||
54 | + private final DoConditionMapper conditionMapper; | ||
55 | + private final DoActionMapper actionMapper; | ||
56 | 56 | ||
57 | - @Override | ||
58 | - @Transactional | ||
59 | - public DeviceDTO insertOrUpdate(String tenantId, DeviceDTO deviceDTO) { | 57 | + @Override |
58 | + @Transactional | ||
59 | + public DeviceDTO insertOrUpdate(String tenantId, DeviceDTO deviceDTO) { | ||
60 | 60 | ||
61 | - if (StringUtils.isBlank(deviceDTO.getId())) { | ||
62 | - return insert(deviceDTO); | ||
63 | - } else { | ||
64 | - return update(deviceDTO); | ||
65 | - } | 61 | + if (StringUtils.isBlank(deviceDTO.getId())) { |
62 | + return insert(deviceDTO); | ||
63 | + } else { | ||
64 | + return update(deviceDTO); | ||
66 | } | 65 | } |
67 | - | ||
68 | - private DeviceDTO update(DeviceDTO deviceDTO) { | ||
69 | - | ||
70 | - validateUpdate(deviceDTO); | ||
71 | - | ||
72 | - YtDevice device = new YtDevice(); | ||
73 | - deviceDTO.copyToEntity( | ||
74 | - device, | ||
75 | - ModelConstants.TablePropertyMapping.ACTIVE_TIME, | ||
76 | - ModelConstants.TablePropertyMapping.DEVICE_STATE, | ||
77 | - ModelConstants.TablePropertyMapping.TB_DEVICE_ID, | ||
78 | - ModelConstants.TablePropertyMapping.TENANT_CODE, | ||
79 | - ModelConstants.TablePropertyMapping.CREATOR, | ||
80 | - ModelConstants.TablePropertyMapping.UPDATER, | ||
81 | - ModelConstants.TablePropertyMapping.CREATE_TIME, | ||
82 | - ModelConstants.TablePropertyMapping.UPDATE, | ||
83 | - ModelConstants.TablePropertyMapping.UPDATE_TIME); | ||
84 | - baseMapper.updateById(device); | ||
85 | - return device.getDTO(DeviceDTO.class); | ||
86 | - } | ||
87 | - | ||
88 | - private void validateUpdate(DeviceDTO deviceDTO) { | ||
89 | - if (StringUtils.isEmpty(deviceDTO.getName())) { | ||
90 | - throw new YtDataValidationException("device name must be specific"); | ||
91 | - } | ||
92 | - } | ||
93 | - | ||
94 | - @Override | ||
95 | - public boolean validateFormdata(String currentTenantId, DeviceDTO deviceDTO) { | ||
96 | - boolean insert = StringUtils.isBlank(deviceDTO.getId()); | ||
97 | - String deviceTenantId = deviceDTO.getTenantId(); | ||
98 | - if (StringUtils.isBlank(deviceDTO.getName())) { | ||
99 | - throw new YtDataValidationException("device name cannot be blank"); | ||
100 | - } | ||
101 | - // validate IOT DB | ||
102 | - if (StringUtils.isBlank(deviceDTO.getProfileId())) { | ||
103 | - throw new YtDataValidationException("device profile cannot be blank"); | ||
104 | - } | ||
105 | - | ||
106 | - // 验证设备名称是否已经存在 如果此处直接使用deviceDTO 将有误 | ||
107 | - if (insert) { | ||
108 | - DeviceDTO check = new DeviceDTO(); | ||
109 | - check.setName(deviceDTO.getName()); | ||
110 | - if (findTbDeviceId(deviceTenantId, check).size() > 0) { | ||
111 | - throw new YtDataValidationException(ErrorMessage.NAME_ALREADY_EXISTS.getMessage()); | ||
112 | - } | ||
113 | - | ||
114 | - deviceTenantId = currentTenantId; | ||
115 | - deviceDTO.setTenantId(currentTenantId); | ||
116 | - } else { | ||
117 | - deviceTenantId = deviceDTO.getTenantId(); | ||
118 | - YtDevice device = baseMapper.selectById(deviceDTO.getId()); | ||
119 | - if (device == null) { | ||
120 | - throw new YtDataValidationException("设备不存在!"); | ||
121 | - } | ||
122 | - if (!device.getTenantId().equals(currentTenantId)) { | ||
123 | - throw new YtDataValidationException(ErrorMessage.TENANT_MISMATCHING.getMessage()); | ||
124 | - } | ||
125 | - String oldOrganizationId = device.getOrganizationId(); | ||
126 | - if (!oldOrganizationId.equals(deviceDTO.getOrganizationId())) { | ||
127 | - sceneNotUsed(currentTenantId, deviceDTO.getTbDeviceId(), oldOrganizationId); | ||
128 | - } | ||
129 | - } | ||
130 | - // 验证数据profileId的正确性 | ||
131 | - TenantId id = TenantId.fromUUID(UUID.fromString(deviceTenantId)); | ||
132 | - DeviceProfile deviceProfile = | ||
133 | - deviceProfileDao.findById(id, UUID.fromString(deviceDTO.getProfileId())); | ||
134 | - Organization organization = ytOrganizationMapper.selectById(deviceDTO.getOrganizationId()); | ||
135 | - if (null == deviceProfile || null == organization) { | ||
136 | - throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); | ||
137 | - } else if (!organization.getTenantId().equals(deviceTenantId)) { | ||
138 | - throw new YtDataValidationException(ErrorMessage.TENANT_MISMATCHING.getMessage()); | ||
139 | - } | ||
140 | - return true; | 66 | + } |
67 | + | ||
68 | + private DeviceDTO update(DeviceDTO deviceDTO) { | ||
69 | + | ||
70 | + validateUpdate(deviceDTO); | ||
71 | + | ||
72 | + YtDevice device = new YtDevice(); | ||
73 | + deviceDTO.copyToEntity( | ||
74 | + device, | ||
75 | + ModelConstants.TablePropertyMapping.ACTIVE_TIME, | ||
76 | + ModelConstants.TablePropertyMapping.DEVICE_STATE, | ||
77 | + ModelConstants.TablePropertyMapping.TB_DEVICE_ID, | ||
78 | + ModelConstants.TablePropertyMapping.TENANT_CODE, | ||
79 | + ModelConstants.TablePropertyMapping.CREATOR, | ||
80 | + ModelConstants.TablePropertyMapping.UPDATER, | ||
81 | + ModelConstants.TablePropertyMapping.CREATE_TIME, | ||
82 | + ModelConstants.TablePropertyMapping.UPDATE, | ||
83 | + ModelConstants.TablePropertyMapping.UPDATE_TIME); | ||
84 | + baseMapper.updateById(device); | ||
85 | + return device.getDTO(DeviceDTO.class); | ||
86 | + } | ||
87 | + | ||
88 | + private void validateUpdate(DeviceDTO deviceDTO) { | ||
89 | + if (StringUtils.isEmpty(deviceDTO.getName())) { | ||
90 | + throw new YtDataValidationException("device name must be specific"); | ||
141 | } | 91 | } |
142 | - | ||
143 | - /** | ||
144 | - * 场景联动是否在使用该设备 | ||
145 | - * | ||
146 | - * @param tbDeviceId 设备接入平台的设备主键 | ||
147 | - * @param organizationId 组织主键 | ||
148 | - * @return | ||
149 | - */ | ||
150 | - private boolean sceneNotUsed(String tenantId, String tbDeviceId, String organizationId) { | ||
151 | - boolean result = true; | ||
152 | - /**激活的场景联动使用了设备,例如:触发器、自行条件、动作等。*/ | ||
153 | - List<SceneLinkage> scenes = sceneLinkageMapper.selectList(new QueryWrapper<SceneLinkage>().lambda() | ||
154 | - .eq(SceneLinkage::getTenantId, tenantId) | ||
155 | - .eq(StringUtils.isNotEmpty(organizationId), SceneLinkage::getOrganizationId, organizationId) | ||
156 | - ); | ||
157 | - if (scenes == null || scenes.isEmpty()) { | ||
158 | - return true; | ||
159 | - } | ||
160 | - Set<String> sceneNames = new HashSet<>(); | ||
161 | - for (SceneLinkage scene : scenes) { | ||
162 | - List<Trigger> triggers = triggerMapper.selectList(new QueryWrapper<Trigger>().lambda() | ||
163 | - .eq(Trigger::getSceneLinkageId, scene.getId()) | ||
164 | - .eq(scene.getStatus() == FastIotConstants.StateValue.DISABLE, Trigger::getTriggerType, ScopeEnum.PART) | ||
165 | - .like(Trigger::getEntityId, tbDeviceId)); | ||
166 | - if (triggers != null && triggers.size() > 0) { | ||
167 | - sceneNames.add(scene.getName()); | ||
168 | - } | ||
169 | - List<DoCondition> conditions = conditionMapper.selectList(new QueryWrapper<DoCondition>().lambda() | ||
170 | - .eq(DoCondition::getSceneLinkageId, scene.getId()) | ||
171 | - .eq(scene.getStatus() == FastIotConstants.StateValue.DISABLE, DoCondition::getEntityType, ScopeEnum.PART) | ||
172 | - .like(DoCondition::getEntityId, tbDeviceId)); | ||
173 | - if (conditions != null && conditions.size() > 0) { | ||
174 | - sceneNames.add(scene.getName()); | ||
175 | - } | ||
176 | - List<DoAction> actions = actionMapper.selectList(new QueryWrapper<DoAction>().lambda() | ||
177 | - .eq(DoAction::getSceneLinkageId, scene.getId()) | ||
178 | - .eq(scene.getStatus() == FastIotConstants.StateValue.DISABLE, DoAction::getEntityType, ScopeEnum.PART) | ||
179 | - .like(DoAction::getDeviceId, tbDeviceId)); | ||
180 | - if (actions != null && actions.size() > 0) { | ||
181 | - sceneNames.add(scene.getName()); | ||
182 | - } | ||
183 | - | ||
184 | - } | ||
185 | - if(sceneNames.size()>0){ | ||
186 | - throw new YtDataValidationException(String.format(ErrorMessage.DEVICE_USED_SCENE_REACT.getMessage(), sceneNames)); | ||
187 | - } | ||
188 | - | ||
189 | - | ||
190 | - return result; | ||
191 | - | ||
192 | - } | ||
193 | - | ||
194 | - private DeviceDTO insert(DeviceDTO deviceDTO) { | ||
195 | - | ||
196 | - YtDevice device = new YtDevice(); | ||
197 | - deviceDTO.copyToEntity( | ||
198 | - device, | ||
199 | - ModelConstants.TablePropertyMapping.ACTIVE_TIME, | ||
200 | - ModelConstants.TablePropertyMapping.DEVICE_STATE, | ||
201 | - ModelConstants.TablePropertyMapping.CREATOR, | ||
202 | - ModelConstants.TablePropertyMapping.UPDATER, | ||
203 | - ModelConstants.TablePropertyMapping.CREATE_TIME, | ||
204 | - ModelConstants.TablePropertyMapping.UPDATE, | ||
205 | - ModelConstants.TablePropertyMapping.UPDATE_TIME); | ||
206 | - | ||
207 | - device.setAlarmStatus(0); | ||
208 | - baseMapper.insert(device); | ||
209 | - return device.getDTO(DeviceDTO.class); | 92 | + } |
93 | + | ||
94 | + @Override | ||
95 | + public boolean validateFormdata(String currentTenantId, DeviceDTO deviceDTO) { | ||
96 | + boolean insert = StringUtils.isBlank(deviceDTO.getId()); | ||
97 | + String deviceTenantId = deviceDTO.getTenantId(); | ||
98 | + if (StringUtils.isBlank(deviceDTO.getName())) { | ||
99 | + throw new YtDataValidationException("device name cannot be blank"); | ||
210 | } | 100 | } |
211 | - | ||
212 | - @Override | ||
213 | - public List<String> findTbDeviceId(String tenantId, Set<String> ids) { | ||
214 | - LambdaQueryWrapper<YtDevice> queryWrapper = | ||
215 | - new QueryWrapper<YtDevice>() | ||
216 | - .lambda() | ||
217 | - .eq(YtDevice::getTenantId, tenantId) | ||
218 | - .in(YtDevice::getId, ids); | ||
219 | - | ||
220 | - List<String> tbDeviceIds = | ||
221 | - baseMapper.selectList(queryWrapper).stream() | ||
222 | - .map(YtDevice::getTbDeviceId) | ||
223 | - .collect(Collectors.toList()); | ||
224 | - for (String tbDeviceId : tbDeviceIds) { | ||
225 | - sceneNotUsed(tenantId, tbDeviceId, null); | ||
226 | - } | ||
227 | - return tbDeviceIds; | 101 | + // validate IOT DB |
102 | + if (StringUtils.isBlank(deviceDTO.getProfileId())) { | ||
103 | + throw new YtDataValidationException("device profile cannot be blank"); | ||
228 | } | 104 | } |
229 | 105 | ||
230 | - @Override | ||
231 | - public List<DeviceDTO> findDevicesByDeviceTypeAndOrganizationId( | ||
232 | - DeviceTypeEnum deviceType, String tenantId, String organizationId) { | ||
233 | - List<String> orgIds = organizationAllIds(tenantId,organizationId); | ||
234 | - return ReflectUtils.sourceToTarget( | ||
235 | - baseMapper.selectList( | ||
236 | - new LambdaQueryWrapper<YtDevice>() | ||
237 | - .eq(YtDevice::getDeviceType, deviceType) | ||
238 | - .in(YtDevice::getOrganizationId, orgIds)), | ||
239 | - DeviceDTO.class); | 106 | + // 验证设备名称是否已经存在 如果此处直接使用deviceDTO 将有误 |
107 | + if (insert) { | ||
108 | + DeviceDTO check = new DeviceDTO(); | ||
109 | + check.setName(deviceDTO.getName()); | ||
110 | + if (findTbDeviceId(deviceTenantId, check).size() > 0) { | ||
111 | + throw new YtDataValidationException(ErrorMessage.NAME_ALREADY_EXISTS.getMessage()); | ||
112 | + } | ||
113 | + | ||
114 | + deviceTenantId = currentTenantId; | ||
115 | + deviceDTO.setTenantId(currentTenantId); | ||
116 | + } else { | ||
117 | + deviceTenantId = deviceDTO.getTenantId(); | ||
118 | + YtDevice device = baseMapper.selectById(deviceDTO.getId()); | ||
119 | + if (device == null) { | ||
120 | + throw new YtDataValidationException("设备不存在!"); | ||
121 | + } | ||
122 | + if (!device.getTenantId().equals(currentTenantId)) { | ||
123 | + throw new YtDataValidationException(ErrorMessage.TENANT_MISMATCHING.getMessage()); | ||
124 | + } | ||
125 | + String oldOrganizationId = device.getOrganizationId(); | ||
126 | + if (!oldOrganizationId.equals(deviceDTO.getOrganizationId())) { | ||
127 | + sceneNotUsed(currentTenantId, deviceDTO.getTbDeviceId(), oldOrganizationId); | ||
128 | + } | ||
240 | } | 129 | } |
241 | - | ||
242 | - @Override | ||
243 | - public DeviceDTO checkDeviceByTenantIdAndDeviceId(String tenantId, String deviceId) { | ||
244 | - if (StringUtils.isEmpty(tenantId) || StringUtils.isEmpty(deviceId)) { | ||
245 | - throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); | ||
246 | - } | ||
247 | - return baseMapper | ||
248 | - .selectOne( | ||
249 | - new LambdaQueryWrapper<YtDevice>() | ||
250 | - .eq(YtDevice::getTenantId, tenantId) | ||
251 | - .eq(YtDevice::getId, deviceId)) | ||
252 | - .getDTO(DeviceDTO.class); | 130 | + // 验证数据profileId的正确性 |
131 | + TenantId id = TenantId.fromUUID(UUID.fromString(deviceTenantId)); | ||
132 | + DeviceProfile deviceProfile = | ||
133 | + deviceProfileDao.findById(id, UUID.fromString(deviceDTO.getProfileId())); | ||
134 | + Organization organization = ytOrganizationMapper.selectById(deviceDTO.getOrganizationId()); | ||
135 | + if (null == deviceProfile || null == organization) { | ||
136 | + throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); | ||
137 | + } else if (!organization.getTenantId().equals(deviceTenantId)) { | ||
138 | + throw new YtDataValidationException(ErrorMessage.TENANT_MISMATCHING.getMessage()); | ||
253 | } | 139 | } |
254 | - | ||
255 | - @Override | ||
256 | - public List<DeviceDTO> findGateWayDeviceByTbDeviceId(String tenantId, String tbDeviceId) { | ||
257 | - checkDeviceByTenantIdAndId(tenantId, tbDeviceId, true); | ||
258 | - return baseMapper.findGateWayDeviceByTbDeviceId(tbDeviceId); | 140 | + return true; |
141 | + } | ||
142 | + | ||
143 | + /** | ||
144 | + * 场景联动是否在使用该设备 | ||
145 | + * | ||
146 | + * @param tbDeviceId 设备接入平台的设备主键 | ||
147 | + * @param organizationId 组织主键 | ||
148 | + * @return | ||
149 | + */ | ||
150 | + private boolean sceneNotUsed(String tenantId, String tbDeviceId, String organizationId) { | ||
151 | + boolean result = true; | ||
152 | + /** 激活的场景联动使用了设备,例如:触发器、自行条件、动作等。 */ | ||
153 | + List<SceneLinkage> scenes = | ||
154 | + sceneLinkageMapper.selectList( | ||
155 | + new QueryWrapper<SceneLinkage>() | ||
156 | + .lambda() | ||
157 | + .eq(SceneLinkage::getTenantId, tenantId) | ||
158 | + .eq( | ||
159 | + StringUtils.isNotEmpty(organizationId), | ||
160 | + SceneLinkage::getOrganizationId, | ||
161 | + organizationId)); | ||
162 | + if (scenes == null || scenes.isEmpty()) { | ||
163 | + return true; | ||
259 | } | 164 | } |
260 | - | ||
261 | - @Override | ||
262 | - public DeviceDTO checkDeviceByTenantIdAndId( | ||
263 | - String tenantId, String deviceId, boolean isTbDeviceId) { | ||
264 | - if (StringUtils.isEmpty(deviceId) || StringUtils.isEmpty(tenantId)) { | ||
265 | - throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); | ||
266 | - } | ||
267 | - YtDevice device = | ||
268 | - baseMapper.selectOne( | ||
269 | - new LambdaQueryWrapper<YtDevice>() | ||
270 | - .eq(YtDevice::getTenantId, tenantId) | ||
271 | - .eq(isTbDeviceId, YtDevice::getTbDeviceId, deviceId) | ||
272 | - .eq(!isTbDeviceId, YtDevice::getId, deviceId)); | ||
273 | - DeviceDTO deviceDTO = null != device ? device.getDTO(DeviceDTO.class) : null; | ||
274 | - if (null == deviceDTO) { | ||
275 | - throw new YtDataValidationException(ErrorMessage.DEVICE_NOT_EXISTENCE_IN_TENANT.getMessage()); | ||
276 | - } | ||
277 | - return deviceDTO; | 165 | + Set<String> sceneNames = new HashSet<>(); |
166 | + for (SceneLinkage scene : scenes) { | ||
167 | + List<Trigger> triggers = | ||
168 | + triggerMapper.selectList( | ||
169 | + new QueryWrapper<Trigger>() | ||
170 | + .lambda() | ||
171 | + .eq(Trigger::getSceneLinkageId, scene.getId()) | ||
172 | + .eq( | ||
173 | + scene.getStatus() == FastIotConstants.StateValue.DISABLE, | ||
174 | + Trigger::getTriggerType, | ||
175 | + ScopeEnum.PART) | ||
176 | + .like(Trigger::getEntityId, tbDeviceId)); | ||
177 | + if (triggers != null && triggers.size() > 0) { | ||
178 | + sceneNames.add(scene.getName()); | ||
179 | + } | ||
180 | + List<DoCondition> conditions = | ||
181 | + conditionMapper.selectList( | ||
182 | + new QueryWrapper<DoCondition>() | ||
183 | + .lambda() | ||
184 | + .eq(DoCondition::getSceneLinkageId, scene.getId()) | ||
185 | + .eq( | ||
186 | + scene.getStatus() == FastIotConstants.StateValue.DISABLE, | ||
187 | + DoCondition::getEntityType, | ||
188 | + ScopeEnum.PART) | ||
189 | + .like(DoCondition::getEntityId, tbDeviceId)); | ||
190 | + if (conditions != null && conditions.size() > 0) { | ||
191 | + sceneNames.add(scene.getName()); | ||
192 | + } | ||
193 | + List<DoAction> actions = | ||
194 | + actionMapper.selectList( | ||
195 | + new QueryWrapper<DoAction>() | ||
196 | + .lambda() | ||
197 | + .eq(DoAction::getSceneLinkageId, scene.getId()) | ||
198 | + .eq( | ||
199 | + scene.getStatus() == FastIotConstants.StateValue.DISABLE, | ||
200 | + DoAction::getEntityType, | ||
201 | + ScopeEnum.PART) | ||
202 | + .like(DoAction::getDeviceId, tbDeviceId)); | ||
203 | + if (actions != null && actions.size() > 0) { | ||
204 | + sceneNames.add(scene.getName()); | ||
205 | + } | ||
278 | } | 206 | } |
279 | - | ||
280 | - @Override | ||
281 | - @Transactional | ||
282 | - public void deleteDevices(String tenantId, Set<String> ids) { | ||
283 | - LambdaQueryWrapper<YtDevice> queryWrapper = | ||
284 | - new QueryWrapper<YtDevice>() | ||
285 | - .lambda() | ||
286 | - .eq(YtDevice::getTenantId, tenantId) | ||
287 | - .in(YtDevice::getId, ids); | ||
288 | - | ||
289 | - baseMapper.delete(queryWrapper); | 207 | + if (sceneNames.size() > 0) { |
208 | + throw new YtDataValidationException( | ||
209 | + String.format(ErrorMessage.DEVICE_USED_SCENE_REACT.getMessage(), sceneNames)); | ||
290 | } | 210 | } |
291 | 211 | ||
292 | - @Override | ||
293 | - public Optional<DeviceDTO> getDevice(String tenantId, String id) { | ||
294 | - return Optional.ofNullable(baseMapper.selectDetail(tenantId, id)); | 212 | + return result; |
213 | + } | ||
214 | + | ||
215 | + private DeviceDTO insert(DeviceDTO deviceDTO) { | ||
216 | + | ||
217 | + YtDevice device = new YtDevice(); | ||
218 | + deviceDTO.copyToEntity( | ||
219 | + device, | ||
220 | + ModelConstants.TablePropertyMapping.ACTIVE_TIME, | ||
221 | + ModelConstants.TablePropertyMapping.DEVICE_STATE, | ||
222 | + ModelConstants.TablePropertyMapping.CREATOR, | ||
223 | + ModelConstants.TablePropertyMapping.UPDATER, | ||
224 | + ModelConstants.TablePropertyMapping.CREATE_TIME, | ||
225 | + ModelConstants.TablePropertyMapping.UPDATE, | ||
226 | + ModelConstants.TablePropertyMapping.UPDATE_TIME); | ||
227 | + | ||
228 | + device.setAlarmStatus(0); | ||
229 | + baseMapper.insert(device); | ||
230 | + return device.getDTO(DeviceDTO.class); | ||
231 | + } | ||
232 | + | ||
233 | + @Override | ||
234 | + public List<String> findTbDeviceId(String tenantId, Set<String> ids) { | ||
235 | + LambdaQueryWrapper<YtDevice> queryWrapper = | ||
236 | + new QueryWrapper<YtDevice>() | ||
237 | + .lambda() | ||
238 | + .eq(YtDevice::getTenantId, tenantId) | ||
239 | + .in(YtDevice::getId, ids); | ||
240 | + | ||
241 | + List<String> tbDeviceIds = | ||
242 | + baseMapper.selectList(queryWrapper).stream() | ||
243 | + .map(YtDevice::getTbDeviceId) | ||
244 | + .collect(Collectors.toList()); | ||
245 | + for (String tbDeviceId : tbDeviceIds) { | ||
246 | + sceneNotUsed(tenantId, tbDeviceId, null); | ||
295 | } | 247 | } |
296 | - | ||
297 | - @Override | ||
298 | - public YtPageData<DeviceDTO> page(String tenantId, Map<String, Object> queryMap) { | ||
299 | - queryMap.put("tenantId", tenantId); | ||
300 | - String organizationId = (String) queryMap.get("organizationId"); | ||
301 | - if (!StringUtils.isEmpty(organizationId)) { | ||
302 | - List<String> queryOrganizationIds = organizationAllIds(tenantId, organizationId); | ||
303 | - queryMap.put("organizationIds", queryOrganizationIds); | ||
304 | - } | ||
305 | - IPage<YtDevice> page = getPage(queryMap, FastIotConstants.DefaultOrder.CREATE_TIME, false); | ||
306 | - IPage<DeviceDTO> deviceIPage = baseMapper.getDevicePage(page, queryMap); | ||
307 | - List<DeviceDTO> records = deviceIPage.getRecords(); | ||
308 | - records.forEach( | ||
309 | - deviceDTO -> { | ||
310 | - if (null != deviceDTO.getCustomerId() | ||
311 | - && deviceDTO.getCustomerId().equals(EntityId.NULL_UUID.toString())) { | ||
312 | - deviceDTO.setCustomerId(null); | ||
313 | - } | ||
314 | - }); | ||
315 | - return new YtPageData<>(records, deviceIPage.getTotal()); | 248 | + return tbDeviceIds; |
249 | + } | ||
250 | + | ||
251 | + @Override | ||
252 | + public List<DeviceDTO> findDevicesByDeviceTypeAndOrganizationId( | ||
253 | + DeviceTypeEnum deviceType, String tenantId, String organizationId) { | ||
254 | + List<String> orgIds = organizationAllIds(tenantId, organizationId); | ||
255 | + return ReflectUtils.sourceToTarget( | ||
256 | + baseMapper.selectList( | ||
257 | + new LambdaQueryWrapper<YtDevice>() | ||
258 | + .eq(YtDevice::getDeviceType, deviceType) | ||
259 | + .in(YtDevice::getOrganizationId, orgIds)), | ||
260 | + DeviceDTO.class); | ||
261 | + } | ||
262 | + | ||
263 | + @Override | ||
264 | + public DeviceDTO checkDeviceByTenantIdAndDeviceId(String tenantId, String deviceId) { | ||
265 | + if (StringUtils.isEmpty(tenantId) || StringUtils.isEmpty(deviceId)) { | ||
266 | + throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); | ||
316 | } | 267 | } |
317 | - | ||
318 | - /** | ||
319 | - * 组织结构下的所有组织ID | ||
320 | - * @param tenantId 租户ID | ||
321 | - * @param organizationId 组织ID | ||
322 | - * @return | ||
323 | - */ | ||
324 | - @NotNull | ||
325 | - private List<String> organizationAllIds(String tenantId, String organizationId) { | ||
326 | - List<String> organizationIds = new ArrayList<>(); | ||
327 | - organizationIds.add(organizationId); | ||
328 | - // 查询该组织的所有子类 | ||
329 | - List<OrganizationDTO> organizationDTOS = | ||
330 | - ytOrganizationMapper.findOrganizationTreeList(tenantId, organizationIds); | ||
331 | - List<String> queryOrganizationIds = new ArrayList<>(); | ||
332 | - organizationDTOS.forEach( | ||
333 | - item -> { | ||
334 | - queryOrganizationIds.add(item.getId()); | ||
335 | - }); | ||
336 | - return queryOrganizationIds; | 268 | + return baseMapper |
269 | + .selectOne( | ||
270 | + new LambdaQueryWrapper<YtDevice>() | ||
271 | + .eq(YtDevice::getTenantId, tenantId) | ||
272 | + .eq(YtDevice::getId, deviceId)) | ||
273 | + .getDTO(DeviceDTO.class); | ||
274 | + } | ||
275 | + | ||
276 | + @Override | ||
277 | + public List<DeviceDTO> findGateWayDeviceByTbDeviceId(String tenantId, String tbDeviceId) { | ||
278 | + checkDeviceByTenantIdAndId(tenantId, tbDeviceId, true); | ||
279 | + return baseMapper.findGateWayDeviceByTbDeviceId(tbDeviceId); | ||
280 | + } | ||
281 | + | ||
282 | + @Override | ||
283 | + public DeviceDTO checkDeviceByTenantIdAndId( | ||
284 | + String tenantId, String deviceId, boolean isTbDeviceId) { | ||
285 | + if (StringUtils.isEmpty(deviceId) || StringUtils.isEmpty(tenantId)) { | ||
286 | + throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); | ||
337 | } | 287 | } |
338 | - | ||
339 | - @Override | ||
340 | - public YtPageData<RelationDeviceDTO> pageRelation(Map<String, Object> queryMap) { | ||
341 | - IPage<YtDevice> page = getPage(queryMap, "last_online_time", false); | ||
342 | - IPage<RelationDeviceDTO> deviceIPage = baseMapper.getRelationDevicePage(page, queryMap); | ||
343 | - List<RelationDeviceDTO> records = deviceIPage.getRecords(); | ||
344 | - return new YtPageData<>(records, deviceIPage.getTotal()); | 288 | + YtDevice device = |
289 | + baseMapper.selectOne( | ||
290 | + new LambdaQueryWrapper<YtDevice>() | ||
291 | + .eq(YtDevice::getTenantId, tenantId) | ||
292 | + .eq(isTbDeviceId, YtDevice::getTbDeviceId, deviceId) | ||
293 | + .eq(!isTbDeviceId, YtDevice::getId, deviceId)); | ||
294 | + DeviceDTO deviceDTO = null != device ? device.getDTO(DeviceDTO.class) : null; | ||
295 | + if (null == deviceDTO) { | ||
296 | + throw new YtDataValidationException(ErrorMessage.DEVICE_NOT_EXISTENCE_IN_TENANT.getMessage()); | ||
345 | } | 297 | } |
346 | - | ||
347 | - @Override | ||
348 | - public List<DeviceDTO> findTbDeviceId(String tenantId, DeviceDTO deviceDTO) { | ||
349 | - List<YtDevice> deviceList = | ||
350 | - baseMapper.selectList( | ||
351 | - new QueryWrapper<YtDevice>() | ||
352 | - .lambda() | ||
353 | - .eq(true, YtDevice::getTenantId, tenantId) | ||
354 | - .eq( | ||
355 | - StringUtils.isNotBlank(deviceDTO.getName()), | ||
356 | - YtDevice::getName, | ||
357 | - deviceDTO.getName())); | ||
358 | - return ReflectUtils.sourceToTarget(deviceList, DeviceDTO.class); | 298 | + return deviceDTO; |
299 | + } | ||
300 | + | ||
301 | + @Override | ||
302 | + @Transactional | ||
303 | + public void deleteDevices(String tenantId, Set<String> ids) { | ||
304 | + LambdaQueryWrapper<YtDevice> queryWrapper = | ||
305 | + new QueryWrapper<YtDevice>() | ||
306 | + .lambda() | ||
307 | + .eq(YtDevice::getTenantId, tenantId) | ||
308 | + .in(YtDevice::getId, ids); | ||
309 | + | ||
310 | + baseMapper.delete(queryWrapper); | ||
311 | + } | ||
312 | + | ||
313 | + @Override | ||
314 | + public Optional<DeviceDTO> getDevice(String tenantId, String id) { | ||
315 | + return Optional.ofNullable(baseMapper.selectDetail(tenantId, id)); | ||
316 | + } | ||
317 | + | ||
318 | + @Override | ||
319 | + public YtPageData<DeviceDTO> page(String tenantId, Map<String, Object> queryMap) { | ||
320 | + queryMap.put("tenantId", tenantId); | ||
321 | + String organizationId = (String) queryMap.get("organizationId"); | ||
322 | + if (!StringUtils.isEmpty(organizationId)) { | ||
323 | + List<String> queryOrganizationIds = organizationAllIds(tenantId, organizationId); | ||
324 | + queryMap.put("organizationIds", queryOrganizationIds); | ||
359 | } | 325 | } |
360 | - | ||
361 | - @Override | ||
362 | - public boolean freshAlarmStatus(EntityId tbDeviceId, Integer created) { | ||
363 | - return baseMapper.freshAlarmStatus(tbDeviceId.getId().toString(), created); | 326 | + IPage<YtDevice> page = getPage(queryMap, FastIotConstants.DefaultOrder.CREATE_TIME, false); |
327 | + IPage<DeviceDTO> deviceIPage = baseMapper.getDevicePage(page, queryMap); | ||
328 | + List<DeviceDTO> records = deviceIPage.getRecords(); | ||
329 | + records.forEach( | ||
330 | + deviceDTO -> { | ||
331 | + if (null != deviceDTO.getCustomerId() | ||
332 | + && deviceDTO.getCustomerId().equals(EntityId.NULL_UUID.toString())) { | ||
333 | + deviceDTO.setCustomerId(null); | ||
334 | + } | ||
335 | + }); | ||
336 | + return new YtPageData<>(records, deviceIPage.getTotal()); | ||
337 | + } | ||
338 | + | ||
339 | + /** | ||
340 | + * 组织结构下的所有组织ID | ||
341 | + * | ||
342 | + * @param tenantId 租户ID | ||
343 | + * @param organizationId 组织ID | ||
344 | + * @return | ||
345 | + */ | ||
346 | + @NotNull | ||
347 | + private List<String> organizationAllIds(String tenantId, String organizationId) { | ||
348 | + List<String> organizationIds = new ArrayList<>(); | ||
349 | + organizationIds.add(organizationId); | ||
350 | + // 查询该组织的所有子类 | ||
351 | + List<OrganizationDTO> organizationDTOS = | ||
352 | + ytOrganizationMapper.findOrganizationTreeList(tenantId, organizationIds); | ||
353 | + List<String> queryOrganizationIds = new ArrayList<>(); | ||
354 | + organizationDTOS.forEach( | ||
355 | + item -> { | ||
356 | + queryOrganizationIds.add(item.getId()); | ||
357 | + }); | ||
358 | + return queryOrganizationIds; | ||
359 | + } | ||
360 | + | ||
361 | + @Override | ||
362 | + public YtPageData<RelationDeviceDTO> pageRelation(Map<String, Object> queryMap) { | ||
363 | + IPage<YtDevice> page = getPage(queryMap, "last_online_time", false); | ||
364 | + IPage<RelationDeviceDTO> deviceIPage = baseMapper.getRelationDevicePage(page, queryMap); | ||
365 | + List<RelationDeviceDTO> records = deviceIPage.getRecords(); | ||
366 | + return new YtPageData<>(records, deviceIPage.getTotal()); | ||
367 | + } | ||
368 | + | ||
369 | + @Override | ||
370 | + public List<DeviceDTO> findTbDeviceId(String tenantId, DeviceDTO deviceDTO) { | ||
371 | + List<YtDevice> deviceList = | ||
372 | + baseMapper.selectList( | ||
373 | + new QueryWrapper<YtDevice>() | ||
374 | + .lambda() | ||
375 | + .eq(true, YtDevice::getTenantId, tenantId) | ||
376 | + .eq( | ||
377 | + StringUtils.isNotBlank(deviceDTO.getName()), | ||
378 | + YtDevice::getName, | ||
379 | + deviceDTO.getName())); | ||
380 | + return ReflectUtils.sourceToTarget(deviceList, DeviceDTO.class); | ||
381 | + } | ||
382 | + | ||
383 | + @Override | ||
384 | + public boolean freshAlarmStatus(EntityId tbDeviceId, Integer created) { | ||
385 | + return baseMapper.freshAlarmStatus(tbDeviceId.getId().toString(), created); | ||
386 | + } | ||
387 | + | ||
388 | + @Override | ||
389 | + public String generateSn() { | ||
390 | + StringBuilder snBuilder = new StringBuilder(); | ||
391 | + for (int i = 0; i < 2; i++) { | ||
392 | + // 生成uuid的hashCode值 | ||
393 | + int hashCode = UUID.randomUUID().toString().hashCode(); | ||
394 | + // 可能为负数 | ||
395 | + if (hashCode < 0) { | ||
396 | + hashCode = -hashCode; | ||
397 | + } | ||
398 | + snBuilder.append(hashCode); | ||
364 | } | 399 | } |
365 | 400 | ||
401 | + return snBuilder.substring(0, 16); | ||
402 | + } | ||
366 | 403 | ||
367 | - @Override | ||
368 | - public String generateSn() { | ||
369 | - StringBuilder snBuilder = new StringBuilder(); | ||
370 | - for (int i = 0; i < 2; i++) { | ||
371 | - //生成uuid的hashCode值 | ||
372 | - int hashCode = UUID.randomUUID().toString().hashCode(); | ||
373 | - //可能为负数 | ||
374 | - if (hashCode < 0) { | ||
375 | - hashCode = -hashCode; | ||
376 | - } | ||
377 | - snBuilder.append(hashCode); | ||
378 | - | ||
379 | - } | ||
380 | - | ||
381 | - return snBuilder.substring(0, 16); | 404 | + @Override |
405 | + public String otherUsing(String deviceId, String tenantId) { | ||
406 | + YtDevice device = baseMapper.selectById(deviceId); | ||
407 | + if (device == null) { | ||
408 | + throw new YtDataValidationException(ErrorMessage.DEVICE_NOT_EXTIED.getMessage()); | ||
382 | } | 409 | } |
383 | - | ||
384 | - | ||
385 | - @Override | ||
386 | - public String otherUsing(String deviceId,String tenantId) { | ||
387 | - YtDevice device = baseMapper.selectById(deviceId); | ||
388 | - if(device == null){ | ||
389 | - throw new YtDataValidationException(ErrorMessage.DEVICE_NOT_EXTIED.getMessage()); | ||
390 | - } | ||
391 | - String tbDeviceId = device.getTbDeviceId(); | ||
392 | - | ||
393 | - return usedBySceneLinkage(tenantId, tbDeviceId); | 410 | + String tbDeviceId = device.getTbDeviceId(); |
411 | + | ||
412 | + return usedBySceneLinkage(tenantId, tbDeviceId); | ||
413 | + } | ||
414 | + | ||
415 | + @Nullable | ||
416 | + private String usedBySceneLinkage(String tenantId, String tbDeviceId) { | ||
417 | + LambdaQueryWrapper<SceneLinkage> sceneFilter = | ||
418 | + new QueryWrapper<SceneLinkage>().lambda().eq(SceneLinkage::getTenantId, tenantId) | ||
419 | + // .eq(SceneLinkage::getStatus, 1) | ||
420 | + ; | ||
421 | + Optional<List<SceneLinkage>> scenes = | ||
422 | + Optional.ofNullable(sceneLinkageMapper.selectList(sceneFilter)); | ||
423 | + if (scenes.isEmpty()) { | ||
424 | + return ""; | ||
394 | } | 425 | } |
395 | - | ||
396 | - @Nullable | ||
397 | - private String usedBySceneLinkage(String tenantId, String tbDeviceId) { | ||
398 | - LambdaQueryWrapper<SceneLinkage> sceneFilter = | ||
399 | - new QueryWrapper<SceneLinkage>() | ||
400 | - .lambda() | ||
401 | - .eq(SceneLinkage::getTenantId, tenantId) | ||
402 | -// .eq(SceneLinkage::getStatus, 1) | ||
403 | - ; | ||
404 | - Optional<List<SceneLinkage>> scenes= Optional.ofNullable(sceneLinkageMapper.selectList(sceneFilter)); | ||
405 | - if(scenes.isEmpty()){ | ||
406 | - return ""; | ||
407 | - } | ||
408 | - Set<String> usedIds = new HashSet<>(); | ||
409 | - List<String> scenFilterIds = scenes.get().stream() | ||
410 | - .map(i -> i.getId()).collect(Collectors.toList()); | ||
411 | - LambdaQueryWrapper<Trigger> triggerFilter = | ||
412 | - new QueryWrapper<Trigger>() | ||
413 | - .lambda() | ||
414 | - .in(scenFilterIds.size()>0,Trigger::getSceneLinkageId, scenFilterIds) | ||
415 | - .like(Trigger::getEntityId, tbDeviceId); | ||
416 | - List<Trigger> triggers = triggerMapper.selectList(triggerFilter); | ||
417 | - triggers.forEach(item ->{ | ||
418 | - usedIds.add(item.getSceneLinkageId()); | 426 | + Set<String> usedIds = new HashSet<>(); |
427 | + List<String> scenFilterIds = | ||
428 | + scenes.get().stream().map(i -> i.getId()).collect(Collectors.toList()); | ||
429 | + LambdaQueryWrapper<Trigger> triggerFilter = | ||
430 | + new QueryWrapper<Trigger>() | ||
431 | + .lambda() | ||
432 | + .in(scenFilterIds.size() > 0, Trigger::getSceneLinkageId, scenFilterIds) | ||
433 | + .like(Trigger::getEntityId, tbDeviceId); | ||
434 | + List<Trigger> triggers = triggerMapper.selectList(triggerFilter); | ||
435 | + triggers.forEach( | ||
436 | + item -> { | ||
437 | + usedIds.add(item.getSceneLinkageId()); | ||
419 | }); | 438 | }); |
420 | 439 | ||
421 | - LambdaQueryWrapper<DoCondition> conditionFilter = | ||
422 | - new QueryWrapper<DoCondition>() | ||
423 | - .lambda() | ||
424 | - .in(scenFilterIds.size()>0,DoCondition::getSceneLinkageId, scenFilterIds) | ||
425 | - .eq(DoCondition::getEntityId, tbDeviceId); | ||
426 | - List<DoCondition> doConditions = conditionMapper.selectList(conditionFilter); | ||
427 | - doConditions.forEach(item ->{ | ||
428 | - usedIds.add(item.getSceneLinkageId()); | 440 | + LambdaQueryWrapper<DoCondition> conditionFilter = |
441 | + new QueryWrapper<DoCondition>() | ||
442 | + .lambda() | ||
443 | + .in(scenFilterIds.size() > 0, DoCondition::getSceneLinkageId, scenFilterIds) | ||
444 | + .eq(DoCondition::getEntityId, tbDeviceId); | ||
445 | + List<DoCondition> doConditions = conditionMapper.selectList(conditionFilter); | ||
446 | + doConditions.forEach( | ||
447 | + item -> { | ||
448 | + usedIds.add(item.getSceneLinkageId()); | ||
429 | }); | 449 | }); |
430 | 450 | ||
431 | - LambdaQueryWrapper<DoAction> actionFilter = | ||
432 | - new QueryWrapper<DoAction>() | ||
433 | - .lambda() | ||
434 | - .in(scenFilterIds.size()>0,DoAction::getSceneLinkageId, scenFilterIds) | ||
435 | - .eq(DoAction::getDeviceId, tbDeviceId); | ||
436 | - List<DoAction> doActions = actionMapper.selectList(actionFilter); | ||
437 | - doActions.forEach(item ->{ | ||
438 | - usedIds.add(item.getSceneLinkageId()); | 451 | + LambdaQueryWrapper<DoAction> actionFilter = |
452 | + new QueryWrapper<DoAction>() | ||
453 | + .lambda() | ||
454 | + .in(scenFilterIds.size() > 0, DoAction::getSceneLinkageId, scenFilterIds) | ||
455 | + .eq(DoAction::getDeviceId, tbDeviceId); | ||
456 | + List<DoAction> doActions = actionMapper.selectList(actionFilter); | ||
457 | + doActions.forEach( | ||
458 | + item -> { | ||
459 | + usedIds.add(item.getSceneLinkageId()); | ||
439 | }); | 460 | }); |
440 | 461 | ||
441 | - if(usedIds.isEmpty()){ | ||
442 | - return ""; | ||
443 | - } | ||
444 | - StringBuilder result= new StringBuilder(); | ||
445 | - for(SceneLinkage item:scenes.get()){ | ||
446 | - if(usedIds.contains(item.getId())){ | ||
447 | - result.append(","+item.getName()); | ||
448 | - } | ||
449 | - } | ||
450 | - return String.format(ErrorMessage.DEVICE_USED_SCENE_REACT.getMessage(), result.substring(1)); | 462 | + if (usedIds.isEmpty()) { |
463 | + return ""; | ||
451 | } | 464 | } |
452 | - | ||
453 | - | ||
454 | - @Override | ||
455 | - public List<SelectItemDTO> findMasterDevices(String tenantId, String customerId, String organizationId) { | ||
456 | - List<String> orgIds = organizationAllIds(tenantId,organizationId); | ||
457 | - List<SelectItemDTO> result = baseMapper.masterDevices(customerId,tenantId,orgIds); | ||
458 | - return result; | ||
459 | - } | ||
460 | - | ||
461 | - @Override | ||
462 | - public List<SelectItemDTO> findSlaveDevices(String masterId,String tenantId,String customerId, String organizationId){ | ||
463 | - List<String> orgIds = organizationAllIds(tenantId,organizationId); | ||
464 | - List<SelectItemDTO> result = baseMapper.slaveDevices(customerId,tenantId,orgIds,masterId); | ||
465 | - return result; | ||
466 | - } | ||
467 | - | ||
468 | - @Override | ||
469 | - public List<String> findDeviceKeys(String tenantId, String customerId, String organizationId, List<String> deviceIds) { | ||
470 | - List<String> orgIds = organizationAllIds(tenantId,organizationId); | ||
471 | - List<String> result = baseMapper.findDeviceKeys(tenantId,customerId,orgIds,deviceIds); | ||
472 | - return result; | ||
473 | - } | ||
474 | - | ||
475 | - @Override | ||
476 | - public boolean saveSlaveDevice(String slaveId, String slaveName, String gatewayId, Long createTime) { | ||
477 | - LambdaQueryWrapper<YtDevice> deviceFilter = | ||
478 | - new QueryWrapper<YtDevice>() | ||
479 | - .lambda() | ||
480 | - .eq(YtDevice::getTbDeviceId, gatewayId); | ||
481 | - YtDevice gateway = baseMapper.selectOne(deviceFilter); | ||
482 | - YtDevice slaveDevice = new YtDevice(); | ||
483 | - slaveDevice.setName(slaveName); | ||
484 | - slaveDevice.setTbDeviceId(slaveId); | ||
485 | - slaveDevice.setSn(generateSn()); | ||
486 | - slaveDevice.setDeviceType(DeviceTypeEnum.SENSOR); | ||
487 | - slaveDevice.setCreateTime(LocalDateTime.now()); | ||
488 | - slaveDevice.setDeviceInfo(JacksonUtil.toJsonNode("{\"avatar\": \"\",\"longitude\": \"\",\"latitude\": \"\",\"address\": \"\"}")); | ||
489 | - | ||
490 | - slaveDevice.setProfileId(gateway.getProfileId()); | ||
491 | - slaveDevice.setGatewayId(gateway.getId()); | ||
492 | - slaveDevice.setOrganizationId(gateway.getOrganizationId()); | ||
493 | - slaveDevice.setTenantId(gateway.getTenantId()); | ||
494 | - slaveDevice.setCreator(gateway.getCreator()); | ||
495 | - | ||
496 | - baseMapper.insert(slaveDevice); | ||
497 | - return true; | ||
498 | - } | ||
499 | - | ||
500 | - @Override | ||
501 | - public List<DeviceDTO> findNumberAttributeDevicesByIds(String tenantId, List<String> ids) { | ||
502 | - return null; | 465 | + StringBuilder result = new StringBuilder(); |
466 | + for (SceneLinkage item : scenes.get()) { | ||
467 | + if (usedIds.contains(item.getId())) { | ||
468 | + result.append("," + item.getName()); | ||
469 | + } | ||
503 | } | 470 | } |
471 | + return String.format(ErrorMessage.DEVICE_USED_SCENE_REACT.getMessage(), result.substring(1)); | ||
472 | + } | ||
473 | + | ||
474 | + @Override | ||
475 | + public List<SelectItemDTO> findMasterDevices( | ||
476 | + String tenantId, String customerId, String organizationId) { | ||
477 | + List<String> orgIds = organizationAllIds(tenantId, organizationId); | ||
478 | + List<SelectItemDTO> result = baseMapper.masterDevices(customerId, tenantId, orgIds); | ||
479 | + return result; | ||
480 | + } | ||
481 | + | ||
482 | + @Override | ||
483 | + public List<SelectItemDTO> findSlaveDevices( | ||
484 | + String masterId, String tenantId, String customerId, String organizationId) { | ||
485 | + List<String> orgIds = organizationAllIds(tenantId, organizationId); | ||
486 | + List<SelectItemDTO> result = baseMapper.slaveDevices(customerId, tenantId, orgIds, masterId); | ||
487 | + return result; | ||
488 | + } | ||
489 | + | ||
490 | + @Override | ||
491 | + public List<String> findDeviceKeys( | ||
492 | + String tenantId, String customerId, String organizationId, List<String> deviceIds) { | ||
493 | + List<String> orgIds = organizationAllIds(tenantId, organizationId); | ||
494 | + List<String> result = baseMapper.findDeviceKeys(tenantId, customerId, orgIds, deviceIds); | ||
495 | + return result; | ||
496 | + } | ||
497 | + | ||
498 | + @Override | ||
499 | + public boolean saveSlaveDevice( | ||
500 | + String slaveId, String slaveName, String gatewayId, Long createTime) { | ||
501 | + LambdaQueryWrapper<YtDevice> deviceFilter = | ||
502 | + new QueryWrapper<YtDevice>().lambda().eq(YtDevice::getTbDeviceId, gatewayId); | ||
503 | + YtDevice gateway = baseMapper.selectOne(deviceFilter); | ||
504 | + YtDevice slaveDevice = new YtDevice(); | ||
505 | + slaveDevice.setName(slaveName); | ||
506 | + slaveDevice.setTbDeviceId(slaveId); | ||
507 | + slaveDevice.setSn(generateSn()); | ||
508 | + slaveDevice.setDeviceType(DeviceTypeEnum.SENSOR); | ||
509 | + slaveDevice.setCreateTime(LocalDateTime.now()); | ||
510 | + slaveDevice.setDeviceInfo( | ||
511 | + JacksonUtil.toJsonNode( | ||
512 | + "{\"avatar\": \"\",\"longitude\": \"\",\"latitude\": \"\",\"address\": \"\"}")); | ||
513 | + | ||
514 | + slaveDevice.setProfileId(gateway.getProfileId()); | ||
515 | + slaveDevice.setGatewayId(gateway.getId()); | ||
516 | + slaveDevice.setOrganizationId(gateway.getOrganizationId()); | ||
517 | + slaveDevice.setTenantId(gateway.getTenantId()); | ||
518 | + slaveDevice.setCreator(gateway.getCreator()); | ||
519 | + | ||
520 | + baseMapper.insert(slaveDevice); | ||
521 | + return true; | ||
522 | + } | ||
523 | + | ||
524 | + @Override | ||
525 | + public List<DeviceDTO> findNumberAttributeDevicesByIds(String tenantId, List<String> ids) { | ||
526 | + return null; | ||
527 | + } | ||
528 | + | ||
529 | + @Override | ||
530 | + public DeviceDTO getSubsetDeviceByTbDeviceId(String tenantId, String tbDeviceId) { | ||
531 | + YtDevice device = | ||
532 | + baseMapper.selectOne( | ||
533 | + new LambdaQueryWrapper<YtDevice>() | ||
534 | + .eq(YtDevice::getTbDeviceId, tbDeviceId) | ||
535 | + .eq(YtDevice::getTenantId, tenantId)); | ||
536 | + return Optional.ofNullable(device).map(obj -> obj.getDTO(DeviceDTO.class)).orElseThrow(()->{ | ||
537 | + throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); | ||
538 | + }); | ||
539 | + } | ||
504 | } | 540 | } |
@@ -151,4 +151,12 @@ public interface YtDeviceService extends BaseService<YtDevice> { | @@ -151,4 +151,12 @@ public interface YtDeviceService extends BaseService<YtDevice> { | ||
151 | */ | 151 | */ |
152 | List<DeviceDTO> findNumberAttributeDevicesByIds(String tenantId,List<String> ids); | 152 | List<DeviceDTO> findNumberAttributeDevicesByIds(String tenantId,List<String> ids); |
153 | 153 | ||
154 | + /** | ||
155 | + * 通过tb设备ID获取平台设备信息 | ||
156 | + * @param tenantId | ||
157 | + * @param tbDeviceId | ||
158 | + * @return | ||
159 | + */ | ||
160 | + DeviceDTO getSubsetDeviceByTbDeviceId(String tenantId,String tbDeviceId); | ||
161 | + | ||
154 | } | 162 | } |