BlowingDetect360.cs 52.2 KB
Newer Older
1
using AutoMapper;
潘栩锋's avatar
潘栩锋 committed
2
using FLY.Thick.Blowing;
潘栩锋's avatar
潘栩锋 committed
3
using FLY.Thick.Blowing.Common;
4
using FLY.Thick.Blowing.IService;
潘栩锋's avatar
潘栩锋 committed
5
using FlyADBase;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
using FObjBase;
using Misc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;

namespace FLY.Thick.Blowing360.Server
{
    /// <summary>
    /// <para>1.每次收到限位信号(有->没 and 没->有),都会触发 FilmInfoChangedEvent ;时间为上一次限位信号</para>
    /// <para>2.改变辊周长,膜长,都会触发 FilmInfoChangedEvent ;时间为前3次限位信号</para>
    /// <para>3.每次查找FilmInfo, 时间比 当前的限位信号+RenZiJiaPeriod 还要未来,修正 RenZiJiaPeriod,</para>
    /// <para>RenZiJiaPeriod预留10s的余量,避免不断修正</para>
    /// <para>并且触发FilmInfoChangedEvent 时间为上一次限位信号</para>
    /// </summary>
24
    public class BlowingDetect360 : IBlowingDetectInServer, IBlowingDetectService, INotifyPropertyChanged
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    {
        #region 无效接口
        /// <summary>
        /// 离开限位 到 撞下一个限位 的 旋转架转动总角度 单位°
        /// </summary>
        [Obsolete("360连续旋转 此接口无效")]
        public double RAngle { get { return 360; } set { } }

        /// <summary>
        /// 信号0 撞2次。 这个能自动设置。 两个撞同一信号 间隔30秒内,算一个换向信号。 
        /// 当为2次撞时:  StartTime = 第1次撞的StartTime, EndTime = 第2次撞的EndTime
        /// </summary>
        [Obsolete("360连续旋转 此接口无效")]
        public bool IsSign0Double { get; set; }


        /// <summary>
        /// 信号1 撞2次。 这个能自动设置。 两个撞同一信号 间隔30秒内,算一个换向信号。 
        /// 当为2次撞时:  StartTime = 第1次撞的StartTime, EndTime = 第2次撞的EndTime
        /// </summary>
        [Obsolete("360连续旋转 此接口无效")]
        public bool IsSign1Double { get; set; }

        /// <summary>
        /// 信号模式, 短信号模式(只撞一下),长信号模式(只要是转着,信号就长亮,直到换向)
        /// </summary>
        [Obsolete("360连续旋转 此接口无效")]
        public BlowingSignType SignType { get; set; } = BlowingSignType.Short;

        /// <summary>
        /// 撞限位 到 离开限位 的时间, 需要初始值,以后测量出来的
        /// </summary>
        [Obsolete("360连续旋转 此接口无效")]
        public TimeSpan LimitSignTime { get; set; } = TimeSpan.FromSeconds(4);


        /// <summary>
        /// 转向信号100ms 滤波
        /// </summary>
        [Obsolete("360连续旋转 此接口无效")]
        public bool IsSignFilter { get; set; }

        /// <summary>
        /// 缺少信号模式
        /// 正反信号线接得不好,有时候会丢失了。 
        /// 当前的旋转时间是上一次的1.1倍时,在以前的位置增加信号!!!!
        /// 当有连续两次相同的信号,且信号差刚好是旋转时间的1.9~2.1倍时,证明真的是缺少信号
        /// </summary>
        [Obsolete("360连续旋转 此接口无效")]
        public bool IsLackSignMode { get; set; }

        /// <summary>
        /// 旋转架为立式,测厚仪安装在二牵引前面,膜距离 与 旋转角度 一起变化, 旋转角度越大 膜距离越大
        /// </summary>
        [Obsolete("360连续旋转 此接口无效")]
        public bool Is3D { get; set; }

潘栩锋's avatar
潘栩锋 committed
82 83 84 85
        /// <summary>
        /// 使用编码器检测旋转架旋转位置模式
        /// </summary>
        public bool IsRPosMode { get; set; }
86

潘栩锋's avatar
潘栩锋 committed
87 88 89 90
        /// <summary>
        /// 人字架 离开限位 到 撞下一个限位 的 旋转架转动总脉冲 单位 脉冲
        /// </summary>
        public int RPosOfR { get; set; } = 50000;
91

潘栩锋's avatar
潘栩锋 committed
92 93 94 95
        /// <summary>
        /// 刚开机,信号列表一个信号都没有,创造一个时间点,作为信号点
        /// </summary>        
        public DateTime DefaultTime { get; set; }
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

        #endregion

        #region 参数

        /// <summary>
        /// 人字架 旋转1周 设置 时间。 
        /// 刚开机时,RenZiJiaPeriod = DefaultRPeriod;
        /// 异常时,RenZiJiaPeriod = DefaultRPeriod;
        /// </summary>
        public TimeSpan DefaultRPeriod { get; set; }


        /// <summary>
        /// 人字架到测厚仪膜长 单位m
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
112
        public double FilmLength { get; set; } = 25;//m
113 114 115 116

        /// <summary>
        /// 辊周长,单位mm
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
117 118 119 120 121 122
        public double RollPerimeter { get; set; } = 314;//mm

        /// <summary>
        /// 辊周长,单位m
        /// </summary>
        double MofR => RollPerimeter / 1000.0;
123

潘栩锋's avatar
潘栩锋 committed
124 125 126 127
        /// <summary>
        /// 自定义 输入口
        /// </summary>
        public bool IsInNoCustomized { get; set; }
128 129 130
        /// <summary>
        /// 自定义 转向信号 输入口序号 从0开始 默认9-1
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
131
        public int InNo_Limit0 { get; set; } = FlyADIODefine.Instance.InNo_Limit0;
132 133 134 135

        /// <summary>
        /// 自定义 转向信号 输入口序号 从0开始 默认10-1
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
136
        public int InNo_Limit1 { get; set; } = FlyADIODefine.Instance.InNo_Limit1;
137 138 139 140

        /// <summary>
        /// 自定义 辊速度 输入口序号 从0开始 默认11-1
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
141 142
        public int InNo_Roll { get; set; } = FlyADIODefine.Instance.InNo_Roll;

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
        #endregion

        #region 状态

        /// <summary>
        /// 当前旋转架旋转角度
        /// </summary>
        public double Angle { get; private set; }

        /// <summary>
        /// 当前方向
        /// </summary>
        public Misc.DIRECTION Direction { get; private set; }

        /// <summary>
        /// 累计旋转次数
        /// </summary>
        public virtual int RotationCnt { get; protected set; }


        /// <summary>
        /// 最新的限位信号序号
        /// </summary>
        public int LastLimitNo { get; private set; }
        /// <summary>
        /// 最后一个限位信号 到当前已经消耗的时间
        /// </summary>
        public TimeSpan PastTime { get; private set; }

        /// <summary>
        /// 当前线速度,单位 m/min
        /// </summary>
        public double FilmVelocity { get; private set; }

        /// <summary>
        /// 离开限位 到 撞下一个限位  的 旋转架转动时间, 需要初始值,以后测量出来的
        /// </summary>
        public TimeSpan RenZiJiaPeriod { get; private set; }

        /// <summary>
        /// 缓冲区拥有数据的时间长度,1s更新一次
        /// </summary>
        public TimeSpan BufTotalTime { get; private set; }

        /// <summary>
潘栩锋's avatar
潘栩锋 committed
188
        /// 原点旋转脉冲 上一次撞限位开关0时, 旋转架脉冲
189
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
190
        public int OrgRPos { get; protected set; }
191
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
192
        ///旋转架全局脉冲, 当前旋转架脉冲 = GlobalRPos - OrgRPos;
193
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
194 195 196 197 198
        public int GlobalRPos { get; protected set; }
        /// <summary>
        /// 旋转架脉冲速度,  旋转架角速度 = RSpeed * RAngle / RPosOfR
        /// </summary>
        public int RSpeed { get; protected set; }
199 200
        #endregion

潘栩锋's avatar
潘栩锋 committed
201 202


203 204 205
        #region 成员变量

        /// <summary>
潘栩锋's avatar
潘栩锋 committed
206
        /// 每圈数据列表
207
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
208
        public RList<LimitCell> mLimitList;
209 210 211 212

        /// <summary>
        /// 辊信号列表
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
213
        RList<RollCell2> mRollList;
214 215 216 217 218 219 220 221 222 223 224

        /// <summary>
        /// 转向信号列表
        /// </summary>
        RList<SignData> mSignList;

        /// <summary>
        /// 后触发,信号改变推送列表!!
        /// </summary>
        List<DateTime> mdtFilmInfoChangedEventList = new List<DateTime>();

潘栩锋's avatar
潘栩锋 committed
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
        /// <summary>
        /// 旋转脉冲列表
        /// </summary>
        RList<RPosData> mRPosList;

        /// <summary>
        /// 原点旋转脉冲 是否有效
        /// </summary>
        OrgRPosValidLevel OrgRPosValidLv;

        enum OrgRPosValidLevel
        {
            /// <summary>
            /// 刚开机,OrgRPos = 0
            /// </summary>
            Invalid,

            /// <summary>
            /// 没有撞任何一个转向信号,使用列表第1个作为OrgRPos
            /// </summary>
            JustListHeader,

            /// <summary>
            /// 撞转向信号0,真正的 OrgRPos = GlobalRPos
            /// </summary>
            Sign0
        }

潘栩锋's avatar
潘栩锋 committed
253
        IFlyADClientAdv flyad;
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public BlowingDetect360()
        {
            DefaultRPeriod = TimeSpan.FromMinutes(8);
            RenZiJiaPeriod = DefaultRPeriod;

            RollPerimeter = 314;//mm
            FilmLength = 25;//m

            Angle = 0;//°
            FilmVelocity = 0;//m/min

潘栩锋's avatar
潘栩锋 committed
270
            mRollList = new Misc.RList<RollCell2>(50 * 60 * 1000 / 314);//以50m/min 线速度,储存大于 60min数据
271 272
            mLimitList = new Misc.RList<LimitCell>(10);//人字架转10次的信号
            mSignList = new RList<SignData>(10);//人字架正反转信号
潘栩锋's avatar
潘栩锋 committed
273 274
            mRPosList = new RList<RPosData>(60 * 60);//人字架脉冲数据 1秒一个 60分钟数据

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
            RotationCnt = 0;


            Clear();

            PropertyChanged += new PropertyChangedEventHandler(RenZiJiaAndFilmPositionDetect_PropertyChanged);

        }


        int RotationCntToLimitIndex(int rotationcnt)
        {
            if (RotationCnt == 0)
                return -1;
            else
                return rotationcnt - 1 - (RotationCnt - mLimitList.Count);
        }
        /// <summary>
        /// 正反转信号列表中的序号,转旋转次数
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        int LimitIndexToRotationCnt(int index)
        {
            if (RotationCnt == 0)
                return 0;
            else
                return RotationCnt - mLimitList.Count + index + 1;
        }

        /// <summary>
        /// 使参数合法
        /// </summary>
        public void CheckParamErr()
        {
            if ((DefaultRPeriod.TotalMinutes < 4) || (DefaultRPeriod.TotalMinutes > 25))
            {
                DefaultRPeriod = TimeSpan.FromMinutes(5.8);
            }

            if ((FilmLength < 0) || (FilmLength > 100))
            {
                FilmLength = 25;
            }

            if ((RollPerimeter < 100) || (RollPerimeter > 1000))
            {
                RollPerimeter = 314;
            }
        }

        /// <summary>
        /// 触发膜信息修改事件
        /// </summary>
        /// <param name="dt">时间点</param>
        void TouchFilmInfoChanged(DateTime dt)
        {
            if (FilmInfoChangedEvent == null)
                return;

            mdtFilmInfoChangedEventList.Add(dt);

            TouchFilmInfoChanged();
        }
        /// <summary>
        /// 触发膜信息修改事件列表的全部事件
        /// </summary>
        void TouchFilmInfoChanged()
        {
            if (FilmInfoChangedEvent == null)
                return;

            while (mdtFilmInfoChangedEventList.Count > 0)
            {
                DateTime dt = mdtFilmInfoChangedEventList.First();
                DateTime dt_future;
                int ret = GetFuture(dt, out dt_future);
                if (ret == 1)//发生在信号列表的未来,留着,以后再触发!! 
                {
                    break;
                }
                else if (ret == 0)//发生在信号列表的中间
                {
                    //只需要触发最老的那个就够了!!!!!
                    mdtFilmInfoChangedEventList.Clear();
                    FilmInfoChangedEvent(this, new FilmInfoChangedEventArgs(dt_future));
                }
                else//发生在信号列表的以前
                {
                    //以前的事件不触发,除非就只有这么一个
                    mdtFilmInfoChangedEventList.RemoveAt(0);
                    if (mdtFilmInfoChangedEventList.Count == 0)
                    {
                        FilmInfoChangedEvent(this, new FilmInfoChangedEventArgs(dt_future));
                    }

                }
            }
        }
        //TODO
        void RenZiJiaAndFilmPositionDetect_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == nameof(FilmLength)) ||
                (e.PropertyName == nameof(RollPerimeter))
            )
            {
                TouchFilmInfoChanged_lastLimit(4);
            }
            else if (e.PropertyName == nameof(RenZiJiaPeriod))
            {
                TouchFilmInfoChanged_lastLimit(1);
            }
            else if (e.PropertyName == nameof(DefaultRPeriod))
            {
                RenZiJiaPeriod = DefaultRPeriod;
            }
        }
        /// <summary>
        /// 触发膜信息修改事件
        /// </summary>
        /// <param name="last_cnt">最后旋转几次</param>
        void TouchFilmInfoChanged_lastLimit(int last_cnt)
        {
            if (mLimitList.Count < last_cnt)
            {
                last_cnt = mLimitList.Count;
            }

            int idx = mLimitList.Count - last_cnt - 1;

            if (idx <= 0)
            {
潘栩锋's avatar
潘栩锋 committed
407
                TouchFilmInfoChanged(DefaultTime);
408 409 410 411 412 413 414 415 416 417 418
            }
            else
            {
                TouchFilmInfoChanged(mLimitList[idx].dt_begin);
            }
        }

        /// <summary>
        /// 创建完对象后,必须赋值
        /// </summary>
        /// <param name="flyad"></param>
