PgBlowingExtVm.cs 10.5 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12
using FLY.Thick.Blowing.IService;
using GalaSoft.MvvmLight.Command;
using LiveCharts;
using LiveCharts.Configurations;
using LiveCharts.Wpf;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
潘栩锋's avatar
潘栩锋 committed
13
using System.Windows.Media;
潘栩锋's avatar
潘栩锋 committed
14 15 16 17 18 19 20 21

namespace FLY.Thick.Blowing360.UI
{
    public class PgBlowingExtVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region 曲线
潘栩锋's avatar
潘栩锋 committed
22 23


潘栩锋's avatar
潘栩锋 committed
24 25 26 27 28 29 30 31 32 33
        /// <summary>
        /// 画在图上的 速度曲线,限制最多100个数据
        /// </summary>
        public ChartValues<TimeValue> VelocityValues { get; } = new ChartValues<TimeValue>();
        /// <summary>
        /// 画在图上的 厚度曲线,限制最多100个数据
        /// </summary>
        public ChartValues<TimeValue> ThicknessValues { get; } = new ChartValues<TimeValue>();

        /// <summary>
潘栩锋's avatar
潘栩锋 committed
34
        /// 画在图上的 角度增量曲线,限制最多100个数据
潘栩锋's avatar
潘栩锋 committed
35
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
36
        public ChartValues<TimeValue> AngleValues { get; } = new ChartValues<TimeValue>();
潘栩锋's avatar
潘栩锋 committed
37 38 39 40 41 42


        /// <summary>
        /// 转换为分区号单位的 每幅图
        /// </summary>
        public SeriesCollection FrameSeries { get; } = new SeriesCollection();
潘栩锋's avatar
潘栩锋 committed
43 44
        public List<AxisSection> LimitSections { get; } = new List<AxisSection>();

潘栩锋's avatar
潘栩锋 committed
45 46 47 48 49
        #endregion

        #region 参数

        /// <summary>
潘栩锋's avatar
潘栩锋 committed
50
        /// 需要 旋转角度增量曲线
潘栩锋's avatar
潘栩锋 committed
51
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
52
        public bool IsRPosMode { get; set; }
潘栩锋's avatar
潘栩锋 committed
53
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
54
        /// 膜距离
潘栩锋's avatar
潘栩锋 committed
55
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
56
        public double FilmLength { get; set; }
潘栩锋's avatar
潘栩锋 committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

        /// <summary>
        /// 膜距离查找范围 m
        /// </summary>
        public int FLRange { get; set; } = 5;

        /// <summary>
        /// 速度滤波 s
        /// </summary>
        public int VelocityFilter { get; set; }

        /// <summary>
        /// 牵引1速度
        /// </summary>
        public double Velocity1 { get; set; }
        #endregion

        #region 状态
        public double VAvg { get; private set; }
        public double VMax { get; private set; }
        public double VMin { get; private set; }

        public double FAvg { get; private set; }
        public double FMax { get; private set; }
        public double FMin { get; private set; }

        #endregion
        #region 界面
        /// <summary>
        /// y轴格式
        /// </summary>
        public Func<double, string> YFormatter { get; set; }

        /// <summary>
        /// X轴格式
        /// </summary>
        public Func<double, string> DateTimeFormatter { get; set; }

        /// <summary>
        /// 分区号X轴格式
        /// </summary>
        public Func<double, string> BoltNoFormatter { get; set; }

        public CartesianMapper<TimeValue> MapperTv { get; set; }

        #endregion
        #region Command
潘栩锋's avatar
潘栩锋 committed
104
        public RelayCommand DownloadCmd { get; private set; }
潘栩锋's avatar
潘栩锋 committed
105 106
        public RelayCommand UpdateVCmd { get; private set; }

潘栩锋's avatar
潘栩锋 committed
107

潘栩锋's avatar
潘栩锋 committed
108 109 110 111 112 113 114 115 116 117 118
        public RelayCommand UpdateFramesCmd { get; private set; }

        public RelayCommand CalCmd { get; private set; }

        public RelayCommand ApplyFilmLengthCmd { get; private set; }

        public RelayCommand SaveCmd { get; private set; }

        public RelayCommand LoadCmd { get; private set; }
        #endregion

潘栩锋's avatar
潘栩锋 committed
119
        public CalFilmLen360 Cfl { get; set; }
潘栩锋's avatar
潘栩锋 committed
120 121 122 123 124
        IBlowingFixService blowing;
        IBlowingDetectService rdetect;

