123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- 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<PanelController>
- {
- [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<string> rlist = new Queue<string>();
- 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<string, string> requestHeader = new Dictionary<string, string>();
- public Dictionary<string, string> RequestHeader => requestHeader;
- public void Post(string methodName, string jsonString, Action<string> 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<string> 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<string, string> 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<string, string> 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;
- }
- }
|