GM_BlowingFix.cs 53.7 KB
Newer Older
1 2
using AutoMapper;
using FLY.Thick.Base.Common;
潘栩锋's avatar
潘栩锋 committed
3
using FLY.Thick.Base.Server;
4
using FLY.Thick.Blowing.Common;
5
using FLY.Thick.Blowing.IService;
6
using FLY.Thick.Blowing.Server.Model;
7 8 9 10 11
using FlyADBase;
using FObjBase;
using FObjBase.Reflect;
using MathNet.Numerics.LinearAlgebra.Double;
using Misc;
12
using Newtonsoft.Json;
13 14 15
using System;
using System.Collections.Generic;
using System.ComponentModel;
16
using System.IO;
17
using System.Linq;
潘栩锋's avatar
潘栩锋 committed
18 19 20

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

28 29
        const int MARKNO_PROFILE_ADD = 87;
        const int MARKNO_PROFILE_CHANGED = 88;
潘栩锋's avatar
潘栩锋 committed
30

31
        //OrgHistoryDb orgHistoryDb;
32 33
        HistoryDb historyDb;
        BulkDb bulkDb;
潘栩锋's avatar
潘栩锋 committed
34 35 36 37 38 39 40 41 42

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

        /// <summary>
        /// 人字架角度 &amp; 膜走带位置检测
        /// </summary>
        public BlowingDetect mPDetect;
43

潘栩锋's avatar
潘栩锋 committed
44 45 46 47 48

        #region IRenZiJiaService

        #region 分区设定

49

潘栩锋's avatar
潘栩锋 committed
50 51 52
        /// <summary>
        /// 加热通道数
        /// </summary>
53
        public int ChannelCnt { get; set; } = 88;
潘栩锋's avatar
潘栩锋 committed
54 55 56 57

        /// <summary>
        /// 分区数/加热通道数
        /// </summary>
58
        public int BPC { get; set; } = 1;
潘栩锋's avatar
潘栩锋 committed
59 60 61 62

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

潘栩锋's avatar
潘栩锋 committed
65 66 67 68

        /// <summary>
        /// 复位时,探头测量的位置对应分区号
        /// </summary>
69
        public int OrgBoltNo { get; set; } = 1;
潘栩锋's avatar
潘栩锋 committed
70 71 72 73 74


        /// <summary>
        /// 第1个螺丝号
        /// </summary>
75
        public int BoltNo1st = 1;
潘栩锋's avatar
潘栩锋 committed
76 77 78
        /// <summary>
        /// 使用分区表
        /// </summary>
79
        public bool IsUsedMap { get; set; }
80 81
        bool IsNeedMap()
        {
82
            if (IsUsedMap && Map != null && Map.Count() > 0)
83 84 85 86
                return true;
            else
                return false;
        }
87
        private BoltMapCell[] map;
潘栩锋's avatar
潘栩锋 committed
88 89 90
        /// <summary>
        /// 分区表,必须保证不能为null
        /// </summary>
91
        public BoltMapCell[] Map
潘栩锋's avatar
潘栩锋 committed
92 93
        {
            get { return map; }
94 95
            set
            {
潘栩锋's avatar
潘栩锋 committed
96
                //检测是否不一样
97
                if (!IsEquals(map, value))
潘栩锋's avatar
潘栩锋 committed
98
                {
99
                    if (!IsValid(value))
潘栩锋's avatar
潘栩锋 committed
100 101
                    {
                        IsUsedMap = false;
102
                        map = null;
潘栩锋's avatar
潘栩锋 committed
103 104 105 106 107
                    }
                    else
                    {
                        map = value;
                    }
潘栩锋's avatar
潘栩锋 committed
108 109 110 111 112 113
                }
            }
        }

        #endregion

114 115 116 117
        /// <summary>
        /// 测厚仪类型, 追边 or 扫描
        /// </summary>
        public BlowingType BType { get; } = BlowingType.Fix;
潘栩锋's avatar
潘栩锋 committed
118

119 120 121 122
        /// <summary>
        /// 测厚仪测量点位置方向:Left, Right (也就是三角形在左还是右)
        /// </summary>
        public bool IsProbeRight { get; set; } = true;
123

124 125 126 127 128 129 130
        /// <summary>
        /// 正方向是顺时钟;
        /// 信号0->信号1 为正方向;
        /// 风环加热棒排列方向为 正方向;
        /// 默认情况 风环加热棒排列是逆时钟
        /// </summary>
        public bool IsForwCW { get; set; }
131 132 133
        /// <summary>
        /// 应用
        /// </summary>
134
        public void Apply()
潘栩锋's avatar
潘栩锋 committed
135
        {
136

潘栩锋's avatar
潘栩锋 committed
137 138
            Save();
        }
139 140
        [Push(typeof(RenZiJiaDataEventArgs))]
        public event EventHandler DataEvent;
潘栩锋's avatar
潘栩锋 committed
141 142 143 144
        #endregion


        DynArea mDynArea;
145
        BlowingFixProfileParam mProfileParam;
潘栩锋's avatar
潘栩锋 committed
146 147 148 149 150 151 152 153
        /// <summary>
        /// 当前正在处理的 ADList 里面的数据.No
        /// </summary>
        int mADListToDoNo;
        /// <summary>
        /// 处理放入filminfo 的数据.No
        /// </summary>
        int mADListToDoNo_ToFilmInfo;
154

潘栩锋's avatar
潘栩锋 committed
155 156 157
        /// <summary>
        /// 1s一个数据,
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
158 159

        RList<ADCell> mADList;
160

161

潘栩锋's avatar
潘栩锋 committed
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

        /// <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;
            }
188
            public bool AddData(IEnumerable<int> idxs, double thk)
潘栩锋's avatar
潘栩锋 committed
189
            {
190
                foreach (int idx in idxs)
191 192
                    if ((idx < 0) || (idx >= n)) return false;

193
                foreach (int idx in idxs)
194
                    ai[idx]++;
195 196

                y += thk;
潘栩锋's avatar
潘栩锋 committed
197 198 199 200 201
                return true;
            }
        }
        class FRAME_INFO//一幅数据信息
        {
202

潘栩锋's avatar
潘栩锋 committed
203 204 205 206 207 208 209 210
            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>
211 212 213 214 215 216
            /// 对应数据库的 id
            /// </summary>
            public Int64? scandata_id = null;
            /// <summary>
            /// 分区数据 N*long
            /// </summary>
217
            public double[] frame;
218 219 220
            /// <summary>
            /// 经过分区表后的结果
            /// </summary>
221
            public double[] frame_map;
222 223 224 225 226 227 228 229 230 231
            /// <summary>
            /// 推送出去的数据,也是数据库中的数据
            /// </summary>
            public RenZiJiaDataEventArgs renZiJiaDataEventArgs;
            /// <summary>
            /// 推送出去的数据 被修改了
            /// </summary>
            public bool renZiJiaDataEventArgsHasChanged = false;


232
            double frame_sum = 0;
潘栩锋's avatar
潘栩锋 committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
            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();
            }
