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

using MathNet.Numerics.LinearAlgebra.Double;


using Misc;
using FLY.Thick.BulkDataModule;
using FlyADBase;
using FLY.Thick.Base.Common;

using FLY.Thick.BlowingScan.IService;
using FLY.Thick.Base.Server;
using FObjBase;
using FLY.Thick.BlowingScan.Common;
using FLY.Thick.Blowing.IService;
using FLY.Thick.Blowing.Server;
潘栩锋's avatar
潘栩锋 committed
23 24 25
using FLY.Thick.Blowing.Server.Model;

using HistoryDb = FLY.Thick.BlowingScan.Server.Model.HistoryDb;
潘栩锋's avatar
潘栩锋 committed
26 27 28 29 30 31 32

namespace FLY.Thick.BlowingScan.Server
{
    public class GM_BlowingScan:GM_ScanMotion, IBlowingScanService,ISaveToXml
    {
        #region 延时操作,markno
        const int MARKNO_SAVE = 1;
潘栩锋's avatar
潘栩锋 committed
33 34
        const int MARKNO_PROFILE_ADD = 87;
        const int MARKNO_PROFILE_CHANGED = 88;
潘栩锋's avatar
潘栩锋 committed
35 36
        #endregion

潘栩锋's avatar
潘栩锋 committed
37

潘栩锋's avatar
潘栩锋 committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        /// <summary>
        ///边界查找模块 
        /// </summary>
        private BorderSearch mBorderSearch;


        /// <summary>
        ///人字架位置检测器 
        /// </summary>
        public BlowingDetect mPDetect;
        
        /// <summary>
        /// AD值转厚度值 
        /// </summary>
        private AD2ThickHandler AD2Thick;

        /// <summary>
        /// 扫描数据库
        /// </summary>
        private IBulkDataServiceAdd mBulkData;

潘栩锋's avatar
潘栩锋 committed
59 60 61 62 63 64
        BlowingProfileParam mProfileParam;

        HistoryDb mHistoryDb;

        BulkDb mBulkDb;

潘栩锋's avatar
潘栩锋 committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 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
        /// <summary>
        /// 上幅数据解方程失败,bookmark被释放出来
        /// </summary>
        private int freebm=-1;
        private int GetFreeBm() 
        {

            int r = freebm;
            freebm = -1;
            return r;
        }
        private void SetFreeBm(int bm) 
        {
            freebm = bm;
        }

        /// <summary>
        /// 动态数据组合
        /// </summary>
        private DynArea mDynArea;
        /// <summary>
        /// 扫描脉冲检测 
        /// </summary>
        private ScanPosDetect mScanPosDetect;
        /// <summary>
        /// 当前扫描号
        /// </summary>
        public int ScanNo =0;
        class ScanPosCell 
        {
            public DateTime dt = DateTime.MinValue;
            public int pos;//位于膜的位置,不是机架
            public int thick;//两层的厚度
            //由pos转换!!
            public double angle1;//上层 位于膜泡的角度
            public double angle2;//下层 位于膜泡的角度
            public Misc.DIRECTION direction;//只有一整副数据都是同一个方向,才能解方程
            public ScanPosCell Clone()
            {
                ScanPosCell scanpos = new ScanPosCell();
                scanpos.dt = dt;
                scanpos.pos = pos;
                scanpos.thick = thick;
                scanpos.angle1 = angle1;
                scanpos.angle2 = angle2;
                scanpos.direction = direction;
                return scanpos;
            }
        }
        class EquationCell
        {
            public Dictionary<int,int> boltPower=new Dictionary<int,int>();//key=boltindex, value=power
            public int thick=0;
            public int mainBolt;//主分区号
            public void Add(int boltindex1, int boltindex2, int thick)
            {
                if (boltPower.ContainsKey(boltindex1))
                {
                    boltPower[boltindex1]++;
                }
                else
                {
                    boltPower.Add(boltindex1, 1);
                }

                if (boltPower.ContainsKey(boltindex2))
                {
                    boltPower[boltindex2]++;
                }
                else
                {
                    boltPower.Add(boltindex2, 1);
                }
                this.thick += thick;
            }
            public override string ToString()
            {
                string str = "";
                foreach (var b in boltPower)
                {
                    str += "[" + b.Key.ToString() + "," + b.Value.ToString() + "] ";
                }
                return str;
            }
            public double[] GetPower(int nbolts) 
            {
                double[] power = new double[nbolts];
                for (int i = 0; i < power.Count(); i++)
                    power[i] = 0;

                foreach (var kv in boltPower) 
                {
                    power[kv.Key] = kv.Value;
                }
                return power;
            }
            public int GetAvg() 
            {
                int cnt=0;
                for (int i = 0; i < boltPower.Count(); i++) 
                {
                    cnt += boltPower.Values.ElementAt(i);
                }
                if (cnt > 0)
                    return thick / cnt;
                else
                    return Misc.MyBase.NULL_VALUE;
            }
        }
        class ScanInfoCell 
        {
            public ScanInfoCell Clone() 
            {
                ScanInfoCell scaninfo = new ScanInfoCell(ScanNo, thicks.Count());
                scaninfo.Direction = Direction;
                scaninfo.ScanNo = ScanNo;
                scaninfo.IsFull = IsFull;
                scaninfo.IndexOfToGetTime = IndexOfToGetTime;
                foreach(ScanPosCell scanpos in frame)
                {
                    scaninfo.frame.Add(scanpos.Clone());
                }
                scaninfo.width = width;
                scaninfo.beginPos = beginPos;
                scaninfo.endPos = endPos;
                scaninfo.lockbm = lockbm;
                return scaninfo;
            }

            public enum GetAngleResult
            {
                /// <summary>
                /// 全部grid 数据已经获取,而且都成功转为方程
                /// </summary>
                AllGet,
                /// <summary>
                /// 部分grid 数据已经获取,这次转化了一些方程
                /// </summary>
                PartGet,
                /// <summary>
                /// 什么都没干,没有转化为任何方程
                /// </summary>
                NothingToDo

            }
            public GetAngleResult GetAngle(BlowingDetect rdetect, int orgboltno, int sensorWidth)
            {
                bool bHasNewData = false;
                for (; IndexOfToGetAngle < IndexOfToGetTime; IndexOfToGetAngle++)
                {
                    int i = IndexOfToGetAngle;
                    ScanPosCell scanpos = frame[i];

                    BlowingDetect.FilmInfo filminfo;
                    if (scanpos.dt == DateTime.MinValue)
                    { 
                    
                    }
                    int ret = rdetect.GetFilmInfo(
                        out filminfo,
                        scanpos.dt,
                        width,
                        scanpos.pos
                        );

                    if (ret == 1)
                    {
                        //发生在未来,等下一次Poll

                        if (IsFull)
                            return GetAngleResult.NothingToDo;
                        else
                        {
                            if (bHasNewData)
                                return GetAngleResult.PartGet;
                            else
                                return GetAngleResult.NothingToDo;
                        }
                    }
                    //成功获取
                    scanpos.angle1 = filminfo.angle1;
                    scanpos.angle2 = filminfo.angle2;
                    scanpos.direction = filminfo.direction;
                    if (IndexOfToGetAngle == 0)
                    {
                        Direction = scanpos.direction;
                    }
                    else
                    {
                        if (Direction != scanpos.direction)
                        {
                            Direction = DIRECTION.FIX;
                        }
                    }
                    //探头直径转为角度范围
                    double sensor_angle = 180.0 * sensorWidth / width;

                    if (AddEquation(i, orgboltno, sensor_angle))
                        bHasNewData = true;
                }

                if (IsFull)
                    return GetAngleResult.AllGet;
                else
                {
                    if (bHasNewData)
                        return GetAngleResult.PartGet;
                    else
                        return GetAngleResult.NothingToDo;
                }
            }


            int GetBoltIndexFromAngle(double a, int orgboltno)
            {
                int boltno1st = 1;
                int nbolts = thicks.Count();
                int boltindex = (int)(a / 360 * nbolts);

                boltindex += orgboltno - boltno1st;

                if (boltindex >= nbolts)
                    boltindex -= nbolts;

                return boltindex;
            }

