using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Runtime.InteropServices; using UnityEngine.UI; using System.Text; using LitJson; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using UnityEngine.Networking; using uPLibrary.Networking.M2Mqtt.Messages; using Vuplex.WebView; using UnityEngine.Scripting; public class PanelController : MonoSingleton { [DllImport("__Internal")] private static extern void Connect(string host, string port, string clientId, string username, string password, string destination); [DllImport("__Internal")] private static extern void Subscribe(string topic); [DllImport("__Internal")] private static extern void Send(string topic, string payload); [DllImport("__Internal")] private static extern void Unsubscribe(string topic); [DllImport("__Internal")] private static extern void Disconnect(); // Start is called before the first frame update void Start() { StartCoroutine(UpdateTimer()); } public string id; public void HandleConnect(string host,string port,string clientId,string username,string password,string destination) { Debug.Log("unity: connect"); id = destination; Connect(host, port, clientId, username, password, destination); } public void HandleSubscribe(string topic) { Debug.Log("unity: subscribe"); Subscribe(topic); } public void HandleSend(string topic,string payload) { Debug.Log("unity: send"); Send(topic, payload); } public void HandleUnsubscribe(string topic) { Debug.Log("unity: unsubscribe"); Unsubscribe(topic); } public void HandleDisconnect() { Debug.Log("unity: disconnect"); Disconnect(); } Queue rlist = new Queue(); public void sendmqtt(bool b) { JsonData d = new JsonData(); d["device"] = "ROOM2"; HandleSend(id+"room/RTC",d.ToJson()); } public void SetLogScroll(string log) { ; rlist.Enqueue(log); Debug.Log("unity: "+log); //scrollLogText.text += "\n" + log; } void Update() { if (rlist.Count > 0) { for (int i = 0; i < rlist.Count; i++) { OnUnityReceived(rlist.Dequeue()); } } // GetCurrentDateTime(); } IEnumerator UpdateTimer() { GetCurrentDateTime(); yield return new WaitForFixedUpdate(); while (true) { yield return new WaitForSeconds(0.2f); GetCurrentDateTime(); } } public CanvasWebViewPrefab webview; public Text SENSO; public Text disp; public Text speed; public Text temperature; public Text humidity; public Text timer1; public Text timer2; //MQTT接收到的数据 public void OnUnityReceived(string obj) { Debug.Log("OnUnityReceived===>"+obj); string msg = obj; //Debug.Log("uid => " + obj.Topic + ":\n" + msg); JsonData data = JsonMapper.ToObject(msg); switch (data["device"]["deviceCode"].ToString()) { case "VOICE_SENSO_JSTZ01"://噪声 SENSO.text = data["value"].ToString() + data["unit"].ToString(); ModelDataManager.Instance.SetNoiseData(obj); break; case "VIBRATE_SENSOR_JSTZ01"://震动 disp.text = data["disp"]["value"].ToString() + data["disp"]["unit"].ToString(); speed.text = data["speed"]["value"].ToString() + data["speed"]["unit"].ToString(); ModelDataManager.Instance.SetOscData(obj); break; case "TEMPERATURE_HUMIDITY_SENSO_JSTZ01"://温湿度 temperature.text = data["temperature"]["value"].ToString() + data["temperature"]["unit"].ToString(); humidity.text = data["humidity"]["value"].ToString() + data["humidity"]["unit"].ToString(); ModelDataManager.Instance.SetHumitdlyData(obj); break; case "ROOM": Debug.Log("ROOM12312312312" + data); Debug.Log("ROOM12312312312" + data["roomId"].ToString()); JsonData joindata = new JsonData(); joindata["roomId"] = data["roomId"]; Post("https://api-fat2.ghz-tech.com" + "/cmcc-endustry/v1/rtc/joinRoom", joindata.ToJson(), (string data)=> { Debug.Log("1111112312312312312"+ data); JsonData jdata = JsonMapper.ToObject(data); webview.InitialUrl = "https://meet.livekit.io/custom?liveKitUrl=wss://livekit-aws.ghz-tech.com&token="+ jdata["data"]["token"].ToString(); webview.gameObject.SetActive(true); webview.WebView?.LoadUrl(webview.InitialUrl); }); break; } // UILogManager.Instance.text3.text = msg; // MQTTManager.Instance.Received(msg); // MQTTManager.Instance.Received(obj); } private Dictionary requestHeader = new Dictionary(); public Dictionary RequestHeader => requestHeader; public void Post(string methodName, string jsonString, Action CallBack) { StartCoroutine(SendHttp(methodName, jsonString, CallBack)); } public void initHead(string ContentType) { requestHeader.Clear(); requestHeader.Add("authorization", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ4NzQxMTY1MDIsImlhdCI6MTcyMDUxNjUwMiwidXNlciI6eyJpZCI6MTA5MiwiZW1haWwiOiIiLCJwaG9uZSI6InprZGVtbyIsIm5hbWUiOiJ6a2RlbW8iLCJyb2xlSWQiOjF9fQ.OZ2HDUp9F5S504cmCUh7Ai15sq_nQ5i7ZG_odxhqGGM"); requestHeader.Add("Content-Type", ContentType); } public IEnumerator SendHttp(string methodName, string jsonString, Action CallBack, string ContentType = "application/json", bool isPost = true) { Debug.Log("Start Queue SendHttp " + methodName); string text = (!methodName.Contains("http")) ? ("https://api-fat2.ghz-tech.com" + methodName) : methodName; Debug.Log("URL:" + text + " isPost " + isPost); UnityWebRequest webRequest = isPost ? new UnityWebRequest(text, "POST") : UnityWebRequest.Get(text); initHead(ContentType); using (webRequest) { if (!isPost) { webRequest.downloadHandler = new DownloadHandlerBuffer(); foreach (KeyValuePair item in requestHeader) { webRequest.SetRequestHeader(item.Key, item.Value); } } else { byte[] bytes = Encoding.UTF8.GetBytes(jsonString); webRequest.uploadHandler = new UploadHandlerRaw(bytes); webRequest.downloadHandler = new DownloadHandlerBuffer(); } foreach (KeyValuePair item2 in requestHeader) { webRequest.SetRequestHeader(item2.Key, item2.Value); } yield return webRequest.SendWebRequest(); Debug.Log("CallBack==>" + webRequest.downloadHandler.text); if (webRequest.isHttpError || webRequest.isNetworkError) { string text2 = webRequest.downloadHandler.text; CallBack(text2); } else { string text3 = webRequest.downloadHandler.text; CallBack(text3); } } } public void GetCurrentDateTime() { var now = DateTime.Now; // 获取当前本地时间 string timer1Str = now.Year + "."; if (now.Month < 10) { timer1Str = timer1Str + "0" + now.Month + "."; } else { timer1Str = timer1Str + now.Month + "."; } if (now.Day < 10) { timer1Str = timer1Str + "0" + now.Day; } else { timer1Str = timer1Str + now.Day; } switch (now.DayOfWeek) { case DayOfWeek.Friday: timer1Str = timer1Str + " 星期五"; break; case DayOfWeek.Monday: timer1Str = timer1Str + " 星期一"; break; case DayOfWeek.Saturday: timer1Str = timer1Str + " 星期六"; break; case DayOfWeek.Sunday: timer1Str = timer1Str + " 星期日"; break; case DayOfWeek.Thursday: timer1Str = timer1Str + " 星期四"; break; case DayOfWeek.Tuesday: timer1Str = timer1Str + " 星期二"; break; case DayOfWeek.Wednesday: timer1Str = timer1Str + " 星期三"; break; default: break; } timer1.text = timer1Str; string timer2Str="" ; if (now.Hour < 10) { timer2Str = timer2Str + "0" + now.Hour+":"; } else { timer2Str = timer2Str + now.Hour + ":"; } if (now.Minute < 10) { timer2Str = timer2Str + "0" + now.Minute + ":"; } else { timer2Str = timer2Str + now.Minute + ":"; } if (now.Second < 10) { timer2Str = timer2Str + "0" + now.Second; } else { timer2Str = timer2Str + now.Second; } timer2.text = timer2Str; } }