using GpsCardGatewayPosition.Model.Config; using System; using System.Collections.Generic; using System.Linq; using System.Security.AccessControl; using System.Text; using System.Threading.Tasks; namespace GpsCardGatewayPosition.Common { public class Utils { /// /// 时间类型转成时间戳 /// /// Local Time /// public static long ConvertToTimeStamp(DateTime time) { long intResult = 0; DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local); intResult = (long)(time - startTime).TotalMilliseconds; return intResult; } /// /// 时间类型转成时间戳秒 /// /// Local Time /// public static int ConvertToSTimeStamp(DateTime time) { DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local); var intResult = (int)(time - startTime).TotalSeconds; return intResult; } /// /// 时间戳转成时间类型 /// /// /// public static DateTime ConvertToLocalDateTime(long timeStamp) { DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local); return dtStart.AddMilliseconds(timeStamp); } /// /// 时间戳转成时间类型 /// /// /// public static DateTime ConvertToLocalDateTime(string timeStamp) { DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local); return dtStart.AddMilliseconds(SafeType.SafeInt64(timeStamp)); } /// /// 时间戳转成时间类型 /// /// /// public static DateTime ConvertToUtcDateTime(long timeStamp) { DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local); return dtStart.AddMilliseconds(timeStamp); } /// /// 根据输入的4位数生成随机数 /// /// 制定位数 /// public static string createRandomNum(int nums) { StringBuilder newRandom = new StringBuilder(); Random rd = new Random(); for (int i = 0; i < nums; i++) { newRandom.Append(rd.Next(0, 9)); } return newRandom.ToString(); } /// /// 生成post表单字符串 /// /// /// public static string createFromPostStr(Dictionary dic) { StringBuilder sb = new StringBuilder(); foreach (var param in dic) { sb.Append(param.Key); sb.Append("="); sb.Append(param.Value.ToString()); sb.Append("&"); } //去掉最後一個&字符 string result = sb.ToString(); return result.Substring(0, result.Length - 1); } /// /// 通过步数计算距离 /// /// /// public static double GetMovingDistance(string steps, WifiParamConfig configWifiParam) { try { var eachStep = steps?.Split(','); if (eachStep != null && eachStep.Length > 1) { int lastIndex = eachStep.Length - 1; int startStep = Convert.ToInt32(eachStep[0]); int endStep = Convert.ToInt32(eachStep[lastIndex]); var distance = (endStep - startStep) * configWifiParam.StepParam; //double.Parse(ConfigHelper.Configuration["WifiParam:StepParam"].ToString()); return distance; } else { return 0; } } catch (Exception) { //LogHelper.Logger.LogError($"GetMovingDistance[异常] {ex.Message}"); } return 0; } /// /// 获取本地Ip地址 /// /// public static string GetLocalIp() { try { var addressList = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList; var ips = addressList.Where(address => address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) .Select(address => address.ToString()).ToArray(); if (ips.Length == 1) { return ips.First(); } return ips.Where(address => !address.EndsWith(".1")).FirstOrDefault() ?? ips.FirstOrDefault(); } catch (Exception) { return "UNKNOWN"; } } public static long GetTimeDeviationMilliseconds(long timestamp) { var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); return Math.Abs(now - timestamp); } public static long GetTimeDeviationMilliseconds(string time) { var timestamp = DateTimeOffset.Parse(time).ToUnixTimeMilliseconds(); var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); return Math.Abs(now - timestamp); } } }