            int boltindex1_last = -1;
            /// <summary>
            /// 完成了一些方程,返回true
            /// </summary>
            /// <param name="scanpos_idx"></param>
            /// <param name="orgboltno">复位区号</param>
            /// <param name="sensor_angle">探头直径对应的角度范围</param>
            /// <returns></returns>
            bool AddEquation(int scanpos_idx, int orgboltno, double sensor_angle)
            {
                ScanPosCell scanpos = frame[scanpos_idx];

                if (equationList.Count() == 0)//复位记录
                    boltindex1_last = -1;

                EquationCell equation = new EquationCell();
                equationList.Add(equation);

                int boltindex1 = GetBoltIndexFromAngle(scanpos.angle1, orgboltno);
                equation.mainBolt = boltindex1;
                equation.Add(
                    GetBoltIndexFromAngle(scanpos.angle1, orgboltno),
                    GetBoltIndexFromAngle(scanpos.angle2, orgboltno),
                    scanpos.thick);

                //添加直径范围内的方程
                for (int i = 1; i < sensor_angle / 2; i++)
                {
                    equation.Add(
                        GetBoltIndexFromAngle(scanpos.angle1 + i, orgboltno),
                        GetBoltIndexFromAngle(scanpos.angle2 + i, orgboltno),
                        scanpos.thick);

                    equation.Add(
                        GetBoltIndexFromAngle(scanpos.angle1 - i, orgboltno),
                        GetBoltIndexFromAngle(scanpos.angle2 - i, orgboltno),
                        scanpos.thick);
                }
                
                if (boltindex1_last != boltindex1)//记录不同,通知外部是时候解方程了
                {
                    boltindex1_last = boltindex1;
                    return true;
                }
                return false;
            }
            public int GetAvg() 
            {
                if (equationList.Count() < 0)
                    return Misc.MyBase.NULL_VALUE;
                int sum = 0;
                int cnt = 0;
                for (int i = 0; i < equationList.Count(); i++)
                {
                    EquationCell equation = equationList[i];
                    int a = equation.GetAvg();
                    if (Misc.MyBase.ISVALIDATA(a)) 
                    {
                        sum += a;
                        cnt++;
                    }
                }
                if (cnt > 0)
                    return sum / cnt;
                else
                    return Misc.MyBase.NULL_VALUE;
            }
            //public void SaveEquation(StreamWriter sw) 
            //{
                
            //    for (int i = 0; i < equationList.Count(); i++) 
            //    {
            //        EquationCell equation = equationList[i];
            //        string s = ScanNo.ToString();
            //        s +=","+ Direction.ToString();
            //        s += "," + StartTime.ToString();
            //        s+=","+equation.thick.ToString();
            //        for (int j = 0; j <  equation.boltPower.Count(); j++) 
            //        {
            //            int boltindex = equation.boltPower.Keys.ElementAt(j);
            //            s += "," + boltindex.ToString();
            //            s += "," + equation.boltPower[boltindex].ToString();
            //        }
            //        sw.WriteLine(s);
            //    }    
            //}
            //public static void SaveEquation_Header(StreamWriter sw)
            //{
            //    string s = "序号,方向,时间,总厚度,序号1,数量1,序号2,数量2";
            //    sw.WriteLine(s);
            //}
            public ScanInfoCell(int scanNo, int nbolts) 
            {
                Reset(scanNo);
                thicks = new int[nbolts];
                for (int i = 0; i < nbolts; i++) 
                {
                    thicks[i] = Misc.MyBase.NULL_VALUE;
                }
            }
            /// <summary>
            /// 开始测量时间
            /// </summary>
            public DateTime StartTime;
            /// <summary>
            /// 结束测量时间
            /// </summary>
            public DateTime EndTime;
            /// <summary>
            /// 旋转架旋转的方向,当同一次扫描中,有两个方向,Direction= Fix;
            /// </summary>
            public Misc.DIRECTION Direction;
            /// <summary>
            /// 扫描号,必须唯一!!!!!
            /// </summary>
            public int ScanNo;
            /// <summary>
            /// grid数据,已经全部放入!!!!!
            /// </summary>
            public bool IsFull;
            /// <summary>
            /// 准备要去获取时间的ScanPosCell序号, -1, 整个scaninfo 都发生在过去,作废
            /// </summary>
            public int IndexOfToGetTime;
            /// <summary>
            /// 准备要去转角度的ScanPosCell序号
            /// </summary>
            public int IndexOfToGetAngle;
            /// <summary>
            /// 快速解方法,equationList序号
            /// </summary>
            public int IndexOfToSolveEquationQuick;
            public void Reset(int scanNo)
            {
                ScanNo = scanNo;
                frame.Clear();
                equationList.Clear();
                IsFull = false;
                IndexOfToGetTime = 0;
                IndexOfToGetAngle = 0;
                Direction = DIRECTION.FORWARD;
            }
            /// <summary>
            /// 复位数据;一幅grid成功生成,重新获取pos,thick,time,angle,equation
            /// </summary>
            /// <param name="beginPos"></param>
            /// <param name="endPos"></param>
            public void Reset_IsFull(int beginPos, int endPos)
            {
                this.beginPos = beginPos;
                this.endPos = endPos;
                this.width = endPos - beginPos + 1;//膜宽
                this.IsFull = true;

                equationList.Clear();
                frame.Clear();
                IndexOfToGetTime = 0;
                IndexOfToGetAngle = 0;
            }



            void ClearFrame() 
            {
                equationList.Clear();
                frame.Clear();
                IndexOfToGetTime = 0;
                IndexOfToGetAngle = 0;
            }
            public void Reset2GetAngle()
            {
                IndexOfToGetAngle = 0;
                equationList.Clear();
                for (int i = 0; i < thicks.Count(); i++)
                    thicks[i] = Misc.MyBase.NULL_VALUE;
            }
            /// <summary>
            /// 原始数据(时间,位置,总厚度
            /// </summary>
            public List<ScanPosCell> frame=new List<ScanPosCell>();
            /// <summary>
            /// 膜的宽度
            /// </summary>
            public int width=-1;
            /// <summary>
            /// 膜边界左
            /// </summary>
            public int beginPos;
            /// <summary>
            /// 膜边界右
            /// </summary>
            public int endPos;

            /// <summary>
            /// 方程列表
            /// </summary>
            public List<EquationCell> equationList=new List<EquationCell>();
            /// <summary>
            /// 下面解方程得到的一幅数据对应的数据,与 StartTime 不一样
            /// </summary>
            public DateTime thicks_dt;
            /// <summary>
            /// 一幅数据
            /// </summary>
            public int[] thicks;
497 498 499 500
            /// <summary>
            /// 经过分区表后的结果
            /// </summary>
            public int[] thicks_map;
潘栩锋's avatar
潘栩锋 committed
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
            /// <summary>
            /// 对应bulkdata 的数据, -1, 没有对应的bookmark,因为从来都没数据
            /// </summary>
            public int bookmark=-1;
            /// <summary>
            /// 锁定 bookmark,只要解过一次方程,bookmark 就会被锁定,不能再修改
            /// </summary>
            public bool lockbm = false;
            public class SolveQuickResult 
            {
                /// <summary>
                /// 新得到数据开始序号
                /// </summary>
                public int start_index;
                /// <summary>
                /// 新得到数据长度,这是一个环形队列
                /// </summary>
                public int len=0;
            }
            /// <summary>
            /// 快速解方程
            /// </summary>
            /// <param name="org_thicks">上一幅完成的数据</param>
            /// <param name="power">上一幅图所占的权重,当前只占1</param>
            /// <returns></returns>
            public SolveQuickResult SolveQuick(int[] org_thicks, int power) 
            {
                List<int> updateboltindex = new List<int>();
                for (int i = IndexOfToSolveEquationQuick; i < equationList.Count; i++, IndexOfToSolveEquationQuick++)
                {
                    EquationCell equation = equationList[i];

                    
                    int mainpower = equation.boltPower[equation.mainBolt];
                    double sum = 0;
                    for (int j = 0; j < equation.boltPower.Count; j++)
                    {
                        int boltindex = equation.boltPower.Keys.ElementAt(j);

                        if (boltindex != equation.mainBolt)
                        {
                            if (org_thicks == null)
                            {
                                mainpower += equation.boltPower[boltindex];
                            }
                            else if (!Misc.MyBase.ISVALIDATA(org_thicks[boltindex]))
                            {
                                mainpower += equation.boltPower[boltindex];
                            }
                            else
                            {
                                sum += equation.boltPower[boltindex] * org_thicks[boltindex];
                            }
                        }
                    }
                    int mainBolt_value = (org_thicks != null)? org_thicks[equation.mainBolt]:Misc.MyBase.NULL_VALUE;

                    if (Misc.MyBase.ISVALIDATA(mainBolt_value))
                    {
                        thicks[equation.mainBolt] = (mainBolt_value * power + (int)((equation.thick - sum) / mainpower)) / (power + 1);
                    }
                    else
                    {
                        thicks[equation.mainBolt] = (int)((equation.thick - sum) / mainpower);
                    }

                    updateboltindex.Add(equation.mainBolt);
                }
                SolveQuickResult result = new SolveQuickResult();
                if (updateboltindex.Count > 0)
                {
                    result.start_index = updateboltindex.Min();
                    result.len = updateboltindex.Max() - updateboltindex.Min() + 1;
                }
                return result;



            }
        }
        /// <summary>
        /// 每次扫描的数据, 
        /// 不是每一幅数据都能解出方程,
        /// 因为要连续3幅才能解出方程
        /// </summary>
        private RList<ScanInfoCell> mScanInfoList;
        /// <summary>
        /// 与 mScanInfoList 基本一样
        /// </summary>
        private RList<FlyData_BlowingScan> mBufList;
        #region ISaveToXml
        public string[] GetSavePropertyNames()
        {
            return new string[]{
                "ChannelCnt",
                "BPC",
                "OrgBoltNo",
                "SolveCnt",
                "Smooth",
                "SensorWidth",
                "IsUsedMap",
                "Map"
            };
        }
        #endregion

