Commit d1eb223d authored by 潘栩锋's avatar 潘栩锋 🚴

添加 RangeStruct.cs,这个 Range 的 struct 版本。 BorderSearch 的 Valid, Border 都是 RangeStruct

parent 0458a2a4
......@@ -87,6 +87,7 @@
<Compile Include="IsErrorAttribute.cs" />
<Compile Include="PropertiesManager.cs" />
<Compile Include="PropertyBinding.cs" />
<Compile Include="RangeStruct.cs" />
<Compile Include="Range.cs" />
<Compile Include="RangeF.cs" />
<Compile Include="RList.cs" />
......
......@@ -17,12 +17,12 @@ namespace Misc
/// <summary>
/// 开始
/// </summary>
public int Begin { get; set; }
public int Begin { get; set; } = Misc.MyBase.NULL_VALUE;
/// <summary>
/// 结束
/// </summary>
public int End { get; set; }
public int End { get; set; } = Misc.MyBase.NULL_VALUE;
#endregion
#region just get property
......@@ -77,7 +77,19 @@ namespace Misc
Misc.SaveToXmlHepler.Regist(typeof(Range));
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public RangeStruct ToStruct()
{
return new RangeStruct(Begin, End);
}
public Range(RangeStruct rangeStruct) {
Begin = rangeStruct.Begin;
End = rangeStruct.End;
}
/// <summary>
/// 使用无效值初始化
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Newtonsoft.Json;
using System.Collections;
namespace Misc
{
/// <summary>
/// int范围类,
/// </summary>
public struct RangeStruct
{
#region property
/// <summary>
/// 开始
/// </summary>
public int Begin { get; set; }
/// <summary>
/// 结束
/// </summary>
public int End { get; set; }
#endregion
#region just get property
/// <summary>
/// 本范围 中心位置
/// </summary>
[JsonIgnore]
public int Mid
{
get
{
if (Misc.MyBase.ISVALIDATA(Begin))
return (End + Begin) / 2;
else
return Misc.MyBase.NULL_VALUE;
}
}
/// <summary>
/// 本范围宽度
/// </summary>
[JsonIgnore]
public int Width
{
get
{
if (IsValid)
return End - Begin + 1;
else
return Misc.MyBase.NULL_VALUE;
}
}
/// <summary>
/// 是否有效
/// </summary>
[JsonIgnore]
public bool IsValid
{
get
{
if (Misc.MyBase.ISVALIDATA(Begin) && Misc.MyBase.ISVALIDATA(End))
return true;
else
return false;
}
}
#endregion
public RangeStruct(int begin, int end) {
Begin = begin;
End = end;
}
#region methods
/// <summary>
/// 范围 r 在 本范围内
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public bool Contain(RangeStruct r)
{
if ((Begin <= r.Begin) && (r.End <= End))
return true;
else
return false;
}
/// <summary>
/// 点 p 在 本范围内
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public bool Contain(int p)
{
if ((Begin <= p) && (p <= End))
return true;
else
return false;
}
/// <summary>
/// 无效值
/// </summary>
public static RangeStruct InvalidValue
{
get {
return new RangeStruct(Misc.MyBase.NULL_VALUE, Misc.MyBase.NULL_VALUE);
}
}
/// <summary>
/// 范围并集
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static RangeStruct operator |(RangeStruct a, RangeStruct b)
{
return new RangeStruct()
{
Begin = (a.Begin < b.Begin) ? a.Begin : b.Begin,
End = (a.End > b.End) ? a.End : b.End
};
}
/// <summary>
/// 范围交集
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static RangeStruct operator &(RangeStruct a, RangeStruct b)
{
RangeStruct r = new RangeStruct()
{
Begin = (a.Begin > b.Begin) ? a.Begin : b.Begin,
End = (a.End < b.End) ? a.End : b.End
};
if (r.End < r.Begin)
{
return new RangeStruct();
}
else
{
return r;
}
}
/// <summary>
/// 范围 a小于b
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool operator <(RangeStruct a, RangeStruct b)
{
if (a.End < b.Begin)//[a.b,a.e] [b.b,b.e]
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 范围 a大于b
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool operator >(RangeStruct a, RangeStruct b)
{
if (b.End < (a.Begin))// [b.b,b.e][a.b,a.e]
{
return true;
}
else
{
return false;
}
}
public static bool operator <(int p, RangeStruct r)
{
if (p < r.Begin)
{
return true;
}
else
{
return false;
}
}
public static bool operator >(int p, RangeStruct r)
{
if (p > r.End)
{
return true;
}
else
{
return false;
}
}
public static bool operator <(RangeStruct r, int p)
{
if (r.End < p)
{
return true;
}
else
{
return false;
}
}
public static bool operator >(RangeStruct r, int p)
{
if (r.Begin > p)
{
return true;
}
else
{
return false;
}
}
public static RangeStruct operator +(RangeStruct a, int b)
{
return new RangeStruct()
{
Begin = a.Begin + b,
End = a.Begin + b
};
}
public static RangeStruct operator -(RangeStruct a, int b)
{
return new RangeStruct()
{
Begin = a.Begin - b,
End = a.Begin - b
};
}
/// <summary>
/// 是否有交集
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public bool HasIntersection(RangeStruct r)
{
if (Contain(r.Begin))
return true;
if (Contain(r.End))
return true;
if (r.Contain(Begin))
return true;
if (r.Contain(End))
return true;
return false;
}
/// <summary>
/// 可以合并
/// </summary>
/// <returns></returns>
public bool CanUnion(RangeStruct r)
{
if (HasIntersection(r))
return true;
if (End == (r.Begin - 1))
return true;
if (Begin == (r.End + 1))
return true;
return false;
}
#endregion
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (IsValid)
return $"[{Begin},{End}]={Width}";
else
return "invalid";
}
}
/// <summary>
/// Range 辅助类
/// </summary>
public static class EListRangeStruct
{
/// <summary>
/// rlist 必须是由小到大排列
/// </summary>
/// <param name="rlist"></param>
/// <param name="r"></param>
public static void Union(this List<RangeStruct> rlist, RangeStruct r)
{
for (int i = 0; i < rlist.Count(); i++)
{
RangeStruct r_dest = rlist[i];
if (r < r_dest)//在 r_dest 前面
{
rlist.Insert(i, r);
return;//完成
}
else if (r.CanUnion(r_dest)) //r 与 r_dest 有交集
{
//r 与 r_dest 合体
r = r | r_dest;
rlist.RemoveAt(i);
i--;
//还需要继续判断
}
}
//r 在 rlist 的后面
rlist.Add(r);
}
/// <summary>
/// 联合
/// </summary>
/// <param name="rlist1"></param>
/// <param name="rlist2"></param>
public static void Union(this List<RangeStruct> rlist1, List<RangeStruct> rlist2)
{
for (int i = 0; i < rlist2.Count(); i++)
{
rlist1.Union(rlist2[i]);
}
}
}
}
......@@ -272,7 +272,10 @@ namespace FLY.Thick.Base.UI
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.Enable), this, nameof(Enable));
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.IsBreakDetect),this, nameof(IsBreakDetect));
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.Valid),this, nameof(Valid));
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.Valid), ()=> {
Valid = new Range(BorderSearchService.Valid);
});
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.TempADBySet),this, nameof(TempADBySet));
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.TempAD),this, nameof(TempAD));
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.TempRange),this, nameof(TempRange));
......@@ -345,7 +348,7 @@ namespace FLY.Thick.Base.UI
BorderSearchService.Enable = Enable;
BorderSearchService.IsBreakDetect = IsBreakDetect;
BorderSearchService.Valid = Valid;
BorderSearchService.Valid = Valid.ToStruct();
BorderSearchService.TempADBySet = TempADBySet;
BorderSearchService.TempAD = TempAD;
BorderSearchService.TempRange = TempRange;
......
......@@ -96,11 +96,10 @@ namespace FLY.Thick.Base.UI
{
update();
}
};
borderSearch.Valid.PropertyChanged += (s, e) =>
else if (e.PropertyName == nameof(borderSearch.Valid))
{
update();
}
};
DataBindAll();
......
......@@ -40,14 +40,14 @@ namespace FLY.Thick.Base.Client
/// </summary>
public bool IsBreakDetect { get; set; } = true;
[PropertyChanged.DoNotCheckEquality]
public Range Valid { get; set; } = new Range();
[PropertyChanged.DoNotCheckEquality]
public RangeStruct Valid { get; set; }
public Range Border_Backw { get; set; } = new Range();
[PropertyChanged.DoNotCheckEquality]
public Range Border_Forw { get; set; } = new Range();
[PropertyChanged.DoNotCheckEquality]
public Range Border { get; set; } = new Range();
public RangeStruct Border { get; set; }
/// <summary>
......
......@@ -25,7 +25,7 @@ namespace FLY.Thick.Base.IService
/// <summary>
/// 有限范围
/// </summary>
Range Valid { get; set; }
RangeStruct Valid { get; set; }
/// <summary>
/// 手动设置温修AD值
......@@ -82,7 +82,7 @@ namespace FLY.Thick.Base.IService
/// <summary>
/// 边界
/// </summary>
Range Border { get; }
RangeStruct Border { get; }
/// <summary>
/// 当前膜宽,测量出来的,单位是 脉冲
/// </summary>
......
......@@ -7,6 +7,9 @@ using Misc;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using FObjBase;
using AutoMapper;
using System.IO;
using Newtonsoft.Json;
namespace FLY.Thick.Base.Server
{
......@@ -14,7 +17,7 @@ namespace FLY.Thick.Base.Server
/// <summary>
/// 边界查找
/// </summary>
public class BorderSearch : IBorderSearchService, INotifyPropertyChanged, Misc.ISaveToXml
public class BorderSearch : IBorderSearchService, INotifyPropertyChanged
{
/// <summary>
/// 报警测试
......@@ -36,82 +39,12 @@ namespace FLY.Thick.Base.Server
/// </summary>
public bool IsBreakDetect { get; set; } = true;
/// <summary>
/// 边界
/// </summary>
protected class BorderWithOk
{
/// <summary>
/// 边界值
/// </summary>
public Range value = new Range();
/// <summary>
/// 开始边界100%可靠
/// </summary>
public bool isBeginOk;
/// <summary>
/// 结束边界100%可靠
/// </summary>
public bool isEndOk;
/// <summary>
/// 真实可靠的边界值
/// </summary>
public Range real = new Range();
public void UpdateReal(int sensorWidth, int expect, int mid, bool isBeginOk, bool isEndOk) {
if (value.IsValid)
{
real.Begin = value.Begin + sensorWidth / 2;
real.End = value.End - sensorWidth / 2;
//修正!!!
if (real.Width < expect)
{
if (isBeginOk && isEndOk)
{
}
else if (isBeginOk)
{
real.End = real.Begin + expect;
}
else if (isEndOk)
{
real.Begin = real.End - expect;
}
else //开始边界与结束边界都是假的
{
//居中处理
real.Begin = mid - expect / 2;
real.End = real.Begin + expect;
}
}
}
}
public void UpdateReal(int sensorWidth, int expect, int mid)
{
UpdateReal(sensorWidth, expect, mid, isBeginOk, isEndOk);
}
public void UpdateReal(int sensorWidth )
{
if (value.IsValid)
{
real.Begin = value.Begin + sensorWidth / 2;
real.End = value.End - sensorWidth / 2;
}
}
}
/// <summary>
/// 正反向边界数据
/// </summary>
protected BorderWithOk[] borders = new BorderWithOk[2] { new BorderWithOk(), new BorderWithOk() };
/// <summary>
/// 有效范围
/// </summary>
[PropertyChanged.DoNotCheckEquality]
public Range Valid { get; set; } = new Range();
public RangeStruct Valid { get; set; } = RangeStruct.InvalidValue;
/// <summary>
/// 边界
/// </summary>
......@@ -124,7 +57,7 @@ namespace FLY.Thick.Base.Server
/// <summary>
/// 膜范围
/// </summary>
public Range Border { get; set; } = new Range();
public RangeStruct Border { get; set; } = RangeStruct.InvalidValue;
/// <summary>
/// 当前膜宽,测量出来的,单位是 脉冲
......@@ -204,6 +137,79 @@ namespace FLY.Thick.Base.Server
#endregion
/// <summary>
/// 边界
/// </summary>
protected class BorderWithOk
{
/// <summary>
/// 边界值
/// </summary>
public Range value = new Range();
/// <summary>
/// 开始边界100%可靠
/// </summary>
public bool isBeginOk;
/// <summary>
/// 结束边界100%可靠
/// </summary>
public bool isEndOk;
/// <summary>
/// 真实可靠的边界值
/// </summary>
public Range real = new Range();
public void UpdateReal(int sensorWidth, int expect, int mid, bool isBeginOk, bool isEndOk)
{
if (value.IsValid)
{
real.Begin = value.Begin + sensorWidth / 2;
real.End = value.End - sensorWidth / 2;
//修正!!!
if (real.Width < expect)
{
if (isBeginOk && isEndOk)
{
}
else if (isBeginOk)
{
real.End = real.Begin + expect;
}
else if (isEndOk)
{
real.Begin = real.End - expect;
}
else //开始边界与结束边界都是假的
{
//居中处理
real.Begin = mid - expect / 2;
real.End = real.Begin + expect;
}
}
}
}
public void UpdateReal(int sensorWidth, int expect, int mid)
{
UpdateReal(sensorWidth, expect, mid, isBeginOk, isEndOk);
}
public void UpdateReal(int sensorWidth)
{
if (value.IsValid)
{
real.Begin = value.Begin + sensorWidth / 2;
real.End = value.End - sensorWidth / 2;
}
}
}
/// <summary>
/// 正反向边界数据
/// </summary>
protected BorderWithOk[] borders = new BorderWithOk[2] { new BorderWithOk(), new BorderWithOk() };
OBJComponents.Server.WarningSystem warningSystem;
......@@ -212,7 +218,7 @@ namespace FLY.Thick.Base.Server
/// </summary>
protected BorderSearchGetViewReponse getViewReponse;
private string param_path = "bordersearch.xml";
private string param_path = "bordersearch.json";
public BorderSearch()
{
Default();
......@@ -234,10 +240,10 @@ namespace FLY.Thick.Base.Server
this.Border_Backw.PropertyChanged += (s, e) => {
NotifyPropertyChanged(nameof(Border_Backw));
};
this.Border.PropertyChanged += (s, e) => {
NotifyPropertyChanged(nameof(Border));
};
Load();
if (!Load()) {
Save();
}
}
public void Init(OBJComponents.Server.WarningSystem warningSystem)
......@@ -247,24 +253,17 @@ namespace FLY.Thick.Base.Server
void Default()
{
Enable = false;
Valid = new Range() { Begin = 1200, End = 7400 };
Init();
Valid = new RangeStruct(1200,7400);
TempAD = -1;
N = 20;
N2 = 400;
N3 = 1000;
SensorWidth = 250;
Width = 0;
Border_Backw.Reset();
Border_Forw.Reset();
Border.Reset();
Mid = Valid.Mid;
TempRange = 500;
TempRangePercent = 500.0 / 50000;
IsTempRangeByPercent = true;
UpdateTime = DateTime.MinValue;
Reset();
}
......@@ -294,12 +293,12 @@ namespace FLY.Thick.Base.Server
}
public void Init()
public void Reset()
{
Border_Backw.Reset();
Border_Forw.Reset();
Width = 0;
Border.Reset();
Border = RangeStruct.InvalidValue;
Mid = Valid.Mid;
}
......@@ -663,16 +662,15 @@ namespace FLY.Thick.Base.Server
foreach(var bo in borders)
bo.UpdateReal(SensorWidth, ProductWidth, Valid.Mid,
borders.All(b=>b.isBeginOk), borders.All(b => b.isEndOk));
Border.Begin = (int)borders.Average(b => b.real.Begin);
Border.End = (int)borders.Average(b => b.real.End);
Border = new RangeStruct(
(int)borders.Average(b => b.real.Begin),
(int)borders.Average(b => b.real.End));
}
else if (border.value.IsValid)
{
foreach (var bo in borders)
bo.UpdateReal(SensorWidth, ProductWidth, Valid.Mid);
Border.Copy(border.real);
Border = border.real.ToStruct();
}
getViewReponse.border = border.real;
......@@ -769,11 +767,11 @@ namespace FLY.Thick.Base.Server
public bool Load()
{
return Misc.SaveToXmlHepler.Load(param_path, this);
return BorderSearchJsonDb.Load(this, param_path);
}
public bool Save()
{
return Misc.SaveToXmlHepler.Save(param_path, this);
return BorderSearchJsonDb.Save(this, param_path);
}
public void Apply()
......@@ -793,26 +791,61 @@ namespace FLY.Thick.Base.Server
#endregion
#region ISaveToXml 成员
public string[] GetSavePropertyNames()
{
return new string[]{
"Enable",
"Valid",
"TempADBySet",
"TempAD",
"TempRange",
"TempRangePercent",
"IsTempRangeByPercent",
"N",
"SensorWidth",
"N2",
"N3",
"IsBreakDetect"
};
}
#endregion
public class BorderSearchJsonDb
{
static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c =>
{
c.CreateMap<BorderSearch, BorderSearchJsonDb>().ReverseMap();
}));
public static bool Load(BorderSearch src, string filePath)
{
try
{
if (File.Exists(filePath))
{
string json = File.ReadAllText(filePath);
var p = JsonConvert.DeserializeObject<BorderSearchJsonDb>(json);
Mapper.Map(p, src);
return true;
}
}
catch
{
//异常,没有json 解码失败
}
return false;
}
public static bool Save(BorderSearch src, string filePath)
{
var p = Mapper.Map<BorderSearchJsonDb>(src);
try
{
File.WriteAllText(filePath, JsonConvert.SerializeObject(p, Formatting.Indented));
return true;
}
catch
{
//异常,没有json 编码失败
}
return false;
}
public bool Enable;
public bool IsBreakDetect = true;
public RangeStruct Valid = new RangeStruct(200,7000);
public bool TempADBySet;
public int TempAD = 50000;
public int TempRange = 500;
public bool IsTempRangeByPercent = true;
public double TempRangePercent = 0.02;
public int N=20;
public int SensorWidth=250;
public int N2=400;
public int N3=1000;
}
}
......@@ -141,12 +141,11 @@ namespace FLY.Thick.Base.Server
if (borders.All(b => b.value.IsValid))
{
Border.Begin = (int)borders.Average(b => b.real.Begin);
Border.End = (int)borders.Average(b => b.real.End);
Border = new RangeStruct((int)borders.Average(b => b.real.Begin), (int)borders.Average(b => b.real.End));
}
else if (border.value.IsValid)
{
Border.Copy(border.real);
Border = border.real.ToStruct();
}
getViewReponse.border = border.real;
......
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