GQ_Communicate/GQ_URP/GQ/Assets/script/接口/修改/DeleteDevice.cs

204 lines
5.9 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
/// <summary>
/// 设备-删除
/// </summary>
public class DeleteDevice : MonoBehaviour
{
public Body mybody = new Body();
public Root URlreturn = new Root();
/// <summary>
/// 删除设备
/// </summary>
/// <param name="callback"></param>
/// <returns></returns>
public IEnumerator SaveJsonCoroutine(Action<bool, string> callback)
{
if (mybody.ids == null || mybody.ids.Count <= 0)
{
callback?.Invoke(true, null);
yield break;
}
yield return StartCoroutine(saveJson((w) =>
{
if (w != null && URlreturn != null && URlreturn.message == "操作成功")
{
DeleteAssign();
callback?.Invoke(true, "操作成功");
StartCoroutine(Succeed((x) =>
{
if (x)
{
refurbish();
mybody.ids.Clear();
}
else
{
SecondConfirmPanel.DeleteConform(null, "更新场景失败");
Debug.Log("接口获取数据失败(删除设备-初始化失败)");
}
}));
}
else
{
string prompt = string.IsNullOrEmpty(URlreturn.message) ? null : URlreturn.message;
Debug.Log("删除设备失败:" + prompt);
SecondConfirmPanel.DeleteConform(null, "删除设备失败" + prompt);
callback?.Invoke(false, prompt);
refurbish();
}
}));
}
/// <summary>
/// 刷新
/// </summary>
public void refurbish()
{
Array.ForEach(GameManager.Inst.pop_ups.ToArray(), (itme) =>
{
itme.gameObject.SetActive(false);
});
//GameManager.Inst.objectToShow_add.gameObject.SetActive(true);
var SN1 = GameManager.Inst.objectToShow_add.GetComponent<SearchName1>();
SN1.hide_menu();
}
public IEnumerator Succeed(Action<bool> callback)
{
Array.ForEach(GameObject.FindObjectsOfType<UPosItem>(), (item) =>
{
item.isOccupied = false;
});
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();
//callback.Invoke(true);
yield return null;
StartCoroutine(GameManager.Inst.Initialize((x) =>
{
if (!string.IsNullOrEmpty(x))
callback.Invoke(true);
else
callback.Invoke(false);
}, true));
}
public IEnumerator saveJson(Action<Root> callback = null)
{
string newData = null;
try
{
mybody.ids.RemoveAll((x) => string.IsNullOrEmpty(x));
if (mybody.ids.Count <= 0)
{
URlreturn.message = "操作成功";
callback?.Invoke(URlreturn);
yield break;
}
newData = JsonConvert.SerializeObject(mybody);
}
catch (Exception e)
{
callback.Invoke(null);
yield break;
}
//var jsonResult = await CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.sb_sc, GameManager.Inst.token, newData);
//URlreturn = JsonConvert.DeserializeObject<Root>(jsonResult);
yield return StartCoroutine(
CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.sb_sc, GameManager.Inst.arguments.token, newData, (jsonResult) =>
{
try
{
URlreturn = JsonConvert.DeserializeObject<Root>(jsonResult);
callback?.Invoke(URlreturn);
}
catch (System.Exception e)
{
Debug.Log("删除设备错误:" + e.Message + "\n" + e.StackTrace);
callback.Invoke(null);
}
})
);
}
/// <summary>
/// 删除更新本地对象
/// </summary>
public void DeleteAssign()
{
//if (GameManager.Inst.nowDevice)
//{
// var d = GameManager.Inst.nowDevice.GetComponent<DeviceQuery>();
// if (d)
// {
// if (d.deviceList.type == "2" && d.deviceList.deviceType == "1")
// {
// GameManager.Inst.Racks_go.Remove(GameManager.Inst.nowDevice);
// }
// else if (d.deviceList.type == "2" && d.deviceList.deviceType == "2")
// {
// GameManager.Inst.MachineSlots_go.Remove(GameManager.Inst.nowDevice);
// }
// else if (d.deviceList.type == "2" && d.deviceList.deviceType == "3")
// {
// GameManager.Inst.TmsCards_go.Remove(GameManager.Inst.nowDevice);
// }
// }
//}
}
#region JSON
[System.Serializable]
public class Body
{
/// <summary>
/// id
/// </summary>
public List<string> ids;
}
[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
}