//using AutoMapper; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; using static UnityEditor.PlayerSettings; /// /// 设备-新增 /// 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; /// /// 是否启用:1-正常;0-禁用 /// public Dropdown status; public InputField deviceName; public InputField deviceCode; /// /// 生产厂家 /// public InputField manufacturer; /// /// 负责人 /// public InputField directorName; public InputField phone; /// /// 投运时间 /// public InputField operationTime; /// /// 所属机柜id /// public Dropdown rackId; /// /// 所属机框id /// public Dropdown shelfId; /// /// 所属机槽id /// public Dropdown slotId; /// /// 设备位置 /// //public InputField devicePosition; //MapperConfiguration config; //IMapper mapper; DeviceQuery.DeviceList device; public Button close_bt; 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.IsFindRacks_go); mybody.shelfId = cabinet.GetComponent().deviceList.id; var upostItem = transform.parent.GetComponent(); if (upostItem) mybody.devicePosition = upostItem.gameObject.name; } } 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; findIndex(type, "设备"); if (GameManager.Inst.nowDevice.GetComponent().deviceList.deviceType == "3") findIndex(deviceType, "板卡"); else findIndex(deviceType, "机框"); findIndex(status, ""); deviceName.text = null; deviceCode.text = null; manufacturer.text = null; directorName.text = null; phone.text = null; operationTime.text = null; } 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()); }); close_bt.onClick.AddListener(() => { gameObject.SetActive(false); if (CabinetUIManager.Instance.current_menu == Menu.M_数字孪生_场景管理) GameManager.Inst.objectToShow_add.SetActive(true); }); } private IEnumerator SaveJsonCoroutine() { yield return StartCoroutine(saveJson((x) => { if (x != null && URlreturn != null && URlreturn.message == "操作成功") { //StartCoroutine(Succeed()); addInFo(); 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, " 添加设备失败" + URlreturn.message); refurbish(); } })); } /// /// 更新本地对象数据 /// public void addInFo() { if (GameManager.Inst.nowDevice) { var d = GameManager.Inst.nowDevice.GetComponent(); if (d) { d.deviceList.id = URlreturn.data; if (d.deviceList.type == "2" && d.deviceList.deviceType == "1") { GameManager.Inst.Racks_go.Add(GameManager.Inst.nowDevice); } else if (d.deviceList.type == "2" && d.deviceList.deviceType == "2") { GameManager.Inst.MachineSlots_go.Add(GameManager.Inst.nowDevice); } else if (d.deviceList.type == "2" && d.deviceList.deviceType == "3") { GameManager.Inst.TmsCards_go.Add(GameManager.Inst.nowDevice); } } } } /// /// 刷新 /// 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(); //var d = GameManager.Inst.nowDevice.GetComponent(); //if (d) //{ // foreach (var item in GameManager.Inst.root_AllDevice.data) // { // //(机柜) // if (item.type == "1") // GameManager.Inst.Cabinets.Add(item); // //(机框) // if (item.type == "2" && item.deviceType == "1") // GameManager.Inst.Racks.Add(item); // //(机槽) // else if (item.type == "2" && item.deviceType == "2") // GameManager.Inst.MachineSlots.Add(item); // //(板卡) // else if (item.type == "2" && item.deviceType == "3") // GameManager.Inst.TmsCards.Add(item); // } // if (d.deviceList.type == "2" && d.deviceList.deviceType == "1") // { // } // else if (d.deviceList.type == "2" && d.deviceList.deviceType == "2") // { // } // else if (d.deviceList.type == "2" && d.deviceList.deviceType == "3") // { // } //} } /// /// 寻找下拉菜单选项 /// /// /// public void findIndex(Dropdown _dro, string _name) { int index = _dro.options.FindIndex(x => x.text == _name); if (index != -1) { _dro.captionText.text = _dro.options[index].text; _dro.value = index; } } 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); }, true)); } 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 = string.IsNullOrEmpty(type.captionText.text) ? null : type.value.ToString(); mybody.deviceType = string.IsNullOrEmpty(deviceType.captionText.text) ? null : deviceType.value.ToString(); mybody.status = string.IsNullOrEmpty(status.captionText.text) ? null : status.value.ToString(); mybody.deviceName = !string.IsNullOrEmpty(deviceName.text) ? deviceName.text : null; mybody.deviceCode = !string.IsNullOrEmpty(deviceCode.text) ? deviceCode.text : null; mybody.manufacturer = !string.IsNullOrEmpty(manufacturer.text) ? manufacturer.text : null; mybody.directorName = !string.IsNullOrEmpty(directorName.text) ? directorName.text : null; mybody.phone = !string.IsNullOrEmpty(phone.text) ? phone.text : null; mybody.operationTime = !string.IsNullOrEmpty(operationTime.text) ? operationTime.text : null; } 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 }