|
|
@@ -1,5 +1,6 @@
|
|
|
package org.jetlinks.community.device.web;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
@@ -22,20 +23,19 @@ import org.hswebframework.web.exception.BusinessException;
|
|
|
import org.hswebframework.web.exception.NotFoundException;
|
|
|
import org.hswebframework.web.exception.ValidationException;
|
|
|
import org.hswebframework.web.id.IDGenerator;
|
|
|
+import org.jetlinks.community.device.configuration.GeoIndexProvider;
|
|
|
+import org.jetlinks.community.device.dto.DeviceGeoDto;
|
|
|
import org.jetlinks.community.device.entity.*;
|
|
|
-import org.jetlinks.community.device.entity.geo.GeoProperty;
|
|
|
import org.jetlinks.community.device.enums.DeviceState;
|
|
|
import org.jetlinks.community.device.response.DeviceDeployResult;
|
|
|
import org.jetlinks.community.device.response.DeviceDetail;
|
|
|
import org.jetlinks.community.device.response.ImportDeviceInstanceResult;
|
|
|
-import org.jetlinks.community.device.service.DeviceConfigMetadataManager;
|
|
|
-import org.jetlinks.community.device.service.DeviceTagsService;
|
|
|
-import org.jetlinks.community.device.service.LocalDeviceInstanceService;
|
|
|
-import org.jetlinks.community.device.service.LocalDeviceProductService;
|
|
|
+import org.jetlinks.community.device.service.*;
|
|
|
import org.jetlinks.community.device.service.data.DeviceDataService;
|
|
|
import org.jetlinks.community.device.web.excel.DeviceExcelInfo;
|
|
|
import org.jetlinks.community.device.web.excel.DeviceWrapper;
|
|
|
import org.jetlinks.community.device.web.request.AggRequest;
|
|
|
+import org.jetlinks.community.elastic.search.service.ElasticSearchService;
|
|
|
import org.jetlinks.community.io.excel.ImportExportService;
|
|
|
import org.jetlinks.community.io.utils.FileUtils;
|
|
|
import org.jetlinks.community.timeseries.query.AggregationData;
|
|
|
@@ -49,6 +49,7 @@ import org.jetlinks.core.message.Message;
|
|
|
import org.jetlinks.core.message.MessageType;
|
|
|
import org.jetlinks.core.message.RepayableDeviceMessage;
|
|
|
import org.jetlinks.core.metadata.*;
|
|
|
+import org.jetlinks.core.metadata.types.GeoPoint;
|
|
|
import org.springframework.core.io.buffer.DataBufferFactory;
|
|
|
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
|
@@ -97,7 +98,9 @@ public class DeviceInstanceController implements
|
|
|
|
|
|
private final DeviceTagsService tagsService;
|
|
|
|
|
|
- private final ReactiveRepository<GeoProperty, String> propertyRepository;
|
|
|
+ private final LocalDeviceInstanceService instanceService;
|
|
|
+
|
|
|
+ private final ElasticSearchService elasticSearchService;
|
|
|
@SuppressWarnings("all")
|
|
|
public DeviceInstanceController(LocalDeviceInstanceService service,
|
|
|
DeviceRegistry registry,
|
|
|
@@ -107,7 +110,8 @@ public class DeviceInstanceController implements
|
|
|
DeviceDataService deviceDataService,
|
|
|
DeviceConfigMetadataManager metadataManager,
|
|
|
DeviceTagsService tagsService,
|
|
|
- ReactiveRepository<GeoProperty, String> propertyRepository) {
|
|
|
+ LocalDeviceInstanceService instanceService,
|
|
|
+ ElasticSearchService elasticSearchService) {
|
|
|
this.service = service;
|
|
|
this.registry = registry;
|
|
|
this.productService = productService;
|
|
|
@@ -116,7 +120,8 @@ public class DeviceInstanceController implements
|
|
|
this.deviceDataService = deviceDataService;
|
|
|
this.metadataManager = metadataManager;
|
|
|
this.tagsService=tagsService;
|
|
|
- this.propertyRepository=propertyRepository;
|
|
|
+ this.instanceService=instanceService;
|
|
|
+ this.elasticSearchService=elasticSearchService;
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -331,20 +336,11 @@ public class DeviceInstanceController implements
|
|
|
@Operation(summary = "删除设备标签")
|
|
|
public Mono<Void> deleteDeviceTag(@PathVariable @Parameter(description = "设备ID") String deviceId,
|
|
|
@PathVariable @Parameter(description = "标签ID") String tagId) {
|
|
|
- return tagRepository.findById(tagId)
|
|
|
- .doOnNext(ignore->
|
|
|
- tagRepository.createDelete()
|
|
|
- .where(DeviceTagEntity::getDeviceId, deviceId)
|
|
|
- .and(DeviceTagEntity::getId, tagId)
|
|
|
- .execute()
|
|
|
- .subscribe()
|
|
|
- ).filter(tagsService::judgementGeo)
|
|
|
- .doOnNext(tag->
|
|
|
- propertyRepository
|
|
|
- .createDelete()
|
|
|
- .where(GeoProperty::getFromId,tagId)
|
|
|
- .execute()
|
|
|
- ).then();
|
|
|
+ return tagRepository.createDelete()
|
|
|
+ .where(DeviceTagEntity::getDeviceId, deviceId)
|
|
|
+ .and(DeviceTagEntity::getId, tagId)
|
|
|
+ .execute()
|
|
|
+ .then();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -423,18 +419,16 @@ public class DeviceInstanceController implements
|
|
|
//保存设备标签
|
|
|
@PatchMapping("/{deviceId}/tag")
|
|
|
@SaveAction
|
|
|
- @Operation(summary = "保存设备标签")
|
|
|
- public Flux<DeviceTagEntity> saveDeviceTag(@PathVariable @Parameter(description = "设备ID") String deviceId,
|
|
|
- @RequestBody Flux<DeviceTagEntity> tags) {
|
|
|
+ @Operation(summary = "保存设备标签(地理位置除外)")
|
|
|
+ public Flux<DeviceTagEntity> saveDeviceNormalTag(@PathVariable @Parameter(description = "设备ID") String deviceId,
|
|
|
+ @RequestBody Flux<DeviceTagEntity> tags) {
|
|
|
return tags
|
|
|
.doOnNext(tag -> {
|
|
|
tag.setId(DeviceTagEntity.createTagId(deviceId, tag.getKey()));
|
|
|
tag.setDeviceId(deviceId);
|
|
|
tag.tryValidate();
|
|
|
- tag.setAutoUpdate(true);
|
|
|
})
|
|
|
- .flatMap(tagsService::saveOrUpdate)
|
|
|
- .onErrorMap(DuplicateKeyException.class, err -> new BusinessException("标签不可重复添加", err))
|
|
|
+ .as(tagRepository::save)
|
|
|
.thenMany(getDeviceTags(deviceId));
|
|
|
}
|
|
|
|
|
|
@@ -839,4 +833,54 @@ public class DeviceInstanceController implements
|
|
|
}
|
|
|
|
|
|
|
|
|
+ //更新设备地理位置
|
|
|
+ @PatchMapping("/{deviceId}/geo")
|
|
|
+ @SaveAction
|
|
|
+ @Operation(summary = "更新设备地理位置信息")
|
|
|
+ public Mono<GeoPoint> patchGeo(@PathVariable @Parameter(description = "设备ID") String deviceId,
|
|
|
+ @RequestBody GeoPoint geoPoint) {
|
|
|
+ return instanceService.createUpdate()
|
|
|
+ .where(DeviceInstanceEntity::getId,deviceId)
|
|
|
+ .set(DeviceInstanceEntity::getCoordinate,geoPoint)
|
|
|
+ .execute()
|
|
|
+ .flatMap(ignore->
|
|
|
+ instanceService.findById(deviceId)
|
|
|
+ .map(DeviceGeoDto::of)
|
|
|
+ .flatMap(geo-> elasticSearchService.save(GeoIndexProvider.GEO.getIndex(),geo))
|
|
|
+ )
|
|
|
+ .thenReturn(geoPoint);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //获取设备地理位置
|
|
|
+ @GetMapping("/{deviceId}/geo")
|
|
|
+ @SaveAction
|
|
|
+ @Operation(summary = "获取设备地理位置信息")
|
|
|
+ public Mono<GeoPoint> getGeo(@PathVariable @Parameter(description = "设备ID") String deviceId) {
|
|
|
+ return instanceService.findById(deviceId).map(DeviceInstanceEntity::getCoordinate);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页查询设备信息
|
|
|
+ * @param query
|
|
|
+ * @param shapes
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/geo/_query/{bool}")
|
|
|
+ public Mono<PagerResult<DeviceInstanceEntity>> queryPagergeo(QueryParamEntity query,@PathVariable("bool") boolean s) {
|
|
|
+ if(s){
|
|
|
+// /**
|
|
|
+// * 没有查询区域
|
|
|
+// */
|
|
|
+ query.setIncludes(CollectionUtil.newHashSet("id","name","coordinate","productId","productName"));
|
|
|
+ return instanceService.queryPager(query);
|
|
|
+ }else {
|
|
|
+ /**
|
|
|
+ * 查询区域
|
|
|
+ * es查询
|
|
|
+ */
|
|
|
+ return elasticSearchService.queryPager(GeoIndexProvider.GEO,query,DeviceInstanceEntity.class);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|