257
            public void SetNBolt(int nbolts)
潘栩锋's avatar
潘栩锋 committed
258
            {
259
                frame = new double[nbolts];
潘栩锋's avatar
潘栩锋 committed
260 261 262 263 264 265 266
                appeared = new bool[nbolts];
                for (int i = 0; i < nbolts; i++)
                {
                    ClearFrameValue(i);
                }
            }

267
            void ClearFrameValue(int index)
潘栩锋's avatar
潘栩锋 committed
268
            {
269
                frame[index] = double.NaN;
潘栩锋's avatar
潘栩锋 committed
270 271
                appeared[index] = false;
            }
272
            public void Clear()
潘栩锋's avatar
潘栩锋 committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
            {
                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;
            }
288

潘栩锋's avatar
潘栩锋 committed
289 290 291 292 293
            public bool HasChanged = false;//数据被改变了!!!,是时候更新bulkdata
            public bool IsValid = true;//是合法数据
            /// <summary>
            /// 提交,完成一次操作。清除状态
            /// </summary>
294
            public void Submit()
潘栩锋's avatar
潘栩锋 committed
295 296 297
            {
                HasChanged = false;
            }
298
            public void CheckForValid()
潘栩锋's avatar
潘栩锋 committed
299 300 301 302 303 304 305 306
            {
                if (rotationCnt == 0)//旋转次数=0,肯定是不完整的数据
                {
                    HasChanged = true;
                    IsValid = false;
                    return;
                }
                //所有的数据都应该大于10
307
                for (int i = 0; i < frame.Count(); i++)
潘栩锋's avatar
潘栩锋 committed
308
                {
309
                    if (!double.IsNaN(frame[i]))
潘栩锋's avatar
潘栩锋 committed
310
                    {
311
                        if (frame[i] < 10.0)
潘栩锋's avatar
潘栩锋 committed
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
                        {
                            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();
346
                if (virtual_nbolts != nbolts)
潘栩锋's avatar
潘栩锋 committed
347 348 349 350 351 352
                {
                    virtual_nbolts = nbolts;
                    UpdateVirtualFormulas();
                }
                //TODO 能否解得方程检查
                int nrow = mFormulas.Count() + virtual_formulas.Count();
353

潘栩锋's avatar
潘栩锋 committed
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
                //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);

                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++)
                {
384 385
                    double d = (appeared[i]) ? X[i] : double.NaN;
                    frame[i] = d;
潘栩锋's avatar
潘栩锋 committed
386 387 388 389 390 391 392 393 394 395 396 397
                }
                HasChanged = true;

                isTimeToSolve = false;
                canSolve = false;
            }

            /// <summary>
            /// 检查是否需要更新
            /// </summary>
            public void CheckToSolveFormula()
            {
398
                if (isTimeToSolve)
潘栩锋's avatar
潘栩锋 committed
399 400 401 402
                {
                    SolveFormula();
                }
            }
403 404 405


            int index_last = -1;
406
            public void SetFrameValue(int index, double value, DateTime dt)
407 408 409 410 411 412 413
            {
                int nbolts = frame.Count();

                if (index >= nbolts)
                    return;//异常
                else if (index < 0)
                    return;//异常
414
                else if (double.IsNaN(value))
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
                    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
430 431 432
            /// <summary>
            /// 解方程方式更新 frame
            /// </summary>
433
            /// <param name="indexs"></param>
潘栩锋's avatar
潘栩锋 committed
434 435
            /// <param name="value"></param>
            /// <param name="dt"></param>
436
            public void SetFrameValue(IEnumerable<int> indexs, double value, DateTime dt)
潘栩锋's avatar
潘栩锋 committed
437 438
            {
                int nbolts = frame.Count();
439 440 441 442 443 444 445
                foreach (int index in indexs)
                {
                    if (index >= nbolts)
                        return;//异常
                    else if (index < 0)
                        return;//异常
                }
446
                if (double.IsNaN(value))
潘栩锋's avatar
潘栩锋 committed
447
                    return;//异常
448

潘栩锋's avatar
潘栩锋 committed
449
                Formula fm = new Formula(nbolts);
450
                fm.AddData(indexs, value);
潘栩锋's avatar
潘栩锋 committed
451
                mFormulas.Add(fm);
452 453 454

                foreach (int index in indexs)
                    appeared[index] = true;
455

潘栩锋's avatar
潘栩锋 committed
456 457 458
                canSolve = true;

                //5秒解一次方程
459
                if ((index_last != indexs.First()) && ((dt - last_solvedt) >= TimeSpan.FromSeconds(5)))
潘栩锋's avatar
潘栩锋 committed
460
                {
461
                    index_last = indexs.First();
潘栩锋's avatar
潘栩锋 committed
462 463 464 465 466 467 468 469
                    last_solvedt = dt;
                    isTimeToSolve = true;
                }
            }
        }
        RList<FRAME_INFO> mFrameInfoList;


470 471
        AD2ThkHandler Ad2Thk;

潘栩锋's avatar
潘栩锋 committed
472 473


474
        void checkParamErr()
潘栩锋's avatar
潘栩锋 committed
475 476 477 478 479
        {
            if (ChannelCnt < 20 || ChannelCnt > 160)
            {
                ChannelCnt = 44;
            }
480

潘栩锋's avatar
潘栩锋 committed
481 482 483 484 485 486 487 488 489
            if (BPC < 1 || BPC > 4)
            {
                BPC = 2;
            }

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

491
            if (!IsValid(Map))
潘栩锋's avatar
潘栩锋 committed
492
            {
潘栩锋's avatar
潘栩锋 committed
493
                IsUsedMap = false;
494
                Map = null;
潘栩锋's avatar
潘栩锋 committed
495 496 497 498 499 500 501
            }
        }

        /// <summary>
        /// 检测通过,返回true
        /// </summary>
        /// <returns></returns>
502
        bool IsValid(BoltMapCell[] mapCells)
潘栩锋's avatar
潘栩锋 committed
503 504 505 506
        {
            bool map_err = false;
            bool g_o = false;
            bool g_n = false;
507 508 509
            if (mapCells == null)
                return true;

潘栩锋's avatar
潘栩锋 committed
510 511 512 513
            //Map 检测
            //OldNo,NewNo不能 <1 >NBolts
            //Map OldNo 必须从小到大环形排列
            //Map NewNo 必须从小到大环形排列
潘栩锋's avatar
潘栩锋 committed
514
            for (int i = 0; i < mapCells.Count(); i++)
潘栩锋's avatar
潘栩锋 committed
515
            {
516 517
                var mapCell = mapCells[i];
                if (mapCell.NewNo < 1 || mapCell.NewNo > NBolts)
潘栩锋's avatar
潘栩锋 committed
518 519 520 521
                {
                    map_err = true;
                    break;
                }
522
                if (mapCell.OldNo < 1 || mapCell.OldNo > NBolts)
潘栩锋's avatar
潘栩锋 committed
523 524 525 526 527 528
                {
                    map_err = true;
                    break;
                }

                int i_next = i + 1;
潘栩锋's avatar
潘栩锋 committed
529
                if (i_next >= mapCells.Count())
潘栩锋's avatar
潘栩锋 committed
530 531
                    i_next = 0;

532 533
                var mapCell_next = mapCells[i_next];
                if (mapCell.OldNo >= mapCell_next.OldNo)
潘栩锋's avatar
潘栩锋 committed
534 535 536 537 538 539 540 541 542 543 544
                {
                    if (!g_o)
                        g_o = true;
                    else
                    {
                        map_err = true;
                        break;
                    }

                }

545
                if (mapCell.NewNo >= mapCell_next.NewNo)
潘栩锋's avatar
潘栩锋 committed
546 547 548 549 550 551 552 553 554 555 556
                {
                    if (!g_n)
                        g_n = true;
                    else
                    {
                        map_err = true;
                        break;
                    }

                }
            }
557

潘栩锋's avatar
潘栩锋 committed
558 559 560 561 562 563 564 565 566
            if (map_err)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
567 568

        bool IsEquals(BoltMapCell c1, BoltMapCell c2)
潘栩锋's avatar
潘栩锋 committed
569
        {
570 571
            if (c1 == c2)
            {
潘栩锋's avatar
潘栩锋 committed
572
                return true;
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
            }
            if (c1 == null && c2 != null)
                return false;
            if (c1 != null && c2 == null)
                return false;
            if (c1.NewNo != c2.NewNo)
                return false;
            if (c1.OldNo != c2.OldNo)
                return false;
            return true;
        }
        bool IsEquals(BoltMapCell[] mapCells1, BoltMapCell[] mapCells2)
        {
            if (mapCells1 == mapCells2)
                return true;
            if (mapCells1 == null && mapCells2 != null)
                return false;
            if (mapCells1 != null && mapCells2 == null)
                return false;
            if (mapCells1.Count() != mapCells2.Count())
                return false;

            //mapCells1 与 mapCells2 肯定都不是 null 且数量一样
596
            for (int i = 0; i < mapCells1.Count(); i++)
潘栩锋's avatar
潘栩锋 committed
597
            {
598 599 600 601
                var c1 = mapCells1[i];
                var c2 = mapCells2[i];
                if (!IsEquals(c1, c2))
                    return false;
潘栩锋's avatar
潘栩锋 committed
602
            }
603
            return true;
潘栩锋's avatar
潘栩锋 committed
604
        }
605

606

潘栩锋's avatar
潘栩锋 committed
607 608 609 610
        /// <summary>
        /// 
        /// </summary>
        /// <param name="flyad"></param>
611
        public GM_BlowingFix()
潘栩锋's avatar
潘栩锋 committed
612 613
        {
            GMState = CTRL_STATE.FIX;
614

潘栩锋's avatar
潘栩锋 committed
615
            mPDetect = new BlowingDetect();
616

潘栩锋's avatar
潘栩锋 committed
617 618 619 620 621 622 623
            mPDetect.Load();
            Load();

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

624

潘栩锋's avatar
潘栩锋 committed
625
            InitList();
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
        }

        public void Init(
            FlyAD7 flyad,
            AD2ThkHandler ad2thk, DynArea dynarea,
            BlowingFixProfileParam profileParam,
            HistoryDb historyDb,
            BulkDb bulkDb
            )
        {
            base.Init(flyad);

            Ad2Thk = ad2thk;
            mDynArea = dynarea;
            mProfileParam = profileParam;
            this.historyDb = historyDb;
            //this.orgHistoryDb = orgHistoryDb;
            this.bulkDb = bulkDb;
            this.bulkDb.GetTempFrameAction = GetTempFrame;


潘栩锋's avatar
潘栩锋 committed
647 648

            PropertyChanged += new PropertyChangedEventHandler(RenZiJiaFix_PropertyChanged);
649

潘栩锋's avatar
潘栩锋 committed
650 651
            mPDetect.FilmInfoChangedEvent += new FilmInfoChangedEventHandler(mPDetect_FilmInfoChangedEvent);
            mPDetect.ClearEvent += new ClearEventHandler(mPDetect_ClearEvent);
潘栩锋's avatar
潘栩锋 committed
652 653 654 655 656 657 658


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

            mPDetect.Init(flyad);


659
            Misc.BindingOperations.SetBinding(mPDetect, nameof(mPDetect.FilmVelocity), mDynArea, nameof(mDynArea.FilmVelocity));
潘栩锋's avatar
潘栩锋 committed
660

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
            mProfileParam.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == nameof(mProfileParam.K))
                {
                    //重新计算当前幅数据
                    UpdateCurrFrame();
                }
            };

            //TODO, 手动设置样品AD
            //getSample.PropertyChanged+=(s,e)=>
            //{
            //    if (e.PropertyName == nameof(getSample.Samples))
            //    {
            //        //重新计算当前幅数据
            //        UpdateCurrFrame();
            //    }
            //};

            //记录调试数据
            MarkDebugInit();
        }
潘栩锋's avatar
潘栩锋 committed
683 684 685 686

        void mPDetect_ClearEvent(object sender)
        {
            //没信号,清空所有数据!!!
687
            Clear();
688

潘栩锋's avatar
潘栩锋 committed
689 690 691 692 693 694 695
        }
        /// <summary>
        /// 从1幅数据,获取头部,尾部,为于 ADList 的位置
        /// </summary>
        /// <param name="frameinfo"></param>
        /// <param name="index_start"></param>
        /// <param name="index_end"></param>
696
        /// <returns>false 数据不存在</returns>
697
        bool GetFilmInfoADIndex(FRAME_INFO frameinfo, out int index_start, out int index_end)
潘栩锋's avatar
潘栩锋 committed
698 699
        {
            index_start = mADList.No2Index(frameinfo.NoStartAD);
700

潘栩锋's avatar
潘栩锋 committed
701
            index_end = mADList.No2Index(frameinfo.NoEndAD);
702 703 704

            if ((index_start < 0) || (index_end < 0) ||
                (index_start >= mADList.Count) || (index_end >= mADList.Count))
潘栩锋's avatar
潘栩锋 committed
705 706 707 708 709 710 711
            {
                //数据已经不存在,删除
                return false;
            }
            return true;
        }

潘栩锋's avatar
潘栩锋 committed
712
        void mPDetect_FilmInfoChangedEvent(object sender, FilmInfoChangedEventArgs e)
潘栩锋's avatar
潘栩锋 committed
713
        {
714
            var ret = ADListFastSearchIndex(e.Time, out int idx);
715
            switch (ret)
潘栩锋's avatar
潘栩锋 committed
716
            {
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
                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
734
            }
735

潘栩锋's avatar
潘栩锋 committed
736 737 738 739
        }

        void RenZiJiaFix_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
740
            if (e.PropertyName == nameof(NBolts))
潘栩锋's avatar
潘栩锋 committed
741 742 743 744 745
            {
                //所有数据删除
                mFrameInfoList.Clear();

            }
746
            else if (e.PropertyName == nameof(OrgBoltNo))
潘栩锋's avatar
潘栩锋 committed
747 748 749 750 751 752 753 754 755 756 757
            {
                //TODO 只处理3幅图
                int idx = mFrameInfoList.Count - 3;
                if (idx < 0)
                    idx = 0;

                if (idx >= mFrameInfoList.Count)
                    return;
                mADListToDoNo_ToFilmInfo = mFrameInfoList[idx].NoStartAD;
            }
        }
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
        BulkDbTempFrameChangedEventArgs GetTempFrame()
        {
            if (mFrameInfoList.Count() > 0)
            {
                var frameInfo = mFrameInfoList.Last();
                var thicks = IsNeedMap() ? frameInfo.frame_map : frameInfo.frame;

                return new BulkDbTempFrameChangedEventArgs()
                {
                    Time = frameInfo.StartTime,
                    EndTime = frameInfo.EndTime,
                    StartIndex = 0,
                    D = thicks
                };

            }
            else
            {
                return new BulkDbTempFrameChangedEventArgs();
            }
        }

