diff --git a/HxServer/HxServer/HxServer.csproj b/HxServer/HxServer/HxServer.csproj index c3801f5..c65ee0f 100644 --- a/HxServer/HxServer/HxServer.csproj +++ b/HxServer/HxServer/HxServer.csproj @@ -13,8 +13,6 @@ - - diff --git a/HxServer/HxServer/ReadMe.cs b/HxServer/HxServer/ReadMe.cs index ddec39b..fbd005a 100644 --- a/HxServer/HxServer/ReadMe.cs +++ b/HxServer/HxServer/ReadMe.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.Linq; +using System.Diagnostics.CodeAnalysis; using System.Text; -using System.Threading.Tasks; namespace HxServer; @@ -14,14 +10,14 @@ public class ReadMe { public static string AppName { get => "HxServer"; } - public static string VersionString { get => $"{AppName} V{Version.First().Key.Split(' ')[0]} ({Version.First().Key.Split(' ')[3]})"; } + public static string VersionString => $"{AppName} V{Version.First().Key.Split(' ')[0]} ({Version.First().Key.Split(' ')[3]})"; - static Dictionary> Version = new Dictionary>() + static readonly Dictionary> Version = new() { - ["1.00 Alpha Bulid 20240111"] = new List() - { + ["1.00 Alpha Bulid 20240111"] = + [ "版本名称格式变换", - }, + ], }; [RequiresAssemblyFiles()] @@ -33,9 +29,9 @@ public class ReadMe using (FileStream fr = new(Path.Combine(path, "ReadMe.md"), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) { - using (StreamWriter sw = new StreamWriter(fr, Encoding.UTF8)) + using (StreamWriter sw = new(fr, Encoding.UTF8)) { - StringBuilder sb = new StringBuilder(); + StringBuilder sb = new(); sb.AppendLine($"# {AppName}").AppendLine(); foreach (var item in Version) { diff --git a/HxServer/HxServer/Utils/DebugTrace.cs b/HxServer/HxServer/Utils/DebugTrace.cs index 498f0dc..403a785 100644 --- a/HxServer/HxServer/Utils/DebugTrace.cs +++ b/HxServer/HxServer/Utils/DebugTrace.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HxServer.Utils +namespace HxServer.Utils { public class DebugTrace { diff --git a/HxServer/HxServer/Utils/FileUtils.cs b/HxServer/HxServer/Utils/FileUtils.cs index d903610..fb7aeb8 100644 --- a/HxServer/HxServer/Utils/FileUtils.cs +++ b/HxServer/HxServer/Utils/FileUtils.cs @@ -1,48 +1,49 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Text; namespace HxServer.Utils; public class FileUtils { public static long Size(string fullpath) { try { return new FileInfo(fullpath).Length; } catch { return 0; } } + public static string GetName(string fullpath) { try { return new FileInfo(fullpath).Name; } catch { return ""; } } + public static string ReadString(string fullpath) { try { return File.ReadAllText(fullpath); } catch { return ""; } } + public static byte[] ReadBytes(string fullpath) { try { return File.ReadAllBytes(fullpath); } catch { return null; } } public static void Append(string fullpath, string contents) { try { - Directory.CreateDirectory(fullpath.Substring(0, fullpath.LastIndexOf("/"))); + Directory.CreateDirectory(fullpath[..fullpath.LastIndexOf('/')]); - using FileStream fr = new FileStream(fullpath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); - using StreamWriter sw = new StreamWriter(fr, Encoding.UTF8); + using FileStream fr = new(fullpath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); + using StreamWriter sw = new(fr, Encoding.UTF8); sw.WriteLine(contents); } catch { } } + public static void Append(string fullpath, byte[] contents) { try { - Directory.CreateDirectory(fullpath.Substring(0, fullpath.LastIndexOf("/"))); + Directory.CreateDirectory(fullpath[..fullpath.LastIndexOf('/')]); - using FileStream fr = new FileStream(fullpath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); + using FileStream fr = new(fullpath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); fr.Write(contents, 0, contents.Length); } catch { } } + public static void Append(string fullpath, byte[] contents, int offset, int count) { try { - Directory.CreateDirectory(fullpath.Substring(0, fullpath.LastIndexOf("/"))); + Directory.CreateDirectory(fullpath[..fullpath.LastIndexOf('/')]); - using FileStream fr = new FileStream(fullpath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); + using FileStream fr = new(fullpath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); fr.Write(contents, offset, count); } catch { } @@ -52,23 +53,22 @@ public class FileUtils { try { - Directory.CreateDirectory(fullpath.Substring(0, fullpath.LastIndexOf("/"))); + Directory.CreateDirectory(fullpath[..fullpath.LastIndexOf('/')]); - using FileStream fr = new FileStream(fullpath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); - using StreamWriter sw = new StreamWriter(fr, Encoding.UTF8); + using FileStream fr = new(fullpath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); + using StreamWriter sw = new(fr, Encoding.UTF8); sw.WriteLine(contents); } catch { } } - public static void WriteFile(string fullpath, byte[] contents) { try { - Directory.CreateDirectory(fullpath.Substring(0, fullpath.LastIndexOf("/"))); + Directory.CreateDirectory(fullpath[..fullpath.LastIndexOf('/')]); - using FileStream fr = new FileStream(fullpath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); + using FileStream fr = new(fullpath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); fr.Write(contents, 0, contents.Length); } catch { } diff --git a/HxServer/HxServer/Utils/HttpUtils.cs b/HxServer/HxServer/Utils/HttpUtils.cs index b7ec044..a3e954a 100644 --- a/HxServer/HxServer/Utils/HttpUtils.cs +++ b/HxServer/HxServer/Utils/HttpUtils.cs @@ -1,7 +1,4 @@ -using System; -using System.Net; -using System.Net.Http; -using System.Text; +using System.Net; namespace HxServer.Utils; @@ -57,7 +54,7 @@ public class HttpUtils /* 设置返回数据为JSON类 */ Context.Response.AddHeader("Content-Type", "application/json"); - System.Threading.Tasks.Task.Factory.StartNew(() => ListenCallBack?.Invoke(null, Context)); + Task.Factory.StartNew(() => ListenCallBack?.Invoke(null, Context)); } /// @@ -71,7 +68,7 @@ public class HttpUtils { result = string.Empty; - using var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(seconds) }; + using HttpClient client = new() { Timeout = TimeSpan.FromSeconds(seconds) }; var response = client.GetAsync(url).Result; if (response.IsSuccessStatusCode) { @@ -97,8 +94,8 @@ public class HttpUtils HttpContent content = new StringContent(data.ToString()); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/josn"); - using HttpClient client = new(); - HttpResponseMessage res = client.PostAsync(url, content).Result; + using HttpClient client = new() { Timeout = TimeSpan.FromSeconds(seconds) }; + var res = client.PostAsync(url, content).Result; if (res.IsSuccessStatusCode) { result = res.Content.ReadAsStringAsync().Result; diff --git a/HxServer/HxServer/Utils/TaskUtils.cs b/HxServer/HxServer/Utils/TaskUtils.cs index 48dec7b..9e83afc 100644 --- a/HxServer/HxServer/Utils/TaskUtils.cs +++ b/HxServer/HxServer/Utils/TaskUtils.cs @@ -12,9 +12,9 @@ namespace HxServer.Utils; #pragma warning disable IL2026 // Using dynamic types might cause types or members to be removed by trimmer. public class TaskUtils { - private static ConcurrentDictionary TaskQueue = new ConcurrentDictionary(); + private static ConcurrentDictionary TaskQueue = new(); - private static ConcurrentDictionary Dispatchers = new ConcurrentDictionary(); + private static ConcurrentDictionary Dispatchers = new(); public static bool TryCancel(Guid guid) { diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..5bc623e --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,212 @@ +# 通讯协议 + +- [协议结构](#协议结构) +- [数据格式](#数据格式) + - [心跳](#心跳) + - [查询软件参数信息](#查询软件参数信息) + - [修改软件参数信息](#修改软件参数信息) + - [查询日志](#查询日志) + - [添加日志](#添加日志) + - [查询录像](#查询录像) + - [更新录像数据](#更新录像数据) + - [删除录像数据](#删除录像数据) + +## 协议结构 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 操作类型 | +| msgInfo | object | 每个接口特有的参数,详见每个接口定义 | + +## 数据格式 + +### 心跳 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 0 | +| msgInfo | object | | + +**msgInfo** 参数如下 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| name | string | 程序名称 | +| version | string | 版本信息 | + +### 查询软件参数信息 + +* 请求 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 1 | + +* 回复 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 1 | +| msgInfo | Dictionary < string, string > | 设置信息 | + + +### 修改软件参数信息 + +* 请求 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 2 | +| msgInfo | Dictionary < string, string > | 设置信息 | + +* 回复 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 2 | +| msgInfo | object | | + +**msgInfo** 参数如下 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| status | bool | true: 成功; false: 失败; | +| message | string | 异常信息 | + +### 查询日志 + +* 请求 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 3 | +| msgInfo | object | | + +**msgInfo** 参数如下 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| type | int | 0: 系统事件; 1: 用户操作; 2: 视频通道; | +| date | string | 日期 | +| index | int | 通道号(仅视频通道日志有效) | +| month | bool | true: 检索全月; false: 不检索全月 | + +* 回复 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 3 | +| msgInfo | object[] | | + +**msgInfo ** 参数如下 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| timestamp | DateTime | 事件事件 | +| type | int | 事件类型 | +| index | int | 视频通道日志类型时为通道号,其他为空 | +| message | string | 消息 | +| data | string | 详细信息 | + + +### 添加日志 + +* 请求 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 4 | +| msgInfo | object | | + +**msgInfo** 参数如下 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| timestamp | DateTime | 事件事件 | +| type | int | 事件类型 | +| index | int | 视频通道日志类型时为通道号,其他为空 | +| message | string | 消息 | +| data | string | 详细信息 | + +### 查询录像 + +* 请求 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 5 | +| msgInfo | object | | + +> **msgInfo** 参数为空,查询最早的一条录像数据 + +> **msgInfo** 参数如下, 按通道、开始日期、结束日期参数查询录像数据 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| channel | int | 通道号 | +| start_date | DateTime | 开始日期 | +| end_date | DateTime | 结束日期 | + +> **msgInfo** 参数如下, 按日期查询录像数据 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| date | DateTime | 日期 | + + +* 回复 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 5 | +| msgInfo | object[] | | + +**msgInfo ** 参数如下 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| channel | int | 通道号 | +| start_date | DateTime | 开始日期 | +| end_date | DateTime | 结束日期 | +| duration | int | 录像时长 | +| name | string | 文件名称 | +| path | string | 文件路径 | + +### 更新录像数据 + +* 请求 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 6 | +| msgInfo | object | | + +**msgInfo ** 参数如下 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| channel | int | 通道号 | +| start_date | DateTime | 开始日期 | +| end_date | DateTime | 结束日期 | +| duration | int | 录像时长 | +| name | string | 文件名称 | +| path | string | 文件路径 | + +### 删除录像数据 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| action_type | int | 7 | +| msgInfo | object | | + +> **msgInfo** 参数如下, 按文件路径删除录像 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| path | string | 文件路径 | + +> **msgInfo** 参数如下, 按录像日期删除录像 + +| 参数名称 | 类型 | 描述 | +| --- | --- | --- | +| date | DateTime | 日期 | \ No newline at end of file diff --git a/通信协议说明(守护程序).docx b/通信协议说明(守护程序).docx deleted file mode 100644 index e22b877..0000000 Binary files a/通信协议说明(守护程序).docx and /dev/null differ