406 lines
11 KiB
C#
406 lines
11 KiB
C#
using AutoMapper;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Net.Http;
|
||
using System.Net.Http.Headers;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using UnityEngine;
|
||
using UnityEngine.Networking;
|
||
using UnityEngine.UI;
|
||
using static GameManager;
|
||
/// <summary>
|
||
/// 端口-编辑
|
||
/// </summary>
|
||
public class RedactPort : MonoBehaviour
|
||
{
|
||
public Body mybody;
|
||
public Root URlreturn;
|
||
//public PortQuery.Root root;
|
||
public Button save_bt;
|
||
PortQuery.PortList portlist;
|
||
|
||
/// <summary>
|
||
/// 端口
|
||
/// </summary>
|
||
//public Dropdown port;
|
||
public InputField port;
|
||
/// <summary>
|
||
/// 编号
|
||
/// </summary>
|
||
public InputField portCode;
|
||
/// <summary>
|
||
/// 名称
|
||
/// </summary>
|
||
public InputField portName;
|
||
/// <summary>
|
||
/// 线缆名称
|
||
/// </summary>
|
||
public Dropdown remark;
|
||
///// <summary>
|
||
///// 所属设备
|
||
///// </summary>
|
||
//public InputField deviceId;
|
||
/// <summary>
|
||
/// 型号
|
||
/// </summary>
|
||
public InputField portModel;
|
||
/// <summary>
|
||
/// 状态
|
||
/// </summary>
|
||
public Dropdown status;
|
||
/// <summary>
|
||
/// 对联设备
|
||
/// </summary>
|
||
public Dropdown conDevice;
|
||
/// <summary>
|
||
/// 对联端口
|
||
/// </summary>
|
||
public Dropdown conPort;
|
||
|
||
MapperConfiguration config;
|
||
IMapper mapper;
|
||
private void OnEnable()
|
||
{
|
||
config = new MapperConfiguration(cfg =>
|
||
{
|
||
cfg.CreateMap<RedactPort.Body, PortQuery.PortList>();
|
||
cfg.CreateMap<PortQuery.PortList, RedactPort.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;
|
||
}
|
||
}
|
||
mybody = mapper.Map<RedactPort.Body>(portlist);
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.Log(e.StackTrace + "**********" + e.Message);
|
||
}
|
||
|
||
#region 正式
|
||
init();
|
||
port.text = mybody.port;
|
||
portCode.text = mybody.portCode;
|
||
portName.text = mybody.portName;
|
||
remark.captionText.text = mybody.remark;
|
||
//deviceId.text = mybody.deviceId;
|
||
portModel.text = mybody.portModel;
|
||
status.value = string.IsNullOrEmpty(mybody.status) ? 2 : Convert.ToInt32(mybody.status);
|
||
if (!string.IsNullOrEmpty(mybody.conDevice))
|
||
{
|
||
//自动识别对联设备
|
||
for (int i = 0; i < conDevice.options.Count; i++)
|
||
{
|
||
if (conDevice.options[i].text == mybody.conDevice)
|
||
{
|
||
conDevice.value = 0;
|
||
conDevice.value = i;
|
||
break;
|
||
}
|
||
}
|
||
|
||
}
|
||
#endregion
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取端口的对联设备和对联端口选项
|
||
/// </summary>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
public void init()
|
||
{
|
||
//var jsonResult = await CombineJSON.GetJson_POST(GameManager.Inst.Jk_URL.dk_cx, GameManager.Inst.token);
|
||
|
||
//root = JsonConvert.DeserializeObject<PortQuery.Root>(jsonResult);
|
||
|
||
List<string> conDevice_list = new List<string>();//对联设备
|
||
List<string> conPort_list = new List<string>();// 对联端口
|
||
conPort_list.Add("");
|
||
conDevice_list.Add("");
|
||
//加载所有对联设备和对联端口选项
|
||
Dictionary<string, string> conDevice_list_dict = new Dictionary<string, string>();
|
||
Dictionary<string, string> conPort_list_dict = new Dictionary<string, string>();
|
||
|
||
if (GameManager.Inst.TmsPorts == null)
|
||
return;
|
||
foreach (var item in GameManager.Inst.TmsPorts)
|
||
{
|
||
if (!conDevice_list_dict.ContainsKey(item.deviceId))
|
||
{
|
||
conDevice_list_dict.Add(item.deviceId, "");
|
||
conDevice_list.Add(item.deviceId);
|
||
}
|
||
if (!conPort_list_dict.ContainsKey(item.port))
|
||
{
|
||
conPort_list_dict.Add(item.port, "");
|
||
conPort_list.Add(item.port);
|
||
}
|
||
}
|
||
conDevice.options.Clear();
|
||
conDevice.AddOptions(conDevice_list);
|
||
|
||
conPort.options.Clear();
|
||
conPort.AddOptions(conPort_list);
|
||
|
||
foreach (var item in LineQuery.Inst.keyValues.Keys)
|
||
{
|
||
Dropdown.OptionData optionData = new Dropdown.OptionData(item);
|
||
remark.options.Add(optionData);
|
||
}
|
||
}
|
||
|
||
|
||
private void Start()
|
||
{
|
||
save_bt.onClick.AddListener(() =>
|
||
{
|
||
StartCoroutine(SaveJsonCoroutine());
|
||
});
|
||
conDevice.onValueChanged.AddListener(OnDropdownValueChanged_conDevice);
|
||
//port.onValueChanged.AddListener(OnDropdownValueChanged_port);
|
||
|
||
}
|
||
|
||
private IEnumerator SaveJsonCoroutine()
|
||
{
|
||
yield return StartCoroutine(saveJson());
|
||
if (URlreturn != null && URlreturn.message == "操作成功")
|
||
{
|
||
yield return StartCoroutine(Succeed());
|
||
|
||
Array.ForEach(GameManager.Inst.pop_ups.ToArray(), (itme) =>
|
||
{
|
||
itme.gameObject.SetActive(false);
|
||
});
|
||
}
|
||
else
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
public IEnumerator Succeed()
|
||
{
|
||
yield return StartCoroutine(GameManager.Inst.Initialize());
|
||
}
|
||
|
||
/// <summary>
|
||
/// 当 端口 值发生改变时(根据端口添加该端口下信息)
|
||
/// </summary>
|
||
/// <param name="index"></param>
|
||
private void OnDropdownValueChanged_port(int index)
|
||
{
|
||
if (index == 0) return;
|
||
//port.options[index]
|
||
//for (int i = 0; i < port.options.Count; i++)
|
||
//{
|
||
// if (i == port.value)
|
||
// {
|
||
// port.value = 0;
|
||
// port.value = i;
|
||
// }
|
||
//}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 当 对联设备 值发生改变时(根据对联设备--添加该设备下所有端口)
|
||
/// </summary>
|
||
/// <param name="index"></param>
|
||
public void OnDropdownValueChanged_conDevice(int index)
|
||
{
|
||
if (index == 0) return;
|
||
List<string> conPort_list = new List<string>();//对联端口
|
||
conPort_list.Add("");
|
||
//conDevice.options[index]//对联设备ID
|
||
foreach (var item in GameManager.Inst.root_AllPort.data)
|
||
{
|
||
if (item.deviceId == conDevice.options[index].text)
|
||
{
|
||
conPort_list.Add(item.port);
|
||
}
|
||
}
|
||
conPort.options.Clear();
|
||
conPort.AddOptions(conPort_list);
|
||
|
||
if (string.IsNullOrEmpty(mybody.conPort)) return;
|
||
|
||
//自动识别端口
|
||
for (int i = 0; i < conPort.options.Count; i++)
|
||
{
|
||
if (conPort.options[i].text == mybody.conPort)
|
||
{
|
||
conPort.value = i;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 端口-编辑保存
|
||
/// </summary>
|
||
/// <param name="token"></param>
|
||
public IEnumerator saveJson()
|
||
{
|
||
if (string.IsNullOrEmpty(mybody.id))
|
||
yield break;
|
||
|
||
try
|
||
{
|
||
mybody.port = port.text;
|
||
mybody.portCode = portCode.text;
|
||
mybody.portName = portName.text;
|
||
if (!string.IsNullOrEmpty(conDevice.captionText.text) && !string.IsNullOrEmpty(conPort.captionText.text))
|
||
mybody.remark = remark.captionText.text;
|
||
//mybody.deviceId = deviceId.text;
|
||
mybody.portModel = portModel.text;
|
||
mybody.status = status.value.ToString();
|
||
mybody.conDevice = conDevice.options[conDevice.value].text;
|
||
mybody.conPort = conPort.options[conPort.value].text;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.Log("修改端口错误:" + e.Message);
|
||
//gameObject.SetActive(false);
|
||
yield break;
|
||
}
|
||
|
||
|
||
string newData = JsonConvert.SerializeObject(mybody);
|
||
//Debug.Log(json);
|
||
|
||
yield return StartCoroutine(
|
||
CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.dk_bj, 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>
|
||
/// id
|
||
/// </summary>
|
||
public string id;
|
||
/// <summary>
|
||
/// 端口
|
||
/// </summary>
|
||
public string port;
|
||
/// <summary>
|
||
/// 编号
|
||
/// </summary>
|
||
public string portCode;
|
||
/// <summary>
|
||
/// 名称
|
||
/// </summary>
|
||
public string portName;
|
||
/// <summary>
|
||
/// 类型
|
||
/// </summary>
|
||
public string portType;
|
||
/// <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;
|
||
|
||
/// <summary>
|
||
/// 所属设备名称
|
||
/// </summary>
|
||
public string deviceName;
|
||
/// <summary>
|
||
/// 对联设备名称
|
||
/// </summary>
|
||
public string conDeviceName;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public string createTime;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public string createName;
|
||
}
|
||
|
||
[System.Serializable]
|
||
public class Root
|
||
{
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public string code;
|
||
/// <summary>
|
||
/// 反馈结果
|
||
/// </summary>
|
||
public string message;
|
||
/// <summary>
|
||
/// 返回内容
|
||
/// </summary>
|
||
public string data;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public string serverTime;
|
||
}
|
||
|
||
#endregion
|
||
|
||
}
|