Skip to content

Commit

Permalink
Adding Directory Handler, Naming Sessions with Date/Time
Browse files Browse the repository at this point in the history
  • Loading branch information
omangbaheti committed Nov 29, 2024
1 parent b27a734 commit b2212c7
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions Assets/Scripts/ViconNexusUnityStream/SubjectDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,23 @@ public StreamType StreamType

private List<string> subjectList = new();
private WebSocket webSocket;
private Dictionary<string, ViconStreamData> data = new Dictionary<string, ViconStreamData>();
private Dictionary<string, string> rawData = new Dictionary<string, string>();
private Dictionary<string, ViconStreamData> data = new();
private Dictionary<string, string> rawData = new();

private Dictionary<string, Dictionary<string, ViconStreamData>> recordedData = new();
private Dictionary<string, Dictionary<string, ViconStreamData>> dataToWrite = new();
private string pathToRecordedData;
private Dictionary<string, Dictionary<string, ViconStreamData>> recordedData;
Dictionary<string, Dictionary<string, ViconStreamData>> dataToWrite = new();
[SerializeField] private int currentFrame = 0;
private int totalFrames = 0;
private string fileName = "Session";
private List<string> recordedSessions = new List<string>();
private void Awake()
{
pathToRecordedData = Path.Combine(Application.dataPath, pathToDataFile);
if (!Directory.Exists(pathToRecordedData))
{
Directory.CreateDirectory(pathToRecordedData);
}
}

/// <inheritdoc />
Expand All @@ -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)
{
Expand Down Expand Up @@ -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<string, Dictionary<string, ViconStreamData>> temp = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, ViconStreamData>>>(recordedJson.ToString());
recordedData = recordedData.Concat(temp).ToDictionary(k => k.Key, v => v.Value);
}

JObject recordedJson = JObject.Parse(json);
recordedData = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, ViconStreamData>>>(recordedJson.ToString());
totalFrames = recordedData.Count;
}

Expand Down Expand Up @@ -198,7 +212,6 @@ private void StreamData(byte[] receivedData)
string rawJsonDataString = jsonDataObject.ToString();
data[subject] = JsonConvert.DeserializeObject<ViconStreamData>(rawJsonDataString);
rawData[subject] = rawJsonDataString;

}
else
{
Expand All @@ -210,7 +223,7 @@ private void StreamData(byte[] receivedData)

if (enableWriteData)
{
dataToWrite[currentTicks.ToString()] = new Dictionary<string, ViconStreamData>(data);
dataToWrite[currentTicks.ToString()] = new(data);
}
}

Expand Down

0 comments on commit b2212c7

Please sign in to comment.