潘栩锋's avatar
潘栩锋 committed
419
        public void Init(IFlyADClientAdv flyad)
420
        {
潘栩锋's avatar
潘栩锋 committed
421
            this.flyad = flyad;
422 423 424 425 426 427 428 429
            flyad.IStatusChangedEvent += new FlyADBase.IStatusChangedEventHandler(flyad_IStatusChangedEvent);
            FObjBase.PollModule.Current.Poll_Config(FObjBase.PollModule.POLL_CONFIG.ADD, new FObjBase.PollModule.PollHandler(OnPoll));
        }


        void update_limit(FlyADBase.IStatusChangedEventArgs e)
        {
            bool bchanged = false;//信号更新?
潘栩锋's avatar
潘栩锋 committed
430 431 432
            //TODO,不使用AD盒的时间
            //DateTime dt = e.Time;//信号更新发生的时间
            DateTime dt = DateTime.Now;
433
            int no = 0;
潘栩锋's avatar
潘栩锋 committed
434 435
            bool status = false;//信号的状态 true开始, false结束
            int position2 = e.Position2;
436

潘栩锋's avatar
潘栩锋 committed
437 438 439 440
            int inNo_Limit0 = IsInNoCustomized ? InNo_Limit0 : FlyADIODefine.Instance.InNo_Limit0;
            int inNo_Limit1 = IsInNoCustomized ? InNo_Limit1 : FlyADIODefine.Instance.InNo_Limit1;

            if (Misc.MyBase.CHECKBIT(e.IChanged, inNo_Limit0))//信号0 发生变化
441 442 443
            {
                bchanged = true;
                no = 0;
潘栩锋's avatar
潘栩锋 committed
444
                if (!Misc.MyBase.CHECKBIT(e.IStatus, inNo_Limit0))
445 446 447 448
                {
                    status = true;
                }
            }
潘栩锋's avatar
潘栩锋 committed
449
            else if (Misc.MyBase.CHECKBIT(e.IChanged, inNo_Limit1))//信号1 发生变化
450 451 452
            {
                bchanged = true;
                no = 1;
潘栩锋's avatar
潘栩锋 committed
453
                if (!Misc.MyBase.CHECKBIT(e.IStatus, inNo_Limit1))
454 455 456 457 458 459 460 461 462
                {
                    status = true;
                }
            }

            if (!bchanged)//没有信号更新
                return;

            SignData sd = new SignData() { Time = dt, On = status, No = no };
潘栩锋's avatar
潘栩锋 committed
463

464 465
            mSignList.RAdd(sd);

潘栩锋's avatar
潘栩锋 committed
466 467 468 469 470 471 472
            if (IsRPosMode)
            {
                //使用编码器测量旋转角度
                //方向 由编码器决定

                update_orgRPos(sd);
            }
潘栩锋's avatar
潘栩锋 committed
473
            else
潘栩锋's avatar
潘栩锋 committed
474 475 476 477 478
            {
                //没有编码器,方向由 信号类型决定
                Direction = no == 0 ? Misc.DIRECTION.FORWARD : Misc.DIRECTION.BACKWARD;
            }
            LastLimitNo = no;
潘栩锋's avatar
潘栩锋 committed
479 480


481
            update_limit(dt, status);
潘栩锋's avatar
潘栩锋 committed
482

483 484
        }

