Skip to content
Snippets Groups Projects
ProtocolHandler.cs 5.65 KiB
Newer Older
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class ProtocolHandler : MonoBehaviour
{
rchia16's avatar
rchia16 committed
    public static ProtocolHandler Instance;
    public GameObject numCyclesInput;
    public GameObject blockControl;
Raymond Chia's avatar
Raymond Chia committed
    public GameObject centreCircle;
    public Canvas canvas;
    public GameObject countdownObject;
    public float TotalRuntime { get; private set; }
Raymond Chia's avatar
Raymond Chia committed
    public bool runProtocol = false; // not running
    [SerializeField] public List<EventData> eventDataList = new List<EventData>();

    BlockHandler blockHandler;
    List<GameObject> blockStack = new List<GameObject>();
Raymond Chia's avatar
Raymond Chia committed
    AnimateCircle animateCircle;
    RunCountdown runCountdown;
    DisplayProgress displayProgress;

    EventLogClass eventLogClass;

    int nCycles = 1;
    int currentBlockIndex = 0;
Raymond Chia's avatar
Raymond Chia committed
    int previousBlockIndex = -1;
    int currentCycle = 0;
    bool isStart = false;
rchia16's avatar
rchia16 committed

    private void Awake()
    {
        if (Instance != null)
        {
            Destroy(gameObject);
            return;
        }
        Instance = this;
        DontDestroyOnLoad(gameObject);
    }

    // Start is called before the first frame update
    void Start()
    {
Raymond Chia's avatar
Raymond Chia committed
        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++;

rchia16's avatar
rchia16 committed
            runProtocol = true;
            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;
rchia16's avatar
rchia16 committed
                runProtocol = false;
Raymond Chia's avatar
Raymond Chia committed
        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());
Raymond Chia's avatar
Raymond Chia committed
            runProtocol = false;
            // log block start
            if (!isStart)
            {
                eventDataList.Add(LogEvent("Start"));
            }
Raymond Chia's avatar
Raymond Chia committed
        }
        else if (previousBlockIndex >= 0 && !animateCircle.enabled && 
            currentBlockIndex < blockStack.Count)
        {
            // log block end
            // log block start
            if (isStart)
            {
                eventDataList.Add(LogEvent("Stop"));
            }
Raymond Chia's avatar
Raymond Chia committed
            runCountdown.enabled = true;
        }

    }

    // Update runtime by adding each block runtime, adding 3s for each block,
    // and multiplying by cycles
    public void UpdateTotalRuntime()
    {
Raymond Chia's avatar
Raymond Chia committed
        TotalRuntime = blockHandler.blockStackRuntime / 60;
        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++;
        }
    }
rchia16's avatar
rchia16 committed

    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;
    }


Raymond Chia's avatar
Raymond Chia committed
    void RunBlock(int blockIndex)
    {
        blockHandler.SetRunningBlockIndex(blockIndex);
        blockHandler.SetRunningBlock();
        blockHandler.UpdateRunningBlock();
        animateCircle.ResetAndRun();
    }


    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;
    }