PanelController.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine.UI;
  6. using System.Text;
  7. using LitJson;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10. using System;
  11. using UnityEngine.Networking;
  12. using uPLibrary.Networking.M2Mqtt.Messages;
  13. using Vuplex.WebView;
  14. using UnityEngine.Scripting;
  15. public class PanelController : MonoSingleton<PanelController>
  16. {
  17. [DllImport("__Internal")]
  18. private static extern void Connect(string host, string port, string clientId, string username, string password, string destination);
  19. [DllImport("__Internal")]
  20. private static extern void Subscribe(string topic);
  21. [DllImport("__Internal")]
  22. private static extern void Send(string topic, string payload);
  23. [DllImport("__Internal")]
  24. private static extern void Unsubscribe(string topic);
  25. [DllImport("__Internal")]
  26. private static extern void Disconnect();
  27. // Start is called before the first frame update
  28. void Start()
  29. {
  30. StartCoroutine(UpdateTimer());
  31. }
  32. public string id;
  33. public void HandleConnect(string host,string port,string clientId,string username,string password,string destination) {
  34. Debug.Log("unity: connect");
  35. id = destination;
  36. Connect(host, port, clientId, username, password, destination);
  37. }
  38. public void HandleSubscribe(string topic) {
  39. Debug.Log("unity: subscribe");
  40. Subscribe(topic);
  41. }
  42. public void HandleSend(string topic,string payload) {
  43. Debug.Log("unity: send");
  44. Send(topic, payload);
  45. }
  46. public void HandleUnsubscribe(string topic) {
  47. Debug.Log("unity: unsubscribe");
  48. Unsubscribe(topic);
  49. }
  50. public void HandleDisconnect() {
  51. Debug.Log("unity: disconnect");
  52. Disconnect();
  53. }
  54. Queue<string> rlist = new Queue<string>();
  55. public void sendmqtt(bool b)
  56. {
  57. JsonData d = new JsonData();
  58. d["device"] = "ROOM2";
  59. HandleSend(id+"room/RTC",d.ToJson());
  60. }
  61. public void SetLogScroll(string log)
  62. {
  63. ;
  64. rlist.Enqueue(log);
  65. Debug.Log("unity: "+log);
  66. //scrollLogText.text += "\n" + log;
  67. }
  68. void Update()
  69. {
  70. if (rlist.Count > 0)
  71. {
  72. for (int i = 0; i < rlist.Count; i++)
  73. {
  74. OnUnityReceived(rlist.Dequeue());
  75. }
  76. }
  77. // GetCurrentDateTime();
  78. }
  79. IEnumerator UpdateTimer()
  80. {
  81. GetCurrentDateTime();
  82. yield return new WaitForFixedUpdate();
  83. while (true)
  84. {
  85. yield return new WaitForSeconds(0.2f);
  86. GetCurrentDateTime();
  87. }
  88. }
  89. public CanvasWebViewPrefab webview;
  90. public Text SENSO;
  91. public Text disp;
  92. public Text speed;
  93. public Text temperature;
  94. public Text humidity;
  95. public Text timer1;
  96. public Text timer2;
  97. //MQTT接收到的数据
  98. public void OnUnityReceived(string obj)
  99. {
  100. Debug.Log("OnUnityReceived===>"+obj);
  101. string msg = obj;
  102. //Debug.Log("uid => " + obj.Topic + ":\n" + msg);
  103. JsonData data = JsonMapper.ToObject(msg);
  104. switch (data["device"]["deviceCode"].ToString())
  105. {
  106. case "VOICE_SENSO_JSTZ01"://噪声
  107. SENSO.text = data["value"].ToString() + data["unit"].ToString();
  108. ModelDataManager.Instance.SetNoiseData(obj);
  109. break;
  110. case "VIBRATE_SENSOR_JSTZ01"://震动
  111. disp.text = data["disp"]["value"].ToString() + data["disp"]["unit"].ToString();
  112. speed.text = data["speed"]["value"].ToString() + data["speed"]["unit"].ToString();
  113. ModelDataManager.Instance.SetOscData(obj);
  114. break;
  115. case "TEMPERATURE_HUMIDITY_SENSO_JSTZ01"://温湿度
  116. temperature.text = data["temperature"]["value"].ToString() + data["temperature"]["unit"].ToString();
  117. humidity.text = data["humidity"]["value"].ToString() + data["humidity"]["unit"].ToString();
  118. ModelDataManager.Instance.SetHumitdlyData(obj);
  119. break;
  120. case "ROOM":
  121. Debug.Log("ROOM12312312312" + data);
  122. Debug.Log("ROOM12312312312" + data["roomId"].ToString());
  123. JsonData joindata = new JsonData();
  124. joindata["roomId"] = data["roomId"];
  125. Post("https://api-fat2.ghz-tech.com" + "/cmcc-endustry/v1/rtc/joinRoom", joindata.ToJson(), (string data)=> {
  126. Debug.Log("1111112312312312312"+ data);
  127. JsonData jdata = JsonMapper.ToObject(data);
  128. webview.InitialUrl = "https://meet.livekit.io/custom?liveKitUrl=wss://livekit-aws.ghz-tech.com&token="+ jdata["data"]["token"].ToString();
  129. webview.gameObject.SetActive(true);
  130. webview.WebView?.LoadUrl(webview.InitialUrl);
  131. });
  132. break;
  133. }
  134. // UILogManager.Instance.text3.text = msg;
  135. // MQTTManager.Instance.Received(msg);
  136. // MQTTManager.Instance.Received(obj);
  137. }
  138. private Dictionary<string, string> requestHeader = new Dictionary<string, string>();
  139. public Dictionary<string, string> RequestHeader => requestHeader;
  140. public void Post(string methodName, string jsonString, Action<string> CallBack)
  141. {
  142. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  143. }
  144. public void initHead(string ContentType)
  145. {
  146. requestHeader.Clear();
  147. requestHeader.Add("authorization", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ4NzQxMTY1MDIsImlhdCI6MTcyMDUxNjUwMiwidXNlciI6eyJpZCI6MTA5MiwiZW1haWwiOiIiLCJwaG9uZSI6InprZGVtbyIsIm5hbWUiOiJ6a2RlbW8iLCJyb2xlSWQiOjF9fQ.OZ2HDUp9F5S504cmCUh7Ai15sq_nQ5i7ZG_odxhqGGM");
  148. requestHeader.Add("Content-Type", ContentType);
  149. }
  150. public IEnumerator SendHttp(string methodName, string jsonString, Action<string> CallBack, string ContentType = "application/json", bool isPost = true)
  151. {
  152. Debug.Log("Start Queue SendHttp " + methodName);
  153. string text = (!methodName.Contains("http")) ? ("https://api-fat2.ghz-tech.com" + methodName) : methodName;
  154. Debug.Log("URL:" + text + " isPost " + isPost);
  155. UnityWebRequest webRequest = isPost ? new UnityWebRequest(text, "POST") : UnityWebRequest.Get(text);
  156. initHead(ContentType);
  157. using (webRequest)
  158. {
  159. if (!isPost)
  160. {
  161. webRequest.downloadHandler = new DownloadHandlerBuffer();
  162. foreach (KeyValuePair<string, string> item in requestHeader)
  163. {
  164. webRequest.SetRequestHeader(item.Key, item.Value);
  165. }
  166. }
  167. else
  168. {
  169. byte[] bytes = Encoding.UTF8.GetBytes(jsonString);
  170. webRequest.uploadHandler = new UploadHandlerRaw(bytes);
  171. webRequest.downloadHandler = new DownloadHandlerBuffer();
  172. }
  173. foreach (KeyValuePair<string, string> item2 in requestHeader)
  174. {
  175. webRequest.SetRequestHeader(item2.Key, item2.Value);
  176. }
  177. yield return webRequest.SendWebRequest();
  178. Debug.Log("CallBack==>" + webRequest.downloadHandler.text);
  179. if (webRequest.isHttpError || webRequest.isNetworkError)
  180. {
  181. string text2 = webRequest.downloadHandler.text;
  182. CallBack(text2);
  183. }
  184. else
  185. {
  186. string text3 = webRequest.downloadHandler.text;
  187. CallBack(text3);
  188. }
  189. }
  190. }
  191. public void GetCurrentDateTime()
  192. {
  193. var now = DateTime.Now; // 获取当前本地时间
  194. string timer1Str = now.Year + ".";
  195. if (now.Month < 10)
  196. {
  197. timer1Str = timer1Str + "0" + now.Month + ".";
  198. }
  199. else
  200. {
  201. timer1Str = timer1Str + now.Month + ".";
  202. }
  203. if (now.Day < 10)
  204. {
  205. timer1Str = timer1Str + "0" + now.Day;
  206. }
  207. else
  208. {
  209. timer1Str = timer1Str + now.Day;
  210. }
  211. switch (now.DayOfWeek)
  212. {
  213. case DayOfWeek.Friday:
  214. timer1Str = timer1Str + " 星期五";
  215. break;
  216. case DayOfWeek.Monday:
  217. timer1Str = timer1Str + " 星期一";
  218. break;
  219. case DayOfWeek.Saturday:
  220. timer1Str = timer1Str + " 星期六";
  221. break;
  222. case DayOfWeek.Sunday:
  223. timer1Str = timer1Str + " 星期日";
  224. break;
  225. case DayOfWeek.Thursday:
  226. timer1Str = timer1Str + " 星期四";
  227. break;
  228. case DayOfWeek.Tuesday:
  229. timer1Str = timer1Str + " 星期二";
  230. break;
  231. case DayOfWeek.Wednesday:
  232. timer1Str = timer1Str + " 星期三";
  233. break;
  234. default:
  235. break;
  236. }
  237. timer1.text = timer1Str;
  238. string timer2Str="" ;
  239. if (now.Hour < 10)
  240. {
  241. timer2Str = timer2Str + "0" + now.Hour+":";
  242. }
  243. else
  244. {
  245. timer2Str = timer2Str + now.Hour + ":";
  246. }
  247. if (now.Minute < 10)
  248. {
  249. timer2Str = timer2Str + "0" + now.Minute + ":";
  250. }
  251. else
  252. {
  253. timer2Str = timer2Str + now.Minute + ":";
  254. }
  255. if (now.Second < 10)
  256. {
  257. timer2Str = timer2Str + "0" + now.Second;
  258. }
  259. else
  260. {
  261. timer2Str = timer2Str + now.Second;
  262. }
  263. timer2.text = timer2Str;
  264. }
  265. }