Files
Studio/Assets/TMPFolder/Panel_DataRepository.cs
2025-05-25 13:24:48 +09:00

215 lines
7.5 KiB
C#

using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
using Studio;
using Studio.Setting.Connect;
using Studio.UI;
using TMPro;
using UnityEngine;
using XRLib.UI;
using Newtonsoft.Json;
using Best.HTTP.JSON;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
namespace Studio
{
public class Panel_DataRepository : PanelBase
{
UI_BaseDataButton apiDataButtonPrefab;
UI_MQTTDataButton mqttDataButtonPrefab;
Button CloseButton;
RectTransform APIDataList;
RectTransform MQTTDataList;
RectTransform Panel_APIInfo;
RectTransform Panel_APIDataInfo;
RectTransform Panel_MQTTInfo;
RectTransform Panel_MQTTDataInfo;
TextMeshProUGUI TotalRequestPacketSize;
TextMeshProUGUI AverageRequestPacketSize;
TextMeshProUGUI TotalResponsePacketSize;
TextMeshProUGUI AverageResponsePacketSize;
TextMeshProUGUI MaximumResponseTime;
TextMeshProUGUI AverageResponseTime;
TextMeshProUGUI MaximumRequestPacketAPI;
TextMeshProUGUI MaximumResponsePacketAPI;
TextMeshProUGUI MaximumResponseTimeAPI;
LayoutGroup[] layoutGroups;
Dictionary<string, UI_BaseDataButton> apiButtons = new();
Dictionary<string, UI_MQTTDataButton> mqttButtons = new();
//public bool apiConnected;
//public bool mqttConnected;
public override void AfterAwake()
{
UI_ProtocolLabel[] protocolLabels = GetComponentsInChildren<UI_ProtocolLabel>();
foreach (UI_ProtocolLabel label in protocolLabels)
{
label.panel_Repository = this;
}
apiDataButtonPrefab = Resources.Load<UI_BaseDataButton>("Prefabs/UI/Button/UI_BaseDataButton");
mqttDataButtonPrefab = Resources.Load<UI_MQTTDataButton>("Prefabs/UI/Button/UI_MQTTDataButton");
CloseButton.onClick.AddListener(Deactivate);
layoutGroups = GetComponentsInChildren<LayoutGroup>();
}
private void OnEnable()
{
gameObject.transform.SetAsLastSibling();
StudioService studioService = StudioService.instance;
studioService.onAPIDataLoaded += UpdateAPIDataButton;
studioService.onMQTTDataLoaded += UpdateMQTTDataButton;
foreach (string key in studioService.apiData.Keys)
{
UpdateAPIDataButton(key, studioService.apiData[key]);
}
foreach (string key in studioService.mqttData.Keys)
{
UpdateMQTTDataButton(key, studioService.mqttData[key].Item1, studioService.mqttData[key].Item2);
}
/*
if (apiConnected)
{
StudioService studioService = StudioService.Instance;
studioService.onAPIDataLoaded += UpdateAPIDataButton;
foreach (string key in studioService.apiData.Keys)
{
UpdateAPIDataButton(key, studioService.apiData[key]);
}
}
if (mqttConnected)
{
StudioService studioService = StudioService.Instance;
studioService.onMQTTDataLoaded += UpdateMQTTDataButton;
foreach (string key in studioService.mqttData.Keys)
{
UpdateMQTTDataButton(key, studioService.mqttData[key].Item1, studioService.mqttData[key].Item2);
}
}
*/
}
private void OnDisable()
{
StudioService.instance.onAPIDataLoaded -= UpdateAPIDataButton;
StudioService.instance.onMQTTDataLoaded -= UpdateMQTTDataButton;
}
void UpdateAPIDataButton<T>(string name, StudioEntityWithState<T> value)
{
if (!apiButtons.ContainsKey(name))
{
// 버튼 새로 생성
var button = Instantiate(apiDataButtonPrefab, APIDataList.transform);
apiButtons.Add(name, button);
button.panel_Repository = this;
button.SetButtonData(name, value);
RebuildLayout();
}
else
{
// 버튼 데이터 업데이트
apiButtons[name].SetButtonData<T>(name, value);
}
}
void UpdateMQTTDataButton(string name, Dictionary<string, Dictionary<string, string>> value, TimeSpan elapsedTime)
{
if (!mqttButtons.ContainsKey(name))
{
var button = Instantiate(mqttDataButtonPrefab, MQTTDataList.transform);
mqttButtons.Add(name, button);
button.panel_Repository = this;
button.SetButtonData(name, value, elapsedTime);
RebuildLayout();
}
else
{
mqttButtons[name].SetButtonData(name, value, elapsedTime);
}
}
public void RebuildLayout()
{
foreach (var layout in layoutGroups)
{
LayoutRebuilder.ForceRebuildLayoutImmediate(layout.GetComponent<RectTransform>());
}
}
public void ShowInformation_API()
{
APITotalBoardEntity info = StudioService.instance.GetAPIStatusBoarder();
TotalRequestPacketSize.text = info.TotalRequestPacketSize.ToString();
AverageRequestPacketSize.text = info.AverageRequestPacketSize.ToString();
TotalResponsePacketSize.text = info.TotalResponsePacketSize.ToString();
AverageResponsePacketSize.text = info.AverageResponsePacketSize.ToString();
MaximumResponseTime.text = info.MaximumResponseTime.ToString();
AverageResponseTime.text = info.AverageResponseTime.ToString();
MaximumRequestPacketAPI.text = info.MaximumRequestPacketAPI;
MaximumResponsePacketAPI.text = info.MaximumResponsePacketAPI;
MaximumResponseTimeAPI.text = info.MaximumResponseTimeAPI;
Panel_APIInfo.gameObject.SetActive(true);
Panel_APIDataInfo.gameObject.SetActive(false);
Panel_MQTTInfo.gameObject.SetActive(false);
Panel_MQTTDataInfo.gameObject.SetActive(false);
}
public void ShowInformation_APIData<T>(StudioEntityWithState<T> baseDataValue)
{
Panel_APIDataInfo.GetComponent<Panel_APIDataInfo>().ShowData(baseDataValue);
Panel_APIInfo.gameObject.SetActive(false);
Panel_APIDataInfo.gameObject.SetActive(true);
Panel_MQTTInfo.gameObject.SetActive(false);
Panel_MQTTDataInfo.gameObject.SetActive(false);
}
public void ShowInformation_MQTT()
{
Panel_APIInfo.gameObject.SetActive(false);
Panel_APIDataInfo.gameObject.SetActive(false);
Panel_MQTTInfo.gameObject.SetActive(true);
Panel_MQTTDataInfo.gameObject.SetActive(false);
}
public void ShowInformation_MQTTData(Dictionary<string, Dictionary<string, string>> data, TimeSpan elapsedTime)
{
Panel_MQTTDataInfo.GetComponent<Panel_MQTTDataInfo>().ShowData(data, elapsedTime);
Panel_APIInfo.gameObject.SetActive(false);
Panel_APIDataInfo.gameObject.SetActive(false);
Panel_MQTTInfo.gameObject.SetActive(false);
Panel_MQTTDataInfo.gameObject.SetActive(true);
}
void Deactivate()
{
gameObject.SetActive(false);
}
}
}