112 lines
3.6 KiB
C#
112 lines
3.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Octopus.Simulator.Networks;
|
|
using Newtonsoft.Json;
|
|
using TMPro;
|
|
|
|
namespace Octopus.Simulator
|
|
{
|
|
public class Panel_SimulationUI : MonoBehaviour
|
|
{
|
|
WebManager webmanager;
|
|
Button Button_Play;
|
|
Button Button_FastForward;
|
|
Button Button_Logic;
|
|
TMP_Text text_FastForward;
|
|
List<int> fastParam = new List<int> { 1, 2, 3, 5, 10 };
|
|
int currentFastIndex = 0;
|
|
|
|
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>();
|
|
text_FastForward = Button_FastForward.GetComponentInChildren<TMP_Text>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
webmanager = WebManager.webManager;
|
|
Button_Play.onClick.AddListener(OnClickPlayBTN);
|
|
Button_FastForward.onClick.AddListener(OnclickFastForwardBTN);
|
|
Button_Logic.onClick.AddListener(OnclickLogicBTN);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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.Parse(info.data.insertedId);
|
|
Get_Simulation();
|
|
}
|
|
});
|
|
}
|
|
|
|
void Get_Simulation()
|
|
{
|
|
string api = $"{ webmanager.apiConfig.history}/{WebParameters.id}";
|
|
webmanager.Request_Get( api, (flag, value) =>
|
|
{
|
|
if (flag)
|
|
{
|
|
SimulatorGetClass info = JsonConvert.DeserializeObject<SimulatorGetClass>(value);
|
|
WebParameters.code = info.data.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.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();
|
|
}
|
|
}
|
|
} |