GM_BlowingFix.cs 72.4 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;
using System.Threading;

using Misc;
using FlyADBase;
using FLY.Thick.BulkDataModule;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.Server;
using FLY.Thick.Blowing.IService;
using MathNet.Numerics.LinearAlgebra.Double;
using FObjBase;
19 20
using Newtonsoft.Json;
using FLY.Thick.Blowing.Common;
21
using FLY.Thick.Blowing.Server.Model;
潘栩锋's avatar
潘栩锋 committed
22 23 24

namespace FLY.Thick.Blowing.Server
{
潘栩锋's avatar
潘栩锋 committed
25 26 27
    /// <summary>
    /// 
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
28 29
    public class GM_BlowingFix : GM_Base, IBlowingFixService, ISaveToXml
    {
30 31
        NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

32 33
        const int MARKNO_PROFILE_ADD = 87;
        const int MARKNO_PROFILE_CHANGED = 88;
潘栩锋's avatar
潘栩锋 committed
34

潘栩锋's avatar
潘栩锋 committed
35 36
        HistoryDb mHistoryDb;
        BulkDb mBulkDb;
潘栩锋's avatar
潘栩锋 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

        #region 延时操作,markno
        const int MARKNO_SAVE = 1;
        const int MARKNO_CALFRAME = 2;
        #endregion

        /// <summary>
        /// 人字架角度 &amp; 膜走带位置检测
        /// </summary>
        public BlowingDetect mPDetect;
        #region ISaveToXml
        public string[] GetSavePropertyNames()
        {
            return new string[]{
                "ChannelCnt",
                "BPC",
                "OrgBoltNo",
                "EPCType",
                "SampleConsume",
                "SampleInterval",
                "BackEdgeWait",
                "IsBtnSelfHold",
                "IsUsedMap",
                "Map"
            };
        }
        #endregion

        #region IRenZiJiaService

        #region 分区设定

69

潘栩锋's avatar
潘栩锋 committed
70 71 72
        /// <summary>
        /// 加热通道数
        /// </summary>
73
        public int ChannelCnt { get; set; } = 88;
潘栩锋's avatar
潘栩锋 committed
74 75 76 77

        /// <summary>
        /// 分区数/加热通道数
        /// </summary>
78
        public int BPC { get; set; } = 1;
潘栩锋's avatar
潘栩锋 committed
79 80 81 82

        /// <summary>
        /// 风环螺丝数
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
83 84
        public int NBolts => ChannelCnt * BPC;

潘栩锋's avatar
潘栩锋 committed
85 86 87 88

        /// <summary>
        /// 复位时,探头测量的位置对应分区号
        /// </summary>
89
        public int OrgBoltNo { get; set; } = 1;
潘栩锋's avatar
潘栩锋 committed
90 91 92 93 94 95 96 97 98


        /// <summary>
        /// 第1个螺丝号
        /// </summary>
        public int BoltNo1st=1;
        /// <summary>
        /// 使用分区表
        /// </summary>
99
        public bool IsUsedMap { get; set; }
100 101 102 103 104 105 106
        bool IsNeedMap()
        {
            if (IsUsedMap && Map.Count() > 0)
                return true;
            else
                return false;
        }
潘栩锋's avatar
潘栩锋 committed
107 108 109 110 111 112 113 114 115 116 117
        private List<BoltMapCell> map = new List<BoltMapCell>();
        /// <summary>
        /// 分区表,必须保证不能为null
        /// </summary>
        public List<BoltMapCell> Map
        {
            get { return map; }
            set {
                //检测是否不一样
                if (value == null)
                    return;
潘栩锋's avatar
潘栩锋 committed
118
                if (checkNoSame_map(map,value))
潘栩锋's avatar
潘栩锋 committed
119
                {
潘栩锋's avatar
潘栩锋 committed
120 121 122
                    if (!checkParamErr_map(value))
                    {
                        IsUsedMap = false;
123
                        map = new List<BoltMapCell>();
潘栩锋's avatar
潘栩锋 committed
124 125 126 127 128
                    }
                    else
                    {
                        map = value;
                    }
潘栩锋's avatar
潘栩锋 committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                }
            }
        }

        #endregion


        #region IRenZiJiaFixService

        /// <summary>
        /// 追边开始按钮自锁
        /// </summary>
        public bool IsBtnSelfHold 
        {
            get {
                return mEPCA10.IsBtnSelfHold;
            }
            set {
                mEPCA10.IsBtnSelfHold = value;
            }
        }
150 151 152 153
        
        /// <summary>
        /// 温修对位硬件类型
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
154
        public RenZiJiaFixEPCType EPCType { get; set; } = RenZiJiaFixEPCType.Null;
潘栩锋's avatar
潘栩锋 committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 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 219 220 221 222 223 224 225
        /// <summary>
        /// 采样时间, 默认2s
        /// </summary>
        public TimeSpan SampleConsume
        {
            get { return mEPCA10.SampleConsume; }
            set
            {
                mEPCA10.SampleConsume = value;
            }
        }

        /// <summary>
        /// 温修间隔,默认45分钟
        /// </summary>
        public TimeSpan SampleInterval
        {
            get { return mEPCA10.SampleInterval; }
            set
            {
                mEPCA10.SampleInterval = value;
            }
        }

        /// <summary>
        /// 当前温修计时,大于温修间隔就会去温修
        /// </summary>
        public TimeSpan SampleTimer 
        {
            get {
                return mEPCA10.SampleTimer;
            }
        }



        /// <summary>
        /// 回到边界后,再等待一段时间,那就肯定找到边界了。 默认5s
        /// </summary>
        public TimeSpan BackEdgeWait 
        {
            get {
                return mEPCA10.BackEdgeWait;
            }
            set {
                mEPCA10.BackEdgeWait = value;
            }
        }

        /// <summary>
        /// 采样得到的样品AD
        /// </summary>
        public int SampleAD 
        {
            get
            {
                return mEPCA10.SampleAD;
            }           
        }
        /// <summary>
        /// 追边运行中!
        /// </summary>
        public bool EPCIsRunning 
        {
            get {
                return mEPCA10.IsRunning;
            }
        }

        #endregion

226 227 228
        /// <summary>
        /// 应用
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
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 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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 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
        public void Apply() 
        {
            
            Save();
        }
        public event RenZiJiaDataEventHandler DataEvent;
        #endregion

        #region EPC
        public delegate void EPCSampledEventHandler(int ad);

        class EPCA10:INotifyPropertyChanged
        {
            /// <summary>
            /// EPC-A10 警报 对应输入位
            /// </summary>
            const int in_epc_alarm_bit = 12;
            /// <summary>
            /// EPC启动 对应输入位
            /// </summary>
            const int in_epc_enable_bit = 6;
            /// <summary>
            /// EPC启动状态 对应输出位
            /// </summary>
            const int out_epc_isrunning_bit = 1;
            /// <summary>
            /// 控制EPC-A10 到温修点 对应输出位
            /// </summary>
            const int out_epc_sample_bit = 2;
            #region public成员

            public bool Enable { get; set; } = false;

            /// <summary>
            /// 温修时间,默认2秒
            /// </summary>
            public TimeSpan SampleConsume { get; set; } = TimeSpan.FromSeconds(2);


            /// <summary>
            /// 温修间隔,默认45分钟
            /// </summary>
            public TimeSpan SampleInterval { get; set; } = TimeSpan.FromMinutes(45);


            /// <summary>
            /// 当前温修计时,大于温修间隔就会去温修
            /// </summary>
            public TimeSpan SampleTimer { get; set; }= TimeSpan.Zero;

            /// <summary>
            /// 回到边界,再等待1段时间
            /// </summary>
            public TimeSpan BackEdgeWait { get; set; } = TimeSpan.FromSeconds(5);



            /// <summary>
            /// 追边开始按钮自锁
            /// </summary>
            public bool IsBtnSelfHold { get; set; } = true;



            /// <summary>
            /// 采样得到的样品AD
            /// </summary>
            public int SampleAD { get; set; } = -1;


            /// <summary>
            /// 追边运行中!
            /// </summary>
            public bool IsRunning { get; set; } = false;

            public event EPCSampledEventHandler Sampled;
            #endregion





            /// <summary>
            /// 采样开始时间
            /// </summary>
            /// <returns></returns>
            DateTime SampleBegin;

            /// <summary>
            /// 回去膜边开始时间
            /// </summary>
            DateTime GoBackBegin;

            /// <summary>
            /// 去采样开始时间
            /// </summary>
            DateTime GoSampleBegin;

            /// <summary>
            /// 去采样消耗的时间
            /// </summary>
            TimeSpan GoSampleConsume;



            IFlyAD mFlyAD;

            List<int> sampledata = new List<int>();
            TimeSpan sampleConsumeTimer = TimeSpan.Zero;
            public EPCA10(IFlyAD flyad)
            {
                mFlyAD = flyad;
                mFlyAD.IStatusChangedEvent += new IStatusChangedEventHandler(mFlyAD_IStatusChangedEvent);
                mFlyAD.TimeGridEvent += new FlyADBase.TimeGridEventHandler(mFlyAD_TimeGridEvent);
                this.PropertyChanged += EPCA10_PropertyChanged;
            }

            private void EPCA10_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                if (e.PropertyName == "Enable")
                {
                    //TODO, 错的!!!!
                    if(!Enable)
                        Stop();
                }
            }

            void mFlyAD_TimeGridEvent(object sender, FlyADBase.TimeGridEventArgs e)
            {
                if (!Enable)
                    return;

                if (State != STATE.SAMPLE)
                    return;

                TimeSpan ts = e.Ts;//准确

                DateTime dt = e.Time;//准确
                int[] data = e.Data;

                if (dt < SampleBegin)
                    return;

                sampledata.AddRange(data);
                sampleConsumeTimer += TimeSpan.FromTicks(ts.Ticks * data.Length);

                if (sampleConsumeTimer >= SampleConsume)
                {
                    int ad = Misc.MyMath.Avg(sampledata.ToArray());
                    if (Misc.MyBase.ISVALIDATA(ad))
                    {
                        SampleAD = ad;
                        if (Sampled != null)
                            Sampled(SampleAD);
                    }
                    else 
                    {
                        SampleAD = -1;
                    }
                    FinishSample();
                }
            }