潘栩锋's avatar
潘栩锋 committed
485 486 487 488 489 490
        void update_orgRPos(SignData sd)
        {
            if (!sd.On)
                return;
            OrgRPosValidLv = OrgRPosValidLevel.Sign0;
            OrgRPos = sd.GlobalRPos;
潘栩锋's avatar
潘栩锋 committed
491

潘栩锋's avatar
潘栩锋 committed
492 493 494 495 496 497
            int speed = flyad.Speed2;
            if (speed > 0)
                Direction = DIRECTION.FORWARD;
            else if (speed < 0)
                Direction = DIRECTION.BACKWARD;
        }
498 499 500 501 502 503 504 505 506 507 508 509


        /// <summary>
        /// 添加正反转信号到 列表
        /// </summary>
        /// <param name="dt">信号更新发生的时间</param>
        /// <param name="status">信号的状态 true开始, false结束</param>
        void update_limit(DateTime dt, bool status)
        {
            //把信号加入到列表
            if (status == true)//添加,写入开始时间
            {
潘栩锋's avatar
潘栩锋 committed
510
                mLimitList.RAdd(new LimitCell() { dt_begin = dt, dt_end = dt });
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
                RotationCnt++;
            }
            else //写入 结束时间
            {
                if (mLimitList.Count > 0)
                {
                    LimitCell lc = mLimitList.Last();
                    lc.dt_end = dt;//完成一次信号
                }
                else
                {
                    //异常,还没开始,就结束了
                    mLimitList.RAdd(new LimitCell() { dt_begin = dt, dt_end = dt });
                    RotationCnt++;
                }
            }

            //更新其它参数
            update_param_for_limit(dt);
        }

        /// <summary>
        /// 更新因为旋转信号的添加导致的变化
        /// </summary>
        /// <param name="dt">信号变化的时间</param>
        void update_param_for_limit(DateTime dt)
        {
            if (mLimitList.Count() > 0)
            {
                LimitCell lc = mLimitList.Last();

                if (dt == lc.dt_begin)//刚撞转向
                {
                    //更新人字架旋转周期
                    if (mLimitList.Count > 1)
                    {
                        DateTime dt1 = mLimitList[mLimitList.Count - 1].dt_begin;
                        DateTime dt2 = mLimitList[mLimitList.Count - 2].dt_begin;
                        RenZiJiaPeriod = dt1 - dt2;//只有这个是正确的!!!
                                                   //触发修正事件!!!!!
                        TouchFilmInfoChanged(dt2);
                    }
                    else if (mLimitList.Count == 1)
                    {
                        DateTime dt1 = mLimitList[mLimitList.Count - 1].dt_begin;
潘栩锋's avatar
潘栩锋 committed
556
                        DefaultTime = dt1 - RenZiJiaPeriod;
557
                        //触发修正事件!!!!!
潘栩锋's avatar
潘栩锋 committed
558
                        TouchFilmInfoChanged(DefaultTime);
559 560 561 562 563 564 565
                    }
                }
            }
        }



潘栩锋's avatar
潘栩锋 committed
566
        void update_roll(FlyADBase.IStatusChangedEventArgs e)
