diff --git a/maibu-common/src/main/java/com/maibu/core/business/uav/IotUAVDevice.java b/maibu-common/src/main/java/com/maibu/core/business/uav/IotUAVDevice.java index 9d45d9b..476abd0 100644 --- a/maibu-common/src/main/java/com/maibu/core/business/uav/IotUAVDevice.java +++ b/maibu-common/src/main/java/com/maibu/core/business/uav/IotUAVDevice.java @@ -37,4 +37,5 @@ public class IotUAVDevice extends BaseDO { @TableField(updateStrategy = FieldStrategy.IGNORED) private Long tenantId; //配置可以设置null 不自动过滤 + } diff --git a/maibu-common/src/main/java/com/maibu/memory/GlobalMemory.java b/maibu-common/src/main/java/com/maibu/memory/GlobalMemory.java index 34da76e..b7284ce 100644 --- a/maibu-common/src/main/java/com/maibu/memory/GlobalMemory.java +++ b/maibu-common/src/main/java/com/maibu/memory/GlobalMemory.java @@ -4,6 +4,7 @@ package com.maibu.memory; import com.maibu.core.business.Device; import com.maibu.core.business.IoTCommonDevice; import com.maibu.core.business.IoTCommonProduct; +import com.maibu.core.business.uav.IotUAVDevice; import lombok.Data; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; @@ -28,7 +29,6 @@ public class GlobalMemory { public static ConcurrentHashMap unRegisterDeviceMap = new ConcurrentHashMap<>(); - // key:orgId - siteId public static ConcurrentHashMap> orgAttrs = new ConcurrentHashMap<>(); diff --git a/maibu-external/src/main/java/com/maibu/uav/common/UAVConstant.java b/maibu-external/src/main/java/com/maibu/uav/common/UAVConstant.java new file mode 100644 index 0000000..2c1641d --- /dev/null +++ b/maibu-external/src/main/java/com/maibu/uav/common/UAVConstant.java @@ -0,0 +1,9 @@ +package com.maibu.uav.common; + +import io.netty.util.AttributeKey; + +public class UAVConstant { + + public static final String uavCameraKey = "uav_camera:"; + +} diff --git a/maibu-external/src/main/java/com/maibu/uav/controller/UAVController.java b/maibu-external/src/main/java/com/maibu/uav/controller/UAVController.java index ad211f9..a01ed11 100644 --- a/maibu-external/src/main/java/com/maibu/uav/controller/UAVController.java +++ b/maibu-external/src/main/java/com/maibu/uav/controller/UAVController.java @@ -41,7 +41,7 @@ public class UAVController extends BaseController { @GetMapping("/getUAVList") public TableDataInfo getUAVList(@RequestParam(required = false) Integer status, - @RequestParam(required = false) String sn) { + @RequestParam(required = false) String sn) { startPage(); Long roleId = getLoginUser().getRoleId(); Long orgId = getLoginUser().getOrgId(); @@ -57,9 +57,9 @@ public class UAVController extends BaseController { return uavService.getDeviceHMS(deviceSn); } - @GetMapping("/getDeviceState") - public String getDeviceState(@RequestParam String deviceSn) { - return uavService.getDeviceState(deviceSn); + @GetMapping("/getUAVState") + public AjaxResult getUAVState(@RequestParam String sn, @RequestParam String droneSn) { + return AjaxResult.success(uavService.getUAVState(sn, droneSn)); } @GetMapping("/getWayline") diff --git a/maibu-external/src/main/java/com/maibu/uav/scheduled/UAVScheduled.java b/maibu-external/src/main/java/com/maibu/uav/scheduled/UAVScheduled.java index cd9c5c8..f2e8c54 100644 --- a/maibu-external/src/main/java/com/maibu/uav/scheduled/UAVScheduled.java +++ b/maibu-external/src/main/java/com/maibu/uav/scheduled/UAVScheduled.java @@ -1,10 +1,13 @@ package com.maibu.uav.scheduled; import com.maibu.core.business.uav.IotUAVDevice; +import com.maibu.core.redis.RedisCache; import com.maibu.mapper.UAVDeviceMapper; +import com.maibu.uav.common.UAVConstant; import com.maibu.uav.dto.UavDeviceStateResponseDTO; import com.maibu.uav.dto.UavDeviceStateResponseDetailDTO; import com.maibu.uav.service.UAVService; +import com.maibu.utils.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @@ -14,6 +17,7 @@ import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @Component @@ -25,6 +29,9 @@ public class UAVScheduled { @Autowired private UAVDeviceMapper uavDeviceMapper; + @Autowired + private RedisCache redisCache; + //10分钟执行一次同步无人机设备 @Scheduled(cron = "0 */10 * * * ?") @@ -47,6 +54,17 @@ public class UAVScheduled { .map(UavDeviceStateResponseDetailDTO::getGateway_sn) .collect(Collectors.toSet()); +// detailList.forEach(y -> { +// if (!StringUtils.isEmpty(y.getGateway_sn())) { +// String snKey = UAVConstant.uavCameraKey + y.getGateway_sn(); +// redisCache.setCacheObject(snKey, y.getGateway_camera_list(), 10, TimeUnit.MINUTES); +// } +// if (!StringUtils.isEmpty(y.getDevice_sn())) { +// String droneKey = UAVConstant.uavCameraKey + y.getDevice_sn(); +// redisCache.setCacheObject(droneKey, y.getDrone_camera_list(), 10, TimeUnit.MINUTES); +// } +// }); + // ======================== // 1. 获取新增设备 // 远程有,数据库没有 diff --git a/maibu-external/src/main/java/com/maibu/uav/service/UAVService.java b/maibu-external/src/main/java/com/maibu/uav/service/UAVService.java index 267dd95..a718ce0 100644 --- a/maibu-external/src/main/java/com/maibu/uav/service/UAVService.java +++ b/maibu-external/src/main/java/com/maibu/uav/service/UAVService.java @@ -5,8 +5,10 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.maibu.core.business.uav.IotUAVDevice; import com.maibu.core.domain.entity.SysRole; +import com.maibu.core.redis.RedisCache; import com.maibu.mapper.UAVDeviceMapper; import com.maibu.service.ISysRoleService; +import com.maibu.uav.common.UAVConstant; import com.maibu.uav.dto.*; import com.maibu.uav.entity.UavDevice; import com.maibu.uav.entity.UavDeviceState; @@ -20,6 +22,7 @@ import io.minio.errors.*; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; @@ -51,6 +54,9 @@ public class UAVService { @Autowired private UAVDeviceMapper uavDeviceMapper; + @Autowired + private RedisCache redisCache; + public static final Map latestActiveTime = new ConcurrentHashMap<>(); @@ -75,9 +81,9 @@ public class UAVService { queryWrapper.eq(IotUAVDevice::getOlineStatus, 1); List list = uavDeviceMapper.selectList(queryWrapper); List res = transferData(list); - if (status!=null && !CollectionUtils.isEmpty(res)) { - return res.stream().filter(x->status.equals(x.getOnlineStatus())).collect(Collectors.toList()); - }else { + if (status != null && !CollectionUtils.isEmpty(res)) { + return res.stream().filter(x -> status.equals(x.getOnlineStatus())).collect(Collectors.toList()); + } else { return res; } } @@ -85,7 +91,7 @@ public class UAVService { return new ArrayList<>(); } - public List getUAVList(Long siteId, Long orgId, Long roleId, Long userId,Integer status, String sn) { + public List getUAVList(Long siteId, Long orgId, Long roleId, Long userId, Integer status, String sn) { SysRole sysRole = roleService.selectRoleById(roleId); if (sysRole != null) { String roleKey = sysRole.getRoleKey(); @@ -110,9 +116,9 @@ public class UAVService { // } list = uavDeviceMapper.selectList(queryWrapper); List res = transferData(list); - if (status!=null && !CollectionUtils.isEmpty(res)) { - return res.stream().filter(x->status.equals(x.getOnlineStatus())).collect(Collectors.toList()); - }else { + if (status != null && !CollectionUtils.isEmpty(res)) { + return res.stream().filter(x -> status.equals(x.getOnlineStatus())).collect(Collectors.toList()); + } else { return res; } } @@ -127,7 +133,7 @@ public class UAVService { IotUAVDevice::getSn, item -> item )); - UavDeviceStateResponseDTO dto = getProjectDeviceList(); + UavDeviceStateResponseDTO dto = getAllDeviceList(); List details = dto.getDetail(); if (!CollectionUtils.isEmpty(details)) { List fiList = details.stream().filter(x -> deviceMap.containsKey(x.getGateway_sn())).collect(Collectors.toList()); @@ -144,7 +150,99 @@ public class UAVService { return fiList; } } - return null; + return new ArrayList<>(); + } + + @Cacheable( + value = "uav_all_state", + key = "'all'", + sync = true, + unless = "#result == null" + ) + public UavDeviceStateResponseDTO getAllDeviceList() { + try { + String url = baseUrl + "/openapi/v0.1/project/device"; + String result = httpService.doGet(url, null); + SK2ResultDTO resultDTO = JsonUtils.parseObject(result, SK2ResultDTO.class); + Map map = (Map) resultDTO.getData(); + UavDeviceStateResponseDTO responseDTO = new UavDeviceStateResponseDTO(); + if (!CollectionUtils.isEmpty(map)) { + List> l = (List>) map.get("list"); + if (!CollectionUtils.isEmpty(l)) { + List detail = new ArrayList<>(); + responseDTO.setDetail(detail); + l.forEach(x -> { + // 方式1:如果droneObj是Map,用TypeReference反序列化(推荐) + UavProjectDeviceDTO up = JsonUtils.parseObject( + JsonUtils.toJsonString(x), // 先转JSON字符串 + new TypeReference() { + } + ); + UavDevice device = up.getDrone(); + UavDevice getway = up.getGateway(); + UavDeviceStateResponseDetailDTO dto = new UavDeviceStateResponseDetailDTO(); + if (getway != null) { + dto.setGateway_sn(getway.getSn()); + dto.setCallsign(getway.getCallsign()); + dto.setOnlineStatus(getway.getDevice_online_status() ? 1 : 0); + dto.setGateway_camera_list(getway.getCamera_list()); + if (device != null) { + dto.setDevice_sn(device.getSn()); + dto.setDrone_callsign(device.getCallsign()); + dto.setDrone_onlineStatus(device.getDevice_online_status() ? 1 : 0); + dto.setDrone_camera_list(device.getCamera_list()); + } + detail.add(dto); + } + }); + } + } + return responseDTO; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public List getCurrentState() { + try { + String url = baseUrl + "/openapi/v0.1/project/device"; + String result = httpService.doGet(url, null); + SK2ResultDTO resultDTO = JsonUtils.parseObject(result, SK2ResultDTO.class); + Map map = (Map) resultDTO.getData(); + List detail = new ArrayList<>(); + if (!CollectionUtils.isEmpty(map)) { + List> l = (List>) map.get("list"); + if (!CollectionUtils.isEmpty(l)) { + l.forEach(x -> { + // 方式1:如果droneObj是Map,用TypeReference反序列化(推荐) + UavProjectDeviceDTO up = JsonUtils.parseObject( + JsonUtils.toJsonString(x), // 先转JSON字符串 + new TypeReference() { + } + ); + UavDevice device = up.getDrone(); + UavDevice getway = up.getGateway(); + UavDeviceStateResponseDetailDTO dto = new UavDeviceStateResponseDetailDTO(); + if (getway != null) { + dto.setGateway_sn(getway.getSn()); + dto.setCallsign(getway.getCallsign()); + dto.setOnlineStatus(getway.getDevice_online_status() ? 1 : 0); + dto.setGateway_camera_list(getway.getCamera_list()); + if (device != null) { + dto.setDevice_sn(device.getSn()); + dto.setDrone_callsign(device.getCallsign()); + dto.setDrone_onlineStatus(device.getDevice_online_status() ? 1 : 0); + dto.setDrone_camera_list(device.getCamera_list()); + } + detail.add(dto); + } + }); + } + } + return detail; + } catch (Exception e) { + throw new RuntimeException(e); + } } @@ -261,6 +359,61 @@ public class UAVService { return httpService.doGet(url, null); } + + public UavDeviceStateResponseDetailDTO getUAVState(String deviceSn, String droneSn) { + List list = getCurrentState(); + UavDeviceStateResponseDetailDTO dto = null; + if(!CollectionUtils.isEmpty(list)){ + dto = list.stream().filter(x->x.getGateway_sn().equals(deviceSn)).findFirst().orElse(null); + if (!StringUtils.isEmpty(deviceSn)) { + String url = baseUrl + "/openapi/v0.1/device/" + deviceSn + "/state"; + String deviceData = httpService.doGet(url, null); + + SK2ResultDTO res = JsonUtils.parseObject(deviceData, SK2ResultDTO.class); + if (res != null && res.getData() != null) { + UavDeviceStateDTO stateDTO = JsonUtils.parseObject( + JsonUtils.toJsonString(res.getData()), // 先转JSON字符串 + new TypeReference() { + } + ); + if (stateDTO != null && stateDTO.getDevice_state() != null) { + UavDeviceState deviceState = stateDTO.getDevice_state(); + dto.setWind_speed(deviceState.getWind_speed()); + dto.setEnvironment_temperature(deviceState.getEnvironment_temperature()); + dto.setRainfall(deviceState.getRainfall()); +// dto.setLatitude(deviceState.getLatitude()); +// dto.setLongitude(deviceState.getLongitude()); + } + } + } + if (!StringUtils.isEmpty(droneSn)) { + String droneUrl = baseUrl + "/openapi/v0.1/device/" + droneSn + "/state"; + String droneData = httpService.doGet(droneUrl, null); + SK2ResultDTO res2 = JsonUtils.parseObject(droneData, SK2ResultDTO.class); + if (res2 != null && res2.getData() != null) { + UavDeviceStateDTO stateDTO = JsonUtils.parseObject( + JsonUtils.toJsonString(res2.getData()), // 先转JSON字符串 + new TypeReference() { + } + ); + if (stateDTO != null && stateDTO.getDevice_state() != null) { + UavDeviceState deviceState = stateDTO.getDevice_state(); + dto.setNetwork_state(deviceState.getNetwork_state()); + dto.setPosition_state(deviceState.getPosition_state()); + dto.setHome_distance(deviceState.getHome_distance()); + dto.setHeight(deviceState.getHeight()); + dto.setLatitude(deviceState.getLatitude()); + dto.setLongitude(deviceState.getLongitude()); + if (deviceState.getBattery() != null) { + dto.setCapacity_percent(deviceState.getBattery().getCapacity_percent()); + } + } + } + } + } + return dto; + } + public String getWayline() { String url = baseUrl + "/openapi/v0.1/wayline"; return httpService.doGet(url, null);