            void mFlyAD_IStatusChangedEvent(object sender, IStatusChangedEventArgs e)
            {
                if (!Enable)
                    return;

                if (Misc.MyBase.CHECKBIT(e.IChanged, in_epc_enable_bit - 1))
                {
                    if (IsBtnSelfHold)
                    {
                        if (!Misc.MyBase.CHECKBIT(e.IStatus, in_epc_enable_bit - 1))
                        {
                            if (!IsRunning)
                            {
                                Start();
                            }
                        }
                        else
                        {
                            if (IsRunning)
                            {
                                Stop();
                            }
                        }
                    }
                    else 
                    {
                        if (!Misc.MyBase.CHECKBIT(e.IStatus, in_epc_enable_bit - 1))
                        {
                            if (!IsRunning)
                            {
                                Start();
                            }
                            else
                            {
                                Stop();
                            }
                        }
                    }
                }
            }
            enum STATE
            {
                /// <summary>
                /// 没动作
                /// </summary>
                STOP,
                /// <summary>
                /// 准备去温修
                /// </summary>
                START,
                /// <summary>
                /// 去温修过程中,记录耗费的时间 
                /// </summary>
                WAITFOR_SAMPLE,
                /// <summary>
                /// 已经到达温修点,温修中,采样 N秒
                /// </summary>
                SAMPLE,
                /// <summary>
                /// 等待 耗费的时间+N秒
                /// </summary>
                WAITFOR_EDGE,
                /// <summary>
                /// 测量中,等待N分钟,下次会进入START
                /// </summary>
                WAITFOR_NEXT,
            }
            /// <summary>
            /// 追边状态
            /// </summary>
            STATE State = STATE.STOP;

            public void Start()
            {
                if (!Enable)
                    return;
                State = STATE.START;
                SetRunning();
            }
            public void Stop()
            {
                if (!Enable)
                    return;
                State = STATE.STOP;
                ClearRunning();
                GoBack();
            }
            void SetRunning()
            {
                IsRunning = true;
                mFlyAD.SetOutput(
                    (UInt16)Misc.MyBase.BIT(out_epc_isrunning_bit - 1),
                    (UInt16)~Misc.MyBase.BIT(out_epc_isrunning_bit - 1));
            }
            void ClearRunning()
            {
                IsRunning = false;
                mFlyAD.SetOutput(
                    (UInt16)Misc.MyBase.BIT(out_epc_isrunning_bit - 1),
                    (UInt16)Misc.MyBase.BIT(out_epc_isrunning_bit - 1));
            }
            bool CheckIsRunning() 
            {
                if (!Misc.MyBase.CHECKBIT(mFlyAD.OStatus, out_epc_isrunning_bit - 1))
                    return true;
                else
                    return false;
            }
            void GoSample()
            {
                mFlyAD.SetOutput(
                    (UInt16)Misc.MyBase.BIT(out_epc_sample_bit - 1),
                    (UInt16)~Misc.MyBase.BIT(out_epc_sample_bit - 1));

            }
            void GoBack()
            {
                mFlyAD.SetOutput(
                    (UInt16)Misc.MyBase.BIT(out_epc_sample_bit - 1),
                    (UInt16)Misc.MyBase.BIT(out_epc_sample_bit - 1));
            }
            bool IsAlarm()
            {
                if (!Misc.MyBase.CHECKBIT(mFlyAD.IStatus, in_epc_alarm_bit - 1))
                    return true;
                else
                    return false;
            }
            public bool IsSampling()
            {

                switch (State)
                {
                    case STATE.WAITFOR_SAMPLE:
                    case STATE.SAMPLE:
                    case STATE.WAITFOR_EDGE:
                        return true;
                    default:
                        return false;
                }
            }

            void FinishSample()
            {
                State = STATE.WAITFOR_EDGE;
                GoBack();
                GoBackBegin = DateTime.Now;
            }
            /// <summary>
            /// 应该放在完成一幅图时触发
            /// </summary>
            public void CheckToSample()
            {
                if (!Enable)
                    return;
                if (!IsRunning)
                    return;

                if(SampleTimer >= SampleInterval)
                {
                    Start();
                }
            }

