PgGetSample.xaml.cs 15.1 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1
using FLY.Thick.Base.IService;
潘栩锋's avatar
潘栩锋 committed
2
using GalaSoft.MvvmLight.Command;
潘栩锋's avatar
潘栩锋 committed
3
using System;
4
using System.Collections.Generic;
潘栩锋's avatar
潘栩锋 committed
5
using System.ComponentModel;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Navigation;
using Unity;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Page_GetSample.xaml 的交互逻辑
    /// </summary>
    public partial class PgGetSample : Page
    {
        IGetSampleService getSampleService;
        IInitParamService initParamService;
        IGageInfoService gageInfoService;
        IUnityContainer container;

        GetSampleVm viewModel;

        public PgGetSample()
        {
            InitializeComponent();
        }
        [InjectionMethod]
        public void Init(
潘栩锋's avatar
潘栩锋 committed
33
            IUnityContainer container,
34 35
            IGetSampleService getSampleService,
            IInitParamService initParamService,
潘栩锋's avatar
潘栩锋 committed
36
            IGageInfoService gageInfoService)
37 38 39 40 41 42 43 44
        {

            this.getSampleService = getSampleService;
            this.initParamService = initParamService;
            this.gageInfoService = gageInfoService;
            this.container = container;

            viewModel = new GetSampleVm();
潘栩锋's avatar
潘栩锋 committed
45
            viewModel.Init(getSampleService, gageInfoService, tempdatas);
46
            this.DataContext = viewModel;
潘栩锋's avatar
潘栩锋 committed
47 48 49 50
            this.grid_initparam.DataContext = this.initParamService;
        }
        private void btnGageInfoClick(object sender, RoutedEventArgs e)
        {
51 52
            Page p = (container.IsRegistered<Page>("pgGageInfo")) ? container.Resolve<Page>("pgGageInfo") : container.Resolve<PgGageInfo>(); ;
            //container.BuildUp(p);
潘栩锋's avatar
潘栩锋 committed
53
            NavigationService.Navigate(p);
54 55
        }

潘栩锋's avatar
潘栩锋 committed
56 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    }

    public class GetSampleVm : INotifyPropertyChanged
    {
        #region 参数

        /// <summary>
        /// 参数:使能
        /// </summary>
        public bool Enable { get; set; }


        /// <summary>
        /// 参数:速度
        /// </summary>
        public UInt32 Velocity { get; set; }

        /// <summary>
        /// 参数:样品点范围
        /// </summary>
        public int Range { get; set; }

        /// <summary>
        /// 参数:移动滤波 ,窗口值
        /// </summary>
        public int Window { get; set; }

        /// <summary>
        /// 使用%方式检查异常
        /// </summary>
        public bool IsCheckByPercent { get; set; }
        /// <summary>
        /// 异常%
        /// </summary>
        public double ErrPercent { get; set; }
        /// <summary>
        /// 异常值
        /// </summary>
        public int ErrValue { get; set; }
        /// <summary>
        /// 参数:样品点参数
        /// </summary>
        public SampleCell[] Samples { get; private set; }
        /// <summary>
        /// 参数:特征查找范围
        /// </summary>
        public int Search { get; set; }
        /// <summary>
        /// 参数:特征参数
        /// </summary>
        public SampleFeature[] Features { get; private set; }

        #endregion

        #region Command
        public RelayCommand ApplyCmd { get; }
        public RelayCommand GetOrgAdCmd { get; }
        public RelayCommand GetTempDataCmd { get; }
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        IGetSampleService getSampleService;
        IGageInfoService gageInfoService;
        ItemsControl tempdatas;
        public GetSampleVm()
        {
            ApplyCmd = new RelayCommand(Apply);
            GetOrgAdCmd = new RelayCommand(GetOrgAd);
            GetTempDataCmd = new RelayCommand(GetTempData);
        }



        [InjectionMethod]
        public void Init(
            IGetSampleService getSampleService,
            IGageInfoService gageInfoService,
            ItemsControl tempdatas)
135 136
        {

潘栩锋's avatar
潘栩锋 committed
137 138 139
            this.getSampleService = getSampleService;
            this.gageInfoService = gageInfoService;
            this.tempdatas = tempdatas;
140 141 142 143 144 145 146 147
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Enable), this, nameof(Enable));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Velocity), this, nameof(Velocity));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Range), this, nameof(Range));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Window), this, nameof(Window));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.IsCheckByPercent), this, nameof(IsCheckByPercent));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.ErrPercent), this, nameof(ErrPercent));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.ErrValue), this, nameof(ErrValue));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Search), this, nameof(Search));
潘栩锋's avatar
潘栩锋 committed
148 149 150 151

            Samples = new SampleCell[this.getSampleService.Samples.Count()];
            Features = new SampleFeature[this.getSampleService.Features.Count()];
            for (int i = 0; i < Samples.Count(); i++)
