182 lines
4.8 KiB
C#
182 lines
4.8 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;
|
||
/// <summary>
|
||
/// 端口-编辑
|
||
/// </summary>
|
||
public class RedactPort : MonoBehaviour
|
||
{
|
||
public 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 void OnEnable()
|
||
{
|
||
if (gameObject.activeSelf)
|
||
{
|
||
port.options[0] =new Dropdown.OptionData("sadsa");
|
||
}
|
||
}
|
||
|
||
/// <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);
|
||
|
||
PortQuery.Root root = JsonConvert.DeserializeObject<PortQuery.Root>(jsonResult);
|
||
|
||
List<string> port_list = new List<string>();//端口
|
||
List<string> conDevice_list = new List<string>();//对联设备
|
||
List<string> conPort_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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 当 Dropdown 值发生改变时调用该方法
|
||
/// </summary>
|
||
/// <param name="index"></param>
|
||
public void OnDropdownValueChanged(int index)
|
||
{
|
||
//conDevice.options[index]
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (gameObject.activeSelf)
|
||
{
|
||
root.port = port.value.ToString();
|
||
root.portCode = portCode.text;
|
||
root.portName = portName.text;
|
||
root.portType = portType.text;
|
||
root.deviceId = deviceId.text;
|
||
root.portModel = portModel.text;
|
||
root.status = int.Parse(status.value.ToString());
|
||
root.conDevice = conDevice.value.ToString();
|
||
root.conPort = conPort.value.ToString();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 端口-编辑保存
|
||
/// </summary>
|
||
/// <param name="token"></param>
|
||
public async Task saveJson(string token)
|
||
{
|
||
if (string.IsNullOrEmpty(root.id)) return;
|
||
string json = JsonConvert.SerializeObject(root);
|
||
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
|
||
|
||
}
|