            /// <summary>
            /// 0.1秒触发一次
            /// </summary>
            public void OnPoll()
            {
                if (!Enable)
                    return;
                
                switch (State)
                {
                    case STATE.STOP:
                        if (CheckIsRunning())
                            ClearRunning();//这是不正常的,灭了它
                        break;
                    case STATE.START:
                        {
                            sampledata.Clear();
                            SampleTimer = TimeSpan.Zero;
                            GoSample();
                            GoSampleBegin = DateTime.Now;
                            
                            State = STATE.WAITFOR_SAMPLE;
                        } break;
                    case STATE.WAITFOR_SAMPLE:
                        {
                            if (IsAlarm())
                            {
                                GoSampleConsume = DateTime.Now - GoSampleBegin;
                                //已经到达
                                State = STATE.SAMPLE;
                                SampleBegin = DateTime.Now;
                                sampleConsumeTimer = TimeSpan.Zero;
                            }
                        } break;
                    case STATE.SAMPLE:
                        {

                        } break;
                    case STATE.WAITFOR_EDGE:
                        {
                            TimeSpan ts = DateTime.Now - GoBackBegin;
                            if (ts >= GoSampleConsume + BackEdgeWait)
                            {
                                //完成
                                State = STATE.WAITFOR_NEXT;
                            }
                        } break;
                    case STATE.WAITFOR_NEXT:
                        {
                            if (SampleTimer < SampleInterval)
                            {
                                SampleTimer += TimeSpan.FromSeconds(1);
                            }
                        }break;
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

        }
        EPCA10 mEPCA10;
        #endregion

        #region 吹膜定点解方程用
620

潘栩锋's avatar
潘栩锋 committed
621
        /// <summary>
622
        /// 测量模式, 决定是否解方程
潘栩锋's avatar
潘栩锋 committed
623
        /// </summary>
624 625
        public MeasureMode MMode { get; set; } = MeasureMode.Edge;

潘栩锋's avatar
潘栩锋 committed
626 627 628
        /// <summary>
        /// 膜宽 单位 mm
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
629 630
        public int FilmWidth { get; set; } = 1500;

潘栩锋's avatar
潘栩锋 committed
631 632 633 634

        /// <summary>
        /// 探头所在膜的横向位置 单位 mm
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
635 636 637 638 639 640 641 642 643 644 645
        public int FilmPosH { get; set; } = 70;

        /// <summary>
        /// 探头测量位置,袋折叠的宽度 单位 mm
        /// </summary>
        public int BagFold0 { get; set; } = 405;

        /// <summary>
        /// 另一端 袋折叠的宽度 单位 mm
        /// </summary>
        public int BagFold1 { get; set; } = 415;
潘栩锋's avatar
潘栩锋 committed
646 647 648 649 650 651

        #endregion


        IBulkDataServiceAdd mBulkData;
        DynArea mDynArea;
652
        BlowingFixProfileParam mProfileParam;
潘栩锋's avatar
潘栩锋 committed
653 654 655 656 657 658 659 660 661
        /// <summary>
        /// 当前正在处理的 ADList 里面的数据.No
        /// </summary>
        int mADListToDoNo;
        /// <summary>
        /// 处理放入filminfo 的数据.No
        /// </summary>
        int mADListToDoNo_ToFilmInfo;
        
潘栩锋's avatar
潘栩锋 committed
662 663 664
        /// <summary>
        /// 1s一个数据,
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
665 666 667

        RList<ADCell> mADList;
        
668

潘栩锋's avatar
潘栩锋 committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

        /// <summary>
        /// 1条方程
        /// </summary>
        class Formula
        {
            /// <summary>
            /// n元方程
            /// </summary>
            public int n;
            /// <summary>
            /// 方程 xi前面的 ai
            /// </summary>
            public double[] ai;
            /// <summary>
            /// 方程 y 值
            /// </summary>
            public double y;
            public Formula(int no_x)
            {
                int i;
                n = no_x;
                ai = new double[no_x];
                for (i = 0; i < n; i++) ai[i] = 0;
                y = 0;
            }
695
            public bool AddData(IEnumerable<int> idxs, int thick)
潘栩锋's avatar
潘栩锋 committed
696
            {
697 698 699 700 701 702
                foreach(int idx in idxs)
                    if ((idx < 0) || (idx >= n)) return false;

                foreach(int idx in idxs)
                    ai[idx]++;
                
潘栩锋's avatar
潘栩锋 committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
                y += thick;
                return true;
            }
        }
        class FRAME_INFO//一幅数据信息
        {
            
            public DateTime StartTime;//开始时间
            public DateTime EndTime;//结束时间
            public int NoStartAD;//在AD列表 处理的数据开始点
            public int NoEndAD;//在AD列表 处理的数据结束点
            public DIRECTION direction;//当前幅数据,方向
            public double velocity;//线速度 m/min
            public int rotationCnt;//人字架旋转次数
            /// <summary>
            /// 对应bulkdata 的数据, -1, 没有对应的bookmark,因为从来都没数据
            /// </summary>
            public int bookmark = -1;
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
            /// <summary>
            /// 对应数据库的 id
            /// </summary>
            public Int64? scandata_id = null;
            /// <summary>
            /// 分区数据 N*long
            /// </summary>
            public int[] frame;
            /// <summary>
            /// 经过分区表后的结果
            /// </summary>
            public int[] frame_map;
            /// <summary>
            /// 推送出去的数据,也是数据库中的数据
            /// </summary>
            public RenZiJiaDataEventArgs renZiJiaDataEventArgs;
            /// <summary>
            /// 推送出去的数据 被修改了
            /// </summary>
            public bool renZiJiaDataEventArgsHasChanged = false;


潘栩锋's avatar
潘栩锋 committed
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 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 906 907 908 909 910 911 912 913 914
            int frame_sum = 0;
            int frame_cnt = 0;

            static int virtual_nbolts = 88;
            static List<Formula> virtual_formulas = new List<Formula>();
            static void UpdateVirtualFormulas()
            {
                virtual_formulas.Clear();
                for (int i = 0; i < virtual_nbolts; i++)
                {
                    int boltindex1 = i;
                    int boltindex2 = i + 1;
                    if (boltindex2 >= virtual_nbolts)
                        boltindex2 = 0;

                    Formula fm = new Formula(virtual_nbolts);
                    fm.ai[boltindex1] = 1;
                    fm.ai[boltindex2] = -1;
                    virtual_formulas.Add(fm);
                }
            }
            static FRAME_INFO()
            {
                UpdateVirtualFormulas();
            }
            public void SetNBolt(int nbolts) 
            {
                frame = new int[nbolts];
                appeared = new bool[nbolts];
                for (int i = 0; i < nbolts; i++)
                {
                    ClearFrameValue(i);
                }
            }

            void ClearFrameValue(int index) 
            {
                frame[index] = Misc.MyBase.NULL_VALUE;
                appeared[index] = false;
            }
            public void Clear() 
            {
                for (int i = 0; i < frame.Count(); i++)
                {
                    ClearFrameValue(i);
                }
                IsValid = true;
                HasChanged = true;
                index_last = -1;
                frame_cnt = 0;
                frame_sum = 0;
                mFormulas.Clear();
                last_solvedt = DateTime.MinValue;
                isTimeToSolve = false;
                canSolve = false;
            }
            
            public bool HasChanged = false;//数据被改变了!!!,是时候更新bulkdata
            public bool IsValid = true;//是合法数据
            /// <summary>
            /// 提交,完成一次操作。清除状态
            /// </summary>
            public void Submit() 
            {
                HasChanged = false;
            }
            public void CheckForValid() 
            {
                if (rotationCnt == 0)//旋转次数=0,肯定是不完整的数据
                {
                    HasChanged = true;
                    IsValid = false;
                    return;
                }
                //所有的数据都应该大于10
                for (int i = 0; i < frame.Count(); i++) 
                {
                    if (Misc.MyBase.ISVALIDATA(frame[i])) 
                    {
                        if (frame[i] < 1000)
                        {
                            HasChanged = true;
                            IsValid = false;
                            return;
                        }
                    }
                }
            }
            /// <summary>
            /// 上一次解方程的时间
            /// </summary>
            DateTime last_solvedt = DateTime.MinValue;
            /// <summary>
            /// 是时候解方程
            /// </summary>
            bool isTimeToSolve = false;
            /// <summary>
            /// 方程多了,能解方程
            /// </summary>
            bool canSolve = false;
            /// <summary>
            /// 每个区号,是否有出现
            /// </summary>
            bool[] appeared;
            /// <summary>
            /// 方程组
            /// </summary>
            List<Formula> mFormulas = new List<Formula>();
            public void SolveFormula()
            {
                if (!canSolve)//没有新方程,没法解
                    return;

                int nbolts = frame.Count();
                if (virtual_nbolts!= nbolts)
                {
                    virtual_nbolts = nbolts;
                    UpdateVirtualFormulas();
                }
                //TODO 能否解得方程检查
                int nrow = mFormulas.Count() + virtual_formulas.Count();
                
                //if (nrow < NBolts) return;

                //Console.WriteLine("Total get {0} formular", nrow);
                //转换为 DenseMatrix 与 DenseVector
                DenseMatrix matrixA = new DenseMatrix(nrow, nbolts);
                DenseMatrix matrixAT = new DenseMatrix(nbolts, nrow);
                DenseVector vectorB = new DenseVector(nrow);
                DenseVector vectorATB = new DenseVector(nbolts);

                int rowindex = 0;
                foreach (Formula fm in mFormulas)
                {
                    matrixA.SetRow(rowindex, fm.ai);
                    matrixAT.SetColumn(rowindex, fm.ai);
                    vectorB[rowindex] = fm.y;
                    rowindex++;
                }
                foreach (Formula fm in virtual_formulas)
                {
                    matrixA.SetRow(rowindex, fm.ai);
                    matrixAT.SetColumn(rowindex, fm.ai);
                    vectorB[rowindex] = fm.y;
                    rowindex++;
                }
                DenseMatrix ATA = (DenseMatrix)matrixA.TransposeThisAndMultiply(matrixA);
                DenseVector ATB = (DenseVector)matrixA.TransposeThisAndMultiply(vectorB);

                DenseVector X = (DenseVector)ATA.Solve(ATB);

                for (int i = 0; i < nbolts; i++)
                {
                    int d = (appeared[i]) ? (int)X[i] : Misc.MyBase.NULL_VALUE;
                    frame[i] = d;// (int)X[i];
                }
                HasChanged = true;

                isTimeToSolve = false;
                canSolve = false;
            }

            /// <summary>
            /// 检查是否需要更新
            /// </summary>
            public void CheckToSolveFormula()
            {
                if(isTimeToSolve)
                {
                    SolveFormula();
                }
            }
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


            int index_last = -1;
            public void SetFrameValue(int index, int value, DateTime dt)
            {
                int nbolts = frame.Count();

                if (index >= nbolts)
                    return;//异常
                else if (index < 0)
                    return;//异常
                else if (!Misc.MyBase.ISVALIDATA(value))
                    return;//异常

                if (index_last != index)
                {
                    index_last = index;
                    HasChanged = true;
                    frame_cnt = 0;
                    frame_sum = 0;
                }

                frame_sum += value;
                frame_cnt++;

                frame[index] = frame_sum / frame_cnt;
            }
潘栩锋's avatar
潘栩锋 committed
942 943 944
            /// <summary>
            /// 解方程方式更新 frame
            /// </summary>
945
            /// <param name="indexs"></param>
潘栩锋's avatar
潘栩锋 committed
946 947
            /// <param name="value"></param>
            /// <param name="dt"></param>
948
            public void SetFrameValue(IEnumerable<int> indexs, int value, DateTime dt)
潘栩锋's avatar
潘栩锋 committed
949 950
            {
                int nbolts = frame.Count();
951 952 953 954 955 956 957
                foreach (int index in indexs)
                {
                    if (index >= nbolts)
                        return;//异常
                    else if (index < 0)
                        return;//异常
                }
潘栩锋's avatar
潘栩锋 committed
958

959
                if (!Misc.MyBase.ISVALIDATA(value))
潘栩锋's avatar
潘栩锋 committed
960 961 962
                    return;//异常
                
                Formula fm = new Formula(nbolts);
963
                fm.AddData(indexs, value);
潘栩锋's avatar
潘栩锋 committed
964
                mFormulas.Add(fm);
965 966 967

                foreach (int index in indexs)
                    appeared[index] = true;
潘栩锋's avatar
潘栩锋 committed
968 969 970 971
                
                canSolve = true;

                //5秒解一次方程
972
                if ((index_last != indexs.First()) && ((dt - last_solvedt) >= TimeSpan.FromSeconds(5)))
潘栩锋's avatar
潘栩锋 committed
973
                {
974
                    index_last = indexs.First();
潘栩锋's avatar
潘栩锋 committed
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 1003 1004 1005 1006 1007 1008 1009 1010
                    last_solvedt = dt;
                    isTimeToSolve = true;
                }
            }
        }
        RList<FRAME_INFO> mFrameInfoList;


        AD2ThickHandler AD2Thick;

        public event EPCSampledEventHandler EPCSampled 
        {
            add {
                mEPCA10.Sampled += value;
            }
            remove {
                mEPCA10.Sampled -= value;              
            }
        }

        void checkParamErr() 
        {
            if (ChannelCnt < 20 || ChannelCnt > 160)
            {
                ChannelCnt = 44;
            }
            
            if (BPC < 1 || BPC > 4)
            {
                BPC = 2;
            }

            if (OrgBoltNo < 1 || OrgBoltNo > NBolts)
            {
                OrgBoltNo = 1;
            }
潘栩锋's avatar
潘栩锋 committed
1011 1012

            if (!checkParamErr_map(Map))
潘栩锋's avatar
潘栩锋 committed
1013
            {
潘栩锋's avatar
潘栩锋 committed
1014
                IsUsedMap = false;
1015
                Map = new List<BoltMapCell>();
潘栩锋's avatar
潘栩锋 committed
1016 1017 1018 1019 1020 1021 1022
            }
        }

        /// <summary>
        /// 检测通过,返回true
        /// </summary>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
1023
        bool checkParamErr_map(List<BoltMapCell> mapCells)
潘栩锋's avatar
潘栩锋 committed
1024 1025 1026 1027 1028 1029 1030 1031
        {
            bool map_err = false;
            bool g_o = false;
            bool g_n = false;
            //Map 检测
            //OldNo,NewNo不能 <1 >NBolts
            //Map OldNo 必须从小到大环形排列
            //Map NewNo 必须从小到大环形排列
潘栩锋's avatar
潘栩锋 committed
1032
            for (int i = 0; i < mapCells.Count(); i++)
潘栩锋's avatar
潘栩锋 committed
1033
            {
1034 1035
                var mapCell = mapCells[i];
                if (mapCell.NewNo < 1 || mapCell.NewNo > NBolts)
潘栩锋's avatar
潘栩锋 committed
1036 1037 1038 1039
                {
                    map_err = true;
                    break;
                }
1040
                if (mapCell.OldNo < 1 || mapCell.OldNo > NBolts)
潘栩锋's avatar
潘栩锋 committed
1041 1042 1043 1044 1045 1046
                {
                    map_err = true;
                    break;
                }

                int i_next = i + 1;
潘栩锋's avatar
潘栩锋 committed
1047
                if (i_next >= mapCells.Count())
潘栩锋's avatar
潘栩锋 committed
1048 1049
                    i_next = 0;

1050 1051
                var mapCell_next = mapCells[i_next];
                if (mapCell.OldNo >= mapCell_next.OldNo)
潘栩锋's avatar
潘栩锋 committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
                {
                    if (!g_o)
                        g_o = true;
                    else
                    {
                        map_err = true;
                        break;
                    }

                }

1063
                if (mapCell.NewNo >= mapCell_next.NewNo)
潘栩锋's avatar
潘栩锋 committed
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
                {
                    if (!g_n)
                        g_n = true;
                    else
                    {
                        map_err = true;
                        break;
                    }

                }
            }
1075

潘栩锋's avatar
潘栩锋 committed
1076 1077 1078 1079 1080 1081 1082 1083 1084
            if (map_err)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
1085
        bool checkNoSame_map(List<BoltMapCell> mapCells1, List<BoltMapCell> mapCells2)
潘栩锋's avatar
潘栩锋 committed
1086
        {
1087
            if (mapCells1.Count() != mapCells2.Count())
潘栩锋's avatar
潘栩锋 committed
1088
                return true;
1089
            for (int i = 0; i < mapCells1.Count(); i++)
潘栩锋's avatar
潘栩锋 committed
1090
            {
1091 1092 1093 1094
                var mapCell1 = mapCells1[i];
                var mapCell2 = mapCells2[i];

                if (mapCell1.NewNo != mapCell2.NewNo)
潘栩锋's avatar
潘栩锋 committed
1095
                    return true;
1096
                if (mapCell1.OldNo != mapCell2.OldNo)
潘栩锋's avatar
潘栩锋 committed
1097 1098 1099 1100
                    return true;
            }
            return false;
        }
1101

潘栩锋's avatar
潘栩锋 committed
1102 1103 1104 1105
        static GM_BlowingFix()
        {
            Misc.SaveToXmlHepler.Regist(typeof(BoltMapCell));
        }
潘栩锋's avatar
潘栩锋 committed
1106 1107 1108 1109 1110

        /// <summary>
        /// 
        /// </summary>
        /// <param name="flyad"></param>
潘栩锋's avatar
潘栩锋 committed
1111 1112 1113 1114 1115 1116 1117
        public GM_BlowingFix(FlyAD7 flyad)
            :base(flyad)
        {
            GMState = CTRL_STATE.FIX;
            
            mPDetect = new BlowingDetect();
            mEPCA10 = new EPCA10(flyad);
潘栩锋's avatar
潘栩锋 committed
1118
            
潘栩锋's avatar
潘栩锋 committed
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
            mPDetect.Load();
            Load();


            //检测参数
            mPDetect.CheckParamErr();
            checkParamErr();

            mEPCA10.Enable = (EPCType == RenZiJiaFixEPCType.EPCA10);
            mEPCA10.PropertyChanged += new PropertyChangedEventHandler(mEPCA10_PropertyChanged);
            InitList();

            PropertyChanged += new PropertyChangedEventHandler(RenZiJiaFix_PropertyChanged);
            
            mPDetect.FilmInfoChangedEvent += new BlowingDetect.FilmInfoChangedEventHandler(mPDetect_FilmInfoChangedEvent);
            mPDetect.ClearEvent += new BlowingDetect.ClearEventHandler(mPDetect_ClearEvent);


            mFlyAD.TimeGridEvent += new FlyADBase.TimeGridEventHandler(flyad_TimeGridEvent);

            mPDetect.Init(flyad);
        }

        void mEPCA10_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "SampleConsume") ||
                (e.PropertyName == "SampleInterval") ||
                (e.PropertyName == "SampleTimer") ||
                (e.PropertyName == "BackEdgeWait") ||
                (e.PropertyName == "IsBtnSelfHold"))
            {
                NotifyPropertyChanged(e.PropertyName);
            }
            else if (e.PropertyName == "IsRunning")
            {
                NotifyPropertyChanged("EPCIsRunning");
            }
        }


1159 1160 1161
        /// <summary>
        /// 作废的功能
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1162 1163 1164 1165 1166 1167
        public void EPCStart() 
        {
            if(EPCType == RenZiJiaFixEPCType.EPCA10)
                mEPCA10.Start();
        }

1168 1169 1170
        /// <summary>
        /// 作废的功能
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
        public void EPCStop()
        {
            if (EPCType == RenZiJiaFixEPCType.EPCA10)
                mEPCA10.Stop();
        }

