PgReject.xaml.cs 13.3 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Forms.DataVisualization.Charting;
using System.Net;

using System.Threading;
using System.IO;

using FLY.Thick.Base.IService;
using FLY.Thick.Base.Client;
using FObjBase;
24
using Unity;
潘栩锋's avatar
潘栩锋 committed
25
using GalaSoft.MvvmLight.Command;
26 27 28 29 30 31 32 33
using LiveCharts;
using Misc;
using LiveCharts.Configurations;
using OfficeOpenXml;
using Microsoft.Win32;
using System.Threading.Tasks;
using System.Data;
using OfficeOpenXml.Table;
潘栩锋's avatar
潘栩锋 committed
34

潘栩锋's avatar
潘栩锋 committed
35
namespace FLY.Thick.Base.UI
潘栩锋's avatar
潘栩锋 committed
36 37 38 39
{
    /// <summary>
    /// Page_Reject.xaml 的交互逻辑
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
40
    public partial class PgReject : Page
潘栩锋's avatar
潘栩锋 committed
41
    {
42 43
        IRejectService rejectService;
        IInitParamService initParamService;
潘栩锋's avatar
潘栩锋 committed
44
        PgRejectVm viewModel;
潘栩锋's avatar
潘栩锋 committed
45

潘栩锋's avatar
潘栩锋 committed
46
        public PgReject()
潘栩锋's avatar
潘栩锋 committed
47 48 49 50 51
        {
            InitializeComponent();
        }


潘栩锋's avatar
潘栩锋 committed
52 53
        [InjectionMethod]
        public void Init(
54
            IUnityContainer container,
潘栩锋's avatar
潘栩锋 committed
55 56 57
            IRejectService rejectService, 
            IInitParamService initParamService)
        {
58
            container.BuildUp(mircoGage);
潘栩锋's avatar
潘栩锋 committed
59
            viewModel = new PgRejectVm();
60
            viewModel.Init(rejectService, initParamService);
潘栩锋's avatar
潘栩锋 committed
61 62 63
            this.DataContext = this.viewModel;
        }
    }
64 65

    public class PgRejectVm : INotifyPropertyChanged
潘栩锋's avatar
潘栩锋 committed
66
    {
67
        public event PropertyChangedEventHandler PropertyChanged;
潘栩锋's avatar
潘栩锋 committed
68 69


70 71
        #region 曲线
        public Func<double, string> YFormatter { get; private set; }
潘栩锋's avatar
潘栩锋 committed
72

73 74
        [PropertyChanged.DependsOn(nameof(PosOfGrid), nameof(Mmpp))]
        public Func<double, string> XFormatter { get; private set; }
潘栩锋's avatar
潘栩锋 committed
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        public CartesianMapper<XY> Mapper { get; set; }

        /// <summary>
        /// X轴最小范围
        /// </summary>
        public double XMin => 0;
        /// <summary>
        /// X轴最大范围
        /// </summary>
        public double XMax => PosLength / PosOfGrid;

        public ChartValues<XY> FilterDatas { get; } = new ChartValues<XY>();
        public ChartValues<XY> RejectDatas { get; } = new ChartValues<XY>();
        #endregion

        #region 状态
        public double Threshold { get; set; } = double.NaN;
        public double UpperLimit { get; set; } = double.NaN;
        public double LowerLimit { get; set; } = double.NaN;

        #endregion
潘栩锋's avatar
潘栩锋 committed
97 98 99 100 101 102 103 104 105 106 107 108


        /// <summary>
        /// 使能
        /// </summary>
        public bool Enable { get; set; }
        /// <summary>
        /// 剔除阈值,低于 ThresholdRatio*Target 都要被删除
        /// </summary>
        public double ThresholdRatio { get; set; }

        /// <summary>
109
        ///  规格范围,Math.Abs(value-Target) > UpperLimitRatio*Target 都要被删除
潘栩锋's avatar
潘栩锋 committed
110
        /// </summary>
111
        public double LimitRangeRatio { get; set; }
潘栩锋's avatar
潘栩锋 committed
112 113 114 115

        /// <summary>
        /// 滤波范围,单位脉冲
        /// </summary>
116
        public int SmoothRange { get; set; }
潘栩锋's avatar
潘栩锋 committed
117 118 119
        /// <summary>
        /// 当前数据需要剔除,那么它的前后Range2个脉冲的数据都是删除,单位脉冲
        /// </summary>
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        public int RejectRange { get; set; }

        /// <summary>
        /// 机架总长,脉冲
        /// </summary>
        public int PosLength { get; set; } = 8000;

        /// <summary>
        /// 1个grid = N个pos
        /// </summary>
        public int PosOfGrid { get; set; } = 10;

        /// <summary>
        /// 比例
        /// </summary>
        public double Mmpp { get; set; } = 0.1;

潘栩锋's avatar
潘栩锋 committed
137

138
        public DateTime DebugUpdateTime { get; set; }
潘栩锋's avatar
潘栩锋 committed
139
        public RelayCommand ApplyCmd { get; }
140

潘栩锋's avatar
潘栩锋 committed
141 142 143 144 145 146 147
        public RelayCommand SaveCmd { get; }

        IRejectService rejectService;
        public IInitParamService InitParamService { get; private set; }
        Page page;
        public PgRejectVm() 
        {
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
            Mapper = Mappers.Xy<XY>()
                .X((value, index) => value.X)
                .Y(value => value.Y);


            XFormatter = (x) =>
            {
                //x 是 grid
                int pos = (int)(x * PosOfGrid);
                int mm = (int)(pos * Mmpp);
                return $"{pos}\n{mm}mm";
            };
            YFormatter = (y) =>
            {
                return $"{ y:F1}";
            };

潘栩锋's avatar
潘栩锋 committed
165
            ApplyCmd = new RelayCommand(Apply);
166
            SaveCmd = new RelayCommand(SaveXlsx);
潘栩锋's avatar
潘栩锋 committed
167 168 169
        }
        public void Init(
            IRejectService rejectService,
170
            IInitParamService initParamService)
潘栩锋's avatar
潘栩锋 committed
171 172 173 174
        {
            this.rejectService = rejectService;
            this.InitParamService = initParamService;

175 176 177 178 179 180 181
            Misc.BindingOperations.SetBinding(rejectService, nameof(rejectService.Enable), this, nameof(Enable));
            Misc.BindingOperations.SetBinding(rejectService, nameof(rejectService.ThresholdRatio), this, nameof(ThresholdRatio));
            Misc.BindingOperations.SetBinding(rejectService, nameof(rejectService.LimitRangeRatio), this, nameof(LimitRangeRatio));
            
            Misc.BindingOperations.SetBinding(rejectService, nameof(rejectService.LimitRangeRatio), this, nameof(LimitRangeRatio));
            Misc.BindingOperations.SetBinding(rejectService, nameof(rejectService.SmoothRange), this, nameof(SmoothRange));
            Misc.BindingOperations.SetBinding(rejectService, nameof(rejectService.RejectRange), this, nameof(RejectRange));
潘栩锋's avatar
潘栩锋 committed
182

183
            Misc.BindingOperations.SetBinding(rejectService, nameof(rejectService.DebugUpdateTime), this, nameof(DebugUpdateTime));
潘栩锋's avatar
潘栩锋 committed
184

185 186 187
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.Encoder1_mmpp), this, nameof(Mmpp));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.PosLength), this, nameof(PosLength));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.PosOfGrid), this, nameof(PosOfGrid));
潘栩锋's avatar
潘栩锋 committed
188

