diff --git a/Assets/Scripts/ViconNexusUnityStream/SubjectDataManager.cs b/Assets/Scripts/ViconNexusUnityStream/SubjectDataManager.cs index bf07066..f6e769f 100644 --- a/Assets/Scripts/ViconNexusUnityStream/SubjectDataManager.cs +++ b/Assets/Scripts/ViconNexusUnityStream/SubjectDataManager.cs @@ -46,17 +46,23 @@ public StreamType StreamType private List subjectList = new(); private WebSocket webSocket; - private Dictionary data = new Dictionary(); - private Dictionary rawData = new Dictionary(); + private Dictionary data = new(); + private Dictionary rawData = new(); + private Dictionary> recordedData = new(); + private Dictionary> dataToWrite = new(); private string pathToRecordedData; - private Dictionary> recordedData; - Dictionary> dataToWrite = new(); [SerializeField] private int currentFrame = 0; private int totalFrames = 0; + private string fileName = "Session"; + private List recordedSessions = new List(); private void Awake() { pathToRecordedData = Path.Combine(Application.dataPath, pathToDataFile); + if (!Directory.Exists(pathToRecordedData)) + { + Directory.CreateDirectory(pathToRecordedData); + } } /// @@ -82,9 +88,10 @@ private void OnDisable() { if (enableWriteData) { - string jsonData = JsonConvert.SerializeObject(dataToWrite, Formatting.Indented); - File.AppendAllTextAsync(pathToRecordedData, jsonData); + fileName = fileName + "_" + DateTime.Now.ToString("dd-MM-yy hh-mm-ss") + ".json"; + pathToRecordedData = Path.Combine(pathToRecordedData, fileName); + File.AppendAllTextAsync(pathToRecordedData, jsonData); } if (webSocket != null) { @@ -118,16 +125,23 @@ private void ProcessDefaultDataAndWebSocket() private void LoadRecordedJson() { if(streamType != StreamType.Recorded) return; + Debug.Log($"Loading Recorded Data"); - string json = File.ReadAllText(pathToRecordedData); - if (string.IsNullOrEmpty(json)) + + foreach (string session in Directory.GetFiles(pathToRecordedData, "*.json")) { - Debug.LogWarning($"No recorded data found at {pathToRecordedData}."); - return; + string jsonData = Path.Combine(pathToRecordedData, session); + string json = File.ReadAllText(jsonData); + if (string.IsNullOrEmpty(json)) + { + Debug.LogWarning($"No recorded data found at {pathToRecordedData}."); + continue; + } + Debug.Log($"Now Loading {jsonData}"); + JObject recordedJson = JObject.Parse(json); + Dictionary> temp = JsonConvert.DeserializeObject>>(recordedJson.ToString()); + recordedData = recordedData.Concat(temp).ToDictionary(k => k.Key, v => v.Value); } - - JObject recordedJson = JObject.Parse(json); - recordedData = JsonConvert.DeserializeObject>>(recordedJson.ToString()); totalFrames = recordedData.Count; } @@ -198,7 +212,6 @@ private void StreamData(byte[] receivedData) string rawJsonDataString = jsonDataObject.ToString(); data[subject] = JsonConvert.DeserializeObject(rawJsonDataString); rawData[subject] = rawJsonDataString; - } else { @@ -210,7 +223,7 @@ private void StreamData(byte[] receivedData) if (enableWriteData) { - dataToWrite[currentTicks.ToString()] = new Dictionary(data); + dataToWrite[currentTicks.ToString()] = new(data); } }