        void mPDetect_ClearEvent(object sender)
        {
            //没信号,清空所有数据!!!
        }
        /// <summary>
        /// 从1幅数据,获取头部,尾部,为于 ADList 的位置
        /// </summary>
        /// <param name="frameinfo"></param>
        /// <param name="index_start"></param>
        /// <param name="index_end"></param>
1187
        /// <returns>false 数据不存在</returns>
潘栩锋's avatar
潘栩锋 committed
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
        bool GetFilmInfoADIndex(FRAME_INFO frameinfo, out int index_start, out int index_end) 
        {
            index_start = mADList.No2Index(frameinfo.NoStartAD);
            
            index_end = mADList.No2Index(frameinfo.NoEndAD);
            
            if ((index_start < 0)||(index_end <0)||
                (index_start >= mADList.Count)||(index_end >= mADList.Count))
            {
                //数据已经不存在,删除
                return false;
            }
            return true;
        }

        void mPDetect_FilmInfoChangedEvent(object sender, BlowingDetect.FilmInfoChangedEventArgs e)
        {
1205 1206
            var ret = ADListFastSearchIndex(e.Time, out int idx);
            switch (ret) 
潘栩锋's avatar
潘栩锋 committed
1207
            {
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
                case ADListFastSearchIndexResult.Before://发送在过去
                    {
                        mADListToDoNo = mADList.Index2No(0);
                        mADListToDoNo_ToFilmInfo = mADListToDoNo;
                    }
                    break;
                case ADListFastSearchIndexResult.Now://发生在列表时间内
                    {
                        mADListToDoNo = mADList.Index2No(idx);
                        mADListToDoNo_ToFilmInfo = mADListToDoNo;
                    }
                    break;
                case ADListFastSearchIndexResult.Future:
                    {
                        //未来的,不处理
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
            }
            
        }

        void RenZiJiaFix_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "NBolts")
            {
                //所有数据删除
                mFrameInfoList.Clear();

            }
            else if (e.PropertyName == "EPCType")
            {
                mEPCA10.Enable = (EPCType == RenZiJiaFixEPCType.EPCA10);
            }
            else if (e.PropertyName == "OrgBoltNo")
            {
                //TODO 只处理3幅图
                int idx = mFrameInfoList.Count - 3;
                if (idx < 0)
                    idx = 0;

                if (idx >= mFrameInfoList.Count)
                    return;
                mADListToDoNo_ToFilmInfo = mFrameInfoList[idx].NoStartAD;
            }
            else if ((e.PropertyName == "FilmWidth") || (e.PropertyName == "FilmPosH") ||
                (e.PropertyName == "SolveEnable"))
            {
                //重新计算当前幅数据
                UpdateCurrFrame();
            }
        }

潘栩锋's avatar
潘栩锋 committed
1260 1261 1262 1263 1264 1265 1266 1267 1268
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bulkdata"></param>
        /// <param name="func_ad2thick"></param>
        /// <param name="dynarea"></param>
        /// <param name="profileParam"></param>
        /// <param name="historyDB"></param>
        /// <param name="bulkDB"></param>
1269 1270 1271
        public void Init(
            IBulkDataServiceAdd bulkdata, AD2ThickHandler func_ad2thick, DynArea dynarea,
            BlowingFixProfileParam profileParam,
潘栩锋's avatar
潘栩锋 committed
1272
            HistoryDb historyDB, BulkDb bulkDB
1273
            )
潘栩锋's avatar
潘栩锋 committed
1274 1275 1276 1277
        {
            AD2Thick = func_ad2thick;
            mBulkData = bulkdata;
            mDynArea = dynarea;
1278
            mProfileParam = profileParam;
潘栩锋's avatar
潘栩锋 committed
1279 1280
            mHistoryDb = historyDB;
            mBulkDb = bulkDB;
潘栩锋's avatar
潘栩锋 committed
1281

1282 1283 1284 1285 1286 1287 1288
            Misc.BindingOperations.SetBinding(profileParam, "MMode", this, "MMode");
            Misc.BindingOperations.SetBinding(profileParam, "FilmWidth", this, "FilmWidth");
            Misc.BindingOperations.SetBinding(profileParam, "FilmPosH", this, "FilmPosH");
            Misc.BindingOperations.SetBinding(profileParam, "BagFold0", this, "BagFold0");
            Misc.BindingOperations.SetBinding(profileParam, "BagFold1", this, "BagFold1");

            Misc.BindingOperations.SetBinding(mPDetect, "FilmVelocity", mDynArea, "FilmVelocity");
潘栩锋's avatar
潘栩锋 committed
1289

1290
            Misc.BindingOperations.SetBinding(this, "NBolts", () =>
潘栩锋's avatar
潘栩锋 committed
1291
            {
1292
                mBulkData.NBolts = NBolts;
潘栩锋's avatar
潘栩锋 committed
1293

1294 1295
                mDynArea.NBolts = NBolts;
                mDynArea.FirstBoltNo = 1;
潘栩锋's avatar
潘栩锋 committed
1296

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
                mDynArea.DataBoltNoBegin = 1;
                mDynArea.DataBoltNoEnd = NBolts;
                mDynArea.ScanBoltNoBegin = 1;
                mDynArea.ScanBoltNoEnd = NBolts;
            });
            
            profileParam.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == "K")
                {
                    //重新计算当前幅数据
                    UpdateCurrFrame();
潘栩锋's avatar
潘栩锋 committed
1309 1310
                }
            };
