HttpHelper.cs 40 KB

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