152
            {
潘栩锋's avatar
潘栩锋 committed
153
                Samples[i] = new SampleCell();
154 155 156 157
                Misc.BindingOperations.SetBinding(this.getSampleService.Samples[i], nameof(SampleCell.Enable), Samples[i], nameof(SampleCell.Enable));
                Misc.BindingOperations.SetBinding(this.getSampleService.Samples[i], nameof(SampleCell.JustForCheck), Samples[i], nameof(SampleCell.JustForCheck));
                Misc.BindingOperations.SetBinding(this.getSampleService.Samples[i], nameof(SampleCell.OrgAD), Samples[i], nameof(SampleCell.OrgAD));
                Misc.BindingOperations.SetBinding(this.getSampleService.Samples[i], nameof(SampleCell.Position), Samples[i], nameof(SampleCell.Position));
158 159
            }

潘栩锋's avatar
潘栩锋 committed
160 161 162
            for (int i = 0; i < Features.Count(); i++)
            {
                Features[i] = new SampleFeature();
163 164 165
                Misc.BindingOperations.SetBinding(this.getSampleService.Features[i], nameof(SampleFeature.Enable), Features[i], nameof(SampleFeature.Enable));
                Misc.BindingOperations.SetBinding(this.getSampleService.Features[i], nameof(SampleFeature.StartPos), Features[i], nameof(SampleFeature.StartPos));
                Misc.BindingOperations.SetBinding(this.getSampleService.Features[i], nameof(SampleFeature.EndPos), Features[i], nameof(SampleFeature.EndPos));
潘栩锋's avatar
潘栩锋 committed
166
            }
潘栩锋's avatar
潘栩锋 committed
167

168 169
        }

潘栩锋's avatar
潘栩锋 committed
170 171

        private void GetOrgAd()
172 173 174 175 176 177 178 179 180 181 182
        {
            if (gageInfoService.ForwData == null || gageInfoService.BackwData == null)
            {
                FLY.ControlLibrary.Window_WarningTip.Show("失败",
                    "机架信息 为空",
                    TimeSpan.FromSeconds(2));
                return;
            }
            int[] forwdata = gageInfoService.ForwData.ToArray();
            int[] backwdata = gageInfoService.BackwData.ToArray();

潘栩锋's avatar
潘栩锋 committed
183
            for (int i = 0; i < Samples.Count(); i++)
184
            {
潘栩锋's avatar
潘栩锋 committed
185
                var sampleCell = Samples[i];
186 187 188 189
                if (sampleCell.Enable)
                {

                    int grid = sampleCell.Position / gageInfoService.PosOfGrid;
潘栩锋's avatar
潘栩锋 committed
190
                    int range = Range / gageInfoService.PosOfGrid;
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

                    int bg = grid - range;
                    int eg = grid + range;

                    if (bg < 0 || bg >= forwdata.Length || bg > backwdata.Length)
                    {
                        FLY.ControlLibrary.Window_WarningTip.Show("失败",
                            "位置出错",
                            TimeSpan.FromSeconds(2));
                        return;
                    }
                    if (eg < 0 || eg >= forwdata.Length || eg > backwdata.Length)
                    {
                        FLY.ControlLibrary.Window_WarningTip.Show("失败",
                            "位置出错",
                            TimeSpan.FromSeconds(2));
                        return;
                    }

                    int ad1 = Misc.MyMath.Avg(forwdata, bg, eg);
                    int ad2 = Misc.MyMath.Avg(backwdata, bg, eg);
                    if (!Misc.MyBase.ISVALIDATA(ad1) || !Misc.MyBase.ISVALIDATA(ad2))
                    {
                        FLY.ControlLibrary.Window_WarningTip.Show("失败",
                            "位置出错",
                            TimeSpan.FromSeconds(2));
                        return;
                    }
潘栩锋's avatar
潘栩锋 committed
219
                    sampleCell.OrgAD = (ad1 + ad2) / 2;
220 221 222 223 224 225 226 227
                }
            }

            FLY.ControlLibrary.Window_Tip.Show("设置完成",
                null,
                TimeSpan.FromSeconds(2));
        }

潘栩锋's avatar
潘栩锋 committed
228
        private void Apply()
