Newer
Older
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ProtocolHandler : MonoBehaviour
{
public GameObject numCyclesInput;
public GameObject blockControl;
public GameObject centreCircle;
public Canvas canvas;
public GameObject countdownObject;
public float TotalRuntime { get; private set; }
[SerializeField] public List<EventData> eventDataList = new List<EventData>();
BlockHandler blockHandler;
List<GameObject> blockStack = new List<GameObject>();
AnimateCircle animateCircle;
RunCountdown runCountdown;
DisplayProgress displayProgress;
EventLogClass eventLogClass;
int nCycles = 1;
int currentBlockIndex = 0;
private void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
}
// Start is called before the first frame update
void Start()
{
blockHandler = blockControl.GetComponent<BlockHandler>();
animateCircle = centreCircle.GetComponent<AnimateCircle>();
runCountdown = countdownObject.GetComponent<RunCountdown>();
displayProgress = GameObject.Find("ProgressObject").GetComponent<DisplayProgress>();
eventLogClass = GetComponent<EventLogClass>();
}
// Update is called once per frame
void Update()
{
if (currentBlockIndex == blockStack.Count && !animateCircle.enabled && previousBlockIndex != -1)
{
if (isStart)
{
eventDataList.Add(LogEvent("Stop"));
}
currentCycle++;
animateCircle.enabled = false;
currentBlockIndex = 0;
previousBlockIndex = -1;
if (currentCycle == nCycles)
{
// export data
eventLogClass.eventDataList = eventDataList;
eventLogClass.SaveToJson();
// reset
currentCycle = 0;
animateCircle.MoveToOffset();
canvas.enabled = true;
displayProgress.enabled = false;
if (runProtocol && !animateCircle.enabled)
{
// if it's time to run and animation not running, then set to run and iterate
RunBlock(currentBlockIndex);
previousBlockIndex = currentBlockIndex;
currentBlockIndex++;
// Debug.Log("Finished " + currentBlockIndex + " / " + blockStack.Count.ToString());
// log block start
if (!isStart)
{
eventDataList.Add(LogEvent("Start"));
}
}
else if (previousBlockIndex >= 0 && !animateCircle.enabled &&
currentBlockIndex < blockStack.Count)
{
// log block end
// log block start
if (isStart)
{
eventDataList.Add(LogEvent("Stop"));
}
}
// Update runtime by adding each block runtime, adding 3s for each block,
// and multiplying by cycles
public void UpdateTotalRuntime()
{
TotalRuntime *= nCycles;
}
public void UpdateNumCycles()
{
TMP_InputField inputField = numCyclesInput.GetComponent<TMP_InputField>();
nCycles = (int)Convert.ToInt32(inputField.text);
}
public void SetBlockStack()
{
blockStack = blockHandler.GetBlockStack();
int i = 0;
foreach (var block in blockStack)
{
BlockObjectClass boc = block.GetComponent<BlockObjectClass>();
i++;
}
}
public void PrintBlockStack()
{
int i = 0;
foreach (var block in blockStack)
{
BlockObjectClass boc = block.GetComponent<BlockObjectClass>();
Debug.Log("boc " + i + " inhale: " + boc.InhalePeriod);
i++;
}
}
public int GetNumCycles()
{
return nCycles;
}
public int GetCurrentBlockIndex()
{
return currentBlockIndex;
}
public int GetBlockStackLen()
{
return blockStack.Count;
}
public int GetCurrentCycle()
{
return currentCycle;
}
void RunBlock(int blockIndex)
{
blockHandler.SetRunningBlockIndex(blockIndex);
blockHandler.SetRunningBlock();
blockHandler.UpdateRunningBlock();
animateCircle.ResetAndRun();
}
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
EventData LogEvent(string eventTag)
{
if (eventTag == "Start")
{
Debug.Log("Logging start");
isStart = true;
}
else if (eventTag == "Stop")
{
isStart = false;
Debug.Log("Logging stop");
}
EventData eventData = new EventData();
BlockObjectClass boc = blockStack[currentBlockIndex-1]
.GetComponent<BlockObjectClass>();
eventData.timestamp = DateTime.Now.ToString();
eventData.inhalePeriod = boc.InhalePeriod.ToString();
eventData.exhalePeriod = boc.ExhalePeriod.ToString();
eventData.eventTag = eventTag;
//ed.timestamp = DateTime.Now.ToString();
//ed.inhalePeriod = rr.ToString();
//ed.exhalePeriod = rr.ToString();
//ed.eventTag = "Start";
return eventData;
}