From e2aa0a5b0c7b4f854b79c9120fc129b83ab150b6 Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Wed, 20 Sep 2023 13:10:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=20catalog=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=20proxy=20=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wvp/api/device/DeviceController.java | 4 +- gb28181-wvp-proxy-service/pom.xml | 6 +++ .../wvp/service/catalog/CatalogService.java | 21 ++++++++++ .../wvp/service/device/DeviceService.java | 39 +++++++++++++++++-- .../gb28181/Gb28181DownloadService.java | 16 ++++++++ .../gb28181/wvp/service/wvp/WvpService.java | 5 ++- .../request/MessageRequestProcessor.java | 4 +- 7 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/gb28181/Gb28181DownloadService.java diff --git a/gb28181-wvp-proxy-api/src/main/java/cn/skcks/docking/gb28181/wvp/api/device/DeviceController.java b/gb28181-wvp-proxy-api/src/main/java/cn/skcks/docking/gb28181/wvp/api/device/DeviceController.java index b2b0307..d239048 100644 --- a/gb28181-wvp-proxy-api/src/main/java/cn/skcks/docking/gb28181/wvp/api/device/DeviceController.java +++ b/gb28181-wvp-proxy-api/src/main/java/cn/skcks/docking/gb28181/wvp/api/device/DeviceController.java @@ -50,8 +50,8 @@ public class DeviceController { @Operation(summary = "根据设备编码(21位) 查询指定设备信息") @GetJson("/info/deviceCode") - public JsonResponse infoByDeviceCode(@RequestParam String deviceCode) { - WvpProxyDevice wvpProxyDevice = deviceService.getDeviceByDeviceCode(deviceCode).orElse(null); + public JsonResponse> infoByDeviceCode(@RequestParam String deviceCode) { + List wvpProxyDevice = deviceService.getDeviceByDeviceCode(deviceCode); return JsonResponse.success(wvpProxyDevice); } diff --git a/gb28181-wvp-proxy-service/pom.xml b/gb28181-wvp-proxy-service/pom.xml index 3af12fe..e71a98f 100644 --- a/gb28181-wvp-proxy-service/pom.xml +++ b/gb28181-wvp-proxy-service/pom.xml @@ -208,6 +208,12 @@ commons-exec 1.3 + + + org.apache.commons + commons-collections4 + 4.4 + diff --git a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/catalog/CatalogService.java b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/catalog/CatalogService.java index 2bec621..2c3a06d 100644 --- a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/catalog/CatalogService.java +++ b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/catalog/CatalogService.java @@ -6,7 +6,9 @@ import cn.skcks.docking.gb28181.core.sip.gb28181.constant.CmdType; import cn.skcks.docking.gb28181.core.sip.gb28181.constant.GB28181Constant; import cn.skcks.docking.gb28181.core.sip.message.subscribe.GenericSubscribe; import cn.skcks.docking.gb28181.core.sip.utils.SipUtil; +import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.model.WvpProxyDevice; import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.model.WvpProxyDocking; +import cn.skcks.docking.gb28181.wvp.service.device.DeviceService; import cn.skcks.docking.gb28181.wvp.service.docking.DockingService; import cn.skcks.docking.gb28181.wvp.sip.message.message.catalog.dto.CatalogItemDTO; import cn.skcks.docking.gb28181.wvp.sip.message.message.catalog.dto.CatalogRequestDTO; @@ -18,6 +20,7 @@ import gov.nist.javax.sip.message.SIPRequest; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.ListUtils; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -34,6 +37,8 @@ public class CatalogService { private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); private final DockingService dockingService; + private final DeviceService deviceService; + @SneakyThrows public CompletableFuture getCatalog(String deviceId){ CompletableFuture result = new CompletableFuture<>(); @@ -100,9 +105,25 @@ public class CatalogService { log.info("订阅结束 {}", key); if(dto != null){ dto.getDeviceList().setDeviceList(deviceList); + scheduledExecutorService.execute(()->{ + updateProxyDevice(dto); + }); } result.complete(dto); } }; } + + public void updateProxyDevice(CatalogResponseDTO dto){ + String gbDeviceId = dto.getDeviceId(); + List deviceByGbDeviceId = deviceService.getDeviceByGbDeviceId(gbDeviceId); + List existChannels = deviceByGbDeviceId.stream().map(WvpProxyDevice::getGbDeviceChannelId).toList(); + List deviceList = dto.getDeviceList().getDeviceList(); + List catalogChannels = deviceList.stream().map(CatalogItemDTO::getDeviceId).toList(); + List noexist = ListUtils.subtract(catalogChannels, existChannels); + noexist.forEach(channel->{ + log.info("更新 设备 {}, 通道 {} 信息", gbDeviceId, channel); + deviceService.autoUpdateDeviceByGbDeviceIdAndChannel(gbDeviceId,channel); + }); + } } diff --git a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/device/DeviceService.java b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/device/DeviceService.java index f9cda8d..a77027d 100644 --- a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/device/DeviceService.java +++ b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/device/DeviceService.java @@ -13,6 +13,7 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.text.MessageFormat; import java.util.List; @@ -31,8 +32,8 @@ public class DeviceService { s.where(WvpProxyDeviceDynamicSqlSupport.id, isEqualTo(id))); } - public Optional getDeviceByDeviceCode(String deviceCode){ - return deviceMapper.selectOne(s-> + public List getDeviceByDeviceCode(String deviceCode){ + return deviceMapper.select(s-> s.where(WvpProxyDeviceDynamicSqlSupport.deviceCode,isEqualTo(deviceCode))); } @@ -47,6 +48,32 @@ public class DeviceService { .and(WvpProxyDeviceDynamicSqlSupport.gbDeviceChannelId,isEqualTo(channel))); } + @Transactional + public boolean autoUpdateDeviceByGbDeviceIdAndChannel(String gbDeviceId, String channel){ + List deviceByGbDeviceId = getDeviceByGbDeviceId(gbDeviceId); + if(deviceByGbDeviceId.isEmpty()){ + WvpProxyDevice device = new WvpProxyDevice(); + device.setDeviceCode(""); + device.setGbDeviceId(gbDeviceId); + device.setGbDeviceChannelId(channel); + return deviceMapper.insert(device) > 0; + } + + Optional deviceByGbDeviceIdAndChannel = getDeviceByGbDeviceIdAndChannel(gbDeviceId,channel); + if(deviceByGbDeviceIdAndChannel.isPresent()) { + return deviceMapper.update(u-> + u.set(WvpProxyDeviceDynamicSqlSupport.gbDeviceChannelId).equalTo(channel) + .where(WvpProxyDeviceDynamicSqlSupport.gbDeviceChannelId, isEqualTo(gbDeviceId)) + .and(WvpProxyDeviceDynamicSqlSupport.gbDeviceChannelId, isEqualTo(channel))) > 0; + } else { + WvpProxyDevice device = new WvpProxyDevice(); + device.setDeviceCode(""); + device.setGbDeviceId(gbDeviceId); + device.setGbDeviceChannelId(channel); + return deviceMapper.insert(device) > 0; + } + } + /** * 添加设备 * @param device 设备 @@ -62,7 +89,7 @@ public class DeviceService { if(StringUtils.isBlank(deviceCode)){ throw new JsonException("设备编码不能为空"); } - if(getDeviceByDeviceCode(deviceCode).isPresent()){ + if(getDeviceByDeviceCode(deviceCode).isEmpty()){ throw new JsonException(MessageFormat.format("设备编码 {0} 已存在" ,deviceCode)); } @@ -81,6 +108,12 @@ public class DeviceService { return deviceMapper.insert(device) > 0; } + public boolean deleteDeviceByGbDeviceIdAndChannel(String gbDeviceId, String channel){ + return deviceMapper.delete(d-> + d.where(WvpProxyDeviceDynamicSqlSupport.gbDeviceChannelId, isEqualTo(gbDeviceId)) + .and(WvpProxyDeviceDynamicSqlSupport.gbDeviceChannelId, isEqualTo(channel))) > 0; + } + /** * 依据 id 或 deviceCode 或 gbDeviceId 删除设备信息 * @param device 设备 diff --git a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/gb28181/Gb28181DownloadService.java b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/gb28181/Gb28181DownloadService.java new file mode 100644 index 0000000..1ecfc0a --- /dev/null +++ b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/gb28181/Gb28181DownloadService.java @@ -0,0 +1,16 @@ +package cn.skcks.docking.gb28181.wvp.service.gb28181; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.util.Date; + +@Slf4j +@Service +@RequiredArgsConstructor +public class Gb28181DownloadService { + public void download(String deviceId, Date startTime, Date endTime){ + + } +} diff --git a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/wvp/WvpService.java b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/wvp/WvpService.java index 25c3406..6d88af9 100644 --- a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/wvp/WvpService.java +++ b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/wvp/WvpService.java @@ -71,11 +71,12 @@ public class WvpService { @SneakyThrows public void video(HttpServletRequest request, HttpServletResponse response, String deviceCode, Date startTime, Date endTime) { - WvpProxyDevice wvpProxyDevice = deviceService.getDeviceByDeviceCode(deviceCode).orElse(null); - if (wvpProxyDevice == null) { + List wvpProxyDeviceList = deviceService.getDeviceByDeviceCode(deviceCode); + if (wvpProxyDeviceList.isEmpty()) { writeErrorToResponse(response, JsonResponse.error("设备不存在")); return; } + WvpProxyDevice wvpProxyDevice = wvpProxyDeviceList.get(0); String deviceId = wvpProxyDevice.getGbDeviceId(); String channelId = wvpProxyDevice.getGbDeviceChannelId(); log.info("设备编码 (deviceCode=>{}) 查询到的设备信息 国标id(gbDeviceId => {}), 通道(channelId => {})", deviceCode, deviceId, channelId); diff --git a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/sip/message/message/processor/request/MessageRequestProcessor.java b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/sip/message/message/processor/request/MessageRequestProcessor.java index d843677..c975a16 100644 --- a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/sip/message/message/processor/request/MessageRequestProcessor.java +++ b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/sip/message/message/processor/request/MessageRequestProcessor.java @@ -19,6 +19,7 @@ import jakarta.annotation.PostConstruct; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import javax.sip.RequestEvent; @@ -64,7 +65,8 @@ public class MessageRequestProcessor implements MessageProcessor { Response ok = response(request, Response.OK, "OK"); Response response; - if(messageDto.getCmdType().equalsIgnoreCase(CmdType.KEEPALIVE)){ + + if(StringUtils.equalsAnyIgnoreCase(messageDto.getCmdType(), CmdType.KEEPALIVE)){ response = ok; // 更新设备在线状态 } else if(messageDto.getCmdType().equalsIgnoreCase(CmdType.CATALOG)){