using Newtonsoft.Json;
using SimpleJSON;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using static GameManager;
public class WebInteraction : MonoBehaviour
{
public string s;
public static WebInteraction Inst;
public string current_videoNumber = null;
///
/// 视频监控是否为打开状态
///
public bool isVideoPlay = false;
///
/// 工作票是否为打开状态
///
public bool isWorkPlay = false;
public GameManager gameManager;
///
/// 机器人网页跳转按钮
///
public Button btnRobotWebPage;
///
/// 巡检数据
///
public string inspectionData;
[Serializable]
public class Menu_Struct
{
public Toggle toggle;
public Menu menu;
///
/// 菜单初始状态
///
[Tooltip("菜单初始状态")] public bool isState;
///
/// 是否为二级菜单
///
[Tooltip("是否为二级菜单")] public bool isSecond_level;
///
/// Toggle初始isOn
///
[Tooltip("Toggle初始isOn")] public bool _ison;
}
///
/// 所有toogle
///
public List menu_struct;
[Header("菜单权限")]
public Root root = new Root();
//public List toggles;
///
/// 二级菜单Toggle父物体
///
public List secondary_menu;
public GameObject first_level_menu;
public List second_level_menu;
///
/// 机器人
///
public List robotPos = new List();
///
/// 记录权限开放的菜单名
///
public List menu_name = new List();
private void Awake()
{
Inst = this;
#if UNITY_EDITOR
// 在编辑器中运行时执行的代码
StartCoroutine(LoadAddress((ct) =>
{
if (ct != null)
{
unity_token_value(ct);
}
else
{
Debug.Log("获取地址错误!");
}
}));
// 在编辑器中运行时执行的代码
StartCoroutine(Inspection((ct) =>
{
if (ct != null)
{
unity_Inspection_value(ct);
}
else
{
Debug.Log("获取地址错误!");
}
}));
// 在编辑器中运行时执行的代码
StartCoroutine(Track((ct) =>
{
if (ct != null)
{
robotPos= JsonConvert.DeserializeObject>(ct);
}
else
{
Debug.Log("获取地址错误!");
}
}));
#endif
//StartCoroutine(QiangZhi());
}
private void Start()
{
//Debug.Log("版本号 1.1.1");
bt.onClick.AddListener(() =>
{
//gameManager.token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2OTI2NDMyODksInVzZXJuYW1lIjoiMTgzMTIzNDU2NzgifQ.uMoC7I4SytLEgYTKHxyvOv7Lygeb9zDCmhLDYv1cJrA";
//gameManager.enabled = true;
unity_power_value(s);
});
//Debug.Log("版本号 1.1.1");
btnRobotWebPage.onClick.AddListener(() => {
OpenRobotWebPage();
});
}
//前端调用Unity 方法逻辑
//参数1 脚本所挂载的物体名 GameManager
//参数2 脚本内方法名 unity_token_value
//参数3 传递的参数 token值
///
/// 前端调用此方法传递Token
///
///
public void unity_token_value(string jsonData)
{
try
{
JSONNode dataNode = JSON.Parse(jsonData);
gameManager.arguments.token = dataNode["data"]["token"].Value;
gameManager.arguments.heatThresholdValueMax = dataNode["data"]["heatThresholdValueMax"].Value;
gameManager.arguments.heatThresholdValueMin = dataNode["data"]["heatThresholdValueMin"].Value;
gameManager.arguments.humidityThresholdValueMax = dataNode["data"]["humidityThresholdValueMax"].Value;
gameManager.arguments.humidityThresholdValueMin = dataNode["data"]["humidityThresholdValueMin"].Value;
Debug.Log("温度最大阈值 " + gameManager.arguments.heatThresholdValueMax);
Debug.Log("温度最小阈值 " + gameManager.arguments.heatThresholdValueMin);
Debug.Log("湿度最大阈值 " + gameManager.arguments.humidityThresholdValueMax);
Debug.Log("湿度最小阈值 " + gameManager.arguments.humidityThresholdValueMin);
}
catch (Exception)
{
Debug.Log("解析Token错误");
}
//yield return null;
StartCoroutine(GameManager.Inst.LoadAddress((ct) =>
{
//加载场景
if (ct != null)
{
GameManager.Inst.Jk_URL = new webURL(ct);
//Application.ExternalCall("OnSceneLoaded", "三维加载完成");
//Debug.Log("三维加载完成");
//return;
StartCoroutine(GameManager.Inst.Initialize((x) =>
{
if (!string.IsNullOrEmpty(x))
{
#region 循环读取信息
if (!gameManager.isgonging)
{
StartCoroutine(
CombineJSON.GetJson_POST(GameManager.Inst.Jk_URL.gj_cx, GameManager.Inst.arguments.token, 1800, (jsonResult3) =>
{
if (!string.IsNullOrEmpty(jsonResult3))
{
try
{
gameManager.root_AllAlarm = JsonConvert.DeserializeObject(jsonResult3);
Array.ForEach(GameObject.FindObjectsOfType(), itme =>
{
itme.tmsAlarmLists.Clear();
});
gameManager.tmsAlarms.Clear();
gameManager.Load_TmsAlarm();
}
catch (Exception e)
{
Debug.Log("序列化-循环告警查询错误:" + e.Message);
}
}
}));
StartCoroutine(
CombineJSON.GetJson_POST(GameManager.Inst.Jk_URL.hj_cx, GameManager.Inst.arguments.token, 1800, (jsonResult2) =>
{
if (!string.IsNullOrEmpty(jsonResult2))
{
try
{
gameManager.root_AllENV = JsonConvert.DeserializeObject(jsonResult2);
gameManager.ENVs.Clear();
gameManager.Load_ENV();
}
catch (Exception e)
{
Debug.Log("序列化-循环环境查询错误:" + e.Message);
}
}
}));
}
#endregion
gameManager.isgonging = true;
}
else
{
SecondConfirmPanel.DeleteConform(null, "进入场景失败");
Debug.Log("接口获取数据失败(初始化失败)");
gameManager.LoadScene();
}
}));
}
else
{
SecondConfirmPanel.DeleteConform(null, "接口地址错误");
Debug.Log("获取地址错误!");
}
}));
}
///
/// 前端调用此方法传递Token
///
///
public void unity_Inspection_value(string jsonData)
{
try
{
inspectionData = jsonData;
}
catch (Exception)
{
Debug.Log("解析Token错误");
}
}
public void unity_power_value(string roleId)
{
Debug.Log("roleId:" + roleId);
Debug.Log("@@QX1");
#region 权限
//加载权限
//bool isgo = false;
try
{
Debug.Log("@@QX2");
init_menu();
Debug.Log("@@QX3");
closeMenu();
Debug.Log("@@QX4");
root = JsonConvert.DeserializeObject(roleId);
Debug.Log("@@QX5");
if (root.message == "操作成功")
{
Debug.Log("@@QX6");
menu_name.Clear();
if (root.data != null && root.data.Count > 0)
{
for (int i = 0; i < root.data.Count; i++)
{
menu_name.Add(root.data[i].menuName);
foreach (var item in root.data[i].children)
{
menu_name.Add(item.menuName);
for(int j=0;j< item.children.Count; j++)
{
menu_name.Add(item.children[j].menuName);
}
}
}
Debug.Log("@@QX7");
openMenu();
Debug.Log("@@QX8");
setMenuIsOn_3();
setMenuIsOn_2();
Debug.Log("@@QX9");
setMenuIsOn_1();
Debug.Log("权限加载完成");
}
else
{
SecondConfirmPanel.DeleteConform(null, "暂无权限");
Debug.Log("@@QX10");
}
}
else
{
SecondConfirmPanel.DeleteConform(null, "获取权限失败");
Debug.Log("@@QX11");
}
}
catch (Exception e)
{
SecondConfirmPanel.DeleteConform(null, "权限加载失败");
Debug.Log("权限加载错误:" + e.Message + "\n" + e.StackTrace);
}
Debug.Log("@@QX13");
//if (!isgo)
// return;
for (int i = 0; i < menu_struct.Count; i++)
{
Debug.Log(menu_struct[i].toggle.name
+ "\n物体显隐: " + menu_struct[i].toggle.gameObject.activeSelf
+ "\n初始显隐: " + menu_struct[i].isState
+ "\n物体IsOn: " + menu_struct[i].toggle.isOn
+ "\n初始IsOn:" + menu_struct[i]._ison);
}
#endregion
}
///
/// 初始化菜单信息
///
public void init_menu()
{
for (int i = 0; i < menu_struct.Count; i++)
{
menu_struct[i]._ison = false;
menu_struct[i].isState = false;
}
}
///
/// 设置一级菜单初始IsOn
///
public void setMenuIsOn_1()
{
List ms = new List();
ms.Add(menu_struct[0]);
ms.Add(menu_struct[1]);
ms.Add(menu_struct[2]);
bool isFind_ison = false;
for (int i = 1; i < ms.Count; i++)
{
if (!isFind_ison && ms[i].toggle.gameObject.activeSelf)
{
ms[i]._ison = true;
ms[i].toggle.isOn = true;
isFind_ison = true;
}
else
ms[i]._ison = false;
}
menu_struct[0] = ms[0];
menu_struct[1] = ms[1];
menu_struct[2] = ms[2];
}
///
/// 设置二级菜单初始IsOn
///
public void setMenuIsOn_2()
{
for (int i = 0; i < secondary_menu.Count; i++)
{
bool isFind_ison = false;
for (int j = 0; j < secondary_menu[i].childCount; j++)
{
if (!isFind_ison && secondary_menu[i].GetChild(j).gameObject.activeSelf)
{
secondary_menu[i].GetChild(j).GetComponent().isOn = true;
isFind_ison = true;
foreach (var item in menu_struct)
{
if (item.toggle == secondary_menu[i].GetChild(j).GetComponent())
{
item._ison = true;
item.toggle.isOn = true;
break;
}
}
}
else
secondary_menu[i].GetChild(j).GetComponent().isOn = false;
}
}
}
///
/// 设置三级菜单初始IsOn
///
public void setMenuIsOn_3()
{
Menu_Struct menu_Struct = new Menu_Struct();
menu_Struct = menu_struct[12];
menu_Struct._ison = true;
menu_Struct.toggle.isOn = true;
}
///
/// 隐藏所有菜单
///
public void closeMenu()
{
for (int i = 0; i < menu_struct.Count; i++)
{
menu_struct[i].toggle.gameObject.SetActive(false);
}
for (int i = 0; i < second_level_menu.Count; i++)
second_level_menu[i].SetActive(false);
first_level_menu.SetActive(false);
//for (int i = 0; i < toggles.Count; i++)
// toggles[i].gameObject.SetActive(false);
}
///
/// 根据获取到的权限显示菜单
///
public void openMenu()
{
for (int i = 0; i < menu_struct.Count; i++)
{
for (int j = 0; j < menu_name.Count; j++)
{
//if (menu_struct[i].toggle.gameObject.name.Split('_')[1] == menu_name[j])
if (menu_struct[i].toggle.gameObject.name.Contains(menu_name[j]))
{
menu_struct[i].toggle.gameObject.SetActive(true);
menu_struct[i].isState = true;
break;
}
}
}
}
///
/// 当前有画面的摄像头编号
///
///
public void unity_video_state(string videoNumber)
{
if (CabinetUIManager.Instance.current_menu == Menu.M_全景监控_现场作业) return;
for (int i = 0; i < PatternChoose.Inst.uiToCabinet.clickEventLens.Count; i++)
{
PatternChoose.Inst.uiToCabinet.clickEventLens[i].isplay = false;
PatternChoose.Inst.uiToCabinet.clickEventLens[i].ChangeMaterialColor(PatternChoose.Inst.uiToCabinet.clickEventLens[i].gameObject, 99);
}
if (!string.IsNullOrEmpty(videoNumber))
{
var _videoNumbers = videoNumber.Split(',');
for (int i = 0; i < PatternChoose.Inst.uiToCabinet.clickEventLens.Count; i++)
{
for (int j = 0; j < _videoNumbers.Length; j++)
{
if (_videoNumbers[j] == PatternChoose.Inst.uiToCabinet.clickEventLens[i].text.text.Replace("摄像", ""))
{
PatternChoose.Inst.uiToCabinet.clickEventLens[i].isplay = true;
PatternChoose.Inst.uiToCabinet.clickEventLens[i].ChangeMaterialColor(PatternChoose.Inst.uiToCabinet.clickEventLens[i].gameObject, 4);
break;
}
}
}
}
}
public Button bt;
public IEnumerator QiangZhi()
{
string filePath = Application.streamingAssetsPath + "/tk.txt";
UnityWebRequest www = UnityWebRequest.Get(filePath);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
string fileContent = www.downloadHandler.text;
if (string.IsNullOrEmpty(fileContent)) yield break;
gameManager.arguments.token = fileContent;
}
}
// 读取地址
public IEnumerator LoadAddress(Action callback)
{
#region UnityWebRequest读取
string filePath = Application.streamingAssetsPath + "/editorDebugging.txt";
UnityWebRequest www = UnityWebRequest.Get(filePath);
DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
www.downloadHandler = dH;
//www.timeout = 15;
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
// 等待一帧
yield return null;
// 检查请求是否已完成
while (!www.isDone)
{
yield return null;
}
string fileContent = www.downloadHandler.text;
if (string.IsNullOrEmpty(fileContent))
{
callback(null);
}
else
{
Debug.Log("获取穿透成功");
callback(fileContent);
}
}
else
{
Debug.LogError("获取穿透失败: " + www.error);
callback(null);
}
#endregion
//yield return 1;
//callback("http://192.168.41.106:8081/");
}
public IEnumerator Inspection(Action callback)
{
#region UnityWebRequest读取
string filePath = Application.streamingAssetsPath + "/InspectionData.json";
UnityWebRequest www = UnityWebRequest.Get(filePath);
DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
www.downloadHandler = dH;
//www.timeout = 15;
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
// 等待一帧
yield return null;
// 检查请求是否已完成
while (!www.isDone)
{
yield return null;
}
string fileContent = www.downloadHandler.text;
if (string.IsNullOrEmpty(fileContent))
{
callback(null);
}
else
{
Debug.Log("获取穿透成功");
callback(fileContent);
}
}
else
{
Debug.LogError("获取穿透失败: " + www.error);
callback(null);
}
#endregion
//yield return 1;
//callback("http://192.168.41.106:8081/");
}
///
/// 机器人轨迹测试
///
///
///
public IEnumerator Track(Action callback)
{
#region UnityWebRequest读取
string filePath = Application.streamingAssetsPath + "/track.txt";
UnityWebRequest www = UnityWebRequest.Get(filePath);
DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
www.downloadHandler = dH;
//www.timeout = 15;
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
// 等待一帧
yield return null;
// 检查请求是否已完成
while (!www.isDone)
{
yield return null;
}
string fileContent = www.downloadHandler.text;
if (string.IsNullOrEmpty(fileContent))
{
callback(null);
}
else
{
Debug.Log("获取穿透成功");
callback(fileContent);
}
}
else
{
Debug.LogError("获取穿透失败: " + www.error);
callback(null);
}
#endregion
//yield return 1;
//callback("http://192.168.41.106:8081/");
}
///
/// 发送视频监控坐标的函数
///
///
///
public void SendVideoPosition(float x, float y)
{
// 在此处将视频监控坐标发送给前端
Debug.Log("Sending video position: (" + x + ", " + y + ")");
Application.ExternalCall("web_video_position", x + "," + y);
}
///
/// 关闭视频监控的函数
///
///
public void CloseVideo(string videoNumber)
{
// 在此处处理关闭视频监控的逻辑
Debug.Log("Closing video number: " + videoNumber);
isVideoPlay = false;
Application.ExternalCall("web_video_close", videoNumber);
}
///
/// 打开视频监控的函数(isVideo="0"视频,1是图片)
///
///
public void OpenVideo(string videoNumber, string isVideo = "0")
{
// 在此处处理打开视频监控的逻辑
Debug.Log("Opening video number: " + videoNumber);
Application.ExternalCall("web_video_open", videoNumber, isVideo);
}
///
/// 打开现场作业的函数
///
public void OpenTicket()
{
// 在此处处理打开现场作业的逻辑
Debug.Log("Opening ticket");
Application.ExternalCall("web_ticket_open", "");
}
///
/// 关闭现场作业的函数
///
public void CloseTicket()
{
// 在此处处理关闭现场作业的逻辑
Debug.Log("Closing ticket");
Application.ExternalCall("web_ticket_close", "");
//foreach (var item in GameManager.Inst.Cabinets_go)
//{
// item.transform.Find("高亮").gameObject.SetActive(false);
//}
for (int i = 0; i < CabinetUIManager.Instance.cabine_hights.Count; i++)
{
CabinetUIManager.Instance.cabine_hights[i].SetActive(false);
}
}
///
/// 打开机器人网页
///
public void OpenRobotWebPage()
{
// 在此处处理打开现场作业的逻辑
Debug.Log("机器人网页");
Application.ExternalCall("web_skip", "");
}
///
///
///
///
public void TestSend(string data)
{
Application.ExternalCall("TestSend", data);
}
[ContextMenu("工作票设备关联的函数")]
///
/// 工作票设备关联机柜变色
///
///
public void unity_ticket_cabinet(string deviceNumbers)
{
CabinetUIManager.Instance.cabine_hights_now.Clear();
//if (gameManager.magnifyState)
// return;
for (int i = 0; i < GameManager.Inst.Cabinets_go.Count; i++)
{
var hight = GameManager.Inst.Cabinets_go[i].transform.Find("高亮").gameObject;
hight.SetActive(false);
}
var devices = deviceNumbers.Split(',');
List ls = new List();
ls.AddRange(GameManager.Inst.Cabinets_go);
ls.AddRange(GameManager.Inst.Racks_go);
for (int i = 0; i < ls.Count; i++)
{
for (int j = 0; j < devices.Length; j++)
{
if (devices[j] == ls[i].GetComponent().deviceList.id)
{
var Cabinet = gameManager.FindParent(ls[i].gameObject, gameManager.IsDesiredParent);
if (Cabinet)
{
var hight = Cabinet.transform.Find("高亮").gameObject;
if (!gameManager.magnifyState)
{
changC(hight, "1");
}
else
CabinetUIManager.Instance.cabine_hights_now.Add(hight);
break;
}
}
}
}
}
///
/// 工作票设备关联机柜镜头聚焦
///
///
public void unity_ticket_locate_cabinet(string deviceNumbers)
{
List ls = new List();
ls.AddRange(GameManager.Inst.Cabinets_go);
for (int i = 0; i < ls.Count; i++)
{
if (ls[i].GetComponent().deviceList.id == deviceNumbers)
{
ls[i].GetComponent().Change_hide(ls[i], true, true);
break;
}
}
}
///
/// 巡检画面开
///
public void Patrolvideo_open()
{
Application.ExternalCall("unity_patrolvideo_open", "巡检画面开");
}
///
/// 巡检画面关
///
public void Patrolvideo_close()
{
Application.ExternalCall("unity_patrolvideo_close", "巡检画面关");
}
///
/// 打开红外
///
///
public void Infraredsensor_open(string num)
{
Application.ExternalCall("web_infraredsensor_open", "红外画面开");
}
///
/// 关闭红外
///
///
public void Infraredsensor_close()
{
Application.ExternalCall("web_infraredsensor_close", "红外画面关");
}
///
/// 隐藏巡检画面
///
public void setbigXunJian()
{
Application.ExternalCall("web_Inspection_popup_magnify", "隐藏巡检画面");
}
///
/// 显示巡检画面
///
public void setlittleXunJian()
{
Application.ExternalCall("web_Inspection_popup_narrow", "显示巡检画面");
}
///
/// 打开AR视频
///
public void ARVideo_open()
{
Application.ExternalCall("web_ar_video_open", "AR画面开启");
}
///
/// 关闭AR视频
///
public void ARVideo_close()
{
Application.ExternalCall("web_ar_video_close", "AR画面关闭");
}
public void changC(GameObject hight, string index)
{
hight.SetActive(true);
Renderer renderer = hight.GetComponent();
Material[] sharedMaterials = renderer.sharedMaterials;
//for (int i = 0; i < sharedMaterials.Length; i++)
//{
// sharedMaterials[i] = newMaterial;
//}
sharedMaterials[0] = Resources.Load("Materials/" + index + "Tou");
renderer.sharedMaterials = sharedMaterials;
}
[System.Serializable]
public class ChildrenTwo
{
///
///
///
public string id { get; set; }
///
///
///
public int roleFlag { get; set; }
///
///
///
public string parentId { get; set; }
///
///
///
public string parentName { get; set; }
///
/// 设备告警
///
public string menuName { get; set; }
///
///
///
public List children { get; set; }
///
///
///
public string code { get; set; }
///
///
///
public int menuType { get; set; }
///
///
///
public int sort { get; set; }
///
///
///
public string webIcon { get; set; }
///
///
///
public string webRoute { get; set; }
///
///
///
public string component { get; set; }
///
///
///
public string type { get; set; }
///
///
///
public int status { get; set; }
///
///
///
public string hidden { get; set; }
///
///
///
public string remark { get; set; }
///
///
///
public string createTime { get; set; }
///
///
///
public string createName { get; set; }
}
public class Children
{
///
///
///
public string id { get; set; }
///
///
///
public int roleFlag { get; set; }
///
///
///
public string parentId { get; set; }
///
///
///
public string parentName { get; set; }
///
/// 全景监控
///
public string menuName { get; set; }
///
///
///
public List children { get; set; }
///
///
///
public string code { get; set; }
///
///
///
public int menuType { get; set; }
///
///
///
public int sort { get; set; }
///
///
///
public string webIcon { get; set; }
///
///
///
public string webRoute { get; set; }
///
///
///
public string component { get; set; }
///
///
///
public string type { get; set; }
///
///
///
public int status { get; set; }
///
///
///
public string hidden { get; set; }
///
///
///
public string remark { get; set; }
///
///
///
public string createTime { get; set; }
///
///
///
public string createName { get; set; }
}
[System.Serializable]
public class Data
{
///
///
///
public string id { get; set; }
///
///
///
public int roleFlag { get; set; }
///
///
///
public string parentId { get; set; }
///
///
///
public string parentName { get; set; }
///
/// 智慧运维
///
public string menuName { get; set; }
///
///
///
public List children { get; set; }
///
///
///
public string code { get; set; }
///
///
///
public int menuType { get; set; }
///
///
///
public int sort { get; set; }
///
///
///
public string webIcon { get; set; }
///
///
///
public string webRoute { get; set; }
///
///
///
public string component { get; set; }
///
///
///
public string type { get; set; }
///
///
///
public int status { get; set; }
///
///
///
public string hidden { get; set; }
///
///
///
public string remark { get; set; }
///
///
///
public string createTime { get; set; }
///
///
///
public string createName { get; set; }
}
[System.Serializable]
public class Root
{
///
///
///
public string code;
///
/// 操作成功
///
public string message;
///
///
///
public List data;
///
///
///
public string serverTime;
}
}
public static class BoolMonitor
{
private static bool _value = false;
///
/// 镜头是否为不动状态
///
public static bool Value
{
get { return _value; }
set
{
if (_value != value)
{
_value = value;
OnValueChanged();
}
}
}
public static event EventHandler ValueChanged;
static void OnValueChanged()
{
ValueChanged?.Invoke(null, EventArgs.Empty);
}
}