567
        {
潘栩锋's avatar
潘栩锋 committed
568
            int inNo_Roll = IsInNoCustomized ? InNo_Roll : FlyADIODefine.Instance.InNo_Roll;
569 570
            //----------------------------------------------------------------------
            //更新辊信号
潘栩锋's avatar
潘栩锋 committed
571
            if (!Misc.MyBase.CHECKBIT(e.IChanged, inNo_Roll))
572 573 574
                return;//没有信号更新

            //信号的状态
潘栩锋's avatar
潘栩锋 committed
575
            bool status = !Misc.MyBase.CHECKBIT(e.IStatus, inNo_Roll);
576 577 578 579

            if (status)//1->0 触发!!!
            {
                mRollList.RAdd(
潘栩锋's avatar
潘栩锋 committed
580
                    new RollCell2()
581
                    {
潘栩锋's avatar
潘栩锋 committed
582 583
                        Time = DateTime.Now,//用于时间定位
                        SysTime = e.Time,//用于算速度
584 585 586 587 588 589
                    });

                //触发修正事件!!!!!
                TouchFilmInfoChanged();
            }
        }
潘栩锋's avatar
潘栩锋 committed
590

591 592 593 594 595 596 597 598 599 600 601 602
        void flyad_IStatusChangedEvent(object sender, FlyADBase.IStatusChangedEventArgs e)
        {
            update_limit(e);
            update_roll(e);

        }

        /// <summary>
        /// 1秒触发一次
        /// </summary>
        void OnPoll()
        {
潘栩锋's avatar
潘栩锋 committed
603 604 605
            if (!flyad.IsReady || !flyad.IsConnected)
                return;

606 607 608 609 610
            //--------------------------------------------------------------------
            //旋转架速度安全检测
            OnPoll_checkRVelocity();

            //--------------------------------------------------------------------
潘栩锋's avatar
潘栩锋 committed
611 612 613 614 615 616 617 618 619 620 621 622 623
            if (IsRPosMode)
            {
                //更新旋转脉冲
                OnPoll_updateRPos();
                //更新旋转角度
                OnPoll_updateAngleByRPos();
            }
            else
            {
                //--------------------------------------------------------------------
                //当前旋转架角度 
                OnPoll_updateAngle();
            }
624 625 626 627 628 629 630 631

            //--------------------------------------------------------------------
            //线速度安全检测
            OnPoll_checkFilmV();

            //--------------------------------------------------------------------
            //更新当前线速度
            OnPoll_updateFilmV();
潘栩锋's avatar
潘栩锋 committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

            //--------------------------------------------------------------------
            //限制列表大小
            KeepSize();
        }
        void KeepSize()
        {
            DateTime keepTime = DateTime.Now - TimeSpan.FromMinutes(60);
            //限制数量
            List<DateTime> dts = new List<DateTime>();

            dts.Add(KeepSize(mRollList, keepTime));
            dts.Add(KeepSize(mSignList, keepTime));
            dts.Add(KeepSize(mRPosList, keepTime));

            var earlyTime = dts.Min();
            if (DateTime.Now > earlyTime)
            {
                BufTotalTime = DateTime.Now - earlyTime;
            }
            else
            {
                BufTotalTime = TimeSpan.Zero;
            }
        }
        DateTime KeepSize<T>(List<T> list, DateTime keepTime) where T : ITimeCell
        {
            if (list.Count() > 0)
            {
                while (true)
                {
                    var cell = list.First();
                    if (cell.Time > keepTime)
                        break;

                    list.RemoveAt(0);
                    if (list.Count() == 0)
                        break;
                }
            }
            if (list.Count() > 0)
            {
                return list.First().Time;
            }
            return DateTime.MaxValue;
        }

        void OnPoll_updateRPos()
        {

            DateTime now = DateTime.Now;
            DateTime sysTime = flyad.Now;
            int globalRPos = flyad.Position2;
            int speed = flyad.Speed2;

            if (mRPosList.Count() > 0)
            {
                var lastPosData = mRPosList.Last();
                if (now - lastPosData.Time < TimeSpan.FromSeconds(1))
                {
                    return;
                }
            }

            mRPosList.RAdd(new RPosData() { Time = now, SysTime = sysTime, Speed = speed, GlobalRPos = globalRPos });


            if (OrgRPosValidLv == OrgRPosValidLevel.Invalid)
            {
                OrgRPos = globalRPos;
                //第1次, 赋值为第1个全局旋转脉冲
                OrgRPosValidLv = OrgRPosValidLevel.JustListHeader;
            }

            GlobalRPos = globalRPos;

            RSpeed = speed;


        }


        /// <summary>
        /// 旋转架速度换算
        /// </summary>
        void OnPoll_updateAngleByRPos()
        {
            //必须在 0~RPosOfR 范围内
            Angle = GetAngleByGlobalRPos(GlobalRPos);
        }
        public double GetAngleByGlobalRPos(int globalRPos)
        {
            //OrgRPos 为 -RAngle/2 

            int rPos = globalRPos - OrgRPos;

            //360°对应的旋转脉冲
            int rPosOf360 = RPosOfR;

            while (rPos < 0)
                rPos += rPosOf360;
            while (rPos > rPosOf360)
                rPos -= rPosOf360;

            double angle = 360 * rPos / RPosOfR;



            //角度范围是 0°~ 360°
            return angle;
742 743 744 745 746 747 748
        }

        /// <summary>
        /// 旋转架速度安全检测
        /// </summary>
        void OnPoll_checkRVelocity()
        {
潘栩锋's avatar
潘栩锋 committed
749
            DateTime dt = DefaultTime;
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
            if (mLimitList.Count > 0)
            {
                LimitCell lc = mLimitList.Last();
                if (lc.dt_end != DateTime.MinValue)
                    dt = lc.dt_end;
                else
                    dt = lc.dt_begin;
            }

            PastTime = DateTime.Now - dt;
            if (PastTime > TimeSpan.FromMinutes(25))//25min 
            {
                //大于25分钟,也没有一个信号,复位
                Clear();
            }
        }
        /// <summary>
        /// 更新旋转架速度
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
769
        void OnPoll_updateAngle()
770
        {
潘栩锋's avatar
潘栩锋 committed
771
            GetAngle(DateTime.Now, false, out double angle, out int rotationCnt);
772 773
            Angle = angle;
        }
潘栩锋's avatar
潘栩锋 committed
774 775


