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

using FObjBase;
using FLY.FeedbackRenZiJia.OBJ_INTERFACE;
using FLY.FeedbackRenZiJia.Common;
using FLY.FeedbackRenZiJia.IService;
using FLY.OBJComponents.Server;
using FLY.OBJComponents.Common;
16
using System.Net;
潘栩锋's avatar
潘栩锋 committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

namespace FLY.FeedbackRenZiJia.Server
{
    public class FeedbackHeat : IFeedbackHeat,Misc.ISaveToXml
    {
        #region markno
        const int MARKNO_SAVE = 1;
        const int MARKNO_LOAD = 2;
        const int MARKNO_DELAY_ISCONNECTED = 3;
        #endregion


        #region IFeedbackHeat 成员
        #region 参数

32 33 34 35 36
        /// <summary>
        /// 作为客户端模式
        /// </summary>
        public bool IsClient { get; set; }
        public IPEndPoint PLCep { get; set; }
潘栩锋's avatar
潘栩锋 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        /// <summary>
        /// 自动控制使能
        /// </summary>
        public bool IsAuto { get; set; }


        /// <summary>
        /// 加热后起效延时(s)
        /// </summary>
        public int Delay { get; set; } = 10;

        /// <summary>
        /// 检测功能使能
        /// </summary>
        public bool HasCheck { get; set; }

        /// <summary>
        /// 手动加热步进
        /// </summary>
        public int Step { get; set; } = 5;


        #endregion
        #region 动态数据
        /// <summary>
        /// 通道数, 从测厚仪获取的!!!!
        /// </summary>
        public int ChannelCnt { get; protected set; } = 44;
        /// <summary>
        /// 分区数, 从测厚仪获取的!!!!
        /// </summary>
        public int NBolts { get; protected set; } = 88;

        /// <summary>
        /// 保存的加热策略名称
        /// </summary>
        public string HeatsProductName { get; protected set; }="default";

        /// 连接到PLC?
        /// </summary>
        public bool IsConnectedWithPLC { get; set; }
        /// <summary>
        /// 上一次加热修改时间
        /// </summary>
        public DateTime LastChangedTime { get; set; } = DateTime.MinValue;

        /// <summary>
        /// 当前
        /// </summary>
86
        //public int[] Currs { get; set; }
潘栩锋's avatar
潘栩锋 committed
87 88 89 90


        #endregion
        #region 异常检测
91

潘栩锋's avatar
潘栩锋 committed
92 93 94
        /// <summary>
        /// 烧了的加热棒
        /// </summary>
95 96
        public bool[] Bads
        {
97
            get { return mHeatCheck.Bads; }
98
        }
潘栩锋's avatar
潘栩锋 committed
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

        /// <summary>
        /// 当前风机启动中
        /// </summary>
        public bool HasFan { get; set; }

        /// <summary>
        /// 加热棒有电流
        /// </summary>
        public bool HasElectricity { get; set; }

        /// <summary>
        /// 线速度 大于 3m/min
        /// </summary>
        public bool HasFilmVelocity { get; set; }
        /// <summary>
        /// 检测线速度 使能
        /// </summary>
        public bool HasCheckFilmVelocity { get; set; }


        /// <summary>
        /// 当前正在检测的加热通道
        /// </summary>
        public int CheckNo { get; set; } = -2;


        /// <summary>
        /// 正在检测中
        /// </summary>
        public bool CheckEnable { get; set; } = false;




        #endregion
        #endregion

137
        
潘栩锋's avatar
潘栩锋 committed
138

139
        private IPLCLink plc;
潘栩锋's avatar
潘栩锋 committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
        private FLY.Thick.Blowing.IService.IBlowing renzijia;
        private FLY.Thick.Blowing.IService.IBlowingDetect mBDetect;
        private UInt16 CurrsUpdate=0;

