You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using GpsCardGatewayPosition.Model.Config;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Security.AccessControl;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace GpsCardGatewayPosition.Common
  9. {
  10. public class Utils
  11. {
  12. /// <summary>
  13. /// 时间类型转成时间戳
  14. /// </summary>
  15. /// <param name="time">Local Time</param>
  16. /// <returns></returns>
  17. public static long ConvertToTimeStamp(DateTime time)
  18. {
  19. long intResult = 0;
  20. DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local);
  21. intResult = (long)(time - startTime).TotalMilliseconds;
  22. return intResult;
  23. }
  24. /// <summary>
  25. /// 时间类型转成时间戳秒
  26. /// </summary>
  27. /// <param name="time">Local Time</param>
  28. /// <returns></returns>
  29. public static int ConvertToSTimeStamp(DateTime time)
  30. {
  31. DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local);
  32. var intResult = (int)(time - startTime).TotalSeconds;
  33. return intResult;
  34. }
  35. /// <summary>
  36. /// 时间戳转成时间类型
  37. /// </summary>
  38. /// <param name="timeStamp"></param>
  39. /// <returns></returns>
  40. public static DateTime ConvertToLocalDateTime(long timeStamp)
  41. {
  42. DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local);
  43. return dtStart.AddMilliseconds(timeStamp);
  44. }
  45. /// <summary>
  46. /// 时间戳转成时间类型
  47. /// </summary>
  48. /// <param name="timeStamp"></param>
  49. /// <returns></returns>
  50. public static DateTime ConvertToLocalDateTime(string timeStamp)
  51. {
  52. DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local);
  53. return dtStart.AddMilliseconds(SafeType.SafeInt64(timeStamp));
  54. }
  55. /// <summary>
  56. /// 时间戳转成时间类型
  57. /// </summary>
  58. /// <param name="timeStamp"></param>
  59. /// <returns></returns>
  60. public static DateTime ConvertToUtcDateTime(long timeStamp)
  61. {
  62. DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
  63. return dtStart.AddMilliseconds(timeStamp);
  64. }
  65. /// <summary>
  66. /// 根据输入的4位数生成随机数
  67. /// </summary>
  68. /// <param name="nums">制定位数</param>
  69. /// <returns></returns>
  70. public static string createRandomNum(int nums)
  71. {
  72. StringBuilder newRandom = new StringBuilder();
  73. Random rd = new Random();
  74. for (int i = 0; i < nums; i++)
  75. {
  76. newRandom.Append(rd.Next(0, 9));
  77. }
  78. return newRandom.ToString();
  79. }
  80. /// <summary>
  81. /// 生成post表单字符串
  82. /// </summary>
  83. /// <param name="dic"></param>
  84. /// <returns></returns>
  85. public static string createFromPostStr(Dictionary<string, Object> dic)
  86. {
  87. StringBuilder sb = new StringBuilder();
  88. foreach (var param in dic)
  89. {
  90. sb.Append(param.Key);
  91. sb.Append("=");
  92. sb.Append(param.Value.ToString());
  93. sb.Append("&");
  94. }
  95. //去掉最後一個&字符
  96. string result = sb.ToString();
  97. return result.Substring(0, result.Length - 1);
  98. }
  99. /// <summary>
  100. /// 通过步数计算距离
  101. /// </summary>
  102. /// <param name="steps"></param>
  103. /// <returns></returns>
  104. public static double GetMovingDistance(string steps, WifiParamConfig configWifiParam)
  105. {
  106. try
  107. {
  108. var eachStep = steps?.Split(',');
  109. if (eachStep != null && eachStep.Length > 1)
  110. {
  111. int lastIndex = eachStep.Length - 1;
  112. int startStep = Convert.ToInt32(eachStep[0]);
  113. int endStep = Convert.ToInt32(eachStep[lastIndex]);
  114. var distance = (endStep - startStep) * configWifiParam.StepParam; //double.Parse(ConfigHelper.Configuration["WifiParam:StepParam"].ToString());
  115. return distance;
  116. }
  117. else
  118. {
  119. return 0;
  120. }
  121. }
  122. catch (Exception)
  123. {
  124. //LogHelper.Logger.LogError($"GetMovingDistance[异常] {ex.Message}");
  125. }
  126. return 0;
  127. }
  128. /// <summary>
  129. /// 获取本地Ip地址
  130. /// </summary>
  131. /// <returns></returns>
  132. public static string GetLocalIp()
  133. {
  134. try
  135. {
  136. var addressList = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList;
  137. var ips = addressList.Where(address => address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  138. .Select(address => address.ToString()).ToArray();
  139. if (ips.Length == 1)
  140. {
  141. return ips.First();
  142. }
  143. return ips.Where(address => !address.EndsWith(".1")).FirstOrDefault() ?? ips.FirstOrDefault();
  144. }
  145. catch (Exception)
  146. {
  147. return "UNKNOWN";
  148. }
  149. }
  150. public static long GetTimeDeviationMilliseconds(long timestamp)
  151. {
  152. var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  153. return Math.Abs(now - timestamp);
  154. }
  155. public static long GetTimeDeviationMilliseconds(string time)
  156. {
  157. var timestamp = DateTimeOffset.Parse(time).ToUnixTimeMilliseconds();
  158. var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  159. return Math.Abs(now - timestamp);
  160. }
  161. }
  162. }