PgReject.xaml.cs 12.8 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1
using CommunityToolkit.Mvvm.Input;
潘栩锋's avatar
潘栩锋 committed
2
using FLY.Thick.Base.IService;
3 4 5
using LiveCharts;
using LiveCharts.Configurations;
using Microsoft.Win32;
潘栩锋's avatar
潘栩锋 committed
6 7
using Misc;
using OfficeOpenXml;
8
using OfficeOpenXml.Table;
潘栩锋's avatar
潘栩锋 committed
9 10 11 12 13 14 15 16
using System;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls;
using Unity;
潘栩锋's avatar
潘栩锋 committed
17

潘栩锋's avatar
潘栩锋 committed
18
namespace FLY.Thick.Base.UI
潘栩锋's avatar
潘栩锋 committed
19 20 21 22
{
    /// <summary>
    /// Page_Reject.xaml 的交互逻辑
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
23
    public partial class PgReject : Page
潘栩锋's avatar
潘栩锋 committed
24
    {
25 26
        IRejectService rejectService;
        IInitParamService initParamService;
潘栩锋's avatar
潘栩锋 committed
27
        PgRejectVm viewModel;
潘栩锋's avatar
潘栩锋 committed
28

潘栩锋's avatar
潘栩锋 committed
29
        public PgReject()
潘栩锋's avatar
潘栩锋 committed
30 31 32 33 34
        {
            InitializeComponent();
        }


潘栩锋's avatar
潘栩锋 committed
35 36
        [InjectionMethod]
        public void Init(
37
            IUnityContainer container,
潘栩锋's avatar
潘栩锋 committed
38
            IRejectService rejectService,
潘栩锋's avatar
潘栩锋 committed
39 40
            IInitParamService initParamService)
        {
41
            container.BuildUp(mircoGage);
潘栩锋's avatar
潘栩锋 committed
42
            viewModel = new PgRejectVm();
43
            viewModel.Init(rejectService, initParamService);
潘栩锋's avatar
潘栩锋 committed
44 45 46
            this.DataContext = this.viewModel;
        }
    }
47 48

    public class PgRejectVm : INotifyPropertyChanged
潘栩锋's avatar
潘栩锋 committed
49
    {
50
        public event PropertyChangedEventHandler PropertyChanged;
潘栩锋's avatar
潘栩锋 committed
51 52


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

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

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        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
80 81 82 83 84 85 86 87 88 89 90 91


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

        /// <summary>
92
        ///  规格范围,Math.Abs(value-Target) > UpperLimitRatio*Target 都要被删除
潘栩锋's avatar
潘栩锋 committed
93
        /// </summary>
94
        public double LimitRangeRatio { get; set; }
潘栩锋's avatar
潘栩锋 committed
95 96 97 98

        /// <summary>
        /// 滤波范围,单位脉冲
        /// </summary>
99
        public int SmoothRange { get; set; }
潘栩锋's avatar
潘栩锋 committed
100 101 102
        /// <summary>
        /// 当前数据需要剔除,那么它的前后Range2个脉冲的数据都是删除,单位脉冲
        /// </summary>
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        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
120

121
        public DateTime DebugUpdateTime { get; set; }
潘栩锋's avatar
潘栩锋 committed
122
        public RelayCommand ApplyCmd { get; }
123

潘栩锋's avatar
潘栩锋 committed
124 125 126 127 128
        public RelayCommand SaveCmd { get; }

        IRejectService rejectService;
        public IInitParamService InitParamService { get; private set; }
        Page page;
潘栩锋's avatar
潘栩锋 committed
129
        public PgRejectVm()
潘栩锋's avatar
潘栩锋 committed
130
        {
131 132 133 134 135 136 137 138 139 140 141 142 143 144
            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) =>
            {
潘栩锋's avatar
潘栩锋 committed
145
                return $"{y:F1}";
146 147
            };

潘栩锋's avatar
潘栩锋 committed
148
            ApplyCmd = new RelayCommand(Apply);
149
            SaveCmd = new RelayCommand(SaveXlsx);
潘栩锋's avatar
潘栩锋 committed
150 151 152
        }
        public void Init(
            IRejectService rejectService,
153
            IInitParamService initParamService)
潘栩锋's avatar
潘栩锋 committed
154 155 156 157
        {
            this.rejectService = rejectService;
            this.InitParamService = initParamService;

158 159 160
            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));
潘栩锋's avatar
潘栩锋 committed
161

162 163 164
            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
165

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

168 169 170
            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
171

172 173
            this.rejectService.PropertyChanged += new PropertyChangedEventHandler(mRejectService_PropertyChanged);
            Download();
潘栩锋's avatar
潘栩锋 committed
174 175
        }

176 177

        private async void SaveXlsx()
