Util.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.Text.Json.Nodes;
  2. using KYFramework;
  3. using KYFramework.Network;
  4. using Model;
  5. using SimulationCommon;
  6. using SimulationSingleServer.Utils;
  7. namespace SimulationServer.Utils;
  8. public class Util
  9. {
  10. public static double GetDistance(double lon1, double lon2, double lat1, double lat2)
  11. {
  12. double R = 6371; // 地球的半径(公里)
  13. double dLat = (lat2 - lat1) * Math.PI / 180.0;
  14. double dLon = (lon2 - lon1) * Math.PI / 180.0;
  15. double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
  16. Math.Cos(lat1 * Math.PI / 180.0) * Math.Cos(lat2 * Math.PI / 180.0) *
  17. Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
  18. double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
  19. double distance = R * c;
  20. return distance;
  21. }
  22. /// <summary>
  23. /// 根据 shape 解析出 经纬度
  24. /// </summary>
  25. /// <param name="shape"></param>
  26. /// <returns></returns>
  27. public static double[] Checkout(string shape)
  28. {
  29. string s = shape.Replace("POINT(", "").Replace(")", "");
  30. string[] tmps = s.Split(" ");
  31. double[] result = new double[tmps.Length];
  32. for (int i = 0; i < tmps.Length; i++)
  33. {
  34. result[i] = double.Parse(tmps[i]);
  35. }
  36. return result;
  37. }
  38. public static AircraftDB GetAircraftDefine(string aircraftType, string aircraftSubType, string aircraftID)
  39. {
  40. string content = HttpManager.Get(HttpInterface.aircraft ,new List<string>
  41. {
  42. "fType","fSubtype","fName","limit"
  43. }, new List<string>
  44. {
  45. aircraftType,aircraftSubType,aircraftID,"1"
  46. });
  47. //Log.Debug($"Type = {aircraftType}, SubType = {aircraftSubType}, ID = {aircraftID}");
  48. var define = JsonHelper.FromJson<AircraftDefine>(content);
  49. if (define.zs.Count == 0)
  50. {
  51. //Log.Debug($"没有找到对应 [{aircraftID}] 的飞机信息");
  52. return new AircraftDB();
  53. }
  54. return define.zs[0];
  55. }
  56. public static Weather GetWeather(string province_name, string city_name, string data)
  57. {
  58. string content = HttpManager.Get(HttpInterface.weather ,new List<string>
  59. {
  60. "province_name","city_name","weather_date"
  61. }, new List<string>
  62. {
  63. province_name,city_name,data
  64. });
  65. var weather = JsonHelper.FromJson<WeatherDB>(content);
  66. var result = JsonHelper.FromJson<WeatherData>(weather.data).result;
  67. return result;
  68. }
  69. public static double GetSlope(double lon, double lat, int gridCount)
  70. {
  71. string content = HttpManager.Get(HttpInterface.slope ,new List<string>
  72. {
  73. "lon","lat","gridCount"
  74. }, new List<string>
  75. {
  76. lon.ToString(),lat.ToString(),gridCount.ToString()
  77. });
  78. var slope = JsonHelper.FromJson<SlopeDb>(content);
  79. return Math.Atan(Math.Abs(slope.average.s1)) * (180.0 / Math.PI);
  80. }
  81. /// <summary>
  82. /// 获取当前位置的温度
  83. /// </summary>
  84. /// <param name="varName"></param>
  85. /// <param name="lon"></param>
  86. /// <param name="lat"></param>
  87. /// <param name="data"></param>
  88. /// <returns></returns>
  89. public static double GetTemperature(double lon, double lat)
  90. {
  91. string content = HttpManager.Get(HttpInterface.temperature ,new List<string>
  92. {
  93. "varName","centerLon","centerLat","weather_date"
  94. }, new List<string>
  95. {
  96. "t2m",lon.ToString(),lat.ToString(),"2024-01-01 12:00:00"
  97. });
  98. var temp = JsonHelper.FromJson<Temperature>(content);
  99. return temp.vale - 273;
  100. }
  101. //
  102. public static Fuel GetFuel(string fName, string fType, string fState, string fTemperature, string fHeight, string fWeight)
  103. {
  104. string content = HttpManager.Get(HttpInterface.fuel ,new List<string>
  105. {
  106. "fname","ftype","fstate","ftemperature","fheight","fweight"
  107. }, new List<string>
  108. {
  109. fName,fType,fState,fTemperature,fHeight,fWeight
  110. });
  111. var fuel = JsonHelper.FromJson<FuelDB>(content);
  112. return fuel.data[0];
  113. }
  114. public static EntitySheetReportValue GetSheetReportValue(string name,Dictionary<string, Dictionary<string, List<string>>> report)
  115. {
  116. EntitySheetReportValue aircraftValue = new EntitySheetReportValue();
  117. aircraftValue.Name = name;
  118. //每架飞机的所有sheet
  119. foreach (var kv1 in report)
  120. {
  121. SheetReportValue sheetValue = new SheetReportValue();
  122. sheetValue.Name = kv1.Key;
  123. aircraftValue.SheetReportValueArr.Add(sheetValue);
  124. //每个sheet的所有数据
  125. foreach (var kv2 in kv1.Value)
  126. {
  127. ReportValue reportValue = new ReportValue();
  128. reportValue.Name = kv2.Key;
  129. reportValue.Value = kv2.Value.First();
  130. sheetValue.ValueArr.Add(reportValue);
  131. }
  132. }
  133. return aircraftValue;
  134. }
  135. }