267 lines
9.7 KiB
C#
267 lines
9.7 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;
|
|
using Best.MQTT;
|
|
using Best.MQTT.Packets;
|
|
using RTG;
|
|
using System.Collections;
|
|
using Octopus.Simulator.UI.Popup;
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
namespace Octopus.Simulator
|
|
{
|
|
public class Panel_SimulationUI : MonoBehaviour
|
|
{
|
|
WebManager webmanager;
|
|
Button Button_Play;
|
|
Button Button_FastForward;
|
|
Button Button_Logic;
|
|
Button Button_Reset;
|
|
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;
|
|
bool isCreate = false;
|
|
public event Action onLogicBTNClicked;
|
|
|
|
Popup_Reset popupReset;
|
|
|
|
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_Reset = transform.Find(nameof(Button_Reset)).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;
|
|
WebParameters.speed = 10;
|
|
Button_Play.onClick.AddListener(OnClickPlayBTN);
|
|
Button_FastForward.onClick.AddListener(OnclickFastForwardBTN);
|
|
Button_Logic.onClick.AddListener(OnclickLogicBTN);
|
|
Button_Reset.onClick.AddListener(OnClickReset);
|
|
Button_Complete.onClick.AddListener(OnClickCompleteBTN);
|
|
popupReset = FindAnyObjectByType<Popup_Reset>(FindObjectsInactive.Include);
|
|
MQTTManager.mqttManager.onConnected += ReConnect;
|
|
FindAnyObjectByType<WebReceiver>().onCompleteWindowReceived += ResumeCallBack;
|
|
}
|
|
|
|
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";
|
|
param.speed = WebParameters.speed;
|
|
string requestAPI = $"{webmanager.apiConfig.history}";
|
|
if (isplaying)
|
|
{
|
|
isplaying = false;
|
|
Button_Play.image.sprite = pause;
|
|
onPlaying?.Invoke(isplaying);
|
|
RTFocusCamera.Get.SaveCameraData();
|
|
var requestURL = $"{requestAPI}/{WebParameters.id}/pause";
|
|
webmanager.Reqeust<HistoryParameters>(requestURL, RequestType.POST, null);
|
|
}
|
|
else
|
|
{
|
|
var unassingedList = LogicUIManager.instance.GetUnassignedList();
|
|
if(!string.IsNullOrEmpty(unassingedList))
|
|
{
|
|
AlertManager.I.ShowAlert("오브젝트 할당 필요", $"{unassingedList} \n해당 오브젝트 목록을 할당해 주세요").Forget();
|
|
return;
|
|
}
|
|
isplaying = true;
|
|
Button_Play.image.sprite = playing;
|
|
onPlaying?.Invoke(isplaying);
|
|
if (!isCreate)
|
|
{
|
|
webmanager.Reqeust<SimulationData>(requestAPI, RequestType.POST, SimulationCreate, param);
|
|
}
|
|
else
|
|
{
|
|
ResumeSimulation();
|
|
}
|
|
}
|
|
}
|
|
|
|
void SimulationCreate(SimulationData response)
|
|
{
|
|
isCreate = true;
|
|
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.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;
|
|
param.parameters.speed = WebParameters.speed;
|
|
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";
|
|
onComplete();
|
|
FindAnyObjectByType<Popup_SimulatorLoading>(FindObjectsInactive.Include).gameObject.SetActive(true);
|
|
StartCoroutine(FailGetSimulationComplete());
|
|
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;
|
|
Button_Reset.interactable = true;
|
|
Button_Complete.interactable = true;
|
|
}
|
|
|
|
void onComplete()
|
|
{
|
|
Button_Play.interactable = false;
|
|
Button_FastForward.interactable = false;
|
|
Button_Complete.interactable = false;
|
|
Button_Reset.interactable = true;
|
|
}
|
|
|
|
public void PauseCallback(UpdatedSimulationsInfo response)
|
|
{
|
|
isplaying = false;
|
|
var requsetURL = $"{webmanager.apiConfig.history}/{WebParameters.id}/toggle-realtime";
|
|
RealTime rt = new RealTime();
|
|
rt.realTime = false;
|
|
webmanager.Reqeust<RealTime>(requsetURL, RequestType.POST, (value) => ResumeSimulation(), rt);
|
|
}
|
|
|
|
public void ResumeSimulation()
|
|
{
|
|
var reqeustURL = $"{webmanager.apiConfig.history}/{WebParameters.id}/resume";
|
|
emptyClass ec = new emptyClass();
|
|
webmanager.Reqeust<emptyClass>(reqeustURL, RequestType.POST, null, ec);
|
|
}
|
|
|
|
void ResumeCallBack()
|
|
{
|
|
FindAnyObjectByType<Popup_SimulatorLoading>(FindObjectsInactive.Include).gameObject.SetActive(false);
|
|
StopAllCoroutines();
|
|
FindAnyObjectByType<Popup_SimulationLoadingFail>(FindObjectsInactive.Include).gameObject.SetActive(false);
|
|
isplaying = false;
|
|
onComplete();
|
|
}
|
|
|
|
IEnumerator FailGetSimulationComplete()
|
|
{
|
|
yield return new WaitForSeconds(20f);
|
|
FindAnyObjectByType<Popup_SimulatorLoading>(FindObjectsInactive.Include).gameObject.SetActive(false);
|
|
FindAnyObjectByType<Popup_SimulationLoadingFail>(FindObjectsInactive.Include).gameObject.SetActive(true);
|
|
}
|
|
|
|
//MQTT 재연결
|
|
private void ReConnect(MQTTClient client)
|
|
{
|
|
if (isplaying)
|
|
SetMqttConnect();
|
|
else
|
|
{
|
|
if (string.IsNullOrEmpty(WebParameters.code))
|
|
return;
|
|
MQTTManager.mqttManager.SubscribeTopic($"simulation/{WebParameters.code}/#");
|
|
}
|
|
}
|
|
|
|
public void OnClickReset()
|
|
{
|
|
popupReset.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
} |