This commit is contained in:
hehaoyang 2024-01-28 01:08:40 +08:00
parent eb9ab319e7
commit c597921c60
8 changed files with 246 additions and 49 deletions

View File

@ -13,8 +13,6 @@
<ItemGroup> <ItemGroup>
<Compile Remove="._Program.cs" /> <Compile Remove="._Program.cs" />
<Compile Remove="._ReadMe.cs" /> <Compile Remove="._ReadMe.cs" />
<Compile Remove="Peripheral\._DataBase.cs" />
<Compile Remove="Peripheral\._MonitorServer.cs" />
<Compile Remove="Utils\._FileUtils.cs" /> <Compile Remove="Utils\._FileUtils.cs" />
<Compile Remove="Utils\._JsonUtils.cs" /> <Compile Remove="Utils\._JsonUtils.cs" />
<Compile Remove="Utils\._LiteDBUtils.cs" /> <Compile Remove="Utils\._LiteDBUtils.cs" />

View File

@ -1,9 +1,5 @@
using System; using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace HxServer; namespace HxServer;
@ -14,14 +10,14 @@ public class ReadMe
{ {
public static string AppName { get => "HxServer"; } 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<string, List<string>> Version = new Dictionary<string, List<string>>() static readonly Dictionary<string, List<string>> Version = new()
{ {
["1.00 Alpha Bulid 20240111"] = new List<string>() ["1.00 Alpha Bulid 20240111"] =
{ [
"版本名称格式变换", "版本名称格式变换",
}, ],
}; };
[RequiresAssemblyFiles()] [RequiresAssemblyFiles()]
@ -33,9 +29,9 @@ public class ReadMe
using (FileStream fr = new(Path.Combine(path, "ReadMe.md"), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) 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(); sb.AppendLine($"# {AppName}").AppendLine();
foreach (var item in Version) foreach (var item in Version)
{ {

View File

@ -1,10 +1,4 @@
using System; namespace HxServer.Utils
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HxServer.Utils
{ {
public class DebugTrace public class DebugTrace
{ {

View File

@ -1,48 +1,49 @@
using System; using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HxServer.Utils; namespace HxServer.Utils;
public class FileUtils public class FileUtils
{ {
public static long Size(string fullpath) { try { return new FileInfo(fullpath).Length; } catch { return 0; } } 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 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 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 byte[] ReadBytes(string fullpath) { try { return File.ReadAllBytes(fullpath); } catch { return null; } }
public static void Append(string fullpath, string contents) public static void Append(string fullpath, string contents)
{ {
try 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);
using StreamWriter sw = new StreamWriter(fr, Encoding.UTF8); using StreamWriter sw = new(fr, Encoding.UTF8);
sw.WriteLine(contents); sw.WriteLine(contents);
} }
catch { } catch { }
} }
public static void Append(string fullpath, byte[] contents) public static void Append(string fullpath, byte[] contents)
{ {
try 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); fr.Write(contents, 0, contents.Length);
} }
catch { } catch { }
} }
public static void Append(string fullpath, byte[] contents, int offset, int count) public static void Append(string fullpath, byte[] contents, int offset, int count)
{ {
try 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); fr.Write(contents, offset, count);
} }
catch { } catch { }
@ -52,23 +53,22 @@ public class FileUtils
{ {
try 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);
using StreamWriter sw = new StreamWriter(fr, Encoding.UTF8); using StreamWriter sw = new(fr, Encoding.UTF8);
sw.WriteLine(contents); sw.WriteLine(contents);
} }
catch { } catch { }
} }
public static void WriteFile(string fullpath, byte[] contents) public static void WriteFile(string fullpath, byte[] contents)
{ {
try 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); fr.Write(contents, 0, contents.Length);
} }
catch { } catch { }

View File

@ -1,7 +1,4 @@
using System; using System.Net;
using System.Net;
using System.Net.Http;
using System.Text;
namespace HxServer.Utils; namespace HxServer.Utils;
@ -57,7 +54,7 @@ public class HttpUtils
/* 设置返回数据为JSON类 */ /* 设置返回数据为JSON类 */
Context.Response.AddHeader("Content-Type", "application/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));
} }
/// <summary> /// <summary>
@ -71,7 +68,7 @@ public class HttpUtils
{ {
result = string.Empty; 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; var response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode)
{ {
@ -97,8 +94,8 @@ public class HttpUtils
HttpContent content = new StringContent(data.ToString()); HttpContent content = new StringContent(data.ToString());
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/josn"); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/josn");
using HttpClient client = new(); using HttpClient client = new() { Timeout = TimeSpan.FromSeconds(seconds) };
HttpResponseMessage res = client.PostAsync(url, content).Result; var res = client.PostAsync(url, content).Result;
if (res.IsSuccessStatusCode) if (res.IsSuccessStatusCode)
{ {
result = res.Content.ReadAsStringAsync().Result; result = res.Content.ReadAsStringAsync().Result;

View File

@ -12,9 +12,9 @@ namespace HxServer.Utils;
#pragma warning disable IL2026 // Using dynamic types might cause types or members to be removed by trimmer. #pragma warning disable IL2026 // Using dynamic types might cause types or members to be removed by trimmer.
public class TaskUtils public class TaskUtils
{ {
private static ConcurrentDictionary<Guid, Task> TaskQueue = new ConcurrentDictionary<Guid, Task>(); private static ConcurrentDictionary<Guid, Task> TaskQueue = new();
private static ConcurrentDictionary<Guid, CancellationTokenSource> Dispatchers = new ConcurrentDictionary<Guid, CancellationTokenSource>(); private static ConcurrentDictionary<Guid, CancellationTokenSource> Dispatchers = new();
public static bool TryCancel(Guid guid) public static bool TryCancel(Guid guid)
{ {

212
ReadMe.md Normal file
View File

@ -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 | 日期 |