From 50452136749414c0c3381bd4298f1ffa91089f89 Mon Sep 17 00:00:00 2001 From: wangliwen Date: Tue, 21 Jun 2022 11:55:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91=E9=A2=84=E8=A7=88=20?= =?UTF-8?q?=E5=B7=A5=E5=8E=82=E6=A8=A1=E5=BC=8F=E4=B8=8B=E5=90=84=E5=8C=BA?= =?UTF-8?q?=E5=9F=9F=E7=9A=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/domain/Tsingtao_xhaProperties.java | 3 +- .../resource/service/ResourceService.java | 2 + .../service/impl/ResourceServiceImpl.java | 8 ++ .../AbstractVideoPreviewService.java | 16 ++++ .../videoPreview/VideoPreviewFactory.java | 64 ++++++++++++++++ .../domain/BaoTouVideoPreviewService.java | 51 +++++++++++++ .../domain/TsingtaoVideoPreviewService.java | 37 +++++++++ .../TsingtaoXHAVideoPreviewService.java | 76 +++++++++++++++++++ .../resources/domain/tsingtao-xha.properties | 2 + .../io/renren/common/constant/Constant.java | 13 +++- 10 files changed, 270 insertions(+), 2 deletions(-) create mode 100644 renren-admin/src/main/java/io/renren/modules/resource/videoPreview/AbstractVideoPreviewService.java create mode 100644 renren-admin/src/main/java/io/renren/modules/resource/videoPreview/VideoPreviewFactory.java create mode 100644 renren-admin/src/main/java/io/renren/modules/resource/videoPreview/domain/BaoTouVideoPreviewService.java create mode 100644 renren-admin/src/main/java/io/renren/modules/resource/videoPreview/domain/TsingtaoVideoPreviewService.java create mode 100644 renren-admin/src/main/java/io/renren/modules/resource/videoPreview/domain/TsingtaoXHAVideoPreviewService.java diff --git a/renren-admin/src/main/java/io/renren/common/domain/Tsingtao_xhaProperties.java b/renren-admin/src/main/java/io/renren/common/domain/Tsingtao_xhaProperties.java index c435f79b..f816620a 100644 --- a/renren-admin/src/main/java/io/renren/common/domain/Tsingtao_xhaProperties.java +++ b/renren-admin/src/main/java/io/renren/common/domain/Tsingtao_xhaProperties.java @@ -14,5 +14,6 @@ import org.springframework.stereotype.Component; @PropertySource("classpath:/domain/tsingtao-xha.properties") @ConfigurationProperties(prefix = "tsingtao-xha") public class Tsingtao_xhaProperties { - + private String cloudhls; + private String localhls; } diff --git a/renren-admin/src/main/java/io/renren/modules/resource/service/ResourceService.java b/renren-admin/src/main/java/io/renren/modules/resource/service/ResourceService.java index 45888fe0..979c40d5 100644 --- a/renren-admin/src/main/java/io/renren/modules/resource/service/ResourceService.java +++ b/renren-admin/src/main/java/io/renren/modules/resource/service/ResourceService.java @@ -81,4 +81,6 @@ public interface ResourceService extends CrudService(); } } + + @Override + public Integer getProjectPlace() { + return this.projectPlace; + } } \ No newline at end of file diff --git a/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/AbstractVideoPreviewService.java b/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/AbstractVideoPreviewService.java new file mode 100644 index 00000000..79f94a8a --- /dev/null +++ b/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/AbstractVideoPreviewService.java @@ -0,0 +1,16 @@ +package io.renren.modules.resource.videoPreview; + + +/** + * 视频预览抽象类 + */ +public abstract class AbstractVideoPreviewService { + + /** + * 获取hls预览地址 + * + * @param key 获取hls地址的键 + * @return + */ + public abstract String getHls(String key); +} diff --git a/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/VideoPreviewFactory.java b/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/VideoPreviewFactory.java new file mode 100644 index 00000000..bd07edfa --- /dev/null +++ b/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/VideoPreviewFactory.java @@ -0,0 +1,64 @@ +package io.renren.modules.resource.videoPreview; + + +import io.renren.common.constant.Constant; +import io.renren.common.domain.BaoTouProperties; +import io.renren.common.domain.TsingtaoProperties; +import io.renren.common.domain.Tsingtao_xhaProperties; +import io.renren.common.utils.SpringContextUtils; +import io.renren.modules.monitor.service.MonitorService; +import io.renren.modules.resource.service.ResourceService; +import io.renren.modules.resource.videoPreview.domain.BaoTouVideoPreviewService; +import io.renren.modules.resource.videoPreview.domain.TsingtaoVideoPreviewService; + +import java.util.Optional; + +/** + * 视频预览工厂类 + */ +public final class VideoPreviewFactory { + private static ResourceService resourceService; + private static int projectPlace; + + private static BaoTouProperties baoTouProperties; // 包头配置 + + private static MonitorService monitorService; + + private static TsingtaoProperties tsingtaoProperties; // 青岛市区配置 + + private static Tsingtao_xhaProperties tsingtao_xhaProperties; // 青岛西海岸配置 + + static { + VideoPreviewFactory.resourceService = SpringContextUtils.getBean(ResourceService.class); + VideoPreviewFactory.projectPlace = resourceService.getProjectPlace(); + VideoPreviewFactory.baoTouProperties = SpringContextUtils.getBean(BaoTouProperties.class); + VideoPreviewFactory.monitorService = SpringContextUtils.getBean(MonitorService.class); + } + + /** + * 获取预览服务实现 + * + * @return + */ + public static Optional build() { + AbstractVideoPreviewService abstractVideoPreviewService = null; + switch (Constant.ProjectPlace.getByFlag(projectPlace)) { + case TSINGTAO: { // 青岛市局 + abstractVideoPreviewService = new TsingtaoVideoPreviewService(monitorService); + } + break; + case TSINGTAO_XHA: { // 青岛西海岸 + + } + break; + case BAOTOU: { // 包头 + abstractVideoPreviewService = new BaoTouVideoPreviewService(baoTouProperties); + } + case UNKNOWN: { // 未知区域 + + } + break; + } + return Optional.ofNullable(abstractVideoPreviewService); + } +} diff --git a/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/domain/BaoTouVideoPreviewService.java b/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/domain/BaoTouVideoPreviewService.java new file mode 100644 index 00000000..b76882b7 --- /dev/null +++ b/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/domain/BaoTouVideoPreviewService.java @@ -0,0 +1,51 @@ +package io.renren.modules.resource.videoPreview.domain; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import io.renren.common.domain.BaoTouProperties; +import io.renren.modules.resource.videoPreview.AbstractVideoPreviewService; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * 包头预览 + */ +public class BaoTouVideoPreviewService extends AbstractVideoPreviewService { + private static Logger logger = LoggerFactory.getLogger(BaoTouVideoPreviewService.class); + private static final OkHttpClient client = new OkHttpClient(); + + private BaoTouProperties baoTouProperties; + + public BaoTouVideoPreviewService(BaoTouProperties baoTouProperties) { + this.baoTouProperties = baoTouProperties; + } + + /** + * 获取hls预览地址 + * + * @param key 获取hls地址的键 + * @return + */ + @Override + public String getHls(String key) { + String hls = null; + Request request = new Request.Builder().url(String.format(baoTouProperties.getHlsurl(), key)).build(); + try (Response response = client.newCall(request).execute()) { + if (response.isSuccessful()) { + JSONObject jsonObject = JSON.parseObject(response.body().string()); + if (jsonObject.containsKey("data")) { + hls = jsonObject.getString("data"); + } + } else { + logger.error("包头预览地址获取失败"); + } + } catch (Exception exception) { + logger.error("包头预览地址获取失败", exception); + } + return hls; + } +} diff --git a/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/domain/TsingtaoVideoPreviewService.java b/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/domain/TsingtaoVideoPreviewService.java new file mode 100644 index 00000000..516b4aae --- /dev/null +++ b/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/domain/TsingtaoVideoPreviewService.java @@ -0,0 +1,37 @@ +package io.renren.modules.resource.videoPreview.domain; + +import io.renren.modules.monitor.service.MonitorService; +import io.renren.modules.resource.videoPreview.AbstractVideoPreviewService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +/** + * 青岛市局预览 + */ +public class TsingtaoVideoPreviewService extends AbstractVideoPreviewService { + private static Logger logger = LoggerFactory.getLogger(BaoTouVideoPreviewService.class); + + private MonitorService monitorService; + + public TsingtaoVideoPreviewService(MonitorService monitorService) { + this.monitorService = monitorService; + } + + /** + * 获取hls预览地址 + * + * @param key 获取hls地址的键 + * @return + */ + @Override + public String getHls(String key) { + try { + return monitorService.fileCode(key); + } catch (IOException e) { + logger.error("青岛市局获取预览地址失败", e); + return null; + } + } +} diff --git a/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/domain/TsingtaoXHAVideoPreviewService.java b/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/domain/TsingtaoXHAVideoPreviewService.java new file mode 100644 index 00000000..41441d00 --- /dev/null +++ b/renren-admin/src/main/java/io/renren/modules/resource/videoPreview/domain/TsingtaoXHAVideoPreviewService.java @@ -0,0 +1,76 @@ +package io.renren.modules.resource.videoPreview.domain; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import io.renren.common.domain.Tsingtao_xhaProperties; +import io.renren.modules.resource.videoPreview.AbstractVideoPreviewService; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CopyOnWriteArrayList; + + +/** + * 青岛西海岸 + */ +public class TsingtaoXHAVideoPreviewService extends AbstractVideoPreviewService { + private static Logger logger = LoggerFactory.getLogger(TsingtaoXHAVideoPreviewService.class); + Tsingtao_xhaProperties tsingtao_xhaProperties; + + public TsingtaoXHAVideoPreviewService(Tsingtao_xhaProperties tsingtao_xhaProperties) { + this.tsingtao_xhaProperties = tsingtao_xhaProperties; + } + + /** + * 获取hls预览地址 + * + * @param key 获取hls地址的键 + * @return + */ + @Override + public String getHls(String key) { + List result = new CopyOnWriteArrayList<>(); + CompletableFuture cloud = + CompletableFuture.runAsync(() -> { // 云脑专网 + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder().url(String.format(tsingtao_xhaProperties.getCloudhls(), key)).build(); + try (Response response = client.newCall(request).execute()) { + if (response.isSuccessful()) { + JSONObject jsonObject = JSON.parseObject(response.body().string()); + if (jsonObject.containsKey("data")) { + result.add(jsonObject.getString("data")); + } + } else { + logger.error("包头预览地址获取失败"); + } + } catch (Exception exception) { + logger.error("包头预览地址获取失败", exception); + } + }); + CompletableFuture local = + CompletableFuture.runAsync(() -> { // 金宏网 + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder().url(String.format(tsingtao_xhaProperties.getLocalhls(), key)).build(); + try (Response response = client.newCall(request).execute()) { + if (response.isSuccessful()) { + JSONObject jsonObject = JSON.parseObject(response.body().string()); + if (jsonObject.containsKey("data")) { + result.add(jsonObject.getString("data")); + } + } else { + logger.error("包头预览地址获取失败"); + } + } catch (Exception exception) { + logger.error("包头预览地址获取失败", exception); + } + }); + CompletableFuture.allOf(cloud, local); + return result.stream().filter(index -> StringUtils.isNotEmpty(index)).findAny().orElse(null); + } +} diff --git a/renren-admin/src/main/resources/domain/tsingtao-xha.properties b/renren-admin/src/main/resources/domain/tsingtao-xha.properties index e69de29b..b8ee1e93 100644 --- a/renren-admin/src/main/resources/domain/tsingtao-xha.properties +++ b/renren-admin/src/main/resources/domain/tsingtao-xha.properties @@ -0,0 +1,2 @@ +tsingtao-xha.cloudhls=http://10.10.30.9:8001/hx-weatherwarning/camera/getCameraLiveStreamByCode?cameraCode=%s&protocol=hls +tsingtao-xha.localhls=http://10.134.135.9:8001/hx-weatherwarning/camera/getCameraLiveStreamByCode?cameraCode=%s&protocol=hls \ No newline at end of file diff --git a/renren-common/src/main/java/io/renren/common/constant/Constant.java b/renren-common/src/main/java/io/renren/common/constant/Constant.java index eb0c1beb..1f306bc2 100644 --- a/renren-common/src/main/java/io/renren/common/constant/Constant.java +++ b/renren-common/src/main/java/io/renren/common/constant/Constant.java @@ -1,5 +1,7 @@ package io.renren.common.constant; +import java.util.Arrays; + /** * 常量 */ @@ -228,7 +230,11 @@ public interface Constant { /** * 包头 */ - BAOTOU(2); + BAOTOU(2), + /** + * 未知 + */ + UNKNOWN(Integer.MAX_VALUE); private int value; ProjectPlace(int value) { @@ -238,5 +244,10 @@ public interface Constant { public int getValue() { return value; } + + public static ProjectPlace getByFlag(int flag) { + ProjectPlace[] index = ProjectPlace.values(); + return Arrays.asList(index).stream().filter(index_ -> index_.value == flag).findAny().orElse(ProjectPlace.UNKNOWN); + } } } \ No newline at end of file