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