776 777 778 779 780 781 782
        /// <summary>
        /// 更新当前线速度
        /// </summary>
        void OnPoll_updateFilmV()
        {
            if (mRollList.Count > 1)
            {
潘栩锋's avatar
潘栩锋 committed
783 784
                DateTime sysTime1 = mRollList[mRollList.Count - 1].SysTime;//最后一次的sysTime
                int cnt = 5;//5次均值
785 786 787
                if (cnt > mRollList.Count)
                    cnt = mRollList.Count;

潘栩锋's avatar
潘栩锋 committed
788
                DateTime sysTime2 = mRollList[mRollList.Count - cnt].SysTime;//5次前的sysTime
潘栩锋's avatar
潘栩锋 committed
789
                TimeSpan ts_last = TimeSpan.FromTicks((sysTime1 - sysTime2).Ticks / (cnt - 1));//5次的平均滤波, 使用AD盒的时间算出来的时间差,最准
790

潘栩锋's avatar
潘栩锋 committed
791
                DateTime dt11 = mRollList[mRollList.Count - (cnt - 1)].Time;//4次前的PC时间
潘栩锋's avatar
潘栩锋 committed
792
                //因为没有使用AD盒的时间,所以可以用DateTime.Now 比较
潘栩锋's avatar
潘栩锋 committed
793
                TimeSpan ts_now = TimeSpan.FromTicks((DateTime.Now - dt11).Ticks / (cnt - 1));//使用 电脑的时间,算出来的时间差,大概而已
794
                TimeSpan ts_max;
潘栩锋's avatar
潘栩锋 committed
795
                if (ts_now > (ts_last + ts_last))//超过了2倍的时间,肯定是慢下来了
796 797 798 799 800 801 802 803
                {
                    ts_max = ts_now;
                }
                else
                {
                    ts_max = ts_last;
                }

潘栩锋's avatar
潘栩锋 committed
804 805
                FilmVelocity = MofR / ts_max.TotalMinutes;

806 807 808 809 810 811 812 813 814 815

            }
        }
        /// <summary>
        /// 线速度安全检测
        /// </summary>
        void OnPoll_checkFilmV()
        {
            if (mRollList.Count > 0)
            {
潘栩锋's avatar
潘栩锋 committed
816
                DateTime dt = mRollList[mRollList.Count - 1].Time;
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
                TimeSpan ts = DateTime.Now - dt;
                if (ts.TotalMinutes > 1)//1min 
                {
                    //大于1分钟,也没有一个信号,复位
                    Clear();
                }
            }
        }

        public void Clear()
        {


            mLimitList.Clear();
            mRollList.Clear();

潘栩锋's avatar
潘栩锋 committed
833
            DefaultTime = DateTime.Now;
834 835 836 837 838 839 840 841 842 843
            FilmVelocity = 0;
            RenZiJiaPeriod = DefaultRPeriod;
            RotationCnt = 0;//复位次数!!!
            LastLimitNo = -1;

            BufTotalTime = TimeSpan.Zero;
            ClearEvent?.Invoke(this);

        }

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
        //public class FilmInfo
        //{
        //    /// <summary>
        //    /// 上层 位于膜泡的角度 0~360°
        //    /// </summary>
        //    public double angle1;
        //    /// <summary>
        //    /// 下层 位于膜泡的角度 0~360°
        //    /// </summary>
        //    public double angle2;
        //    /// <summary>
        //    /// 膜泡旋转方向
        //    /// </summary>
        //    public Misc.DIRECTION direction;
        //    /// <summary>
        //    /// 线速度
        //    /// </summary>
        //    public double filmVelocity;
        //    /// <summary>
        //    /// 累计旋转次数
        //    /// </summary>
        //    public int rotationCnt;
        //}
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930




        public enum GetLimitTimeResult
        {
            /// <summary>
            /// 获取成功
            /// </summary>
            OK,
            /// <summary>
            /// 不存在该点
            /// </summary>
            NOEXIST,
            /// <summary>
            /// 限位已经撞了,但膜还没走完,等吧
            /// </summary>
            ISFURTURE
        }



        /// <summary>
        /// 计算膜长用;
        /// 输入膜长,获取对应的 限位点时间
        /// </summary>
        /// <param name="rotationcnt">旋转次数</param>
        /// <param name="filmlength">膜长</param>
        /// <param name="timeOfFilmLength">膜长对应限位时间</param>
        /// <returns></returns>
        public GetLimitTimeResult GetLimitTime(int rotationcnt, double filmlength, out DateTime timeOfFilmLength)
        {
            timeOfFilmLength = DateTime.MinValue;
            int index = RotationCntToLimitIndex(rotationcnt);
            if (index == -1)
                return GetLimitTimeResult.NOEXIST;
            if (index >= mLimitList.Count)
                return GetLimitTimeResult.ISFURTURE;

            DateTime dt = mLimitList[index].dt_begin;
            int ret = GetFuture(dt, filmlength, out timeOfFilmLength);
            if (ret == 1)
                return GetLimitTimeResult.ISFURTURE;

            return GetLimitTimeResult.OK;
        }


        /// <summary>
        /// 获取当前在膜上的测量点,对应于膜泡的角度信息, 当时间点,比LimitList.Last.dt+RenZiJiaPeriod还要后,更新RenZiJiaPeriod
        /// 返回 -1, 当前的时间点在列表以前;
        /// 返回 0, 当前的时间点在列表中;
        /// 返回 1, 当前的时间点在列表的未来;
        /// </summary>
        /// <param name="filmWidth">被压扁后的膜宽度</param>
        /// <param name="filmPosH">探头所在膜的横向位置</param>
        /// <param name="dt">测量的时间点</param>
        /// <returns></returns>
        public int GetFilmInfo(out FilmInfo filminfo, DateTime dt, double filmWidth, double filmPosH)
        {
            double filmlen = FilmLength;

            return GetFilmInfo(out filminfo, dt, filmWidth, filmPosH, filmlen);
        }
931

932 933 934 935 936 937 938 939 940 941 942 943 944 945
        int GetFilmInfo(out FilmInfo filminfo, DateTime dt, double filmWidth, double filmPosH, double filmLen)
        {
            filminfo = null;

            //与0°的偏移
            double a = filmPosH / filmWidth * 180;

            if ((a < 0) || (a > 180))
            {
                return -1;//不在膜的范围内
            }
            if (dt == DateTime.MinValue)
            {
                //TODO, BUG
潘栩锋's avatar
潘栩锋 committed
946
                return -1;//异常!!!
947 948
            }

潘栩锋's avatar
潘栩锋 committed
949
            int ret = GetAgo(dt, out DateTime ago_dt, out int ago_index);
950

潘栩锋's avatar
潘栩锋 committed
951 952 953 954 955
            bool ret2 = GetAngle(a, ago_dt, out double angle0, out double angle1, out int rotationcnt);
            if (!ret2)
                return -1;//异常!!!

            double velocity = RollList_GetVelocity(ago_index);
956 957 958 959 960 961 962

            filminfo = new FilmInfo()
            {
                angle1 = angle0,
                angle2 = angle1,
                filmVelocity = velocity,
                direction = Direction,
潘栩锋's avatar
潘栩锋 committed
963 964
                rotationCnt = rotationcnt,
                inCV = true
965 966 967 968 969 970 971 972 973 974 975 976
            };
            return ret;
        }

        #region 人字架时间点<-->24m后膜时间点
        /// <summary>
        /// 输入当前时间点,找到 刚出人字架时的时间点, 及那时候的线速度;
        /// 返回 -1, 当前的时间点在列表以前;
        /// 返回 0, 当前的时间点在列表中;
        /// 返回 1, 当前的时间点在列表的未来;
        /// </summary>
        /// <param name="dt">当前时间</param>
潘栩锋's avatar
潘栩锋 committed
977
        /// <param name="ago_dt">24米真实长度</param>
978
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
979
        int GetAgo(DateTime dt, out DateTime ago_dt, out int ago_index)
980
        {
潘栩锋's avatar
潘栩锋 committed
981
            return GetAgoFix(dt, out ago_dt, out ago_index);
982 983
        }
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
984
        /// 膜距离固定 获取膜距离前的时间点