        #region IRenZiJiaService




        #region 分区设置

614

潘栩锋's avatar
潘栩锋 committed
615 616 617
        /// <summary>
        /// 加热通道数
        /// </summary>
618 619
        public int ChannelCnt { get; set; } = 88;

潘栩锋's avatar
潘栩锋 committed
620 621 622 623

        /// <summary>
        /// 分区数/加热通道数
        /// </summary>
624
        public int BPC { get; set; } = 1;
潘栩锋's avatar
潘栩锋 committed
625 626 627 628

        /// <summary>
        /// 风环螺丝数
        /// </summary>
629
        public int NBolts => ChannelCnt * BPC;
潘栩锋's avatar
潘栩锋 committed
630 631 632 633

        /// <summary>
        /// 人字架复位时,对应的螺丝号,0° 对应的 螺丝号
        /// </summary>
634
        public int OrgBoltNo { get; set; } = 1;
潘栩锋's avatar
潘栩锋 committed
635 636 637 638 639 640 641 642 643 644 645 646

        private int smooth = 1;
        /// <summary>
        /// 平滑
        /// </summary>
        public int Smooth
        {
            get { return smooth; }
            set
            {
                if (value < 0)
                    value = 0;
647

潘栩锋's avatar
潘栩锋 committed
648 649 650 651 652 653 654
                if (smooth != value)
                {
                    smooth = value;
                }
            }
        }

655

潘栩锋's avatar
潘栩锋 committed
656 657 658
        /// <summary>
        /// 使用分区表
        /// </summary>
659 660
        public bool IsUsedMap { get; set; }
        bool IsNeedMap()
潘栩锋's avatar
潘栩锋 committed
661
        {
662 663 664 665
            if (IsUsedMap && Map.Count() > 0)
                return true;
            else
                return false;
潘栩锋's avatar
潘栩锋 committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679
        }