        public HeatCell mHeatCell = new HeatCell();
        HeatChanged mHeatChanged = new HeatChanged();
        public HeatBuf mHeatBuf = new HeatBuf();
        HeatCheck mHeatCheck = new HeatCheck();
        public SnapShotBuf mSnapShotBuf = new SnapShotBuf();
        /// <summary>
        /// 报警系统
        /// </summary>
        public WarningSystem mWarning;

        static FeedbackHeat()
        {
            Misc.SaveToXmlHepler.Regist(typeof(HeatCell));
            Misc.SaveToXmlHepler.Regist(typeof(HeatBuf));
        }


        public FeedbackHeat()
        {
163
            PLCep = Misc.StringConverter.ToIPEndPoint("192.168.50.60:502");
潘栩锋's avatar
潘栩锋 committed
164
            Load();
165 166 167 168 169 170
            if (IsClient)
                plc = new PLCLink(PLCep);
            else
                plc = new HMI();

            plc.PropertyChanged += new PropertyChangedEventHandler(hmi_PropertyChanged);
171 172 173 174 175
            HasCheckFilmVelocity = plc.HasElectricity;
            HasFan = plc.HasFan;
          


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


            InitBuf();

            //-------------------------------------------------------------------


            PropertyChanged += new PropertyChangedEventHandler(FeedbackHeat_PropertyChanged);

            //-------------------------------------------------------------------
            //HeatBuf
            //UpdateTotalDelay();

            //-------------------------------------------------------------------
            //HeatCell
            mHeatCell.Init(mHeatBuf);

            mHeatCell.PreHeatApplyEvent = (c) => {
                //有开风机才能加热
                if ((HasCheck) && (!HasFan))
                    return false;//返回false,不能加热

                //检测时,也是使用 HeatApply 确定加热的,要把下面注解掉。所以只能寄望于客户不要按HeatApply

                //检测中,不能改变加热
                //if (mHeatCheck.Enable)
                //    return false;

                return true;
            };

            mHeatCell.AfterHeatApplyEvent += (c) => {
                //检测中,不保存加热
                if (mHeatCheck.Enable)
                    return;

                //保存最后一次加热
                SaveHeatsLast();
                
                //记录改变!!!!
                mHeatChanged.Add(mHeatCell.Heats);
                UndoIdx = mHeatChanged.mItem.Count() - 1;
                
            };

            mHeatCell.AfterClearOffsetsEvent += (c) => {
                UndoIdx = mHeatChanged.mItem.Count() - 1;
            };
            

            mHeatCell.PropertyChanged += new PropertyChangedEventHandler(mHeatCell_PropertyChanged);//当HeatCell.Heats 修改后,设置到HMI

            Misc.BindingOperations.SetBinding(mHeatCell, "Kp", mHeatBuf, "Kp", Misc.BindingOperations.BindingMode.TwoWay);

           
            if (!Directory.Exists("feedbackheats"))
                Directory.CreateDirectory("feedbackheats");
            
            //-------------------------------------------------------------------
            //HeatChanged
            Misc.BindingOperations.SetBinding(this.mHeatChanged, "Last", this, "LastChangedTime");

            //-------------------------------------------------------------------
            //HeatCheck
            mHeatCheck.Init(mHeatCell);
            mHeatCheck.PropertyChanged += new PropertyChangedEventHandler(mHeatCheck_PropertyChanged);

243
            Misc.BindingOperations.SetBinding(this, "HasElectricity", mHeatCheck, "HasElectricity");
潘栩锋's avatar
潘栩锋 committed
244 245 246
            Misc.BindingOperations.SetBinding(this, "HasCheck", mHeatCheck, "Has");
            Misc.BindingOperations.SetBinding(mHeatCheck, "Enable", this, "CheckEnable", Misc.BindingOperations.BindingMode.TwoWay);
            Misc.BindingOperations.SetBinding(mHeatCheck, "CheckNo", this, "CheckNo");
247
            //Misc.BindingOperations.SetBinding(mHeatCheck, "Bads", this, "Bads");
248
            
249 250 251 252 253 254 255
            mHeatCheck.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == "Bads")
                {
                    NotifyPropertyChanged("Bads");
                }
            };
潘栩锋's avatar
潘栩锋 committed
256
            mWarning = new WarningSystem();
257 258 259 260 261 262