1311
            mDynArea.PropertyChanged += (s, e) =>
潘栩锋's avatar
潘栩锋 committed
1312
            {
1313 1314 1315 1316 1317
                if (e.PropertyName == "SampleAD")
                {
                    //重新计算当前幅数据
                    UpdateCurrFrame();
                }
1318
            };
潘栩锋's avatar
潘栩锋 committed
1319 1320 1321

            //记录调试数据
            MarkDebugInit();
潘栩锋's avatar
潘栩锋 committed
1322 1323
        }

1324 1325

        #region 记录测试数据
潘栩锋's avatar
潘栩锋 committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
        DateTime markdebug_dt = DateTime.MinValue;
        void MarkDebugInit()
        {
            if (markdebug_dt == DateTime.MinValue)
                markdebug_dt = DateTime.Now;

            //启动1分钟定时器
            FObjBase.PollModule.Current.Poll_Config(
                FObjBase.PollModule.POLL_CONFIG.ADD,
                ()=>
                {
1337
                    mPDetect.GetSignList(markdebug_dt, (asyncState, retData) =>
潘栩锋's avatar
潘栩锋 committed
1338
                    {
1339 1340
                        var reponse = retData as GetSignListReponse;
                        var cells = reponse.datas;
潘栩锋's avatar
潘栩锋 committed
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
                        if (cells.Count > 0)
                        {
                            mHistoryDb.AddSignData(
                                new Lc_Sign()
                                {
                                    Time = cells.First().Time,
                                    Signs = cells.ToArray()
                                });
                        }
                    }, null);
                    mPDetect.GetRollList(markdebug_dt, (asyncState, retData) =>
                    {
1353 1354
                        var reponse = retData as GetRollListReponse;
                        var cells = reponse.datas;
潘栩锋's avatar
潘栩锋 committed
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
                        if (cells.Count > 0)
                        {
                            mHistoryDb.AddRollData(
                                new Lc_Roll()
                                {
                                    Time = cells.First().dt,
                                    Signs = cells.ToArray()
                                });
                        }
                    }, null);
                    this.GetADList(markdebug_dt, (asyncState, retData) =>
                    {
1367 1368
                        var reponse = retData as GetADListReponse;
                        var cells = reponse.datas;
潘栩锋's avatar
潘栩锋 committed
1369 1370
                        if (cells.Count > 0)
                        {
1371
                            var thicks = ToRealThicks(cells.Select(c => AD2Thick(c.thick)));
潘栩锋's avatar
潘栩锋 committed
1372 1373
                            mHistoryDb.AddTrendData(
                                new Lc_TrendData(){
1374
                                    Time = cells.First().dt,
潘栩锋's avatar
潘栩锋 committed
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
                                    K = mProfileParam.K,
                                
                                    Thicks = thicks
                                });
                        }
                    }, null);
                    markdebug_dt = DateTime.Now;
                },
                TimeSpan.FromMinutes(1));
        }
1385 1386
        #endregion

潘栩锋's avatar
潘栩锋 committed
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
        void UpdateCurrFrame() 
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
                new FObjBase.PollModule.PollHandler(delegate()
                {
                    if (mFrameInfoList.Count > 0) 
                    {
                        mADListToDoNo_ToFilmInfo = mFrameInfoList.Last().NoStartAD;
                    }

                }), this, MARKNO_CALFRAME);
        } 


        
潘栩锋's avatar
潘栩锋 committed
1402 1403 1404
        TimeSpan datOf1s_ts = TimeSpan.Zero;
        TimeSpan datOf1s_interval = TimeSpan.FromSeconds(1);
        List<int> datOf1s = new List<int>();
潘栩锋's avatar
潘栩锋 committed
1405

潘栩锋's avatar
潘栩锋 committed
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
        void flyad_TimeGridEvent(object sender, FlyADBase.TimeGridEventArgs e)
        {
            bool issample = false;
            if (EPCType == RenZiJiaFixEPCType.EPCA10)
            {
                if (mEPCA10.IsSampling())
                {
                    issample = true;
                }
            }

            TimeSpan ts = e.Ts;//准确
 
            DateTime dt = e.Time;//准确
            int[] data = e.Data;

            DateTime datOf100ms_dt = DateTime.MinValue;
            for(int i=0;i<data.Length;i++)
            {
                if (issample)//采样中,数据都是无效的
                {
潘栩锋's avatar
潘栩锋 committed
1427
                    datOf1s.Add(Misc.MyBase.NULL_VALUE);
潘栩锋's avatar
潘栩锋 committed
1428 1429 1430
                }
                else if (data[i] > UInt16.MaxValue)
                {
潘栩锋's avatar
潘栩锋 committed
1431
                    datOf1s.Add(Misc.MyBase.NULL_VALUE);
潘栩锋's avatar
潘栩锋 committed
1432 1433 1434
                }
                else if (data[i] < 0)
                {
潘栩锋's avatar
潘栩锋 committed
1435
                    datOf1s.Add(Misc.MyBase.NULL_VALUE);
潘栩锋's avatar
潘栩锋 committed
1436 1437 1438
                }
                else
                {
潘栩锋's avatar
潘栩锋 committed
1439
                    datOf1s.Add(data[i]);
潘栩锋's avatar
潘栩锋 committed
1440 1441
                }

潘栩锋's avatar
潘栩锋 committed
1442
                datOf1s_ts = datOf1s_ts + ts;
潘栩锋's avatar
潘栩锋 committed
1443 1444 1445 1446 1447 1448

                if (datOf100ms_dt == DateTime.MinValue)
                    datOf100ms_dt = dt;
                else 
                    datOf100ms_dt = datOf100ms_dt + ts;
                
潘栩锋's avatar
潘栩锋 committed
1449
                if (datOf1s_ts>= datOf1s_interval)//1s 一个数据
潘栩锋's avatar
潘栩锋 committed
1450
                {
潘栩锋's avatar
潘栩锋 committed
1451
                    datOf1s_ts = datOf1s_ts - datOf1s_interval;
潘栩锋's avatar
潘栩锋 committed
1452 1453 1454

                    int avgad = Misc.MyBase.NULL_VALUE;

潘栩锋's avatar
潘栩锋 committed
1455
                    if (datOf1s.Count() > 0)
潘栩锋's avatar
潘栩锋 committed
1456
                    {
潘栩锋's avatar
潘栩锋 committed
1457 1458
                        avgad = Misc.MyMath.Avg(datOf1s.ToArray());
                        datOf1s.Clear();
潘栩锋's avatar
潘栩锋 committed
1459 1460 1461
                    }
                    else if (mADList.Count() > 0)
                    {
潘栩锋's avatar
潘栩锋 committed
1462
                        avgad = mADList.Last().Ad;
潘栩锋's avatar
潘栩锋 committed
1463 1464
                    }

潘栩锋's avatar
潘栩锋 committed
1465

潘栩锋's avatar
潘栩锋 committed
1466 1467 1468
                    mADList.RAdd(
                        new ADCell()
                        {
潘栩锋's avatar
潘栩锋 committed
1469 1470
                            Time = datOf100ms_dt,
                            Ad = avgad
潘栩锋's avatar
潘栩锋 committed
1471
                        });
潘栩锋's avatar
潘栩锋 committed
1472
                    
潘栩锋's avatar
潘栩锋 committed
1473 1474 1475
                }
            }
        }
潘栩锋's avatar
潘栩锋 committed
1476

潘栩锋's avatar
潘栩锋 committed
1477 1478 1479 1480 1481

        private void InitList()//初始化列表
        {
            if (mADList == null)
            {
潘栩锋's avatar
潘栩锋 committed
1482
                mADList = new RList<ADCell>(4000);
潘栩锋's avatar
潘栩锋 committed
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
                mADListToDoNo = mADList.GetLastNo();
                mADListToDoNo_ToFilmInfo = mADListToDoNo;
            }   
            if(mFrameInfoList==null)
		        mFrameInfoList=new RList<FRAME_INFO>(6);
        }


        
        //复位
        void Clear()
        {
            if(mADList!=null)
		        mADList.Clear();
            if (mFrameInfoList != null)
            {
                mFrameInfoList.Clear();
            }
        }

        int Angle2BoltIndex(double angle)
        {
            if(angle<0)
            {
                angle+=360;
            }

            int boltindex = (int)(OrgBoltNo - BoltNo1st +angle*NBolts/360);
            if (boltindex >= NBolts)
            {
                boltindex -= NBolts;
            }
            else if (boltindex < 0)//不应该发生,可能是 OrgBoltNo 出错了。
            {
                boltindex += NBolts;
            }
            return boltindex;
        }
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536

        enum ADListFastSearchIndexResult 
        {
            /// <summary>
            /// 时间点在列表以前;
            /// </summary>
            Before = -1,
            /// <summary>
            /// 时间点在列表中;
            /// </summary>
            Now = 0,
            /// <summary>
            /// 时间点在列表的未来;
            /// </summary>
            Future = 1
        }
潘栩锋's avatar
潘栩锋 committed
1537
        /// <summary>
1538 1539 1540
        /// 找到在 dt 前最近的 ADList 序号;
        /// 当ADList 没有数据,返回 Future;
        /// 已知AD列表每个数据之间的时间差,快速推断结果;
潘栩锋's avatar
潘栩锋 committed
1541 1542 1543 1544
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="index"></param>
        /// <returns></returns>
1545
        ADListFastSearchIndexResult ADListFastSearchIndex(DateTime dt, out int index)
