CalFilmLen360.cs 30.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
using FLY.Thick.Blowing;
using FLY.Thick.Blowing.IService;
using Misc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace FLY.Thick.Blowing360.UI
{
    public class CalFilmLen360 : INotifyPropertyChanged
    {

        #region 状态
        /// <summary>
        /// 信息
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
21
        public string Msg { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
22 23 24 25

        /// <summary>
        /// 下载中
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
26
        public bool IsDownloading { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
27 28 29 30

        /// <summary>
        /// 数据准备好了
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
31
        public bool IsDataReady { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
32 33 34 35

        /// <summary>
        /// 计算中
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
36
        public bool IsCaling { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
37 38 39 40

        /// <summary>
        /// 有足够的数据,可以下载
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
41
        public bool IsCanDownload { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
42 43 44 45

        /// <summary>
        /// 当前数据总分钟数 min
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
46
        public int DataMaxMinute { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
47 48 49
        /// <summary>
        /// 必须获取的最少数据量 min
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
50
        public int DataMinMinute { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
51 52 53 54

        /// <summary>
        /// 最佳获取数据量 min
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
55
        public int DataBestMinute { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
56 57 58
        #endregion

        #region 参数
潘栩锋's avatar
潘栩锋 committed
59 60

        public bool IsRPosMode { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
61 62 63
        /// <summary>
        /// 第1牵引速度
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
64
        public double Velocity1 { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
65 66 67 68

        /// <summary>
        /// 速度滤波 单位s
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
69
        public int VelocityFilter { get; protected set; } = 1;
潘栩锋's avatar
潘栩锋 committed
70 71 72 73

        /// <summary>
        /// 膜距离
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
74
        public double FilmLength { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
75 76 77 78 79 80 81 82 83
        #endregion

        IBlowingFixService mCurrBlowing;
        IBlowingDetectService mCurrRDetect;
        BlowingDetect360Core mRDetectCore;

        List<ThkCell> mThkList = new List<ThkCell>();
        List<LimitCell> mLimitList = new List<LimitCell>();
        List<RollCell> mRollList = new List<RollCell>();
潘栩锋's avatar
潘栩锋 committed
84
        List<RPosData> mRPosList = new List<RPosData>();
潘栩锋's avatar
潘栩锋 committed
85 86 87 88 89 90 91 92

        #region 画图
        public List<TimeValue> LimitValues { get; } = new List<TimeValue>();

        public List<TimeValue> VelocityValues { get; } = new List<TimeValue>();

        public List<TimeValue> ThicknessValues { get; } = new List<TimeValue>();

潘栩锋's avatar
潘栩锋 committed
93 94
        public List<TimeValue> AngleValues { get; } = new List<TimeValue>();

潘栩锋's avatar
潘栩锋 committed
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        public class Frame
        {
            /// <summary>
            /// 厚度
            /// </summary>
            public double[] Thicks;
            /// <summary>
            /// 旋转方向
            /// </summary>
            public Misc.DIRECTION Direction;
            /// <summary>
            /// 旋转次数
            /// </summary>
            public int RotationCnt;
            /// <summary>
            /// 与上一幅图的相关性
            /// </summary>
            public double R;
            /// <summary>
            /// 大于30%数据 非 NaN
            /// </summary>
            public bool IsVaild;
        }
        [PropertyChanged.DoNotCheckEquality]
        public List<Frame> Frames { get; set; } = new List<Frame>();
        #endregion

        int NBolts;
        int OrgBoltNo;

        DateTime dataBegin;
        /// <summary>
        /// 最小旋转次数
        /// </summary>
        const int minRotationCnt = 2;
        public CalFilmLen360()
        {
            mRDetectCore = new BlowingDetect360Core();
        }

        public void Init(IBlowingFixService mRenZiJiaFix, IBlowingDetectService mRDetect)
        {
            this.mCurrBlowing = mRenZiJiaFix;
            this.mCurrRDetect = mRDetect;

            this.FilmLength = mRDetect.FilmLength;

            Misc.BindingOperations.SetBinding(mCurrRDetect, nameof(IBlowingDetectService.BufTotalTime), () =>
            {
                DataMaxMinute = (int)(mCurrRDetect.BufTotalTime.TotalMinutes);
            });

            Misc.BindingOperations.SetBinding(mCurrRDetect, nameof(IBlowingDetectService.RotationCnt), () =>
            {
                if (mRDetect.RotationCnt < minRotationCnt)
                {
                    IsCanDownload = false;
                }
                else
                {
                    IsCanDownload = true;
                }
            });

            Misc.BindingOperations.SetBinding(mCurrRDetect, nameof(IBlowingDetectService.RenZiJiaPeriod), () =>
            {
                DataMinMinute = (int)(mRDetect.RenZiJiaPeriod.TotalMinutes * (minRotationCnt - 1));
                DataBestMinute = (int)Math.Round((mRDetect.RenZiJiaPeriod.TotalMinutes * 3));
            });



        }

潘栩锋's avatar
潘栩锋 committed
169
        ActionQueue actionQueue = new ActionQueue();
潘栩锋's avatar
潘栩锋 committed
170

潘栩锋's avatar
潘栩锋 committed
171
        public bool DownloadData()
潘栩锋's avatar
潘栩锋 committed
172 173 174 175 176 177 178 179
        {
            //应该在界面 检测错误原因
            if (IsDownloading)
                return false;

            if (IsCaling)
                return false;

潘栩锋's avatar
潘栩锋 committed
180
            dataBegin = DateTime.Now - mCurrRDetect.BufTotalTime;
潘栩锋's avatar
潘栩锋 committed
181 182 183
            IsDownloading = true;
            IsDataReady = false;

潘栩锋's avatar
潘栩锋 committed
184 185
            //按步骤下载数据
            actionQueue.Clear();
潘栩锋's avatar
潘栩锋 committed
186

潘栩锋's avatar
潘栩锋 committed
187 188 189 190 191 192 193 194 195 196
            actionQueue.Add(DownloadData_limit);
            actionQueue.Add(DownloadData_roll);
            actionQueue.Add(DownloadData_ad);

            if (mCurrRDetect.IsRPosMode)
                actionQueue.Add(DownloadData_rPos);

            actionQueue.Add(DownloadData_OK);

            actionQueue.Start();
潘栩锋's avatar
潘栩锋 committed
197 198
            return true;
        }
潘栩锋's avatar
潘栩锋 committed
199 200

        public class ActionQueue
潘栩锋's avatar
潘栩锋 committed
201
        {
潘栩锋's avatar
潘栩锋 committed
202 203 204 205 206 207 208 209 210 211
            List<Action> actions = new List<Action>();
            public void Add(Action action)
            {
                actions.Add(action);
            }
            public bool Start()
            {
                return Next();
            }
            public bool Next()
潘栩锋's avatar
潘栩锋 committed
212
            {
潘栩锋's avatar
潘栩锋 committed
213
                if (actions.Count() > 0)
潘栩锋's avatar
潘栩锋 committed
214
                {
潘栩锋's avatar
潘栩锋 committed
215 216 217 218
                    var action = actions.First();
                    actions.RemoveAt(0);
                    action.Invoke();
                    return true;
潘栩锋's avatar
潘栩锋 committed
219
                }
潘栩锋's avatar
潘栩锋 committed
220 221
                return false;
            }
潘栩锋's avatar
潘栩锋 committed
222

潘栩锋's avatar
潘栩锋 committed
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
            public bool IsEmpty()
            {
                if (actions.Count() == 0)
                    return true;
                else
                    return false;
            }

            public void Clear()
            {
                actions.Clear();
            }
        }

        void DownloadData_limit()
        {
            DateTime dateTime = dataBegin;
            if (mLimitList.Count() > 0)
            {
                dateTime = mLimitList.Last().dt_begin;
                if (dateTime < dataBegin)
                {
                    //数据太久,先删除,再下载
                    mLimitList.Clear();
                    dateTime = dataBegin;
                }
            }
            Msg = $"下载 转向信号数据 ({dateTime} - Now]....";
潘栩锋's avatar
潘栩锋 committed
251

潘栩锋's avatar
潘栩锋 committed
252 253 254 255 256
            mCurrRDetect.GetLimitList(dateTime,
                (object asyncContext, object retData) => {
                    var p = retData as GetLimitListReponse;
                    if (p.datas != null && p.datas.Count() > 0)//有数据
                        mLimitList.AddRange(p.datas);
潘栩锋's avatar
潘栩锋 committed
257

潘栩锋's avatar
潘栩锋 committed
258 259 260
                    //下一步
                    actionQueue.Next();
                }, null);
潘栩锋's avatar
潘栩锋 committed
261 262 263
        }
        void DownloadData_roll()
        {
潘栩锋's avatar
潘栩锋 committed
264 265
            DateTime dateTime = dataBegin;
            if (mRollList.Count() > 0)
潘栩锋's avatar
潘栩锋 committed
266
            {
潘栩锋's avatar
潘栩锋 committed
267 268
                dateTime = mRollList.Last().Time;
                if (dateTime < dataBegin)
潘栩锋's avatar
潘栩锋 committed
269
                {
潘栩锋's avatar
潘栩锋 committed
270 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
                    //数据太久,先删除,再下载
                    mRollList.Clear();
                    dateTime = dataBegin;
                }
            }
            Msg = $"下载 辊信号数据 [{dateTime} - Now]....";

            mCurrRDetect.GetRollList(dateTime,
                (object asyncContext, object retData) => {
                    var p = retData as GetRollListReponse;
                    if (p.datas != null && p.datas.Count() > 0)
                        mRollList.AddRange(p.datas);

                    //下一步
                    actionQueue.Next();
                }, null);
        }

        void DownloadData_rPos()
        {
            DateTime dateTime = dataBegin;
            if (mRPosList.Count() > 0)
            {
                dateTime = mRPosList.Last().Time;
                if (dateTime < dataBegin)
                {
                    //数据太久,先删除,再下载
                    mRPosList.Clear();
                    dateTime = dataBegin;
潘栩锋's avatar
潘栩锋 committed
299
                }
潘栩锋's avatar
潘栩锋 committed
300 301 302
            }

            Msg = $"下载 旋转脉冲数据 [{dateTime} - Now]....";
潘栩锋's avatar
潘栩锋 committed
303 304


潘栩锋's avatar
潘栩锋 committed
305 306 307 308 309 310 311 312
            mCurrRDetect.GetRPosList(dateTime,
                (object asyncContext, object retData) => {
                    var p = retData as GetRPosListReponse;
                    if (p.datas != null && p.datas.Count() > 0)
                        mRPosList.AddRange(p.datas);

                    actionQueue.Next();
                }, null);
潘栩锋's avatar
潘栩锋 committed
313 314 315
        }
        void DownloadData_ad()
        {
潘栩锋's avatar
潘栩锋 committed
316 317
            DateTime dateTime = dataBegin;
            if (mThkList.Count() > 0)
潘栩锋's avatar
潘栩锋 committed
318
            {
潘栩锋's avatar
潘栩锋 committed
319 320
                dateTime = mThkList.Last().Time;
                if (dateTime < dataBegin)
潘栩锋's avatar
潘栩锋 committed
321
                {
潘栩锋's avatar
潘栩锋 committed
322 323 324
                    //数据太久,先删除,再下载
                    mThkList.Clear();
                    dateTime = dataBegin;
潘栩锋's avatar
潘栩锋 committed
325
                }
潘栩锋's avatar
潘栩锋 committed
326
            }
潘栩锋's avatar
潘栩锋 committed
327

潘栩锋's avatar
潘栩锋 committed
328 329 330 331 332
            Msg = $"下载 厚度数据 [{dateTime} - Now]....";
            mCurrBlowing.GetADList(dateTime,
                (object asyncContext, object retData) => {
                    var p = retData as GetADListReponse;
                    if (p.datas != null && p.datas.Count() > 0)
潘栩锋's avatar
潘栩锋 committed
333
                    {
潘栩锋's avatar
潘栩锋 committed
334 335 336 337 338 339 340 341
                        mThkList.AddRange(
                            p.datas.Select(
                                a => new ThkCell()
                                {
                                    Thk = a.thk,
                                    Time = a.dt
                                }
                            ));
潘栩锋's avatar
潘栩锋 committed
342
                    }
潘栩锋's avatar
潘栩锋 committed
343 344 345

                    actionQueue.Next();
                }, null);
潘栩锋's avatar
潘栩锋 committed
346 347 348 349 350 351 352 353 354
        }

        void DownloadData_OK()
        {
            NBolts = mCurrBlowing.NBolts;
            OrgBoltNo = mCurrBlowing.OrgBoltNo;
            if (OrgBoltNo < 1)
                OrgBoltNo = 1;

潘栩锋's avatar
潘栩锋 committed
355
            //把下载的数据,都放入 旋转模拟器
潘栩锋's avatar
潘栩锋 committed
356 357 358 359 360
            mRDetectCore.Init(
                mCurrRDetect.RollPerimeter,
                mCurrRDetect.RenZiJiaPeriod,
                mCurrRDetect.FilmLength,
                mRollList,
潘栩锋's avatar
潘栩锋 committed
361 362 363 364 365 366 367
                mLimitList,
                mCurrRDetect.DefaultTime,
                mCurrRDetect.IsRPosMode,
                mCurrRDetect.OrgRPos,
                mCurrRDetect.RPosOfR,
                mRPosList,
                mCurrRDetect.Direction);
潘栩锋's avatar
潘栩锋 committed
368 369 370 371

            UpdateLimitValues();
            UpdateThicknessValues();
            UpdateVelocityValues(VelocityFilter);
潘栩锋's avatar
潘栩锋 committed
372 373 374 375
            if (mCurrRDetect.IsRPosMode)
            {
                UpdateAngleValues();
            }
潘栩锋's avatar
潘栩锋 committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

            UpdateThkList();
            UpdateFrames();

            Msg = "数据下载完成";
            IsDownloading = false;
            IsDataReady = true;
        }

        public void UpdateVelocityValues(int velocity_filter)
        {
            VelocityFilter = velocity_filter;
            var filter_half = TimeSpan.FromSeconds(VelocityFilter / 2.0);

            VelocityValues.Clear();

            for (int i = 0; i < mRollList.Count(); i++)
            {

潘栩锋's avatar
潘栩锋 committed
395 396
                var sysTime_min = mRollList[i].SysTime - filter_half;
                var sysTime_max = mRollList[i].SysTime + filter_half;
潘栩锋's avatar
潘栩锋 committed
397 398 399 400 401 402

                int i_begin = 0;
                int i_end = mRollList.Count() - 1;

                for (int j = i; j >= 0; j--)
                {
潘栩锋's avatar
潘栩锋 committed
403
                    if (mRollList[j].SysTime <= sysTime_min)
潘栩锋's avatar
潘栩锋 committed
404 405 406 407 408 409 410 411 412
                    {
                        //找到了 
                        i_begin = j;
                        break;
                    }
                }

                for (int j = i; j < mRollList.Count(); j++)
                {
潘栩锋's avatar
潘栩锋 committed
413
                    if (mRollList[j].SysTime >= sysTime_max)
潘栩锋's avatar
潘栩锋 committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
                    {
                        //找到了 
                        i_end = j;
                        break;
                    }
                }

                if (i_begin == i_end)
                {
                    i_begin = i;
                    i_end = i + 1;
                    if (i_end >= mRollList.Count())
                    {
                        i_begin = i - 1;
                        i_end = i;
                    }
                }

潘栩锋's avatar
潘栩锋 committed
432
                TimeSpan ts = mRollList[i_end].SysTime - mRollList[i_begin].SysTime;
潘栩锋's avatar
潘栩锋 committed
433 434 435 436 437
                double minute = 1.0 * ts.Ticks / TimeSpan.TicksPerMinute;
                double v = (i_end - i_begin) * mCurrRDetect.RollPerimeter / 1000 / minute;
                VelocityValues.Add(
                    new TimeValue()
                    {
潘栩锋's avatar
潘栩锋 committed
438
                        Time = mRollList[i].Time,
潘栩锋's avatar
潘栩锋 committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
                        Value = v
                    });
            }
            OnPropertyChanged(nameof(VelocityValues));

            Velocity1 = VelocityValues.Average(tv => tv.Value);


        }

        void UpdateThicknessValues()
        {
            ThicknessValues.Clear();
            foreach (var thkcell in mThkList)
            {
                ThicknessValues.Add(
                    new TimeValue()
                    {
                        Time = thkcell.Time,
                        Value = thkcell.Thk
                    });
            }
            OnPropertyChanged(nameof(ThicknessValues));
        }
        void UpdateLimitValues()
        {
            LimitValues.Clear();
            foreach (var limit in mLimitList)
            {
                DateTime dt = limit.dt_begin;
                if (dt == DateTime.MinValue)
                    dt = limit.dt_end;

                LimitValues.Add(new TimeValue() { Time = dt, Value = limit.no });
            }
            OnPropertyChanged(nameof(LimitValues));
        }

潘栩锋's avatar
潘栩锋 committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
        /// <summary>
        /// 通过旋转脉冲算出旋转角度曲线
        /// </summary>
        void UpdateAngleValues()
        {
            AngleValues.Clear();
            if (mRPosList != null)
            {
                foreach (var rPosData in mRPosList)
                {
                    var angle = mRDetectCore.GetAngleByGlobalRPos(rPosData.GlobalRPos);
                    AngleValues.Add(new TimeValue() { Time = rPosData.Time, Value = angle });
                }
            }
            OnPropertyChanged(nameof(AngleValues));
        }
潘栩锋's avatar
潘栩锋 committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

        void UpdateThkList()
        {
            for (int i = 0; i < mThkList.Count(); i++)
            {
                ThkCell c = mThkList[i];

                int ret = mRDetectCore.GetFilmInfo(
                    out FilmInfo filminfo,
                    mThkList[i].Time);
                if (ret > 0)//发生在辊信号的未来,把数据删除!!!!
                {
                    //异常
                    mThkList.RemoveRange(i, mThkList.Count() - i);
                    return;
                }
                if (filminfo.rotationCnt == 1)
                {

                }
                else if (filminfo.rotationCnt != 1)
                {

                }

                //计算相关性,只使用 膜边=0mm 模式
                c.Direction = filminfo.direction;
                c.Angles.Clear();
                c.Angles.Add(filminfo.angle1);
                c.RotationCnt = filminfo.rotationCnt;
                c.Velocity = filminfo.filmVelocity;
                c.InCV = filminfo.inCV;

            }
        }

        public void SetFilmLength(double filmLength)
        {
            FilmLength = filmLength;
            mRDetectCore.FilmLength = filmLength;
            UpdateThkList();
            UpdateFrames();
        }
        void UpdateFrames()
        {
            Frames = GetFrames();
        }
        List<Frame> GetFrames()
        {
            List<Frame> frames = new List<Frame>();
            int boltIndex = -1;
            List<double> datas = new List<double>();

            int roationCnt = -1;
            Frame frame = null;

            foreach (ThkCell c in mThkList)
            {
                int bi = (int)(NBolts * c.Angles[0] / 360);
                if (bi < 0) bi = 0;
                else if (bi >= NBolts) bi = NBolts - 1;


                int boltno = bi + OrgBoltNo;
                if (boltno > NBolts)
                    boltno -= NBolts;

                //添加到
                if ((roationCnt == -1) || (roationCnt != c.RotationCnt)
                    )
                {
                    roationCnt = c.RotationCnt;

                    frame = new Frame();
                    frame.RotationCnt = roationCnt;
                    frame.Direction = c.Direction;
                    frame.Thicks = new double[NBolts];
                    for (int j = 0; j < frame.Thicks.Count(); j++)
                        frame.Thicks[j] = double.NaN;

                    frames.Add(frame);
                    boltIndex = -1;
                }

                if ((boltIndex == -1) || (boltIndex != boltno - 1))
                {
                    datas.Clear();
                }

                datas.Add(c.Thk);
                boltIndex = boltno - 1;
                double avg = datas.AverageNoNull();

                if (!double.IsNaN(avg))
                    frame.Thicks[boltIndex] = avg;


            }

            //计算整体相关性,TODO
            //for (int i = 1; i < frames.Count; i++)
            //{
            //    frames[i].IsVaild = 1.0 * frames[i].Thicks.Count(t => !double.IsNaN(t)) / frames[i].Thicks.Count() > 0.3;
            //    if (frames[i].IsVaild)
            //    {
            //        //计算相关性
            //        frames[i].R = Misc.MyMath.Correl(frames[i].Thicks, frames[i - 1].Thicks);
            //    }

            //}
            return frames;
        }


        bool TestFilmLength(double filmlength, ref double max_r, ref double best_filmlength, out double r)
        {
            bool ret = false;
            mRDetectCore.FilmLength = filmlength;
            UpdateThkList();
            var frames = GetFrames();
            r = frames.Max(f => f.R);
            if (r > max_r)
            {
                max_r = r;
                best_filmlength = mRDetectCore.FilmLength;
                ret = true;
            }


            return ret;
        }

        public async void Cal(double begin_filmlength, double end_filmlength)
        {
            if (mRDetectCore.RotationCnt < 3)
            {
                Msg = "---警告--- 没法自动计算,旋转次数少于3次";
                return;
            }
            if (begin_filmlength < 3)
                begin_filmlength = 3;

            if (end_filmlength > 50)
                end_filmlength = 50;

            IsCaling = true;

            double max_r = 0;
            double best_filmlength = FilmLength;

            await Task.Factory.StartNew(() =>
            {
                for (double i = begin_filmlength; i < end_filmlength; i += 0.1)
                {
                    TestFilmLength(i, ref max_r, ref best_filmlength, out double r);
                    int progress = (int)((i - begin_filmlength) / (end_filmlength - begin_filmlength) * 100);
                    Msg = $"{progress:000}/100 {i:F1}m R={r:F3}  最合理[{best_filmlength:F1}m MaxR={max_r:F3}]";
                }
            });

            SetFilmLength(best_filmlength);
            Msg = $"最合理[{best_filmlength:F1}m MaxR={max_r:F3}]";
            IsCaling = false;
        }



        public event PropertyChangedEventHandler PropertyChanged;


        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        //string file_path = "bulkdb.json";
        public void Save(string file_path)
        {
            try
            {
                CalFilmLen360JsonDb param = new CalFilmLen360JsonDb()
                {
                    RollPerimeter = mRDetectCore.RollPerimeter,
                    RenZiJiaPeriod = mRDetectCore.RenZiJiaPeriod,
                    FilmLength = mRDetectCore.FilmLength,
潘栩锋's avatar
潘栩锋 committed
678
                    mRollList = mRDetectCore.mRollList.Select(r => new RollCell() { Time = r.Time, SysTime = r.SysTime }).ToList(),
潘栩锋's avatar
潘栩锋 committed
679
                    mLimitList = mRDetectCore.mLimitList,
潘栩锋's avatar
潘栩锋 committed
680 681 682 683 684
                    DefaultTime = mRDetectCore.DefaultTime,
                    IsRPosMode = mRDetectCore.IsRPosMode,
                    mRPosList = mRDetectCore.mRPosList,
                    RPosOfR = mRDetectCore.RPosOfR,
                    OrgRPos = mRDetectCore.OrgRPos,
潘栩锋's avatar
潘栩锋 committed
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

                    NBolts = NBolts,
                    OrgBoltNo = OrgBoltNo,
                    mADList = mThkList.Select(a => new ADSingle() { dt = a.Time, thk = a.Thk }).ToList()
                };

                File.WriteAllText(file_path, JsonConvert.SerializeObject(param, Formatting.Indented));
            }
            catch
            {
                //异常,没有json 编码失败

            }
        }
        public void Load(string file_path)
        {
            try
            {
                if (File.Exists(file_path))
                {
                    string json = File.ReadAllText(file_path);

                    var param = JsonConvert.DeserializeObject<CalFilmLen360JsonDb>(json);
                    Load(param);
                }
            }
            catch
            {
                //异常,没有json 解码失败

            }
        }

        void Load(CalFilmLen360JsonDb param)
        {
潘栩锋's avatar
潘栩锋 committed
720 721 722 723 724 725 726 727 728
            mLimitList.Clear();
            mRPosList.Clear();
            mRollList.Clear();

            mLimitList.AddRange(param.mLimitList);
            mRPosList.AddRange(param.mRPosList);
            mRollList.AddRange(param.mRollList);

            IsRPosMode = param.IsRPosMode;
潘栩锋's avatar
潘栩锋 committed
729 730 731 732 733 734

            mRDetectCore.Init(
                param.RollPerimeter,
                param.RenZiJiaPeriod,
                param.FilmLength,
                param.mRollList,
潘栩锋's avatar
潘栩锋 committed
735 736 737 738 739 740 741
                param.mLimitList,
                param.DefaultTime,
                param.IsRPosMode,
                param.OrgRPos,
                param.RPosOfR,
                param.mRPosList,
                param.Direction);
潘栩锋's avatar
潘栩锋 committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762

            NBolts = param.NBolts;
            OrgBoltNo = param.OrgBoltNo;
            if (OrgBoltNo < 1)
                OrgBoltNo = 1;
            FilmLength = param.FilmLength;

            mLimitList = param.mLimitList;
            mRollList = param.mRollList;

            mThkList = param.mADList.Select(
                a => new ThkCell()
                {
                    Thk = a.thk,//TODO 以后把 mADList 变成 mThkList
                    Time = a.dt
                }
                ).ToList();

            UpdateLimitValues();
            UpdateThicknessValues();
            UpdateVelocityValues(VelocityFilter);
潘栩锋's avatar
潘栩锋 committed
763
            UpdateAngleValues();
潘栩锋's avatar
潘栩锋 committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905

            UpdateThkList();
            UpdateFrames();

            IsDataReady = true;
        }

        public void Test()
        {
            CalFilmLen360JsonDb param = new CalFilmLen360JsonDb()
            {
                NBolts = 96,
                RollPerimeter = 314,
                FilmLength = 24,
                RenZiJiaPeriod = TimeSpan.FromMinutes(6.1),
                mADList = new List<ADSingle>(),
                mLimitList = new List<LimitCell>(),
                mRollList = new List<RollCell>()
            };

            #region 虚拟数据
            int[] thicks = new int[]{
                3518    ,
                3484    ,
                3634    ,
                3620    ,
                3579    ,
                3625    ,
                3635    ,
                3638    ,
                3635    ,
                3606    ,
                3611    ,
                3603    ,
                3590    ,
                3631    ,
                3646    ,
                3647    ,
                3710    ,
                3705    ,
                3607    ,
                3720    ,
                3680    ,
                3637    ,
                3659    ,
                3703    ,
                3860    ,
                3720    ,
                3615    ,
                3810    ,
                3772    ,
                3666    ,
                3614    ,
                3627    ,
                3618    ,
                3502    ,
                3575    ,
                3583    ,
                3615    ,
                3680    ,
                3730    ,
                3840    ,
                3927    ,
                3946    ,
                3923    ,
                3841    ,
                3797    ,
                3865    ,
                3810    ,
                3711    ,
                3754    ,
                3705    ,
                3723    ,
                3699    ,
                3723    ,
                3801    ,
                3786    ,
                3798    ,
                3907    ,
                3945    ,
                3828    ,
                3860    ,
                3846    ,
                3903    ,
                3871    ,
                3844    ,
                3759    ,
                3816    ,
                3886    ,
                3815    ,
                3789    ,
                3876    ,
                3942    ,
                3854    ,
                3842    ,
                3921    ,
                3846    ,
                3835    ,
                3886    ,
                3881    ,
                3820    ,
                3787    ,
                3730    ,
                3617    ,
                3650    ,
                3610    ,
                3536    ,
                3545    ,
                3564    ,
                3584    ,
                3580    ,
                3572    ,
                3571    ,
                3579    ,
                3620    ,
                3634    ,
                3484    ,
                3518    ,
                };

            //线速度 m/min
            double FilmVelocity = 20;
            TimeSpan Cool = TimeSpan.FromSeconds(2.3);
            //速度震荡周期 m
            double FV_Range = 2;
            TimeSpan DataLen = TimeSpan.FromMinutes(30);

            DateTime dt_begin = DateTime.Now;
            DateTime dt_end = dt_begin + DataLen;



            for (TimeSpan ts = TimeSpan.Zero; ts < DataLen; ts += TimeSpan.FromSeconds(1))
            {
                DateTime dt = dt_begin + ts;
                double min = ts.TotalMinutes;

                //速度以1/2旋转周期变化
                double v = FilmVelocity + Math.Sin(Math.PI * 2 * min / (param.RenZiJiaPeriod.TotalMinutes / 2)) * FV_Range;

                if (param.mRollList.Count() == 0)
                {
潘栩锋's avatar
潘栩锋 committed
906
                    param.mRollList.Add(new RollCell() { Time = dt });
潘栩锋's avatar
潘栩锋 committed
907 908 909
                }
                else
                {
潘栩锋's avatar
潘栩锋 committed
910
                    while ((dt - param.mRollList.Last().Time).TotalMinutes * v > (param.RollPerimeter / 1000))
潘栩锋's avatar
潘栩锋 committed
911 912 913
                    {
                        param.mRollList.Add(new RollCell()
                        {
潘栩锋's avatar
潘栩锋 committed
914
                            Time = param.mRollList.Last().Time + TimeSpan.FromMinutes((param.RollPerimeter / 1000) / v)
潘栩锋's avatar
潘栩锋 committed
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
                        });
                    }
                }

                //int n = ((int)(min / param.RenZiJiaPeriod.TotalMinutes)) % 2;

                double min2 = min - param.FilmLength / v + param.RenZiJiaPeriod.TotalMinutes * 2;

                //int n2 = ((int)(min2 / param.RenZiJiaPeriod.TotalMinutes)) % 2;
                double m = min2 % param.RenZiJiaPeriod.TotalMinutes;
                int idx = (int)(thicks.Count() * m / param.RenZiJiaPeriod.TotalMinutes);
                if (idx < 0) idx = 0;
                else if (idx > thicks.Count()) idx = thicks.Count() - 1;

                int thick;

                //if (n2 == 0)
                thick = thicks[idx];
                //else
                //    thick = thicks[thicks.Count() - 1 - idx];


                param.mADList.Add(new ADSingle
                {
                    dt = dt,
                    thk = thick
                });

                if (param.mLimitList.Count() == 0)
                {
                    if (dt >= dt_begin + param.RenZiJiaPeriod)
                    {
                        param.mLimitList.Add(new LimitCell()
                        {
                            dt_begin = dt_begin + param.RenZiJiaPeriod,
                            dt_end = dt_begin + param.RenZiJiaPeriod + Cool,
                            no = 0
                        });
                    }
                }
                else if (dt >= param.mLimitList.Last().dt_end + param.RenZiJiaPeriod)
                {
                    param.mLimitList.Add(new LimitCell()
                    {
                        dt_begin = param.mLimitList.Last().dt_end + param.RenZiJiaPeriod,
                        dt_end = param.mLimitList.Last().dt_end + param.RenZiJiaPeriod + Cool,
                        no = 0
                    });
                }
            }





            #endregion

            Load(param);
        }
    }

    public class CalFilmLen360JsonDb
    {
        /// <summary>
        /// 辊周长,单位mm
        /// </summary>
        public double RollPerimeter;
        /// <summary>
        /// 离开限位 到 撞下一个限位  的 旋转架转动时间, 需要初始值,以后测量出来的
        /// </summary>
        public TimeSpan RenZiJiaPeriod;
        /// <summary>
        /// 人字架到测厚仪膜长 单位m
        /// </summary>
        public double FilmLength;

        /// <summary>
        /// 辊信号列表
        /// </summary>
        public List<RollCell> mRollList;

        /// <summary>
        /// 转向信号列表
        /// </summary>
        public List<LimitCell> mLimitList;

        public List<ADSingle> mADList;

潘栩锋's avatar
潘栩锋 committed
1003 1004 1005 1006 1007 1008
        public DateTime DefaultTime;
        public bool IsRPosMode;
        public int OrgRPos;
        public int RPosOfR;
        public List<RPosData> mRPosList;

潘栩锋's avatar
潘栩锋 committed
1009 1010 1011
        public int NBolts;
        public int OrgBoltNo = 1;

潘栩锋's avatar
潘栩锋 committed
1012
        public Misc.DIRECTION Direction;
潘栩锋's avatar
潘栩锋 committed
1013 1014 1015

    }
}