//using AutoMapper;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Unity.VisualScripting.Antlr3.Runtime;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using static GameManager;
///
/// 设备-新增
///
public class AddDevice : MonoBehaviour
{
public Root URlreturn = new Root();
public Body mybody = new Body();
public Button save_bt;
///
/// 类型:1-机柜;2-设备
///
public Dropdown type;
///
/// 设备类型:1-机框;2-机槽;3-板卡
///
public Dropdown deviceType;
///
/// 所属机柜id
///
public Dropdown rackId;
///
/// 所属机框id
///
public Dropdown shelfId;
///
/// 所属机槽id
///
public Dropdown slotId;
///
/// 设备位置
///
//public InputField devicePosition;
///
/// 是否启用:1-正常;0-禁用
///
public Dropdown status;
//MapperConfiguration config;
//IMapper mapper;
DeviceQuery.DeviceList device;
private void OnEnable()
{
//Debug.Log("版本号 1.1.1");
mybody = new Body();
GameManager.Inst.editorMenu.gameObject.SetActive(false);
init();
//config = new MapperConfiguration(cfg =>
//{
// cfg.CreateMap();
// cfg.CreateMap();
//});
//mapper = config.CreateMapper();
try
{
if (GameManager.Inst.nowDevice.GetComponent().deviceList.deviceType != "1")
{
if (!string.IsNullOrEmpty(GameManager.Inst.nowDeviceID))
{
for (int i = 0; i < GameManager.Inst.root_AllDevice.data.Count; i++)
{
if (GameManager.Inst.TmsPorts[i].id == GameManager.Inst.nowDeviceID)
{
device = GameManager.Inst.root_AllDevice.data[i];
break;
}
}
if (device != null)
{
//mybody = mapper.Map(deviceList);
}
}
else
{
//mybody = mapper.Map(GameManager.Inst.nowDevice.GetComponent().deviceList);
}
}
}
catch (Exception e)
{
Debug.Log(e.StackTrace + "**********" + e.Message);
}
tbinfo();
//Debug.Log("版本号 1.1.1");
}
public void tbinfo()
{
if (GameManager.Inst.nowDevice.GetComponent().deviceList.deviceType == "1")
{
var part = GameManager.Inst.FindParent(GameManager.Inst.nowDevice, GameManager.Inst.IsDesiredParent);
mybody.rackId = part.GetComponent().deviceList.id;
mybody.devicePosition = GameManager.Inst.nowDevice.transform.parent.name;
mybody.modelNum = GameManager.Inst.objectToShow_add.GetComponent().dragController.oriObjectPrefab.gameObject.name;
}
else if (GameManager.Inst.nowDevice.GetComponent().deviceList.deviceType == "3")
{
var cabinet = GameManager.Inst.FindParent(GameManager.Inst.nowDevice, GameManager.Inst.IsDesiredParent);
mybody.shelfId = cabinet.GetComponent().deviceList.id;
}
}
public void init()
{
rackId.options.Clear();
shelfId.options.Clear();
slotId.options.Clear();
List rackId_ops = new List();
rackId_ops.Add("");
for (int i = 0; i < GameManager.Inst.Cabinets.Count; i++)
{
rackId_ops.Add(GameManager.Inst.Cabinets[i].id);
}
rackId.AddOptions(rackId_ops);
List shelfId_ops = new List();
shelfId_ops.Add("");
for (int i = 0; i < GameManager.Inst.Racks.Count; i++)
{
shelfId_ops.Add(GameManager.Inst.Racks[i].id);
}
shelfId.AddOptions(shelfId_ops);
List slotId_ops = new List();
slotId_ops.Add("");
for (int i = 0; i < GameManager.Inst.MachineSlots.Count; i++)
{
slotId_ops.Add(GameManager.Inst.MachineSlots[i].id);
}
slotId.AddOptions(slotId_ops);
//devicePosition.text = "";
status.value = 0;
}
private void Start()
{
rackId.onValueChanged.AddListener(rackId_fuc);//机柜
//shelfId.onValueChanged.AddListener(shelfId_fuc);//机框
//slotId.onValueChanged.AddListener(slotId_fuc);//基槽
save_bt.onClick.AddListener(() =>
{
GameManager.Inst.editorMenu.gameObject.SetActive(false);
StartCoroutine(SaveJsonCoroutine());
});
}
private IEnumerator SaveJsonCoroutine()
{
yield return StartCoroutine(saveJson((x) =>
{
if (x != null && URlreturn != null && URlreturn.message == "操作成功")
{
//StartCoroutine(Succeed());
StartCoroutine(Succeed((x) =>
{
if (x)
{
DestroyImmediate(GameManager.Inst.nowDevice);
GameManager.Inst.lastDeviceID = URlreturn.data;
refurbish();
}
else
{
SecondConfirmPanel.DeleteConform(null, "更新场景失败");
Debug.Log("接口获取数据失败(新增设备-初始化失败)");
}
}));
}
else
{
Debug.Log("添加设备失败原因:" + URlreturn.message);
SecondConfirmPanel.DeleteConform(null, " 添加设备失败");
refurbish();
}
}));
}
///
/// 刷新
///
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();
SN1.hide_menu();
}
public IEnumerator Succeed(Action callback)
{
Array.ForEach(GameObject.FindObjectsOfType(), (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);
}));
}
private void slotId_fuc(int arg0)
{
//办卡
}
private void shelfId_fuc(int arg0)
{
//办卡
}
private void rackId_fuc(int arg0)
{
//该机柜下 机框-基槽
//rackId.options[arg0].text;//机柜ID
shelfId.options.Clear();
slotId.options.Clear();
List shelfId_ops = new List();
shelfId_ops.Add("");
for (int i = 0; i < GameManager.Inst.Racks_go.Count; i++)
{
var n = GameManager.Inst.Racks_go[i].GetComponent().deviceList;
if (n.rackId == rackId.options[arg0].text)
shelfId_ops.Add(n.id);
}
shelfId.AddOptions(shelfId_ops);
List slotId_ops = new List();
slotId_ops.Add("");
for (int i = 0; i < GameManager.Inst.MachineSlots_go.Count; i++)
{
var n = GameManager.Inst.MachineSlots_go[i].GetComponent().deviceList;
if (n.rackId == rackId.options[arg0].text)
slotId_ops.Add(n.id);
}
slotId.AddOptions(slotId_ops);
}
private void Update()
{
}
public IEnumerator saveJson(Action callback = null)
{
//if (type.value == 0 || deviceType.value == 0 || status.value == 2) yield break;
try
{
DragTest1 d = new DragTest1();
if (GameManager.Inst.nowDevice.GetComponent())
{
DeviceQuery dq = GameManager.Inst.nowDevice.GetComponent();
if (dq.deviceList.deviceType == "1")
{
d = GameManager.Inst.nowDevice.GetComponent();
mybody.occupyNum = d.volume.ToString();
mybody.modelNum = d.name.Replace("(Clone)", "");
mybody.devicePosition = d.transform.parent.name;
}
else if (dq.deviceList.deviceType == "3")
{
var cabinet = GameManager.Inst.nowDevice.transform.parent;
mybody.shelfId = cabinet.GetComponent().deviceList.id;
mybody.devicePosition = dq.deviceList.devicePosition;
}
}
mybody.type = type.value.ToString();
mybody.deviceType = deviceType.value.ToString();
//mybody.rackId = rackId.options[rackId.value].text;
//mybody.shelfId = shelfId.options[shelfId.value].text;
//mybody.slotId = slotId.options[slotId.value].text;
//mybody.devicePosition = devicePosition.text;
mybody.status = status.value.ToString();
}
catch (Exception e)
{
Debug.Log("新增设备错误:" + e.Message);
gameObject.SetActive(false);
callback.Invoke(null);
yield break;
}
var newData = JsonConvert.SerializeObject(mybody);
//var jsonResult = await CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.sb_xz, GameManager.Inst.token, newData);
//URlreturn = JsonConvert.DeserializeObject(jsonResult);
yield return
StartCoroutine(
CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.sb_xz, GameManager.Inst.arguments.token, newData, (jsonResult) =>
{
try
{
URlreturn = JsonConvert.DeserializeObject(jsonResult);
callback?.Invoke(URlreturn);
}
catch (Exception e)
{
Debug.Log("新增设备接口错误:" + e.Message);
callback.Invoke(null);
}
})
);
}
#region JSON
[System.Serializable]
public class Body
{
///
/// 名称
///
public string deviceName;
///
/// 设备编号
///
public string deviceCode;
///
/// 类型:1-机柜;2-设备
///
public string type;
///
///
///
public string typeStr;
///
/// 设备类型:1-机框;2-机槽;3-板卡
///
public string deviceType;
///
///
///
public string deviceTypeStr;
///
/// 所属机柜id
///
public string rackId;
///
/// 所属机框id
///
public string shelfId;
///
/// 所属机槽id
///
public string slotId;
///
/// 设备位置
///
public string devicePosition;
///
/// 机柜型号
///
public string machineModel;
///
/// 是否启用:1-正常;0-禁用
///
public string status;
///
///
///
public string conDeviceName;
///
/// 机柜类型:1-通信机柜;2-非通信机柜
///
public string rackType;
///
/// 柜门是否打开:1-开;0-关
///
public string openFlag;
///
/// 模型编号
///
public string modelNum;
///
/// U位占用数量
///
public string occupyNum;
///
///
///
public string residueNum;
///
/// 投运时间
///
public string operationTime;
///
/// 负责人
///
public string directorName;
///
/// 联系电话
///
public string phone;
///
/// 生产厂家
///
public string manufacturer;
///
/// 电源属性
///
public string powerProperties;
///
/// 维护单位
///
public string maintenanceUnit;
///
/// 机框高度
///
public string machineFrameHigh;
///
/// 机框宽度
///
public string machineFrameWide;
///
/// 机框厚度
///
public string machineFrameThick;
///
/// 插槽数量
///
public string slotNum;
///
/// 插槽排列方式
///
public string slotSort;
///
/// 安装方式
///
public string installMethod;
///
/// 所属机框
///
public string affiliationFrame;
///
/// 父插槽名称
///
public string parentSlotName;
///
/// 插槽类型
///
public string slotType;
///
/// 占用状态
///
public string occupyStatus;
///
/// 所属机槽名称
///
public string affiliationSlot;
///
/// 端口数量
///
public string portNum;
///
/// 板卡功能
///
public string cardFunction;
///
/// 板卡类型
///
public string cardType;
///
/// 运行状态
///
public string runStatus;
///
/// 备注
///
public string remark;
}
[System.Serializable]
public class Root
{
///
///
///
public string code;
///
/// 反馈结果
///
public string message;
///
/// 返回内容(ID)
///
public string data;
///
///
///
public string serverTime;
}
#endregion
}