Skip to content

Commit

Permalink
Making Fixes with RecordedDataStreamer
Browse files Browse the repository at this point in the history
  • Loading branch information
omangbaheti committed Nov 29, 2024
1 parent a0eb522 commit b27a734
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
12 changes: 12 additions & 0 deletions Assets/Scripts/ViconNexusUnityStream/CustomHandScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ public class CustomHandScript : CustomSubjectScript
private List<XRHandJointRadius> xrHandJointRadiiList = new();

public float normalOffset = 0.001f;

[Range(0, 0.09f)]
[SerializeField] public float indexNormalOffset = 0.001f;
[Range(0, 0.09f)]
[SerializeField] public float middleNormalOffset = 0.0018f;
[Range(0, 0.09f)]
[SerializeField] public float ringNormalOffset = 0.0013f;
[Range(0, 0.09f)]
[SerializeField] public float littleNormalOffset = 0.0012f;
[Range(0, 0.09f)]
[SerializeField] public float thumbNormalOffset = 0.00009f;

public bool setPosition = true;
public bool setScale = true;
public float scaleToSet = 0.02f;
Expand Down
46 changes: 30 additions & 16 deletions Assets/Scripts/ViconNexusUnityStream/SubjectDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

public class SubjectDataManager : MonoBehaviour
Expand Down Expand Up @@ -37,7 +38,7 @@ public StreamType StreamType
/// Enable writing data to disk.
/// </summary>
public bool EnableWriteData { get => enableWriteData; set => enableWriteData = value; }

[Tooltip("Path to write the subject data file.")]
[SerializeField] private string pathToDataFile;
public Dictionary<string, ViconStreamData> StreamedData => data;
Expand All @@ -47,10 +48,12 @@ public StreamType StreamType
private WebSocket webSocket;
private Dictionary<string, ViconStreamData> data = new Dictionary<string, ViconStreamData>();
private Dictionary<string, string> rawData = new Dictionary<string, string>();

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 void Awake()
{
pathToRecordedData = Path.Combine(Application.dataPath, pathToDataFile);
Expand All @@ -60,6 +63,7 @@ private void Awake()
private void OnEnable()
{
MaybeSetupConnection();
LoadRecordedJson();
}

/// <inheritdoc />
Expand All @@ -68,13 +72,20 @@ private void FixedUpdate()
if (streamType == StreamType.Recorded)
{
StreamLocalData();
return;
}
webSocket?.DispatchLatestMessage();
}

/// <inheritdoc />
private void OnDisable()
{
if (enableWriteData)
{

string jsonData = JsonConvert.SerializeObject(dataToWrite, Formatting.Indented);
File.AppendAllTextAsync(pathToRecordedData, jsonData);
}
if (webSocket != null)
{
webSocket.OnMessage -= StreamData;
Expand Down Expand Up @@ -103,21 +114,21 @@ private void ProcessDefaultDataAndWebSocket()
MaybeSetupConnection();
}
}

private void LoadRecordedJson()
{
if(streamType != StreamType.Recorded) return;


Debug.Log($"Loading Recorded Data");
string json = File.ReadAllText(pathToRecordedData);
if (string.IsNullOrEmpty(json))
{
Debug.LogWarning($"No recorded data found at {pathToRecordedData}.");
return;
}

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

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

}
else
{
Expand All @@ -198,22 +210,24 @@ private void StreamData(byte[] receivedData)

if (enableWriteData)
{
Dictionary<string, Dictionary<string, ViconStreamData>> dataToWrite = new();
dataToWrite[currentTicks.ToString()] = data;
string jsonData = JsonConvert.SerializeObject(dataToWrite);
File.WriteAllText(pathToRecordedData, jsonData);
dataToWrite[currentTicks.ToString()] = new Dictionary<string, ViconStreamData>(data);
}
}

private void StreamLocalData()
{
foreach (KeyValuePair<string, Dictionary<string, ViconStreamData>> frame in recordedData)
if (currentFrame >= totalFrames)
{
foreach (KeyValuePair<string, ViconStreamData> subject in frame.Value)
{
data[subject.Key] = subject.Value;
}
currentFrame = 0;
}

KeyValuePair<string, Dictionary<string, ViconStreamData>> currentFrameData = recordedData.ElementAt(currentFrame);
foreach (KeyValuePair<string, ViconStreamData> subject in currentFrameData.Value)
{
data[subject.Key] = subject.Value;
rawData[subject.Key] = subject.Value.ToString();
}
currentFrame++;
}

/// <summary>
Expand Down

0 comments on commit b27a734

Please sign in to comment.