潘栩锋's avatar
潘栩锋 committed
178
        {
179 180 181 182 183 184 185 186 187
            //下载数据
            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
188
            {
189 190 191 192 193 194 195 196
                filename = sfd.FileName;

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

                FLY.ControlLibrary.Window_Tip.Show("成功", $"导出到{filename}", TimeSpan.FromSeconds(2));
潘栩锋's avatar
潘栩锋 committed
197
            }
198 199 200 201 202
        }

        private void SaveToXlsx(string filepath)
        {
            if (FilterDatas.Count() == 0)
潘栩锋's avatar
潘栩锋 committed
203
            {
204
                return;
潘栩锋's avatar
潘栩锋 committed
205 206
            }

207 208 209 210 211 212 213 214 215 216 217 218 219
            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" });
潘栩锋's avatar
潘栩锋 committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233

                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
234
                    {
潘栩锋's avatar
潘栩锋 committed
235
                        break;
236
                    }
潘栩锋's avatar
潘栩锋 committed
237 238 239
                }

                for (int i = 0; i < FilterDatas.Count() && i < RejectDatas.Count(); i++)
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
                {
                    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
266
        {
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
            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
292

293 294 295
            var tbl = sheet.Tables.Add(range, dataTable.TableName);
            tbl.TableStyle = TableStyles.Medium9;
            sheet.Cells[sheet.Dimension.Address].AutoFitColumns();
潘栩锋's avatar
潘栩锋 committed
296 297 298 299 300 301 302 303 304
        }

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

            rejectService.Enable = this.Enable;
            rejectService.ThresholdRatio = this.ThresholdRatio;
305 306 307
            rejectService.LimitRangeRatio = this.LimitRangeRatio;
            rejectService.SmoothRange = this.SmoothRange;
            rejectService.RejectRange = this.RejectRange;
潘栩锋's avatar
潘栩锋 committed
308 309 310 311 312

            rejectService.Apply();
            FLY.ControlLibrary.Window_Tip.Show("应用成功",
                null,
                TimeSpan.FromSeconds(2));
潘栩锋's avatar
潘栩锋 committed
313

潘栩锋's avatar
潘栩锋 committed
314 315
        }

潘栩锋's avatar
潘栩锋 committed
316

317
        void mRejectService_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
潘栩锋's avatar
潘栩锋 committed
318 319
        {

320
            if (e.PropertyName == nameof(rejectService.DebugUpdateTime))
潘栩锋's avatar
潘栩锋 committed
321
            {
322 323 324
                //下载
                Download();
            }
潘栩锋's avatar
潘栩锋 committed
325 326
            else if (e.PropertyName == nameof(FObjBase.FObjServiceClient.IsConnected))
            {
327
                var client = rejectService as FObjBase.FObjServiceClient;
潘栩锋's avatar
潘栩锋 committed
328 329
                if (client.IsConnected)
                {
330
                    Download();
潘栩锋's avatar
潘栩锋 committed
331 332 333 334
                }
            }
        }

潘栩锋's avatar
潘栩锋 committed
335
        void Download()
潘栩锋's avatar
潘栩锋 committed
336
        {
337
            rejectService.GetDebugData((asyncContext, retData) =>
潘栩锋's avatar
潘栩锋 committed
338
            {
339 340 341 342
                var reponse = retData as RejectDebugData;

                FilterDatas.Clear();
                RejectDatas.Clear();
潘栩锋's avatar
潘栩锋 committed
343 344
                if (reponse == null)
                {
345 346 347 348 349 350
                    //无数据
                    Threshold = double.NaN;
                    UpperLimit = double.NaN;
                    LowerLimit = double.NaN;
                    return;
                }
潘栩锋's avatar
潘栩锋 committed
351

潘栩锋's avatar
潘栩锋 committed
352 353 354 355 356 357
                if (reponse.filterDatas != null && reponse.filterDatas.Count() > 0)
                {

                    FilterDatas.AddRange(reponse.filterDatas.Select((d, i) => new XY()
                    {
                        X = reponse.start_grid + i,
358 359 360 361
                        Y = d
                    }));
                }
                if (reponse.rejectDatas != null && reponse.rejectDatas.Count() > 0)
潘栩锋's avatar
潘栩锋 committed
362
                {
363 364
                    RejectDatas.AddRange(reponse.rejectDatas.Select((d, i) => new XY()
                    {
潘栩锋's avatar
潘栩锋 committed
365
                        X = reponse.start_grid + i,
366 367
                        Y = d
                    }));
潘栩锋's avatar
潘栩锋 committed
368
                }
潘栩锋's avatar
潘栩锋 committed
369

370
                Threshold = reponse.target * rejectService.ThresholdRatio;
潘栩锋's avatar
潘栩锋 committed
371 372
                UpperLimit = reponse.target * (1 + rejectService.LimitRangeRatio);
                LowerLimit = reponse.target * (1 - rejectService.LimitRangeRatio);
潘栩锋's avatar
潘栩锋 committed
373

374
            }, this);
潘栩锋's avatar
潘栩锋 committed
375 376 377 378
        }

    }
}