229
        {
潘栩锋's avatar
潘栩锋 committed
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
            if (!WdPassword.Authorize("GetSample"))
                return;



            this.getSampleService.Enable = this.Enable;
            this.getSampleService.Range = this.Range;
            this.getSampleService.Velocity = this.Velocity;
            this.getSampleService.Window = this.Window;
            this.getSampleService.IsCheckByPercent = this.IsCheckByPercent;
            this.getSampleService.ErrPercent = this.ErrPercent;
            this.getSampleService.ErrValue = this.ErrValue;

            for (int i = 0; i < Samples.Count(); i++)
            {
                var sample_src = this.Samples[i];
                var sample_desp = this.getSampleService.Samples[i];

                sample_desp.Enable = sample_src.Enable;
                sample_desp.JustForCheck = sample_src.JustForCheck;
                sample_desp.OrgAD = sample_src.OrgAD;
                sample_desp.Position = sample_src.Position;
            }
            this.getSampleService.Search = this.Search;
            for (int i = 0; i < Features.Count(); i++)
            {
                var feature_src = this.Features[i];
                var feature_desp = this.getSampleService.Features[i];


                feature_desp.Enable = feature_src.Enable;
                feature_desp.StartPos = feature_src.StartPos;
                feature_desp.EndPos = feature_src.EndPos;
            }

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

潘栩锋's avatar
潘栩锋 committed
271
        private void GetTempData()
潘栩锋's avatar
潘栩锋 committed
272 273 274 275 276 277 278 279 280 281
        {
            getSampleService.GetTempFilterDatas((asyncContext, retData) =>
            {
                var ll = retData as List<List<TempFilterData>>;
                tempdatas.ItemsSource = ll;
                FLY.ControlLibrary.Window_Tip.Show("加载完成",
                    $"共加载{ll.Count()}列数据",
                    TimeSpan.FromSeconds(2));
            }, null);
        }
282
    }
潘栩锋's avatar
潘栩锋 committed
283
    public class GetSampleVmUt : INotifyPropertyChanged
284 285 286
    {
        public GetSampleVmUt()
        {
潘栩锋's avatar
潘栩锋 committed
287
            Samples = new SampleCell[3];
288 289 290 291 292
            for (int i = 0; i < Samples.Count(); i++)
            {
                SampleCell sampleCell = new SampleCell();
                Samples[i] = sampleCell;
            }
293
            Features = new SampleFeature[2];
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
            Features[0] = new SampleFeature();
            Features[1] = new SampleFeature();


            Enable = true;
            Samples[0].Enable = true;

            Features[0].Enable = true;

        }

        #region IGetSampleService 成员

        /// <summary>
        /// 参数:使能
        /// </summary>
        public bool Enable { get; set; }


        /// <summary>
        /// 参数:速度
        /// </summary>
        public UInt32 Velocity { get; set; }

        /// <summary>
        /// 参数:样品点范围
        /// </summary>
        public int Range { get; set; }

        /// <summary>
潘栩锋's avatar
潘栩锋 committed
324
        /// 参数:移动滤波 ,窗口值
325 326 327 328
        /// </summary>
        public int Window { get; set; }

        /// <summary>
潘栩锋's avatar
潘栩锋 committed
329
        /// 使用%方式检查异常
330
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
331
        public bool IsCheckByPercent { get; set; }
332
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
333 334 335 336 337 338 339 340 341 342 343 344 345
        /// 异常%
        /// </summary>
        public double ErrPercent { get; set; }
        /// <summary>
        /// 异常值
        /// </summary>
        public int ErrValue { get; set; }
        /// <summary>
        /// 参数:样品点参数
        /// </summary>
        public SampleCell[] Samples { get; }
        /// <summary>
        /// 参数:特征查找范围
346 347 348
        /// </summary>
        public int Search { get; set; }
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
349
        /// 参数:特征参数
350
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
351
        public SampleFeature[] Features { get; }
352 353 354 355 356 357 358 359 360 361 362 363

        /// <summary>
        /// 状态:从flyad7 得到的。 用于绘制图
        /// </summary>
        public int PosOfGrid { get; set; } = 10;

        public event PropertyChangedEventHandler PropertyChanged;


        #endregion

    }
潘栩锋's avatar
潘栩锋 committed
364 365


366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

    /// <summary>
    /// 列表转序号
    /// </summary>
    [ValueConversion(typeof(object), typeof(int))]
    public class Item2IndexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            int result = -1;
            if (value != null)
            {

                var collectionViewSource = parameter as CollectionViewSource;
                if (collectionViewSource != null)
                {
                    var cv = (CollectionView)collectionViewSource.View;
                    //在 设计模式中, View为null, 所以下面必须判断
                    if (cv != null)
                    {
                        result = cv.IndexOf(value);
                    }
                }
            }
            return result >= 0 ? result : System.Windows.DependencyProperty.UnsetValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 列表转序号
    /// </summary>
    [ValueConversion(typeof(object), typeof(string))]
    public class Item2DirectionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {

                var collectionViewSource = parameter as CollectionViewSource;
                if (collectionViewSource != null)
                {
                    var cv = (CollectionView)collectionViewSource.View;
                    //在 设计模式中, View为null, 所以下面必须判断
                    if (cv != null)
                    {
潘栩锋's avatar
潘栩锋 committed
418
                        switch (cv.IndexOf(value))
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
                        {
                            case 0:
                                return "正向";
                            case 1:
                                return "反向";

                        }
                    }
                }
            }
            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}