780
        #region 记录测试数据
潘栩锋's avatar
潘栩锋 committed
781 782 783 784 785 786 787 788 789
        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,
790
                () =>
潘栩锋's avatar
潘栩锋 committed
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
                    //mPDetect.GetSignList(markdebug_dt, (asyncContext, retData) =>
                    //{
                    //    var reponse = retData as GetSignListReponse;
                    //    var cells = reponse.datas;
                    //    if (cells.Count > 0)
                    //    {
                    //        orgHistoryDb.AddSignData(
                    //            new Lc_Sign()
                    //            {
                    //                Time = cells.First().Time,
                    //                Signs = cells.ToArray()
                    //            });
                    //    }
                    //}, null);
                    //mPDetect.GetRollList(markdebug_dt, (asyncContext, retData) =>
                    //{
                    //    var reponse = retData as GetRollListReponse;
                    //    var cells = reponse.datas;
                    //    if (cells.Count > 0)
                    //    {
                    //        orgHistoryDb.AddRollData(
                    //            new Lc_Roll()
                    //            {
                    //                Time = cells.First().dt,
                    //                Signs = cells.ToArray()
                    //            });
                    //    }
                    //}, null);
820
                    this.GetADList(markdebug_dt, (asyncContext, retData) =>
潘栩锋's avatar
潘栩锋 committed
821
                    {
822 823
                        var reponse = retData as GetADListReponse;
                        var cells = reponse.datas;
潘栩锋's avatar
潘栩锋 committed
824 825
                        if (cells.Count > 0)
                        {
826 827 828 829
                            var thicks = cells.Select(c => c.thk).ToArray();
                            historyDb.AddTrendData(
                                new Lc_TrendData()
                                {
830
                                    Time = cells.First().dt,
潘栩锋's avatar
潘栩锋 committed
831
                                    K = mProfileParam.K,
832

潘栩锋's avatar
潘栩锋 committed
833 834 835 836 837 838 839 840
                                    Thicks = thicks
                                });
                        }
                    }, null);
                    markdebug_dt = DateTime.Now;
                },
                TimeSpan.FromMinutes(1));
        }