潘栩锋's avatar
潘栩锋 committed
1546 1547 1548
        {
            index = 0;

1549 1550
            if (mADList.Count <= 0)//列表没有数据, 只能当它在未来
                return ADListFastSearchIndexResult.Future;
潘栩锋's avatar
潘栩锋 committed
1551 1552


1553 1554 1555 1556
            if (dt > mADList.Last().Time)
                return ADListFastSearchIndexResult.Future;
            else if (dt < mADList.First().Time)
                return ADListFastSearchIndexResult.Before;
潘栩锋's avatar
潘栩锋 committed
1557 1558
            else
            {
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569

                int offset = (int)((mADList.Last().Time - dt).TotalSeconds / datOf1s_interval.TotalSeconds);
                index = mADList.Count - offset - 1;//大约在这个位置附近

                //安全限制
                if (index >= mADList.Count)
                    index = mADList.Count() - 1;
                else if (index < 0)
                    index = 0;
                
                if (mADList[index].Time == dt)
潘栩锋's avatar
潘栩锋 committed
1570
                {
1571
                    //已经找到就是它!!!!
潘栩锋's avatar
潘栩锋 committed
1572
                }
1573
                else if (mADList[index].Time > dt)
潘栩锋's avatar
潘栩锋 committed
1574
                {
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
                    while (true)
                    {
                        index--;
                        if (index <= 0)
                        {
                            index = 0;//最前面一个了
                            break;
                        }
                        if (mADList[index].Time <= dt) 
                        {
                            //找到了
                            break;
                        }
潘栩锋's avatar
潘栩锋 committed
1588
                    }
1589
                }
潘栩锋's avatar
潘栩锋 committed
1590 1591
                else
                {
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
                    while(true)
                    {
                        index++;//测试一下
                        if (index >= mADList.Count)
                        {
                            //到顶了,不找了
                            index--;
                            break;
                        }
                        else if (mADList[index].Time == dt)
                        {
                            //找到了,就是它
                            break;
                        }
                        else if (mADList[index].Time > dt)
                        {
                            //上一个小于,现在大于,上一个就对了
                            index--;
                            break;
                        }
潘栩锋's avatar
潘栩锋 committed
1612 1613
                    }
                }
1614 1615
                
                return ADListFastSearchIndexResult.Now;
潘栩锋's avatar
潘栩锋 committed
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
            }
        }




        void OnPoll_UpdateADList() 
        {
            
            int adlist_index = mADList.No2Index(mADListToDoNo);
            if (adlist_index >= mADList.Count)
                return;//没有数据需要处理
            if (adlist_index < 0)
                adlist_index = 0;


            for (int i = adlist_index; i < mADList.Count; i++)
            {
                mADListToDoNo = mADList.Index2No(i);

1636 1637
                //被测膜模式选择
                switch (MMode)
潘栩锋's avatar
潘栩锋 committed
1638
                {
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
                    case MeasureMode.Edge:
                        {
                            if (!OnPoll_UpdateADList_Edge(i))
                                return;
                        }
                        break;
                    case MeasureMode.Normal:
                        {
                            if (!OnPoll_UpdateADList_Normal(i))
                                return;
                        }
                        break;
                    default:
                    //case MeasureMode.Bag:
                        {
                            if (!OnPoll_UpdateADList_Bag(i))
                                return;
                        }
                        break;
潘栩锋's avatar
潘栩锋 committed
1658 1659 1660 1661
                }
            }
            mADListToDoNo = mADList.Index2No(mADList.Count);
        }
1662 1663 1664 1665 1666

        #region 不同测量模式的ADList数据更新
        bool OnPoll_UpdateADList_Edge(int idx)
        {
            BlowingDetect.FilmInfo filminfo;
潘栩锋's avatar
潘栩锋 committed
1667
            int ret = mPDetect.GetFilmInfo(out filminfo, mADList[idx].Time, 2.3, 0);
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
            if (ret == 1)//发生在未来
            {
                return false;
            }
            if (filminfo == null)//异常,FilmPosH 错误
            {
                return false;
            }
            if (mADListToDoNo != mADList.Index2No(idx))//触发了事件
            {
                return false;
            }


            ADCell adcell = mADList[idx];
潘栩锋's avatar
潘栩锋 committed
1683 1684 1685 1686 1687 1688
            adcell.Angles.Clear();
            adcell.Angles.Add(filminfo.angle1);
            adcell.Direction = filminfo.direction;
            adcell.Velocity = filminfo.filmVelocity;
            adcell.RotationCnt = filminfo.rotationCnt;
            adcell.InCV = filminfo.inCV;
1689 1690 1691 1692 1693
            return true;
        }
        bool OnPoll_UpdateADList_Normal(int idx)
        {
            BlowingDetect.FilmInfo filminfo;
潘栩锋's avatar
潘栩锋 committed
1694
            int ret = mPDetect.GetFilmInfo(out filminfo, mADList[idx].Time, FilmWidth, FilmPosH);
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
            if (ret == 1)//发生在未来
            {
                return false;
            }
            if (filminfo == null)//异常,FilmPosH 错误
            {
                return false;
            }
            if (mADListToDoNo != mADList.Index2No(idx))//触发了事件
            {
                return false;
            }

            ADCell adcell = mADList[idx];
潘栩锋's avatar
潘栩锋 committed
1709 1710 1711 1712 1713 1714 1715
            adcell.Angles.Clear();
            adcell.Angles.Add(filminfo.angle1);
            adcell.Angles.Add(filminfo.angle2);
            adcell.Direction = filminfo.direction;
            adcell.Velocity = filminfo.filmVelocity;
            adcell.RotationCnt = filminfo.rotationCnt;
            adcell.InCV = filminfo.inCV;
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
            return true;
        }
        bool OnPoll_UpdateADList_Bag(int idx)
        {
            double filmWidth = FilmWidth + BagFold0 + BagFold1;

            //最内2层
            BlowingDetect.FilmInfo filminfo0;
            double filmPosH0 = BagFold0 - FilmPosH;

潘栩锋's avatar
潘栩锋 committed
1726
            int ret = mPDetect.GetFilmInfo(out filminfo0, mADList[idx].Time, filmWidth, filmPosH0);
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
            if (ret == 1)//发生在未来
            {
                return false;
            }
            if (filminfo0 == null)//异常,FilmPosH 错误
            {
                return false;
            }
            if (mADListToDoNo != mADList.Index2No(idx))//触发了事件
            {
                return false;
            }

            //最外2层
            BlowingDetect.FilmInfo filminfo1;
            double filmPosH1 = BagFold0 + FilmPosH;
潘栩锋's avatar
潘栩锋 committed
1743
            mPDetect.GetFilmInfo(out filminfo1, mADList[idx].Time, filmWidth, filmPosH1);
1744 1745

            ADCell adcell = mADList[idx];
潘栩锋's avatar
潘栩锋 committed
1746 1747 1748 1749 1750
            adcell.Angles.Clear();
            adcell.Angles.Add(filminfo1.angle1);
            adcell.Angles.Add(filminfo0.angle1);
            adcell.Angles.Add(filminfo0.angle2);
            adcell.Angles.Add(filminfo1.angle2);
1751 1752 1753


            //下面的参数 filminfo0 与 filminfo1 一样
潘栩锋's avatar
潘栩锋 committed
1754 1755 1756 1757
            adcell.Direction = filminfo0.direction;
            adcell.Velocity = filminfo0.filmVelocity;
            adcell.RotationCnt = filminfo0.rotationCnt;
            adcell.InCV = filminfo0.inCV;
1758 1759 1760 1761

            return true;
        }
        #endregion
