HttpHelper.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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. Debug.Log("API_B08_dataStatis"+jsonStr);
  751. GlobalData.B08DataStatis=JsonConvert.DeserializeObject<B08_dataStatis>(jsonStr);
  752. }
  753. catch (Exception e)
  754. {
  755. Debug.LogWarning("获取API_B08_dataStatis数据不成功,原因:" + e.ToString());
  756. }
  757. try
  758. {
  759. jsonStr = await B08_API_data(ServerAddress.API_B08_inspectResult);
  760. Debug.Log("API_B08_inspectResult"+jsonStr);
  761. GlobalData.B08InspectResult=JsonConvert.DeserializeObject<B08_inspectResult>(jsonStr);
  762. }
  763. catch (Exception e)
  764. {
  765. Debug.LogWarning("获取API_B08_inspectResult数据不成功,原因:" + e.ToString());
  766. }
  767. try
  768. {
  769. jsonStr = await B08_API_data(ServerAddress.API_B08_projectExamine);
  770. Debug.Log("API_B08_projectExamine"+jsonStr);
  771. GlobalData.B08ProjectExamine=JsonConvert.DeserializeObject<B08_projectExamine>(jsonStr);
  772. }
  773. catch (Exception e)
  774. {
  775. Debug.LogWarning("获取API_B08_projectExamine数据不成功,原因:" + e.ToString());
  776. }
  777. try
  778. {
  779. jsonStr = await B08_API_data(ServerAddress.API_B08_event);
  780. Debug.Log("API_B08_event"+jsonStr);
  781. GlobalData.B08Event=JsonConvert.DeserializeObject<B08_event>(jsonStr);
  782. }
  783. catch (Exception e)
  784. {
  785. Debug.LogWarning("获取API_B08_event数据不成功,原因:" + e.ToString());
  786. }
  787. try
  788. {
  789. jsonStr = await B08_API_data(ServerAddress.API_B08_questionStatis);
  790. Debug.Log("API_B08_questionStatis"+jsonStr);
  791. GlobalData.B08QuestionStatis=JsonConvert.DeserializeObject<B08_questionStatis>(jsonStr);
  792. }
  793. catch (Exception e)
  794. {
  795. Debug.LogWarning("获取API_B08_questionStatis数据不成功,原因:" + e.ToString());
  796. }
  797. try
  798. {
  799. jsonStr = await B08_API_data(ServerAddress.API_B08_inspectType);
  800. Debug.Log("API_B08_inspectType"+jsonStr);
  801. GlobalData.B08InspectType=JsonConvert.DeserializeObject<B08_inspectType>(jsonStr);
  802. }
  803. catch (Exception e)
  804. {
  805. Debug.LogWarning("获取API_B08_inspectType数据不成功,原因:" + e.ToString());
  806. }
  807. }
  808. public async Task<string> B08_API_data(string cmdUrl)
  809. {
  810. UnityWebRequest www = new UnityWebRequest(cmdUrl, "Get");
  811. www.downloadHandler = new DownloadHandlerBuffer();
  812. www.SetRequestHeader("Content-Type", "application/json");
  813. await www.SendWebRequest();
  814. if (www.result != UnityWebRequest.Result.Success)
  815. {
  816. Debug.LogWarning($"获取{cmdUrl}数据不成功,原因:返request不成功:" + www.downloadHandler.text);
  817. }
  818. string result = www.downloadHandler.text;
  819. www.Dispose();
  820. return result;
  821. }
  822. public void SaveFirstFrame(byte[] uploadData,string fileName,string _targetName)
  823. {
  824. StartCoroutine(SendSaveFirstFrame(uploadData, fileName, _targetName));
  825. }
  826. IEnumerator SendSaveFirstFrame(byte[] uploadData,string fileName,string _targetName)
  827. {
  828. Debug.Log(fileName);
  829. WWWForm form = new WWWForm();
  830. form.AddBinaryData("file",uploadData,fileName);
  831. UnityWebRequest www = UnityWebRequest.Post(ServerAddress.API_TextureUploadPath, form);
  832. yield return www.SendWebRequest();
  833. if (www.result == UnityWebRequest.Result.Success)
  834. {
  835. ActionInstance._Instance.obsFmUpdata?.Invoke(_targetName);
  836. }
  837. Debug.Log(www.downloadHandler.text);
  838. www.Dispose();
  839. }
  840. #region 摄像头远程控制
  841. public void GetDeviceChannelId(string deviceId, Action<string> callBack)
  842. {
  843. StartCoroutine(SendGetDevicesChannel(deviceId, 1, 1, callBack));
  844. }
  845. IEnumerator SendGetDevicesChannel(string deviceId, int page, int count, Action<string> call)
  846. {
  847. string sendUrl = ServerAddress.API_GetDevicesInfo;
  848. sendUrl += $"/{deviceId}/channels?page={page}&count={count}&query&online&channelType&catalogUnderDevice";
  849. UnityWebRequest www = UnityWebRequest.Get(sendUrl);
  850. yield return www.SendWebRequest();
  851. ObsHttpRequestData tempData = JsonConvert.DeserializeObject<ObsHttpRequestData>(www.downloadHandler.text);
  852. GetObsChannelRequestData channelData = tempData.data.ToObject<GetObsChannelRequestData>();
  853. call?.Invoke(channelData.list[0].channelId);
  854. www.Dispose();
  855. }
  856. IEnumerator SendStartRTSP(string deviceId, string channelId)
  857. {
  858. string sendUrl = ServerAddress.API_PlayStart;
  859. sendUrl += $"/{deviceId}/{channelId}";
  860. UnityWebRequest www = UnityWebRequest.Get(sendUrl);
  861. yield return www.SendWebRequest();
  862. Debug.Log(www.downloadHandler.text);
  863. www.Dispose();
  864. }
  865. public void SendObsCameraCtrlCmd(string deviceId, string channelId, ObsCtrlType type)
  866. {
  867. StartCoroutine(SendCameraCtrl(deviceId, channelId, type.ToString()));
  868. }
  869. IEnumerator SendCameraCtrl(string deviceId, string channelId, string command)
  870. {
  871. string sendUrl = ServerAddress.API_CameraCtrl;
  872. sendUrl += $"/{deviceId}/{channelId}?command={command}&horizonSpeed=10&verticalSpeed=10&zoomSpeed=10";
  873. UnityWebRequest www = UnityWebRequest.Post(sendUrl, "");
  874. Debug.Log("send:" + sendUrl);
  875. yield return www.SendWebRequest();
  876. Debug.Log(www.downloadHandler.text);
  877. www.Dispose();
  878. }
  879. #endregion
  880. /// <summary>
  881. /// 初始化所有补元硬件设备的获取信息
  882. /// </summary>
  883. public void InItBuYuanDevicesList()
  884. {
  885. _devicesIdDatas_BuYuan = new Dictionary<string, DevicesId_Data>();
  886. _devicesIdDatas_BuYuan.Add("上游水位计", new DevicesId_Data()
  887. {
  888. ChannelID = 7,
  889. DeviceID = 4,
  890. name = "上游水位计",
  891. showName = "",
  892. value = -1
  893. });
  894. _devicesIdDatas_BuYuan.Add("下游水位计", new DevicesId_Data()
  895. {
  896. ChannelID = 8,
  897. DeviceID = 4,
  898. name = "上游水位计",
  899. showName = "",
  900. value = -1
  901. });
  902. #region P
  903. _devicesIdDatas_BuYuan.Add("P1", new DevicesId_Data()
  904. {
  905. ChannelID = 1,
  906. DeviceID = 2,
  907. name = "P1/UP1",
  908. showName = "",
  909. value = -1
  910. });
  911. _devicesIdDatas_BuYuan.Add("P2", new DevicesId_Data()
  912. {
  913. ChannelID = 2,
  914. DeviceID = 2,
  915. name = "P2/UP2",
  916. showName = "",
  917. value = -1
  918. });
  919. _devicesIdDatas_BuYuan.Add("P3", new DevicesId_Data()
  920. {
  921. ChannelID = 3,
  922. DeviceID = 2,
  923. name = "P3",
  924. showName = "",
  925. value = -1
  926. });
  927. _devicesIdDatas_BuYuan.Add("P4", new DevicesId_Data()
  928. {
  929. ChannelID = 4,
  930. DeviceID = 2,
  931. name = "P4/UP3",
  932. showName = "",
  933. value = -1
  934. });
  935. _devicesIdDatas_BuYuan.Add("P5", new DevicesId_Data()
  936. {
  937. ChannelID = 1,
  938. DeviceID = 3,
  939. name = "P5/UP5",
  940. showName = "",
  941. value = -1
  942. });
  943. _devicesIdDatas_BuYuan.Add("P6", new DevicesId_Data()
  944. {
  945. ChannelID = 2,
  946. DeviceID = 3,
  947. name = "P6/",
  948. showName = "",
  949. value = -1
  950. });
  951. _devicesIdDatas_BuYuan.Add("P7", new DevicesId_Data()
  952. {
  953. ChannelID = 3,
  954. DeviceID = 3,
  955. name = "P7/UP5",
  956. showName = "",
  957. value = -1
  958. });
  959. _devicesIdDatas_BuYuan.Add("P8", new DevicesId_Data()
  960. {
  961. ChannelID = 4,
  962. DeviceID = 3,
  963. name = "P8/UP6",
  964. showName = "",
  965. value = -1
  966. });
  967. _devicesIdDatas_BuYuan.Add("P5温度", new DevicesId_Data()
  968. {
  969. ChannelID = 9,
  970. DeviceID = 3,
  971. name = "P5温度",
  972. showName = "",
  973. value = -1
  974. });
  975. _devicesIdDatas_BuYuan.Add("P6温度", new DevicesId_Data()
  976. {
  977. ChannelID = 10,
  978. DeviceID = 3,
  979. name = "P6温度",
  980. showName = "",
  981. value = -1
  982. });
  983. _devicesIdDatas_BuYuan.Add("P7温度", new DevicesId_Data()
  984. {
  985. ChannelID = 11,
  986. DeviceID = 3,
  987. name = "P7温度",
  988. showName = "",
  989. value = -1
  990. });
  991. _devicesIdDatas_BuYuan.Add("P8温度", new DevicesId_Data()
  992. {
  993. ChannelID = 12,
  994. DeviceID = 3,
  995. name = "P8温度",
  996. showName = "",
  997. value = -1
  998. });
  999. _devicesIdDatas_BuYuan.Add("P9温度", new DevicesId_Data()
  1000. {
  1001. ChannelID = 9,
  1002. DeviceID = 4,
  1003. name = "P9温度",
  1004. showName = "",
  1005. value = -1
  1006. });
  1007. _devicesIdDatas_BuYuan.Add("P10温度", new DevicesId_Data()
  1008. {
  1009. ChannelID = 10,
  1010. DeviceID = 4,
  1011. name = "P10温度",
  1012. showName = "",
  1013. value = -1
  1014. });
  1015. _devicesIdDatas_BuYuan.Add("P12温度", new DevicesId_Data()
  1016. {
  1017. ChannelID = 11,
  1018. DeviceID = 4,
  1019. name = "P12温度",
  1020. showName = "",
  1021. value = -1
  1022. });
  1023. _devicesIdDatas_BuYuan.Add("P9", new DevicesId_Data()
  1024. {
  1025. ChannelID = 3,
  1026. DeviceID = 4,
  1027. name = "P9",
  1028. showName = "",
  1029. value = -1
  1030. });
  1031. _devicesIdDatas_BuYuan.Add("P10", new DevicesId_Data()
  1032. {
  1033. ChannelID = 4,
  1034. DeviceID = 4,
  1035. name = "P10/UP7",
  1036. showName = "",
  1037. value = -1
  1038. });
  1039. _devicesIdDatas_BuYuan.Add("P11", new DevicesId_Data()
  1040. {
  1041. ChannelID = 5,
  1042. DeviceID = 4,
  1043. name = "P11/UP8",
  1044. showName = "",
  1045. value = -1
  1046. });
  1047. _devicesIdDatas_BuYuan.Add("P12", new DevicesId_Data()
  1048. {
  1049. ChannelID = 1,
  1050. DeviceID = 4,
  1051. name = "P12/UP9",
  1052. showName = "",
  1053. value = -1
  1054. });
  1055. _devicesIdDatas_BuYuan.Add("P13", new DevicesId_Data()
  1056. {
  1057. ChannelID = 2,
  1058. DeviceID = 4,
  1059. name = "P13/UP10",
  1060. showName = "",
  1061. value = -1
  1062. });
  1063. #endregion
  1064. #region M
  1065. //M1-8
  1066. int m_cid = 1;
  1067. int m_did = 5;
  1068. for (int i = 0; i < 8; i++)
  1069. {
  1070. _devicesIdDatas_BuYuan.Add($"M{i + 1}", new DevicesId_Data()
  1071. {
  1072. ChannelID = m_cid + i,
  1073. DeviceID = m_did,
  1074. name = $"M{i + 1}",
  1075. showName = "",
  1076. value = -1
  1077. });
  1078. }
  1079. //M温度
  1080. _devicesIdDatas_BuYuan.Add($"M2温度", new DevicesId_Data()
  1081. {
  1082. ChannelID = 10,
  1083. DeviceID = 5,
  1084. name = $"M2温度",
  1085. showName = "",
  1086. value = -1
  1087. });
  1088. _devicesIdDatas_BuYuan.Add($"M5温度", new DevicesId_Data()
  1089. {
  1090. ChannelID = 13,
  1091. DeviceID = 5,
  1092. name = $"M5温度",
  1093. showName = "",
  1094. value = -1
  1095. });
  1096. _devicesIdDatas_BuYuan.Add($"M6温度", new DevicesId_Data()
  1097. {
  1098. ChannelID = 14,
  1099. DeviceID = 5,
  1100. name = $"M6温度",
  1101. showName = "",
  1102. value = -1
  1103. });
  1104. _devicesIdDatas_BuYuan.Add($"M7温度", new DevicesId_Data()
  1105. {
  1106. ChannelID = 15,
  1107. DeviceID = 5,
  1108. name = $"M7温度",
  1109. showName = "",
  1110. value = -1
  1111. });
  1112. _devicesIdDatas_BuYuan.Add($"M8温度", new DevicesId_Data()
  1113. {
  1114. ChannelID = 16,
  1115. DeviceID = 5,
  1116. name = $"M8温度",
  1117. showName = "",
  1118. value = -1
  1119. });
  1120. #endregion
  1121. }
  1122. }