| | | 1 | | using System; |
| | | 2 | | using System.Collections; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using UnityEngine; |
| | | 7 | | |
| | | 8 | | public class SimMonitor : MonoBehaviour { |
| | | 9 | | private static class EventTypes { |
| | | 10 | | public const string NewInterceptor = "NEW_INTERCEPTOR"; |
| | | 11 | | public const string NewThreat = "NEW_THREAT"; |
| | | 12 | | public const string InterceptorHit = "INTERCEPTOR_HIT"; |
| | | 13 | | public const string InterceptorMiss = "INTERCEPTOR_MISS"; |
| | | 14 | | public const string ThreatHit = "THREAT_HIT"; |
| | | 15 | | public const string ThreatMiss = "THREAT_MISS"; |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | [Serializable] |
| | | 19 | | private class EventRecord { |
| | | 20 | | public float Time; |
| | | 21 | | public string EventType; |
| | | 22 | | public string AgentType; |
| | | 23 | | public string AgentID; |
| | | 24 | | public float PositionX; |
| | | 25 | | public float PositionY; |
| | | 26 | | public float PositionZ; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | // Telemetry update period in seconds. |
| | | 30 | | private const float _updatePeriod = 0.1f; // 10 Hz |
| | | 31 | | |
| | 2 | 32 | | public static SimMonitor Instance { get; private set; } |
| | | 33 | | |
| | 26 | 34 | | public string Timestamp { get; private set; } = ""; |
| | | 35 | | |
| | | 36 | | private Coroutine _monitorRoutine; |
| | | 37 | | |
| | | 38 | | private string _sessionDirectory; |
| | | 39 | | |
| | | 40 | | private string _telemetryBinPath; |
| | | 41 | | private FileStream _telemetryFileStream; |
| | | 42 | | private BinaryWriter _telemetryBinaryWriter; |
| | | 43 | | |
| | | 44 | | private string _eventLogPath; |
| | | 45 | | [SerializeField] |
| | | 46 | | private List<EventRecord> _eventLog; |
| | | 47 | | |
| | 2 | 48 | | private bool _isLoggingDestroyed = false; |
| | | 49 | | |
| | 1 | 50 | | private void Awake() { |
| | 1 | 51 | | if (Instance != null && Instance != this) { |
| | 0 | 52 | | Destroy(gameObject); |
| | 0 | 53 | | return; |
| | | 54 | | } |
| | 1 | 55 | | Instance = this; |
| | 1 | 56 | | DontDestroyOnLoad(gameObject); |
| | 1 | 57 | | } |
| | | 58 | | |
| | 1 | 59 | | private void Start() { |
| | 1 | 60 | | SimManager.Instance.OnSimulationStarted += RegisterSimulationStarted; |
| | 1 | 61 | | SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded; |
| | 1 | 62 | | SimManager.Instance.OnNewThreat += RegisterNewThreat; |
| | 1 | 63 | | SimManager.Instance.OnNewInterceptor += RegisterNewInterceptor; |
| | 1 | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | private void OnDestroy() { |
| | 0 | 67 | | DestroyLogging(); |
| | 0 | 68 | | } |
| | | 69 | | |
| | 12 | 70 | | private void RegisterSimulationStarted() { |
| | 12 | 71 | | _isLoggingDestroyed = false; |
| | 12 | 72 | | Timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss"); |
| | 12 | 73 | | InitializeSessionDirectory(); |
| | 12 | 74 | | if (SimManager.Instance.SimulatorConfig.EnableTelemetryLogging) { |
| | 0 | 75 | | InitializeTelemetryLogging(); |
| | 0 | 76 | | _monitorRoutine = StartCoroutine(MonitorRoutine()); |
| | 0 | 77 | | } |
| | 24 | 78 | | if (SimManager.Instance.SimulatorConfig.EnableEventLogging) { |
| | 12 | 79 | | InitializeEventLogging(); |
| | 12 | 80 | | } |
| | 12 | 81 | | } |
| | | 82 | | |
| | 11 | 83 | | private void RegisterSimulationEnded() { |
| | 11 | 84 | | DestroyLogging(); |
| | 11 | 85 | | } |
| | | 86 | | |
| | 14 | 87 | | private void RegisterNewInterceptor(IInterceptor interceptor) { |
| | 14 | 88 | | RegisterAgentEvent(interceptor, EventTypes.NewInterceptor); |
| | 14 | 89 | | interceptor.OnHit += RegisterInterceptorHit; |
| | 14 | 90 | | interceptor.OnMiss += RegisterInterceptorMiss; |
| | 14 | 91 | | } |
| | | 92 | | |
| | 955 | 93 | | private void RegisterNewThreat(IThreat threat) { |
| | 955 | 94 | | RegisterAgentEvent(threat, EventTypes.NewThreat); |
| | 955 | 95 | | threat.OnHit += RegisterThreatHit; |
| | 955 | 96 | | threat.OnMiss += RegisterThreatMiss; |
| | 955 | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | private void RegisterInterceptorHit(IInterceptor interceptor) { |
| | 0 | 100 | | RegisterAgentEvent(interceptor, EventTypes.InterceptorHit); |
| | 0 | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | private void RegisterInterceptorMiss(IInterceptor interceptor) { |
| | 0 | 104 | | RegisterAgentEvent(interceptor, EventTypes.InterceptorMiss); |
| | 0 | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | private void RegisterThreatHit(IThreat threat) { |
| | 0 | 108 | | RegisterAgentEvent(threat, EventTypes.ThreatHit); |
| | 0 | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | private void RegisterThreatMiss(IThreat threat) { |
| | 0 | 112 | | RegisterAgentEvent(threat, EventTypes.ThreatMiss); |
| | 0 | 113 | | } |
| | | 114 | | |
| | 969 | 115 | | private void RegisterAgentEvent(IAgent agent, string eventType) { |
| | 1938 | 116 | | if (SimManager.Instance.SimulatorConfig.EnableEventLogging) { |
| | 969 | 117 | | float time = SimManager.Instance.ElapsedTime; |
| | 969 | 118 | | Vector3 position = agent.Position; |
| | 969 | 119 | | var record = new EventRecord { |
| | | 120 | | Time = time, |
| | | 121 | | EventType = eventType, |
| | | 122 | | AgentType = agent.StaticConfig.AgentType.ToString(), |
| | | 123 | | AgentID = agent.gameObject.name, |
| | | 124 | | PositionX = position.x, |
| | | 125 | | PositionY = position.y, |
| | | 126 | | PositionZ = position.z, |
| | | 127 | | }; |
| | 969 | 128 | | _eventLog.Add(record); |
| | 969 | 129 | | } |
| | 969 | 130 | | } |
| | | 131 | | |
| | 12 | 132 | | private void InitializeSessionDirectory() { |
| | 12 | 133 | | if (RunManager.Instance.HasRunConfig()) { |
| | 0 | 134 | | _sessionDirectory = |
| | | 135 | | Path.Combine(Application.persistentDataPath, "Logs", |
| | | 136 | | $"{RunManager.Instance.RunConfig.Name}_{SimManager.Instance.Timestamp}", |
| | | 137 | | $"run_{RunManager.Instance.RunIndex + 1}_seed_{RunManager.Instance.Seed}"); |
| | 12 | 138 | | } else { |
| | 12 | 139 | | _sessionDirectory = Path.Combine(Application.persistentDataPath, "Logs", |
| | | 140 | | $"run_{SimManager.Instance.Timestamp}"); |
| | 12 | 141 | | } |
| | 12 | 142 | | Directory.CreateDirectory(_sessionDirectory); |
| | 12 | 143 | | Debug.Log($"Monitoring simulation logs to {_sessionDirectory}."); |
| | 12 | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | private void InitializeTelemetryLogging() { |
| | 0 | 147 | | string telemetryFile = $"sim_telemetry_{Timestamp}.bin"; |
| | 0 | 148 | | _telemetryBinPath = Path.Combine(_sessionDirectory, telemetryFile); |
| | 0 | 149 | | _telemetryFileStream = |
| | | 150 | | new FileStream(_telemetryBinPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); |
| | 0 | 151 | | _telemetryBinaryWriter = new BinaryWriter(_telemetryFileStream); |
| | 0 | 152 | | Debug.Log($"Telemetry file initialized successfully: {telemetryFile}."); |
| | 0 | 153 | | } |
| | | 154 | | |
| | 12 | 155 | | private void InitializeEventLogging() { |
| | 12 | 156 | | string eventLog = $"sim_events_{Timestamp}.csv"; |
| | 12 | 157 | | _eventLogPath = Path.Combine(_sessionDirectory, eventLog); |
| | 12 | 158 | | _eventLog = new List<EventRecord>(); |
| | 12 | 159 | | Debug.Log($"Event log initialized successfully: {eventLog}."); |
| | 12 | 160 | | } |
| | | 161 | | |
| | 11 | 162 | | private void DestroyLogging() { |
| | 11 | 163 | | if (_isLoggingDestroyed) { |
| | 0 | 164 | | return; |
| | | 165 | | } |
| | 11 | 166 | | _isLoggingDestroyed = true; |
| | | 167 | | |
| | 11 | 168 | | if (SimManager.Instance.SimulatorConfig.EnableTelemetryLogging) { |
| | 0 | 169 | | if (_monitorRoutine != null) { |
| | 0 | 170 | | StopCoroutine(_monitorRoutine); |
| | 0 | 171 | | _monitorRoutine = null; |
| | 0 | 172 | | } |
| | 0 | 173 | | RecordTelemetry(); |
| | 0 | 174 | | DestroyTelemetryLogging(); |
| | 0 | 175 | | ConvertTelemetryBinaryToCsv(_telemetryBinPath, |
| | | 176 | | Path.ChangeExtension(_telemetryBinPath, ".csv")); |
| | 0 | 177 | | } |
| | 22 | 178 | | if (SimManager.Instance.SimulatorConfig.EnableEventLogging) { |
| | 11 | 179 | | WriteEventsToFile(); |
| | 11 | 180 | | } |
| | 11 | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | private void DestroyTelemetryLogging() { |
| | 0 | 184 | | if (_telemetryBinaryWriter != null) { |
| | 0 | 185 | | _telemetryBinaryWriter.Flush(); |
| | 0 | 186 | | _telemetryBinaryWriter.Close(); |
| | 0 | 187 | | _telemetryBinaryWriter = null; |
| | 0 | 188 | | } |
| | | 189 | | |
| | 0 | 190 | | if (_telemetryFileStream != null) { |
| | 0 | 191 | | _telemetryFileStream.Close(); |
| | 0 | 192 | | _telemetryFileStream = null; |
| | 0 | 193 | | } |
| | 0 | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | private IEnumerator MonitorRoutine() { |
| | 0 | 197 | | while (true) { |
| | 0 | 198 | | RecordTelemetry(); |
| | 0 | 199 | | yield return new WaitForSeconds(_updatePeriod); |
| | 0 | 200 | | } |
| | | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | private void RecordTelemetry() { |
| | 0 | 204 | | float time = SimManager.Instance.ElapsedTime; |
| | 0 | 205 | | List<IAgent> agents = SimManager.Instance.Agents.Where(agent => !agent.IsTerminated).ToList(); |
| | 0 | 206 | | if (_telemetryBinaryWriter == null) { |
| | 0 | 207 | | Debug.LogWarning("Telemetry binary writer is null."); |
| | 0 | 208 | | return; |
| | | 209 | | } |
| | 0 | 210 | | foreach (var agent in agents) { |
| | 0 | 211 | | if (!agent.gameObject.activeInHierarchy) { |
| | 0 | 212 | | continue; |
| | | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | Vector3 position = agent.Position; |
| | 0 | 216 | | if (position == Vector3.zero) { |
| | 0 | 217 | | continue; |
| | | 218 | | } |
| | 0 | 219 | | Vector3 velocity = agent.Velocity; |
| | | 220 | | |
| | | 221 | | // Write telemetry data directly to the binary file. |
| | 0 | 222 | | _telemetryBinaryWriter.Write(time); |
| | 0 | 223 | | _telemetryBinaryWriter.Write((int)agent.StaticConfig.AgentType); |
| | 0 | 224 | | _telemetryBinaryWriter.Write(agent.gameObject.name); |
| | 0 | 225 | | _telemetryBinaryWriter.Write(position.x); |
| | 0 | 226 | | _telemetryBinaryWriter.Write(position.y); |
| | 0 | 227 | | _telemetryBinaryWriter.Write(position.z); |
| | 0 | 228 | | _telemetryBinaryWriter.Write(velocity.x); |
| | 0 | 229 | | _telemetryBinaryWriter.Write(velocity.y); |
| | 0 | 230 | | _telemetryBinaryWriter.Write(velocity.z); |
| | 0 | 231 | | } |
| | 0 | 232 | | } |
| | | 233 | | |
| | 0 | 234 | | private void ConvertTelemetryBinaryToCsv(string binaryFilePath, string csvFilePath) { |
| | 0 | 235 | | try { |
| | 0 | 236 | | using var fs = |
| | | 237 | | new FileStream(binaryFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| | 0 | 238 | | using var reader = new BinaryReader(fs); |
| | 0 | 239 | | using var writer = new StreamWriter(csvFilePath, false); |
| | 0 | 240 | | { |
| | | 241 | | // Write the CSV header. |
| | 0 | 242 | | writer.WriteLine( |
| | | 243 | | "Time,AgentType,AgentID,PositionX,PositionY,PositionZ,VelocityX,VelocityY,VelocityZ"); |
| | | 244 | | |
| | 0 | 245 | | while (reader.BaseStream.Position != reader.BaseStream.Length) { |
| | 0 | 246 | | float time = reader.ReadSingle(); |
| | 0 | 247 | | var agentType = (Configs.AgentType)reader.ReadInt32(); |
| | 0 | 248 | | string agentID = reader.ReadString(); |
| | 0 | 249 | | float positionX = reader.ReadSingle(); |
| | 0 | 250 | | float positionY = reader.ReadSingle(); |
| | 0 | 251 | | float positionZ = reader.ReadSingle(); |
| | 0 | 252 | | float velocityX = reader.ReadSingle(); |
| | 0 | 253 | | float velocityY = reader.ReadSingle(); |
| | 0 | 254 | | float velocityZ = reader.ReadSingle(); |
| | | 255 | | |
| | | 256 | | // Write the data to CSV. |
| | 0 | 257 | | writer.WriteLine($"{time:F2},{agentType.ToString()},{agentID}," + |
| | | 258 | | $"{positionX:F2},{positionY:F2},{positionZ:F2}," + |
| | | 259 | | $"{velocityX:F2},{velocityY:F2},{velocityZ:F2}"); |
| | 0 | 260 | | } |
| | 0 | 261 | | Debug.Log($"Telemetry CSV file converted successfully: {csvFilePath}."); |
| | 0 | 262 | | } |
| | 0 | 263 | | } catch (IOException e) { |
| | 0 | 264 | | Debug.LogWarning( |
| | | 265 | | $"An IO error occurred while converting binary telemetry file to CSV format: {e.Message}."); |
| | 0 | 266 | | } |
| | 0 | 267 | | } |
| | | 268 | | |
| | 11 | 269 | | private void WriteEventsToFile() { |
| | 22 | 270 | | using (var writer = new StreamWriter(_eventLogPath, append: false)) { |
| | | 271 | | // Write the CSV header. |
| | 11 | 272 | | writer.WriteLine("Time,Event,AgentType,AgentID,PositionX,PositionY,PositionZ"); |
| | | 273 | | |
| | 2916 | 274 | | foreach (var record in _eventLog) { |
| | 961 | 275 | | writer.WriteLine( |
| | | 276 | | $"{record.Time:F2},{record.EventType},{record.AgentType},{record.AgentID}," + |
| | | 277 | | $"{record.PositionX:F2},{record.PositionY:F2},{record.PositionZ:F2}"); |
| | 961 | 278 | | } |
| | 11 | 279 | | } |
| | 11 | 280 | | } |
| | | 281 | | } |