985
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
986 987 988
        /// <param name="dt"></param>
        /// <param name="ago_dt"></param>
        /// <param name="ago_index"></param>
989
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
990
        int GetAgoFix(DateTime dt, out DateTime ago_dt, out int ago_index)
991 992
        {
            ago_dt = DateTime.MinValue;
潘栩锋's avatar
潘栩锋 committed
993 994 995 996
            ago_index = -1;
            int index;
            int ret = RollList_FindIndex(dt, out index);
            if (ret == 1)//发生在未来,什么都不干
997 998
                return ret;

潘栩锋's avatar
潘栩锋 committed
999 1000 1001 1002 1003 1004 1005
            //纵向位置
            double filmPosition = RollList_GetPosition(index, dt);


            double filmPos0 = filmPosition - FilmLength;
            ret = RollList_GetDateTime(filmPos0, out index, out ago_dt);
            ago_index = index;
1006
            return ret;
潘栩锋's avatar
潘栩锋 committed
1007 1008


1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
        }
        /// <summary>
        /// 输入过去时间,让它再加上膜长时间,得到未来时间;
        /// 返回 -1, 未来的时间在列表以前;
        /// 返回 0, 未来的时间在列表中;
        /// 返回 1, 未来的时间在列表的未来;
        /// </summary>
        /// <param name="dt">过去时间</param>
        /// <param name="dt_future">未来时间</param>
        /// <returns></returns>
        int GetFuture(DateTime dt, out DateTime dt_future)
        {
            return GetFuture(dt, FilmLength, out dt_future);
        }
        /// <summary>
        /// 输入过去时间,让它再加上膜长时间,得到未来时间;
        /// 返回 -1, 未来的时间在列表以前;
        /// 返回 0, 未来的时间在列表中;
        /// 返回 1, 未来的时间在列表的未来;
        /// </summary>
        /// <param name="dt">过去时间</param>
        /// <param name="filmlength">膜长</param>
        /// <param name="dt_future">未来时间</param>
        /// <returns></returns>
        int GetFuture(DateTime dt, double filmlength, out DateTime dt_future)
        {
            dt_future = DateTime.MinValue;
            double position;
            int i;
            int ret = RollList_FindIndex(dt, out i);
            if (ret == -1)
                position = RollList_GetPosition(0, dt);
            else if (ret == 0)
                position = RollList_GetPosition(i, dt);
            else
                return ret;

潘栩锋's avatar
潘栩锋 committed
1046
            position += filmlength;
1047

潘栩锋's avatar
潘栩锋 committed
1048
            return RollList_GetDateTime(position, out i, out dt_future);
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063

        }
        #endregion

        /// <summary>
        /// 计算出角度 0~360° 恒速阶段
        /// </summary>
        /// <param name="dtEnd">旋转结束时间点</param>
        /// <param name="dtStart">旋转开始时间点</param>
        /// <param name="dt">用于转换为角度的时间</param>
        /// <param name="angle">输出: 角度</param>
        void LimitListCal_ConstantVelocity(DateTime dtEnd, DateTime dtStart, DateTime dt, out double angle)
        {
            TimeSpan ts1 = dt - dtStart;
            TimeSpan ts2 = dtEnd - dtStart;
潘栩锋's avatar
潘栩锋 committed
1064

1065 1066 1067 1068 1069
            angle = 360 * ts1.Ticks / ts2.Ticks;
        }


        /// <summary>
潘栩锋's avatar
潘栩锋 committed
1070
        /// 时间点查找旋转角度 0~360°
1071 1072 1073 1074 1075 1076 1077
        /// </summary>
        /// <param name="dt">时间</param>
        /// <param name="modifyparam">当dt 发生在限位列表的未来,是否修改参数,如RenZiJiaPeriod</param>
        /// <param name="angle">角度</param>
        /// <param name="direction">旋转的方向</param>
        /// <param name="rotationCnt">旋转的次数</param>
        /// <param name="inCV">在恒速区</param>
潘栩锋's avatar
潘栩锋 committed
1078
        void GetAngle(DateTime dt, bool modifyparam, out double angle, out int rotationCnt)
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
        {
            int index;
            ListFindIndexResult ret = LimitList_FindIndex(dt, out index);

            DateTime dt_start;
            DateTime dt_end;

            switch (ret)
            {
                case ListFindIndexResult.Future://发生在未来 
                    {
潘栩锋's avatar
潘栩锋 committed
1090 1091
                        if (dt < DefaultTime)
                            DefaultTime = dt - TimeSpan.FromSeconds(10);
1092 1093 1094 1095

                        //检测是否需要修正 RenZiJiaPeriod
                        if (mLimitList.Count == 0)
                        {
潘栩锋's avatar
潘栩锋 committed
1096
                            dt_start = DefaultTime;
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
                        }
                        else
                        {
                            dt_start = mLimitList[index].dt_begin;
                        }
                        rotationCnt = LimitIndexToRotationCnt(index);

                        if (dt > (dt_start + RenZiJiaPeriod))
                        {
                            if (modifyparam)
                            {
                                //修正!!!!
                                RenZiJiaPeriod = dt - dt_start + TimeSpan.FromSeconds(10);
                            }
                            else
                            {
                                dt = dt_start + RenZiJiaPeriod;//只是为了看,随便了。 时间停止下来
                            }
                        }

                        dt_end = dt_start + RenZiJiaPeriod;
                        LimitListCal_ConstantVelocity(dt_end, dt_start, dt, out angle);
                    }
                    break;
                case ListFindIndexResult.BetweenCells:
                    {
                        dt_start = mLimitList[index].dt_begin;
                        dt_end = mLimitList[index + 1].dt_begin;
                        rotationCnt = LimitIndexToRotationCnt(index);
                        LimitListCal_ConstantVelocity(dt_end, dt_start, dt, out angle);
                    }
                    break;
                //case ListFindIndexResult.Past://过去 
                default:
                    {

潘栩锋's avatar
潘栩锋 committed
1133
                        dt_start = DefaultTime;
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
                        dt_end = mLimitList[0].dt_begin;
                        rotationCnt = LimitIndexToRotationCnt(-1);
                        LimitListCal_ConstantVelocity(dt_end, dt_start, dt, out angle);
                    }
                    break;
            }

            if (Direction == DIRECTION.BACKWARD)
                angle = 360 - angle;
        }

        #region RollList 查找
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
1147
        /// 获取 [idx,idx+1] 之间 的 位置, 单位m
1148 1149 1150 1151 1152 1153
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        double RollList_GetPosition(int idx, DateTime dt)
        {
潘栩锋's avatar
潘栩锋 committed
1154
            DateTime dt1 = mRollList[idx + 1].Time;
潘栩锋's avatar
潘栩锋 committed
1155 1156 1157
            double filmPos1 = (idx + 1) * MofR;
            DateTime dt0 = mRollList[idx].Time;
            double filmPos0 = (idx) * MofR;
1158

潘栩锋's avatar
潘栩锋 committed
1159
            return (filmPos1 - filmPos0) * (dt - dt0).Ticks / (dt1 - dt0).Ticks + filmPos0;
1160 1161
        }
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
1162
        /// 获取 [idx,idx+1] 之间 的 速度 m/min
