|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- 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
- {
- /// <summary>
- /// 时间类型转成时间戳
- /// </summary>
- /// <param name="time">Local Time</param>
- /// <returns></returns>
- 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;
- }
-
- /// <summary>
- /// 时间类型转成时间戳秒
- /// </summary>
- /// <param name="time">Local Time</param>
- /// <returns></returns>
- 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;
- }
-
- /// <summary>
- /// 时间戳转成时间类型
- /// </summary>
- /// <param name="timeStamp"></param>
- /// <returns></returns>
- public static DateTime ConvertToLocalDateTime(long timeStamp)
- {
- DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local);
- return dtStart.AddMilliseconds(timeStamp);
- }
-
- /// <summary>
- /// 时间戳转成时间类型
- /// </summary>
- /// <param name="timeStamp"></param>
- /// <returns></returns>
- 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));
- }
-
- /// <summary>
- /// 时间戳转成时间类型
- /// </summary>
- /// <param name="timeStamp"></param>
- /// <returns></returns>
- public static DateTime ConvertToUtcDateTime(long timeStamp)
- {
- DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
- return dtStart.AddMilliseconds(timeStamp);
- }
-
- /// <summary>
- /// 根据输入的4位数生成随机数
- /// </summary>
- /// <param name="nums">制定位数</param>
- /// <returns></returns>
- 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();
- }
-
- /// <summary>
- /// 生成post表单字符串
- /// </summary>
- /// <param name="dic"></param>
- /// <returns></returns>
- public static string createFromPostStr(Dictionary<string, Object> 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);
- }
-
- /// <summary>
- /// 通过步数计算距离
- /// </summary>
- /// <param name="steps"></param>
- /// <returns></returns>
- 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;
- }
-
- /// <summary>
- /// 获取本地Ip地址
- /// </summary>
- /// <returns></returns>
- 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);
- }
- }
- }
|