189 190
            this.rejectService.PropertyChanged += new PropertyChangedEventHandler(mRejectService_PropertyChanged);
            Download();
潘栩锋's avatar
潘栩锋 committed
191 192
        }

193 194

        private async void SaveXlsx()
潘栩锋's avatar
潘栩锋 committed
195
        {
196 197 198 199 200 201 202 203 204
            //下载数据
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

            string filename = $"剔除_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "xlsx files (*.xlsx)|*.xlsx";
            sfd.InitialDirectory = strDesktopPath;
            sfd.FileName = filename;
            if (sfd.ShowDialog() == true)
潘栩锋's avatar
潘栩锋 committed
205
            {
206 207 208 209 210 211 212 213
                filename = sfd.FileName;

                await Task.Factory.StartNew(() =>
                {
                    SaveToXlsx(filename);
                });

                FLY.ControlLibrary.Window_Tip.Show("成功", $"导出到{filename}", TimeSpan.FromSeconds(2));
潘栩锋's avatar
潘栩锋 committed
214
            }
215 216 217 218 219
        }

        private void SaveToXlsx(string filepath)
        {
            if (FilterDatas.Count() == 0)
潘栩锋's avatar
潘栩锋 committed
220
            {
221
                return;
潘栩锋's avatar
潘栩锋 committed
222 223
            }

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            using (ExcelPackage p = new ExcelPackage(new FileInfo(filepath)))
            {
                ExcelWorksheet sheet = p.Workbook.Worksheets.Add("剔除");
                DataTable dataTable = new DataTable("table_reject");
                dataTable.Columns.Add(new DataColumn() { ColumnName = "序号", DataType = typeof(double), Caption = "0" });
                dataTable.Columns.Add(new DataColumn() { ColumnName = "脉冲", DataType = typeof(double), Caption = "0" });
                dataTable.Columns.Add(new DataColumn() { ColumnName = "mm", DataType = typeof(double), Caption = "0.0" });
                dataTable.Columns.Add(new DataColumn() { ColumnName = "滤波", DataType = typeof(double), Caption = "0.00" });
                dataTable.Columns.Add(new DataColumn() { ColumnName = "剔除后", DataType = typeof(double), Caption = "0.00" });
                
                    XY data = FilterDatas.First();
                    int start_grid = (int)data.X;
                    for (int i = 0; i < start_grid; i++)
                    {
                        try
                        {
                            var dataRow = dataTable.NewRow();
                            dataRow["序号"] = i;
                            dataRow["脉冲"] = i * PosOfGrid;
                            dataRow["mm"] = i * PosOfGrid * Mmpp;
                            dataTable.Rows.Add(dataRow);
                        }
                        catch
                        {
                            break;
                        }
                    }
                
                for (int i = 0; i < FilterDatas.Count() && i< RejectDatas.Count(); i++)
                {
                    try
                    {
                        var dataRow = dataTable.NewRow();
                        int index = start_grid + i;
                        dataRow["序号"] = index;
                        dataRow["脉冲"] = index * PosOfGrid;
                        dataRow["mm"] = index * PosOfGrid * Mmpp;
                        dataRow["滤波"] = FilterDatas[i].Y;
                        dataRow["剔除后"] = RejectDatas[i].Y;
                        dataTable.Rows.Add(dataRow);
                    }
                    catch
                    {
                        break;
                    }
                }

                ToSheet(sheet, dataTable);

                p.Save();

            }

        }
        void ToSheet(ExcelWorksheet sheet, DataTable dataTable)