1163 1164 1165 1166 1167
        /// </summary>
        /// <param name="idx"></param>
        /// <returns></returns>
        double RollList_GetVelocity(int idx)
        {
潘栩锋's avatar
潘栩锋 committed
1168 1169
            DateTime dt1 = mRollList[idx + 1].SysTime;
            DateTime dt2 = mRollList[idx].SysTime;
1170

潘栩锋's avatar
潘栩锋 committed
1171
            return MofR / dt1.Subtract(dt2).TotalMinutes;
1172 1173 1174 1175 1176
        }
        /// <summary>
        /// 获取 [idx,idx+1] 之间 的 时间
        /// </summary>
        /// <param name="idx"></param>
潘栩锋's avatar
潘栩锋 committed
1177
        /// <param name="filmPosition">纵向位置m</param>
1178
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
1179
        DateTime RollList_GetDateTime(int idx, double filmPosition)
1180 1181
        {
            //TODO 经常出错,position 无限小,idx=0
潘栩锋's avatar
潘栩锋 committed
1182
            DateTime dt1 = mRollList[idx + 1].Time;
潘栩锋's avatar
潘栩锋 committed
1183
            double filmPos1 = (idx + 1) * MofR;
潘栩锋's avatar
潘栩锋 committed
1184
            DateTime dt2 = mRollList[idx].Time;
潘栩锋's avatar
潘栩锋 committed
1185
            double filmPos2 = (idx) * MofR;
1186

潘栩锋's avatar
潘栩锋 committed
1187
            return dt2.Add(new TimeSpan((long)((dt1 - dt2).Ticks * (filmPosition - filmPos2) / (filmPos1 - filmPos2))));
1188 1189
        }
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
1190
        /// 通过 filmPosition 查找时间
1191 1192 1193 1194
        /// 返回 -1,这个过去的时间;
        /// 返回 0,在列表中;
        /// 返回 1,发生在未来的时间;
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1195
        /// <param name="filmPosition">单位m</param>
1196
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
1197
        int RollList_GetDateTime(double filmPosition, out int idx, out DateTime dt)
1198 1199 1200
        {
            idx = 0;
            dt = DateTime.MinValue;
潘栩锋's avatar
潘栩锋 committed
1201 1202
            if (filmPosition < 0)
                return -1;
1203 1204 1205 1206

            if (mRollList.Count == 0)
                return 1;

潘栩锋's avatar
潘栩锋 committed
1207 1208
            idx = (int)(filmPosition / MofR);
            if (idx >= mRollList.Count - 1)
1209
            {
潘栩锋's avatar
潘栩锋 committed
1210
                return 1;
1211
            }
潘栩锋's avatar
潘栩锋 committed
1212 1213
            dt = RollList_GetDateTime(idx, filmPosition);
            return 0;
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
        }

        /// <summary>
        /// dt 这个时间,在是 index 与 index+1 中间发生的
        /// 返回 -1,这个过去的时间;
        /// 返回 0,在列表中;
        /// 返回 1,发生在未来的时间;
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        int RollList_FindIndex(DateTime dt, out int index)
        {
            index = 0;
            if (mRollList.Count <= 1)//没数据,所以是发生在未来的
                return 1;

            for (int i = mRollList.Count - 1; i >= 0; i--)
            {
潘栩锋's avatar
潘栩锋 committed
1232
                if (dt >= mRollList[i].Time)
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 1280 1281 1282 1283 1284 1285 1286 1287 1288 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 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
                {
                    //找到了
                    if (i == (mRollList.Count - 1))//这个是未来的时间点,还没发生!!! 
                    {
                        index = i;
                        return 1;
                    }
                    else
                    {
                        index = i;
                        return 0;
                    }
                }
            }

            //这个在过去的时间
            return -1;
        }

        #endregion

        /// <summary>
        /// 时间对应旋转状态
        /// </summary>
        enum DTSTATUS
        {
            /// <summary>
            /// 发生在未来的均速阶段
            /// </summary>
            Future,
            /// <summary>
            /// 发生在过去的均速阶段
            /// </summary>
            Past,
            /// <summary>
            /// 发生在均速阶段
            /// </summary>
            Normal,
            /// <summary>
            /// 发生在方向切换阶段
            /// </summary>
            Swap,

        }
        enum ListFindIndexResult
        {
            /// <summary>
            /// 发生在未来, index.dt_end ~ 未来, 可能 index都还没有发生
            /// </summary>
            Future,
            /// <summary>
            /// 发生在 index.dt_end ~ (index+1).dt_begin
            /// </summary>
            BetweenCells,
            /// <summary>
            /// 发生在 过去
            /// </summary>
            Past
        }
        /// <summary>
        /// 找到 列表中 (开始时间,结束时间) 小于 dt 的信号点
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        ListFindIndexResult LimitList_FindIndex(DateTime dt, out int index)
        {
            index = 0;
            if (mLimitList.Count <= 0)//没数据,所以是发生在未来的
                return ListFindIndexResult.Future;

            for (int i = mLimitList.Count - 1; i >= 0; i--)
            {
                LimitCell lc = mLimitList[i];
                if (dt >= lc.dt_begin)
                {
                    //找到了
                    index = i;
                    if (i == (mLimitList.Count - 1))//这个是未来的时间点,还没发生!!! 
                    {
                        return ListFindIndexResult.Future;
                    }
                    else
                    {
                        return ListFindIndexResult.BetweenCells;
                    }
                }
            }

            //这个在过去的时间
            return ListFindIndexResult.Past;
        }



        /// <summary>
        /// 获取膜角度
        /// </summary>
        /// <param name="a">与0°的偏移, 0~180</param>
        /// <param name="dt">时间点</param>
        /// <param name="angle0">上面的膜对应的膜角度</param>
        /// <param name="angle1">下面的膜对应的膜角度</param>
        /// <param name="rotationCnt">扫描次数</param>
        bool GetAngle(double a, DateTime dt, out double angle0, out double angle1, out int rotationCnt)
        {
            angle0 = 0;
            angle1 = 0;
            rotationCnt = 0;
            if (a > 180 || a < 0)
                return false;

            //压扁对面的角度
            double a_other = 360 - a;
            double angle;

            //不修改旋转速度!!!!!!!!!!!!
潘栩锋's avatar
潘栩锋 committed
1349
            GetAngle(dt, false, out angle, out rotationCnt);
1350

潘栩锋's avatar
潘栩锋 committed
1351 1352
            a = GetValidAngle(a + angle);
            a_other = GetValidAngle(a_other + angle);
1353 1354 1355 1356 1357 1358

            angle0 = a;
            angle1 = a_other;
            return true;
        }

潘栩锋's avatar
潘栩锋 committed
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
        /// <summary>
        /// 限制角度在 [0~360)
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        double GetValidAngle(double a)
        {
            if (a >= 360)
                a -= 360;
            else if (a < 0)
                a += 360;
            return a;
        }
