130 lines
4.6 KiB
C#
130 lines
4.6 KiB
C#
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UVC.Core;
|
|
using UVC.Data;
|
|
using UVC.Data.Core;
|
|
using UVC.Data.Mqtt;
|
|
using Simulator.Config;
|
|
|
|
namespace Simulator
|
|
{
|
|
public class SimulationSettings : SingletonScene<SimulationSettings>
|
|
{
|
|
[SerializeField]
|
|
TMP_Text text_SimulationName;
|
|
[SerializeField]
|
|
TMP_Text text_SimulationSpeed;
|
|
[SerializeField]
|
|
TMP_Text text_SimulationTime;
|
|
[SerializeField]
|
|
Image ProgressBar;
|
|
[SerializeField]
|
|
TMP_Text ProgressText;
|
|
[SerializeField]
|
|
TMP_Text SimulationTimer;
|
|
[SerializeField]
|
|
RectTransform rect;
|
|
[SerializeField]
|
|
GameObject SimulationSetting;
|
|
[SerializeField]
|
|
GameObject SimulationTime;
|
|
[SerializeField]
|
|
GameObject SimulationProgressBar;
|
|
|
|
Action<string> onSimulationCodeSetHandler;
|
|
|
|
protected override void Init()
|
|
{
|
|
var rackDataMask = new DataMask();
|
|
rackDataMask.ObjectName = "progress";
|
|
rackDataMask.ObjectIdKey = "simulation_id";
|
|
rackDataMask["simulation_id"] = "";
|
|
rackDataMask["timestamp"] = new DateTime();
|
|
rackDataMask["data"] = new DataMask()
|
|
{
|
|
["progress"] = new DataMask()
|
|
{
|
|
["current_time"] = 0f,
|
|
["total_time"] = 0f,
|
|
["percentage"] = 0f,
|
|
["status"] = ""
|
|
}
|
|
};
|
|
|
|
DataMapper progressmapper = new DataMapper(rackDataMask);
|
|
onSimulationCodeSetHandler = (value) => GetSimulationSettings(value, progressmapper);
|
|
SimulationConfig.onSimulationCodeSet += onSimulationCodeSetHandler;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
SimulationConfig.onSimulationCodeSet -= onSimulationCodeSetHandler;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(rect);
|
|
this.gameObject.SetActive(false);
|
|
SimulationSetting.SetActive(false);
|
|
SimulationTime.SetActive(false);
|
|
SimulationProgressBar.SetActive(false);
|
|
|
|
}
|
|
|
|
public void GetSimulationSettings(string simulationCode, DataMapper mapper)
|
|
{
|
|
DataRepository.Instance.MqttReceiver.AddTopic($"simulation/{simulationCode}/progress_updated");
|
|
var mqttConfig = new MqttSubscriptionConfig($"simulation/{simulationCode}/progress_updated");
|
|
mqttConfig.SetDataMapper(mapper);
|
|
mqttConfig.SetHandler(SetSimulationSettings);
|
|
DataRepository.Instance.MqttReceiver.Add(mqttConfig);
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(rect);
|
|
}
|
|
|
|
public void SetSimulationSettings(IDataObject data)
|
|
{
|
|
if (data == null) return;
|
|
|
|
DataObject? obj = data as DataObject;
|
|
if (obj == null) return;
|
|
|
|
text_SimulationName.text = obj.GetString("simulation_id");
|
|
var progress = obj.GetDataObject("progress");
|
|
if (progress == null) return;
|
|
var percentage = (float)progress.GetFloat("percentage");
|
|
ProgressBar.fillAmount = percentage / 100f;
|
|
ProgressText.text = $"{percentage.ToString("#")}%";
|
|
var currentTime = (float)progress.GetFloat("current_time");
|
|
var totalTime = (float)progress.GetFloat("total_time");
|
|
SetSimulationTimer(currentTime, totalTime);
|
|
SetSimulationConfig();
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(rect);
|
|
this.gameObject.SetActive(true);
|
|
SimulationSetting.SetActive(true);
|
|
SimulationTime.SetActive(true);
|
|
SimulationProgressBar.SetActive(true);
|
|
}
|
|
|
|
public void SetSimulationConfig()
|
|
{
|
|
text_SimulationSpeed.text = $"{SimulationConfig.param.speed}X";
|
|
text_SimulationTime.text = $"{ToHHMMSS(SimulationConfig.param.duration)}";
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(rect);
|
|
}
|
|
|
|
public void SetSimulationTimer(float current, float total)
|
|
{
|
|
SimulationTimer.text = $"{ToHHMMSS(current)}/{ToHHMMSS(total)}";
|
|
}
|
|
|
|
public string ToHHMMSS(float timeInSeconds)
|
|
{
|
|
int hours = Mathf.FloorToInt(timeInSeconds / 3600);
|
|
int minutes = Mathf.FloorToInt((timeInSeconds % 3600) / 60);
|
|
int seconds = Mathf.FloorToInt(timeInSeconds % 60);
|
|
return string.Format("{0:D2}:{1:D2}:{2:D2}", hours, minutes, seconds);
|
|
}
|
|
}
|
|
} |