841 842
        #endregion

843
        void UpdateCurrFrame()
潘栩锋's avatar
潘栩锋 committed
844 845
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
846
                new FObjBase.PollModule.PollHandler(delegate ()
潘栩锋's avatar
潘栩锋 committed
847
                {
848
                    if (mFrameInfoList.Count > 0)
潘栩锋's avatar
潘栩锋 committed
849 850 851 852 853
                    {
                        mADListToDoNo_ToFilmInfo = mFrameInfoList.Last().NoStartAD;
                    }

                }), this, MARKNO_CALFRAME);
854 855
        }

潘栩锋's avatar
潘栩锋 committed
856 857


潘栩锋's avatar
潘栩锋 committed
858 859
        TimeSpan datOf1s_ts = TimeSpan.Zero;
        TimeSpan datOf1s_interval = TimeSpan.FromSeconds(1);
860
        List<int> datOf1s = new List<int>();//1秒数据缓存,用于求均值
861 862
        void ClearDatOf1s()
        {
863 864 865
            datOf1s_ts = TimeSpan.Zero;
            datOf1s.Clear();
        }
潘栩锋's avatar
潘栩锋 committed
866 867 868 869 870
        void flyad_TimeGridEvent(object sender, FlyADBase.TimeGridEventArgs e)
        {
            bool issample = false;

            TimeSpan ts = e.Ts;//准确
871 872 873
            //不使用AD盒的时间
            //DateTime dt = e.Time;//准确
            DateTime dt = DateTime.Now;//数据的尾
874

潘栩锋's avatar
潘栩锋 committed
875
            int[] data = e.Data;
876 877
            //这个数据包的总耗时
            TimeSpan data_interval = TimeSpan.FromTicks((long)(ts.TotalMilliseconds * data.Count() * TimeSpan.TicksPerMillisecond));
潘栩锋's avatar
潘栩锋 committed
878

879 880 881 882
            //这个数据包开始的时间
            DateTime data_begin_dt = dt - data_interval;
            //当前循环处理的数据
            DateTime datOf100ms_dt = data_begin_dt - ts;
883
            for (int i = 0; i < data.Length; i++)
潘栩锋's avatar
潘栩锋 committed
884 885 886
            {
                if (issample)//采样中,数据都是无效的
                {
潘栩锋's avatar
潘栩锋 committed
887
                    datOf1s.Add(Misc.MyBase.NULL_VALUE);
潘栩锋's avatar
潘栩锋 committed
888 889 890
                }
                else if (data[i] > UInt16.MaxValue)
                {
潘栩锋's avatar
潘栩锋 committed
891
                    datOf1s.Add(Misc.MyBase.NULL_VALUE);
潘栩锋's avatar
潘栩锋 committed
892 893 894
                }
                else if (data[i] < 0)
                {
潘栩锋's avatar
潘栩锋 committed
895
                    datOf1s.Add(Misc.MyBase.NULL_VALUE);
潘栩锋's avatar
潘栩锋 committed
896 897 898
                }
                else
                {
潘栩锋's avatar
潘栩锋 committed
899
                    datOf1s.Add(data[i]);
潘栩锋's avatar
潘栩锋 committed
900 901 902
                }


903 904 905

                datOf1s_ts = datOf1s_ts + ts;
                datOf100ms_dt = datOf100ms_dt + ts;
906 907

                if (datOf1s_ts >= datOf1s_interval)//1s 一个数据
潘栩锋's avatar
潘栩锋 committed
908
                {
909

潘栩锋's avatar
潘栩锋 committed
910 911
                    int avgad = Misc.MyBase.NULL_VALUE;

潘栩锋's avatar
潘栩锋 committed
912
                    if (datOf1s.Count() > 0)
潘栩锋's avatar
潘栩锋 committed
913
                    {
914
                        avgad = Misc.MyMath.Avg(datOf1s);
潘栩锋's avatar
潘栩锋 committed
915 916 917
                    }
                    else if (mADList.Count() > 0)
                    {
潘栩锋's avatar
潘栩锋 committed
918
                        avgad = mADList.Last().Ad;
潘栩锋's avatar
潘栩锋 committed
919 920
                    }

潘栩锋's avatar
潘栩锋 committed
921

潘栩锋's avatar
潘栩锋 committed
922 923 924
                    mADList.RAdd(
                        new ADCell()
                        {
潘栩锋's avatar
潘栩锋 committed
925 926
                            Time = datOf100ms_dt,
                            Ad = avgad
潘栩锋's avatar
潘栩锋 committed
927
                        });
928

929
                    ClearDatOf1s();
潘栩锋's avatar
潘栩锋 committed
930 931 932
                }
            }
        }