        private List<BoltMapCell> map = new List<BoltMapCell>();
        /// <summary>
        /// 分区表,必须保证不能为null
        /// </summary>
        public List<BoltMapCell> Map
        {
            get { return map; }
            set
            {
                //检测是否不一样
                if (value == null)
                    return;
680
                if (checkNoSame_map(map, value))
潘栩锋's avatar
潘栩锋 committed
681
                {
682 683 684 685 686 687 688 689 690
                    if (!checkParamErr_map(value))
                    {
                        IsUsedMap = false;
                        map = new List<BoltMapCell>();
                    }
                    else
                    {
                        map = value;
                    }
潘栩锋's avatar
潘栩锋 committed
691 692 693
                }
            }
        }
694
        bool checkNoSame_map(List<BoltMapCell> mapCells1, List<BoltMapCell> mapCells2)
潘栩锋's avatar
潘栩锋 committed
695
        {
696
            if (mapCells1.Count() != mapCells2.Count())
潘栩锋's avatar
潘栩锋 committed
697
                return true;
698
            for (int i = 0; i < mapCells1.Count(); i++)
潘栩锋's avatar
潘栩锋 committed
699
            {
700 701 702 703
                var mapCell1 = mapCells1[i];
                var mapCell2 = mapCells2[i];

                if (mapCell1.NewNo != mapCell2.NewNo)
潘栩锋's avatar
潘栩锋 committed
704
                    return true;
705
                if (mapCell1.OldNo != mapCell2.OldNo)
潘栩锋's avatar
潘栩锋 committed
706 707 708 709
                    return true;
            }
            return false;
        }
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 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
        /// <summary>
        /// 检测通过,返回true
        /// </summary>
        /// <returns></returns>
        bool checkParamErr_map(List<BoltMapCell> mapCells)
        {
            bool map_err = false;
            bool g_o = false;
            bool g_n = false;
            //Map 检测
            //OldNo,NewNo不能 <1 >NBolts
            //Map OldNo 必须从小到大环形排列
            //Map NewNo 必须从小到大环形排列
            for (int i = 0; i < mapCells.Count(); i++)
            {
                var mapCell = mapCells[i];
                if (mapCell.NewNo < 1 || mapCell.NewNo > NBolts)
                {
                    map_err = true;
                    break;
                }
                if (mapCell.OldNo < 1 || mapCell.OldNo > NBolts)
                {
                    map_err = true;
                    break;
                }

                int i_next = i + 1;
                if (i_next >= mapCells.Count())
                    i_next = 0;

                var mapCell_next = mapCells[i_next];
                if (mapCell.OldNo >= mapCell_next.OldNo)
                {
                    if (!g_o)
                        g_o = true;
                    else
                    {
                        map_err = true;
                        break;
                    }

                }

                if (mapCell.NewNo >= mapCell_next.NewNo)
                {
                    if (!g_n)
                        g_n = true;
                    else
                    {
                        map_err = true;
                        break;
                    }

                }
            }

            if (map_err)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
潘栩锋's avatar
潘栩锋 committed
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
        #endregion


        /// <summary>
        /// 应用
        /// </summary>
        public void Apply()
        {
            Save();
        }
        /// <summary>
        /// 扫描一次推送事件
        /// </summary>
        public event RenZiJiaDataEventHandler DataEvent;
        #endregion

        /// <summary>
        /// 缓冲区第一个记录序号
        /// </summary>
795 796 797
        public int FirstBM { get; set; } = -1;


潘栩锋's avatar
潘栩锋 committed
798 799 800
        /// <summary>
        /// 缓冲区最后一个记录序号
        /// </summary>
801
        public int LastBM { get; protected set; } = -1;
潘栩锋's avatar
潘栩锋 committed
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820

        private int sensorwidth = 250;
        /// <summary>
        /// 探头宽度,用于解方程,可以与实际不符合 单位脉冲
        /// </summary>
        public int SensorWidth
        {
            get { return sensorwidth; }
            set
            {
                if (value < 1)
                    value = 1;

                if (sensorwidth != value)
                {
                    sensorwidth = value;
                }
            }
        }
821

潘栩锋's avatar
潘栩锋 committed
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
        private int solvecnt = 4;
        /// <summary>
        /// 多少次扫描解一次方程
        /// </summary>
        public int SolveCnt
        {
            get { return solvecnt; }
            set
            {
                if (value < 3)
                    value = 3;

                if (solvecnt != value)
                {
                    solvecnt = value;
                }
            }
        }
        void checkParamErr()
        {


            if (ChannelCnt < 20 || ChannelCnt > 160)
            {
                ChannelCnt = 44;
            }

            if (BPC < 1 || BPC > 4)
            {
                BPC = 2;
            }
            
            if (OrgBoltNo < 1 || OrgBoltNo > NBolts)
            {
                OrgBoltNo = 1;
            }

            if (SolveCnt > 20)
            {
                SolveCnt = 5;
            }

864
            if (!checkParamErr_map(Map))
潘栩锋's avatar
潘栩锋 committed
865 866
            {
                IsUsedMap = false;
867
                Map = new List<BoltMapCell>();
潘栩锋's avatar
潘栩锋 committed
868 869
            }
        }
870 871 872



潘栩锋's avatar
潘栩锋 committed
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 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
        static GM_BlowingScan()
        {
            Misc.SaveToXmlHepler.Regist(typeof(BoltMapCell));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="flyad"></param>
        /// <param name="gsample"></param>
        public GM_BlowingScan(FlyAD7 flyad, GSample gsample)
            : base(flyad, gsample)
        {

            mPDetect = new BlowingDetect();
            mScanInfoList = new RList<ScanInfoCell>(30);
            mBufList = new RList<FlyData_BlowingScan>(100);
            mScanPosDetect = new ScanPosDetect();
            
            ChannelCnt = 44;//加热棒数量
            BPC = 2;//分区数量/加热棒数量

            OrgBoltNo = 10;//复位时,测厚仪对应的螺丝号

            SolveCnt = 5;
            mPDetect.Load();
            Load();

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

            if (SolveCnt < 3)
                SolveCnt = 3;

            PropertyChanged+=new PropertyChangedEventHandler(RenZiJiaScan_PropertyChanged);
            
            mPDetect.PropertyChanged += (s, e) =>
            {
                NotifyPropertyChanged(e.PropertyName);
            };

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

            mPDetect.Init(mFlyAD);

            mScanPosDetect.Init(mFlyAD);

            ScanCnt = -1;

            
        }


        bool Load()
        {
            return Misc.SaveToXmlHepler.Load("renzijiascan.xml", this);
            
        }
        void Save()
        {
            Misc.SaveToXmlHepler.Save("renzijiascan.xml", this);
        }


潘栩锋's avatar
潘栩锋 committed
938

潘栩锋's avatar
潘栩锋 committed
939 940 941 942 943 944 945
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bulkdata"></param>
        /// <param name="borderSearch"></param>
        /// <param name="dynArea"></param>
        /// <param name="func_ad2thick"></param>
潘栩锋's avatar
潘栩锋 committed
946 947 948
        /// <param name="profileParam"></param>
        /// <param name="historyDb"></param>
        /// <param name="bulkDb"></param>
潘栩锋's avatar
潘栩锋 committed
949 950 951 952
        public void Init(
            IBulkDataServiceAdd bulkdata,
            BorderSearch borderSearch,
            DynArea dynArea, 
潘栩锋's avatar
潘栩锋 committed
953 954 955
            AD2ThickHandler func_ad2thick,
            BlowingProfileParam profileParam,
            HistoryDb historyDb, BulkDb bulkDb
潘栩锋's avatar
潘栩锋 committed
956 957 958 959 960 961
            )
        {
            AD2Thick = func_ad2thick;
            mBulkData = bulkdata;
            mBorderSearch = borderSearch;
            mDynArea = dynArea;
潘栩锋's avatar
潘栩锋 committed
962 963 964
            mProfileParam = profileParam;
            mHistoryDb = historyDb;
            mBulkDb = bulkDb;
潘栩锋's avatar
潘栩锋 committed
965 966 967 968

            //接收PollScan的推送
            DataPosEvent += new EndEventHandler(pollScan_DataPosEvent);
            EndEvent += new EndEventHandler(pollScan_EndEvent);
潘栩锋's avatar
潘栩锋 committed
969 970 971

            //记录调试数据
            MarkDebugInit();
潘栩锋's avatar
潘栩锋 committed
972
        }
潘栩锋's avatar
潘栩锋 committed
973 974 975 976 977 978 979
        #region 记录原始数据
        DateTime markdebug_dt = DateTime.MinValue;
        int markdebug_lastBm = -1;
        void MarkDebugInit()
        {
            if (markdebug_dt == DateTime.MinValue)
                markdebug_dt = DateTime.Now;
潘栩锋's avatar
潘栩锋 committed
980

潘栩锋's avatar
潘栩锋 committed
981 982 983 984 985
            //启动1分钟定时器
            FObjBase.PollModule.Current.Poll_Config(
                FObjBase.PollModule.POLL_CONFIG.ADD,
                () =>
                {
986
                    mPDetect.GetSignList(markdebug_dt, (asyncState, retData) =>
潘栩锋's avatar
潘栩锋 committed
987
                    {
988 989
                        var reponse = retData as GetSignListReponse;
                        var cells = reponse.datas;
潘栩锋's avatar
潘栩锋 committed
990 991 992 993 994 995 996 997 998 999 1000 1001
                        if (cells.Count > 0)
                        {
                            mHistoryDb.AddSignData(
                                new Lc_Sign()
                                {
                                    Time = cells.First().Time,
                                    Signs = cells.ToArray()
                                });
                        }
                    }, null);
                    mPDetect.GetRollList(markdebug_dt, (asyncState, retData) =>
                    {
1002 1003
                        var reponse = retData as GetRollListReponse;
                        var cells = reponse.datas;
潘栩锋's avatar
潘栩锋 committed
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
                        if (cells.Count > 0)
                        {
                            mHistoryDb.AddRollData(
                                new Lc_Roll()
                                {
                                    Time = cells.First().dt,
                                    Signs = cells.ToArray()
                                });
                        }
                    }, null);
                    this.GetBufList((asyncState, retData) =>
                    {
1016 1017 1018
                        var reponse = retData as GetBufListReponse;
                        var cells = reponse.datas;
                        if (cells.Count() == 0)
潘栩锋's avatar
潘栩锋 committed
1019 1020
                        {
                            //没有数据
1021
                            markdebug_lastBm = reponse.lastbm;
潘栩锋's avatar
潘栩锋 committed
1022 1023
                            return;
                        }
1024 1025 1026 1027
                        int new_cnt = reponse.lastbm - markdebug_lastBm;
                        if (new_cnt > cells.Count())
                            new_cnt = cells.Count();
                        markdebug_lastBm = reponse.lastbm;
潘栩锋's avatar
潘栩锋 committed
1028 1029 1030 1031 1032 1033

                        if (new_cnt <= 0)
                            return;
                        List<Model.Lc_OrgScanData> lc_OrgScanDatas = new List<Model.Lc_OrgScanData>();
                        for (int i = 0; i < new_cnt; i++)
                        {
1034 1035
                            int index = reponse.datas.Count() - new_cnt + i;
                            var data = reponse.datas[index];
潘栩锋's avatar
潘栩锋 committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

                            var times = data.ThicksDt.Where(dt => dt != DateTime.MinValue);
                            if (times.Count() == 0)
                                continue;
                            Model.Lc_OrgScanData lcOrgScanData = new Model.Lc_OrgScanData();
                            lcOrgScanData.Time = times.Min();
                            lcOrgScanData.FilmBegin = data.FilmRange.Begin;
                            lcOrgScanData.FilmEnd = data.FilmRange.End;
                            lcOrgScanData.PosOfGrid = data.PosOfGrid;
                            lcOrgScanData.K = mProfileParam.K;
                            lcOrgScanData.Thicks = ToRealThicks(data.Thicks);
                            lcOrgScanData.ThicksDt = data.ThicksDt;
                            lc_OrgScanDatas.Add(lcOrgScanData);
                        }
                        mHistoryDb.AddOrgScanDatas(lc_OrgScanDatas);
                        
                    }, null);
                    markdebug_dt = DateTime.Now;
                },
                TimeSpan.FromMinutes(1));
        }

        #endregion
        double[] ToRealThicks(IEnumerable<int> frame)
        {
            return frame.Select(t =>
            {
                if (Misc.MyBase.ISVALIDATA(t))
                    return t / 100.0;
                else
                    return double.NaN;
            }).ToArray();
        }
潘栩锋's avatar
潘栩锋 committed
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
        void RenZiJiaScan_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "NBolts") 
            {
                //所有数据删除
                mScanInfoList.Clear();
                //TODO!!!!!
                //停下来
            }
        }
        void mPDetect_FilmInfoChangedEvent(object sender, BlowingDetect.FilmInfoChangedEventArgs e)
        {
            // 找到 e.Time 对应的 scaninfo

            for (int i = 0; i < mScanInfoList.Count; i++) 
            {
                ScanInfoCell scaninfo = mScanInfoList[i];
                if (scaninfo.IndexOfToGetTime > 0) 
                {
                    DateTime dt_start = scaninfo.frame[0].dt;
                    DateTime dt_end = scaninfo.frame[scaninfo.IndexOfToGetTime-1].dt;
                    if (e.Time < dt_end)//找到了 
                    {
                        for (int j = i; j < mScanInfoList.Count; j++) 
                        {
                            scaninfo = mScanInfoList[j];
                            scaninfo.IndexOfToGetAngle = 0;
                        }
                        return;
                    }
                }
            }
        }
        void mPDetect_ClearEvent(object sender)
        {
            //已经很久没数据,清空全部!!!!!!!

            //scanposdetect
            mScanPosDetect.ScanNo = ScanNo;

            if (mScanInfoList.Count > 0)
            {
                mScanInfoList.Clear();

                //scaninfo
                ScanInfoCell scaninfo = new ScanInfoCell(ScanNo, NBolts);
                //scaninfo.bookmark = bookmark;
                mScanInfoList.Add(scaninfo);
            }
        }