        public PgBlowingExtVm()
        {
潘栩锋's avatar
潘栩锋 committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            #region 界面配置
            MapperTv = Mappers.Xy<TimeValue>()
                .X(value => value.Time.Ticks)
                .Y(value => value.Value);

            DateTimeFormatter = value => new DateTime((long)value).ToString("HH:mm:ss");
            BoltNoFormatter = value => (value + 1).ToString();
            YFormatter = value => value.ToString("F1");

            #endregion

            DownloadCmd = new RelayCommand(Download);
            UpdateVCmd = new RelayCommand(UpdateVelocity);
            UpdateFramesCmd = new RelayCommand(UpdateFrames);
            CalCmd = new RelayCommand(Cal);
            ApplyFilmLengthCmd = new RelayCommand(ApplyFilmLength);
            SaveCmd = new RelayCommand(Save);
            LoadCmd = new RelayCommand(Load);
潘栩锋's avatar
潘栩锋 committed
143 144 145 146 147 148 149 150 151 152 153
        }

        public void Init(IBlowingFixService blowing, IBlowingDetectService rdetect)
        {
            this.rdetect = rdetect;
            this.blowing = blowing;

            Cfl = new CalFilmLen360();
            Cfl.Init(blowing, rdetect);


潘栩锋's avatar
潘栩锋 committed
154
            Misc.BindingOperations.SetBinding(Cfl, nameof(Cfl.IsRPosMode), this, nameof(IsRPosMode));
潘栩锋's avatar
潘栩锋 committed
155 156 157 158

            Misc.BindingOperations.SetBinding(Cfl, nameof(Cfl.FilmLength), this, nameof(FilmLength));
            Misc.BindingOperations.SetBinding(Cfl, nameof(Cfl.VelocityFilter), this, nameof(VelocityFilter));
            Misc.BindingOperations.SetBinding(Cfl, nameof(Cfl.Velocity1), this, nameof(Velocity1));
潘栩锋's avatar
潘栩锋 committed
159
            
潘栩锋's avatar
潘栩锋 committed
160 161 162 163 164 165 166 167 168 169

            Cfl.PropertyChanged += CalFilmLen_PropertyChanged;
        }



        private void CalFilmLen_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Cfl.LimitValues))
            {
潘栩锋's avatar
潘栩锋 committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
                var vList = Cfl.LimitValues;

                LimitSections.Clear();
                for (int i = 0; i < vList.Count(); i++) {

                    var value = vList[i]; 
                    var axisSection = new AxisSection()
                    {
                        StrokeThickness = 1,
                        DataLabel = true,
                        DisableAnimations = true,
                        DataLabelForeground = new SolidColorBrush(Colors.White),
                        Opacity = 1,
                        Stroke = new SolidColorBrush(Colors.Orange),
                    };
                    if (value.Value == 1)
                        axisSection.Stroke = new SolidColorBrush(Colors.DarkBlue);
                    axisSection.Value = value.Time.Ticks;
                    LimitSections.Add(axisSection);
                }
                
潘栩锋's avatar
潘栩锋 committed
191
            }
潘栩锋's avatar
潘栩锋 committed
192
            else if (e.PropertyName == nameof(Cfl.VelocityValues))
潘栩锋's avatar
潘栩锋 committed
193 194 195 196 197 198
            {
                var vList = Cfl.VelocityValues;

                VAvg = vList.Average(tv => tv.Value);
                VMax = vList.Max(tv => tv.Value);
                VMin = vList.Min(tv => tv.Value);
潘栩锋's avatar
潘栩锋 committed
199 200 201 202 203

                var newList = GetLimitedList(vList,200);

                VelocityValues.Clear();
                VelocityValues.AddRange(newList);
潘栩锋's avatar
潘栩锋 committed
204
            }
潘栩锋's avatar
潘栩锋 committed
205
            else if (e.PropertyName == nameof(Cfl.ThicknessValues))
潘栩锋's avatar
潘栩锋 committed
206
            {
潘栩锋's avatar
潘栩锋 committed
207 208 209
                var vList = Cfl.ThicknessValues;
                var newList = GetLimitedList(vList, 200);

潘栩锋's avatar
潘栩锋 committed
210
                ThicknessValues.Clear();
潘栩锋's avatar
潘栩锋 committed
211
                ThicknessValues.AddRange(newList);
潘栩锋's avatar
潘栩锋 committed
212
            }
