378 lines
9.8 KiB
C#
378 lines
9.8 KiB
C#
using AutoMapper;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using Unity.VisualScripting;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using static GameManager;
|
||
/// <summary>
|
||
/// 端口-新增
|
||
/// </summary>
|
||
public class AddPort : MonoBehaviour
|
||
{
|
||
public Root URlreturn;
|
||
public Body mybody = new Body();
|
||
public Button save_bt;
|
||
|
||
|
||
public InputField port;
|
||
|
||
public InputField portCode;
|
||
|
||
public InputField portName;
|
||
|
||
public InputField portType;
|
||
|
||
//public Dropdown deviceId;
|
||
|
||
//public InputField portModel;
|
||
|
||
public Dropdown status;
|
||
|
||
public Dropdown conDevice;
|
||
|
||
public Dropdown conPort;
|
||
|
||
public InputField cableName;
|
||
|
||
public Dropdown cableGroupName;
|
||
|
||
//MapperConfiguration config;
|
||
//IMapper mapper;
|
||
PortQuery.PortList portlist;
|
||
private void OnEnable()
|
||
{
|
||
init();
|
||
//config = new MapperConfiguration(cfg =>
|
||
//{
|
||
// cfg.CreateMap<AddPort.Body, PortQuery.PortList>();
|
||
// cfg.CreateMap<PortQuery.PortList, AddPort.Body>();
|
||
//});
|
||
//mapper = config.CreateMapper();
|
||
try
|
||
{
|
||
if (!string.IsNullOrEmpty(GameManager.Inst.nowDeviceID))
|
||
{
|
||
for (int i = 0; i < GameManager.Inst.TmsPorts.Count; i++)
|
||
{
|
||
if (GameManager.Inst.TmsPorts[i].id == GameManager.Inst.nowDeviceID)
|
||
{
|
||
portlist = GameManager.Inst.TmsPorts[i];
|
||
break;
|
||
}
|
||
}
|
||
if (portlist != null)
|
||
{
|
||
// mybody = mapper.Map<AddPort.Body>(portlist);
|
||
}
|
||
}
|
||
//else
|
||
//{
|
||
// mybody = mapper.Map<AddPort.Body>(GameManager.Inst.nowDevice.GetComponent<PortQuery>().portList);
|
||
//}
|
||
mybody.deviceId = transform.parent.GetComponent<DeviceQuery>().deviceList.id;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.Log(e.StackTrace + "**********" + e.Message);
|
||
}
|
||
}
|
||
|
||
private void init()
|
||
{
|
||
port.text = null;
|
||
|
||
portCode.text = null;
|
||
|
||
portName.text = null;
|
||
|
||
portType.text = null;
|
||
|
||
status.value = 1;
|
||
|
||
conDevice.options.Clear();
|
||
|
||
conPort.options.Clear();
|
||
|
||
cableGroupName.options.Clear();
|
||
foreach (var item in LineQuery.Inst.keyValues.Keys)
|
||
{
|
||
Dropdown.OptionData optionData = new Dropdown.OptionData(item);
|
||
cableGroupName.options.Add(optionData);
|
||
}
|
||
|
||
//deviceId.options.Clear();
|
||
List<string> deviceId_ops = new List<string>();
|
||
deviceId_ops.Add("");
|
||
|
||
var list = Array.FindAll(GameObject.FindObjectsOfType<PortQuery>(), itme =>
|
||
{
|
||
var n = itme.transform.parent.GetComponent<DeviceQuery>().deviceList;
|
||
if (!string.IsNullOrEmpty(itme.portList.id) && n != null)
|
||
return true;
|
||
return false;
|
||
}).ToList();
|
||
|
||
Dictionary<string, string> dic = new Dictionary<string, string>();
|
||
|
||
|
||
|
||
for (int i = 0; i < list.Count; i++)
|
||
{
|
||
string s = list[i].transform.parent.GetComponent<DeviceQuery>().deviceList.id;
|
||
if (!dic.ContainsKey(s))
|
||
{
|
||
dic.Add(s, null);
|
||
deviceId_ops.Add(s);
|
||
}
|
||
}
|
||
//deviceId.AddOptions(deviceId_ops);
|
||
conDevice.AddOptions(deviceId_ops);
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
//deviceId.onValueChanged.AddListener(deviceId_fuc);//所属设备
|
||
conDevice.onValueChanged.AddListener(conDevice_fuc);//对联设备
|
||
save_bt.onClick.AddListener(() =>
|
||
{
|
||
GameManager.Inst.editorMenu.gameObject.SetActive(false);
|
||
|
||
StartCoroutine(SaveJsonCoroutine());
|
||
});
|
||
}
|
||
|
||
private IEnumerator SaveJsonCoroutine()
|
||
{
|
||
yield return StartCoroutine(saveJson((x) =>
|
||
{
|
||
gameObject.SetActive(x);
|
||
return;
|
||
}));
|
||
|
||
if (URlreturn != null && URlreturn.message == "操作成功")
|
||
{
|
||
|
||
StartCoroutine(Succeed((x) =>
|
||
{
|
||
if (x)
|
||
{
|
||
GameManager.Inst.lastDeviceID = URlreturn.data;
|
||
Array.ForEach(GameManager.Inst.pop_ups.ToArray(), (itme) =>
|
||
{
|
||
itme.gameObject.SetActive(false);
|
||
});
|
||
}
|
||
}));
|
||
|
||
}
|
||
else
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
public IEnumerator Succeed(Action<bool> callback)
|
||
{
|
||
GameManager.Inst.isLoading = false;
|
||
|
||
//TransparentGlowManage.Inst.addScript();
|
||
|
||
//yield return StartCoroutine(GameManager.Inst.init_Device());
|
||
|
||
//yield return StartCoroutine(GameManager.Inst.init_Port());
|
||
|
||
|
||
//yield return StartCoroutine(LineQuery.Inst.getJsonCoroutine());
|
||
|
||
//GameManager.Inst.LoadScene();
|
||
|
||
yield return null;
|
||
StartCoroutine(GameManager.Inst.Initialize((x) =>
|
||
{
|
||
if (!string.IsNullOrEmpty(x))
|
||
callback.Invoke(true);
|
||
}));
|
||
|
||
|
||
}
|
||
|
||
private void conDevice_fuc(int arg0)
|
||
{
|
||
List<string> conPort_ops = new List<string>();
|
||
//对联设备下所有端口
|
||
//conDevice.options[arg0].text//对联设备id
|
||
//Array.FindAll(FindObjectsOfType<PortQuery>(), itme =>
|
||
//{
|
||
// if (itme.portList.deviceId== conDevice.options[arg0].text)
|
||
// return true;
|
||
// return false;
|
||
//}).ToList();
|
||
for (int i = 0; i < GameManager.Inst.TmsPorts_go.Count; i++)
|
||
{
|
||
var n = GameManager.Inst.TmsPorts_go[i].GetComponent<PortQuery>().portList;
|
||
if (n.deviceId == conDevice.options[arg0].text)
|
||
conPort_ops.Add(n.port);
|
||
}
|
||
|
||
conPort.options.Clear();
|
||
|
||
conPort.AddOptions(conPort_ops);
|
||
}
|
||
|
||
private void deviceId_fuc(int arg0)
|
||
{
|
||
//deviceId.options[arg0].text//所属设备ID
|
||
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
|
||
}
|
||
|
||
public IEnumerator saveJson(Action<bool> callback = null)
|
||
{
|
||
if (string.IsNullOrEmpty(mybody.deviceId))
|
||
{
|
||
callback?.Invoke(false);
|
||
yield break;
|
||
}
|
||
try
|
||
{
|
||
mybody.port = port.text;
|
||
|
||
mybody.portCode = portCode.text;
|
||
|
||
mybody.portName = portName.text;
|
||
|
||
mybody.portType = portType.text;
|
||
|
||
//mybody.deviceId = deviceId.options[deviceId.value].text;
|
||
|
||
//mybody.portModel = portModel;
|
||
|
||
mybody.status = status.value.ToString();
|
||
|
||
mybody.conDevice = conDevice.captionText.text;
|
||
|
||
mybody.conPort = conPort.captionText.text;
|
||
|
||
mybody.cableName = cableName.text;
|
||
|
||
if (!string.IsNullOrEmpty(conDevice.captionText.text) && !string.IsNullOrEmpty(conPort.captionText.text))
|
||
mybody.cableGroupName = cableGroupName.captionText.text;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.Log("新增端口错误:" + e.Message);
|
||
gameObject.SetActive(false);
|
||
yield break;
|
||
}
|
||
|
||
|
||
var newData = JsonConvert.SerializeObject(mybody);
|
||
|
||
//var jsonResult = await CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.dk_xz, GameManager.Inst.token, newData);
|
||
|
||
//URlreturn = JsonConvert.DeserializeObject<Root>(jsonResult);
|
||
|
||
yield return StartCoroutine(
|
||
CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.dk_xz, GameManager.Inst.arguments.token, newData, (jsonResult) =>
|
||
{
|
||
try
|
||
{
|
||
URlreturn = JsonConvert.DeserializeObject<Root>(jsonResult);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.Log("新增端口接口错误:" + e.Message);
|
||
}
|
||
})
|
||
);
|
||
}
|
||
|
||
#region JSON
|
||
[System.Serializable]
|
||
public class Body
|
||
{
|
||
/// <summary>
|
||
/// 端口
|
||
/// </summary>
|
||
public string port;
|
||
/// <summary>
|
||
/// 编号
|
||
/// </summary>
|
||
public string portCode;
|
||
/// <summary>
|
||
/// 名称
|
||
/// </summary>
|
||
public string portName;
|
||
/// <summary>
|
||
/// 类型
|
||
/// </summary>
|
||
public string portType;
|
||
/// <summary>
|
||
/// 端口位置
|
||
/// </summary>
|
||
public string portPosition;
|
||
/// <summary>
|
||
/// 所属设备
|
||
/// </summary>
|
||
public string deviceId;
|
||
/// <summary>
|
||
/// 型号
|
||
/// </summary>
|
||
public string portModel;
|
||
/// <summary>
|
||
/// 状态:1-正常;0-禁用
|
||
/// </summary>
|
||
public string status;
|
||
/// <summary>
|
||
/// 对联设备
|
||
/// </summary>
|
||
public string conDevice;
|
||
/// <summary>
|
||
/// 对联端口
|
||
/// </summary>
|
||
public string conPort;
|
||
/// <summary>
|
||
/// 线缆名
|
||
/// </summary>
|
||
[Tooltip("线缆名")] public string cableName;
|
||
/// <summary>
|
||
/// 线缆组名
|
||
/// </summary>
|
||
[Tooltip("线缆组名")] public string cableGroupName;
|
||
/// <summary>
|
||
/// 备注
|
||
/// </summary>
|
||
public string remark;
|
||
}
|
||
[System.Serializable]
|
||
public class Root
|
||
{
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public string code;
|
||
/// <summary>
|
||
/// 反馈结果
|
||
/// </summary>
|
||
public string message;
|
||
/// <summary>
|
||
/// 返回内容(ID)
|
||
/// </summary>
|
||
public string data;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public string serverTime;
|
||
}
|
||
#endregion
|
||
}
|