From 98d0761a196f630d76cc30fddc77564ffea34428 Mon Sep 17 00:00:00 2001
From: hehaoyang <1109196436@qq.com>
Date: Tue, 23 Jan 2024 18:44:16 +0800
Subject: [PATCH] 20240123
---
HxServer/HxServer/HxServer.csproj | 5 +-
HxServer/HxServer/Peripheral/DataBase.cs | 231 ++++++++----------
HxServer/HxServer/Peripheral/MonitorServer.cs | 36 +--
HxServer/HxServer/ReadMe.cs | 2 +-
HxServer/HxServer/Utils/LiteDBUtils.cs | 19 ++
HxServer/HxServer/Utils/SQLiteUtils.cs | 21 --
HxVideoCaptor/HxVideoCaptor.pro | 4 +-
.../external/HxUtils/{ => lib}/libHxUtils.a | Bin
HxVideoCaptor/main.cpp | 3 +
9 files changed, 142 insertions(+), 179 deletions(-)
create mode 100644 HxServer/HxServer/Utils/LiteDBUtils.cs
delete mode 100644 HxServer/HxServer/Utils/SQLiteUtils.cs
rename HxVideoCaptor/external/HxUtils/{ => lib}/libHxUtils.a (100%)
diff --git a/HxServer/HxServer/HxServer.csproj b/HxServer/HxServer/HxServer.csproj
index 03734ad..eed2a9a 100644
--- a/HxServer/HxServer/HxServer.csproj
+++ b/HxServer/HxServer/HxServer.csproj
@@ -4,7 +4,7 @@
Exe
net8.0
enable
- enable
+ disable
true
true
@@ -16,6 +16,7 @@
+
@@ -23,8 +24,8 @@
+
-
diff --git a/HxServer/HxServer/Peripheral/DataBase.cs b/HxServer/HxServer/Peripheral/DataBase.cs
index 200c582..93186a4 100644
--- a/HxServer/HxServer/Peripheral/DataBase.cs
+++ b/HxServer/HxServer/Peripheral/DataBase.cs
@@ -6,7 +6,6 @@ using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
-using System.Data.SQLite;
using HxServer.Utils;
using System.Data.SqlTypes;
using Newtonsoft.Json;
@@ -16,201 +15,163 @@ using System.Reflection;
using System.IO;
using System.Threading.Channels;
using System.Xml.Linq;
+using LiteDB;
+using LiteDB.Async;
+
namespace HxServer.Peripheral
{
+ public class HxSetting
+ {
+ public int ID { get; set; }
+ public string Name { get; set; }
+ public string Value { get; set; }
+ }
+
+ public class HxLog
+ {
+ public int ID { get; set; }
+ public DateTime Timestamp { get; set; }
+ public int Type { get; set; }
+ public int Index { get; set; }
+ public string Message { get; set; }
+ public string Data { get; set; }
+
+ }
+
public class HxVideoRecord
{
+ [JsonIgnore]
+ public int ID { get; set; }
+
[JsonProperty(PropertyName = "channel")]
public int Channel { get; set; }
[JsonProperty(PropertyName = "start_time")]
- public string? StartTime { get; set; }
+ public DateTime StartTime { get; set; }
[JsonProperty(PropertyName = "end_time")]
- public string? EndTime { get; set; }
+ public DateTime EndTime { get; set; }
[JsonProperty(PropertyName = "duration")]
public int Duration { get; set; }
[JsonProperty(PropertyName = "name")]
- public string? Name { get; set; }
+ public string Name { get; set; }
[JsonProperty(PropertyName = "path")]
- public string? Path { get; set; }
-}
+ public string Path { get; set; }
+ }
public class DataBase
{
- private const string filename ="HxNvr.DataBase.db";
+ private const string filename = "HxNvr.DataBase.db";
- private static SQLiteConnection? Connection { get; set; }
+ // 打开数据库 (如果不存在则创建)
+ private static LiteDatabaseAsync database { get; set; } = new(@"HxNvr.DataBase.db");
- private static SQLiteCommand InsertLogCommand = new("INSERT INTO [Log] ([timestamp], [type], [index], [message], [data]) VALUES (@timestamp, @type, @index, @message, @data); SELECT LAST_INSERT_ROWID();");
+ //private static SQLiteCommand InsertLogCommand = new("INSERT INTO [Log] ([timestamp], [type], [index], [message], [data]) VALUES (@timestamp, @type, @index, @message, @data); SELECT LAST_INSERT_ROWID();");
public static void Initialization()
{
- if (!File.Exists(filename)) try { SQLiteConnection.CreateFile(filename); } catch { return; }
-
- Connection = new SQLiteConnection("Data Source=" + filename);
-
- try { Connection.Open(); }
- catch
+ var collection = database.GetCollection("Settings");
+ var array = collection.FindAllAsync().Result;
+ if (array.Count() == 0)
{
- try
- {
- var corrupted = filename + string.Format("_[{0:yyyyMMddHHmmss}].BAK", DateTime.Now);
- File.Move(filename, corrupted);
- SQLiteConnection.CreateFile(filename);
- Connection.Open();
- }
- catch { }
+ collection.InsertAsync(new HxSetting() { Name = "code", Value = "TVIS" });
+ collection.InsertAsync(new HxSetting() { Name = "recording_split_time", Value = "10" });
+ collection.InsertAsync(new HxSetting() { Name = "recording_reserver_time", Value = "15" });
+ collection.InsertAsync(new HxSetting() { Name = "drive_threshold", Value = "10" });
+ collection.InsertAsync(new HxSetting() { Name = "is_cleanup_enabled", Value = "1" });
+ collection.InsertAsync(new HxSetting() { Name = "is_cleanup_forced_for_reserve_time", Value = "0" });
+ collection.InsertAsync(new HxSetting() { Name = "disks", Value = "/home/hehaoyang/harddisk1;D://" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_00", Value = "0;通道 01;Bsm;teweishi.oicp.net;554;1;15;1;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_01", Value = "1;通道 02;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_02", Value = "2;通道 03;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_03", Value = "3;通道 04;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_04", Value = "4;通道 05;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_05", Value = "5;通道 06;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_06", Value = "6;通道 07;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_07", Value = "7;通道 08;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_08", Value = "8;通道 09;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_09", Value = "9;通道 10;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_10", Value = "10;通道 11;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_11", Value = "11;通道 12;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_12", Value = "12;通道 13;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_13", Value = "13;通道 14;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_14", Value = "14;通道 15;Bsm;192.168.10.101;554;1;15;0;1;0" });
+ collection.InsertAsync(new HxSetting() { Name = "channel_15", Value = "15;通道 16;Bsm;192.168.10.101;554;1;15;0;1;0" });
}
-
- /* 创建程序配置表 */
- Connection.ExecuteNonQuery(
- "CREATE TABLE IF NOT EXISTS [Settings] (" +
- " [name] TEXT NOT NULL PRIMARY KEY," +
- " [value] TEXT" +
- ");");
-
- /* 创建日志信息表 */
- Connection.ExecuteNonQuery(
- "CREATE TABLE IF NOT EXISTS [Log] (" +
- " [id] INTEGER NOT NULL PRIMARY KEY," +
- " [timestamp] DATETIME NOT NULL," +
- " [type] INTEGER NOT NULL," +
- " [index] INTEGER," +
- " [message] NTEXT NOT NULL," +
- " [data] NTEXT" +
- ");");
-
- /* 创建录像信息表 */
- Connection.ExecuteNonQuery(
- "CREATE TABLE IF NOT EXISTS [Records] (" +
- " [channel] INTEGER NOT NULL," +
- " [date] DATE NOT NULL," +
- " [start_time] TIME NOT NULL," +
- " [end_time] TIME," +
- " [duration] INTEGER," +
- " [name] NTEXT NOT NULL," +
- " [path] NTEXT NOT NULL," +
- "PRIMARY KEY (name));");
-
-
- InsertLogCommand.Connection = Connection;
- InsertLogCommand.Parameters.Add("@timestamp", DbType.DateTime);
- InsertLogCommand.Parameters.Add("@type", DbType.Int64);
- InsertLogCommand.Parameters.Add("@index", DbType.Int64);
- InsertLogCommand.Parameters.Add("@message", DbType.String);
- InsertLogCommand.Parameters.Add("@data", DbType.String);
- InsertLogCommand.Prepare();
}
+
+
public static Dictionary ReadSettings()
{
Dictionary keyValuePairs = [];
- using SQLiteCommand command = new("SELECT * FROM [Settings]", Connection);
- using SQLiteDataReader reader = command.ExecuteReader();
- while(reader.Read())
- {
- keyValuePairs.Add(reader.GetString(0), reader.IsDBNull(1) ? "" : reader.GetString(1));
- }
+ /* 打开表 */
+ var collection = database.GetCollection("Settings");
+
+ var array = collection.FindAllAsync().Result;
+
+ /* 查询所有数据 */
+ foreach (var c in array)
+ keyValuePairs.Add(c.Name, c.Value);
return keyValuePairs;
}
- public static void WriteLogger(DateTime timestamp, int type, int? index, string? message, string? data)
+ public static async void WriteLogger(DateTime timestamp, int type, int index, string message, string data)
{
- InsertLogCommand.Parameters["@timestamp"].Value = timestamp;
- InsertLogCommand.Parameters["@type"].Value = (long)type;
- if (index.HasValue && index != -1) InsertLogCommand.Parameters["@index"].Value = index.Value;
- InsertLogCommand.Parameters["@message"].Value = message;
- InsertLogCommand.Parameters["@data"].Value = data;
- try { object res = InsertLogCommand.ExecuteScalar(); } catch { }
+ /* 打开表 */
+ var collection = database.GetCollection("Log");
+
+ /* 插入数据 */
+ await collection.InsertAsync(new HxLog() { Timestamp = timestamp, Type = type, Index = index, Message = message, Data = data });
}
- public static List