Files
Simulation/Assets/WorkSpace/LH/Panel_SimulationUI.cs
2025-07-07 09:57:13 +09:00

232 lines
8.3 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
using Octopus.Simulator.Networks;
using Newtonsoft.Json;
using TMPro;
using UVC.Networks;
namespace Octopus.Simulator
{
public class Panel_SimulationUI : MonoBehaviour
{
WebManager webmanager;
Button Button_Play;
Button Button_FastForward;
Button Button_Logic;
Button Button_Complete;
TMP_Text text_FastForward;
List<int> fastParam = new List<int> { 1, 2, 3, 5, 10 };
int currentFastIndex = 4;
public Sprite playing;
public Sprite pause;
public bool isplaying = false;
public event Action<bool> onPlaying;
public event Action onLogicBTNClicked;
private void Awake()
{
Button_Play = transform.Find(nameof(Button_Play)).GetComponent<Button>();
Button_FastForward = transform.Find(nameof(Button_FastForward)).GetComponent<Button>();
Button_Logic = transform.Find(nameof(Button_Logic)).GetComponent<Button>();
Button_Complete = transform.Find(nameof(Button_Complete)).GetComponent<Button>();
text_FastForward = Button_FastForward.GetComponentInChildren<TMP_Text>();
onPlaying += onPlay;
}
private void Start()
{
webmanager = WebManager.Instance;
Button_Play.onClick.AddListener(OnClickPlayBTN);
Button_FastForward.onClick.AddListener(OnclickFastForwardBTN);
Button_Logic.onClick.AddListener(OnclickLogicBTN);
Button_Complete.onClick.AddListener(OnClickCompleteBTN);
}
void OnClickPlayBTN()
{
HistoryParameters param = new HistoryParameters();
param.projectId = int.Parse(WebParameters.config.projectId);
param.logicId = int.Parse(WebParameters.config.logicId);
param.logicData = null;
param.name = "simualtion";
string requestAPI = $"{webmanager.apiConfig.history}";
//Request_SimulationCreate(param, requestAPI);
if (isplaying)
{
isplaying = false;
Button_Play.image.sprite = pause;
Button_FastForward.interactable = true;
onPlaying?.Invoke(isplaying);
var requestURL = $"{requestAPI}/{WebParameters.id}/pause";
webmanager.Reqeust<HistoryParameters>(requestURL, RequestType.POST, null);
//Request_SimulationPause(null, $"{requestAPI}/{WebParameters.id}/pause");
}
else
{
isplaying = true;
Button_Play.image.sprite = playing;
//Button_FastForward.interactable = false;
onPlaying?.Invoke(isplaying);
webmanager.Reqeust<SimulationData>(requestAPI, RequestType.POST, SimulationCreate, param);
//Request_SimulationCreate(param, requestAPI);
}
}
//void Request_SimulationCreate(HistoryParameters param, string api)
//{
// webmanager.Request_Post(param, api, (flag, value) =>
// {
// if (flag)
// {
// SimulatorPostClass info = JsonConvert.DeserializeObject<SimulatorPostClass>(value);
// WebParameters.id = (int)info.data.insertedId;
// Application.ExternalCall("setSimulationID", WebParameters.id);
// Debug.Log("Call external setSimulationID" + WebParameters.id.ToString());
// Get_Simulation();
// }
// });
//}
//void Request_SimulationPause(HistoryParameters param, string api)
//{
// webmanager.Request_Post(param, api, (flag, value) =>
// {
// if (flag)
// {
// }
// });
//}
void SimulationCreate(SimulationData response)
{
WebParameters.id = (int)response.insertedId;
Application.ExternalCall("setSimulationID", WebParameters.id);
Debug.Log("Call external setSimulationID" + WebParameters.id.ToString());
Get_Simulation();
}
void Get_Simulation()
{
var reqeustURL = $"{webmanager.apiConfig.history}/{WebParameters.id}";
//webmanager.Request_Get(reqeustURL, (flag, value) =>
//{
// if (flag)
// {
// SimulatorGetClass info = JsonConvert.DeserializeObject<SimulatorGetClass>(value);
// WebParameters.code = info.data.simulationCode;
// SetMqttConnect();
// }
//});
WebManager.Instance.Reqeust<SimulatorGetData>(reqeustURL, RequestType.GET, GetSimulationCallback);
}
public void GetSimulationCallback(SimulatorGetData response)
{
WebParameters.code = response.simulationCode;
SetMqttConnect();
}
void SetMqttConnect()
{
MQTTManager.mqttManager.SubscribeTopic($"simulation/{WebParameters.code}/#");
Request_SimulationStart();
}
void Request_SimulationStart()
{
paramclass param = new paramclass();
param.parameters.duration = 3600;
param.parameters.real_time = true;
string requestAPI = $"{webmanager.apiConfig.history}/{WebParameters.id}/start";
webmanager.Reqeust<UpdatedSimulationsInfo>(requestAPI, RequestType.POST, null, param);
//webmanager.Request_Post(param, requestAPI, (flag, value) =>
//{
// if (flag)
// {
// }
//});
}
void OnclickFastForwardBTN()
{
if (currentFastIndex++ >= fastParam.Count - 1)
{
currentFastIndex = 0;
}
WebParameters.speed = fastParam[currentFastIndex];
text_FastForward.text = $"x{fastParam[currentFastIndex]}";
}
void OnclickLogicBTN()
{
onLogicBTNClicked?.Invoke();
}
void OnClickCompleteBTN()
{
Request_SimulationComplete();
}
void Request_SimulationComplete()
{
var requestURL = $"{webmanager.apiConfig.history}/{WebParameters.id}/pause";
webmanager.Reqeust<UpdatedSimulationsInfo>(requestURL, RequestType.POST, PauseCallback);
//webmanager.Request_Post(null, requestURL, (flag, value) =>
//{
// if (flag)
// {
// requestURL = $"{webmanager.apiConfig.history}/{WebParameters.id}/toggle-realtime";
// RealTime rt = new RealTime();
// rt.realTime = false;
// webmanager.Reqeust<RealTime>(requestURL, RequestType.POST, ToggleRealTimeCallback, rt);
// //webmanager.Request_Post(rt, requestURL, (flag, value) =>
// //{
// // if (flag)
// // {
// // var reqeustURL = $"{webmanager.apiConfig.history}/{WebParameters.id}/resume";
// // emptyClass ec = new emptyClass();
// // //webmanager.Request_Post(ec, reqeustURL, (flag, value) => { });
// // webmanager.Reqeust<emptyClass>(reqeustURL, RequestType.POST, null);
// // }
// //});
// }
//});
}
void onPlay(bool flag)
{
Button_FastForward.interactable = !flag;
}
public void PauseCallback(UpdatedSimulationsInfo response)
{
var requsetURL = $"{webmanager.apiConfig.history}/{WebParameters.id}/toggle-realtime";
RealTime rt = new RealTime();
rt.realTime = false;
webmanager.Reqeust<RealTime>(requsetURL, RequestType.POST, ToggleRealTimeCallback, rt);
}
public void ToggleRealTimeCallback(RealTime response)
{
var reqeustURL = $"{webmanager.apiConfig.history}/{WebParameters.id}/resume";
emptyClass ec = new emptyClass();
webmanager.Reqeust<emptyClass>(reqeustURL, RequestType.POST, null);
}
}
}