            if (plc is PLCLink)
            {
                ((PLCLink)plc).Start();
            }
潘栩锋's avatar
潘栩锋 committed
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
        }

        void mHeatCheck_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Enable") 
            {
                if (mHeatCheck.Enable)
                {
                    if (!IsConnectedWithPLC)
                    { 
                        //连接断开
                        mHeatCheck.Enable = false;
                    }
                    else if ((HasCheck) && (!HasFan))
                    {
                        //风机没启动,要关闭加热
                        mHeatCheck.Enable = false;
                    }
                    else
                    {
                        //需要关闭自控
                        IsAuto = false;
                    }
                }
            } 
        }

        #region 异常
        void InitError()
        {
            //--------------------------------------------------------------------------------
            //连接断开事件
            FObjBase.PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD,
                () =>
                {
                    Misc.BindingOperations.SetBinding(this, "IsConnectedWithPLC", () =>
                    {
                        bool b = !IsConnectedWithPLC;

                        ERR_STATE state = b ? ERR_STATE.ON : ERR_STATE.OFF;

                        ERRNO errno = ERRNOs.ERRNO_PLC_DISCONNECTED;
                        mWarning.Add(errno.Code, errno.Descrption, state);
                    });
                }, TimeSpan.FromSeconds(3), true, false, this, MARKNO_DELAY_ISCONNECTED, true);


            //------------------------------------------------------------------------
            //检测功能的结果
            this.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == "HasCheck")
                {
                    if (!HasCheck)
                    {
                        ERRNO[] eRRNOs = new ERRNO[] {
                            ERRNOs.ERRNO_NO_FAN,
                            ERRNOs.ERRNO_OPEN_CIRCUIT,
                            ERRNOs.ERRNO_SHORT_CIRCUIT,
                            ERRNOs.ERRNO_POWER_OFF
                        };
                        foreach (ERRNO eRRNO in eRRNOs)
                        {
                            mWarning.Add(eRRNO.Code, eRRNO.Descrption, ERR_STATE.OFF);
                        }
                    }
                    return;
                }
                else if (e.PropertyName == "HasFan")
                {
                    if (HasCheck)
                    {
                        ERR_STATE state = (!HasFan) ? ERR_STATE.ON : ERR_STATE.OFF;

                        ERRNO errno = ERRNOs.ERRNO_NO_FAN;
                        mWarning.Add(errno.Code, errno.Descrption, state);
                    }
                }
            };

            //------------------------------------------------------------------------
            //检测功能的结果
            Dictionary<string, ERRNO> error_property = new Dictionary<string, ERRNO>();
            error_property.Add("OpenCircuit", ERRNOs.ERRNO_OPEN_CIRCUIT);
            error_property.Add("ShortCircuit", ERRNOs.ERRNO_SHORT_CIRCUIT);
            error_property.Add("PowerOff", ERRNOs.ERRNO_POWER_OFF);
            mHeatCheck.PropertyChanged += (s, e) =>
            {
                if (HasCheck)
                {
                    if (error_property.ContainsKey(e.PropertyName))
                    {
355 356
                        bool b = (bool)Misc.PropertiesManager.GetValue(mHeatCheck, e.PropertyName);
                        
潘栩锋's avatar
潘栩锋 committed
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
                        ERR_STATE state = b ? ERR_STATE.ON : ERR_STATE.OFF;

                        ERRNO errno = error_property[e.PropertyName];
                        mWarning.Add(errno.Code, errno.Descrption, state);
                    }
                }
            };

            //------------------------------------------------------------------------
            //线速度异常
            this.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == "HasCheckFilmVelocity")
                {
                    if (!HasCheckFilmVelocity)
                    {
                        ERRNO eRRNO = ERRNOs.ERRNO_NO_VELOCITY;
                        mWarning.Add(eRRNO.Code, eRRNO.Descrption, ERR_STATE.OFF);
                    }
                }
                else if (e.PropertyName == "HasFilmVelocity")
                {
                    if (HasCheckFilmVelocity)
                    {
                        ERR_STATE b = (!HasFilmVelocity) ? ERR_STATE.ON : ERR_STATE.OFF;
                        ERRNO eRRNO = ERRNOs.ERRNO_NO_VELOCITY;
                        mWarning.Add(eRRNO.Code, eRRNO.Descrption, b);
                    }
                }
            };
        }

        #endregion

        void InitBuf()
        {
393 394
            plc.ChannelCnt = (UInt16)ChannelCnt;
            //Currs = new int[ChannelCnt];
潘栩锋's avatar
潘栩锋 committed
395 396 397 398 399 400 401 402
            mHeatChanged.Init(ChannelCnt);
            mHeatBuf.Init(NBolts, ChannelCnt);
            mHeatCell.Init2(NBolts, ChannelCnt);

            UndoIdx = mHeatChanged.mItem.Count() - 1;

            mSnapShotBuf.Init(NBolts, ChannelCnt);
        }