潘栩锋's avatar
潘栩锋 committed
213 214 215 216
            else if (e.PropertyName == nameof(Cfl.AngleValues))
            {
                var vList = Cfl.AngleValues;
                var newList = GetLimitedList(vList, 200);
潘栩锋's avatar
潘栩锋 committed
217

潘栩锋's avatar
潘栩锋 committed
218 219 220
                AngleValues.Clear();
                AngleValues.AddRange(newList);
            }
潘栩锋's avatar
潘栩锋 committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
            else if (e.PropertyName == nameof(Cfl.Frames))
            {
                FrameSeries.Clear();
                for (int i = 0; i < Cfl.Frames.Count(); i++)
                {

                    var frame = Cfl.Frames[i];
                    string title = $"{((frame.Direction == Misc.DIRECTION.FORWARD) ? "正" : "反")}" +
                        $" [{frame.RotationCnt}]";
                    if (frame.IsVaild)
                        title += $" R={frame.R:F3}";
                    FrameSeries.Add(
                        new LineSeries()
                        {
                            Title = title,
                            Values = new ChartValues<double>(frame.Thicks),
                            LineSmoothness = 1,
                            StrokeThickness = 3,
                            PointGeometrySize = 0
                        });
                }
            }
        }

潘栩锋's avatar
潘栩锋 committed
245 246 247 248 249 250 251 252 253 254 255 256
        /// <summary>
        /// 从原始数据队列平均获取100个数据
        /// </summary>
        /// <param name="orgList"></param>
        /// <returns></returns>
        protected List<T> GetLimitedList<T>(List<T> orgList, int limitCnt) {
            var tList = orgList;
            var newList = new List<T>();
            if (tList.Count < limitCnt) 
            {
                limitCnt = tList.Count;
            }
潘栩锋's avatar
潘栩锋 committed
257

潘栩锋's avatar
潘栩锋 committed
258 259 260 261 262
            for (int i = 0; i < limitCnt; i++)
            {
                int idx = tList.Count() * i / limitCnt;
                if (idx >= tList.Count()) idx = tList.Count() - 1;
                newList.Add(tList[idx]);
潘栩锋's avatar
潘栩锋 committed
263

潘栩锋's avatar
潘栩锋 committed
264 265
            }
            return newList;
潘栩锋's avatar
潘栩锋 committed
266
        }
潘栩锋's avatar
潘栩锋 committed
267 268

        public void Download()
潘栩锋's avatar
潘栩锋 committed
269
        {
潘栩锋's avatar
潘栩锋 committed
270
            Cfl.DownloadData();
潘栩锋's avatar
潘栩锋 committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
        }
        public void UpdateVelocity()
        {
            Cfl.UpdateVelocityValues(VelocityFilter);
        }

        public void UpdateFrames()
        {
            Cfl.SetFilmLength(FilmLength);
        }

        public void Cal()
        {
            Cfl.Cal(FilmLength - FLRange, FilmLength + FLRange);
        }

        public void ApplyFilmLength()
        {
            rdetect.FilmLength = FilmLength;
            rdetect.Apply();
            FLY.ControlLibrary.Window_Tip.Show(
                "应用成功",
                $"膜距离 设置为{FilmLength:F1}m",
                TimeSpan.FromSeconds(2));
        }

        public void Save()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = $"{DateTime.Now:yyyyMMdd_HHmmss}.json";
            if (sfd.ShowDialog() == true)
            {
                Cfl.Save(sfd.FileName);
                FLY.ControlLibrary.Window_Tip.Show(
                    "保存成功",
                    $"保存到{sfd.FileName}",
                    TimeSpan.FromSeconds(2));
            }
        }
        public void Load()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.DefaultExt = ".json";
            if (ofd.ShowDialog() == true)
            {
                Cfl.Load(ofd.FileName);
                FLY.ControlLibrary.Window_Tip.Show(
                    "加载成功",
                    $"读取 {ofd.FileName}",
                    TimeSpan.FromSeconds(2));
            }
        }
    }


潘栩锋's avatar
潘栩锋 committed
326 327 328 329 330
    public class TimeValue : INotifyPropertyChanged
    {
        public DateTime Time { get; set; }

        public double Value { get; set; }
潘栩锋's avatar
潘栩锋 committed
331

潘栩锋's avatar
潘栩锋 committed
332 333
        public event PropertyChangedEventHandler PropertyChanged;
    }
潘栩锋's avatar
潘栩锋 committed
334
}