潘栩锋's avatar
潘栩锋 committed
933

潘栩锋's avatar
潘栩锋 committed
934 935 936 937 938

        private void InitList()//初始化列表
        {
            if (mADList == null)
            {
潘栩锋's avatar
潘栩锋 committed
939
                mADList = new RList<ADCell>(4000);
潘栩锋's avatar
潘栩锋 committed
940 941
                mADListToDoNo = mADList.GetLastNo();
                mADListToDoNo_ToFilmInfo = mADListToDoNo;
942 943 944
            }
            if (mFrameInfoList == null)
                mFrameInfoList = new RList<FRAME_INFO>(6);
潘栩锋's avatar
潘栩锋 committed
945 946 947
        }


948

潘栩锋's avatar
潘栩锋 committed
949 950 951
        //复位
        void Clear()
        {
952 953
            if (mADList != null)
                mADList.Clear();
潘栩锋's avatar
潘栩锋 committed
954 955 956 957
            if (mFrameInfoList != null)
            {
                mFrameInfoList.Clear();
            }
958
            ClearDatOf1s();
潘栩锋's avatar
潘栩锋 committed
959 960 961 962
        }

        int Angle2BoltIndex(double angle)
        {
963
            if (angle < 0)
潘栩锋's avatar
潘栩锋 committed
964
            {
965
                angle += 360;
潘栩锋's avatar
潘栩锋 committed
966 967
            }

968
            int boltindex = (int)(OrgBoltNo - BoltNo1st + angle * NBolts / 360);
潘栩锋's avatar
潘栩锋 committed
969 970 971 972 973 974 975 976 977 978
            if (boltindex >= NBolts)
            {
                boltindex -= NBolts;
            }
            else if (boltindex < 0)//不应该发生,可能是 OrgBoltNo 出错了。
            {
                boltindex += NBolts;
            }
            return boltindex;
        }
979

980
        enum ADListFastSearchIndexResult
981 982 983 984 985 986 987 988 989 990 991 992 993 994
        {
            /// <summary>
            /// 时间点在列表以前;
            /// </summary>
            Before = -1,
            /// <summary>
            /// 时间点在列表中;
            /// </summary>
            Now = 0,
            /// <summary>
            /// 时间点在列表的未来;
            /// </summary>
            Future = 1
        }
潘栩锋's avatar
潘栩锋 committed
995
        /// <summary>
996 997 998
        /// 找到在 dt 前最近的 ADList 序号;
        /// 当ADList 没有数据,返回 Future;
        /// 已知AD列表每个数据之间的时间差,快速推断结果;
潘栩锋's avatar
潘栩锋 committed
999 1000 1001 1002
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="index"></param>
        /// <returns></returns>
1003
        ADListFastSearchIndexResult ADListFastSearchIndex(DateTime dt, out int index)
潘栩锋's avatar
潘栩锋 committed
1004 1005 1006
        {
            index = 0;

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


1011 1012 1013 1014
            if (dt > mADList.Last().Time)
                return ADListFastSearchIndexResult.Future;
            else if (dt < mADList.First().Time)
                return ADListFastSearchIndexResult.Before;
潘栩锋's avatar
潘栩锋 committed
1015 1016
            else
            {
1017 1018 1019 1020 1021 1022 1023 1024 1025

                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;
1026

1027
                if (mADList[index].Time == dt)
潘栩锋's avatar
潘栩锋 committed
1028
                {
1029
                    //已经找到就是它!!!!
潘栩锋's avatar
潘栩锋 committed
1030
                }
1031
                else if (mADList[index].Time > dt)
潘栩锋's avatar
潘栩锋 committed
1032
                {
1033 1034 1035 1036 1037 1038 1039 1040
                    while (true)
                    {
                        index--;
                        if (index <= 0)
                        {
                            index = 0;//最前面一个了
                            break;
                        }
1041
                        if (mADList[index].Time <= dt)
1042 1043 1044 1045
                        {
                            //找到了
                            break;
                        }
潘栩锋's avatar
潘栩锋 committed
1046
                    }
1047
                }
潘栩锋's avatar
潘栩锋 committed
1048 1049
                else
                {
1050
                    while (true)
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
                    {
                        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
1070 1071
                    }
                }
1072

1073
                return ADListFastSearchIndexResult.Now;
潘栩锋's avatar
潘栩锋 committed
1074 1075 1076 1077 1078 1079
            }
        }




1080
        void OnPoll_UpdateADList()
潘栩锋's avatar
潘栩锋 committed
1081
        {
1082

潘栩锋's avatar
潘栩锋 committed
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
            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);

1094 1095
                if (!OnPoll_UpdateADList_Edge(i))
                    return;
潘栩锋's avatar
潘栩锋 committed
1096 1097 1098
            }
            mADListToDoNo = mADList.Index2No(mADList.Count);
        }
1099 1100 1101 1102

        #region 不同测量模式的ADList数据更新
        bool OnPoll_UpdateADList_Edge(int idx)
        {
潘栩锋's avatar
潘栩锋 committed
1103
            FilmInfo filminfo;
潘栩锋's avatar
潘栩锋 committed
1104
            int ret = mPDetect.GetFilmInfo(out filminfo, mADList[idx].Time, 2.3, 0);
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
            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
1120 1121 1122 1123 1124 1125
            adcell.Angles.Clear();
            adcell.Angles.Add(filminfo.angle1);
            adcell.Direction = filminfo.direction;
            adcell.Velocity = filminfo.filmVelocity;
            adcell.RotationCnt = filminfo.rotationCnt;
            adcell.InCV = filminfo.inCV;
1126 1127 1128 1129
            return true;
        }

        #endregion
潘栩锋's avatar
潘栩锋 committed
1130 1131 1132 1133 1134 1135
        /// <summary>
        /// 状态不一样,人字架已经转了新一圈,需要增加新一幅数据
        /// </summary>
        /// <param name="adcell"></param>
        /// <param name="frameinfo"></param>
        /// <returns></returns>
