639 lines
18 KiB
C#
639 lines
18 KiB
C#
//using AutoMapper;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Threading.Tasks;
|
||
using Unity.VisualScripting;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using UnityEngine.UI;
|
||
using static GameManager;
|
||
/// <summary>
|
||
/// 端口-新增
|
||
/// </summary>
|
||
public class AddPort : MonoBehaviour
|
||
{
|
||
public Root URlreturn = new Root();
|
||
public Body mybody = new Body();
|
||
public Button save_bt;
|
||
public Image save_bt_img;
|
||
|
||
/// <summary>
|
||
/// 端口
|
||
/// </summary>
|
||
public InputField port;
|
||
/// <summary>
|
||
/// 编号
|
||
/// </summary>
|
||
public InputField portCode;
|
||
/// <summary>
|
||
/// 名称
|
||
/// </summary>
|
||
public InputField portName;
|
||
/// <summary>
|
||
/// 类型
|
||
/// </summary>
|
||
public Dropdown portType;
|
||
|
||
//public Dropdown deviceId;
|
||
|
||
//public InputField portModel;
|
||
/// <summary>
|
||
/// 状态
|
||
/// </summary>
|
||
//public Dropdown status;
|
||
/// <summary>
|
||
/// 对联机柜
|
||
/// </summary>
|
||
public Dropdown conCabinet;
|
||
/// <summary>
|
||
/// 对联设备
|
||
/// </summary>
|
||
public Dropdown conDevice;
|
||
/// <summary>
|
||
/// 对联端口
|
||
/// </summary>
|
||
public Dropdown conPort;
|
||
/// <summary>
|
||
/// 线缆名
|
||
/// </summary>
|
||
public InputField cableName;
|
||
/// <summary>
|
||
/// 线缆组名
|
||
/// </summary>
|
||
public Dropdown cableGroupName;
|
||
|
||
//MapperConfiguration config;
|
||
//IMapper mapper;
|
||
PortQuery.PortList portlist;
|
||
|
||
/// <summary>
|
||
/// 对联设备ID和名字关联字典
|
||
/// </summary>
|
||
public Dictionary<string, string> conDevice_dic = new Dictionary<string, string>();
|
||
public string conDevice_dic_id;
|
||
|
||
public Button close_bt;
|
||
|
||
private void Awake()
|
||
{
|
||
|
||
save_bt_img = save_bt.GetComponent<Image>();
|
||
addconCabinet();
|
||
conCabinet.captionText.text = null;
|
||
gameObject.SetActive(false);
|
||
}
|
||
public void addconCabinet()
|
||
{
|
||
conCabinet.options.Clear();
|
||
List<string> conCabinetname = new List<string>();
|
||
for (int i = 0; i < GameManager.Inst.Cabinets_go.Count; i++)
|
||
conCabinetname.Add(GameManager.Inst.Cabinets_go[i].name);
|
||
conCabinetname.Add("");
|
||
conCabinet.AddOptions(conCabinetname);
|
||
}
|
||
|
||
|
||
private void OnEnable()
|
||
{
|
||
//Debug.Log("版本号 1.1.1");
|
||
mybody = new Body();
|
||
conDevice_dic.Clear();
|
||
if (conCabinet.options.Count == 0)
|
||
{
|
||
addconCabinet();
|
||
conCabinet.captionText.text = null;
|
||
|
||
}
|
||
init();
|
||
findIndex(conCabinet, "");
|
||
port.text = null;
|
||
|
||
portCode.text = null;
|
||
|
||
portName.text = null;
|
||
|
||
portType.captionText.text = null;
|
||
|
||
conCabinet.captionText.text = null;
|
||
|
||
conDevice.captionText.text = null;
|
||
|
||
conPort.captionText.text = null;
|
||
|
||
cableName.text = null;
|
||
cableGroupName.captionText.text = null;
|
||
|
||
//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);
|
||
//}
|
||
|
||
tbinfo();
|
||
//Debug.Log("版本号 1.1.1");
|
||
}
|
||
|
||
public void tbinfo()
|
||
{
|
||
var p = GameManager.Inst.nowDevice.GetComponent<PortQuery>();
|
||
if (p)
|
||
{
|
||
mybody.port = p.portList.port;
|
||
mybody.portCode = p.portList.portCode;
|
||
mybody.portName = p.portList.portName;
|
||
mybody.portType = p.portList.portType;
|
||
mybody.portPosition = p.portList.portPosition;
|
||
mybody.deviceId = p.portList.deviceId;
|
||
//mybody.deviceName = p.portList.deviceName;
|
||
mybody.portModel = p.portList.portModel;
|
||
mybody.status = p.portList.status;
|
||
mybody.conDevice = p.portList.conDevice;
|
||
mybody.conPort = p.portList.conPort;
|
||
// mybody.conDeviceName = p.portList.conDeviceName;
|
||
mybody.cableName = p.portList.cableName;
|
||
mybody.cableGroupName = p.portList.cableGroupName;
|
||
mybody.remark = p.portList.remark;
|
||
//mybody.createName = p.portList.createName;
|
||
//mybody.createTime = p.portList.createTime;
|
||
}
|
||
}
|
||
|
||
private void init()
|
||
{
|
||
|
||
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);
|
||
}
|
||
Dropdown.OptionData optionData1 = new Dropdown.OptionData("");
|
||
cableGroupName.options.Add(optionData1);
|
||
//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);
|
||
|
||
List<string> portType_list = new List<string>();
|
||
foreach (var item in PatternChoose.Inst.xianlan.lineCode_dic.Keys)
|
||
{
|
||
portType_list.Add(item);
|
||
}
|
||
portType_list.Add("");
|
||
portType.options.Clear();
|
||
portType.AddOptions(portType_list);
|
||
portType.captionText.text = "";
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
|
||
//deviceId.onValueChanged.AddListener(deviceId_fuc);//所属设备
|
||
conDevice.onValueChanged.AddListener(conDevice_fuc);//对联设备
|
||
conCabinet.onValueChanged.AddListener(OnDropdownValueChanged_conCabinet);//对联机柜
|
||
save_bt.onClick.AddListener(() =>
|
||
{
|
||
GameManager.Inst.editorMenu.gameObject.SetActive(false);
|
||
|
||
StartCoroutine(SaveJsonCoroutine());
|
||
});
|
||
|
||
close_bt.onClick.AddListener(() =>
|
||
{
|
||
gameObject.SetActive(false);
|
||
});
|
||
}
|
||
|
||
public void OnDropdownValueChanged_conCabinet(int index)
|
||
{
|
||
List<string> conDevice_list = new List<string>();//对联设备
|
||
Dictionary<string, string> conDevice_list_dict = new Dictionary<string, string>();
|
||
|
||
if (index != conCabinet.options.Count - 1)
|
||
{
|
||
var _s = GameManager.Inst.Cabinets_go[index].GetComponentsInChildren<DeviceQuery>();
|
||
|
||
Array.ForEach(_s, (item) =>
|
||
{
|
||
if (!conDevice_list_dict.ContainsKey(item.deviceList.id) && !string.IsNullOrEmpty(item.deviceList.id))
|
||
{
|
||
//conDevice_list_dict.Add(item.deviceList.id, "");
|
||
//conDevice_list.Add(item.deviceList.id);
|
||
conDevice_list_dict.Add(item.deviceList.id, item.deviceList.deviceName);
|
||
conDevice_list.Add(item.deviceList.deviceName);
|
||
}
|
||
});
|
||
conDevice_list.Add("");
|
||
conDevice.options.Clear();
|
||
conDevice.AddOptions(conDevice_list);
|
||
|
||
conDevice_dic.Clear();
|
||
conDevice_dic = conDevice_list_dict;
|
||
}
|
||
else
|
||
{
|
||
conDevice_dic.Clear();
|
||
conDevice.options.Clear();
|
||
conDevice.captionText.text = "";
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 寻找下拉菜单选项
|
||
/// </summary>
|
||
/// <param name="_dro"></param>
|
||
/// <param name="_name"></param>
|
||
public string findIndex(Dropdown _dro, string _name)
|
||
{
|
||
int index = _dro.options.FindIndex(x => x.text == _name);
|
||
if (index != -1)
|
||
{
|
||
_dro.captionText.text = _dro.options[index].text;
|
||
_dro.value = index;
|
||
return index.ToString();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private IEnumerator SaveJsonCoroutine()
|
||
{
|
||
yield return StartCoroutine(saveJson((y) =>
|
||
{
|
||
if (string.IsNullOrEmpty(y))
|
||
{
|
||
SecondConfirmPanel.DeleteConform(null, "添加端口失败");
|
||
refurbish();
|
||
}
|
||
else if (y == "2")
|
||
{
|
||
Debug.Log("添加端口失败:该端口未配置所属设备,端口:" + mybody.port);
|
||
SecondConfirmPanel.DeleteConform(null, "该端口未配置所属设备");
|
||
refurbish();
|
||
}
|
||
else
|
||
{
|
||
if (URlreturn != null && URlreturn.message == "操作成功")
|
||
{
|
||
addInFo();
|
||
StartCoroutine(Succeed((x) =>
|
||
{
|
||
if (x)
|
||
{
|
||
GameManager.Inst.lastDeviceID = URlreturn.data;
|
||
refurbish();
|
||
}
|
||
else
|
||
{
|
||
SecondConfirmPanel.DeleteConform(null, "更新场景失败");
|
||
Debug.Log("接口获取数据失败(新增端口-初始化失败)");
|
||
}
|
||
}));
|
||
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("添加端口失败:" + URlreturn.message);
|
||
SecondConfirmPanel.DeleteConform(null, "添加端口失败");
|
||
refurbish();
|
||
}
|
||
}
|
||
}));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新本地对象数据
|
||
/// </summary>
|
||
public void addInFo()
|
||
{
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刷新
|
||
/// </summary>
|
||
public void refurbish()
|
||
{
|
||
Array.ForEach(GameManager.Inst.pop_ups.ToArray(), (itme) =>
|
||
{
|
||
itme.gameObject.SetActive(false);
|
||
});
|
||
}
|
||
|
||
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);
|
||
else
|
||
callback.Invoke(false);
|
||
}, true));
|
||
|
||
|
||
}
|
||
|
||
private void conDevice_fuc(int arg0)
|
||
{
|
||
List<KeyValuePair<string, string>> orderedList = conDevice_dic.ToList();
|
||
try
|
||
{
|
||
KeyValuePair<string, string> element = orderedList.ElementAt(arg0);
|
||
conDevice_dic_id = element.Key;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
conDevice_dic_id = null;
|
||
}
|
||
|
||
|
||
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 (string.IsNullOrEmpty(conDevice_dic_id))
|
||
{
|
||
if (n.deviceName == conDevice.options[arg0].text && !string.IsNullOrEmpty(n.port))
|
||
//if (n.deviceId == conDevice.options[arg0].text)
|
||
{
|
||
conPort_ops.Add(n.port);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (n.deviceId == conDevice_dic_id && !string.IsNullOrEmpty(n.port))
|
||
{
|
||
conPort_ops.Add(n.port);
|
||
}
|
||
}
|
||
}
|
||
conPort.options.Clear();
|
||
|
||
conPort_ops.Add("");
|
||
conPort.AddOptions(conPort_ops);
|
||
}
|
||
|
||
private void deviceId_fuc(int arg0)
|
||
{
|
||
//deviceId.options[arg0].text//所属设备ID
|
||
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if ((cableGroupName.transform.Find("Dropdown List") && cableGroupName.transform.Find("Dropdown List").gameObject.activeSelf) ||
|
||
(portType.transform.Find("Dropdown List") && portType.transform.Find("Dropdown List").gameObject.activeSelf))
|
||
{
|
||
// 下拉菜单处于展开状态。
|
||
//Color color = save_bt_img.color;
|
||
//color.a = 0;
|
||
//save_bt_img.color = color;
|
||
save_bt.gameObject.SetActive(false);
|
||
}
|
||
|
||
else
|
||
{
|
||
// 下拉菜单未展开。
|
||
//Color color = save_bt_img.color;
|
||
//color.a = 1;
|
||
//save_bt_img.color = color;
|
||
save_bt.gameObject.SetActive(true);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
public IEnumerator saveJson(Action<string> callback = null)
|
||
{
|
||
if (string.IsNullOrEmpty(mybody.deviceId))
|
||
{
|
||
callback?.Invoke("2");
|
||
yield break;
|
||
}
|
||
try
|
||
{
|
||
mybody.port = port.text;
|
||
|
||
mybody.portCode = portCode.text;
|
||
|
||
mybody.portName = portName.text;
|
||
|
||
mybody.portType = portType.captionText.text;
|
||
|
||
var dp = GameManager.Inst.FindParent(GameManager.Inst.nowDevice, GameManager.Inst.IsFindRacks_goORMachineSlots_goORTmsCards_go);
|
||
if (dp)
|
||
{
|
||
var dp_d = dp.GetComponent<DeviceQuery>();
|
||
mybody.deviceId = string.IsNullOrEmpty(dp_d.deviceList.id) ? null : dp_d.deviceList.id;
|
||
}
|
||
else
|
||
mybody.deviceId = null;
|
||
//mybody.deviceId = deviceId.options[deviceId.value].text;
|
||
|
||
//mybody.portModel = portModel;
|
||
|
||
//mybody.status = status.value.ToString();
|
||
|
||
//mybody.conDevice = conDevice.captionText.text;
|
||
|
||
//mybody.conDevice = conDevice_dic_id;
|
||
|
||
//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);
|
||
callback.Invoke(null);
|
||
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);
|
||
callback?.Invoke("1");
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.Log("新增端口接口错误:" + e.Message);
|
||
callback.Invoke(null);
|
||
}
|
||
})
|
||
);
|
||
}
|
||
|
||
#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>
|
||
/// 所属设备(ID)
|
||
/// </summary>
|
||
public string deviceId;
|
||
/// <summary>
|
||
/// 型号
|
||
/// </summary>
|
||
public string portModel;
|
||
/// <summary>
|
||
/// 状态:1-正常;0-禁用
|
||
/// </summary>
|
||
public string status;
|
||
/// <summary>
|
||
/// 对联设备(ID)
|
||
/// </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
|
||
}
|