        #region 1.grid 数据  ScanPosCell.(pos,thick)  放入 mScanInfoList.Last()
        /// <summary>
        /// 动态获取!!!! 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        void pollScan_DataPosEvent(object obj, FlyADBase.MiniGridEventArgs e)
        {
            DIRECTION direction = e.direction;
            int posOfGrid = e.posOfGrid;
            int start_idx = e.grid_start;
            int start_pos = e.grid_start * e.posOfGrid;
            int[] dat = e.buf;
1134 1135 1136 1137 1138 1139 1140
            //-------------------------------------
            //DIR
            mDynArea.Direction = direction;
            mDynArea.AD = dat[0];
            mDynArea.Thick = AD2Thick(dat[0]);


潘栩锋's avatar
潘栩锋 committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 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 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
            if (mScanInfoList.Count == 0)
                return;

            ScanInfoCell scaninfocell = mScanInfoList.Last();
            if (scaninfocell.width <= 0)
            {
                //刚开始,连膜边都还没找到
                return;
            }

            //找边界前,要对数据进行校正

            //尽可能不要两个边的数据,N2是边界向内减, 只是不要更多的数据而已
            int bp = scaninfocell.beginPos + mBorderSearch.N2;
            int ep = scaninfocell.endPos - mBorderSearch.N2;
            //对应的grid号
            int bp_idx = bp / posOfGrid;
            int ep_idx = ep / posOfGrid;

            if(bp_idx < start_idx)
                bp_idx = start_idx;

            if(ep_idx > (start_idx+dat.Length-1))
                ep_idx = start_idx+dat.Length-1;

            for (int i = bp_idx; i <= ep_idx; i++)
            {
                int idx = i - start_idx;

                int pos = i * posOfGrid + posOfGrid / 2;//一个grid的中间脉冲,也就是大概而已
                int thick = Misc.MyBase.NULL_VALUE;
                if (Misc.MyBase.ISVALIDATA(dat[idx]))
                    thick = AD2Thick(dat[idx])*2;
               
                pos -= scaninfocell.beginPos;//膜上面的位置
                scaninfocell.frame.Add(new ScanPosCell() { pos = pos, thick = thick });
            }
            OnPoll_GetTime2();
        }
        void pollScan_EndEvent(object obj, FlyADBase.MiniGridEventArgs e)
        {
            DIRECTION direction = e.direction;
            int posOfGrid = e.posOfGrid;
            int start_idx = e.grid_start;
            int start_pos = e.grid_start * e.posOfGrid;
            int[] dat = e.buf;

            //int scaninfo_idx = mScanInfoList.FindIndex(sil => sil.IndexOfToGetTime < sil.frame.Count);
            //if ((scaninfo_idx>0) && (mScanInfoList.Count - scaninfo_idx > 1) )
            //{
            //    //有问题
            //}


            ScanNo++;
            mScanPosDetect.ScanNo = ScanNo;

            if(gsample.Samples[0].AD <= 0)
            {
                //无温修AD
                if (mScanInfoList.Count == 0)
                    return;

                ScanInfoCell scaninfo = mScanInfoList.Last();
                if (scaninfo.bookmark != -1)
                    mBulkData.ChangeEmply(scaninfo.bookmark);
                scaninfo.Reset(ScanNo);
                return;
            }

            //找边界
            bool ret = mBorderSearch.FindFilm(direction, posOfGrid, start_idx, dat);
            if (!ret)//不能找到边界!!!没膜!!!!
            {
                mBorderSearch.Init();

            }
            //设置扫描范围
            //POLLSCAN----------------------------------------	
            Range scanrange = mBorderSearch.GetScanRange();

            ScanStart = scanrange.Begin;// beginPos;
            ScanEnd = scanrange.End;// endPos;

            if (!ret)
            {
                if (mScanInfoList.Count == 0)
                    return;

                ScanInfoCell scaninfo = mScanInfoList.Last();
                if (scaninfo.bookmark != -1)
                    mBulkData.ChangeEmply(scaninfo.bookmark);
                scaninfo.Reset(ScanNo);
                return;
            }

            //必须设置好 mBorderSearch.SensorWidth,它就是探头直径
            Range border = mBorderSearch.GetBorder(direction);

            if (mScanInfoList.Count == 0)//安全检查,不可能发生的!!!!!
                return;

            ScanInfoCell scaninfocell = mScanInfoList.Last();
            //膜宽,和位置一起计算旋转架上的角度
            scaninfocell.Reset_IsFull(border.Begin, border.End);

            //尽可能不要两个边的数据,N2是边界向内减, 只是不要更多的数据而已
            Range boltrange = mBorderSearch.GetBoltRange(direction);

            //对应的grid号
            int bp_idx = boltrange.Begin / posOfGrid;
            int ep_idx = boltrange.End / posOfGrid;

            if (bp_idx < start_idx)
                bp_idx = start_idx;

            if (ep_idx > (start_idx + dat.Length - 1))
                ep_idx = start_idx + dat.Length - 1;

            for (int i = bp_idx; i <= ep_idx; i++)
            {
                int idx = i - start_idx;
                int pos = i * posOfGrid + posOfGrid / 2;//一个grid的中间脉冲,也就是大概而已
                int thick = Misc.MyBase.NULL_VALUE;
                if (Misc.MyBase.ISVALIDATA(dat[idx]))
                    thick = AD2Thick(dat[idx]) * 2;

                pos -= border.Begin;//膜上面的位置
                scaninfocell.frame.Add(new ScanPosCell() { pos = pos, thick = thick });
            }



            //或取每个数据对应的时间点
            OnPoll_GetTime2();

            //把扫描数据放多一份到 mBufList
            PushToBufList(e, border, scaninfocell.ScanNo);

潘栩锋's avatar
潘栩锋 committed
1280 1281 1282 1283 1284 1285 1286 1287
            //插入纵向数据
            #region sqlite3 历史数据保存
            {
                List<double> boltValues = new List<double>();
                double boltwidth = scaninfocell.width * 1.0 / (NBolts / 2);
                int boltIdx = 0;
                double boltSum = 0;
                int boltCnt = 0;
潘栩锋's avatar
潘栩锋 committed
1288

潘栩锋's avatar
潘栩锋 committed
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
                for (int i = 0; i < scaninfocell.frame.Count(); i++)
                {
                    int idx = (int)(scaninfocell.frame[i].pos / boltwidth);
                    if (boltIdx != idx)
                    {
                        if (boltCnt > 0)
                        {
                            boltValues.Add(boltSum / boltCnt);
                            boltSum = 0;
                            boltCnt = 0;
                        }
                    }
                    boltIdx = idx;
                    if (Misc.MyBase.ISVALIDATA(scaninfocell.frame[i].thick))
                    {
                        boltSum += scaninfocell.frame[i].thick / 2 / 100.0;
                        boltCnt++;
                    }

                }
                if (boltCnt > 0)
                {
                    boltValues.Add(boltSum / boltCnt);
                    boltSum = 0;
                    boltCnt = 0;
                }
                
                mHistoryDb.AddTrendData(new Lc_TrendData()
                {
                    Time = DateTime.Now,
                    K = mProfileParam.K,
                    Thicks = boltValues.ToArray()
                });
            }
潘栩锋's avatar
潘栩锋 committed
1323

潘栩锋's avatar
潘栩锋 committed
1324 1325 1326 1327 1328 1329 1330 1331 1332
            #endregion


            //创建一个新的 scaninfocell 
            //为下一趟即时扫描作准备
            scaninfocell = new ScanInfoCell(ScanNo,NBolts);

            direction = (direction == DIRECTION.FORWARD) ? DIRECTION.BACKWARD : DIRECTION.FORWARD;
            border = mBorderSearch.GetBorder(direction);
潘栩锋's avatar
潘栩锋 committed
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
            scaninfocell.beginPos = border.Begin;
            scaninfocell.endPos = border.End;
            scaninfocell.width = border.Width;//膜宽
            scaninfocell.IsFull = false;
            mScanInfoList.RAdd(scaninfocell);
        }
        void PushToBufList(MiniGridEventArgs e, Range border, int scanNo)
        {
            DIRECTION direction = e.direction;
            int posOfGrid = e.posOfGrid;
            int start_idx = e.grid_start;
            int start_pos = e.grid_start * e.posOfGrid;
            int[] dat = e.buf;
            

            FlyData_BlowingScan f = new FlyData_BlowingScan();
            f.FilmRange = border;
            f.PosOfGrid = posOfGrid;
            f.Thicks = new int[mFlyAD.PosLen/ posOfGrid];
            f.ThicksDt = new DateTime[f.Thicks.Count()];
            //对应的grid号
            for (int i = 0; i < f.Thicks.Count(); i++)
            {
                int idx = i - start_idx;
                int pos = i * posOfGrid + posOfGrid / 2;//一个grid的中间脉冲,也就是大概而已

                int thick = Misc.MyBase.NULL_VALUE;
                if (idx >= 0 && idx < dat.Count())
                {
                    if (Misc.MyBase.ISVALIDATA(dat[idx]))
                        thick = AD2Thick(dat[idx]) * 2;
                }
                f.Thicks[i] = thick;

                DateTime dt = DateTime.MinValue;
                if (Misc.MyBase.ISVALIDATA(thick))
                {
                    int ret = mScanPosDetect.GetTime(pos, scanNo, out dt);
                }
                f.ThicksDt[i] = dt;
            }

            f.RotateCnt = mPDetect.RotationCnt;
            if (mPDetect.mLimitList.Count() > 0)
            {
                LimitCell lc = mPDetect.mLimitList.Last();
                f.SignNo = lc.no;
                f.Time = lc.dt_begin;
                f.LeaveTime = lc.dt_end;
            }
            else
            {
                f.SignNo = -1;
                f.Time = DateTime.MinValue;
                f.LeaveTime = DateTime.MinValue;
            }

            f.LimitSignTime = mPDetect.LimitSignTime;
            f.RotatePeriod = mPDetect.RenZiJiaPeriod;

            f.Velocity = mPDetect.FilmVelocity;
            

            mBufList.RAdd(f);
            FirstBM = mBufList.Index2No(0);
            LastBM = mBufList.GetLastNo();
潘栩锋's avatar
潘栩锋 committed
1399 1400 1401



潘栩锋's avatar
潘栩锋 committed
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 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 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 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 1607 1608 1609 1610 1611 1612 1613 1614 1615 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 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
        }
        #endregion
        #region 2.填入 ScanPosCell.(dt)  
        /// <summary>
        /// 找到一幅数据,每个grid 对应的时间点
        /// </summary>
        void OnPoll_GetTime2()
        {
            ScanInfoCell scaninfo = mScanInfoList.Last();

            for (; scaninfo.IndexOfToGetTime < scaninfo.frame.Count; scaninfo.IndexOfToGetTime++)
            {
                int i = scaninfo.IndexOfToGetTime;
                ScanPosCell scanpos = scaninfo.frame[i];
                //还原为机架位置
                int pos = scaninfo.beginPos + scanpos.pos;

                DateTime dt;
                int ret = mScanPosDetect.GetTime(pos, scaninfo.ScanNo, out dt);
                if (ret == -2)
                {
                    //发生在过去,不正常

                }
                else if (ret == 1)//这个是未来的,等下一次Poll
                {
                    return;
                }
                else if (dt == DateTime.MinValue)
                {
                    //出错, 把scaninfo 删除掉
                    //mScanInfoList.RemoveAt(scaninfo_idx);
                    //scaninfo_idx--;
                    break;
                }
                //成功获取

                scanpos.dt = dt;
                if (i == 0)
                {
                    scaninfo.StartTime = dt;
                    scaninfo.thicks_dt = dt;
                }
                scaninfo.EndTime = dt;
            }
        }
        #endregion
        #region 3.填入 ScanPosCell.(angle1,angle2), 且转为 Equation 
        void OnPoll_GetAngle() 
        {
            int scaninfo_idx = mScanInfoList.FindIndex(
                sil => 
                    (sil.IndexOfToGetAngle < sil.IndexOfToGetTime));

            if (scaninfo_idx == -1)
            {
                return;//全部都转换完成!!
            }

            while (true)
            {
                ScanInfoCell scaninfo = mScanInfoList[scaninfo_idx];

                ScanInfoCell.GetAngleResult result = scaninfo.GetAngle(mPDetect, OrgBoltNo, SensorWidth);
                if (result == ScanInfoCell.GetAngleResult.NothingToDo)
                {
                    //getangle 失败,发生在未来,等下一次Poll
                    break;
                }

                //解方程,画数据!!
                ToThicks(scaninfo_idx);
                    
                
                //全部GetAngle 成功,下一个scaninfo
                scaninfo_idx++;
                if (scaninfo_idx >= mScanInfoList.Count)
                    break;//已经没有了
            }
        }
        