1136
        bool FrameInfoCheckToNew(ADCell adcell, FRAME_INFO frameinfo)
潘栩锋's avatar
潘栩锋 committed
1137
        {
潘栩锋's avatar
潘栩锋 committed
1138
            if ((adcell.Direction != frameinfo.direction) ||
1139 1140
                (adcell.RotationCnt != frameinfo.rotationCnt))
            //(Math.Abs(adcell.velocity - frameinfo.velocity) > 2))
潘栩锋's avatar
潘栩锋 committed
1141 1142 1143
            {
                return true;
            }
1144
            else
潘栩锋's avatar
潘栩锋 committed
1145 1146 1147 1148 1149
            {
                return false;
            }
        }

1150 1151


潘栩锋's avatar
潘栩锋 committed
1152 1153 1154 1155

        /// <summary>
        /// 输出的数据都必须是稳定,可靠的。 生成一幅幅测厚仪数据!!!!!!!!!!!!
        /// </summary>
1156
        void OnPoll_UpdateFrameInfo()
潘栩锋's avatar
潘栩锋 committed
1157
        {
1158 1159


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

潘栩锋's avatar
潘栩锋 committed
1164 1165
            if (adlist_index < 0)//全部都要处理
                adlist_index = 0;
1166

潘栩锋's avatar
潘栩锋 committed
1167
            //当没有任何一幅数据时, frameinfo_index = mFrameInfoList.Count = 0
1168
            int frameinfo_index = Math.Max(mFrameInfoList.Count - 1, 0);
潘栩锋's avatar
潘栩锋 committed
1169 1170

            //查找adlist_index 属于哪个frameinfo;
1171 1172
            //当找到,清空里面的.frame,一切重新开始!!!
            //当不能找到,说明只是接着上次的处理而已,不需要重新开始
潘栩锋's avatar
潘栩锋 committed
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
            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);//已经成功获取完位置信息的数据尽头
1195
            for (int i = adlist_index; (i < mADList.Count && i < adlist_todo_index); i++)
潘栩锋's avatar
潘栩锋 committed
1196 1197 1198
            {
                ADCell adcell = mADList[i];
                //mADListToDoNo_ToFilmInfo = mADList.Index2No(i);
1199 1200


潘栩锋's avatar
潘栩锋 committed
1201 1202 1203 1204 1205 1206
                FRAME_INFO frameinfo;
                //--------------------------------------------------------------------------------------------------------
                //条件检测
                if (frameinfo_index >= mFrameInfoList.Count)//根本没数据,新建
                {
                    frameinfo = new FRAME_INFO();
潘栩锋's avatar
潘栩锋 committed
1207 1208 1209 1210 1211
                    frameinfo.StartTime = adcell.Time;
                    frameinfo.EndTime = adcell.Time;
                    frameinfo.direction = adcell.Direction;
                    frameinfo.velocity = adcell.Velocity;
                    frameinfo.rotationCnt = adcell.RotationCnt;
潘栩锋's avatar
潘栩锋 committed
1212 1213 1214 1215 1216 1217
                    frameinfo.NoStartAD = mADList.Index2No(i);
                    frameinfo.NoEndAD = mADList.Index2No(i);
                    frameinfo.SetNBolt(NBolts);
                    mFrameInfoList.RAdd(frameinfo);
                    frameinfo_index = mFrameInfoList.Count - 1;
                }
1218
                else
潘栩锋's avatar
潘栩锋 committed
1219 1220
                {
                    frameinfo = mFrameInfoList[frameinfo_index];
1221
                    if (i <= mADList.No2Index(frameinfo.NoEndAD))
潘栩锋's avatar
潘栩锋 committed
1222
                    {
1223 1224 1225
                        //当为新数据, i应该>mADList.No2Index(frameinfo.NoEndAD)
                        //能进这里,证明是要重新计算
                        //清空数据,重新来过
1226

潘栩锋's avatar
潘栩锋 committed
1227 1228 1229 1230 1231
                        frameinfo.StartTime = adcell.Time;
                        frameinfo.EndTime = adcell.Time;
                        frameinfo.direction = adcell.Direction;
                        frameinfo.velocity = adcell.Velocity;
                        frameinfo.rotationCnt = adcell.RotationCnt;
潘栩锋's avatar
潘栩锋 committed
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
                        frameinfo.NoStartAD = mADList.Index2No(i);
                        frameinfo.NoEndAD = mADList.Index2No(i);
                        frameinfo.Clear();
                    }
                }
                //--------------------------------------------------------------------------------------------------------


                if (FrameInfoCheckToNew(adcell, frameinfo))
                {
                    //一幅数据已经完成
1243

1244 1245
                    //重新分区
                    if (IsNeedMap())
潘栩锋's avatar
潘栩锋 committed
1246
                    {
1247
                        //上面已经检查了 Map == null 的情况
1248
                        Dictionary<int, int> m = new Dictionary<int, int>();
潘栩锋's avatar
潘栩锋 committed
1249
                        for (int j = 0; j < Map.Count(); j++)
1250 1251
                            m.Add(Map[j].OldNo - 1, Map[j].NewNo - 1);
                        frameinfo.frame_map = Misc.MyMath.Map(frameinfo.frame, m);
潘栩锋's avatar
潘栩锋 committed
1252 1253
                    }

1254
                    //更新数据库内容
潘栩锋's avatar
潘栩锋 committed
1255
                    {
1256
                        double[] frame;
1257 1258 1259
                        List<BoltMapCell> map;
                        if (IsNeedMap())
                        {
1260
                            //上面已经检查了 Map == null 的情况
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
                            frame = frameinfo.frame_map;
                            map = new List<BoltMapCell>();
                            foreach (var m in Map)
                                map.Add(m.Clone());
                        }
                        else
                        {
                            frame = frameinfo.frame;
                            map = null;
                        }
1271 1272 1273
                        //限制小数点
                        for (int j = 0; j < frame.Count(); j++)
                        {
1274
                            if (!double.IsNaN(frame[j]))
1275 1276
                                frame[j] = Math.Round(frame[j], 2);
                        }
1277 1278 1279 1280 1281 1282 1283 1284 1285

                        frameinfo.renZiJiaDataEventArgs = new RenZiJiaDataEventArgs()
                        {
                            Time = frameinfo.StartTime,
                            EndTime = frameinfo.EndTime,
                            IsBackw = frameinfo.direction == DIRECTION.BACKWARD,
                            RPeriod = mPDetect.RenZiJiaPeriod,
                            RCnt = frameinfo.rotationCnt,
                            OrgBoltNo = OrgBoltNo,
1286 1287 1288 1289 1290
                            RAngle = Math.Round(mPDetect.RAngle, 2),
                            FilmLength = Math.Round(mPDetect.FilmLength, 2),
                            FilmVelocity = Math.Round(mPDetect.FilmVelocity, 2),
                            K = Math.Round(mProfileParam.K, 3),
                            Thicks = frame,
1291 1292 1293 1294
                            Boltmap = map
                        };
                        frameinfo.renZiJiaDataEventArgsHasChanged = true;
                    }
潘栩锋's avatar
潘栩锋 committed
1295 1296 1297 1298 1299 1300 1301 1302 1303

                    //更新到下一个frameinfo
                    frameinfo_index++;


                    i--;
                    continue;
                }

1304
                double thk = Ad2Thk(adcell.Ad);
1305 1306
                //均值,更新frameinfo.frame
                frameinfo.SetFrameValue(Angle2BoltIndex(adcell.Angles.First()), thk, adcell.Time);
潘栩锋's avatar
潘栩锋 committed
1307

潘栩锋's avatar
潘栩锋 committed
1308
                frameinfo.EndTime = adcell.Time;
潘栩锋's avatar
潘栩锋 committed
1309 1310 1311
                frameinfo.NoEndAD = mADList.Index2No(i);
            }
            mADListToDoNo_ToFilmInfo = mADListToDoNo;
