HeatCheck.cs 17.4 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using FLY.FeedbackRenZiJia.IService;
using FLY.FeedbackRenZiJia.Common;
9
using PropertyChanged;
潘栩锋's avatar
潘栩锋 committed
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

namespace FLY.FeedbackRenZiJia.Server
{
    /// <summary>
    /// 加热棒检测
    /// </summary>
    class HeatCheck:IHeatCheck,INotifyPropertyChanged
    {
        const int HEAT_WAIT = 3;//3s
        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 HalfIdx;
        private List<int> maybeIdx = new List<int>();


        /// <summary>
        /// 坏的加热棒
        /// </summary>
79
        [DoNotCheckEquality]
潘栩锋's avatar
潘栩锋 committed
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
        public bool[] Bads { get; 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 == "Enable")
            {
                if (Enable)
                    Start();
                else
                    Stop();
            }
        }

        void mHeatCell_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "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("Bads");
            OpenCircuit = false;
            PowerOff = false;
            ShortCircuit = false;
        }
        /// <summary>
        /// 停止
        /// </summary>
        void Stop()
        {
            checkMode = CHECK_MODE.IDLE;
            //清空全部加热
            mHeatCell.ClearPreHeats();
            mHeatCell.HeatApply();


            //保存Bads
            Save();
        }
        void Load() 
        {
            string path = "bads.xml";

            FeedbackHeatsMark mark = FeedbackHeatsMark.Load(path);
            if ((mark != null) && (mark.heats != null))
            {
                for (int i = 0; i < Bads.Count() ; i++)
                {
                    if (i < mark.heats.Count())
                        Bads[i] = ((mark.heats[i] == 0) ? false : true);
                    else
                        Bads[i] = false;
                }
                NotifyPropertyChanged("Bads");

                OpenCircuit = Bads.Any(b => b);
            }
        }
        void Save() 
        {
            string path = "bads.xml";
            FeedbackHeatsMark mark = new FeedbackHeatsMark();
            mark.heats = (from b in Bads select (b) ? 1 : 0).ToArray();
            mark.Save(path);
        }


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

        /// <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("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)//刚进入!!!
                        {
                            HalfIdx = -1;//可能坏的加热棒中,还没有一条是好的
                            for (int i = 0; i < Bads.Count(); i++)//登记全部坏的加热棒
                            {
                                if (Bads[i])
                                {
                                    maybeIdx.Add(i);
                                }
                            }
                            CheckNo = maybeIdx.First() + 1;//当前检测的位置,现在没用!!!

                            //肯定有2条加热棒是坏了,不用检测maybeIdx.Count()
                            mHeatCell.ModifyPreHeats(getSingleHeat_100(mHeatCell.ChannelCnt,
                                maybeIdx[0],
                                maybeIdx[1]));

                            mHeatCell.HeatApply();
                            counter = 1;
                        }
                        else if (counter < HEAT_WAIT)//3秒延时
                        {
                            counter++;
                        }
                        else//3秒后
                        {
                            if (!HasElectricity)//没电流,肯定是坏了
                            {
                                if (HalfIdx < 0)//还没有一个 "肯定是好的,只是电流不够的加热棒"
                                {
                                    maybeIdx.RemoveRange(0, 2);//之前测试的两条肯定是坏的,不用再查了(这个有bug的)

                                    if (maybeIdx.Count() <= 1)
                                    {
                                        //只剩下一个,没法检测结束
                                        Enable = false;//停止检测
                                        return;
                                    }
                                }
                                else
                                {
                                    //已经有 好的!!!, 刚才测的那条肯定是坏的
                                    maybeIdx.RemoveAt(0);

                                    if (maybeIdx.Count() == 0)
                                    {
                                        //全部检测完了
                                        Enable = false;//停止检测
                                        return;
                                    }
                                }

                                
                                CheckNo = maybeIdx[0] + 1;

                                int idx1 = (HalfIdx >= 0) ? HalfIdx : maybeIdx[1];

                                mHeatCell.ModifyPreHeats(getSingleHeat_100(mHeatCell.ChannelCnt,
                                    maybeIdx[0],
                                    idx1));

                                mHeatCell.HeatApply();
                                counter = 1;
                            }
                            else
                            {
                                //刚才测量的是好的!!!!!
                                if (HalfIdx < 0)//还没有一个 "肯定是好的,只是电流不够的加热棒"
                                {
                                    Bads[maybeIdx[0]] = false;//清除坏的记录
                                    Bads[maybeIdx[1]] = false;
                                    HalfIdx = maybeIdx[0];//登记其中一条好的加热棒
                                    maybeIdx.RemoveRange(0, 2);
                                }
                                else
                                {
                                    Bads[maybeIdx[0]] = false;//清除坏的记录
                                    maybeIdx.RemoveAt(0);
                                }
                                NotifyPropertyChanged("Bads");

                                OpenCircuit = Bads.Any(b => b);

                                if (maybeIdx.Count() <= 0)
                                {
                                    //一个都不剩,完成
                                    Enable = false;//停止检测
                                    return;
                                }

                                CheckNo = maybeIdx[0] + 1;
                                
                                mHeatCell.ModifyPreHeats(getSingleHeat_100(mHeatCell.ChannelCnt,
                                    maybeIdx[0],
                                    HalfIdx));

                                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)
        {
508
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
潘栩锋's avatar
潘栩锋 committed
509 510 511 512 513 514
        }
        #endregion
    }


}