潘栩锋's avatar
潘栩锋 committed
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
            int from_row = 1;
            int row = from_row;
            //添加标题
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                int col = i + 1;
                sheet.Cells[row, col].Value = dataTable.Columns[i].ColumnName;
                //格式
                sheet.Column(col).Style.Numberformat.Format = dataTable.Columns[i].Caption;
            }

            row++;
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                for (int j = 0; j < dataTable.Columns.Count; j++)
                {
                    int col = j + 1;
                    sheet.Cells[row, col].Value = dataTable.Rows[i][j];
                }
                row++;
            }

            int colcnt = dataTable.Columns.Count;
            int rowcnt = dataTable.Rows.Count;
            var range = sheet.Cells[from_row, 1, from_row + rowcnt, colcnt];
潘栩锋's avatar
潘栩锋 committed
309

310 311 312
            var tbl = sheet.Tables.Add(range, dataTable.TableName);
            tbl.TableStyle = TableStyles.Medium9;
            sheet.Cells[sheet.Dimension.Address].AutoFitColumns();
潘栩锋's avatar
潘栩锋 committed
313 314 315 316 317 318 319 320 321
        }

        private void Apply()
        {
            if (!WdPassword.Authorize("Reject"))
                return;

            rejectService.Enable = this.Enable;
            rejectService.ThresholdRatio = this.ThresholdRatio;
322 323 324
            rejectService.LimitRangeRatio = this.LimitRangeRatio;
            rejectService.SmoothRange = this.SmoothRange;
            rejectService.RejectRange = this.RejectRange;
潘栩锋's avatar
潘栩锋 committed
325 326 327 328 329 330 331 332

            rejectService.Apply();
            FLY.ControlLibrary.Window_Tip.Show("应用成功",
                null,
                TimeSpan.FromSeconds(2));
            
        }

潘栩锋's avatar
潘栩锋 committed
333

334
        void mRejectService_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
潘栩锋's avatar
潘栩锋 committed
335 336
        {

337
            if (e.PropertyName == nameof(rejectService.DebugUpdateTime))
潘栩锋's avatar
潘栩锋 committed
338
            {
339 340 341 342 343 344 345
                //下载
                Download();
            }
            else if (e.PropertyName == nameof(FObjBase.FObjServiceClient.IsConnected)) {
                var client = rejectService as FObjBase.FObjServiceClient;
                if (client.IsConnected) {
                    Download();
潘栩锋's avatar
潘栩锋 committed
346 347 348 349
                }
            }
        }

350
        void Download() 
潘栩锋's avatar
潘栩锋 committed
351
        {
352
            rejectService.GetDebugData((asyncContext, retData) =>
潘栩锋's avatar
潘栩锋 committed
353
            {
354 355 356 357 358 359 360 361 362 363 364
                var reponse = retData as RejectDebugData;

                FilterDatas.Clear();
                RejectDatas.Clear();
                if (reponse == null) {
                    //无数据
                    Threshold = double.NaN;
                    UpperLimit = double.NaN;
                    LowerLimit = double.NaN;
                    return;
                }
潘栩锋's avatar
潘栩锋 committed
365

366 367 368 369 370 371 372 373
                if (reponse.filterDatas != null && reponse.filterDatas.Count() > 0) {
                    
                    FilterDatas.AddRange(reponse.filterDatas.Select((d,i) => new XY() {
                        X = reponse.start_grid+i,
                        Y = d
                    }));
                }
                if (reponse.rejectDatas != null && reponse.rejectDatas.Count() > 0)
潘栩锋's avatar
潘栩锋 committed
374
                {
375 376 377 378 379
                    RejectDatas.AddRange(reponse.rejectDatas.Select((d, i) => new XY()
                    {
                        X = reponse.start_grid+i,
                        Y = d
                    }));
潘栩锋's avatar
潘栩锋 committed
380
                }
潘栩锋's avatar
潘栩锋 committed
381

382 383 384
                Threshold = reponse.target * rejectService.ThresholdRatio;
                UpperLimit = reponse.target * (1+rejectService.LimitRangeRatio);
                LowerLimit = reponse.target * (1-rejectService.LimitRangeRatio);
潘栩锋's avatar
潘栩锋 committed
385

386
            }, this);
潘栩锋's avatar
潘栩锋 committed
387 388 389 390
        }

    }
}