From 21a452c59e64ba4c5e90372fe59e51c662f50367 Mon Sep 17 00:00:00 2001 From: Tian jianyong <11429339@qq.com> Date: Fri, 1 Nov 2024 17:36:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B0=86=E4=BB=BF=E7=9C=9F?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BF=9D=E5=AD=98=E4=B8=BA=20json=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=B9=B6=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Program.cs | 6 +- ...d6-1a0a-4faf-8a12-380969649862_172941.json | 34 ++++++ ...e5-13c7-4a39-9e40-8e20c5e4c531_173610.json | 34 ++++++ ...0f-218e-4ac4-a40c-335dbe1037eb_173217.json | 34 ++++++ ...30-6780-46bf-afbe-8cf6aca0b25f_173249.json | 34 ++++++ src/DataProcessing/SimulationRecord.cs | 111 ++++++++++++++++++ src/DataProcessing/SimulationRecordExample.cs | 103 ++++++++++++++++ src/DataProcessing/SimulationRecordManager.cs | 66 +++++++++++ 8 files changed, 421 insertions(+), 1 deletion(-) create mode 100644 SimulationRecords/2024-11-01/0109c8d6-1a0a-4faf-8a12-380969649862_172941.json create mode 100644 SimulationRecords/2024-11-01/242f85e5-13c7-4a39-9e40-8e20c5e4c531_173610.json create mode 100644 SimulationRecords/2024-11-01/58e19d0f-218e-4ac4-a40c-335dbe1037eb_173217.json create mode 100644 SimulationRecords/2024-11-01/d30c9c30-6780-46bf-afbe-8cf6aca0b25f_173249.json create mode 100644 src/DataProcessing/SimulationRecord.cs create mode 100644 src/DataProcessing/SimulationRecordExample.cs create mode 100644 src/DataProcessing/SimulationRecordManager.cs diff --git a/Program.cs b/Program.cs index 577c0f6..6b4efae 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,6 @@ using ActiveProtect.Simulation; using ActiveProtect.Evaluation; +using ActiveProtect.DataProcessing; namespace ActiveProtect { @@ -14,7 +15,10 @@ namespace ActiveProtect //SimulationExample.RunAllExamples(); // 运行效能评估示例 - EvaluationExample.RunAllExamples(); + //EvaluationExample.RunAllExamples(); + + // 运行数据记录示例 + SimulationRecordExample.RunExample(); } } } diff --git a/SimulationRecords/2024-11-01/0109c8d6-1a0a-4faf-8a12-380969649862_172941.json b/SimulationRecords/2024-11-01/0109c8d6-1a0a-4faf-8a12-380969649862_172941.json new file mode 100644 index 0000000..f5e654a --- /dev/null +++ b/SimulationRecords/2024-11-01/0109c8d6-1a0a-4faf-8a12-380969649862_172941.json @@ -0,0 +1,34 @@ +{ + "SimulationId": "0109c8d6-1a0a-4faf-8a12-380969649862", + "Timestamp": "2024-11-01T17:29:41.519964+08:00", + "Duration": 15.5, + "Result": { + "IsHit": true, + "InterceptionDistance": 150.5, + "WarningResponseTime": 0.8, + "JammingActivationTime": 1.2, + "SystemStabilityScore": 0.85 + }, + "Events": [ + { + "TimeStamp": 0, + "EventType": "\u5BFC\u5F39\u53D1\u5C04", + "Description": "\u5BFC\u5F39\u5F00\u59CB\u53D1\u5C04" + }, + { + "TimeStamp": 0.8, + "EventType": "\u544A\u8B66\u89E6\u53D1", + "Description": "\u6FC0\u5149\u544A\u8B66\u5668\u63A2\u6D4B\u5230\u5A01\u80C1" + }, + { + "TimeStamp": 1.2, + "EventType": "\u5E72\u6270\u542F\u52A8", + "Description": "\u6FC0\u5149\u5E72\u6270\u5668\u5F00\u59CB\u5DE5\u4F5C" + }, + { + "TimeStamp": 15.5, + "EventType": "\u4EFF\u771F\u7ED3\u675F", + "Description": "\u5BFC\u5F39\u547D\u4E2D\u76EE\u6807" + } + ] +} \ No newline at end of file diff --git a/SimulationRecords/2024-11-01/242f85e5-13c7-4a39-9e40-8e20c5e4c531_173610.json b/SimulationRecords/2024-11-01/242f85e5-13c7-4a39-9e40-8e20c5e4c531_173610.json new file mode 100644 index 0000000..94591ba --- /dev/null +++ b/SimulationRecords/2024-11-01/242f85e5-13c7-4a39-9e40-8e20c5e4c531_173610.json @@ -0,0 +1,34 @@ +{ + "SimulationId": "242f85e5-13c7-4a39-9e40-8e20c5e4c531", + "Timestamp": "2024-11-01T17:36:10.414084+08:00", + "Duration": 15.5, + "Result": { + "IsHit": true, + "InterceptionDistance": 150.5, + "WarningResponseTime": 0.8, + "JammingActivationTime": 1.2, + "SystemStabilityScore": 0.85 + }, + "Events": [ + { + "TimeStamp": 0, + "EventType": "\u5BFC\u5F39\u53D1\u5C04", + "Description": "\u5BFC\u5F39\u5F00\u59CB\u53D1\u5C04" + }, + { + "TimeStamp": 0.8, + "EventType": "\u544A\u8B66\u89E6\u53D1", + "Description": "\u6FC0\u5149\u544A\u8B66\u5668\u63A2\u6D4B\u5230\u5A01\u80C1" + }, + { + "TimeStamp": 1.2, + "EventType": "\u5E72\u6270\u542F\u52A8", + "Description": "\u6FC0\u5149\u5E72\u6270\u5668\u5F00\u59CB\u5DE5\u4F5C" + }, + { + "TimeStamp": 15.5, + "EventType": "\u4EFF\u771F\u7ED3\u675F", + "Description": "\u5BFC\u5F39\u547D\u4E2D\u76EE\u6807" + } + ] +} \ No newline at end of file diff --git a/SimulationRecords/2024-11-01/58e19d0f-218e-4ac4-a40c-335dbe1037eb_173217.json b/SimulationRecords/2024-11-01/58e19d0f-218e-4ac4-a40c-335dbe1037eb_173217.json new file mode 100644 index 0000000..6f5e2c6 --- /dev/null +++ b/SimulationRecords/2024-11-01/58e19d0f-218e-4ac4-a40c-335dbe1037eb_173217.json @@ -0,0 +1,34 @@ +{ + "SimulationId": "58e19d0f-218e-4ac4-a40c-335dbe1037eb", + "Timestamp": "2024-11-01T17:32:17.404421+08:00", + "Duration": 15.5, + "Result": { + "IsHit": true, + "InterceptionDistance": 150.5, + "WarningResponseTime": 0.8, + "JammingActivationTime": 1.2, + "SystemStabilityScore": 0.85 + }, + "Events": [ + { + "TimeStamp": 0, + "EventType": "\u5BFC\u5F39\u53D1\u5C04", + "Description": "\u5BFC\u5F39\u5F00\u59CB\u53D1\u5C04" + }, + { + "TimeStamp": 0.8, + "EventType": "\u544A\u8B66\u89E6\u53D1", + "Description": "\u6FC0\u5149\u544A\u8B66\u5668\u63A2\u6D4B\u5230\u5A01\u80C1" + }, + { + "TimeStamp": 1.2, + "EventType": "\u5E72\u6270\u542F\u52A8", + "Description": "\u6FC0\u5149\u5E72\u6270\u5668\u5F00\u59CB\u5DE5\u4F5C" + }, + { + "TimeStamp": 15.5, + "EventType": "\u4EFF\u771F\u7ED3\u675F", + "Description": "\u5BFC\u5F39\u547D\u4E2D\u76EE\u6807" + } + ] +} \ No newline at end of file diff --git a/SimulationRecords/2024-11-01/d30c9c30-6780-46bf-afbe-8cf6aca0b25f_173249.json b/SimulationRecords/2024-11-01/d30c9c30-6780-46bf-afbe-8cf6aca0b25f_173249.json new file mode 100644 index 0000000..b59a3d4 --- /dev/null +++ b/SimulationRecords/2024-11-01/d30c9c30-6780-46bf-afbe-8cf6aca0b25f_173249.json @@ -0,0 +1,34 @@ +{ + "SimulationId": "d30c9c30-6780-46bf-afbe-8cf6aca0b25f", + "Timestamp": "2024-11-01T17:32:49.51351+08:00", + "Duration": 15.5, + "Result": { + "IsHit": true, + "InterceptionDistance": 150.5, + "WarningResponseTime": 0.8, + "JammingActivationTime": 1.2, + "SystemStabilityScore": 0.85 + }, + "Events": [ + { + "TimeStamp": 0, + "EventType": "\u5BFC\u5F39\u53D1\u5C04", + "Description": "\u5BFC\u5F39\u5F00\u59CB\u53D1\u5C04" + }, + { + "TimeStamp": 0.8, + "EventType": "\u544A\u8B66\u89E6\u53D1", + "Description": "\u6FC0\u5149\u544A\u8B66\u5668\u63A2\u6D4B\u5230\u5A01\u80C1" + }, + { + "TimeStamp": 1.2, + "EventType": "\u5E72\u6270\u542F\u52A8", + "Description": "\u6FC0\u5149\u5E72\u6270\u5668\u5F00\u59CB\u5DE5\u4F5C" + }, + { + "TimeStamp": 15.5, + "EventType": "\u4EFF\u771F\u7ED3\u675F", + "Description": "\u5BFC\u5F39\u547D\u4E2D\u76EE\u6807" + } + ] +} \ No newline at end of file diff --git a/src/DataProcessing/SimulationRecord.cs b/src/DataProcessing/SimulationRecord.cs new file mode 100644 index 0000000..c8596b9 --- /dev/null +++ b/src/DataProcessing/SimulationRecord.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Text.Json.Serialization; + +namespace ActiveProtect.DataProcessing +{ + /// + /// 仿真数据记录类 + /// 用于记录仿真过程中的关键数据和事件 + /// + public class SimulationRecord + { + /// + /// 仿真ID + /// + public string? SimulationId { get; set; } + + /// + /// 仿真时间戳 + /// + public DateTime Timestamp { get; set; } + + /// + /// 仿真时长(秒) + /// + public float Duration { get; set; } + + /// + /// 仿真结果 + /// + public SimulationResult? Result { get; set; } + + /// + /// 关键事件记录 + /// + public List Events { get; set; } = new List(); + } + + /// + /// 仿真结果 + /// 记录仿真的最终评估结果 + /// + public class SimulationResult + { + /// + /// 是否命中目标 + /// + public bool IsHit { get; set; } + + /// + /// 拦截距离(米) + /// + public float InterceptionDistance { get; set; } + + /// + /// 告警响应时间(秒) + /// + public float WarningResponseTime { get; set; } + + /// + /// 干扰启动时间(秒) + /// + public float JammingActivationTime { get; set; } + + /// + /// 系统稳定性评分 [0,1] + /// + public float SystemStabilityScore { get; set; } + } + + /// + /// 仿真事件记录 + /// 记录仿真过程中的关键事件 + /// + public class SimulationEvent + { + /// + /// 事件发生时间(相对于仿真开始的时间,单位:秒) + /// + public float TimeStamp { get; set; } + + /// + /// 事件类型 + /// + public string? EventType { get; set; } + + /// + /// 事件描述 + /// + public string? Description { get; set; } + + /// + /// 事件相关的数据 + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public Dictionary? EventData { get; set; } + } + + /// + /// 事件类型常量 + /// + public static class EventTypes + { + public const string MissileLaunch = "导弹发射"; + public const string WarningTrigger = "告警触发"; + public const string JammingActivation = "干扰启动"; + public const string TargetHit = "命中目标"; + public const string MissileMiss = "未命中目标"; + public const string SimulationEnd = "仿真结束"; + } +} \ No newline at end of file diff --git a/src/DataProcessing/SimulationRecordExample.cs b/src/DataProcessing/SimulationRecordExample.cs new file mode 100644 index 0000000..e319fdf --- /dev/null +++ b/src/DataProcessing/SimulationRecordExample.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; + +namespace ActiveProtect.DataProcessing +{ + /// + /// 仿真数据记录示例类 + /// 展示如何记录和读取仿真数据 + /// + public class SimulationRecordExample + { + /// + /// 运行数据记录示例 + /// + public static void RunExample() + { + Console.WriteLine("\n=== 仿真数据记录示例 ==="); + + // 创建记录管理器 + var recordManager = new SimulationRecordManager("SimulationRecords"); + + // 创建示例记录 + var record = new SimulationRecord + { + SimulationId = Guid.NewGuid().ToString(), + Timestamp = DateTime.Now, + Duration = 15.5f, + Result = new SimulationResult + { + IsHit = true, + InterceptionDistance = 150.5f, + WarningResponseTime = 0.8f, + JammingActivationTime = 1.2f, + SystemStabilityScore = 0.85f + }, + Events = new List + { + new SimulationEvent + { + TimeStamp = 0.0f, + EventType = "导弹发射", + Description = "导弹开始发射" + }, + new SimulationEvent + { + TimeStamp = 0.8f, + EventType = "告警触发", + Description = "激光告警器探测到威胁" + }, + new SimulationEvent + { + TimeStamp = 1.2f, + EventType = "干扰启动", + Description = "激光干扰器开始工作" + }, + new SimulationEvent + { + TimeStamp = 15.5f, + EventType = "仿真结束", + Description = "导弹命中目标" + } + } + }; + + // 保存记录 + Console.WriteLine("保存仿真记录..."); + recordManager.SaveRecord(record); + + // 读取记录 + Console.WriteLine("读取今日仿真记录..."); + var records = recordManager.LoadRecords(DateTime.Today); + + // 显示记录内容 + foreach (var loadedRecord in records) + { + Console.WriteLine($"\n仿真ID: {loadedRecord.SimulationId}"); + Console.WriteLine($"时间戳: {loadedRecord.Timestamp}"); + Console.WriteLine($"仿真时长: {loadedRecord.Duration}秒"); + if (loadedRecord.Result != null) + { + Console.WriteLine($"命中结果: {(loadedRecord.Result.IsHit ? "命中" : "未命中")}"); + Console.WriteLine($"拦截距离: {loadedRecord.Result.InterceptionDistance}米"); + Console.WriteLine($"告警响应时间: {loadedRecord.Result.WarningResponseTime}秒"); + } + else + { + Console.WriteLine("仿真结果数据不可用"); + } + if (loadedRecord.Result != null) + { + Console.WriteLine($"干扰启动时间: {loadedRecord.Result.JammingActivationTime}秒"); + Console.WriteLine($"系统稳定性评分: {loadedRecord.Result.SystemStabilityScore:F2}"); + } + + Console.WriteLine("\n关键事件:"); + foreach (var evt in loadedRecord.Events) + { + Console.WriteLine($"[{evt.TimeStamp:F2}s] {evt.EventType}: {evt.Description}"); + } + } + } + } +} \ No newline at end of file diff --git a/src/DataProcessing/SimulationRecordManager.cs b/src/DataProcessing/SimulationRecordManager.cs new file mode 100644 index 0000000..af1e7f4 --- /dev/null +++ b/src/DataProcessing/SimulationRecordManager.cs @@ -0,0 +1,66 @@ +using System; +using System.IO; +using System.Text.Json; +using System.Collections.Generic; + +namespace ActiveProtect.DataProcessing +{ + /// + /// 仿真记录管理器 + /// 使用JSON文件存储仿真数据 + /// + public class SimulationRecordManager + { + private readonly string baseDirectory; + + public SimulationRecordManager(string baseDirectory = "SimulationRecords") + { + this.baseDirectory = baseDirectory; + Directory.CreateDirectory(baseDirectory); + } + + /// + /// 保存仿真记录 + /// + public void SaveRecord(SimulationRecord record) + { + // 按日期创建子目录 + string dateDir = Path.Combine(baseDirectory, record.Timestamp.ToString("yyyy-MM-dd")); + Directory.CreateDirectory(dateDir); + + // 生成文件名 + string fileName = $"{record.SimulationId}_{record.Timestamp:HHmmss}.json"; + string filePath = Path.Combine(dateDir, fileName); + + // 序列化并保存 + var options = new JsonSerializerOptions { WriteIndented = true }; + string jsonString = JsonSerializer.Serialize(record, options); + File.WriteAllText(filePath, jsonString); + } + + /// + /// 读取指定日期的所有记录 + /// + public List LoadRecords(DateTime date) + { + string dateDir = Path.Combine(baseDirectory, date.ToString("yyyy-MM-dd")); + if (!Directory.Exists(dateDir)) + { + return new List(); + } + + var records = new List(); + foreach (string file in Directory.GetFiles(dateDir, "*.json")) + { + string jsonString = File.ReadAllText(file); + var record = JsonSerializer.Deserialize(jsonString); + if (record is not null) + { + records.Add(record); + } + } + + return records; + } + } +} \ No newline at end of file