216 lines
		
	
	
		
			5.9 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			216 lines
		
	
	
		
			5.9 KiB
		
	
	
	
		
			C#
		
	
	
	
using Newtonsoft.Json;
 | 
						||
using System;
 | 
						||
using System.Net.Http.Headers;
 | 
						||
using System.Net.Http;
 | 
						||
using UnityEngine;
 | 
						||
using UnityEngine.UI;
 | 
						||
using static System.Net.Mime.MediaTypeNames;
 | 
						||
using System.Threading.Tasks;
 | 
						||
using static PortQuery;
 | 
						||
using System.Collections.Generic;
 | 
						||
using Unity.VisualScripting;
 | 
						||
using System.Reflection;
 | 
						||
using static UnityEditor.Progress;
 | 
						||
/// <summary>
 | 
						||
/// 端口-编辑
 | 
						||
/// </summary>
 | 
						||
public class RedactPort : MonoBehaviour
 | 
						||
{
 | 
						||
    public Root myroot;
 | 
						||
    public PortQuery.Root root;
 | 
						||
    public Button save_bt;
 | 
						||
 | 
						||
    public Dropdown port;//端口
 | 
						||
    public InputField portCode;//编号
 | 
						||
    public InputField portName;//名称
 | 
						||
    public InputField portType;//端口类型
 | 
						||
    public InputField deviceId;//所属设备
 | 
						||
    public InputField portModel;//型号
 | 
						||
    public Dropdown status;//状态
 | 
						||
    public Dropdown conDevice;//对联设备
 | 
						||
    public Dropdown conPort;//对联端口
 | 
						||
 | 
						||
 | 
						||
    private async void OnEnable()
 | 
						||
    {
 | 
						||
        if (gameObject.activeSelf)
 | 
						||
        {
 | 
						||
            #region 正式
 | 
						||
            //await initAsync("");
 | 
						||
            //port.options[0].text = myroot.port;
 | 
						||
            //portCode.text = myroot.portCode;
 | 
						||
            //portType.text = myroot.portType;
 | 
						||
            //deviceId.text = myroot.deviceId;
 | 
						||
            //portModel.text = myroot.portModel;
 | 
						||
            //status.value = myroot.status;
 | 
						||
            //if (!string.IsNullOrEmpty(myroot.conDevice))
 | 
						||
            //{
 | 
						||
            //    //自动识别对联设备
 | 
						||
            //    for (int i = 0; i < conDevice.options.Count; i++)
 | 
						||
            //    {
 | 
						||
            //        if (conDevice.options[i].text == myroot.conDevice)
 | 
						||
            //        {
 | 
						||
            //            conDevice.value = i;
 | 
						||
            //            OnDropdownValueChanged_conDevice(i);
 | 
						||
            //            break;
 | 
						||
            //        }
 | 
						||
            //    }
 | 
						||
            //    //自动识别端口
 | 
						||
            //    for (int i = 0; i < conPort.options.Count; i++)
 | 
						||
            //    {
 | 
						||
            //        if (conPort.options[i].text == myroot.conPort)
 | 
						||
            //        {
 | 
						||
            //            conPort.value = i;
 | 
						||
            //            break;
 | 
						||
            //        }
 | 
						||
            //    }
 | 
						||
            //}
 | 
						||
            #endregion
 | 
						||
 | 
						||
 | 
						||
        }
 | 
						||
    }
 | 
						||
 | 
						||
    /// <summary>
 | 
						||
    /// 获取所有端口
 | 
						||
    /// </summary>
 | 
						||
    /// <param name="token"></param>
 | 
						||
    /// <returns></returns>
 | 
						||
    public async Task initAsync(string token)
 | 
						||
    {
 | 
						||
        var jsonResult = await CombineJSON.GetJson_POST("https://jsonplaceholder.typicode.com/posts", token);
 | 
						||
 | 
						||
        root = JsonConvert.DeserializeObject<PortQuery.Root>(jsonResult);
 | 
						||
 | 
						||
        List<string> port_list = new List<string>();//端口
 | 
						||
        List<string> conDevice_list = new List<string>();//对联设备
 | 
						||
        foreach (var item in root.data)
 | 
						||
        {
 | 
						||
            port_list.Add(item.port);
 | 
						||
            conDevice_list.Add(item.conDevice);
 | 
						||
        }
 | 
						||
        conDevice.options.Clear();
 | 
						||
        conDevice.AddOptions(conDevice_list);
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
    private void Start()
 | 
						||
    {
 | 
						||
        save_bt.onClick.AddListener(async () => await saveJson(""));
 | 
						||
        conDevice.onValueChanged.AddListener(OnDropdownValueChanged_conDevice);
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
    /// <summary>
 | 
						||
    /// 当 Dropdown 值发生改变时(根据对联设备添加该设备下所有端口)
 | 
						||
    /// </summary>
 | 
						||
    /// <param name="index"></param>
 | 
						||
    public void OnDropdownValueChanged_conDevice(int index)
 | 
						||
    {
 | 
						||
        List<string> conPort_list = new List<string>();//对联端口
 | 
						||
        //conDevice.options[index]//对联设备ID
 | 
						||
        foreach (var item in root.data)
 | 
						||
        {
 | 
						||
            if (item.deviceId == conDevice.options[index].text)
 | 
						||
            {
 | 
						||
                conPort_list.Add(item.port);
 | 
						||
            }
 | 
						||
        }
 | 
						||
        conPort.options.Clear();
 | 
						||
        conPort.AddOptions(conPort_list);
 | 
						||
    }
 | 
						||
 | 
						||
    private void Update()
 | 
						||
    {
 | 
						||
 | 
						||
    }
 | 
						||
 | 
						||
    /// <summary>
 | 
						||
    /// 端口-编辑保存
 | 
						||
    /// </summary>
 | 
						||
    /// <param name="token"></param>
 | 
						||
    public async Task saveJson(string token)
 | 
						||
    {
 | 
						||
        if (string.IsNullOrEmpty(myroot.id)) return;
 | 
						||
        string json = JsonConvert.SerializeObject(myroot);
 | 
						||
        Debug.Log(json);
 | 
						||
 | 
						||
        var client = new HttpClient();
 | 
						||
        var request = new HttpRequestMessage
 | 
						||
        {
 | 
						||
            Method = HttpMethod.Post,
 | 
						||
            RequestUri = new Uri("http://wu4ifs.natappfree.cc/machineRoom/port/updateById"),
 | 
						||
            Headers =
 | 
						||
    {
 | 
						||
        { "X-Token", 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();
 | 
						||
            Debug.Log(body);
 | 
						||
        }
 | 
						||
    }
 | 
						||
 | 
						||
    #region JSON
 | 
						||
    [System.Serializable]
 | 
						||
    public class Root
 | 
						||
    {
 | 
						||
        /// <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;
 | 
						||
    }
 | 
						||
 | 
						||
    #endregion
 | 
						||
 | 
						||
}
 |