DeMaDataManager.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using LitJson;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. using static Manager;
  9. using System.Security.Cryptography;
  10. using Random = System.Random;
  11. using XCharts.Runtime;
  12. using System.Linq;
  13. public class DeMaDataManager : MonoBehaviour
  14. {
  15. public static DeMaDataManager Instance;
  16. private void Awake()
  17. {
  18. Instance = this;
  19. // StartCoroutine(wx_demo("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx9a280bbd81c64f56&secret=aeb6e5818800f8196fcac007e992dae0"));
  20. }
  21. public static string ip = "";
  22. public IEnumerator Post_Demo(string url,string lstformData,Action<string> callback)
  23. {
  24. Debug.Log("开始请求" + url + "_" + lstformData);
  25. while (ip=="")
  26. {
  27. Debug.Log("ip未设置" + ip + "_" + lstformData);
  28. yield return null;
  29. }
  30. string m_url = ip + url;
  31. Debug.Log("请求地址" + m_url + "_" + lstformData);
  32. var request = new UnityWebRequest(m_url, "POST");
  33. byte[] bodyRaw = Encoding.UTF8.GetBytes(lstformData);
  34. request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  35. request.SetRequestHeader("Content-Type", "application/json");
  36. DownloadHandler downloadHandler = new DownloadHandlerBuffer();
  37. request.downloadHandler = downloadHandler;
  38. request.SetRequestHeader("AUTHORIZATION", "c9a83db6a916ba530da2df11624c0ae6");
  39. yield return request.SendWebRequest();
  40. if (string.IsNullOrEmpty(request.error))
  41. {//Post的请求成功 //Post请求的返回参数
  42. var data = request.downloadHandler.text;
  43. Debug.Log(m_url+"_"+ lstformData + "——成功:" + data);
  44. callback.Invoke(data);
  45. }
  46. else
  47. {//Post的请求失败
  48. Debug.Log(m_url + "失败"+ lstformData+"_"+ request.error);
  49. callback.Invoke(null);
  50. }
  51. }
  52. public IEnumerator Get_Demo(string url, string lstformData, Action<string> callback)
  53. {
  54. Debug.Log("开始请求" + url + "_" + lstformData);
  55. while (ip == "")
  56. {
  57. Debug.Log("ip未设置" + ip + "_" + lstformData);
  58. yield return null;
  59. }
  60. JsonData d = JsonMapper.ToObject(lstformData);
  61. string m_url = ip + url+"?";
  62. foreach (var item in d.Keys)
  63. {
  64. m_url+=item+"="+d[item].ToString()+"&";
  65. }
  66. Debug.Log("请求地址" + m_url + "_" + lstformData);
  67. var request = new UnityWebRequest(m_url, "Get");
  68. DownloadHandler downloadHandler = new DownloadHandlerBuffer();
  69. request.downloadHandler = downloadHandler;
  70. request.SetRequestHeader("AUTHORIZATION", "c9a83db6a916ba530da2df11624c0ae6");
  71. yield return request.SendWebRequest();
  72. if (string.IsNullOrEmpty(request.error))
  73. {//Post的请求成功 //Post请求的返回参数
  74. var data = request.downloadHandler.text;
  75. Debug.Log(m_url + "_" + lstformData + "——成功:" + data);
  76. callback.Invoke(data);
  77. }
  78. else
  79. {//Post的请求失败
  80. Debug.Log(m_url + "失败" + lstformData + "_" + request.error);
  81. // callback.Invoke(request.error);
  82. }
  83. }
  84. public class ShiShiDianList
  85. {
  86. public string name;
  87. public float f;
  88. }
  89. public IEnumerator GetUseDianLiang(Action<List<ShiShiDianList>> callback)
  90. {
  91. yield return StartCoroutine(Get_Demo("/digital-twin/v1/electricity/realtime", "", (string msg) => {
  92. List<ShiShiDianList> lf =new List<ShiShiDianList>();
  93. try
  94. {
  95. JsonData data =JsonMapper.ToObject(msg);
  96. for (int i = 0;i<data["data"]["workshops"].Count;i++)
  97. {
  98. ShiShiDianList ssdl =new ShiShiDianList();
  99. ssdl.name = data["data"]["workshops"][i]["name"].ToString();
  100. for (int j = 0;j< data["data"]["electricity"].Count;j++)
  101. {
  102. if( data["data"]["workshops"][i]["id"].ToString()==data["data"]["electricity"][j]["workshopsID"].ToString())
  103. {
  104. ssdl.f +=float.Parse(data["data"]["electricity"][j]["value"].ToString()) ;
  105. // break;
  106. }
  107. }
  108. lf.Add(ssdl);
  109. }
  110. callback.Invoke(lf);
  111. }
  112. catch{
  113. lf =new List<ShiShiDianList>();
  114. for (int i = 0;i<10;i++)
  115. {
  116. ShiShiDianList ssdl =new ShiShiDianList();
  117. ssdl.name = "";
  118. ssdl.f = 0;
  119. lf.Add(ssdl);
  120. }
  121. callback.Invoke(lf);
  122. }
  123. }));
  124. }
  125. public class dvtype
  126. {
  127. public float pre;
  128. public int ct;
  129. }
  130. public IEnumerator GetShishiType(Action<List<dvtype>> callback)
  131. {
  132. yield return StartCoroutine(Post_Demo("/digital-twin/v1/subscribe/status", "", (string msg) => {
  133. List<dvtype> lf =new List<dvtype>();
  134. for (int i = 0;i<10;i++)
  135. {
  136. dvtype dt=new dvtype();
  137. dt.ct =1;
  138. dt.pre=100;
  139. lf.Add(dt);
  140. }
  141. callback.Invoke(lf);
  142. }));
  143. }
  144. public static string[] GetHoursArray()
  145. {
  146. return Enumerable.Range(1, 24)
  147. .Select(hour => hour.ToString("D2"))
  148. .Select(hour => $"{hour}:00")
  149. .ToArray();
  150. }
  151. public static string[] GetHoursArray2()
  152. {
  153. return Enumerable.Range(0, 25)
  154. .Select(hour => hour.ToString("D2"))
  155. .Select(hour => $"{hour}:00")
  156. .ToArray();
  157. }
  158. public void GetDeviceType(string id,Action<JsonData> callback)
  159. {
  160. JsonData jsonData =new JsonData();
  161. jsonData["deviceCode"]=id;
  162. StartCoroutine(Post_Demo("/digital-twin/v1/device/runtime", jsonData.ToJson(), (string msg) => {
  163. JsonData d =JsonMapper.ToObject(msg);
  164. callback.Invoke(d);
  165. }));
  166. }
  167. public void GetJinRiRanQi(Action<LineChartData> callback)
  168. {
  169. LineChartData data = new LineChartData();
  170. data.xdata = new List<string>();
  171. data.sdata = new List<LineChartSeriesData>();
  172. StartCoroutine(Get_Demo("/digital-twin/v1/gas/realtime", "", (string msg) => {
  173. try
  174. {
  175. JsonData d =JsonMapper.ToObject(msg);
  176. DateTime startTime = new DateTime(DateTime.Now.Date.Ticks);
  177. DateTime[] timeStamps = new DateTime[25];
  178. LineChartSeriesData lc = new LineChartSeriesData();
  179. lc.name = "kWh";
  180. lc.values = new List<float>();
  181. data.xdata.AddRange(GetHoursArray2());
  182. for (int i = 0; i < data.xdata.Count; i++)
  183. {
  184. timeStamps[i] = startTime.AddHours(i + 1);
  185. // 获取当前的秒级时间戳
  186. long timestamp = (long)(timeStamps[i].ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
  187. float ft =0;
  188. int ct = 0;
  189. long oldtime = i<=0?0:(long)(timeStamps[i-1].ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
  190. for (int j = 0;j<d["data"]["list"].Count;j++)
  191. {
  192. long vtime =long.Parse(d["data"]["list"][j]["date"].ToString());
  193. if(timestamp>vtime&&oldtime<=vtime)
  194. {
  195. ft += float.Parse(d["data"]["list"][j]["value"].ToString());
  196. ct++;
  197. }
  198. else
  199. {
  200. }
  201. }
  202. if(ct==0)
  203. {
  204. Debug.Log(oldtime+"_"+timestamp+"_"+data.xdata[i]);
  205. ct=1;
  206. }
  207. float value =ft/ct;
  208. lc.values.Add(value);
  209. }
  210. data.sdata.Add(lc);
  211. callback.Invoke(data);
  212. }catch
  213. {
  214. Debug.Log("error==>"+msg);
  215. }
  216. }));
  217. return;
  218. for (int i = 0; i < 20; i++)
  219. {
  220. data.xdata.Add("xdata_" + i);
  221. }
  222. LineChartSeriesData lc = new LineChartSeriesData();
  223. lc.name = "数据1";
  224. lc.values = new List<float>();
  225. for (int i = 0; i < 20; i++)
  226. {
  227. lc.values.Add(UnityEngine.Random.Range(1, 1000));
  228. }
  229. data.sdata.Add(lc);
  230. callback.Invoke(data);
  231. }
  232. public void GetLeiJiRanqi(Action<LineChartData> callback)
  233. {
  234. LineChartData data = new LineChartData();
  235. data.xdata = new List<string>();
  236. data.sdata = new List<LineChartSeriesData>();
  237. StartCoroutine(Get_Demo("/digital-twin/v1/gas/total/date","", (string msg) => {
  238. JsonData d =JsonMapper.ToObject(msg);
  239. LineChartSeriesData lc = new LineChartSeriesData();
  240. lc.name = "kWh";
  241. lc.values = new List<float>();
  242. for (int i = 0;i<d["data"]["list"].Count;i++)
  243. {
  244. data.xdata.Add(changedate(d["data"]["list"][i]["date"].ToString(),1));
  245. lc.values.Add(float.Parse(d["data"]["list"][i]["value"].ToString()));
  246. }
  247. data.sdata.Add(lc);
  248. callback.Invoke(data);
  249. }));
  250. return;
  251. for (int i = 0; i < 20; i++)
  252. {
  253. data.xdata.Add("xdata_" + i);
  254. }
  255. LineChartSeriesData lc = new LineChartSeriesData();
  256. lc.name = "数据1";
  257. lc.values = new List<float>();
  258. for (int i = 0; i < 20; i++)
  259. {
  260. lc.values.Add(UnityEngine.Random.Range(1, 1000));
  261. }
  262. data.sdata.Add(lc);
  263. callback.Invoke(data);
  264. }
  265. public void GetDeviceInfo(string id,Action<JsonData> callback)
  266. {
  267. JsonData dataj=new JsonData();
  268. dataj["buID"] =int.Parse(id) ;
  269. LineChartData data = new LineChartData();
  270. data.xdata = new List<string>();
  271. data.sdata = new List<LineChartSeriesData>();
  272. StartCoroutine(Post_Demo("/digital-twin/v1/device/realtime",dataj.ToJson(), (string msg) => {
  273. JsonData d =JsonMapper.ToObject(msg);
  274. callback.Invoke(d);
  275. }));
  276. return;
  277. }
  278. public void GetgongyeZaiXian(Action<LineChartData> callback,int idx)
  279. {
  280. LineChartData data = new LineChartData();
  281. data.xdata = new List<string>();
  282. data.sdata = new List<LineChartSeriesData>();
  283. for (int i = 0; i < 20; i++)
  284. {
  285. data.xdata.Add("xdata_" + i);
  286. }
  287. LineChartSeriesData lc = new LineChartSeriesData();
  288. lc.name = "数据1";
  289. lc.values = new List<float>();
  290. for (int i = 0; i < 20; i++)
  291. {
  292. lc.values.Add(UnityEngine.Random.Range(1, 1000));
  293. }
  294. data.sdata.Add(lc);
  295. callback.Invoke(data);
  296. }
  297. public void GetgongyeLiYong(Action<LineChartData> callback,int idx)
  298. {
  299. LineChartData data = new LineChartData();
  300. data.xdata = new List<string>();
  301. data.sdata = new List<LineChartSeriesData>();
  302. for (int i = 0; i < 20; i++)
  303. {
  304. data.xdata.Add("xdata_" + i);
  305. }
  306. LineChartSeriesData lc = new LineChartSeriesData();
  307. lc.name = "数据1";
  308. lc.values = new List<float>();
  309. for (int i = 0; i < 20; i++)
  310. {
  311. lc.values.Add(UnityEngine.Random.Range(1, 1000));
  312. }
  313. data.sdata.Add(lc);
  314. callback.Invoke(data);
  315. }
  316. public void GetKeJiZaiXian(string url,Action<LineChartData> callback,int idx,int type)
  317. {
  318. JsonData datas =new JsonData();
  319. switch(idx)
  320. {
  321. case 1:
  322. datas["mode"] = "day";
  323. break;
  324. case 2:
  325. datas["mode"] = "week";
  326. break;
  327. case 3:
  328. datas["mode"] = "month";
  329. break;
  330. }
  331. datas["buID"] = type;
  332. LineChartData data = new LineChartData();
  333. data.xdata = new List<string>();
  334. data.sdata = new List<LineChartSeriesData>();
  335. StartCoroutine(Post_Demo(url, datas.ToJson(), (string msg) => {
  336. try
  337. {
  338. JsonData d =JsonMapper.ToObject(msg);
  339. LineChartSeriesData lc = new LineChartSeriesData();
  340. lc.name = "kWh";
  341. lc.values = new List<float>();
  342. for (int i = 0;i<d["data"]["list"].Count;i++)
  343. {
  344. data.xdata.Add(changedate(d["data"]["list"][i]["date"].ToString(),idx));
  345. lc.values.Add(float.Parse(d["data"]["list"][i]["value"].ToString()));
  346. }
  347. data.sdata.Add(lc);
  348. callback.Invoke(data);
  349. }
  350. catch
  351. {
  352. Debug.Log("msg error===>"+msg);
  353. }
  354. }));
  355. return;
  356. }
  357. public void GetGaoJingData(Action<LineChartData> callback,int idx,int type)
  358. {
  359. JsonData datas =new JsonData();
  360. switch(idx)
  361. {
  362. case 1:
  363. datas["mode"] = "day";
  364. break;
  365. case 2:
  366. datas["mode"] = "week";
  367. break;
  368. case 3:
  369. datas["mode"] = "month";
  370. break;
  371. }
  372. datas["buID"] = type;
  373. LineChartData data = new LineChartData();
  374. data.xdata = new List<string>();
  375. data.sdata = new List<LineChartSeriesData>();
  376. StartCoroutine(Post_Demo("/digital-twin/v1/device/ratio/alarm", datas.ToJson(), (string msg) => {
  377. try
  378. {
  379. JsonData d =JsonMapper.ToObject(msg);
  380. LineChartSeriesData lc = new LineChartSeriesData();
  381. lc.name = "kWh";
  382. lc.values = new List<float>();
  383. for (int i = 0;i<d["data"]["list"].Count;i++)
  384. {
  385. data.xdata.Add(changedate(d["data"]["list"][i]["date"].ToString(),idx));
  386. lc.values.Add(float.Parse(d["data"]["list"][i]["value"].ToString()));
  387. Debug.Log(" data.xdata=1=>"+ d["data"]["list"][i]["date"].ToString());
  388. Debug.Log(" data.xdata=2=>"+ data.xdata[ data.xdata.Count-1]);
  389. }
  390. data.sdata.Add(lc);
  391. callback.Invoke(data);
  392. }
  393. catch
  394. {
  395. Debug.Log("msg error===>"+msg);
  396. }
  397. }));
  398. return;
  399. }
  400. public void GetZongyongdianData(Action<LineChartData> callback,int idx)
  401. {
  402. JsonData datas =new JsonData();
  403. switch(idx)
  404. {
  405. case 1:
  406. datas["mode"] = "day";
  407. break;
  408. case 2:
  409. datas["mode"] = "week";
  410. break;
  411. case 3:
  412. datas["mode"] = "month";
  413. break;
  414. }
  415. LineChartData data = new LineChartData();
  416. data.xdata = new List<string>();
  417. data.sdata = new List<LineChartSeriesData>();
  418. StartCoroutine(Post_Demo("/digital-twin/v1/electricity/total/date", datas.ToJson(), (string msg) => {
  419. try
  420. {
  421. JsonData d =JsonMapper.ToObject(msg);
  422. LineChartSeriesData lc = new LineChartSeriesData();
  423. lc.name = "kWh";
  424. lc.values = new List<float>();
  425. for (int i = 0;i<d["data"]["list"].Count;i++)
  426. {
  427. data.xdata.Add(changedate(d["data"]["list"][i]["date"].ToString(),idx));
  428. lc.values.Add(float.Parse(d["data"]["list"][i]["value"].ToString()));
  429. }
  430. data.sdata.Add(lc);
  431. callback.Invoke(data);
  432. }
  433. catch
  434. {
  435. Debug.Log("msg error===>"+msg);
  436. }
  437. }));
  438. return;
  439. for (int i = 0; i < 20; i++)
  440. {
  441. data.xdata.Add("xdata_" + i);
  442. }
  443. LineChartSeriesData lc = new LineChartSeriesData();
  444. lc.name = "数据1";
  445. lc.values = new List<float>();
  446. for (int i = 0; i < 20; i++)
  447. {
  448. lc.values.Add(UnityEngine.Random.Range(1, 1000));
  449. }
  450. data.sdata.Add(lc);
  451. callback.Invoke(data);
  452. }
  453. public static string TimeStampToHMS(float timeStampInSeconds)
  454. {
  455. TimeSpan time = TimeSpan.FromSeconds(timeStampInSeconds);
  456. return time.ToString("hh\\:mm");
  457. }
  458. public static DateTime UnixTimeStampToDateTime(double timestamp)
  459. {
  460. // Unix时间戳基准时间1970年1月1日对应的DateTime
  461. DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  462. // 将时间戳转换为秒
  463. long unixTimeStampInTicks = (long)(timestamp * TimeSpan.TicksPerSecond);
  464. // 在UTC时间基础上增加转换后的秒数得到中国区的时间
  465. DateTime dateTimeCn = new DateTime(unixStartTime.Ticks + unixTimeStampInTicks, DateTimeKind.Utc).ToLocalTime();
  466. return dateTimeCn;
  467. }
  468. public static string changedate(string time,int i)
  469. {
  470. DateTime dateTime = UnixTimeStampToDateTime(long.Parse(time));
  471. switch(i)
  472. {
  473. case 0:
  474. return TimeStampToHMS((long.Parse(time)));
  475. break;
  476. case 1:
  477. return dateTime.ToString("MM/dd");
  478. break;
  479. case 2:
  480. DateTime dateTime2 =UnixTimeStampToDateTime(long.Parse(time)+604800);
  481. return dateTime.ToString("MM/dd")+"\n"+dateTime2.ToString("MM/dd");
  482. break;
  483. case 3:
  484. return dateTime.ToString("MM月");
  485. break;
  486. case 4:
  487. return dateTime.ToString("MM-dd");
  488. break;
  489. }
  490. return time;
  491. }
  492. public class baojing
  493. {
  494. public string name;
  495. public string time;
  496. public string dev;
  497. }
  498. public void GetGaoJingTaiZhang(Action<List<baojing>> callback)
  499. {
  500. List<baojing> bj = new List<baojing>();
  501. StartCoroutine(getAlarmledger(1,bj,callback));
  502. }
  503. IEnumerator getAlarmledger(int page, List<baojing> bj,Action<List<baojing>> callback)
  504. {
  505. JsonData datas =new JsonData();
  506. datas["page"] = page;
  507. datas["pageSize"] = 10;
  508. JsonData d =new JsonData();
  509. yield return StartCoroutine(Post_Demo("/digital-twin/v1/device/alarmLedger",datas.ToJson(), (string msg) => {
  510. d =JsonMapper.ToObject(msg);
  511. for (var i=0;i<d["data"]["list"].Count;i++)
  512. {
  513. for (var j=0;j< d["data"]["list"][i]["alarmLocation"].Count;j++)
  514. {
  515. baojing b = new baojing();
  516. b.name = d["data"]["list"][i]["deviceName"].ToString();
  517. b.time = changedate( d["data"]["list"][i]["date"].ToString(),4);
  518. b.dev = d["data"]["list"][i]["alarmLocation"][j].ToString();
  519. bj.Add(b);
  520. }
  521. }
  522. }));
  523. while(d==null)
  524. {
  525. yield return null;
  526. }
  527. if((int.Parse(d["data"]["total"].ToString())/10+1)>page)
  528. {
  529. page++;
  530. yield return StartCoroutine(getAlarmledger(page,bj,callback));
  531. }
  532. else
  533. {
  534. callback.Invoke(bj);
  535. }
  536. }
  537. }