DeMaDataManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. byte[] bodyRaw = Encoding.UTF8.GetBytes(lstformData);
  69. request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  70. request.SetRequestHeader("Content-Type", "application/json");
  71. DownloadHandler downloadHandler = new DownloadHandlerBuffer();
  72. request.downloadHandler = downloadHandler;
  73. request.SetRequestHeader("AUTHORIZATION", "c9a83db6a916ba530da2df11624c0ae6");
  74. yield return request.SendWebRequest();
  75. if (string.IsNullOrEmpty(request.error))
  76. {//Post的请求成功 //Post请求的返回参数
  77. var data = request.downloadHandler.text;
  78. Debug.Log(m_url + "_" + lstformData + "——成功:" + data);
  79. callback.Invoke(data);
  80. }
  81. else
  82. {//Post的请求失败
  83. Debug.Log(m_url + "失败" + lstformData + "_" + request.error);
  84. callback.Invoke(null);
  85. }
  86. }
  87. public class ShiShiDianList
  88. {
  89. public string name;
  90. public float f;
  91. }
  92. public IEnumerator GetUseDianLiang(Action<List<ShiShiDianList>> callback)
  93. {
  94. yield return StartCoroutine(Get_Demo("/digital-twin/v1/electricity/realtime", "", (string msg) => {
  95. List<ShiShiDianList> lf =new List<ShiShiDianList>();
  96. try
  97. {
  98. JsonData data =JsonMapper.ToObject(msg);
  99. for (int i = 0;i<data["data"]["workshops"].Count;i++)
  100. {
  101. ShiShiDianList ssdl =new ShiShiDianList();
  102. ssdl.name = data["data"]["workshops"][i]["name"].ToString();
  103. for (int j = 0;j< data["data"]["electricity"].Count;j++)
  104. {
  105. if( data["data"]["workshops"][i]["id"].ToString()==data["data"]["electricity"][j]["workshopsID"].ToString())
  106. {
  107. ssdl.f +=float.Parse(data["data"]["electricity"][j]["value"].ToString()) ;
  108. // break;
  109. }
  110. }
  111. lf.Add(ssdl);
  112. }
  113. callback.Invoke(lf);
  114. }
  115. catch{
  116. lf =new List<ShiShiDianList>();
  117. for (int i = 0;i<10;i++)
  118. {
  119. ShiShiDianList ssdl =new ShiShiDianList();
  120. ssdl.name = "";
  121. ssdl.f = 0;
  122. lf.Add(ssdl);
  123. }
  124. callback.Invoke(lf);
  125. }
  126. }));
  127. }
  128. public class dvtype
  129. {
  130. public float pre;
  131. public int ct;
  132. }
  133. public IEnumerator GetShishiType(Action<List<dvtype>> callback)
  134. {
  135. yield return StartCoroutine(Post_Demo("/digital-twin/v1/subscribe/status", "", (string msg) => {
  136. List<dvtype> lf =new List<dvtype>();
  137. for (int i = 0;i<10;i++)
  138. {
  139. dvtype dt=new dvtype();
  140. dt.ct =1;
  141. dt.pre=100;
  142. lf.Add(dt);
  143. }
  144. callback.Invoke(lf);
  145. }));
  146. }
  147. public static string[] GetHoursArray()
  148. {
  149. return Enumerable.Range(1, 24)
  150. .Select(hour => hour.ToString("D2"))
  151. .Select(hour => $"{hour}:00")
  152. .ToArray();
  153. }
  154. public static string[] GetHoursArray2()
  155. {
  156. return Enumerable.Range(0, 24)
  157. .Select(hour => hour.ToString("D2"))
  158. .Select(hour => $"{hour}:00")
  159. .ToArray();
  160. }
  161. public void GetJinRiRanQi(Action<LineChartData> callback)
  162. {
  163. LineChartData data = new LineChartData();
  164. data.xdata = new List<string>();
  165. data.sdata = new List<LineChartSeriesData>();
  166. StartCoroutine(Get_Demo("/digital-twin/v1/gas/realtime", "", (string msg) => {
  167. JsonData d =JsonMapper.ToObject(msg);
  168. DateTime startTime = new DateTime(DateTime.Now.Date.Ticks);
  169. DateTime[] timeStamps = new DateTime[25];
  170. LineChartSeriesData lc = new LineChartSeriesData();
  171. lc.name = "kWh";
  172. lc.values = new List<float>();
  173. data.xdata.AddRange(GetHoursArray2());
  174. for (int i = 0; i < data.xdata.Count; i++)
  175. {
  176. timeStamps[i] = startTime.AddHours(i + 1);
  177. // 获取当前的秒级时间戳
  178. long timestamp = (long)(timeStamps[i].ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
  179. float ft =0;
  180. int ct = 0;
  181. long oldtime = i<=0?0:(long)(timeStamps[i-1].ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
  182. for (int j = 0;j<d["data"]["list"].Count;j++)
  183. {
  184. long vtime =long.Parse(d["data"]["list"][j]["date"].ToString());
  185. if(timestamp>=vtime&&oldtime<vtime)
  186. {
  187. ft += float.Parse(d["data"]["list"][j]["value"].ToString());
  188. ct++;
  189. }
  190. else
  191. {
  192. }
  193. }
  194. if(ct==0)
  195. {
  196. Debug.Log(oldtime+"_"+timestamp+"_"+data.xdata[i]);
  197. ct=1;
  198. }
  199. float value =ft/ct;
  200. lc.values.Add(value);
  201. }
  202. data.sdata.Add(lc);
  203. callback.Invoke(data);
  204. }));
  205. return;
  206. for (int i = 0; i < 20; i++)
  207. {
  208. data.xdata.Add("xdata_" + i);
  209. }
  210. LineChartSeriesData lc = new LineChartSeriesData();
  211. lc.name = "数据1";
  212. lc.values = new List<float>();
  213. for (int i = 0; i < 20; i++)
  214. {
  215. lc.values.Add(UnityEngine.Random.Range(1, 1000));
  216. }
  217. data.sdata.Add(lc);
  218. callback.Invoke(data);
  219. }
  220. public void GetLeiJiRanqi(Action<LineChartData> callback)
  221. {
  222. LineChartData data = new LineChartData();
  223. data.xdata = new List<string>();
  224. data.sdata = new List<LineChartSeriesData>();
  225. StartCoroutine(Get_Demo("/digital-twin/v1/gas/total/date","", (string msg) => {
  226. JsonData d =JsonMapper.ToObject(msg);
  227. LineChartSeriesData lc = new LineChartSeriesData();
  228. lc.name = "kWh";
  229. lc.values = new List<float>();
  230. for (int i = 0;i<d["data"]["list"].Count;i++)
  231. {
  232. data.xdata.Add(changedate(d["data"]["list"][i]["date"].ToString(),1));
  233. lc.values.Add(float.Parse(d["data"]["list"][i]["value"].ToString()));
  234. }
  235. data.sdata.Add(lc);
  236. callback.Invoke(data);
  237. }));
  238. return;
  239. for (int i = 0; i < 20; i++)
  240. {
  241. data.xdata.Add("xdata_" + i);
  242. }
  243. LineChartSeriesData lc = new LineChartSeriesData();
  244. lc.name = "数据1";
  245. lc.values = new List<float>();
  246. for (int i = 0; i < 20; i++)
  247. {
  248. lc.values.Add(UnityEngine.Random.Range(1, 1000));
  249. }
  250. data.sdata.Add(lc);
  251. callback.Invoke(data);
  252. }
  253. public void GetgongyeZaiXian(Action<LineChartData> callback,int idx)
  254. {
  255. LineChartData data = new LineChartData();
  256. data.xdata = new List<string>();
  257. data.sdata = new List<LineChartSeriesData>();
  258. for (int i = 0; i < 20; i++)
  259. {
  260. data.xdata.Add("xdata_" + i);
  261. }
  262. LineChartSeriesData lc = new LineChartSeriesData();
  263. lc.name = "数据1";
  264. lc.values = new List<float>();
  265. for (int i = 0; i < 20; i++)
  266. {
  267. lc.values.Add(UnityEngine.Random.Range(1, 1000));
  268. }
  269. data.sdata.Add(lc);
  270. callback.Invoke(data);
  271. }
  272. public void GetgongyeLiYong(Action<LineChartData> callback,int idx)
  273. {
  274. LineChartData data = new LineChartData();
  275. data.xdata = new List<string>();
  276. data.sdata = new List<LineChartSeriesData>();
  277. for (int i = 0; i < 20; i++)
  278. {
  279. data.xdata.Add("xdata_" + i);
  280. }
  281. LineChartSeriesData lc = new LineChartSeriesData();
  282. lc.name = "数据1";
  283. lc.values = new List<float>();
  284. for (int i = 0; i < 20; i++)
  285. {
  286. lc.values.Add(UnityEngine.Random.Range(1, 1000));
  287. }
  288. data.sdata.Add(lc);
  289. callback.Invoke(data);
  290. }
  291. public void GetKeJiZaiXian(Action<LineChartData> callback,int idx)
  292. {
  293. LineChartData data = new LineChartData();
  294. data.xdata = new List<string>();
  295. data.sdata = new List<LineChartSeriesData>();
  296. for (int i = 0; i < 20; i++)
  297. {
  298. data.xdata.Add("xdata_" + i);
  299. }
  300. LineChartSeriesData lc = new LineChartSeriesData();
  301. lc.name = "数据1";
  302. lc.values = new List<float>();
  303. for (int i = 0; i < 20; i++)
  304. {
  305. lc.values.Add(UnityEngine.Random.Range(1, 1000));
  306. }
  307. data.sdata.Add(lc);
  308. callback.Invoke(data);
  309. }
  310. public void GetGaoJingData(Action<LineChartData> callback,int idx)
  311. {
  312. LineChartData data = new LineChartData();
  313. data.xdata = new List<string>();
  314. data.sdata = new List<LineChartSeriesData>();
  315. for (int i = 0; i < 20; i++)
  316. {
  317. data.xdata.Add("xdata_" + i);
  318. }
  319. LineChartSeriesData lc = new LineChartSeriesData();
  320. lc.name = "数据1";
  321. lc.values = new List<float>();
  322. for (int i = 0; i < 20; i++)
  323. {
  324. lc.values.Add(UnityEngine.Random.Range(1, 1000));
  325. }
  326. data.sdata.Add(lc);
  327. callback.Invoke(data);
  328. }
  329. public void GetZongyongdianData(Action<LineChartData> callback,int idx)
  330. {
  331. JsonData datas =new JsonData();
  332. switch(idx)
  333. {
  334. case 1:
  335. datas["mode"] = "day";
  336. break;
  337. case 2:
  338. datas["mode"] = "week";
  339. break;
  340. case 3:
  341. datas["mode"] = "month";
  342. break;
  343. }
  344. LineChartData data = new LineChartData();
  345. data.xdata = new List<string>();
  346. data.sdata = new List<LineChartSeriesData>();
  347. StartCoroutine(Get_Demo("/digital-twin/v1/electricity/total/date", datas.ToJson(), (string msg) => {
  348. JsonData d =JsonMapper.ToObject(msg);
  349. LineChartSeriesData lc = new LineChartSeriesData();
  350. lc.name = "kWh";
  351. lc.values = new List<float>();
  352. for (int i = 0;i<d["data"]["list"].Count;i++)
  353. {
  354. data.xdata.Add(changedate(d["data"]["list"][i]["date"].ToString(),idx));
  355. lc.values.Add(float.Parse(d["data"]["list"][i]["value"].ToString()));
  356. }
  357. data.sdata.Add(lc);
  358. callback.Invoke(data);
  359. }));
  360. return;
  361. for (int i = 0; i < 20; i++)
  362. {
  363. data.xdata.Add("xdata_" + i);
  364. }
  365. LineChartSeriesData lc = new LineChartSeriesData();
  366. lc.name = "数据1";
  367. lc.values = new List<float>();
  368. for (int i = 0; i < 20; i++)
  369. {
  370. lc.values.Add(UnityEngine.Random.Range(1, 1000));
  371. }
  372. data.sdata.Add(lc);
  373. callback.Invoke(data);
  374. }
  375. public static string TimeStampToHMS(float timeStampInSeconds)
  376. {
  377. TimeSpan time = TimeSpan.FromSeconds(timeStampInSeconds);
  378. return time.ToString("hh\\:mm");
  379. }
  380. public string changedate(string time,int i)
  381. {
  382. DateTime dateTime = DateTimeOffset.FromUnixTimeSeconds(long.Parse(time)).DateTime;
  383. switch(i)
  384. {
  385. case 0:
  386. return TimeStampToHMS((long.Parse(time)));
  387. break;
  388. case 1:
  389. return dateTime.ToString("MM/dd");
  390. break;
  391. case 2:
  392. DateTime dateTime2 = DateTimeOffset.FromUnixTimeSeconds(long.Parse(time)+604800).DateTime;
  393. return dateTime.ToString("MM/dd")+"\n"+dateTime2.ToString("MM/dd");
  394. break;
  395. case 3:
  396. return dateTime.ToString("MM月");
  397. break;
  398. }
  399. return time;
  400. }
  401. public class baojing
  402. {
  403. public string name;
  404. public string time;
  405. public string dev;
  406. }
  407. public void GetGaoJingTaiZhang(Action<List<baojing>> callback)
  408. {
  409. List<baojing> bj = new List<baojing>();
  410. for (var i=0;i<100;i++)
  411. {
  412. baojing b = new baojing();
  413. b.name = i.ToString();
  414. b.time = DateTime.Now.ToString();
  415. b.dev = i.ToString();
  416. bj.Add(b);
  417. }
  418. callback.Invoke(bj);
  419. }
  420. }