1312

潘栩锋's avatar
潘栩锋 committed
1313 1314

            //检测数据更新情况,放入bulkdata
1315
            for (int i = 0; i < mFrameInfoList.Count; i++)
潘栩锋's avatar
潘栩锋 committed
1316 1317 1318
            {
                FRAME_INFO frameinfo = mFrameInfoList[i];

1319
                if (frameinfo.HasChanged)//数据检测过,需要更新bulkdata里面的数据, 大部分发生在扫描过程中,还没结束一幅图
潘栩锋's avatar
潘栩锋 committed
1320
                {
1321 1322
                    //重新分区
                    if (IsNeedMap())
潘栩锋's avatar
潘栩锋 committed
1323
                    {
1324
                        //上面已经检查了 Map == null 的情况
1325 1326 1327 1328 1329
                        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
1330

1331 1332
                    //TODO 更新即时图!!!!
                    //var thicks = IsNeedMap() ? frameinfo.frame_map : frameinfo.frame;
1333 1334
                }
            }
潘栩锋's avatar
潘栩锋 committed
1335

1336 1337
            //检查数据是否被修改, 推送出去!!!!!
            if (mFrameInfoList.Count() > 0)
1338
            {
1339 1340
                FRAME_INFO frameinfo = mFrameInfoList.Last();
                if (frameinfo.HasChanged)
潘栩锋's avatar
潘栩锋 committed
1341
                {
1342 1343 1344
                    var thicks = IsNeedMap() ? frameinfo.frame_map : frameinfo.frame;
                    //TODO, 推送的数据,可能太多了!!!!
                    bulkDb.SetTempFrame(frameinfo.StartTime, frameinfo.EndTime, 0, thicks);
1345 1346
                }
            }
潘栩锋's avatar
潘栩锋 committed
1347

1348 1349 1350 1351 1352 1353 1354 1355 1356
            foreach (var frameinfo in mFrameInfoList)
            {
                frameinfo.Submit();
            }

            #region sqlite3 历史数据保存
            if (mFrameInfoList.Any((frameinfo) => frameinfo.renZiJiaDataEventArgsHasChanged))
            {
                //下辊检测
1357
                if (historyDb.localDb.IsProfileFinished)
1358 1359
                {
                    AddProfile();
潘栩锋's avatar
潘栩锋 committed
1360 1361 1362
                }
            }

1363
            //检测数据更新情况,放入数据库, 数据不能被修改
1364
            
1365

1366 1367
            RenZiJiaDataEventArgs renZiJiaDataEventArgs = null;//当数据被修改,推送事件!!!,当只有最后一幅图被推送

1368 1369 1370 1371 1372 1373 1374 1375
            foreach (var frameinfo in mFrameInfoList)
            {
                if (frameinfo.renZiJiaDataEventArgsHasChanged)
                {
                    frameinfo.renZiJiaDataEventArgsHasChanged = false;

                    if (frameinfo.scandata_id == null)
                    {
1376 1377
                        if (frameinfo.renZiJiaDataEventArgs.FilmVelocity < 2)
                        {
1378 1379 1380
                            //异常
                            continue;
                        }
1381
                        //添加数据
1382
                        var lc_scandata = Lc_AutoMapperProfile.Mapper.Map<Lc_ScanData>(frameinfo.renZiJiaDataEventArgs);
1383

1384 1385 1386 1387 1388 1389
                        if (!mDynArea.SecuteLock)//授权过期, 没有数据
                        {
                            historyDb.AddScanData(lc_scandata);
                            frameinfo.scandata_id = lc_scandata.ID;
                            renZiJiaDataEventArgs = frameinfo.renZiJiaDataEventArgs;
                        }
1390 1391 1392 1393 1394 1395 1396
                    }
                }
            }
            #endregion
            //当数据被修改,只推送最后一次的数据
            if (renZiJiaDataEventArgs != null)
                DataEvent?.Invoke(this, renZiJiaDataEventArgs);
潘栩锋's avatar
潘栩锋 committed
1397
        }
1398

1399

潘栩锋's avatar
潘栩锋 committed
1400 1401 1402 1403 1404 1405
        protected override void OnPoll()
        {
            OnPoll_UpdateADList();
            OnPoll_UpdateFrameInfo();
        }

1406
        public override void Start()
