//using AutoMapper;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using static GameManager;
///
/// 设备-编辑
///
public class RedactDevice : MonoBehaviour
{
public Body mybody = new Body();
public Root URlreturn = new Root();
DeviceQuery.DeviceList device = null;
///
/// 类型
///
public Dropdown type;
///
/// 设备类型
///
public Dropdown deviceType;
public InputField deviceName;
public InputField deviceCode;
public InputField devicePosition;
public Dropdown status;
public Dropdown rackType;
public Dropdown openFlag;
#region 机柜的
public InputField machineModel;
public InputField manufacturer;
#endregion
public Button save_bt;
public Text modelNum;
public Button close_bt;
//MapperConfiguration config;
//IMapper mapper;
public void OnEnable()
{
//Debug.Log("版本号 1.1.1");
mybody = new Body();
//config = new MapperConfiguration(cfg =>
//{
// cfg.CreateMap();
// cfg.CreateMap();
//});
//mapper = config.CreateMapper();
init();
try
{
if (!string.IsNullOrEmpty(GameManager.Inst.nowDeviceID))
{
for (int i = 0; i < GameManager.Inst.root_AllDevice.data.Count; i++)
{
if (GameManager.Inst.root_AllDevice.data[i].id == GameManager.Inst.nowDeviceID)
{
device = GameManager.Inst.root_AllDevice.data[i];
break;
}
}
//mybody = mapper.Map(device);
if (device != null)
{
mybody.id = device.id;
mybody.deviceName = device.deviceName;
mybody.deviceCode = device.deviceCode;
mybody.type = device.type;
mybody.typeStr = device.typeStr;
mybody.deviceType = device.deviceType;
mybody.deviceTypeStr = device.deviceTypeStr;
mybody.rackId = device.rackId;
mybody.shelfId = device.shelfId;
mybody.slotId = device.slotId;
mybody.devicePosition = device.devicePosition;
mybody.machineModel = device.machineModel;
mybody.status = device.status;
mybody.conDeviceName = device.conDeviceName;
mybody.rackType = device.rackType;
mybody.openFlag = device.openFlag;
mybody.backDoorFlag = device.backDoorFlag;
mybody.modelNum = device.modelNum;
mybody.occupyNum = device.occupyNum;
mybody.residueNum = device.residueNum;
mybody.operationTime = device.operationTime;
mybody.directorName = device.directorName;
mybody.phone = device.phone;
mybody.manufacturer = device.manufacturer;
mybody.powerProperties = device.powerProperties;
mybody.maintenanceUnit = device.maintenanceUnit;
mybody.machineFrameHigh = device.machineFrameHigh;
mybody.machineFrameWide = device.machineFrameWide;
mybody.machineFrameThick = device.machineFrameThick;
mybody.slotNum = device.slotNum;
mybody.slotSort = device.slotSort;
mybody.installMethod = device.installMethod;
mybody.affiliationFrame = device.affiliationFrame;
mybody.parentSlotName = device.parentSlotName;
mybody.slotType = device.slotType;
mybody.occupyStatus = device.occupyStatus;
mybody.affiliationSlot = device.affiliationSlot;
mybody.portNum = device.portNum;
mybody.cardFunction = device.cardFunction;
mybody.cardType = device.cardType;
mybody.runStatus = device.runStatus;
}
}
}
catch (Exception e)
{
Debug.Log(e.StackTrace + "**********" + e.Message);
}
if (type) type.value = int.Parse(String.IsNullOrEmpty(mybody.type) ? "0" : mybody.type);
if (deviceType) deviceType.value = String.IsNullOrEmpty(mybody.deviceType) ? 0 : int.Parse(mybody.deviceType);
if (deviceName) deviceName.text = mybody.deviceName;
if (deviceCode) deviceCode.text = mybody.deviceCode;
if (devicePosition) devicePosition.text = mybody.devicePosition;
if (machineModel) machineModel.text = mybody.machineModel;
if (status) status.value = int.Parse(String.IsNullOrEmpty(mybody.status) ? "2" : mybody.status);
if (rackType) rackType.value = String.IsNullOrEmpty(mybody.rackType) ? 0 : int.Parse(mybody.rackType);
if (openFlag) openFlag.value = String.IsNullOrEmpty(mybody.openFlag) ? 0 : int.Parse(mybody.openFlag);
if (manufacturer) manufacturer.text = mybody.manufacturer;
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;
if (devicePosition) devicePosition.text = mybody.devicePosition;
}
else if (GameManager.Inst.nowDevice.GetComponent().deviceList.deviceType == "3")
{
//var cabinet = GameManager.Inst.FindParent(GameManager.Inst.nowDevice, GameManager.Inst.IsDesiredParent);
mybody.shelfId = GameManager.Inst.nowDevice.transform.parent.GetComponent().deviceList.id;
}
if (!string.IsNullOrEmpty(mybody.modelNum))
modelNum.text = mybody.modelNum;
else
modelNum.text = null;
//Debug.Log("版本号 1.1.1");
}
public void init()
{
if (type) findIndex(type, "");
if (deviceType) findIndex(deviceType, "");
if (deviceName) deviceName.text = null;
if (deviceCode) deviceCode.text = null;
if (devicePosition) devicePosition.text = null;
if (machineModel) machineModel.text = null;
if (status) findIndex(status, "");
if (manufacturer) manufacturer.text = null;
mybody.rackId = null;
mybody.devicePosition = null;
mybody.shelfId = null;
}
private void Start()
{
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);
});
}
///
/// 寻找下拉菜单选项
///
///
///
public string 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;
return index.ToString();
}
return null;
}
private IEnumerator SaveJsonCoroutine()
{
yield return StartCoroutine(saveJson((y) =>
{
if (string.IsNullOrEmpty(y))
{
Debug.Log("编辑设备失败");
SecondConfirmPanel.DeleteConform(null, "编辑设备失败");
refurbish();
}
else
{
if (URlreturn != null && URlreturn.message == "操作成功")
{
StartCoroutine(Succeed((x) =>
{
if (x)
{
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 Update()
{
}
public IEnumerator saveJson(Action callback)
{
if (type.value == 0 || deviceType.value == 0 || status.value > 1) yield break;
if (string.IsNullOrEmpty(mybody.id)) yield break;
try
{
if (type) mybody.type = type.value.ToString();
if (deviceType) mybody.deviceType = deviceType.value == 0 ? null : deviceType.value.ToString();
if (deviceName) mybody.deviceName = deviceName.text;
if (deviceCode) mybody.deviceCode = deviceCode.text;
if (devicePosition) mybody.devicePosition = devicePosition.text;
if (machineModel) mybody.machineModel = machineModel.text;
if (status) mybody.status = status.value.ToString();
if (rackType) mybody.rackType = rackType.value == 0 ? null : rackType.value.ToString();
if (openFlag) mybody.openFlag = openFlag.value == 0 ? null : openFlag.value.ToString();
if (manufacturer) mybody.manufacturer = manufacturer.text;
}
catch (Exception e)
{
Debug.Log("修改设备错误:" + e.Message);
//gameObject.SetActive(false);
callback.Invoke(null);
yield break;
}
string newData = JsonConvert.SerializeObject(mybody);
yield return StartCoroutine(
CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.sb_bj, GameManager.Inst.arguments.token, newData, (jsonResult) =>
{
try
{
URlreturn = JsonConvert.DeserializeObject(jsonResult);
callback?.Invoke(jsonResult);
}
catch (Exception e)
{
Debug.Log("修改设备接口错误:" + e.Message);
callback.Invoke(null);
}
}
));
}
#region JSON
[System.Serializable]
public class Body
{
///
/// id
///
public string id;
///
/// 名称
///
public string deviceName;
///
/// 类型:1-机柜;2-设备
///
public string type;
///
/// 投运时间
///
public string operationTime;
///
/// 是否启用:1-正常;0-禁用
///
public string status;
///
/// 负责人
///
public string directorName;
///
/// 联系电话
///
public string phone;
///
/// 机柜型号
///
public string machineModel;
///
/// 机柜位置
///
public string machinePosition;
///
/// 编号
///
[Tooltip("编号")] public string deviceCode;
///
/// 类型:1-机柜;2-设备
///
[Tooltip("类型:1-机柜;2-设备")] public string typeStr;
///
/// 设备类型:1-机框;2-机槽;3-板卡
///
[Tooltip("设备类型:1-机框;2-机槽;3-板卡")] public string deviceType;
///
/// 设备类型:1-机框;2-机槽;3-板卡
///
[Tooltip("设备类型:1-机框;2-机槽;3-板卡")] public string deviceTypeStr;
///
/// 所属机柜id
///
[Tooltip("所属机柜id")] public string rackId;
///
/// 所属机框id
///
[Tooltip("所属机框id")] public string shelfId;
///
/// 所属机槽id
///
[Tooltip("所属机槽id")] public string slotId;
///
/// 设备位置
///
[Tooltip("设备位置")] public string devicePosition;
///
///
///
[Tooltip("")] public string conDeviceName;
///
/// 机柜类型:1-通信机柜;2-非通信机柜
///
[Tooltip("机柜类型:1-通信机柜;2-非通信机柜")] public string rackType;
///
/// 柜门是否打开:1-开;0-关
///
[Tooltip("柜门是否打开:1-开;0-关")] public string openFlag;
///
/// 后柜门是否打开:1-开;0-关
///
[Tooltip("后柜门是否打开:1-开;0-关")] public string backDoorFlag;
///
/// 模型编号
///
[Tooltip("模型编号")] public string modelNum;
///
/// U位占用数量
///
[Tooltip("U位占用数量")] public string occupyNum;
///
///
///
[Tooltip("")] public string residueNum;
///
/// 生产厂家
///
[Tooltip("生产厂家")] public string manufacturer;
///
/// 电源属性
///
[Tooltip("电源属性")] public string powerProperties;
///
/// 维护单位
///
[Tooltip("维护单位")] public string maintenanceUnit;
///
/// 机框高度
///
[Tooltip("机框高度")] public string machineFrameHigh;
///
/// 机框宽度
///
[Tooltip("机框宽度")] public string machineFrameWide;
///
/// 机框厚度
///
[Tooltip("机框厚度")] public string machineFrameThick;
///
/// 插槽数量
///
[Tooltip("插槽数量")] public string slotNum;
///
/// 插槽排列方式
///
[Tooltip("插槽排列方式")] public string slotSort;
///
/// 安装方式
///
[Tooltip("安装方式")] public string installMethod;
///
/// 所属机框
///
[Tooltip("所属机框")] public string affiliationFrame;
///
/// 父插槽名称
///
[Tooltip("父插槽名称")] public string parentSlotName;
///
/// 插槽类型
///
[Tooltip("插槽类型")] public string slotType;
///
/// 占用状态
///
[Tooltip("占用状态")] public string occupyStatus;
///
/// 所属机槽名称
///
[Tooltip("所属机槽名称")] public string affiliationSlot;
///
/// 端口数量
///
[Tooltip("端口数量")] public string portNum;
///
/// 板卡功能
///
[Tooltip("板卡功能")] public string cardFunction;
///
/// 板卡类型
///
[Tooltip("板卡类型")] public string cardType;
///
/// 运行状态
///
[Tooltip("运行状态")] public string runStatus;
///
/// 备注
///
[Tooltip("备注")] public string remark;
}
[System.Serializable]
public class Root
{
///
///
///
public string code;
///
/// 反馈结果
///
public string message;
///
/// 返回内容
///
public string data;
///
///
///
public string serverTime;
}
#endregion
}