From 8a8612e236708871c8bf3d95661e24aee6f6e685 Mon Sep 17 00:00:00 2001 From: fajiao <1519100073@qq.com> Date: Tue, 1 Mar 2022 16:35:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E2=80=9C=E8=BD=AC=E5=9B=BE?= =?UTF-8?q?=E8=BD=ACBase64=E2=80=9D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JsonHelper.cs => Common/ConfigHelper.cs} | 6 +- BaseModule/EC.Helper/Common/FileHelper.cs | 12 +++ BaseModule/EC.Helper/Common/JsonHelper.cs | 73 ++++++++++++++++++ BaseModule/EC.Helper/EC.Helper.csproj | 1 + BaseModule/EC.Onvif/OnvifClient.cs | 10 +-- .../EC.App.ThatBLL/Onvif/IMediaBLL.cs | 6 +- .../EC.App.ThatBLL/Onvif/Impl/MediaBLL.cs | 38 ++++++++-- OnvifSocketServer/Manager/AppConfig.cs | 4 +- OnvifSocketServer/OnvifSocketServer.csproj | 1 + OnvifSocketServer/Program.cs | 74 +++++++++++-------- OnvifSocketServer/appsettings.json | 2 +- OnvifWebServer/.config/dotnet-tools.json | 12 +++ .../Controllers/Onvif/MediaController.cs | 20 ++++- 13 files changed, 206 insertions(+), 53 deletions(-) rename BaseModule/EC.Helper/{FileExt/JsonHelper.cs => Common/ConfigHelper.cs} (68%) create mode 100644 BaseModule/EC.Helper/Common/FileHelper.cs create mode 100644 BaseModule/EC.Helper/Common/JsonHelper.cs create mode 100644 OnvifWebServer/.config/dotnet-tools.json diff --git a/BaseModule/EC.Helper/FileExt/JsonHelper.cs b/BaseModule/EC.Helper/Common/ConfigHelper.cs similarity index 68% rename from BaseModule/EC.Helper/FileExt/JsonHelper.cs rename to BaseModule/EC.Helper/Common/ConfigHelper.cs index eee8ffe..c9ab59f 100644 --- a/BaseModule/EC.Helper/FileExt/JsonHelper.cs +++ b/BaseModule/EC.Helper/Common/ConfigHelper.cs @@ -1,14 +1,14 @@ using Microsoft.Extensions.Configuration; using System.IO; -namespace EC.Helper.FileExt +namespace EC.Helper.Common { - public class JsonHelper + public class ConfigHelper { public static IConfiguration ReadConfiguration(string configPath) { ConfigurationBuilder builder = new(); - if (!File.Exists(configPath)) { return builder.Build(); } + if (!File.Exists(configPath)) { return null; } builder.AddJsonFile(configPath); return builder.Build(); } diff --git a/BaseModule/EC.Helper/Common/FileHelper.cs b/BaseModule/EC.Helper/Common/FileHelper.cs new file mode 100644 index 0000000..ada184f --- /dev/null +++ b/BaseModule/EC.Helper/Common/FileHelper.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EC.Helper.Common +{ + public class FileHelper + { + } +} diff --git a/BaseModule/EC.Helper/Common/JsonHelper.cs b/BaseModule/EC.Helper/Common/JsonHelper.cs new file mode 100644 index 0000000..f2cd32a --- /dev/null +++ b/BaseModule/EC.Helper/Common/JsonHelper.cs @@ -0,0 +1,73 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System.Collections.Generic; + +namespace EC.Helper.Common +{ + public class JsonHelper + { + /// + /// Object To Json + /// + /// + /// + public static string ToJson(object obj) + { + return JsonConvert.SerializeObject(obj); + } + + /// + /// Json To Object + /// + /// + /// + public static object ToObject(string json) + { + return !string.IsNullOrEmpty(json) ? JsonConvert.DeserializeObject(json) : default; + } + + /// + /// Json To T Object + /// + /// + /// + /// + public static T ToObject(string json) where T : class + { + return !string.IsNullOrEmpty(json) ? JsonConvert.DeserializeObject(json) : default; + } + + /// + /// Json To JObject + /// + /// + /// + public static JObject ToJObject(string json) + { + return ToObject(json); + } + + /// + /// Json To Object List + /// + /// + /// + /// + public static List ToList(string json) where T : class + { + return ToObject>(json); + } + + /// + /// 深克隆 + /// + /// + /// + /// + public static T DeepClone(T obj) where T : class + { + string json = ToJson(obj); + return ToObject(json); + } + } +} \ No newline at end of file diff --git a/BaseModule/EC.Helper/EC.Helper.csproj b/BaseModule/EC.Helper/EC.Helper.csproj index 4b56119..05f976b 100644 --- a/BaseModule/EC.Helper/EC.Helper.csproj +++ b/BaseModule/EC.Helper/EC.Helper.csproj @@ -7,6 +7,7 @@ + diff --git a/BaseModule/EC.Onvif/OnvifClient.cs b/BaseModule/EC.Onvif/OnvifClient.cs index cb7b92c..2f64583 100644 --- a/BaseModule/EC.Onvif/OnvifClient.cs +++ b/BaseModule/EC.Onvif/OnvifClient.cs @@ -13,15 +13,15 @@ namespace EC.Onvif { #region Attr - private string Hostname { get; set; } + public string Hostname { get; private set; } - private string Username { get; set; } + public string Username { get; private set; } - private string Password { get; set; } + public string Password { get; private set; } - public static float atomDist { get; set; } = 0.01f; + public static float atomDist { get; private set; } = 0.01f; - public static float atomSpeed { get; set; } = 0.1f; + public static float atomSpeed { get; private set; } = 0.1f; #endregion Attr diff --git a/BusinessModule/EC.App.ThatBLL/Onvif/IMediaBLL.cs b/BusinessModule/EC.App.ThatBLL/Onvif/IMediaBLL.cs index 25169dc..a2d4a01 100644 --- a/BusinessModule/EC.App.ThatBLL/Onvif/IMediaBLL.cs +++ b/BusinessModule/EC.App.ThatBLL/Onvif/IMediaBLL.cs @@ -13,8 +13,10 @@ namespace EC.App.ThatBLL.Onvif /// bool IsConnected(string ip); - Task GetStreamUri(string ip); + Task GetStreamUrl(string ip); - Task GetSnapshotUri(string ip); + Task GetSnapshotUrl(string ip); + + Task GetSnapshot(string ip); } } \ No newline at end of file diff --git a/BusinessModule/EC.App.ThatBLL/Onvif/Impl/MediaBLL.cs b/BusinessModule/EC.App.ThatBLL/Onvif/Impl/MediaBLL.cs index 565f827..279c2f9 100644 --- a/BusinessModule/EC.App.ThatBLL/Onvif/Impl/MediaBLL.cs +++ b/BusinessModule/EC.App.ThatBLL/Onvif/Impl/MediaBLL.cs @@ -1,5 +1,8 @@ using EC.App.ThatService.Onvif.Impl; using EC.Onvif; +using System; +using System.IO; +using System.Net; using System.Threading.Tasks; namespace EC.App.ThatBLL.Onvif.Impl @@ -20,20 +23,43 @@ namespace EC.App.ThatBLL.Onvif.Impl return ret; } - public async Task GetStreamUri(string ip) + public async Task GetStreamUrl(string ip) { bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient); if (!flag) return string.Empty; - string uri = await onvifClient.GetStreamUri(); - return uri; + string url = await onvifClient.GetStreamUri(); + return url; } - public async Task GetSnapshotUri(string ip) + public async Task GetSnapshotUrl(string ip) { bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient); if (!flag) return string.Empty; - string uri = await onvifClient.GetSnapshotUri(); - return uri; + string url = await onvifClient.GetSnapshotUri(); + return url; + } + + public async Task GetSnapshot(string ip) + { + bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient); + if (!flag) return string.Empty; + string url = await onvifClient.GetSnapshotUri(); + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "GET"; + request.PreAuthenticate = true; + request.Credentials = new NetworkCredential(onvifClient.Username, onvifClient.Password); + using HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + using Stream stream = response.GetResponseStream(); + using MemoryStream mStream = new(); + byte[] buffer = new byte[1024]; + int byteCount; + do + { + byteCount = stream.Read(buffer, 0, buffer.Length); + mStream.Write(buffer, 0, byteCount); + } while (byteCount > 0); + mStream.Position = 0; + return Convert.ToBase64String(mStream.ToArray()); } } } \ No newline at end of file diff --git a/OnvifSocketServer/Manager/AppConfig.cs b/OnvifSocketServer/Manager/AppConfig.cs index 742fbad..0bf5690 100644 --- a/OnvifSocketServer/Manager/AppConfig.cs +++ b/OnvifSocketServer/Manager/AppConfig.cs @@ -1,4 +1,4 @@ -using EC.Helper.FileExt; +using EC.Helper.Common; using Microsoft.Extensions.Configuration; using System.IO; @@ -17,7 +17,7 @@ namespace OnvifSocketServer if (config == null) { string configPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json"); - config = JsonHelper.ReadConfiguration(configPath); + config = ConfigHelper.ReadConfiguration(configPath); } return config; } diff --git a/OnvifSocketServer/OnvifSocketServer.csproj b/OnvifSocketServer/OnvifSocketServer.csproj index 097a605..62c02d5 100644 --- a/OnvifSocketServer/OnvifSocketServer.csproj +++ b/OnvifSocketServer/OnvifSocketServer.csproj @@ -10,6 +10,7 @@ + diff --git a/OnvifSocketServer/Program.cs b/OnvifSocketServer/Program.cs index 516a28b..efeac79 100644 --- a/OnvifSocketServer/Program.cs +++ b/OnvifSocketServer/Program.cs @@ -1,7 +1,10 @@ -using Flurl; +using EC.App.Entity; +using Flurl; using Flurl.Http; using Newtonsoft.Json.Linq; using System; +using System.IO; +using System.Net; using System.Net.Http; namespace OnvifSocketServer @@ -21,58 +24,69 @@ namespace OnvifSocketServer string onvifHttpsUrl = AppConfig.GetOnvifHttpsUrl(); Uri baseUri = new Uri(onvifHttpUrl); JObject result; - Console.WriteLine(onvifHttpUrl); - // Get - result = baseUri.AbsoluteUri - .AppendPathSegment("onvif/Curd/IsExist") - .SetQueryParams(new { ip = "192.168.1.108" }) - .GetAsync() - .ReceiveJson().Result; - Console.WriteLine(result); + result = Request(baseUri, "onvif/Curd/IsExist", HttpMethod.Get, new { ip = "192.168.1.65" }); + result = Request(baseUri, "onvif/Curd/Add", HttpMethod.Post, + new { ip = "192.168.1.65", username = "admin", password = "hk123456" }); + result = Request(baseUri, "onvif/Media/GetStreamUri", HttpMethod.Get, new { ip = "192.168.1.65" }); + result = Request(baseUri, "onvif/Media/GetSnapshotUri", HttpMethod.Get, new { ip = "192.168.1.65" }); + DownloadSnapShot(result); - // Post - result = baseUri.AbsoluteUri - .AppendPathSegment("onvif/Curd/Add") - .SetQueryParams(new { ip = "192.168.1.65", username = "admin", password = "hk123456" }) - .PostAsync() - .ReceiveJson().Result; - Console.WriteLine(result); + // 192.168.1.108 + //result = Request(baseUri, "onvif/Curd/Add", HttpMethod.Post, + // new { ip = "192.168.1.108", username = "admin", password = "hk123456" }); + //result = Request(baseUri, "onvif/Media/GetStreamUri", HttpMethod.Get, new { ip = "192.168.1.108" }); + //result = Request(baseUri, "onvif/Media/GetSnapshotUri", HttpMethod.Get, new { ip = "192.168.1.108" }); + } - result = Request(baseUri, "onvif/Media/GetStreamUri", HttpMethod.Get, new { ip = "192.168.1.65" }); - result = Request(baseUri, "onvif/Media/GetSnapshotUri", HttpMethod.Get, new { ip = "192.168.1.65" }); + private static void DownloadSnapShot(JObject jObj) + { + RespParam result = jObj.ToObject>(); + string snapshotUrl = result.Data.ToString(); + if (string.IsNullOrEmpty(snapshotUrl)) return; - // Post - result = Request(baseUri, "onvif/Curd/Add", HttpMethod.Post, - new { ip = "192.168.1.108", username = "admin", password = "hk123456" }); + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(snapshotUrl); + request.Method = "GET"; + request.PreAuthenticate = true; + request.Credentials = new NetworkCredential("admin", "hk123456"); - result = Request(baseUri, "onvif/Media/GetStreamUri", HttpMethod.Get, new { ip = "192.168.1.108" }); - result = Request(baseUri, "onvif/Media/GetSnapshotUri", HttpMethod.Get, new { ip = "192.168.1.108" }); + using HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + using Stream stream = response.GetResponseStream(); + using MemoryStream mStream = new(); + byte[] buffer = new byte[1024]; + int byteCount; + do + { + byteCount = stream.Read(buffer, 0, buffer.Length); + mStream.Write(buffer, 0, byteCount); + } while (byteCount > 0); + mStream.Position = 0; + string base64Str = Convert.ToBase64String(mStream.ToArray()); } - public static JObject Request(Uri uri, string relativePath, HttpMethod httpMethod, object data) + public static T Request(Uri uri, string relativePath, HttpMethod httpMethod, object data) { Url url = uri.AbsoluteUri.AppendPathSegment(relativePath).SetQueryParams(data); - JObject result; + T result; if (httpMethod == HttpMethod.Get) { - result = url.GetAsync().ReceiveJson().Result; + result = url.GetAsync().ReceiveJson().Result; } else if (httpMethod == HttpMethod.Post) { - result = url.PostAsync().ReceiveJson().Result; + result = url.PostAsync().ReceiveJson().Result; } else if (httpMethod == HttpMethod.Put) { - result = url.PutAsync().ReceiveJson().Result; ; + result = url.PutAsync().ReceiveJson().Result; ; } else if (httpMethod == HttpMethod.Delete) { - result = url.DeleteAsync().ReceiveJson().Result; ; + result = url.DeleteAsync().ReceiveJson().Result; ; } else { - result = null; + result = default; } Console.WriteLine(result); return result; diff --git a/OnvifSocketServer/appsettings.json b/OnvifSocketServer/appsettings.json index f6379aa..5da5aec 100644 --- a/OnvifSocketServer/appsettings.json +++ b/OnvifSocketServer/appsettings.json @@ -1,6 +1,6 @@ { "Apis": { - "onvif_http_url": "http://localhost:6000", + "onvif_http_url": "http://192.168.1.119:10011", "onvif_https_url": "https://localhost:6001" } } \ No newline at end of file diff --git a/OnvifWebServer/.config/dotnet-tools.json b/OnvifWebServer/.config/dotnet-tools.json new file mode 100644 index 0000000..82e7e22 --- /dev/null +++ b/OnvifWebServer/.config/dotnet-tools.json @@ -0,0 +1,12 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-ef": { + "version": "6.0.2", + "commands": [ + "dotnet-ef" + ] + } + } +} \ No newline at end of file diff --git a/OnvifWebServer/Controllers/Onvif/MediaController.cs b/OnvifWebServer/Controllers/Onvif/MediaController.cs index 755b2a2..4e32bb1 100644 --- a/OnvifWebServer/Controllers/Onvif/MediaController.cs +++ b/OnvifWebServer/Controllers/Onvif/MediaController.cs @@ -48,8 +48,8 @@ namespace OnvifWebServer.Controllers.Onvif public async Task GetStreamUri( [Required][DataValidation(ValidationTypes.IPv4)] string ip) { - string uri = await _mediaBLL.GetStreamUri(ip); - return uri; + string url = await _mediaBLL.GetStreamUrl(ip); + return url; } /// @@ -61,8 +61,20 @@ namespace OnvifWebServer.Controllers.Onvif public async Task GetSnapshotUri( [Required][DataValidation(ValidationTypes.IPv4)] string ip) { - string uri = await _mediaBLL.GetSnapshotUri(ip); - return uri; + string url = await _mediaBLL.GetSnapshotUrl(ip); + return url; + } + + /// + /// 获取抓图 Base64 + /// + /// + /// + public async Task GetSnapshot( + [Required][DataValidation(ValidationTypes.IPv4)] string ip) + { + string snapshotBase64 = await _mediaBLL.GetSnapshot(ip); + return snapshotBase64; } } } \ No newline at end of file