Commit 96e1113f authored by 潘栩锋's avatar 潘栩锋 🚴

优化 TimeGridAdvHelper 的第1个 AD包,也计算它的 时间间隔(不是用1.28ms)

parent 7a078aec
...@@ -41,7 +41,7 @@ namespace FlyADBase ...@@ -41,7 +41,7 @@ namespace FlyADBase
/// <summary> /// <summary>
/// AD数据池时间间隔为1.28ms, 肯定最少有一个数据 /// AD数据池时间间隔为1.28ms, 肯定最少有一个数据
/// 每个数据包(200个数据)理论间隔是200*1.28ms, 但实际上不是。 /// 每个数据包(200个数据)理论间隔是200*1.28ms, 但实际上不是。
/// 数据输出时,输出的每个数据必须是1.28ms, 需要通过数据包的systick线性填充数据 /// 数据输出时,输出的每个数据必须是1ms, 需要通过数据包的systick线性填充数据
/// </summary> /// </summary>
public List<DateTimeUnit3> ADPool = new List<DateTimeUnit3>(); public List<DateTimeUnit3> ADPool = new List<DateTimeUnit3>();
...@@ -474,6 +474,7 @@ namespace FlyADBase ...@@ -474,6 +474,7 @@ namespace FlyADBase
/// </summary> /// </summary>
public int[] datas; public int[] datas;
public override string ToString() public override string ToString()
{ {
return $"{dt.Ticks} | cnt={datas.Count()}"; return $"{dt.Ticks} | cnt={datas.Count()}";
...@@ -500,37 +501,54 @@ namespace FlyADBase ...@@ -500,37 +501,54 @@ namespace FlyADBase
public static class TimeGridAdvHelperExt public static class TimeGridAdvHelperExt
{ {
/// <summary> /// <summary>
/// 每个ad数据的时间间隔 1.28ms /// 每个ad数据的时间间隔 1ms//1.28ms
/// </summary> /// </summary>
public const double ad_ts_ms = 1.28; public const double ad_ts_ms = 1.28;
public static TimeSpan ad_ts => TimeSpan.FromTicks((long)TimeGridAdvHelperExt.ad_ts_ms * TimeSpan.TicksPerMillisecond); public static TimeSpan ad_ts => TimeSpan.FromTicks((long)(ad_ts_ms * TimeSpan.TicksPerMillisecond));
public static List<int> GetAD(List<DateTimeUnit3> adPool, DateTime begin, DateTime end, out DateTime reponse_endTime) public static List<int> GetAD(List<DateTimeUnit3> adPool, DateTime begin, DateTime end, out DateTime reponse_endTime)
{ {
return GetAD(adPool, ad_ts_ms, begin, end, out reponse_endTime); reponse_endTime = DateTime.MinValue;
var reponse2 = GetAD2(adPool, begin, end);
if (reponse2.Count() == 0)
return new List<int>();
DateTime lastTime = reponse2.Last().dt;
reponse_endTime = lastTime;
return GetAD(reponse2, begin);
} }
public static List<int> GetAD(List<DateTimeUnit3> adPool) public static List<int> GetAD(List<DateTimeUnit3> adPool)
{ {
//通过adPool[0].dt 与 adPool[1].dt 的时间差,计算adPool[0].dt的开始时间
//如果没有adPool[1].dt,就当 adPool[0].dt 的开始时间为 adPool[0].dt - adPool[0].datas.Count() * ad_ts_ms;
DateTime begin;
if (adPool.Count() >= 2)
{
begin = adPool[0].dt - (adPool[1].dt - adPool[0].dt);
}
else {
double t_ms = adPool.First().datas.Count() * ad_ts_ms; double t_ms = adPool.First().datas.Count() * ad_ts_ms;
begin = adPool.First().dt - TimeSpan.FromTicks((long)(t_ms * TimeSpan.TicksPerMillisecond));
DateTime begin = adPool.First().dt - TimeSpan.FromTicks((long)(t_ms * TimeSpan.TicksPerMillisecond)); }
return GetAD(adPool, begin); return GetAD(adPool, begin);
} }
public static List<int> GetAD(List<DateTimeUnit3> adPool, DateTime begin) public static List<int> GetAD(List<DateTimeUnit3> adPool, DateTime begin)
{ {
var ts = TimeSpan.FromTicks((long)(ad_ts_ms * TimeSpan.TicksPerMillisecond));
List<int> reponse = new List<int>(); List<int> reponse = new List<int>();
DateTime lastTime = adPool.Last().dt; DateTime lastTime = adPool.Last().dt;
//每个ad包的每个数据不是准确的1.28ms,肯定比1.28ms大,每次需要准确计算 //每个ad包的每个数据不是准确的1.28ms,肯定比1.28ms大,每次需要准确计算
for (int i = 0; i < adPool.Count(); i++) for (int i = 0; i < adPool.Count(); i++)
{ {
int idx = adPool.Count() - 1 - i; int idx = adPool.Count() - 1 - i;
double curr_ad_ts_ms = ad_ts_ms; double curr_ad_ts_ms;
//计算这个包每个数据的时间间隔 //计算这个包每个数据的时间间隔
DateTime end_dt = adPool[idx].dt; DateTime end_dt = adPool[idx].dt;
...@@ -540,10 +558,21 @@ namespace FlyADBase ...@@ -540,10 +558,21 @@ namespace FlyADBase
double total_ms = (end_dt - begin_dt).Ticks / TimeSpan.TicksPerMillisecond; double total_ms = (end_dt - begin_dt).Ticks / TimeSpan.TicksPerMillisecond;
curr_ad_ts_ms = total_ms / adPool[idx].datas.Count(); curr_ad_ts_ms = total_ms / adPool[idx].datas.Count();
} }
else if (adPool.Count() > 1)
{
var begin_dt = adPool[idx].dt - (adPool[idx + 1].dt - adPool[idx].dt);
double total_ms = (end_dt - begin_dt).Ticks / TimeSpan.TicksPerMillisecond;
curr_ad_ts_ms = total_ms / adPool[idx].datas.Count();
}
else
{
curr_ad_ts_ms = ad_ts_ms;
}
double act_ms = 0; double act_ms = 0;
double ideal_ms = 0; double ideal_ms = 0;//插值,让输出可以有准确的间隔为 ad_ts_ms
for (int j = 0; j < adPool[idx].datas.Count(); j++) for (int j = 0; j < adPool[idx].datas.Count(); j++)
{ {
int idx2 = adPool[idx].datas.Count() - 1 - j; int idx2 = adPool[idx].datas.Count() - 1 - j;
...@@ -554,7 +583,7 @@ namespace FlyADBase ...@@ -554,7 +583,7 @@ namespace FlyADBase
{ {
reponse.Add(ad); reponse.Add(ad);
ideal_ms += ad_ts_ms; ideal_ms += ad_ts_ms;
lastTime -= ts; lastTime -= ad_ts;
} }
if (lastTime < begin) if (lastTime < begin)
goto _end;//完成 goto _end;//完成
...@@ -567,29 +596,7 @@ namespace FlyADBase ...@@ -567,29 +596,7 @@ namespace FlyADBase
} }
/// <summary>
/// adPool 每个包内,数据间隔不为1.28ms,肯定比1.28ms大;
/// 输出的数据列,必须是 间隔为1.28ms
/// </summary>
/// <param name="adPool"></param>
/// <param name="ad_ts_ms"></param>
/// <param name="begin">开始时间</param>
/// <param name="end">结束时间</param>
/// <param name="reponse_endTime">返回数据的最后一个数据的时间,数据间隔为1.28ms</param>
/// <returns></returns>
static List<int> GetAD(List<DateTimeUnit3> adPool, double ad_ts_ms, DateTime begin, DateTime end, out DateTime reponse_endTime)
{
reponse_endTime = DateTime.MinValue;
var reponse2 = GetAD2(adPool, begin, end);
if (reponse2.Count() == 0)
return new List<int>();
DateTime lastTime = reponse2.Last().dt;
reponse_endTime = lastTime;
return GetAD(reponse2, begin);
}
...@@ -631,22 +638,18 @@ namespace FlyADBase ...@@ -631,22 +638,18 @@ namespace FlyADBase
int begin_pos = GetPos(posPool, begin, ref searchIdx); int begin_pos = GetPos(posPool, begin, ref searchIdx);
return (end_pos - begin_pos) / (end - begin).TotalMinutes; return (end_pos - begin_pos) / (end - begin).TotalMinutes;
} }
public static List<int> GetPos(List<DateTimeUnit> posPool, DateTime end_dt, int cnt) {
return GetPos(posPool, ad_ts_ms, end_dt, cnt);
}
/// <summary> /// <summary>
/// 输出以 ad_ts_ms 为间隔脉冲列表 /// 输出以 ad_ts_ms 为间隔脉冲列表
/// </summary> /// </summary>
/// <param name="posPool"></param> /// <param name="posPool"></param>
/// <param name="ad_ts_ms"></param>
/// <param name="end_dt"></param> /// <param name="end_dt"></param>
/// <param name="cnt"></param> /// <param name="cnt"></param>
/// <returns></returns> /// <returns></returns>
static List<int> GetPos(List<DateTimeUnit> posPool, double ad_ts_ms, DateTime end_dt, int cnt) public static List<int> GetPos(List<DateTimeUnit> posPool, DateTime end_dt, int cnt)
{ {
//从后面开始找
int searchIdx = posPool.Count() - 1; int searchIdx = posPool.Count() - 1;
long ticks = (long)(ad_ts_ms * TimeSpan.TicksPerMillisecond);
var ts = TimeSpan.FromTicks(ticks);
List<int> reponse = new List<int>(); List<int> reponse = new List<int>();
...@@ -654,7 +657,7 @@ namespace FlyADBase ...@@ -654,7 +657,7 @@ namespace FlyADBase
for (int i = 0; i < cnt; i++) for (int i = 0; i < cnt; i++)
{ {
reponse.Add(GetPos(posPool, lastTime, ref searchIdx)); reponse.Add(GetPos(posPool, lastTime, ref searchIdx));
lastTime -= ts; lastTime -= ad_ts;
} }
//反转 //反转
reponse.Reverse(); reponse.Reverse();
...@@ -824,22 +827,15 @@ namespace FlyADBase ...@@ -824,22 +827,15 @@ namespace FlyADBase
public static List<UInt16> GetIStatus(List<DateTimeUnit2> iStatusPool, DateTime end_dt, int cnt) public static List<UInt16> GetIStatus(List<DateTimeUnit2> iStatusPool, DateTime end_dt, int cnt)
{
return GetIStatus(iStatusPool, ad_ts_ms, end_dt, cnt);
}
static List<UInt16> GetIStatus(List<DateTimeUnit2> iStatusPool, double ad_ts_ms, DateTime end_dt, int cnt)
{ {
int searchIdx = iStatusPool.Count() - 1; int searchIdx = iStatusPool.Count() - 1;
long ticks = (long)(ad_ts_ms * TimeSpan.TicksPerMillisecond);
var ts = TimeSpan.FromTicks(ticks);
List<UInt16> reponse = new List<UInt16>(); List<UInt16> reponse = new List<UInt16>();
DateTime lastTime = end_dt; DateTime lastTime = end_dt;
for (int i = 0; i < cnt; i++) for (int i = 0; i < cnt; i++)
{ {
reponse.Add(GetIStatus(iStatusPool, lastTime, ref searchIdx)); reponse.Add(GetIStatus(iStatusPool, lastTime, ref searchIdx));
lastTime -= ts; lastTime -= ad_ts;
} }
//反转 //反转
reponse.Reverse(); reponse.Reverse();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment