HeatCheck.cs 21.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 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 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
using FLY.FilmCasting.AutoDie.IService;
using Newtonsoft.Json;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;

namespace FLY.FilmCasting.AutoDie.Server
{
    /// <summary>
    /// 加热棒检测
    /// </summary>
    class HeatCheck : IHeatCheck, INotifyPropertyChanged
    {
        const int HEAT_WAIT = 3;//3s
        const int HeatsOfGroup = 12;//1块加热板 12路
        HeatCell mHeatCell;

        enum CHECK_MODE
        {
            /// <summary>
            /// 什么都不干
            /// </summary>
            IDLE,
            /// <summary>
            /// 当前0输出,检测电流是否为0
            /// </summary>
            CHECK_ZERO,
            /// <summary>
            /// 当前有输出, 电流不应该为0
            /// </summary>
            CHECK_NOZERO,
            /// <summary>
            /// 检测每根加热棒
            /// </summary>
            CHECK_EVERYONE
        }
        CHECK_MODE checkMode = CHECK_MODE.IDLE;
        int counter = 0;
        #region IHeatCheck

        /// <summary>
        /// 有这个功能
        /// </summary>
        public bool Has { get; set; }

        /// <summary>
        /// 动作使能
        /// </summary>
        public bool Enable { get; set; }

        /// <summary>
        /// 有电流
        /// </summary>
        public bool HasElectricity { get; set; }
        /// <summary>
        /// 当前正在检测的加热棒号, 1~88
        /// </summary>
        public int CheckNo { get; set; }

        /// <summary>
        /// 当前检测的阶段
        /// </summary>
        public int CheckStep { get; set; }

        #region 根据电流判断的加热棒状态
        /// <summary>
        /// 加热棒是好的,但电流小,必须两条同时加热才可以;
        /// 加热棒分组
        /// </summary>
        private int groupIdx;

        private List<GroupBad> groupBads = new List<GroupBad>();
        class GroupBad
        {
            /// <summary>
            /// 分组号
            /// </summary>
            public int group;
            /// <summary>
            /// 在 好的 maybe序号
            /// </summary>
            public int goodIdx = -1;
            /// <summary>
            /// 全部待测的加热棒序号
            /// </summary>
            List<int> maybe = new List<int>();

            /// <summary>
            /// 已经测试过的 maybe序号
            /// </summary>
            List<int> hasCheck = new List<int>();

            /// <summary>
            /// 获取下一个 需要检查的 序号
            /// </summary>
            /// <returns></returns>
            public bool GetNextIdx()
            {
                for (int i = 0; i < maybe.Count(); i++)
                {
                    if (!hasCheck.Contains(i))
                    {
                        //这个没检查过,可以
                        checkIdx1 = i;
                        return true;
                    }
                }
                return false;
            }

            /// <summary>
            /// 同时加热的加热棒0
            /// </summary>
            int checkIdx0;
            /// <summary>
            /// 同时加热的加热棒1
            /// </summary>
            int checkIdx1;

            public int NextCheckIdx
            {
                get { return checkIdx1 + 1; }
            }
            public int HeatIdx0
            {
                get { return maybe[checkIdx0]; }
            }
            public int HeatIdx1
            {
                get { return maybe[checkIdx1]; }
            }

            public bool GetCheckIdx(int startIdx)
            {
                if (startIdx >= maybe.Count() - 1)
                    return false;//不够数


                //同一个组可以一起加热测试
                checkIdx0 = startIdx;
                checkIdx1 = startIdx + 1;
                return true;
            }

            public void PushToHasCheck()
            {
                if (!hasCheck.Contains(checkIdx0))
                    hasCheck.Add(checkIdx0);
                if (!hasCheck.Contains(checkIdx1))
                    hasCheck.Add(checkIdx1);
            }
            public void MarkGoodIdx()
            {
                goodIdx = checkIdx0;
            }
            public bool HasGood
            {
                get
                {

                    return goodIdx != -1;
                }
            }
            public void Add(int heatIdx)
            {
                maybe.Add(heatIdx);
            }
        }
        /// <summary>
        /// 坏的加热棒
        /// </summary>
        public bool[] Bads { get; private set; }


        /// <summary>
        /// 没加热,也有电流, 电路板短路了
        /// </summary>
        public bool ShortCircuit { get; set; }

        /// <summary>
        /// 没有上电, 或者电流计坏了
        /// </summary>
        public bool PowerOff { get; set; }

        /// <summary>
        /// 有加热棒断路了
        /// </summary>
        public bool OpenCircuit { get; set; }
        #endregion

        #endregion

        public void Init(HeatCell heatCell)
        {
            mHeatCell = heatCell;
            FObjBase.PollModule.Current.Poll_Config(FObjBase.PollModule.POLL_CONFIG.ADD, OnPoll, TimeSpan.FromSeconds(1));

            Init();
            Load();

            mHeatCell.PropertyChanged += new PropertyChangedEventHandler(mHeatCell_PropertyChanged);

            this.PropertyChanged += HeatCheck_PropertyChanged;
        }

        private void HeatCheck_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Enable))
            {
                if (Enable)
                    Start();
                else
                    Stop();
            }
        }

        void mHeatCell_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(mHeatCell.ChannelCnt))
            {
                Init();
                Load();
            }
        }
        void Init()
        {
            Bads = new bool[mHeatCell.ChannelCnt];
            OpenCircuit = false;
            PowerOff = false;
            ShortCircuit = false;
        }
        /// <summary>
        /// 启动
        /// </summary>
        void Start()
        {
            checkMode = CHECK_MODE.CHECK_EVERYONE;
            CheckStep = 0;
            counter = 0;
            Array.Clear(Bads, 0, Bads.Count());
            NotifyPropertyChanged(nameof(Bads));
            OpenCircuit = false;
            PowerOff = false;
            ShortCircuit = false;
        }
        /// <summary>
        /// 停止
        /// </summary>
        void Stop()
        {
            checkMode = CHECK_MODE.IDLE;
            //清空全部加热
            mHeatCell.ClearPreHeats();
            mHeatCell.HeatApply();


            //保存Bads
            Save();
        }
        void Load()
        {
            if (HeatCheckJsonDb.Load(this, "heatCheck.json")) 
            {
                NotifyPropertyChanged(nameof(Bads));
                OpenCircuit = Bads.Any(b => b);
            }
        }
        void Save()
        {
            HeatCheckJsonDb.Save(this, "heatCheck.json");
        }


        void OnPoll()
        {
            switch (checkMode)
            {
                case CHECK_MODE.CHECK_EVERYONE:
                    OnPoll_Check();
                    break;
                default:
                    OnPoll_Normal();
                    break;
            }
        }
        /// <summary>
        /// 平常,测有没电流
        /// </summary>
        void OnPoll_Normal()
        {
            if (!Has)
                return;

            if (mHeatCell.Heats.All(h => h == 0))//没有加热,不应该有电流 
            {
                if (checkMode != CHECK_MODE.CHECK_ZERO) //第1次
                {
                    checkMode = CHECK_MODE.CHECK_ZERO;
                    counter = 0;
                }
                //先等待3秒,3秒后再判断
                counter++;
                if (counter > 5)
                {
                    counter = 0;
                    if (HasElectricity)//有电流,有问题 
                    {
                        ShortCircuit = true;
                    }
                    else
                    {
                        ShortCircuit = false;
                    }
                }
            }
            else
            {
                if (checkMode != CHECK_MODE.CHECK_NOZERO) //第1次
                {
                    checkMode = CHECK_MODE.CHECK_NOZERO;
                    counter = 0;
                }
                if (HasElectricity)
                {
                    counter = 0;
                    PowerOff = false;
                }
                else
                {
                    counter++;
                    if (counter > 7)//连续7秒,都没有一秒有电流,没有上电
                    {
                        PowerOff = true;
                        counter = 0;
                    }
                }
            }
        }

        bool GetCheckIdx_2_loop(int grougIdx_start, int startIdx)
        {
            for (int i = grougIdx_start; i < groupBads.Count(); i++)
            {
                if (groupBads[i].GetCheckIdx(startIdx))
                {
                    groupIdx = i;
                    return true;
                }
                startIdx = 0;
            }
            return false;
        }




        /// <summary>
        /// 加热棒检测周期
        /// </summary>
        void OnPoll_Check()
        {
            switch (CheckStep)
            {
                case 0://刚按了开始,清空,检查应该没电流才对
                    {
                        if (counter == 0)
                        {
                            mHeatCell.ClearPreHeats();
                            mHeatCell.HeatApply();
                            counter++;
                        }
                        else if (counter < HEAT_WAIT)
                        {
                            counter++;
                        }
                        else//3秒后
                        {
                            if (HasElectricity)
                            {
                                ShortCircuit = true;//没输出,也有电流,板烧了
                                Enable = false;//停止检测
                                return;
                            }
                            CheckStep++;
                            counter = 0;
                        }
                    }
                    break;
                case 1://全部加热30%
                    {
                        if (counter == 0)
                        {
                            mHeatCell.ModifyPreHeats(getAllHeat_30(mHeatCell.ChannelCnt));
                            mHeatCell.HeatApply();
                            counter++;
                        }
                        else if (counter < HEAT_WAIT)
                        {
                            if (HasElectricity)
                            {
                                //有上电!!!
                                //正式开始
                                CheckStep++;
                                counter = 0;
                                break;
                            }
                            counter++;
                        }
                        else//3秒内,都没1秒有电,不正常,应该是没上电
                        {
                            PowerOff = true;
                            Enable = false;//停止检测
                            return;
                        }
                    }
                    break;
                case 2://正式开始
                    {
                        if (counter == 0)
                        {
                            CheckNo = 1;
                            mHeatCell.ModifyPreHeats(getSingleHeat_100(mHeatCell.ChannelCnt, CheckNo - 1));
                            mHeatCell.HeatApply();
                            counter = 1;
                        }
                        else if (counter < HEAT_WAIT)
                        {
                            counter++;
                        }
                        else
                        {
                            //3秒后
                            if (!HasElectricity)//这个位没电流,可能是坏了,还需要2次检查
                            {
                                Bads[CheckNo - 1] = true;

                                NotifyPropertyChanged(nameof(Bads));

                                OpenCircuit = true;
                            }
                            if (CheckNo < mHeatCell.ChannelCnt)
                            {
                                CheckNo++;
                                mHeatCell.ModifyPreHeats(getSingleHeat_100(mHeatCell.ChannelCnt, CheckNo - 1));
                                mHeatCell.HeatApply();
                                counter = 1;
                            }
                            else
                            {
                                //检查是否2次检测
                                int cnt = Bads.Count((b) => { return b; });
                                if (cnt < 2)
                                {
                                    //没有坏,或只有一条加热棒坏了,没法2次检测
                                    //完成
                                    Enable = false;//停止检测
                                    return;
                                }
                                else
                                {
                                    //需要2次检测
                                    CheckStep++;
                                    counter = 0;
                                }
                            }
                        }
                    }
                    break;
                case 3://2次检测
                    {
                        if (counter == 0)//刚进入!!!
                        {
                            groupIdx = -1;
                            groupBads.Clear();

                            for (int i = 0; i < Bads.Count(); i++)//分组登记全部坏的加热棒
                            {
                                int group = i / HeatsOfGroup;
                                if (Bads[i])
                                {
                                    if (groupBads.Count() == 0)
                                        groupBads.Add(new GroupBad() { group = group });
                                    else
                                    {
                                        if (groupBads.Last().group != group)
                                        {
                                            groupBads.Add(new GroupBad() { group = group });
                                        }
                                    }
                                    groupBads.Last().Add(i);
                                }
                            }


                            if (!GetCheckIdx_2_loop(0, 0))
                            {
                                //没有两条同一组,测试结束
                                Enable = false;//停止检测
                                return;
                            }
                            var groupBad = groupBads[groupIdx];
                            CheckNo = groupBad.HeatIdx1 + 1;//当前检测的位置,现在没用!!!
                            mHeatCell.ModifyPreHeats(getSingleHeat_100(mHeatCell.ChannelCnt,
                                groupBad.HeatIdx0,
                                groupBad.HeatIdx1));

                            mHeatCell.HeatApply();
                            counter = 1;
                        }
                        else if (counter < HEAT_WAIT)//3秒延时
                        {
                            counter++;
                        }
                        else//3秒后
                        {
                            var groupBad = groupBads[groupIdx];
                            if (!HasElectricity)//没电流,肯定是坏了
                            {
                                if (!groupBad.HasGood)//还没找到好的加热棒
                                {
                                    //继续
                                    if (!GetCheckIdx_2_loop(groupIdx, groupBad.NextCheckIdx))
                                    {
                                        //没有两条同一组,测试结束
                                        Enable = false;//停止检测
                                        return;
                                    }
                                }
                                else
                                {
                                    //checkIdx1是坏的
                                    groupBad.PushToHasCheck();
                                    //获取下一个需要测试的
                                    if (!groupBad.GetNextIdx())
                                    {
                                        //这个分组已经查完,下一个分组
                                        groupIdx++;
                                        if (!GetCheckIdx_2_loop(groupIdx, 0))
                                        {
                                            //没有两条同一组,测试结束
                                            Enable = false;//停止检测
                                            return;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                //刚才测量的是好的!!!!!只是电流不够的加热棒
                                groupBad.MarkGoodIdx(); //找到好的加热棒了,这个组的加热棒全部测试一次
                                groupBad.PushToHasCheck();
                                Bads[groupBad.HeatIdx0] = false;//清除坏的记录
                                Bads[groupBad.HeatIdx1] = false;
                                NotifyPropertyChanged(nameof(Bads));
                                OpenCircuit = Bads.Any(b => b);

                                //获取下一个需要测试的
                                if (!groupBad.GetNextIdx())
                                {
                                    //这个分组已经查完,下一个分组
                                    groupIdx++;
                                    if (!GetCheckIdx_2_loop(groupIdx, 0))
                                    {
                                        //没有两条同一组,测试结束
                                        Enable = false;//停止检测
                                        return;
                                    }
                                }
                            }

                            groupBad = groupBads[groupIdx];
                            CheckNo = groupBad.HeatIdx1 + 1;
                            mHeatCell.ModifyPreHeats(getSingleHeat_100(mHeatCell.ChannelCnt,
                                groupBad.HeatIdx0,
                                groupBad.HeatIdx1));

                            mHeatCell.HeatApply();
                            counter = 1;
                        }
                    }
                    break;
            }

        }

        int[] getSingleHeat_100(int channelcnt, params int[] index)
        {
            int[] heats = new int[channelcnt];
            Array.Clear(heats, 0, heats.Count());
            foreach (int i in index)
                heats[i] = 100;
            return heats;
        }
        int[] getSingleHeat_100(int channelcnt, int index)
        {
            int[] heats = new int[channelcnt];
            Array.Clear(heats, 0, heats.Count());
            heats[index] = 100;
            return heats;
        }
        int[] getAllHeat_30(int channelcnt)
        {
            int[] heats = new int[channelcnt];
            for (int i = 0; i < heats.Count(); i++)
            {
                heats[i] = 30;
            }
            return heats;
        }
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
        }
        #endregion
    }

    class HeatCheckJsonDb
    {
        public bool[] Bads;
        public static bool Load(HeatCheck src, string filePath)
        {
            if (!File.Exists(filePath))
                return false;
            try
            {
                string json = File.ReadAllText(filePath);
                var jsonDb = JsonConvert.DeserializeObject<HeatCheckJsonDb>(json);
                if (jsonDb.Bads != null && jsonDb.Bads.Count()== src.Bads.Count())
                    Array.Copy(jsonDb.Bads, src.Bads, src.Bads.Count());
                return true;
            }
            catch
            {
                //异常,没有json 解码失败
                return true;
            }
        }

        public static bool Save(HeatCheck src, string filePath)
        {
            try
            {
                HeatCheckJsonDb jsonDb = new HeatCheckJsonDb()
                {
                    Bads = src.Bads
                };
                File.WriteAllText(filePath, JsonConvert.SerializeObject(jsonDb, Formatting.Indented));
                return true;
            }
            catch
            {
                //异常,没有json 编码失败

            }
            return false;
        }
    }
}