潘栩锋's avatar
潘栩锋 committed
1407
        {
1408
            //AD盒有没断开,也能触发,一直触发中!!!
潘栩锋's avatar
潘栩锋 committed
1409
            IsRunning = true;
1410 1411 1412 1413 1414

            #region sqlite3 历史数据保存
            CheckProfile();
            mProfileParam.PropertyChanged += MProfileParam_PropertyChanged;
            #endregion
潘栩锋's avatar
潘栩锋 committed
1415 1416 1417 1418 1419 1420

            FObjBase.PollModule.Current.Poll_Config(
                FObjBase.PollModule.POLL_CONFIG.ADD,
                onpoll_func,
                TimeSpan.FromSeconds(1));
        }
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
        #region sqlite3 历史数据保存
        string[] profile_propertynames_add = new string[]{
                "PName",
                "OrderNo",
                "Number"
            };
        string[] profile_propertynames_update = new string[]{
                "Target",
                "TolerancePercent"
            };
        void CheckProfile()
        {
1433
            if (historyDb.localDb.IsProfileFinished)
1434 1435 1436 1437 1438 1439
            {
                //复位 膜纵向位置
                AddProfile();
            }
            else
            {
1440
                if (historyDb.localDb.CurrProfile != null)
1441
                {
1442
                    Db_Profile dB_Profile = historyDb.localDb.CurrProfile;
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
                    if ((dB_Profile.PName != mProfileParam.PName) ||
                    (dB_Profile.OrderNo != mProfileParam.OrderNo) ||
                    (dB_Profile.Number != mProfileParam.Number))
                    {
                        AddProfile();
                    }
                    else
                    {
                        //继续上一次生产!!!!
                        UpdateProfile();


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

            }
        }
        void AddProfile()
        {
1467
            historyDb.AddProfile(new Db_Profile()
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
            {
                PName = mProfileParam.PName,
                OrderNo = mProfileParam.OrderNo,
                Number = mProfileParam.Number,
                Target = mProfileParam.Target,
                TolerancePercent = mProfileParam.TolerancePercent,
                StartTime = DateTime.Now,
                EndTime = DateTime.Now
            });
        }
        void UpdateProfile()
        {
1480
            historyDb.UpdateProfile(new Db_Profile()
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
            {
                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(
                    () =>
                    {
1496
                        if (!historyDb.localDb.IsProfileFinished)
1497
                        {
1498
                            historyDb.FinishProfile();
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
                        }
                        AddProfile();//新
                    }, this, MARKNO_PROFILE_ADD);
            }
            else if (profile_propertynames_update.Contains(e.PropertyName))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    () =>
                    {
                        UpdateProfile();//修改
                    }, this, MARKNO_PROFILE_CHANGED);
            }
        }
        #endregion
1513 1514

        string file_path = "renzijiafix.json";
潘栩锋's avatar
潘栩锋 committed
1515 1516
        bool Load()
        {
1517
            if (BlowingFixJsonDb.Load(this, file_path))
1518
            {
1519
                return true;
1520 1521 1522 1523 1524 1525
            }
            else//只是为了版本兼容,以后就会删除
            {
                var jsonDb = FLY.Thick.Blowing.Server.Update.GM_BlowingFix.ToJsonDb();
                if (jsonDb == null)
                    return false;
1526
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonDb);
1527 1528
                File.WriteAllText(file_path, json);

1529
                return BlowingFixJsonDb.Load(this, file_path);
1530
            }
潘栩锋's avatar
潘栩锋 committed
1531
        }
1532 1533 1534 1535
        bool Save()
        {
            return BlowingFixJsonDb.Save(this, file_path);
        }
潘栩锋's avatar
潘栩锋 committed
1536

潘栩锋's avatar
潘栩锋 committed
1537
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
1538
        /// 获取 厚度列表
潘栩锋's avatar
潘栩锋 committed
1539 1540
        /// </summary>
        /// <param name="begin">开始时间</param>
1541 1542 1543 1544
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [Call(typeof(GetADListReponse))]
        public void GetADList(DateTime begin, AsyncCBHandler asyncDelegate, object asyncContext)
潘栩锋's avatar
潘栩锋 committed
1545
        {
潘栩锋's avatar
潘栩锋 committed
1546
            var p = new GetADListReponse
潘栩锋's avatar
潘栩锋 committed
1547
            {
1548
                datas = mADList.FindAll(a => a.Time > begin).Select(a => new ADSingle()
1549 1550 1551 1552
                {
                    dt = a.Time,
                    thk = Ad2Thk(a.Ad)
                }).ToList()
潘栩锋's avatar
潘栩锋 committed
1553
            };
潘栩锋's avatar
潘栩锋 committed
1554

1555
            asyncDelegate(asyncContext, p);
潘栩锋's avatar
潘栩锋 committed
1556 1557
        }

潘栩锋's avatar
潘栩锋 committed
1558
    }
潘栩锋's avatar
潘栩锋 committed
1559

1560 1561
    public class BlowingFixJsonDb
    {
1562
        static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c => {
1563 1564
            c.CreateMap<GM_BlowingFix, BlowingFixJsonDb>().ReverseMap();
        }));
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606

        public static bool Load(GM_BlowingFix src, string file_path)
        {
            if (File.Exists(file_path))
            {
                try
                {
                    string json = File.ReadAllText(file_path);
                    var jsonDb = JsonConvert.DeserializeObject<BlowingFixJsonDb>(json);
                    Mapper.Map(jsonDb, src);
                    return true;
                }
                catch
                {
                    //异常,没有json 解码失败

                }
            }

            return false;
        }
        public static bool Save(GM_BlowingFix src, string file_path)
        {
            try
            {
                var jsonDb = Mapper.Map<BlowingFixJsonDb>(src);
                string json = JsonConvert.SerializeObject(jsonDb, Formatting.Indented);
                File.WriteAllText(file_path, json);
                return true;
            }
            catch
            {
                //异常,没有json 编码失败

            }
            return false;
        }


        public int ChannelCnt = 88;
        public int BPC = 1;
        public int OrgBoltNo = 1;
1607 1608
        public bool IsUsedMap;
        public BoltMapCell[] Map;
1609
        public bool IsProbeRight = true;
1610
        public bool IsForwCW;
1611 1612
    }

1613

1614
}
1615
namespace FLY.Thick.Blowing.Server.Update
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
{
    public class GM_BlowingFix : ISaveToXml
    {
        /// <summary>
        /// 加热通道数
        /// </summary>
        public int ChannelCnt { get; set; } = 88;

        /// <summary>
        /// 分区数/加热通道数
        /// </summary>
        public int BPC { get; set; } = 1;

        /// <summary>
        /// 复位时,探头测量的位置对应分区号
        /// </summary>
        public int OrgBoltNo { get; set; } = 1;

        /// <summary>
        /// 使用分区表
        /// </summary>
        public bool IsUsedMap { get; set; } = false;


        /// <summary>
        /// 分区表,必须保证不能为null
        /// </summary>
1643
        public List<BoltMapCell> Map { get; } = new List<BoltMapCell>();
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662

        static GM_BlowingFix()
        {
            Misc.SaveToXmlHepler.Regist(typeof(BoltMapCell));
        }

        #region ISaveToXml
        public string[] GetSavePropertyNames()
        {
            return new string[]{
                nameof(ChannelCnt),
                nameof(BPC),
                nameof(OrgBoltNo),
                nameof(IsUsedMap),
                nameof(Map)
            };
        }
        #endregion

1663
        public static BlowingFixJsonDb ToJsonDb()
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
        {
            if (File.Exists("renzijiafix.xml"))
            {//只是为了版本兼容,以后就会删除

                FLY.Thick.Blowing.Server.Update.GM_BlowingFix blowingFix = new Update.GM_BlowingFix();

                Misc.SaveToXmlHepler.Load("renzijiafix.xml", blowingFix);

                FLY.Thick.Blowing.Server.BlowingFixJsonDb jsonDb = new BlowingFixJsonDb();
                jsonDb.ChannelCnt = blowingFix.ChannelCnt;
                jsonDb.BPC = blowingFix.BPC;
                jsonDb.OrgBoltNo = blowingFix.OrgBoltNo;
                jsonDb.IsUsedMap = blowingFix.IsUsedMap;
                jsonDb.Map = blowingFix.Map.ToArray();
1678
                return jsonDb;
1679 1680 1681 1682 1683
            }
            else
                return null;
        }
    }
潘栩锋's avatar
潘栩锋 committed
1684
}