403
        void hmi_PropertyChanged(object s, PropertyChangedEventArgs _e)
潘栩锋's avatar
潘栩锋 committed
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
            FObjBase.PollModule.Current.Dispatcher.Invoke(
                new PropertyChangedEventHandler(
                (sender, e)=> {
                    if (e.PropertyName == "Errno")
                    {
                        if (plc.Errno == -1)
                        {
                            IsConnectedWithPLC = false;
                        }
                        else
                        {
                            IsConnectedWithPLC = true;
                            //刚成功连接上
                            //重新复制数据到 PLC
                            RecoverPLC();
                        }
                    }
                    else if (e.PropertyName == "HasElectricity")
                    {
                        HasElectricity = plc.HasElectricity;
                    }
                    else if (e.PropertyName == "HasFan")
                    {
                        HasFan = plc.HasFan;
                    }
            }), s, _e);
潘栩锋's avatar
潘栩锋 committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
        }

        List<FlyData_FeedbackHeat> test_data = new List<FlyData_FeedbackHeat>();
        int test_index = 0;
        public int TestIndex 
        {
            get { return test_index; }
            set {
                if (test_index != value) 
                {
                    test_index = value;
                    NotifyPropertyChanged("TestIndex");
                }
            }
        }
        bool TestMode = false;
        /// <summary>
        /// 从历史数据加载,模拟整个过程
        /// </summary>
        public void Test()
        {
            TestMode = true;
            //if (TestIndex == 0)
            //{
            //    StreamReader sr = new StreamReader("test.csv", Encoding.GetEncoding("GB2312"));
            //    string header = sr.ReadLine();
            //    while (!sr.EndOfStream)
            //    {
            //        string str = sr.ReadLine();
            //        FlyData_FeedbackHeat d = new FlyData_FeedbackHeat();
            //        d.TryParse(header, str);
            //        test_data.Add(d);
            //    }
            //    sr.Close();
            //    NBolts = test_data[0].Thicks.Count();
            //    ChannelCnt = test_data[0].Heats.Count();
            //    Add( 
            //        test_data[0].Time,
            //        test_data[0].Direction,
            //        test_data[0].OrgBoltNo,
            //        test_data[0].RotatePeriod,

            //        test_data[0].Thicks);
                
            //}

            //if (TestIndex < test_data.Count())
            //{
            //    int i = TestIndex;
            //    FlyData_FeedbackHeat d = test_data[i];
                
            //    mHeatChanged.Test_SetHeats(d.Heats, d.HTime);
                
            //    mHeatCell.ModifyPreHeats(d.Heats);
            //    mHeatCell.HeatApply();

            //    Add(i + 1, d.Time, d.Direction, d.OrgBoltNo, d.Thicks);
            //    TestIndex++;
            //}
            TestMode = false;
        }


        void mHeatCell_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Heats")
            {
                Output(mHeatCell.Heats.ToArray());
            }

        }
         void FeedbackHeat_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (GetSavePropertyNames().Contains(e.PropertyName))
            {
                //PollModule.Current.Poll_JustOnce(new PollModule.PollHandler(delegate()
                //{
                //    //Save();//数据滞后保存!!!!!
                //}), this, MARKNO_SAVE);
            }
            if (e.PropertyName == "ChannelCnt") 
            {
                //重新初始化!!!!
                InitBuf();
                //滞后触发
                PollModule.Current.Poll_JustOnce(new PollModule.PollHandler(delegate ()
                {
                    LoadHeatsLast();
                }), this, MARKNO_LOAD);

            }
            else if (e.PropertyName == "NBolts")
            {
                //重新初始化!!!!
                InitBuf();
            }
            else if (e.PropertyName == "Delay")
            {
                //重新计算总延时!!!!
                UpdateTotalDelay();
            }
            else if (e.PropertyName == "HasFan")
            {
                if ((HasCheck) &&(!HasFan))
                { 
                    //风机没开,停止一切动作。
                    StopAnyHeating();
                }
            }
            else if (e.PropertyName == "IsAuto")
            {
                if (IsAuto)
                {
                    //启动了自控,应该关闭检测
                    mHeatCheck.Enable = false;
                }
            }
            else if (e.PropertyName == "IsConnectedWithPLC")
            {
                if (!IsConnectedWithPLC)
                {
                    mHeatCheck.Enable = false;
                    IsAuto = false;
                }
            }
        }

        public void Init(
            FLY.Thick.Blowing.Client.BlowingServiceClient renzijia,
            FLY.Thick.Blowing.IService.IBlowingDetect bDetect
            ) 
        {
            
            this.renzijia = renzijia;
            mBDetect = bDetect;


            //新数据,与参数被修改时,都会收到事件,应该区别开
            renzijia.DataEvent += (s, e) =>
            {
                Add(e.Time, e.EndTime, e.Direction, e.RotatePeriod, e.RotationCnt,
                    renzijia.OrgBoltNo, mBDetect.RAngle, mBDetect.FilmLength,
                    e.Frame);
            };

            Misc.BindingOperations.SetBinding(renzijia, "NBolts", this, "NBolts");
            Misc.BindingOperations.SetBinding(renzijia, "ChannelCnt", this, "ChannelCnt");

            mBDetect.PropertyChanged += MBDetect_PropertyChanged;

            InitError();
        }

        private void MBDetect_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "FilmLength")
            {
                UpdateTotalDelay();
            }
            else if (e.PropertyName == "FilmVelocity")
            {
                UpdateTotalDelay();
                HasFilmVelocity = (mBDetect.FilmVelocity > 3);//大于3m/min 正常生产

                //当线速度 很小,肯定是没生产,停止加热,停止自控
596
                if (HasCheckFilmVelocity && (!HasFilmVelocity)) 
潘栩锋's avatar
潘栩锋 committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
                {
                    StopAnyHeating();
                    //停止自控
                    IsAuto = false;
                }
            }
        }


        /// <summary>
        /// 停止任何加热行为
        /// </summary>
        void StopAnyHeating() 
        {
            //停止自控
            IsAuto = false;

            //备份 加热策略
            int[] preheats = mHeatCell.PreHeats.ToArray();
            bool needrestore = true;
            
            //停止检测加热棒
            if (mHeatCheck.Enable) 
            {
                mHeatCheck.Enable = false;
                needrestore = false;
            }
            
            //停止加热
            mHeatCell.ClearPreHeats();
            mHeatCell.HeatApply();

            //恢复 加热策略
            if(needrestore)
                mHeatCell.ModifyPreHeats(preheats);
        }
        void UpdateTotalDelay() 
        {
            if (mBDetect == null)
                return;
            if (mBDetect.FilmVelocity < 10)
                return;
            int totaldelay = (int)(Delay + mBDetect.FilmLength / mBDetect.FilmVelocity * 60);
            int t = (int)(mHeatBuf.Delay.TotalSeconds);
            if (Math.Abs(totaldelay - t) > 5)
            {
                mHeatBuf.Delay = TimeSpan.FromSeconds(totaldelay);
            }
            
        }