        class CalSolveCnt 
        {
            List<int> list = new List<int>();
            const int listcap = 5;
            void Add(int cnt) 
            {
                list.Add(cnt);
                if (list.Count > listcap)
                    list.RemoveAt(0);
            }
            public int Max() 
            {
                if (list.Count > 0)
                    return list.Max();
                else
                    return 4;
            }
            Misc.DIRECTION direction = DIRECTION.FIX;
            int cnt = 0;
            public bool Check(Misc.DIRECTION direction) 
            {
                if (this.direction != direction)
                {
                    if((this.direction!=Misc.DIRECTION.FIX) && (cnt>3))
                        Add(cnt);
                    cnt = 0;
                }
                cnt++;
                this.direction = direction;
                return OK();
            }
            public bool OK() 
            {
                if (list.Count >= listcap) 
                {
                    return true;
                }
                return false;
            }
        }
        CalSolveCnt mCalSolveCnt = new CalSolveCnt();

        bool ToThicks_SolveEquation(int scaninfo_idx) 
        {
            ScanInfoCell scaninfo = mScanInfoList[scaninfo_idx];
            if (scaninfo.Direction == DIRECTION.FIX)//当前是Fix 不解方程
                return false;

            //解方程数量检测,免得一直数量不足,解不了方程
            //if (mCalSolveCnt.Check(scaninfo.Direction)) 
            //{
            //    if (SolveCnt > (mCalSolveCnt.Max()-1)) 
            //    {
            //        SolveCnt = (mCalSolveCnt.Max() - 1);
            //    }
            //}
            
            if (scaninfo_idx < (SolveCnt - 1))
                return false;

            //全部方向都必须一致,当然也不能是Fix
            //for (int i = 0; i < (SolveCnt - 1); i++)
            //{
            //    if (scaninfo.Direction != mScanInfoList[scaninfo_idx - 1 - i].Direction)
            //        return false;
            //}

            int avg = scaninfo.GetAvg();
            if (!Misc.MyBase.ISVALIDATA(avg))//平均值不合法!!!!
                return false;

            if (avg < 500)//值太小!!!
                return false;

            List<EquationCell> equationlist = new List<EquationCell>();
            ////全部平均值相差不能超过 5%
            //for (int i = 0; i < SolveCnt; i++)
            //{
            //    int a = mScanInfoList[scaninfo_idx - 1 - i].GetAvg();
            //    if (Math.Abs((a - avg) / avg) > 0.05)
            //        return false;
            //    equationlist.AddRange(mScanInfoList[scaninfo_idx - i].equationList);
            //}


            int cnt = 0;
            for (int i = scaninfo_idx; i >=0; i--) 
            {
                if (mScanInfoList[i].Direction == DIRECTION.FIX)
                    continue;
                    cnt++;
                double a = mScanInfoList[i].GetAvg();
                double p = (a - avg) / avg;
                if (Math.Abs(p) > 0.05)
                    return false;
                equationlist.AddRange(mScanInfoList[i].equationList);
                
                if (cnt >= SolveCnt)
                    break;
            }
            if (cnt < SolveCnt)//方程不够!!!!
                return false;
            
            if (equationlist.Count < 100)//方程数太少
                return false;

            //2105 条方程,耗时 0.3s
            SolveEquation(mScanInfoList[scaninfo_idx].thicks, equationlist);

            //不能有任何一个NULL_VALUE
            for (int i = 0; i < scaninfo.thicks.Count(); i++)
            {
                if (scaninfo.thicks[i] > 1000000)
                    return false;
                if (scaninfo.thicks[i] < -10000)
                    return false;
            }

            //检测
            {
                int sigma = Misc.MyMath.Sigma(scaninfo.thicks);
                int avg2 = Misc.MyMath.Avg(scaninfo.thicks);


                double p = 1.0 * (avg2 - avg) / avg;
                if (Math.Abs(p) > 0.05)//变化太大,不可能!!!
                    return false;
                p = 1.0 * sigma / avg2;
                if (Math.Abs(p) > 0.5) //波动太大,异常
                    return false;
            }
            //最后处理,滤波,平滑
            if (Smooth > 0)
            {
                int[] data = new int[scaninfo.thicks.Count()];
                for (int i = 0; i < scaninfo.thicks.Count(); i++)
                {
                    int sum = 0;

                    for (int j = 0; j < (Smooth * 2 + 1); j++)
                    {
                        int index = i + j - Smooth;
                        if (index < 0)
                            index += scaninfo.thicks.Count();
                        else if (index >= scaninfo.thicks.Count())
                            index -= scaninfo.thicks.Count();
                        sum += scaninfo.thicks[index];
                    }
                    data[i] = (int)(sum / (Smooth * 2 + 1));
                }
                Array.Copy(data, scaninfo.thicks, data.Count());
            }
            scaninfo.thicks_dt = mScanInfoList[scaninfo_idx - (SolveCnt-1)].StartTime;

            return true;
        }


        enum BindingMotion
        {
            Add,
            Remove
        }
        void BookmarkRestartToBinding(int scaninfo_idx, BindingMotion motion) 
        {
            if (motion == BindingMotion.Add)
            {
                if (mScanInfoList[scaninfo_idx].bookmark == -1)
                {
                    int bookmark = mScanInfoList[scaninfo_idx].bookmark;
                    mBulkData.ChangeEmply(bookmark);

                    //删除与 bulkdata 关系
                    mScanInfoList[scaninfo_idx].bookmark = -1;
                    for (int i = mScanInfoList.Count()-1;i>(scaninfo_idx + 1); i--)
                    {
                        if (mScanInfoList[i].bookmark != -1)
                        {
                            int tmp = bookmark;
                            bookmark = mScanInfoList[i].bookmark;
                            mScanInfoList[i].bookmark = tmp;
                        }
                    }
                }
            }
            else 
            {
                if (mScanInfoList[scaninfo_idx].bookmark != -1)
                {
                    int bookmark = mScanInfoList[scaninfo_idx].bookmark;
                    mBulkData.ChangeEmply(bookmark);

                    //删除与 bulkdata 关系
                    mScanInfoList[scaninfo_idx].bookmark = -1;
                    for (int i = scaninfo_idx + 1; i < mScanInfoList.Count(); i++)
                    {
                        if (mScanInfoList[i].bookmark != -1)
                        {
                            int tmp = bookmark;
                            bookmark = mScanInfoList[i].bookmark;
                            mScanInfoList[i].bookmark = tmp;
                        }
                    }
                }
            }
        }

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

