HttpHelper.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. using UnityAsync;
  7. using Best.HTTP;
  8. using Best.HTTP.JSON;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. using UnityEngine;
  12. using UnityEngine.Networking;
  13. using Unity.VisualScripting;
  14. using static System.Net.WebRequestMethods;
  15. public class HttpHelper : MonoBehaviour
  16. {
  17. public Action OnSWYJRefresh;
  18. public static HttpHelper _Instance;
  19. public static Dictionary<string, DevicesId_Data> _devicesIdDatas_BuYuan =new Dictionary<string, DevicesId_Data>();
  20. private async void Awake()
  21. {
  22. _Instance = this;
  23. InItBuYuanDevicesList();
  24. SendBuYuan_GetSensorData();
  25. GetBuYuanDevicesGroupInfo();
  26. if (GlobalData.pageIndex == PageIndex.Page1 || GlobalData.pageIndex == PageIndex.Page2 || GlobalData.pageIndex == PageIndex.Page3)
  27. {
  28. //可以并行,无需等待
  29. InitYZTData();
  30. }
  31. if (GlobalData.pageIndex == PageIndex.Page1)
  32. {
  33. //可以并行,无需等待
  34. InitXHGKData();
  35. }
  36. if (GlobalData.pageIndex == PageIndex.Page1)
  37. {
  38. //可以并行,无需等待
  39. InitGCJKData();
  40. }
  41. if (GlobalData.pageIndex == PageIndex.Page1 || GlobalData.pageIndex == PageIndex.Page2)
  42. {
  43. //可以并行,无需等待
  44. InitSWYJData();
  45. }
  46. if (GlobalData.pageIndex == PageIndex.Page1 || GlobalData.pageIndex == PageIndex.Page2)
  47. {
  48. //可以并行,无需等待
  49. InitGCYWData();
  50. }
  51. }
  52. public async Task InitYZTData() {
  53. await InitYZTData1();
  54. InitYZTData2();
  55. InitYZTData3();
  56. }
  57. public async Task InitYZTData1()
  58. {
  59. bool successInternet = true;
  60. UnityWebRequest requestData = UnityWebRequest.Get(ServerAddress.APIGetWaterManagerProject);
  61. await requestData.SendWebRequest();
  62. try
  63. {
  64. if (requestData.result != UnityWebRequest.Result.Success)
  65. {
  66. Debug.LogWarning("YZTLayer联网不成功,读本地缓存数据,原因:返request不成功");
  67. successInternet = false;
  68. }
  69. else
  70. {
  71. JObject jsonObject = JObject.Parse(requestData.downloadHandler.text);
  72. // 提取data字段的值
  73. JToken dataToken = jsonObject["data"];
  74. JToken codeToken = jsonObject["code"];
  75. if (codeToken.ToString() == "200")
  76. {
  77. GlobalData.layerUnitDatas = JsonConvert.DeserializeObject<List<LayerUnitData>>(dataToken.ToString());
  78. }
  79. else
  80. {
  81. Debug.LogWarning("YZTLayer联网不成功,读本地缓存数据,原因:返序列化失败");
  82. successInternet = false;
  83. }
  84. }
  85. }
  86. catch (Exception e)
  87. {
  88. successInternet = false;
  89. Debug.LogWarning("YZTLayer联网不成功,读本地缓存数据,原因:" + e.ToString());
  90. }
  91. requestData.Dispose();
  92. if (!successInternet)
  93. {
  94. WWW www = new WWW(Application.streamingAssetsPath + "/layerUnitDatas.json");
  95. await new UnityAsync.WaitUntil(() =>
  96. {
  97. return www.isDone;
  98. });
  99. GlobalData.layerUnitDatas = JsonConvert.DeserializeObject<List<LayerUnitData>>(www.text);
  100. www.Dispose();
  101. }
  102. for (int i = 0; i < GlobalData.layerUnitDatas.Count; i++) {
  103. if (GlobalData.layerUnitDatas[i].type == LayerUnitType.JK) {
  104. GlobalData.obsCameraLib.Add(GlobalData.layerUnitDatas[i].name, GlobalData.layerUnitDatas[i].name_pri);
  105. }
  106. }
  107. }
  108. public async Task InitYZTData2()
  109. {
  110. bool successInternet = true;
  111. string sendUrl = ServerAddress.API_GetDevicesInfo;
  112. sendUrl += "/34020000001180000001/channels?page=1&count=40";
  113. Debug.Log(sendUrl);
  114. UnityWebRequest requestData = UnityWebRequest.Get(sendUrl);
  115. await requestData.SendWebRequest();
  116. try
  117. {
  118. if (requestData.result != UnityWebRequest.Result.Success)
  119. {
  120. Debug.LogWarning("监控数据联网不成功,原因:返request不成功:" + requestData.downloadHandler.text.ToString());
  121. successInternet = false;
  122. }
  123. else
  124. {
  125. JObject jsonObject = JObject.Parse(requestData.downloadHandler.text);
  126. // 提取data字段的值
  127. JToken dataToken = jsonObject["data"];
  128. JToken codeToken = jsonObject["code"];
  129. if (codeToken.ToString() == "0")
  130. {
  131. GlobalData.obsDatas = new List<ObsData>();
  132. ServerObsDatas serverObsDatas = JsonConvert.DeserializeObject<ServerObsDatas>(dataToken.ToString());
  133. for (int i = 0; i < serverObsDatas.list.Count; i++)
  134. {
  135. ObsData obsData = new ObsData();
  136. obsData.name = serverObsDatas.list[i].name;
  137. obsData.targetName = GlobalData.obsCameraLib[obsData.name];
  138. // if (i > 0)
  139. // {
  140. // obsData.url =
  141. // "http://www.chatgpt918.top:9080/rtp/34020000001180000001_34020000001310000001/hls.m3u8";
  142. // }
  143. // else
  144. // {
  145. obsData.url = ServerAddress.ObsSeverM3U8Address + string.Format("/rtp/{0}_{1}/hls.m3u8", serverObsDatas.list[i].deviceId, serverObsDatas.list[i].channelId);
  146. //}
  147. obsData.type = obsType.BuYuanObs;
  148. obsData.deviceId = serverObsDatas.list[i].deviceId;
  149. obsData.channelId = serverObsDatas.list[i].channelId;
  150. obsData.status = serverObsDatas.list[i].status;
  151. GlobalData.obsDatas.Add(obsData);
  152. }
  153. }
  154. else
  155. {
  156. Debug.LogWarning("监控数据联网不成功,原因:返序列化失败");
  157. successInternet = false;
  158. }
  159. }
  160. }
  161. catch (Exception e)
  162. {
  163. successInternet = false;
  164. Debug.LogWarning("监控数据联网不成功,原因:" + e.ToString());
  165. }
  166. requestData.Dispose();
  167. }
  168. public async Task InitYZTData3()
  169. {
  170. bool successInternet = true;
  171. UnityWebRequest requestData = UnityWebRequest.Get(ServerAddress.APIGetWaterHotPoint);
  172. await requestData.SendWebRequest();
  173. try
  174. {
  175. if (requestData.result != UnityWebRequest.Result.Success)
  176. {
  177. Debug.LogWarning("YZTLayer联网不成功,读本地缓存数据,原因:返request不成功");
  178. successInternet = false;
  179. }
  180. else
  181. {
  182. JObject jsonObject = JObject.Parse(requestData.downloadHandler.text);
  183. // 提取data字段的值
  184. JToken dataToken = jsonObject["data"];
  185. JToken codeToken = jsonObject["code"];
  186. if (codeToken.ToString() == "200")
  187. {
  188. GlobalData.hotPointDatas = JsonConvert.DeserializeObject<List<HotPointData>>(dataToken.ToString());
  189. }
  190. else
  191. {
  192. Debug.LogWarning("YZTLayer联网不成功,读本地缓存数据,原因:返序列化失败");
  193. successInternet = false;
  194. }
  195. }
  196. }
  197. catch (Exception e)
  198. {
  199. successInternet = false;
  200. Debug.LogWarning("YZTLayer联网不成功,读本地缓存数据,原因:" + e.ToString());
  201. }
  202. requestData.Dispose();
  203. if (!successInternet)
  204. {
  205. WWW www = new WWW(Application.streamingAssetsPath + "/hotPoints.json.json");
  206. await new UnityAsync.WaitUntil(() =>
  207. {
  208. return www.isDone;
  209. });
  210. GlobalData.hotPointDatas = JsonConvert.DeserializeObject<List<HotPointData>>(www.text);
  211. www.Dispose();
  212. }
  213. }
  214. public async Task InitXHGKData()
  215. {
  216. bool successInternet = true;
  217. UnityWebRequest requestData = UnityWebRequest.Get(ServerAddress.APIGetGetProjectMilestones);
  218. await requestData.SendWebRequest();
  219. try
  220. {
  221. if (requestData.result != UnityWebRequest.Result.Success)
  222. {
  223. Debug.LogWarning("XHGKLayer联网不成功,读本地缓存数据,原因:返request不成功");
  224. successInternet = false;
  225. }
  226. else
  227. {
  228. JObject jsonObject = JObject.Parse(requestData.downloadHandler.text);
  229. // ȡdata ֶε ֵ
  230. JToken dataToken = jsonObject["data"];
  231. //Debug.Log(dataToken.ToString());
  232. JToken codeToken = jsonObject["code"];
  233. if (codeToken.ToString() == "200")
  234. {
  235. GlobalData.dsjContents = JsonConvert.DeserializeObject<List<DSJData>>(dataToken.ToString());
  236. }
  237. else
  238. {
  239. Debug.LogWarning("XHGKLayer联网不成功,读本地缓存数据,原因:返序列化失败");
  240. successInternet = false;
  241. }
  242. }
  243. }
  244. catch (Exception e)
  245. {
  246. successInternet = false;
  247. Debug.LogWarning("XHGKLayer联网不成功,读本地缓存数据,原因:" + e.ToString());
  248. }
  249. requestData.Dispose();
  250. if (!successInternet)
  251. {
  252. WWW www = new WWW(Application.streamingAssetsPath + "/dsj.json");
  253. await new UnityAsync.WaitUntil(() =>
  254. {
  255. return www.isDone;
  256. });
  257. GlobalData.dsjContents = JsonConvert.DeserializeObject<List<DSJData>>(www.text);
  258. www.Dispose();
  259. }
  260. }
  261. public async Task InitGCJKData() {
  262. bool successInternet = true;
  263. UnityWebRequest requestData = UnityWebRequest.Get(ServerAddress.APIGetGeQXZData);
  264. await requestData.SendWebRequest();
  265. try
  266. {
  267. if (requestData.result != UnityWebRequest.Result.Success)
  268. {
  269. Debug.LogWarning("水文站联网不成功,原因:返request不成功");
  270. successInternet = false;
  271. }
  272. else
  273. {
  274. JObject jsonObject = JObject.Parse(requestData.downloadHandler.text);
  275. // 提取data字段的值
  276. JToken dataToken = jsonObject["data"];
  277. JToken codeToken = jsonObject["code"];
  278. if (codeToken.ToString() == "200")
  279. {
  280. GlobalData.qXZDatas = JsonConvert.DeserializeObject<List<QXZData>>(dataToken.ToString());
  281. }
  282. else
  283. {
  284. Debug.LogWarning("水文站联网不成功,原因:返序列化失败");
  285. successInternet = false;
  286. }
  287. }
  288. }
  289. catch (Exception e)
  290. {
  291. successInternet = false;
  292. Debug.LogWarning("水文站联网不成功,原因:" + e.ToString());
  293. }
  294. requestData.Dispose();
  295. successInternet = true;
  296. UnityWebRequest requestData1 = UnityWebRequest.Get(ServerAddress.APIGetGeSWZData);
  297. await requestData1.SendWebRequest();
  298. try
  299. {
  300. if (requestData1.result != UnityWebRequest.Result.Success)
  301. {
  302. Debug.LogWarning("水文站联网不成功,原因:返request不成功");
  303. successInternet = false;
  304. }
  305. else
  306. {
  307. JObject jsonObject = JObject.Parse(requestData1.downloadHandler.text);
  308. // 提取data字段的值
  309. JToken dataToken = jsonObject["data"];
  310. JToken codeToken = jsonObject["code"];
  311. if (codeToken.ToString() == "200")
  312. {
  313. GlobalData.sWZDatas = JsonConvert.DeserializeObject<List<SWZData>>(dataToken.ToString());
  314. }
  315. else
  316. {
  317. Debug.LogWarning("水文站联网不成功,原因:返序列化失败");
  318. successInternet = false;
  319. }
  320. }
  321. }
  322. catch (Exception e)
  323. {
  324. successInternet = false;
  325. Debug.LogWarning("水文站联网不成功,原因:" + e.ToString());
  326. }
  327. requestData1.Dispose();
  328. }
  329. public async Task InitSWYJData() {
  330. InitSWYJData0();
  331. await InitSWYJData1();
  332. await InitSWYJData2();
  333. OnSWYJRefresh?.Invoke();
  334. }
  335. public async Task InitSWYJData1()
  336. {
  337. GlobalData.BuYuanShuiWeiDataList = new List<ShuiWeiData>();
  338. var devicesInfo = _devicesIdDatas_BuYuan["上游水位计"];
  339. string sw1Result = await GetSingleDevicesInfo(devicesInfo.ChannelID, devicesInfo.DeviceID, 24);
  340. try
  341. {
  342. GetDevicesRequestData tempData =JsonConvert.DeserializeObject<GetDevicesRequestData>(sw1Result);
  343. if (tempData != null)
  344. {
  345. GlobalData.BuYuanShuiWeiDataList.Add(new ShuiWeiData()
  346. {
  347. name = "补元上游水位",
  348. targetName = "BY_ShuiWei_Up",
  349. value = tempData.datas[0].NodeValue,
  350. type = shuiWeiType.BuYuanShuiWei,
  351. datas = tempData.datas.ToArray()
  352. });
  353. }
  354. }
  355. catch (Exception e)
  356. {
  357. Debug.LogWarning("补元上游水位不成功,原因:" + e.ToString());
  358. }
  359. var devicesInfo1 = _devicesIdDatas_BuYuan["下游水位计"];
  360. string sw2Result = await GetSingleDevicesInfo(devicesInfo1.ChannelID, devicesInfo1.DeviceID, 24);
  361. try
  362. {
  363. GetDevicesRequestData tempData = JsonConvert.DeserializeObject<GetDevicesRequestData>(sw2Result);
  364. if (tempData != null)
  365. {
  366. GlobalData.BuYuanShuiWeiDataList.Add(new ShuiWeiData()
  367. {
  368. name = "补元下游水位",
  369. targetName = "BY_ShuiWei_Down",
  370. value = tempData.datas[0].NodeValue,
  371. type = shuiWeiType.BuYuanShuiWei,
  372. datas = tempData.datas.ToArray()
  373. });
  374. }
  375. }
  376. catch (Exception e)
  377. {
  378. Debug.LogWarning("补元下游水位不成功,原因:" + e.ToString());
  379. }
  380. string dayResult = await GetSingleDevicesInfo_day(devicesInfo.ChannelID, devicesInfo.DeviceID, 6);
  381. try
  382. {
  383. GetDevicesRequestData tempData = JsonConvert.DeserializeObject<GetDevicesRequestData>(dayResult);
  384. GlobalData.buYuanShuiWei_day = new ShuiWeiBianHuaData();
  385. GlobalData.buYuanShuiWei_day.name = new string[6];
  386. GlobalData.buYuanShuiWei_day.value = new double[6];
  387. for (int i = 0; i < tempData.datas.Count; i++)
  388. {
  389. GlobalData.buYuanShuiWei_day.name[i] = tempData.datas[i].RecvTime;
  390. GlobalData.buYuanShuiWei_day.value[i] = tempData.datas[i].NodeValue;
  391. }
  392. }
  393. catch (Exception e)
  394. {
  395. Debug.LogWarning("获取6天平均数据不成功,原因:" + e.ToString());
  396. }
  397. string monthResult = await GetSingleDevicesInfo_month(devicesInfo.ChannelID, devicesInfo.DeviceID, 6);
  398. try
  399. {
  400. GetDevicesRequestData tempData = JsonConvert.DeserializeObject<GetDevicesRequestData>(monthResult);
  401. GlobalData.buYuanShuiWei_month = new ShuiWeiBianHuaData();
  402. GlobalData.buYuanShuiWei_month.name = new string[6];
  403. GlobalData.buYuanShuiWei_month.value = new double[6];
  404. for (int i = 0; i < tempData.datas.Count; i++)
  405. {
  406. GlobalData.buYuanShuiWei_month.name[i] = tempData.datas[i].RecvTime;
  407. GlobalData.buYuanShuiWei_month.value[i] = tempData.datas[i].NodeValue;
  408. }
  409. }
  410. catch (Exception e)
  411. {
  412. Debug.LogWarning("获取6月平均数据不成功,原因:" + e.ToString());
  413. }
  414. string hourResult = await GetSingleDevicesInfo_hour(devicesInfo.ChannelID, devicesInfo.DeviceID, 6);
  415. try
  416. {
  417. GetDevicesRequestData tempData = JsonConvert.DeserializeObject<GetDevicesRequestData>(hourResult);
  418. GlobalData.buYuanShuiWei_hour = new ShuiWeiBianHuaData();
  419. GlobalData.buYuanShuiWei_hour.name = new string[6];
  420. GlobalData.buYuanShuiWei_hour.value = new double[6];
  421. for (int i = 0; i < tempData.datas.Count; i++)
  422. {
  423. GlobalData.buYuanShuiWei_hour.name[i] = tempData.datas[i].RecvTime;
  424. GlobalData.buYuanShuiWei_hour.value[i] = tempData.datas[i].NodeValue;
  425. }
  426. }
  427. catch (Exception e)
  428. {
  429. Debug.LogWarning("获取6小时平均数据不成功,原因:" + e.ToString());
  430. }
  431. }
  432. public async Task InitSWYJData2() {
  433. bool successInternet = true;
  434. UnityWebRequest requestData = UnityWebRequest.Get(ServerAddress.APIGetGeSWData);
  435. await requestData.SendWebRequest();
  436. try
  437. {
  438. if (requestData.result != UnityWebRequest.Result.Success)
  439. {
  440. Debug.LogWarning("水位高度联网不成功,原因:返request不成功");
  441. successInternet = false;
  442. }
  443. else
  444. {
  445. JObject jsonObject = JObject.Parse(requestData.downloadHandler.text);
  446. // 提取data字段的值
  447. JToken dataToken = jsonObject["data"];
  448. JToken codeToken = jsonObject["code"];
  449. if (codeToken.ToString() == "200")
  450. {
  451. GlobalData.swDatas = JsonConvert.DeserializeObject<List<StationData>>(dataToken.ToString());
  452. }
  453. else
  454. {
  455. Debug.LogWarning("水位高度联网不成功,原因:返序列化失败");
  456. successInternet = false;
  457. }
  458. for (int i = 0; i < GlobalData.BuYuanShuiWeiDataList.Count; i++)
  459. {
  460. StationData stationData = new StationData();
  461. stationData.LGTD = 113.910187f;
  462. stationData.LTTD = 30.1765823f;
  463. stationData.STNM = GlobalData.BuYuanShuiWeiDataList[i].name;
  464. stationData.upz = GlobalData.BuYuanShuiWeiDataList[i].name.Contains("上")? GlobalData.BuYuanShuiWeiDataList[i].value:-1;
  465. stationData.dwz = GlobalData.BuYuanShuiWeiDataList[i].name.Contains("下") ? GlobalData.BuYuanShuiWeiDataList[i].value : -1;
  466. GlobalData.swDatas.Add(stationData);
  467. }
  468. }
  469. }
  470. catch (Exception e)
  471. {
  472. successInternet = false;
  473. Debug.LogWarning("水位高度联网不成功,原因:" + e.ToString());
  474. }
  475. }
  476. public async Task InitSWYJData0()
  477. {
  478. bool successInternet = true;
  479. UnityWebRequest requestData = UnityWebRequest.Get(ServerAddress.APIGetThreeLevel);
  480. await requestData.SendWebRequest();
  481. try
  482. {
  483. if (requestData.result != UnityWebRequest.Result.Success)
  484. {
  485. Debug.LogWarning("三线水位联网不成功,原因:返request不成功");
  486. successInternet = false;
  487. }
  488. else
  489. {
  490. JObject jsonObject = JObject.Parse(requestData.downloadHandler.text);
  491. // 提取data字段的值
  492. JToken dataToken = jsonObject["data"];
  493. JToken codeToken = jsonObject["code"];
  494. if (codeToken.ToString() == "200")
  495. {
  496. GlobalData.threeLevelShuiWeiDatas = JsonConvert.DeserializeObject<List<ThreeLevelShuiWeiData>>(dataToken.ToString());
  497. }
  498. else
  499. {
  500. Debug.LogWarning("三线水位联网不成功,原因:返序列化失败");
  501. successInternet = false;
  502. }
  503. }
  504. }
  505. catch (Exception e)
  506. {
  507. successInternet = false;
  508. Debug.LogWarning("三线水位联网不成功,原因:" + e.ToString());
  509. }
  510. }
  511. public async Task SendBuYuan_GetSensorData()
  512. {
  513. UnityWebRequest www = UnityWebRequest.Get(ServerAddress.API_BuYuan_GetSensorData);
  514. await www.SendWebRequest();
  515. try
  516. {
  517. if (www.result != UnityWebRequest.Result.Success)
  518. {
  519. Debug.LogWarning("获取补元闸门设备数据不成功,原因:返request不成功:" + www.downloadHandler.text);
  520. }
  521. else
  522. {
  523. GlobalData.buYuanSensorData= JsonUtility.FromJson<SensorDataResult>(www.downloadHandler.text);
  524. }
  525. }
  526. catch (Exception e)
  527. {
  528. Debug.LogWarning("获取补元闸门设备数据不成功,原因:" +e.ToString());
  529. }
  530. www.Dispose();
  531. }
  532. public async Task GetBuYuanDevicesGroupInfo()
  533. {
  534. //获取所有渗压记
  535. GetDevicesData tempData = new GetDevicesData();
  536. tempData.idsList = new List<DevicesMessage>();
  537. for (int i = 0; i < 13; i++)
  538. {
  539. var devicesInfo = _devicesIdDatas_BuYuan[$"P{i+1}"];
  540. tempData.idsList.Add(new DevicesMessage()
  541. {
  542. DeviceID = devicesInfo.DeviceID,
  543. ChannelID = devicesInfo.ChannelID
  544. });
  545. }
  546. string jsonStr;
  547. try
  548. {
  549. jsonStr = await Devices(tempData);
  550. jsonStr = "{\"datas\":" + jsonStr+"}";
  551. GetDevicesRequestData resultData= JsonUtility.FromJson<GetDevicesRequestData>(jsonStr);
  552. GlobalData.BuYuanShuiYaDataList = new List<ShuiYaData>();
  553. for (int i = 0; i < resultData.datas.Count; i++)
  554. {
  555. GlobalData.BuYuanShuiYaDataList.Add(new ShuiYaData()
  556. {
  557. name = resultData.datas[i].NodeName,
  558. value = resultData.datas[i].NodeValue,
  559. type = shuiYaType.BuYuanShuiYa,
  560. targetName = $"BY_{resultData.datas[i].NodeName}"
  561. });
  562. }
  563. }
  564. catch (Exception e)
  565. {
  566. Debug.LogWarning("获取补元水压计设备数据不成功,原因:" +e.ToString());
  567. throw;
  568. }
  569. //获取所有位移记
  570. tempData = new GetDevicesData();
  571. tempData.idsList = new List<DevicesMessage>();
  572. for (int i = 0; i < 8; i++)
  573. {
  574. var devicesInfo = _devicesIdDatas_BuYuan[$"M{i+1}"];
  575. tempData.idsList.Add(new DevicesMessage()
  576. {
  577. DeviceID = devicesInfo.DeviceID,
  578. ChannelID = devicesInfo.ChannelID
  579. });
  580. }
  581. try
  582. {
  583. jsonStr = await Devices(tempData);
  584. jsonStr = "{\"datas\":" + jsonStr+"}";
  585. GetDevicesRequestData resultData= JsonUtility.FromJson<GetDevicesRequestData>(jsonStr);
  586. GlobalData.BuYuanWeiYiDataList = new List<WeiYiData>();
  587. for (int i = 0; i < resultData.datas.Count; i++)
  588. {
  589. GlobalData.BuYuanWeiYiDataList.Add(new WeiYiData()
  590. {
  591. name = resultData.datas[i].NodeName,
  592. value = resultData.datas[i].NodeValue,
  593. type = weiYiType.BuYuanWeiYi,
  594. targetName = $"BY_{resultData.datas[i].NodeName}"
  595. });
  596. }
  597. }
  598. catch (Exception e)
  599. {
  600. Debug.LogWarning("获取补元位移计设备数据不成功,原因:" +e.ToString());
  601. throw;
  602. }
  603. }
  604. /// <summary>
  605. /// 获取补元硬件组数据
  606. /// </summary>
  607. /// <param name="sendData"></param>
  608. /// <returns></returns>
  609. public async Task<string> Devices(GetDevicesData sendData)
  610. {
  611. string cmdUrl = ServerAddress.API_BuYuan_GetDevicesGroupInfo;
  612. string tempStr = JsonConvert.SerializeObject(sendData);
  613. var sendByte = System.Text.Encoding.UTF8.GetBytes(tempStr);
  614. UnityWebRequest www = new UnityWebRequest(cmdUrl, "POST");
  615. www.uploadHandler = new UploadHandlerRaw(sendByte);
  616. www.downloadHandler = new DownloadHandlerBuffer();
  617. www.SetRequestHeader("Content-Type", "application/json");
  618. await www.SendWebRequest();
  619. if (www.result != UnityWebRequest.Result.Success)
  620. {
  621. Debug.LogWarning($"获取设备数据不成功,原因:返request不成功:" + www.downloadHandler.text);
  622. }
  623. string result = www.downloadHandler.text;
  624. www.Dispose();
  625. return result;
  626. }
  627. /// <summary>
  628. /// 获取设备的最近N条记录
  629. /// </summary>
  630. /// <param name="channelId"></param>
  631. /// <param name="deviceId"></param>
  632. /// <param name="count"></param>
  633. /// <returns></returns>
  634. public async Task<string> GetSingleDevicesInfo(int channelId, int deviceId, int count) {
  635. string cmdUrl = ServerAddress.API_BuYuan_GetSingleDevicesInfo;
  636. GetSingleDeviceData sendData = new GetSingleDeviceData();
  637. sendData.ChannelID = channelId;
  638. sendData.DeviceID = deviceId;
  639. sendData.N = count;
  640. string tempStr = JsonConvert.SerializeObject(sendData);
  641. var sendByte = System.Text.Encoding.UTF8.GetBytes(tempStr);
  642. UnityWebRequest www = new UnityWebRequest(cmdUrl, "POST");
  643. www.uploadHandler = new UploadHandlerRaw(sendByte);
  644. www.downloadHandler = new DownloadHandlerBuffer();
  645. www.SetRequestHeader("Content-Type", "application/json");
  646. await www.SendWebRequest();
  647. if (www.result != UnityWebRequest.Result.Success)
  648. {
  649. Debug.LogWarning("获取设备数据不成功,原因:返request不成功:" + www.downloadHandler.text);
  650. }
  651. string result = www.downloadHandler.text;
  652. www.Dispose();
  653. return result;
  654. }
  655. /// <summary>
  656. /// 获取设备最近几天的平均数据
  657. /// </summary>
  658. /// <param name="channelId"></param>
  659. /// <param name="deviceId"></param>
  660. /// <param name="count"></param>
  661. /// <returns></returns>
  662. public async Task<string> GetSingleDevicesInfo_day(int channelId, int deviceId, int count) {
  663. string cmdUrl = ServerAddress.API_BuYuan_GetSingleDevicesInfo_avg;
  664. GetSingleDeviceData_AvgType sendData = new GetSingleDeviceData_AvgType();
  665. sendData.ChannelID = channelId;
  666. sendData.DeviceID = deviceId;
  667. sendData.AvgType = "day";
  668. sendData.N = count;
  669. string tempStr = JsonConvert.SerializeObject(sendData);
  670. var sendByte = System.Text.Encoding.UTF8.GetBytes(tempStr);
  671. UnityWebRequest www = new UnityWebRequest(cmdUrl, "POST");
  672. www.uploadHandler = new UploadHandlerRaw(sendByte);
  673. www.downloadHandler = new DownloadHandlerBuffer();
  674. www.SetRequestHeader("Content-Type", "application/json");
  675. await www.SendWebRequest();
  676. if (www.result != UnityWebRequest.Result.Success)
  677. {
  678. Debug.LogWarning("获取设备天平均数据不成功,原因:返request不成功:" + www.downloadHandler.text);
  679. }
  680. string result = www.downloadHandler.text;
  681. www.Dispose();
  682. return result;
  683. }
  684. /// <summary>
  685. /// 获取设备最近几小时的平均数据
  686. /// </summary>
  687. /// <param name="channelId"></param>
  688. /// <param name="deviceId"></param>
  689. /// <param name="count"></param>
  690. /// <returns></returns>
  691. public async Task<string> GetSingleDevicesInfo_hour(int channelId, int deviceId, int count) {
  692. string cmdUrl = ServerAddress.API_BuYuan_GetSingleDevicesInfo_avg;
  693. GetSingleDeviceData_AvgType sendData = new GetSingleDeviceData_AvgType();
  694. sendData.ChannelID = channelId;
  695. sendData.DeviceID = deviceId;
  696. sendData.AvgType = "hour";
  697. sendData.N = count;
  698. string tempStr = JsonConvert.SerializeObject(sendData);
  699. var sendByte = System.Text.Encoding.UTF8.GetBytes(tempStr);
  700. UnityWebRequest www = new UnityWebRequest(cmdUrl, "POST");
  701. www.uploadHandler = new UploadHandlerRaw(sendByte);
  702. www.downloadHandler = new DownloadHandlerBuffer();
  703. www.SetRequestHeader("Content-Type", "application/json");
  704. await www.SendWebRequest();
  705. if (www.result != UnityWebRequest.Result.Success)
  706. {
  707. Debug.LogWarning("获取设备小时平均数据不成功,原因:返request不成功:" + www.downloadHandler.text);
  708. }
  709. string result = www.downloadHandler.text;
  710. www.Dispose();
  711. return result;
  712. }
  713. /// <summary>
  714. /// 获取设备最近几个月的平均数据
  715. /// </summary>
  716. /// <param name="channelId"></param>
  717. /// <param name="deviceId"></param>
  718. /// <param name="count"></param>
  719. /// <returns></returns>
  720. public async Task<string> GetSingleDevicesInfo_month(int channelId, int deviceId, int count)
  721. {
  722. string cmdUrl = ServerAddress.API_BuYuan_GetSingleDevicesInfo_avg;
  723. GetSingleDeviceData_AvgType sendData = new GetSingleDeviceData_AvgType();
  724. sendData.ChannelID = channelId;
  725. sendData.DeviceID = deviceId;
  726. sendData.AvgType = "month";
  727. sendData.N = count;
  728. string tempStr = JsonConvert.SerializeObject(sendData);
  729. var sendByte = System.Text.Encoding.UTF8.GetBytes(tempStr);
  730. UnityWebRequest www = new UnityWebRequest(cmdUrl, "POST");
  731. www.uploadHandler = new UploadHandlerRaw(sendByte);
  732. www.downloadHandler = new DownloadHandlerBuffer();
  733. www.SetRequestHeader("Content-Type", "application/json");
  734. await www.SendWebRequest();
  735. if (www.result != UnityWebRequest.Result.Success)
  736. {
  737. Debug.LogWarning("获取设备月平均数据不成功,原因:返request不成功:" + www.downloadHandler.text);
  738. }
  739. string result = www.downloadHandler.text;
  740. www.Dispose();
  741. return result;
  742. }
  743. public async Task InitGCYWData()
  744. {
  745. string jsonStr;
  746. try
  747. {
  748. jsonStr = await B08_API_data(ServerAddress.API_B08_dataStatis);
  749. GlobalData.B08DataStatis=JsonConvert.DeserializeObject<B08_dataStatis>(jsonStr);
  750. }
  751. catch (Exception e)
  752. {
  753. Debug.LogWarning("获取API_B08_dataStatis数据不成功,原因:" + e.ToString());
  754. }
  755. try
  756. {
  757. jsonStr = await B08_API_data(ServerAddress.API_B08_inspectResult);
  758. GlobalData.B08InspectResult=JsonConvert.DeserializeObject<B08_inspectResult>(jsonStr);
  759. }
  760. catch (Exception e)
  761. {
  762. Debug.LogWarning("获取API_B08_inspectResult数据不成功,原因:" + e.ToString());
  763. }
  764. try
  765. {
  766. jsonStr = await B08_API_data(ServerAddress.API_B08_projectExamine);
  767. GlobalData.B08ProjectExamine=JsonConvert.DeserializeObject<B08_projectExamine>(jsonStr);
  768. }
  769. catch (Exception e)
  770. {
  771. Debug.LogWarning("获取API_B08_projectExamine数据不成功,原因:" + e.ToString());
  772. }
  773. try
  774. {
  775. jsonStr = await B08_API_data(ServerAddress.API_B08_event);
  776. GlobalData.B08Event=JsonConvert.DeserializeObject<B08_event>(jsonStr);
  777. }
  778. catch (Exception e)
  779. {
  780. Debug.LogWarning("获取API_B08_event数据不成功,原因:" + e.ToString());
  781. }
  782. try
  783. {
  784. jsonStr = await B08_API_data(ServerAddress.API_B08_questionStatis);
  785. GlobalData.B08QuestionStatis=JsonConvert.DeserializeObject<B08_questionStatis>(jsonStr);
  786. }
  787. catch (Exception e)
  788. {
  789. Debug.LogWarning("获取API_B08_questionStatis数据不成功,原因:" + e.ToString());
  790. }
  791. try
  792. {
  793. jsonStr = await B08_API_data(ServerAddress.API_B08_inspectType);
  794. GlobalData.B08InspectType=JsonConvert.DeserializeObject<B08_inspectType>(jsonStr);
  795. }
  796. catch (Exception e)
  797. {
  798. Debug.LogWarning("获取API_B08_inspectType数据不成功,原因:" + e.ToString());
  799. }
  800. }
  801. public async Task<string> B08_API_data(string cmdUrl)
  802. {
  803. UnityWebRequest www = new UnityWebRequest(cmdUrl, "Get");
  804. www.downloadHandler = new DownloadHandlerBuffer();
  805. www.SetRequestHeader("Content-Type", "application/json");
  806. await www.SendWebRequest();
  807. if (www.result != UnityWebRequest.Result.Success)
  808. {
  809. Debug.LogWarning($"获取{cmdUrl}数据不成功,原因:返request不成功:" + www.downloadHandler.text);
  810. }
  811. string result = www.downloadHandler.text;
  812. www.Dispose();
  813. return result;
  814. }
  815. public void SaveFirstFrame(byte[] uploadData,string fileName,string _targetName)
  816. {
  817. StartCoroutine(SendSaveFirstFrame(uploadData, fileName, _targetName));
  818. }
  819. IEnumerator SendSaveFirstFrame(byte[] uploadData,string fileName,string _targetName)
  820. {
  821. Debug.Log(fileName);
  822. WWWForm form = new WWWForm();
  823. form.AddBinaryData("file",uploadData,fileName);
  824. UnityWebRequest www = UnityWebRequest.Post(ServerAddress.API_TextureUploadPath, form);
  825. yield return www.SendWebRequest();
  826. if (www.result == UnityWebRequest.Result.Success)
  827. {
  828. ActionInstance._Instance.obsFmUpdata?.Invoke(_targetName);
  829. }
  830. Debug.Log(www.downloadHandler.text);
  831. www.Dispose();
  832. }
  833. #region 摄像头远程控制
  834. public void GetDeviceChannelId(string deviceId, Action<string> callBack)
  835. {
  836. StartCoroutine(SendGetDevicesChannel(deviceId, 1, 1, callBack));
  837. }
  838. IEnumerator SendGetDevicesChannel(string deviceId, int page, int count, Action<string> call)
  839. {
  840. string sendUrl = ServerAddress.API_GetDevicesInfo;
  841. sendUrl += $"/{deviceId}/channels?page={page}&count={count}&query&online&channelType&catalogUnderDevice";
  842. UnityWebRequest www = UnityWebRequest.Get(sendUrl);
  843. yield return www.SendWebRequest();
  844. ObsHttpRequestData tempData = JsonConvert.DeserializeObject<ObsHttpRequestData>(www.downloadHandler.text);
  845. GetObsChannelRequestData channelData = tempData.data.ToObject<GetObsChannelRequestData>();
  846. call?.Invoke(channelData.list[0].channelId);
  847. www.Dispose();
  848. }
  849. IEnumerator SendStartRTSP(string deviceId, string channelId)
  850. {
  851. string sendUrl = ServerAddress.API_PlayStart;
  852. sendUrl += $"/{deviceId}/{channelId}";
  853. UnityWebRequest www = UnityWebRequest.Get(sendUrl);
  854. yield return www.SendWebRequest();
  855. Debug.Log(www.downloadHandler.text);
  856. www.Dispose();
  857. }
  858. public void SendObsCameraCtrlCmd(string deviceId, string channelId, ObsCtrlType type)
  859. {
  860. StartCoroutine(SendCameraCtrl(deviceId, channelId, type.ToString()));
  861. }
  862. IEnumerator SendCameraCtrl(string deviceId, string channelId, string command)
  863. {
  864. string sendUrl = ServerAddress.API_CameraCtrl;
  865. sendUrl += $"/{deviceId}/{channelId}?command={command}&horizonSpeed=10&verticalSpeed=10&zoomSpeed=10";
  866. UnityWebRequest www = UnityWebRequest.Post(sendUrl, "");
  867. Debug.Log("send:" + sendUrl);
  868. yield return www.SendWebRequest();
  869. Debug.Log(www.downloadHandler.text);
  870. www.Dispose();
  871. }
  872. #endregion
  873. /// <summary>
  874. /// 初始化所有补元硬件设备的获取信息
  875. /// </summary>
  876. public void InItBuYuanDevicesList()
  877. {
  878. _devicesIdDatas_BuYuan = new Dictionary<string, DevicesId_Data>();
  879. _devicesIdDatas_BuYuan.Add("上游水位计", new DevicesId_Data()
  880. {
  881. ChannelID = 7,
  882. DeviceID = 4,
  883. name = "上游水位计",
  884. showName = "",
  885. value = -1
  886. });
  887. _devicesIdDatas_BuYuan.Add("下游水位计", new DevicesId_Data()
  888. {
  889. ChannelID = 8,
  890. DeviceID = 4,
  891. name = "上游水位计",
  892. showName = "",
  893. value = -1
  894. });
  895. #region P
  896. _devicesIdDatas_BuYuan.Add("P1", new DevicesId_Data()
  897. {
  898. ChannelID = 1,
  899. DeviceID = 2,
  900. name = "P1/UP1",
  901. showName = "",
  902. value = -1
  903. });
  904. _devicesIdDatas_BuYuan.Add("P2", new DevicesId_Data()
  905. {
  906. ChannelID = 2,
  907. DeviceID = 2,
  908. name = "P2/UP2",
  909. showName = "",
  910. value = -1
  911. });
  912. _devicesIdDatas_BuYuan.Add("P3", new DevicesId_Data()
  913. {
  914. ChannelID = 3,
  915. DeviceID = 2,
  916. name = "P3",
  917. showName = "",
  918. value = -1
  919. });
  920. _devicesIdDatas_BuYuan.Add("P4", new DevicesId_Data()
  921. {
  922. ChannelID = 4,
  923. DeviceID = 2,
  924. name = "P4/UP3",
  925. showName = "",
  926. value = -1
  927. });
  928. _devicesIdDatas_BuYuan.Add("P5", new DevicesId_Data()
  929. {
  930. ChannelID = 1,
  931. DeviceID = 3,
  932. name = "P5/UP5",
  933. showName = "",
  934. value = -1
  935. });
  936. _devicesIdDatas_BuYuan.Add("P6", new DevicesId_Data()
  937. {
  938. ChannelID = 2,
  939. DeviceID = 3,
  940. name = "P6/",
  941. showName = "",
  942. value = -1
  943. });
  944. _devicesIdDatas_BuYuan.Add("P7", new DevicesId_Data()
  945. {
  946. ChannelID = 3,
  947. DeviceID = 3,
  948. name = "P7/UP5",
  949. showName = "",
  950. value = -1
  951. });
  952. _devicesIdDatas_BuYuan.Add("P8", new DevicesId_Data()
  953. {
  954. ChannelID = 4,
  955. DeviceID = 3,
  956. name = "P8/UP6",
  957. showName = "",
  958. value = -1
  959. });
  960. _devicesIdDatas_BuYuan.Add("P5温度", new DevicesId_Data()
  961. {
  962. ChannelID = 9,
  963. DeviceID = 3,
  964. name = "P5温度",
  965. showName = "",
  966. value = -1
  967. });
  968. _devicesIdDatas_BuYuan.Add("P6温度", new DevicesId_Data()
  969. {
  970. ChannelID = 10,
  971. DeviceID = 3,
  972. name = "P6温度",
  973. showName = "",
  974. value = -1
  975. });
  976. _devicesIdDatas_BuYuan.Add("P7温度", new DevicesId_Data()
  977. {
  978. ChannelID = 11,
  979. DeviceID = 3,
  980. name = "P7温度",
  981. showName = "",
  982. value = -1
  983. });
  984. _devicesIdDatas_BuYuan.Add("P8温度", new DevicesId_Data()
  985. {
  986. ChannelID = 12,
  987. DeviceID = 3,
  988. name = "P8温度",
  989. showName = "",
  990. value = -1
  991. });
  992. _devicesIdDatas_BuYuan.Add("P9温度", new DevicesId_Data()
  993. {
  994. ChannelID = 9,
  995. DeviceID = 4,
  996. name = "P9温度",
  997. showName = "",
  998. value = -1
  999. });
  1000. _devicesIdDatas_BuYuan.Add("P10温度", new DevicesId_Data()
  1001. {
  1002. ChannelID = 10,
  1003. DeviceID = 4,
  1004. name = "P10温度",
  1005. showName = "",
  1006. value = -1
  1007. });
  1008. _devicesIdDatas_BuYuan.Add("P12温度", new DevicesId_Data()
  1009. {
  1010. ChannelID = 11,
  1011. DeviceID = 4,
  1012. name = "P12温度",
  1013. showName = "",
  1014. value = -1
  1015. });
  1016. _devicesIdDatas_BuYuan.Add("P9", new DevicesId_Data()
  1017. {
  1018. ChannelID = 3,
  1019. DeviceID = 4,
  1020. name = "P9",
  1021. showName = "",
  1022. value = -1
  1023. });
  1024. _devicesIdDatas_BuYuan.Add("P10", new DevicesId_Data()
  1025. {
  1026. ChannelID = 4,
  1027. DeviceID = 4,
  1028. name = "P10/UP7",
  1029. showName = "",
  1030. value = -1
  1031. });
  1032. _devicesIdDatas_BuYuan.Add("P11", new DevicesId_Data()
  1033. {
  1034. ChannelID = 5,
  1035. DeviceID = 4,
  1036. name = "P11/UP8",
  1037. showName = "",
  1038. value = -1
  1039. });
  1040. _devicesIdDatas_BuYuan.Add("P12", new DevicesId_Data()
  1041. {
  1042. ChannelID = 1,
  1043. DeviceID = 4,
  1044. name = "P12/UP9",
  1045. showName = "",
  1046. value = -1
  1047. });
  1048. _devicesIdDatas_BuYuan.Add("P13", new DevicesId_Data()
  1049. {
  1050. ChannelID = 2,
  1051. DeviceID = 4,
  1052. name = "P13/UP10",
  1053. showName = "",
  1054. value = -1
  1055. });
  1056. #endregion
  1057. #region M
  1058. //M1-8
  1059. int m_cid = 1;
  1060. int m_did = 5;
  1061. for (int i = 0; i < 8; i++)
  1062. {
  1063. _devicesIdDatas_BuYuan.Add($"M{i + 1}", new DevicesId_Data()
  1064. {
  1065. ChannelID = m_cid + i,
  1066. DeviceID = m_did,
  1067. name = $"M{i + 1}",
  1068. showName = "",
  1069. value = -1
  1070. });
  1071. }
  1072. //M温度
  1073. _devicesIdDatas_BuYuan.Add($"M2温度", new DevicesId_Data()
  1074. {
  1075. ChannelID = 10,
  1076. DeviceID = 5,
  1077. name = $"M2温度",
  1078. showName = "",
  1079. value = -1
  1080. });
  1081. _devicesIdDatas_BuYuan.Add($"M5温度", new DevicesId_Data()
  1082. {
  1083. ChannelID = 13,
  1084. DeviceID = 5,
  1085. name = $"M5温度",
  1086. showName = "",
  1087. value = -1
  1088. });
  1089. _devicesIdDatas_BuYuan.Add($"M6温度", new DevicesId_Data()
  1090. {
  1091. ChannelID = 14,
  1092. DeviceID = 5,
  1093. name = $"M6温度",
  1094. showName = "",
  1095. value = -1
  1096. });
  1097. _devicesIdDatas_BuYuan.Add($"M7温度", new DevicesId_Data()
  1098. {
  1099. ChannelID = 15,
  1100. DeviceID = 5,
  1101. name = $"M7温度",
  1102. showName = "",
  1103. value = -1
  1104. });
  1105. _devicesIdDatas_BuYuan.Add($"M8温度", new DevicesId_Data()
  1106. {
  1107. ChannelID = 16,
  1108. DeviceID = 5,
  1109. name = $"M8温度",
  1110. showName = "",
  1111. value = -1
  1112. });
  1113. #endregion
  1114. }
  1115. }