648 649
        UInt16[] last_heat;
        UInt16 last_heatupdate;
潘栩锋's avatar
潘栩锋 committed
650 651
        void Output(int[] heats)
        {
652
            List<UInt16> list = new List<UInt16>();
潘栩锋's avatar
潘栩锋 committed
653 654
            for (int i = 0; i < ChannelCnt; i++)
            {
655
                UInt16 heat = (UInt16)heats[i];
潘栩锋's avatar
潘栩锋 committed
656 657 658 659
                if (heat < 0)
                    heat = 0;
                else if (heat > 100)
                    heat = 100;
660
                list.Add(heat);
潘栩锋's avatar
潘栩锋 committed
661
            }
662 663 664
            plc.SetHeat(list);
            last_heat = list.ToArray();

665 666
            UInt16 heatupdate = (UInt16)(plc.HeatUpdate_R + 1);
            if (heatupdate >100)
潘栩锋's avatar
潘栩锋 committed
667
                heatupdate = 1;
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
            plc.HeatUpdate = heatupdate;
            last_heatupdate = heatupdate;
        }
        void RecoverPLC()
        {
            plc.ChannelCnt = (UInt16)ChannelCnt;
            if (last_heat != null)
            {
                plc.SetHeat(last_heat);
                plc.HeatUpdate = last_heatupdate;
            }
            else
            {

                //设置全部0进去
                plc.SetHeat(new UInt16[ChannelCnt]);
                plc.HeatUpdate = plc.HeatUpdate;
            }
潘栩锋's avatar
潘栩锋 committed
686 687
        }