        bool ToThicks(int scaninfo_idx) 
        {
            ScanInfoCell scaninfo = mScanInfoList[scaninfo_idx];

            if (scaninfo.IsFull)//所有grid数据都获取完,解方程
            {
                if (scaninfo.lockbm)
                { 
                    //不需要再解方程,算thicks 的值!!!
                    return false;
                }



                if (ToThicks_SolveEquation(scaninfo_idx))
                {
                    //解方程成功
                    if (scaninfo.bookmark == -1)
                        scaninfo.bookmark = GetFreeBm();

潘栩锋's avatar
潘栩锋 committed
1719
                    //重新分区
1720
                    if (IsNeedMap())
潘栩锋's avatar
潘栩锋 committed
1721 1722 1723 1724
                    {
                        Dictionary<int, int> map = new Dictionary<int, int>();
                        for (int i = 0; i < Map.Count(); i++)
                            map.Add(Map[i].OldNo - 1, Map[i].NewNo - 1);
1725
                        scaninfo.thicks_map = Misc.MyMath.Map(scaninfo.thicks, map);
潘栩锋's avatar
潘栩锋 committed
1726
                    }
潘栩锋's avatar
潘栩锋 committed
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
                    
                    //更新数据库内容
                    {
                        int[] frame;
                        List<BoltMapCell> map;
                        if (IsNeedMap())
                        {
                            frame = scaninfo.thicks_map;
                            map = new List<BoltMapCell>();
                            foreach (var m in Map)
                                map.Add(m.Clone());
                        }
                        else
                        {
                            frame = scaninfo.thicks;
                            map = null;
                        }
                        var realthicks = ToRealThicks(frame);
                        var renZiJiaDataEventArgs = new RenZiJiaDataEventArgs()
                        {
                            Time = scaninfo.thicks_dt,
                            EndTime = scaninfo.EndTime,
                            IsBackw = scaninfo.Direction == DIRECTION.BACKWARD,
                            RPeriod = mPDetect.RenZiJiaPeriod,
                            RCnt = mPDetect.RotationCnt,
                            OrgBoltNo = OrgBoltNo,
                            RAngle = mPDetect.RAngle,
                            FilmLength = mPDetect.FilmLength,
                            FilmVelocity = mPDetect.FilmVelocity,
                            K = mProfileParam.K,
                            Thicks = realthicks,
                            Boltmap = map
                        };

                        DataEvent?.Invoke(this, renZiJiaDataEventArgs);

                        //TODO 需要检查是否重复写入了!!!!!
                        #region sqlite3 历史数据保存
1765
                        var lc_scandata = Lc_AutoMapperProfile.Mapper.Map<Lc_ScanData>(renZiJiaDataEventArgs);
潘栩锋's avatar
潘栩锋 committed
1766 1767 1768 1769 1770 1771
                        mHistoryDb.AddScanData(lc_scandata);
                        #endregion

                    }


1772
                    int[] thicks = IsNeedMap()? scaninfo.thicks_map:scaninfo.thicks;
潘栩锋's avatar
潘栩锋 committed
1773 1774 1775 1776 1777 1778 1779 1780 1781

                    if (scaninfo.bookmark == -1)
                    {
                        scaninfo.bookmark = mBulkData.Add(thicks, Direction2FR_DATA_TYPE(scaninfo.Direction), scaninfo.thicks_dt);
                    }
                    else
                    {
                        mBulkData.Change(scaninfo.bookmark, 0, thicks, Direction2FR_DATA_TYPE(scaninfo.Direction), scaninfo.thicks_dt);
                    }
潘栩锋's avatar
潘栩锋 committed
1782
                    
潘栩锋's avatar
潘栩锋 committed
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
                    mBulkData.Save();
                }
                else 
                {
                    //失败,删除!!!
                    for (int i = 0; i < scaninfo.thicks.Count(); i++) 
                        scaninfo.thicks[i] = Misc.MyBase.NULL_VALUE;
                    
                    if (scaninfo.bookmark != -1)
                    {
                        mBulkData.ChangeEmply(scaninfo.bookmark);
                        SetFreeBm(scaninfo.bookmark);
                        scaninfo.bookmark = -1;
                    }
                }
                scaninfo.lockbm = true;
                return true;
            }
潘栩锋's avatar
潘栩锋 committed
1801
            else
潘栩锋's avatar
潘栩锋 committed
1802
            {
潘栩锋's avatar
潘栩锋 committed
1803 1804 1805
                //还没获取完所有grid, 简易解方程,只认为 mainBolt 是未知的,其它都是已知的,用上一幅数据的值
                //这样的数据不需要放在数据库中!!!!!!

潘栩锋's avatar
潘栩锋 committed
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
                int[] org_thick = null;
                if (scaninfo_idx != 0)
                    org_thick = mScanInfoList[scaninfo_idx - 1].thicks;

                ScanInfoCell.SolveQuickResult result = scaninfo.SolveQuick(org_thick,SolveCnt-1);
                if (result.len > 0) 
                {
                    //如果scaninfo.bookmark ==-1 ,也就是没有对应的 bulkdata 数据,创建一个
                    //先从freebm 获取,不行再创建新的
                    if (scaninfo.bookmark == -1)
                        scaninfo.bookmark = GetFreeBm();

                    if (scaninfo.bookmark == -1)
                        scaninfo.bookmark = mBulkData.AddEmply();

                    int[] thicks = scaninfo.thicks.Where((t, i) => (i >= result.start_index && i < (result.start_index + result.len))).ToArray();

                    mBulkData.Change(
                        scaninfo.bookmark,
                        result.start_index, thicks, Direction2FR_DATA_TYPE(scaninfo.Direction), scaninfo.thicks_dt);
潘栩锋's avatar
潘栩锋 committed
1826 1827 1828

                    
                    mBulkDb.SetTempFrame(0, ToRealThicks(thicks));
潘栩锋's avatar
潘栩锋 committed
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
                }
            }
            return false;
        }
        /// <summary>
        /// 解方程
        /// </summary>
        /// <param name="equationList">N条方程</param>
        /// <param name="frame_out">输出的结果</param>
        void SolveEquation(int[] frame_out, List<EquationCell> equationList)
        {
            
            int nrow = equationList.Count();
            //A~AX = A~B
            //转换为 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 (EquationCell ec in equationList)
            {

                double[] datas = ec.GetPower(NBolts);

                matrixA.SetRow(rowindex, datas);

                matrixAT.SetColumn(rowindex, datas);
                vectorB[rowindex] = ec.thick;
                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++)
            {
                frame_out[i] = (int)X[i];
            }
        }
        #endregion
        
潘栩锋's avatar
潘栩锋 committed
1876 1877 1878 1879
        /// <summary>
        /// 开始扫描
        /// </summary>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
        public override bool Start()
        {
            bool b = base.Start();
            if (!b)
                return false;

            //设置扫描范围
            int beginPos, endPos;
            
            mBorderSearch.Init();
            mBorderSearch.GetScanRange(out beginPos, out endPos);



            ScanNo++;
            //scanposdetect
            mScanPosDetect.ScanNo = ScanNo;

            //scaninfo
            ScanInfoCell scaninfo = new ScanInfoCell(ScanNo, NBolts);
            mScanInfoList.Add(scaninfo);


            mBulkData.InitPush();
潘栩锋's avatar
潘栩锋 committed
1904 1905 1906 1907 1908 1909 1910 1911 1912
            //清空临时数据
            mBulkDb.SetTempFrame(0, null);

            #region sqlite3 历史数据保存
            CheckProfile();
            mProfileParam.PropertyChanged += MProfileParam_PropertyChanged;
            #endregion


潘栩锋's avatar
潘栩锋 committed
1913 1914
            return base.Start2(beginPos, endPos);
        }
潘栩锋's avatar
潘栩锋 committed
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
        #region sqlite3 历史数据保存
        string[] profile_propertynames_add = new string[]{
                "PName",
                "OrderNo",
                "Number"
            };
        string[] profile_propertynames_update = new string[]{
                "Target",
                "TolerancePercent"
            };
        /// <summary>
        /// 检查产品信息更新情况,是否需要新加单,或者更新单信息
        /// </summary>
        void CheckProfile()
        {
            if (mHistoryDb.localDb.IsProfileFinished)
            {
                //上一单结束了,添加新单
                AddProfile();
            }
            else
            {
                if (mHistoryDb.localDb.CurrProfile != null)
                {
                    Db_Profile db_Profile = mHistoryDb.localDb.CurrProfile;

                    if ((db_Profile.PName != mProfileParam.PName) ||
                    (db_Profile.OrderNo != mProfileParam.OrderNo) ||
                    (db_Profile.Number != mProfileParam.Number))
                    {
                        //产品名称/订单号/卷号改变了,需要新加单
                        AddProfile();
                    }
                    else
                    {
                        //继续上一次生产!!!!更新订单信息
                        UpdateProfile();
                    }
                }
                else
                {
                    //异常,居然没有订单信息
                    AddProfile();
                }

            }
        }
        void AddProfile()
        {
            mHistoryDb.AddProfile(new Db_Profile()
            {
                PName = mProfileParam.PName,
                OrderNo = mProfileParam.OrderNo,
                Number = mProfileParam.Number,
                Target = mProfileParam.Target,
                TolerancePercent = mProfileParam.TolerancePercent,
                StartTime = DateTime.Now,
                EndTime = DateTime.Now
            });
        }
        void UpdateProfile()
        {
            mHistoryDb.UpdateProfile(new Db_Profile()
            {
                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(
                    () =>
                    {
                        if (!mHistoryDb.localDb.IsProfileFinished)
                        {
                            mHistoryDb.FinishProfile();
                        }
                        AddProfile();//新
                    }, this, MARKNO_PROFILE_ADD);
            }
            else if (profile_propertynames_update.Contains(e.PropertyName))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    () =>
                    {
                        UpdateProfile();//修改
                    }, this, MARKNO_PROFILE_CHANGED);
            }
        }
        #endregion