1372 1373 1374 1375 1376

        public void Apply()
        {
            Save();
        }
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
        //public class FilmInfoChangedEventArgs : EventArgs
        //{
        //    public FilmInfoChangedEventArgs(DateTime time)
        //    {
        //        Time = time;
        //    }
        //    /// <summary>
        //    /// 开始时间点
        //    /// </summary>
        //    public DateTime Time
        //    {
        //        get;
        //        set;
        //    }
        //}

        //public delegate void FilmInfoChangedEventHandler(object sender, FilmInfoChangedEventArgs e);
1394 1395
        public event FilmInfoChangedEventHandler FilmInfoChangedEvent;

1396
        //public delegate void ClearEventHandler(object sender);
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
        public event ClearEventHandler ClearEvent;


        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            Clear();
        }

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion


        /// <summary>
        /// 加载历史数据
        /// </summary>
        /// <returns></returns>
        public bool Load()
        {
潘栩锋's avatar
潘栩锋 committed
1421
            return BlowingDetect360JsonDb.Load(this, file_path);
1422 1423 1424 1425 1426
        }
        string file_path = "blowingdetect360.json";
        /// <summary>
        /// 保存
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1427
        public bool Save()
1428
        {
潘栩锋's avatar
潘栩锋 committed
1429
            return BlowingDetect360JsonDb.Save(this, file_path);
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
        }


        /// <summary>
        /// 获取 信号列表
        /// </summary>
        /// <param name="asyncDelegate">retdata = GetSignListReponse</param>
        /// <param name="asyncContext"></param>
        public void GetSignList(AsyncCBHandler asyncDelegate, object asyncContext)
        {
            GetSignListReponse p = new GetSignListReponse();

            p.datas = mSignList;

            asyncDelegate(asyncContext, p);
        }
        /// <summary>
        /// 获取 信号列表
        /// </summary>
        /// <param name="begin">开始时间</param>
        /// <param name="asyncDelegate">retdata = GetSignListReponse</param>
        /// <param name="asyncContext"></param>
        public void GetSignList(DateTime begin, AsyncCBHandler asyncDelegate, object asyncContext)
        {
            GetSignListReponse p = new GetSignListReponse();

            p.datas = mSignList.FindAll(r => r.Time >= begin);

            asyncDelegate(asyncContext, p);
        }

        /// <summary>
        /// 获取 辊信号列表
        /// </summary>
        /// <param name="begin">开始时间</param>
        /// <param name="asyncDelegate">retdata = GetRollListReponse</param>
        /// <param name="asyncContext"></param>
        public void GetRollList(DateTime begin, AsyncCBHandler asyncDelegate, object asyncContext)
        {
            var p = new GetRollListReponse();

潘栩锋's avatar
潘栩锋 committed
1471
            p.datas = mRollList.FindAll(r => r.Time > begin).Select(r => new RollCell() { Time = r.Time, SysTime = r.SysTime }).ToList();
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487

            asyncDelegate(asyncContext, p);
        }

        /// <summary>
        /// 获取 转向信号列表
        /// </summary>
        /// <param name="begin">开始时间</param>
        /// <param name="asyncDelegate">retdata = GetRollListReponse</param>
        /// <param name="asyncContext"></param>
        public void GetLimitList(DateTime begin, AsyncCBHandler asyncDelegate, object asyncContext)
        {
            var p = new GetLimitListReponse();

            p.datas = mLimitList.FindAll(r =>
            {
潘栩锋's avatar
潘栩锋 committed
1488
                return (r.dt_end != DateTime.MinValue) && (r.dt_begin > begin);
1489 1490 1491 1492
            });
            asyncDelegate(asyncContext, p);
        }

潘栩锋's avatar
潘栩锋 committed
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
        /// <summary>
        /// 获取 旋转位置列表
        /// </summary>
        /// <param name="begin">开始时间</param>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        public void GetRPosList(DateTime begin, AsyncCBHandler asyncDelegate, object asyncContext)
        {
            var p = new GetRPosListReponse();

            p.datas = mRPosList.FindAll(r => r.Time > begin);

            asyncDelegate(asyncContext, p);
        }


1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
        public void GetFilmLength3D(AsyncCBHandler asyncDelegate, object asyncContext)
        {
            //不实现
            //throw new NotImplementedException();
        }

        public void SetFilmLength3D(List<double> data)
        {
            //不实现
            //throw new NotImplementedException();
        }
潘栩锋's avatar
潘栩锋 committed
1520 1521


1522 1523 1524 1525
    }

    public class BlowingDetect360JsonDb
    {
潘栩锋's avatar
潘栩锋 committed
1526 1527
        static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c =>
        {
1528 1529 1530
            c.CreateMap<BlowingDetect360, BlowingDetect360JsonDb>().ReverseMap();
        }));

潘栩锋's avatar
潘栩锋 committed
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
        /// <summary>
        /// 加载历史数据
        /// </summary>
        /// <returns></returns>
        public static bool Load(BlowingDetect360 src, string file_path)
        {
            if (File.Exists(file_path))
            {
                try
                {
                    string json = File.ReadAllText(file_path);
                    var jsonDb = JsonConvert.DeserializeObject<BlowingDetect360JsonDb>(json);
                    Mapper.Map(jsonDb, src);
                    return true;
                }
                catch
                {
                    //异常,没有json 解码失败

                }

            }
            return false;
        }

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

            }
            return false;
        }

1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
        /// <summary>
        /// 人字架 旋转1周 设置 时间。 
        /// 刚开机时,RenZiJiaPeriod = DefaultRPeriod;
        /// 异常时,RenZiJiaPeriod = DefaultRPeriod;
        /// </summary>
        public TimeSpan DefaultRPeriod;

        /// <summary>
        /// 人字架到测厚仪膜长 单位m
        /// </summary>
        public double FilmLength;

        /// <summary>
        /// 辊周长,单位mm
        /// </summary>
        public double RollPerimeter;

        /// <summary>
        /// 旋转反向
        /// </summary>
        public bool IsReversed;
潘栩锋's avatar
潘栩锋 committed
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606

        /// <summary>
        /// 使用编码器检测旋转架旋转位置模式
        /// </summary>
        public bool IsRPosMode;

        /// <summary>
        /// 人字架 离开限位 到 撞下一个限位 的 旋转架转动总脉冲 单位 脉冲
        /// </summary>
        public int RPosOfR = 50000;
潘栩锋's avatar
潘栩锋 committed
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626

        /// <summary>
        /// 自定义 转向信号 输入口序号 从0开始 默认9-1
        /// </summary>
        public int InNo_Limit0 = 9 - 1;

        /// <summary>
        /// 自定义 转向信号 输入口序号 从0开始 默认10-1
        /// </summary>
        public int InNo_Limit1 = 10 - 1;

        /// <summary>
        /// 自定义 辊速度 输入口序号 从0开始 默认11-1
        /// </summary>
        public int InNo_Roll = 11 - 1;

        /// <summary>
        /// 自定义 输入口
        /// </summary>
        public bool IsInNoCustomized;
1627 1628
    }
}