| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using CounterDrone.Core.Models; |
| | | 5 | | using SQLite; |
| | | 6 | | |
| | | 7 | | namespace CounterDrone.Core.Simulation |
| | | 8 | | { |
| | | 9 | | public class FrameDataStore |
| | | 10 | | { |
| | | 11 | | private readonly IPathProvider _paths; |
| | | 12 | | /// <summary>帧数据保留天数,默认 180 天(6 个月)</summary> |
| | 20 | 13 | | public int RetentionDays { get; set; } = 180; |
| | | 14 | | |
| | 15 | 15 | | public FrameDataStore(IPathProvider paths) |
| | 15 | 16 | | { |
| | 15 | 17 | | _paths = paths; |
| | 15 | 18 | | } |
| | | 19 | | |
| | | 20 | | public SQLiteConnection CreateOrOpen(string taskId) |
| | 15 | 21 | | { |
| | 15 | 22 | | var dbPath = Path.Combine(_paths.GetFramesDir(), $"{taskId}.db"); |
| | 15 | 23 | | var db = new SQLiteConnection(dbPath); |
| | 15 | 24 | | db.CreateTable<SimFrameRecord>(); |
| | 15 | 25 | | db.CreateIndex("SimFrameRecord", new[] { "TaskId", "FrameIndex" }); |
| | 15 | 26 | | return db; |
| | 15 | 27 | | } |
| | | 28 | | |
| | | 29 | | public void WriteFrame(SQLiteConnection db, string taskId, int frameIdx, float timestamp, List<EntitySnapshot> s |
| | 5544 | 30 | | { |
| | 28448 | 31 | | foreach (var s in snapshots) |
| | 5908 | 32 | | { |
| | 5908 | 33 | | db.Insert(new SimFrameRecord |
| | 5908 | 34 | | { |
| | 5908 | 35 | | TaskId = taskId, |
| | 5908 | 36 | | FrameIndex = frameIdx, |
| | 5908 | 37 | | Timestamp = timestamp, |
| | 5908 | 38 | | EntityId = s.EntityId, |
| | 5908 | 39 | | EntityType = (int)s.EntityType, |
| | 5908 | 40 | | PosX = s.PosX, |
| | 5908 | 41 | | PosY = s.PosY, |
| | 5908 | 42 | | PosZ = s.PosZ, |
| | 5908 | 43 | | RotX = s.RotX, |
| | 5908 | 44 | | RotY = s.RotY, |
| | 5908 | 45 | | RotZ = s.RotZ, |
| | 5908 | 46 | | StateData = s.StateData, |
| | 5908 | 47 | | }); |
| | 5908 | 48 | | } |
| | 5544 | 49 | | } |
| | | 50 | | |
| | | 51 | | public List<SimFrameRecord> ReadFrames(SQLiteConnection db, string taskId, int fromFrame, int toFrame) |
| | 0 | 52 | | { |
| | 0 | 53 | | return db.Table<SimFrameRecord>() |
| | 0 | 54 | | .Where(f => f.TaskId == taskId && f.FrameIndex >= fromFrame && f.FrameIndex <= toFrame) |
| | 0 | 55 | | .OrderBy(f => f.FrameIndex) |
| | 0 | 56 | | .ToList(); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | public void DeleteFrameDb(string taskId) |
| | 0 | 60 | | { |
| | 0 | 61 | | var dbPath = Path.Combine(_paths.GetFramesDir(), $"{taskId}.db"); |
| | 0 | 62 | | if (File.Exists(dbPath)) File.Delete(dbPath); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary>清理过期的帧数据库文件</summary> |
| | | 66 | | /// <returns>被删除的文件数量</returns> |
| | | 67 | | public int CleanupExpired() |
| | 3 | 68 | | { |
| | 3 | 69 | | var dir = _paths.GetFramesDir(); |
| | 3 | 70 | | if (!Directory.Exists(dir)) return 0; |
| | | 71 | | |
| | 3 | 72 | | var cutoff = DateTime.Now.AddDays(-RetentionDays); |
| | 3 | 73 | | var deleted = 0; |
| | | 74 | | |
| | 15 | 75 | | foreach (var file in Directory.GetFiles(dir, "*.db")) |
| | 3 | 76 | | { |
| | 3 | 77 | | var creationTime = File.GetCreationTime(file); |
| | 3 | 78 | | if (creationTime < cutoff) |
| | 2 | 79 | | { |
| | 2 | 80 | | File.Delete(file); |
| | 2 | 81 | | deleted++; |
| | 2 | 82 | | } |
| | 3 | 83 | | } |
| | | 84 | | |
| | 3 | 85 | | return deleted; |
| | 3 | 86 | | } |
| | | 87 | | } |
| | | 88 | | } |