        /// <summary>
        /// 停止扫描
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
2014 2015
        public override void Stop()
        {
潘栩锋's avatar
潘栩锋 committed
2016 2017 2018 2019
            #region sqlite3 历史数据保存
            mProfileParam.PropertyChanged -= MProfileParam_PropertyChanged;
            #endregion

潘栩锋's avatar
潘栩锋 committed
2020 2021 2022 2023 2024
            base.Stop();
            //已经停止
            //删除全部
            mScanInfoList.Clear();
        }
潘栩锋's avatar
潘栩锋 committed
2025 2026 2027 2028

        /// <summary>
        /// 周期动作处理
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
        protected override void OnPoll()
        {
            base.OnPoll();
            OnPoll_GetAngle();
        }

        /// <summary>
        /// 获取 缓冲区列表
        /// </summary>
        /// <param name="AsyncDelegate">retData 为 </param>
        /// <param name="AsyncState"></param>
        public void GetBufList(AsyncCBHandler AsyncDelegate, object AsyncState)
        {
            GetBufListReponse p = new GetBufListReponse();

            p.lastbm = LastBM;
            p.firstbm = FirstBM;
            p.datas = mBufList;

            AsyncDelegate(AsyncState, p);
        }
    }
    /// <summary>
    /// 扫描位置检测
    /// 里面的列表会缓存一些旧数据,如1次扫描前的位置信息。
    /// 什么时候删除,由ScanPosDetect 自行决定,使用者不用关心
    /// </summary>
    public class ScanPosDetect 
    {
        /// <summary>
        /// IOStatus 推送出来扫描位置与时间
        /// </summary>
        class PosCell
        {
            public DateTime dt;
            public int position;
            public override string ToString()
            {
                return dt.ToString() + " pos=" + position.ToString();
            }
        }
        class PosCellList
        {
            public int scanNo;
            private List<PosCell> mPosList = new List<PosCell>();
            public void Add(int position, DateTime dt) 
            {
                if (mPosList.Count() > 0) 
                {
                    if (mPosList.Last().position == position) 
                    {
                        mPosList.Last().dt = dt;
                        return;
                    }
                }
                mPosList.Add(new PosCell() { position = position, dt = dt });
            }
            private DateTime GetTime(int position, int idx)
            {
                
                double d_pos = mPosList[idx].position - mPosList[idx + 1].position;
                
                TimeSpan ts = mPosList[idx].dt - mPosList[idx + 1].dt;
                long ticks = (long)((position - mPosList[idx + 1].position) / d_pos * ts.Ticks);
                return mPosList[idx + 1].dt + new TimeSpan(ticks);
            }
            /// <summary>
            /// 获取位置对应的数据点.
            /// 返回
            /// -1:position在列表的前面;
            /// 0:position在列表的里面
            /// 1:position在列表的后面
            /// </summary>
            /// <param name="position"></param>
            /// <param name="dt"></param>
            /// <returns></returns>
            public int GetTime(int position, out DateTime dt)
            {
                dt = DateTime.MinValue;
                //TODO
                if (mPosList.Count < 2)
                    return -1;
                //通过列表最前面的数据,与最后面的数据,确认方向
                DIRECTION direction;
                if (mPosList.First().position < mPosList.Last().position)
                    direction = DIRECTION.FORWARD;
                else
                    direction = DIRECTION.BACKWARD;
                int idx;
                if (direction == DIRECTION.FORWARD)
                {
                    idx = mPosList.FindIndex(p => position <= p.position);
                }
                else
                {
                    idx = mPosList.FindIndex(p => position >= p.position);
                }
                
                if (idx == -1) 
                {
                    //在列表后面
                    dt = GetTime(position, mPosList.Count - 2);
                    return 1;
                }
                else if (idx == 0)
                {
                    //在列表前面
                    dt = GetTime(position, 0);
                    return -1;
                }
                else 
                {
                    //在列表里面
                    dt = GetTime(position, idx - 1);
                    return 0;
                }
            }
        }
        //TODO
        private List<PosCellList> mPosLists = new List<PosCellList>();
        private int lastGetTimeScanNo=-1;
        private int scanNo;//当前扫描号。每扫描一次,scanNo++
        public int ScanNo 
        {
            get {
                return scanNo;
            }
            set {
                if (scanNo != value) 
                {

                    //Clear(ScanNo, -2);

                    scanNo = value;
                }
            }
        }

        /// <summary>
        /// 删除scanNo 偏移 offset 之前的数据
        /// </summary>
        /// <param name="scanNo"></param>
        /// <param name="offset"></param>
        public void Clear(int scanNo,int offset) 
        {
            if (mPosLists.Count == 0)//没数据
                return;

            int index = mPosLists.FindIndex(pl => pl.scanNo == scanNo);
            if (index == -1)
                return;//可能还没出现
            index += offset;
            if (index < 0)
                return;//没有之前的数据
            mPosLists.RemoveRange(0, index + 1);
        }
        public void Clear() 
        {
            mPosLists.Clear();
        }
        public ScanPosDetect()
        {
        }
        
        public void Init(IFlyAD flyad) 
        {
            flyad.IStatusChangedEvent += new IStatusChangedEventHandler(flyad_IStatusChangedEvent);
        }

        void flyad_IStatusChangedEvent(object sender, IStatusChangedEventArgs e)
        {
            PosCellList poslist;
            if (mPosLists.Count == 0)
                mPosLists.Add(new PosCellList() { scanNo = ScanNo });
            else
            {
                poslist = mPosLists.Last();
                if (poslist.scanNo != ScanNo)
                {
                    mPosLists.Add(new PosCellList() { scanNo = ScanNo });
                }
            }
            poslist = mPosLists.Last();

            poslist.Add(e.Position,e.Time);
            
            
            //保持20个

            //if (mPosLists.Count() > 20) 
            //{
            //    mPosLists.RemoveAt(0);
            //}
        }
        
        
        /// <summary>
        /// 获取pos 对应的时间;
        /// 返回 -1 数据在 列表前;
        /// 返回 0 数据在列表内;
        /// 返回 1 数据在列表外;
        /// </summary>
        /// <param name="position">位置</param>
        /// <param name="scanNo">扫描号</param>
        /// <returns></returns>
        public int GetTime(int position, int scanNo, out DateTime dt)
        {
            if (lastGetTimeScanNo != scanNo)
            {
                //TODO
                Clear(lastGetTimeScanNo, -8);
                lastGetTimeScanNo = scanNo;
            }
            dt = DateTime.MinValue;
            if (mPosLists.Count == 0)//没数据
                return 1;

            int d = scanNo - mPosLists[0].scanNo;
            if (d < 0)
            { 
                //发生在过去
                //不正常
                return -2;
            }
            
            int index = mPosLists.FindIndex(pl => pl.scanNo == scanNo);
            if (index == -1)
                return 1;//可能还没出现


            PosCellList poslist = mPosLists[index];
            
            int ret = poslist.GetTime(position, out dt);
            if( (ret == 1) && (index != (mPosLists.Count - 1)))
                return 0;
            else if( (ret == -1) && (index != 0))
                return 0;
            else
                return ret;
        }
    }
}