688

潘栩锋's avatar
潘栩锋 committed
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 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 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
        void Add(DateTime time, DateTime endtime, Misc.DIRECTION direction, TimeSpan period, int rotateCnt,
            int orgboltno, double rAngle, double filmLength,
            int[] frame)
        {
            //1.求平均值
            //2.每个螺丝,加热量的增量 = (Thick - Avg) * Factor;
            //3.求出输出量, 输出值 = 当前值 + 增量
            
            if (frame.Count() != NBolts)
                return;

            //检测数据有效性
            int invalid_cnt  = frame.Count(
                (_d) => {return !Misc.MyBase.ISVALIDATA(_d);}
                );

            if (invalid_cnt > NBolts / 2)//有一半数据都是无效的
                return;

            bool need_cal = true;
            //获取对应方向的加热幅
            HeatChanged.ITEM heat = mHeatChanged.Get(time + period - mHeatBuf.Delay);
            if (heat != mHeatChanged.mItem.Last())
            {
                //后面还加了热, 不用自控
                need_cal = false;
            }
            mHeatBuf.IsIntoAutoONo = mHeatCell.IsInAutoONo;
                        
            //添加到缓冲区, 计算分区平移
            if (!mHeatBuf.Add(
                time, endtime, direction, period, rotateCnt,
                orgboltno, rAngle, filmLength,
                frame,
                heat.Time, heat.Heats))
            {
                //数据有问题,不需要在处理
                return;
            }
            if (mHeatBuf.IsIntoAutoONo != mHeatCell.IsInAutoONo)//状态切换了, 只能是变为false
            {
                if (mHeatBuf.IsIntoAutoONo == false)
                {
                    if (mHeatBuf.AutoONoResult == EAutoONoResult.OK)
                    {
                        if (renzijia.OrgBoltNo != mHeatBuf.BestOrgBoltNo)
                        {
                            renzijia.OrgBoltNo = mHeatBuf.BestOrgBoltNo;
                            renzijia.Apply();
                        }
                    }
                    //对位完成
                    mHeatCell.IsAutoONo = false;
                }
            }
            
            FlyData_FeedbackHeat d = mHeatBuf.mData.Last();

            mHeatCell.SetThickPercents(d.Thicks);

            
            if (IsAuto && need_cal)//如果自控中!!! 
            {
                mHeatCell.Auto();
            }
        }

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

        #region 撤销
        private int undoidx = 0;
        /// <summary>
        /// undo 的序号
        /// </summary>
        public int UndoIdx
        {
            get {
                return undoidx;
            }
            protected set {
                if (undoidx != value)
                {
                    undoidx = value;
                    NotifyPropertyChanged("UndoIdx");
                }
            }
        }
        //读取 UndoIdx 的 加热
        public void Undo()
        {
            int idx = UndoIdx - 1;
            if (idx < 0)
                return;
            else if (idx >= mHeatChanged.mItem.Count())
                return;

            mHeatCell.ModifyPreHeats(mHeatChanged.mItem[idx].Heats);
            UndoIdx--;
        }
        #endregion

        #region IFeedbackHeat 成员

        public void Apply() 
        {
            Save();
        }



        private void SaveHeatsLast() 
        {
            string path = @"feedbackheats\最后记录.xml";
            FeedbackHeatsMark mark = new FeedbackHeatsMark();
            mark.heats = mHeatCell.Heats.ToArray();
            mark.Save(path);
        }
        private void LoadHeatsLast()
        {
            string path = @"feedbackheats\最后记录.xml";
            FeedbackHeatsMark mark = FeedbackHeatsMark.Load(path);
            if ((mark != null) && (mark.heats != null))
            {
                mHeatCell.ModifyPreHeats(mark.heats);
            }          
        }
        #region 加热配置读写
        public void SaveHeats(string productname)
        {
            string path = @"feedbackheats\" + productname + ".xml";
            HeatsProductName = productname;
            FeedbackHeatsMark mark = new FeedbackHeatsMark();
            mark.heats = mHeatCell.Heats.ToArray();
            mark.Save(path);
        }
        /// <summary>
        /// 获取保存的加热量文件列表 返回类型为 List&lt;string&gt;
        /// </summary>
        /// <param name="AsyncDelegate"></param>
        /// <param name="AsyncState"></param>
        public void GetHeatsFileList(AsyncCBHandler AsyncDelegate, object AsyncState) 
        {
            DirectoryInfo dinfo = new DirectoryInfo(@"feedbackheats");
            List<string> filenames = new List<string>();
            if (dinfo.Exists)
            {

                foreach (FileInfo info in dinfo.GetFiles())
                {
                    if (Path.GetExtension(info.Name) == ".xml")
                    {
                        filenames.Add(Path.GetFileNameWithoutExtension(info.Name));
                    }
                }
            }
            AsyncDelegate(AsyncState, filenames);
        }

        /// <summary>
        /// 删除 加热量文件
        /// </summary>
        /// <param name="productname"></param>
        public void DelHeatsFile(string productname) 
        {
            string path = @"feedbackheats\" + productname + ".xml";
            if (System.IO.File.Exists(path))
                System.IO.File.Delete(path);        
        }

        /// <summary>
        /// 加载 加热量文件 
        /// </summary>
        /// <param name="filename"></param>
        public void LoadHeatsFile(string productname) 
        {
            string path = @"feedbackheats\" + productname + ".xml";
            HeatsProductName = productname;
            FeedbackHeatsMark mark = FeedbackHeatsMark.Load(path);
            if ((mark != null) && (mark.heats != null))
            {
                mHeatCell.ModifyPreHeats(mark.heats);
            }           
        }
        #endregion
        #endregion





        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        #endregion
        #region ISaveToXml 成员

        public string[] GetSavePropertyNames()
        {
            return new string[]{
                "Step",
                "Delay",
                "HasCheck",
                "HeatsProductName",
906 907 908
                "HasCheckFilmVelocity",
                "IsClient",
                "PLCep"
潘栩锋's avatar
潘栩锋 committed
909 910 911 912 913 914 915
            };
        }

        #endregion
    }

}