潘栩锋's avatar
潘栩锋 committed
1762 1763 1764 1765 1766 1767 1768 1769
        /// <summary>
        /// 状态不一样,人字架已经转了新一圈,需要增加新一幅数据
        /// </summary>
        /// <param name="adcell"></param>
        /// <param name="frameinfo"></param>
        /// <returns></returns>
        bool FrameInfoCheckToNew(ADCell adcell, FRAME_INFO frameinfo) 
        {
潘栩锋's avatar
潘栩锋 committed
1770 1771
            if ((adcell.Direction != frameinfo.direction) ||
                (adcell.RotationCnt != frameinfo.rotationCnt) )
潘栩锋's avatar
潘栩锋 committed
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
                //(Math.Abs(adcell.velocity - frameinfo.velocity) > 2))
            {
                return true;
            }
            else 
            {
                return false;
            }
        }

        
        FR_DATA_TYPE Direction2FR_DATA_TYPE(Misc.DIRECTION direction) 
        {
            if (direction == DIRECTION.FORWARD)
                return FR_DATA_TYPE.INVALID;
            else
                return FR_DATA_TYPE.VALID;
        }

        /// <summary>
        /// 输出的数据都必须是稳定,可靠的。 生成一幅幅测厚仪数据!!!!!!!!!!!!
        /// </summary>
        void OnPoll_UpdateFrameInfo() 
        {
1796 1797 1798
            RenZiJiaDataEventArgs renZiJiaDataEventArgs=null;//当数据被修改,推送事件!!!,当只有最后一幅图被推送


潘栩锋's avatar
潘栩锋 committed
1799 1800 1801
            int adlist_index = mADList.No2Index(mADListToDoNo_ToFilmInfo);//当前在处理的 数据点
            if (adlist_index >= mADList.Count)
                return;//没有数据需要处理
1802

潘栩锋's avatar
潘栩锋 committed
1803 1804
            if (adlist_index < 0)//全部都要处理
                adlist_index = 0;
1805
            
潘栩锋's avatar
潘栩锋 committed
1806
            //当没有任何一幅数据时, frameinfo_index = mFrameInfoList.Count = 0
1807
            int frameinfo_index = Math.Max(mFrameInfoList.Count - 1, 0); 
潘栩锋's avatar
潘栩锋 committed
1808 1809

            //查找adlist_index 属于哪个frameinfo;
1810 1811
            //当找到,清空里面的.frame,一切重新开始!!!
            //当不能找到,说明只是接着上次的处理而已,不需要重新开始
潘栩锋's avatar
潘栩锋 committed
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
            for (int i = 0; i < mFrameInfoList.Count; i++)
            {
                int index_start;
                int index_end;
                FRAME_INFO frameinfo = mFrameInfoList[i];
                if (!GetFilmInfoADIndex(frameinfo, out index_start, out index_end))
                {
                    //数据已经不存在,删除
                    mFrameInfoList.RemoveAt(i);
                    i--;
                    continue;
                }
                if (adlist_index <= index_end)
                {
                    //要处理的数据点,位于 这幅数据 (frameinfo); 重新计算整幅数据 
                    frameinfo_index = i;
                    //adlist_index = mADList.No2Index(frameinfo.NoStartAD);
                    break;
                }
            }

            int adlist_todo_index = mADList.No2Index(mADListToDoNo);//已经成功获取完位置信息的数据尽头
            for (int i = adlist_index; (i < mADList.Count && i<adlist_todo_index); i++)
            {
                ADCell adcell = mADList[i];
                //mADListToDoNo_ToFilmInfo = mADList.Index2No(i);
                 
                
                FRAME_INFO frameinfo;
                //--------------------------------------------------------------------------------------------------------
                //条件检测
                if (frameinfo_index >= mFrameInfoList.Count)//根本没数据,新建
                {
                    frameinfo = new FRAME_INFO();
潘栩锋's avatar
潘栩锋 committed
1846 1847 1848 1849 1850
                    frameinfo.StartTime = adcell.Time;
                    frameinfo.EndTime = adcell.Time;
                    frameinfo.direction = adcell.Direction;
                    frameinfo.velocity = adcell.Velocity;
                    frameinfo.rotationCnt = adcell.RotationCnt;
潘栩锋's avatar
潘栩锋 committed
1851 1852 1853 1854 1855 1856 1857 1858 1859
                    frameinfo.NoStartAD = mADList.Index2No(i);
                    frameinfo.NoEndAD = mADList.Index2No(i);
                    frameinfo.SetNBolt(NBolts);
                    mFrameInfoList.RAdd(frameinfo);
                    frameinfo_index = mFrameInfoList.Count - 1;
                }
                else 
                {
                    frameinfo = mFrameInfoList[frameinfo_index];
1860
                    if (i <= mADList.No2Index(frameinfo.NoEndAD)) 
潘栩锋's avatar
潘栩锋 committed
1861
                    {
1862 1863 1864 1865
                        //当为新数据, i应该>mADList.No2Index(frameinfo.NoEndAD)
                        //能进这里,证明是要重新计算
                        //清空数据,重新来过
                        
潘栩锋's avatar
潘栩锋 committed
1866 1867 1868 1869 1870
                        frameinfo.StartTime = adcell.Time;
                        frameinfo.EndTime = adcell.Time;
                        frameinfo.direction = adcell.Direction;
                        frameinfo.velocity = adcell.Velocity;
                        frameinfo.rotationCnt = adcell.RotationCnt;
潘栩锋's avatar
潘栩锋 committed
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
                        frameinfo.NoStartAD = mADList.Index2No(i);
                        frameinfo.NoEndAD = mADList.Index2No(i);
                        frameinfo.Clear();
                    }
                }
                //--------------------------------------------------------------------------------------------------------


                if (FrameInfoCheckToNew(adcell, frameinfo))
                {
                    //一幅数据已经完成
1882
                    switch (MMode)
潘栩锋's avatar
潘栩锋 committed
1883
                    {
1884 1885 1886 1887 1888 1889 1890
                        case MeasureMode.Normal:
                        case MeasureMode.Bag:
                            {
                                //最后检查一下是否需要解方程
                                frameinfo.SolveFormula();
                            }
                            break;
潘栩锋's avatar
潘栩锋 committed
1891 1892
                    }

1893 1894 1895
                    
                    //重新分区
                    if (IsNeedMap())
潘栩锋's avatar
潘栩锋 committed
1896
                    {
1897
                        Dictionary<int, int> m = new Dictionary<int, int>();
潘栩锋's avatar
潘栩锋 committed
1898
                        for (int j = 0; j < Map.Count(); j++)
1899 1900
                            m.Add(Map[j].OldNo - 1, Map[j].NewNo - 1);
                        frameinfo.frame_map = Misc.MyMath.Map(frameinfo.frame, m);
潘栩锋's avatar
潘栩锋 committed
1901 1902
                    }

1903
                    //更新数据库内容
潘栩锋's avatar
潘栩锋 committed
1904
                    {
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
                        int[] frame;
                        List<BoltMapCell> map;
                        if (IsNeedMap())
                        {
                            frame = frameinfo.frame_map;
                            map = new List<BoltMapCell>();
                            foreach (var m in Map)
                                map.Add(m.Clone());
                        }
                        else
                        {
                            frame = frameinfo.frame;
                            map = null;
                        }
潘栩锋's avatar
潘栩锋 committed
1919
                        var realthicks = ToRealThicks(frame);
1920 1921 1922 1923 1924 1925 1926 1927 1928

                        frameinfo.renZiJiaDataEventArgs = new RenZiJiaDataEventArgs()
                        {
                            Time = frameinfo.StartTime,
                            EndTime = frameinfo.EndTime,
                            IsBackw = frameinfo.direction == DIRECTION.BACKWARD,
                            RPeriod = mPDetect.RenZiJiaPeriod,
                            RCnt = frameinfo.rotationCnt,
                            OrgBoltNo = OrgBoltNo,
1929 1930 1931 1932
                            RAngle = Math.Round(mPDetect.RAngle,2),
                            FilmLength = Math.Round(mPDetect.FilmLength,2),
                            FilmVelocity = Math.Round(mPDetect.FilmVelocity,2),
                            K = Math.Round(mProfileParam.K,3),
潘栩锋's avatar
潘栩锋 committed
1933
                            Thicks = realthicks,
1934 1935 1936 1937
                            Boltmap = map
                        };
                        frameinfo.renZiJiaDataEventArgsHasChanged = true;
                    }
潘栩锋's avatar
潘栩锋 committed
1938

1939
                    renZiJiaDataEventArgs = frameinfo.renZiJiaDataEventArgs;
潘栩锋's avatar
潘栩锋 committed
1940 1941 1942 1943 1944 1945 1946 1947
                    //更新到下一个frameinfo
                    frameinfo_index++;


                    i--;
                    continue;
                }

潘栩锋's avatar
潘栩锋 committed
1948
                int thick = AD2Thick(adcell.Ad);
1949
                switch (MMode)
潘栩锋's avatar
潘栩锋 committed
1950
                {
1951 1952 1953
                    case MeasureMode.Edge:
                        {
                            //均值,更新frameinfo.frame
潘栩锋's avatar
潘栩锋 committed
1954
                            frameinfo.SetFrameValue(Angle2BoltIndex(adcell.Angles.First()), thick , adcell.Time);
1955 1956 1957 1958 1959
                        }
                        break;
                    case MeasureMode.Normal:
                        {
                            //解方程,更新frameinfo.frame
潘栩锋's avatar
潘栩锋 committed
1960 1961
                            var list = from a in adcell.Angles select Angle2BoltIndex(a);
                            frameinfo.SetFrameValue(list, thick * 2, adcell.Time);
1962 1963 1964 1965 1966 1967
                        }
                        break;
                    default:
                    //case MeasureMode.Bag:
                        {
                            //解方程,更新frameinfo.frame
潘栩锋's avatar
潘栩锋 committed
1968 1969
                            var list = from a in adcell.Angles select Angle2BoltIndex(a);
                            frameinfo.SetFrameValue(list, thick * 4, adcell.Time);
1970 1971
                        }
                        break;
潘栩锋's avatar
潘栩锋 committed
1972 1973
                }

潘栩锋's avatar
潘栩锋 committed
1974
                frameinfo.EndTime = adcell.Time;
潘栩锋's avatar
潘栩锋 committed
1975 1976 1977
                frameinfo.NoEndAD = mADList.Index2No(i);
            }
            mADListToDoNo_ToFilmInfo = mADListToDoNo;
1978
            
潘栩锋's avatar
潘栩锋 committed
1979 1980 1981 1982 1983

            //检测数据更新情况,放入bulkdata
            for (int i = 0; i < mFrameInfoList.Count; i++) 
            {
                FRAME_INFO frameinfo = mFrameInfoList[i];
1984
                switch (MMode)
潘栩锋's avatar
潘栩锋 committed
1985
                {
1986 1987 1988 1989 1990 1991 1992
                    case MeasureMode.Normal:
                    case MeasureMode.Bag:
                        {
                            //最后检查一下是否需要解方程
                            frameinfo.SolveFormula();
                        }
                        break;
潘栩锋's avatar
潘栩锋 committed
1993 1994
                }

1995
                if (frameinfo.HasChanged)//数据检测过,需要更新bulkdata里面的数据, 大部分发生在扫描过程中,还没结束一幅图
潘栩锋's avatar
潘栩锋 committed
1996
                {
1997 1998
                    //重新分区
                    if (IsNeedMap())
潘栩锋's avatar
潘栩锋 committed
1999
                    {
2000 2001 2002 2003 2004
                        Dictionary<int, int> m = new Dictionary<int, int>();
                        for (int j = 0; j < Map.Count(); j++)
                            m.Add(Map[j].OldNo - 1, Map[j].NewNo - 1);
                        frameinfo.frame_map = Misc.MyMath.Map(frameinfo.frame, m);
                    }
潘栩锋's avatar
潘栩锋 committed
2005

2006
                    int[] thicks = IsNeedMap() ? frameinfo.frame_map : frameinfo.frame;
潘栩锋's avatar
潘栩锋 committed
2007

2008 2009
                    if (frameinfo.bookmark != -1)
                    {
潘栩锋's avatar
潘栩锋 committed
2010 2011 2012 2013
                        if (!mBulkData.Change(frameinfo.bookmark, 0, thicks,
                                Direction2FR_DATA_TYPE(frameinfo.direction),
                                frameinfo.StartTime)) 
                        {
2014 2015
                            //修改失败,肯定是 bulkdata被清空了
                            frameinfo.bookmark = -1;
潘栩锋's avatar
潘栩锋 committed
2016
                        }
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
                    }

                    if (frameinfo.bookmark == -1)
                    {
                        mBulkData.Save();
                        //添加新的
                        frameinfo.bookmark = mBulkData.Add(thicks,
                            Direction2FR_DATA_TYPE(frameinfo.direction),
                            frameinfo.StartTime);
                        if (EPCType == RenZiJiaFixEPCType.EPCA10)
                            mEPCA10.CheckToSample();
                    }
                }
            }
潘栩锋's avatar
潘栩锋 committed
2031 2032

            if(mFrameInfoList.Count()>0)
2033
            {
潘栩锋's avatar
潘栩锋 committed
2034 2035 2036 2037 2038 2039 2040 2041 2042
                FRAME_INFO frameinfo = null;
                try
                {
                    frameinfo = mFrameInfoList.Last(f => f.HasChanged);
                }
                catch
                {

                }
2043 2044 2045 2046
                if (frameinfo != null)
                {
                    int[] thicks = IsNeedMap() ? frameinfo.frame_map : frameinfo.frame;

潘栩锋's avatar
潘栩锋 committed
2047
                    mBulkDb.SetTempFrame(0, ToRealThicks(thicks));
2048 2049
                }
            }
潘栩锋's avatar
潘栩锋 committed
2050

2051 2052 2053 2054 2055 2056 2057 2058 2059
            foreach (var frameinfo in mFrameInfoList)
            {
                frameinfo.Submit();
            }

            #region sqlite3 历史数据保存
            if (mFrameInfoList.Any((frameinfo) => frameinfo.renZiJiaDataEventArgsHasChanged))
            {
                //下辊检测
潘栩锋's avatar
潘栩锋 committed
2060
                if (mHistoryDb.localDb.IsProfileFinished)
2061 2062
                {
                    AddProfile();
潘栩锋's avatar
潘栩锋 committed
2063 2064 2065
                }
            }

2066 2067
            //检测数据更新情况,放入数据库, 数据不能被修改

2068 2069 2070 2071 2072 2073 2074 2075 2076
            foreach (var frameinfo in mFrameInfoList)
            {
                if (frameinfo.renZiJiaDataEventArgsHasChanged)
                {
                    frameinfo.renZiJiaDataEventArgsHasChanged = false;

                    if (frameinfo.scandata_id == null)
                    {
                        //添加数据
2077
                        var lc_scandata = Lc_AutoMapperProfile.Mapper.Map<Lc_ScanData>(frameinfo.renZiJiaDataEventArgs);
潘栩锋's avatar
潘栩锋 committed
2078
                        mHistoryDb.AddScanData(lc_scandata);
2079 2080 2081 2082 2083
                        frameinfo.scandata_id = lc_scandata.ID;
                    }
                    else
                    {
                        //修改数据
2084
                        var lc_scandata = Lc_AutoMapperProfile.Mapper.Map<Lc_ScanData>(frameinfo.renZiJiaDataEventArgs);
2085
                        lc_scandata.ID = (Int64)frameinfo.scandata_id;
潘栩锋's avatar
潘栩锋 committed
2086
                        mHistoryDb.UpdateScanData(lc_scandata);
2087 2088 2089 2090 2091 2092 2093
                    }
                }
            }
            #endregion
            //当数据被修改,只推送最后一次的数据
            if (renZiJiaDataEventArgs != null)
                DataEvent?.Invoke(this, renZiJiaDataEventArgs);
潘栩锋's avatar
潘栩锋 committed
2094
        }
