333 lines
8.8 KiB
C#
333 lines
8.8 KiB
C#
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Net.Http;
|
||
using System.Net.Http.Headers;
|
||
using System.Threading.Tasks;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
/// <summary>
|
||
/// 端口-编辑
|
||
/// </summary>
|
||
public class RedactPort : MonoBehaviour
|
||
{
|
||
public Body mybody;
|
||
public Root URlreturn;
|
||
//public PortQuery.Root root;
|
||
public Button save_bt;
|
||
|
||
/// <summary>
|
||
/// 端口
|
||
/// </summary>
|
||
//public Dropdown port;
|
||
public InputField port;
|
||
/// <summary>
|
||
/// 编号
|
||
/// </summary>
|
||
public InputField portCode;
|
||
/// <summary>
|
||
/// 名称
|
||
/// </summary>
|
||
public InputField portName;
|
||
/// <summary>
|
||
/// 线缆名称
|
||
/// </summary>
|
||
public InputField 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;
|
||
|
||
|
||
private void OnEnable()
|
||
{
|
||
if (gameObject.activeSelf)
|
||
{
|
||
#region 正式
|
||
initAsync();
|
||
port.text = mybody.port;
|
||
portCode.text = mybody.portCode;
|
||
remark.text = mybody.remark;
|
||
deviceId.text = mybody.deviceId;
|
||
portModel.text = mybody.portModel;
|
||
status.value = 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 initAsync()
|
||
{
|
||
//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);
|
||
|
||
|
||
}
|
||
|
||
|
||
private void Start()
|
||
{
|
||
save_bt.onClick.AddListener(async () => await saveJson());
|
||
conDevice.onValueChanged.AddListener(OnDropdownValueChanged_conDevice);
|
||
//port.onValueChanged.AddListener(OnDropdownValueChanged_port);
|
||
|
||
}
|
||
|
||
/// <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()
|
||
{
|
||
mybody.port = port.text;
|
||
mybody.portCode = portCode.text;
|
||
mybody.portName = portName.text;
|
||
if (conDevice.value != 0 && conPort.value != 0) mybody.remark = remark.text;
|
||
mybody.deviceId = deviceId.text;
|
||
mybody.portModel = portModel.text;
|
||
mybody.status = status.value;
|
||
mybody.conDevice = conDevice.options[conDevice.value].text;
|
||
mybody.conPort = conPort.options[conPort.value].text;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 端口-编辑保存
|
||
/// </summary>
|
||
/// <param name="token"></param>
|
||
public async Task saveJson()
|
||
{
|
||
if (string.IsNullOrEmpty(mybody.id)) return;
|
||
string json = JsonConvert.SerializeObject(mybody);
|
||
Debug.Log(json);
|
||
|
||
var client = new HttpClient();
|
||
var request = new HttpRequestMessage
|
||
{
|
||
Method = HttpMethod.Post,
|
||
RequestUri = new Uri(GameManager.Inst.Jk_URL.dk_bj),
|
||
Headers =
|
||
{
|
||
{ "X-Token", GameManager.Inst.token },
|
||
},
|
||
Content = new StringContent(json)
|
||
{
|
||
Headers =
|
||
{
|
||
ContentType = new MediaTypeHeaderValue("application/json")
|
||
}
|
||
}
|
||
};
|
||
using (var response = await client.SendAsync(request))
|
||
{
|
||
response.EnsureSuccessStatusCode();
|
||
var body = await response.Content.ReadAsStringAsync();
|
||
|
||
URlreturn = JsonConvert.DeserializeObject<Root>(body);
|
||
Debug.Log(body);
|
||
}
|
||
}
|
||
|
||
#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 int status;
|
||
/// <summary>
|
||
/// 对联设备
|
||
/// </summary>
|
||
public string conDevice;
|
||
/// <summary>
|
||
/// 对联端口
|
||
/// </summary>
|
||
public string conPort;
|
||
/// <summary>
|
||
/// 备注
|
||
/// </summary>
|
||
public string remark;
|
||
|
||
/// <summary>
|
||
/// 所属设备名称
|
||
/// </summary>
|
||
public string deviceName;
|
||
/// <summary>
|
||
/// 对联设备名称
|
||
/// </summary>
|
||
public string conDeviceName;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public int 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
|
||
|
||
}
|