2095

潘栩锋's avatar
潘栩锋 committed
2096
        double[] ToRealThicks(IEnumerable<int> frame)
2097
        {
2098
            return frame.Select(t =>
2099 2100 2101 2102 2103 2104 2105
            {
                if (Misc.MyBase.ISVALIDATA(t))
                    return t / 100.0;
                else
                    return double.NaN;
            }).ToArray();
        }
潘栩锋's avatar
潘栩锋 committed
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
        protected override void OnPoll()
        {
            if (EPCType == RenZiJiaFixEPCType.EPCA10)
                mEPCA10.OnPoll();

            OnPoll_UpdateADList();
            OnPoll_UpdateFrameInfo();
        }

        public override bool Start()
        {
            //以前的 Start()
            IsRunning = true;
            mBulkData.InitPush();
2120
            //清空临时数据
潘栩锋's avatar
潘栩锋 committed
2121
            mBulkDb.SetTempFrame(0, null);
2122 2123 2124 2125 2126

            #region sqlite3 历史数据保存
            CheckProfile();
            mProfileParam.PropertyChanged += MProfileParam_PropertyChanged;
            #endregion
潘栩锋's avatar
潘栩锋 committed
2127 2128 2129 2130 2131 2132 2133

            FObjBase.PollModule.Current.Poll_Config(
                FObjBase.PollModule.POLL_CONFIG.ADD,
                onpoll_func,
                TimeSpan.FromSeconds(1));
            return true;
        }
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
        #region sqlite3 历史数据保存
        string[] profile_propertynames_add = new string[]{
                "PName",
                "OrderNo",
                "Number"
            };
        string[] profile_propertynames_update = new string[]{
                "Target",
                "TolerancePercent"
            };
        void CheckProfile()
        {
潘栩锋's avatar
潘栩锋 committed
2146
            if (mHistoryDb.localDb.IsProfileFinished)
2147 2148 2149 2150 2151 2152
            {
                //复位 膜纵向位置
                AddProfile();
            }
            else
            {
潘栩锋's avatar
潘栩锋 committed
2153
                if (mHistoryDb.localDb.CurrProfile != null)
2154
                {
潘栩锋's avatar
潘栩锋 committed
2155
                    Db_Profile dB_Profile = mHistoryDb.localDb.CurrProfile;
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
                    if ((dB_Profile.PName != mProfileParam.PName) ||
                    (dB_Profile.OrderNo != mProfileParam.OrderNo) ||
                    (dB_Profile.Number != mProfileParam.Number))
                    {
                        AddProfile();
                    }
                    else
                    {
                        //继续上一次生产!!!!
                        UpdateProfile();


                    }
                }
                else
                {
                    //异常
                    AddProfile();
                }

            }
        }
        void AddProfile()
        {
潘栩锋's avatar
潘栩锋 committed
2180
            mHistoryDb.AddProfile(new Db_Profile()
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
            {
                PName = mProfileParam.PName,
                OrderNo = mProfileParam.OrderNo,
                Number = mProfileParam.Number,
                Target = mProfileParam.Target,
                TolerancePercent = mProfileParam.TolerancePercent,
                StartTime = DateTime.Now,
                EndTime = DateTime.Now
            });
        }
        void UpdateProfile()
        {
潘栩锋's avatar
潘栩锋 committed
2193
            mHistoryDb.UpdateProfile(new Db_Profile()
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
            {
                PName = mProfileParam.PName,
                OrderNo = mProfileParam.OrderNo,
                Number = mProfileParam.Number,
                Target = mProfileParam.Target,
                TolerancePercent = mProfileParam.TolerancePercent,
            });
        }
        private void MProfileParam_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (profile_propertynames_add.Contains(e.PropertyName))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    () =>
                    {
潘栩锋's avatar
潘栩锋 committed
2209
                        if (!mHistoryDb.localDb.IsProfileFinished)
2210
                        {
潘栩锋's avatar
潘栩锋 committed
2211
                            mHistoryDb.FinishProfile();
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
                        }
                        AddProfile();//新
                    }, this, MARKNO_PROFILE_ADD);
            }
            else if (profile_propertynames_update.Contains(e.PropertyName))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    () =>
                    {
                        UpdateProfile();//修改
                    }, this, MARKNO_PROFILE_CHANGED);
            }
        }
        #endregion
潘栩锋's avatar
潘栩锋 committed
2226 2227 2228 2229 2230 2231 2232 2233 2234
        bool Load()
        {
            return Misc.SaveToXmlHepler.Load("renzijiafix.xml",this);
        }
        void Save()
        {
            Misc.SaveToXmlHepler.Save("renzijiafix.xml", this);
        }

潘栩锋's avatar
潘栩锋 committed
2235
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
2236
        /// 获取 厚度列表
潘栩锋's avatar
潘栩锋 committed
2237 2238 2239 2240 2241
        /// </summary>
        /// <param name="begin">开始时间</param>
        /// <param name="AsyncDelegate">retdata = GetRollListReponse</param>
        /// <param name="AsyncState"></param>
        public void GetADList(DateTime begin, AsyncCBHandler AsyncDelegate, object AsyncState)
潘栩锋's avatar
潘栩锋 committed
2242
        {
潘栩锋's avatar
潘栩锋 committed
2243
            var p = new GetADListReponse
潘栩锋's avatar
潘栩锋 committed
2244
            {
2245 2246
                datas = mADList.FindAll(a => a.Time >= begin).Select(a => new ADSingle() { dt = a.Time,
                    thick = AD2Thick(a.Ad) }).ToList()
潘栩锋's avatar
潘栩锋 committed
2247
            };
潘栩锋's avatar
潘栩锋 committed
2248

潘栩锋's avatar
潘栩锋 committed
2249
            AsyncDelegate(AsyncState, p);
潘栩锋's avatar
潘栩锋 committed
2250 2251
        }

潘栩锋's avatar
潘栩锋 committed
2252
    }
潘栩锋's avatar
潘栩锋 committed
2253 2254

}