ClientBean.cs 18.7 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
using Asa;
using HuichuanLibrary;
using log4net;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace OnlineStore.DeviceLibrary
{
    public partial class ClientBean : DeviceBase
    {


        #region  初始化 
        public bool IsDebug = false;

        public InletEquip inletEquip;
        public OutletEquip outletEquip;
        public RobotEquip robotEquip;
        public Dictionary<int, EquipBase> equipsMap = new Dictionary<int, EquipBase>();

        public Client_Config Config = null;
        private bool canStart = false;
        //public SQLite sQLite = null;
        public ClientBean(Client_Config config)
        {
            equipsMap = new Dictionary<int, EquipBase>();
            if (config.IOSingle_TimerOut <= 0)
            {
                config.IOSingle_TimerOut = 5000;
            }
            Init();
            baseConfig = config;
            this.Config = config;
            this.DeviceID = config.Id;

            Name = (" 机器人运输线" + " ").ToUpper();
            MoveInfo = new DeviceMoveInfo(Name);
            inletEquip = new InletEquip(config, 1);
            outletEquip = new OutletEquip(config, 2);
            robotEquip = new RobotEquip(config, 3);
            List<string> ioList = new List<string>();
            ioList = new List<string>(DeviceConfig.ProIOIpMap.Values);
            equipsMap.Add(1, inletEquip);
            equipsMap.Add(2, outletEquip);
            equipsMap.Add(3, robotEquip);
            //先初始化设备
            // RFIDManager.Open();
            HCBoardManager.InitConfig();
            IOManager.Init();
            AxisManager.Init();
            AxisManager.instance.OpenCard();

            //初始化摄像机配置  
            CodeManager.LoadConfig();

            //AgvClient.NodeList.Add(inputConfig.AgvName_A);
            //AgvClient.NodeList.Add(inputConfig.AgvName_B);

            BufferDataManager.InitData();
            mainTimer.Enabled = true;
            canStart = true;
            AgvClient.Init();
        }

        #endregion

        public string CanStart()
        {
            if (!canStart)
            {
                return "启动失败:设备未初始化完成";
            }
            return "";
        }
        #region 启动和复位

        public override bool StartRun()
        {
            string result = CanStart();
            if (String.IsNullOrEmpty(result).Equals(false))
            {
                SetWarnMsg(result);
                return false;
            }
            else if (IOValue(IO_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
            {
                SetWarnMsg("启动失败:急停未开");
                return false;
            }
            else
            {
                mainTimer.Enabled = false;
                //lineStatus = LineStatus.ResetMove;
                runStatus = RunStatus.HomeMoving;
                StartTime = DateTime.Now;
                LogUtil.info(Name + "开始启动,启动时间:" + StartTime.ToString() + "");

                Thread.Sleep(5);

                mainTimer.Interval = 1000;
                maxSeconds = 10;
                alarmType = AlarmType.None;
                mainTimer.Enabled = false;
                isInSuddenDown = false;
                isNoAirpressure_Check = false;
                ClearWarnMsg();
                MoveInfo.NewMove(MoveType.Reset);
                MoveInfo.NextMoveStep(StepEnum.Wait);

                foreach (EquipBase moveEquip in this.equipsMap.Values)
                {
                    if (runStatus.Equals(RunStatus.Wait))
                    {
                        LogUtil.error(Name + "启动过程中发现 runStatus=Wait,中断启动");
                        return false;
                    }

                    EquipStartRun(moveEquip);
                }
                if (runStatus.Equals(RunStatus.Wait))
                {
                    return false;
                }
                //RFIDManager.Open(new string[] { });
                mainTimer.Enabled = true;
                return true;
            }
        }
        private void EquipStartRun(EquipBase moveEquip)
        {
            if (moveEquip.IsDebug)
            {
                LogUtil.info(moveEquip.Name + "调试状态,暂不启动");
            }
            else
            {
                bool result = moveEquip.StartRun();
                Thread.Sleep(60);
            }
        }


        public override bool Reset()
        {
            bool isNeedAllReset = false;
            if (isInSuddenDown || isNoAirpressure_Check)
            {
                //TrayManager.LineNeedEmptyTrayNum = 0;
                isNeedAllReset = true;
                LogUtil.error(Name + "收到复位信号,急停中或没有气压报警中,强制所有设备复位~");
            }
            else if ((runStatus == RunStatus.HomeMoving || runStatus == RunStatus.Reset) && NoAlarm())
            {
                LogUtil.error(Name + "收到复位信号,已经在复位或原点返回中,且当前无报警,不处理复位");
                return false;
            }

            //停止运动
            MoveInfo.EndMove();

            runStatus = RunStatus.Reset;

            //重置通用处理
            mainTimer.Interval = 1000;
            // maxSeconds = 10;
            alarmType = AlarmType.None;
            mainTimer.Enabled = false;
            isInSuddenDown = false;
            isNoAirpressure_Check = false;
            ClearWarnMsg();
            MoveInfo.NewMove(MoveType.Reset);
            MoveInfo.NextMoveStep(StepEnum.Wait);

            foreach (EquipBase equip in equipsMap.Values)
            {
                if (runStatus.Equals(RunStatus.Wait))
                {
                    LogUtil.error(Name + "复位过程中发现 runStatus=Wait,中断启动");
                    return false;
                }
                EquipReset(equip, isNeedAllReset);
            }
            if (runStatus.Equals(RunStatus.Wait))
            {
                return false;
            }
            mainTimer.Enabled = true;
            return true;
        }
        private void EquipReset(EquipBase equip, bool isNeedAllReset)
        {
            //调试状态不再重置
            if (!equip.IsDebug)
            {
                if (isNeedAllReset || (!equip.NoAlarm()))
                {
                    LogUtil.info(Name + "收到复位信号," + equip.Name + " 需要复位");
                    equip.Reset();
                    Thread.Sleep(60);
                }
                else
                {
                    LogUtil.info(Name + "收到复位信号," + equip.Name + " 正常无报警,不需要复位");
                }
            }
        }


        protected override void ResetProcess()
        {
            if (MoveInfo.IsInWait)
            {
                CheckWait(MoveInfo);
            }
            if (!MoveInfo.IsInWait)
            {
                bool isOk = true;
                string msg = "";
                //判断是否所有的已经返回完成
                TimeSpan span = DateTime.Now - MoveInfo.LastSetpTime;
                foreach (EquipBase moveEquip in this.equipsMap.Values)
                {
                    if (moveEquip.runStatus.Equals(RunStatus.HomeMoving) || moveEquip.runStatus.Equals(RunStatus.Reset) || moveEquip.runStatus.Equals(RunStatus.Wait))
                    {
                        if (moveEquip.NoAlarm())
                        {
                            msg = moveEquip.Name + "复位结束";
                            isOk = false;
                            break;
                        }

                    }
                }

                if (isOk)
                {
                    runStatus = RunStatus.Runing;
                    MoveInfo.EndMove();
                    mainTimer.Interval = 300;
                    maxSeconds = 3;
                    AgvClient.SetCancelState(AgvClient.CurrCancelState);
                    LogUtil.info(Name + "复位完成 [" + FormUtil.GetSpanStr(span) + "]");
                }
                else if (span.TotalSeconds > 120)
                {
                    WarnMsg = Name + "[" + MoveInfo.MoveStep + "][" + msg + "]已等待[" + Math.Round(span.TotalSeconds, 1) + "]秒";
                    LogUtil.error(WarnMsg, MoveInfo.ErrorLogType);
                    Alarm(AlarmType.IoSingleTimeOut);
                }
            }
        }
        #endregion

        private bool busyPro = false;
        private DateTime busyProTime = DateTime.Now;
        private int maxSeconds = 3;
        protected override void mainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                TimeSpan span = DateTime.Now - busyProTime;
                if (busyPro && span.TotalSeconds < maxSeconds)
                {
                    return;
                }
                try
                {
                    busyPro = true;
                    busyProTime = DateTime.Now;
                    switch (runStatus)
                    {
                        case RunStatus.HomeMoving:
                            ResetProcess();
                            break;
                        case RunStatus.Reset:
                            ResetProcess();
                            break;
                        case RunStatus.Runing:
                            if ((isInSuddenDown.Equals(false) && isNoAirpressure_Check.Equals(false)))
                            {
                                //清理异常
                                IOTimeOutProcess();
                            }
                            break;
                        default: break;
                    }

                    inletEquip.AutoStartProcess();
                    outletEquip.AutoStartProcess();
                    robotEquip.AutoStartProcess();
                }
                catch (Exception ex)
                {
                    LogUtil.error(Name + "LineTimerPro 出错:", ex);
                }
                busyPro = false;
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "主定时器出错:" + ex.ToString());
            }
            Thread.Sleep(1);
        }


        #region 停止运动

        internal override void StopMove()
        {
            MoveInfo.EndMove();
            foreach (EquipBase equip in this.equipsMap.Values)
            {
                if (!equip.IsDebug)
                {
                    equip.StopRun();
                }
            }
        }
        public override void StopRun()
        {
            mainTimer.Enabled = false;
            StopMove();
            AgvClient.SetCancelState(false);
            RFIDManager.Close();
            runStatus = RunStatus.Wait;
            TimeSpan span = DateTime.Now - StartTime;
            LogUtil.info(Name + ",停止运行,总运行时间:" + span.ToString());
        }

        #endregion

        #region IOTimeOutProcess     

        private DateTime preIoTimerOutTime = DateTime.Now;
        private void IOTimeOutProcess()
        {
            try
            {
                TimeSpan span = DateTime.Now - preIoTimerOutTime;
                if (span.TotalSeconds > 1 && alarmType.Equals(AlarmType.IoSingleTimeOut))
                {
                    preIoTimerOutTime = DateTime.Now;

                    if (runStatus < RunStatus.Runing || isInSuddenDown || isNoAirpressure_Check)
                    {
                        return;
                    }

                    //若BOX和移栽都没有在等待Io的过程中则此Io超时异常可能已经处理过
                    //     if (MoveInfo.IsInWait.Equals(false) && SW41_MoveInfo.IsInWait.Equals(false) && SW23_MoveInfo.IsInWait.Equals(false))
                    {
                        LogUtil.info(Name + "清理信号超时报警【" + WarnMsg + "】 ");
                        alarmType = AlarmType.None;
                        ClearWarnMsg();
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("IOTimeOutProcess出错:", ex);
            }
        }
        #endregion

        #region    CheckWait  
        private DateTime preRWTime = DateTime.Now;
        private void CheckWait(DeviceMoveInfo checkWaitInfo)
        {
            try
            {
                List<WaitResultInfo> list = checkWaitInfo.WaitList;
                if (list.Count <= 0)
                {
                    checkWaitInfo.EndStepWait();
                    return;
                }

                //当等待超过一分钟时,需要打印提示 
                TimeSpan span = DateTime.Now - checkWaitInfo.LastSetpTime;
                string NotOkMsg = "";
                bool isOk = true;
                if (checkWaitInfo.OneWaitCanEndStep)
                {
                    isOk = false;
                }
                foreach (WaitResultInfo wait in list)
                {
                    if (wait == null || wait.IsEnd)
                    {
                        continue;
                    }
                    NotOkMsg = " [" + wait.ToStr() + "] ";
                    if (wait.WaitType.Equals(WaitEnum.W002_IOValue))
                    {
                        NotOkMsg = " [" + IOManager.GetIO(wait.IoType, DeviceID).DisplayStr + "=" + wait.IoValue + "] ";
                        wait.IsEnd = IOValue(wait.IoType).Equals(wait.IoValue);

                        if (!wait.IsEnd)
                        {
                            TimeSpan rwSpan = DateTime.Now - preRWTime;
                            //一分钟还未检测到
                            if (span.TotalSeconds > StoreManager.Config.IOSingle_TimerOut && NoAlarm())
                            {
                                ConfigIO io = baseConfig.getWaitIO(wait.IoType);
                                WarnMsg = Name + "[" + checkWaitInfo.MoveStep + "]等待" + NotOkMsg + " 超时";
                                Alarm(AlarmType.IoSingleTimeOut);
                                LogUtil.error(WarnMsg, checkWaitInfo.ErrorLogType);
                            }
                            //超过报警时长
                            else if (rwSpan.TotalSeconds > 5 && span.TotalSeconds > 6 && span.TotalSeconds < StoreManager.Config.IOSingle_TimerOut * 2)
                            {
                                preRWTime = DateTime.Now;
                                string msg = checkWaitInfo.Name + "  " + NotOkMsg + "已等待 " + Math.Round(span.TotalSeconds, 1) + "秒,重写DO:";
                                bool isLog = false;
                                foreach (WaitResultInfo ww in list)
                                {
                                    if (ww != null && ww.WaitType.Equals(2) && baseConfig.DOList.ContainsKey(ww.IoType))
                                    {
                                        if (IOManager.DOValue(ww.IoType, baseConfig.Id).Equals(ww.IoValue).Equals(false))
                                        {
                                            isLog = true;
                                            IOMove(ww.IoType, ww.IoValue);
                                            msg += ww.ToStr() + ",";
                                        }
                                    }
                                }
                                if (isLog)
                                {
                                    LogUtil.error(msg);
                                }
                            }
                            if (!checkWaitInfo.OneWaitCanEndStep)
                            {
                                isOk = false;
                                break;
                            }
                        }
                    }
                    else if (wait.WaitType.Equals(WaitEnum.W003_Time))
                    {
                        wait.IsEnd = (span.TotalMilliseconds >= wait.TimeMSeconds);
                    }

                    if (wait.IsEnd)
                    {
                        if (checkWaitInfo.OneWaitCanEndStep)
                        {
                            isOk = true;
                            break;
                        }
                    }
                    else
                    {
                        if (!checkWaitInfo.OneWaitCanEndStep)
                        {
                            isOk = false;
                            break;
                        }
                    }
                }
                if (isOk)
                {
                    checkWaitInfo.EndStepWait();
                }
                else if (span.TotalSeconds > checkWaitInfo.TimeOutSeconds)
                {
                    WarnMsg = checkWaitInfo.Name + "[" + checkWaitInfo.MoveStep + "][" + NotOkMsg + "]已等待[" + Math.Round(span.TotalSeconds, 1) + "]秒";
                    LogUtil.error(WarnMsg, checkWaitInfo.ErrorLogType);
                    Alarm(AlarmType.IoSingleTimeOut);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(checkWaitInfo.Name + " [" + checkWaitInfo.MoveStep + "] CheckWait 出错:", ex);
            }
        }
        #endregion

        #region 忙碌处理


        public override bool StartInstore(InOutParam param)
        {
            return true;

        }
        protected override void InstoreProcess()
        {
        }

        public override bool StartOutstore(InOutParam param)
        {
            return false;
        }

        protected override void OutstoreProcess()
        {
        }
        private void InOutLog(string msg)
        {
            LogUtil.debug(Name + msg);
        }

        #endregion


        public override void Alarm(AlarmType alarmType)
        {
            if (alarmType.Equals(AlarmType.None).Equals(false))
            {
                LastAlarmTime = DateTime.Now;
            }
            if (this.alarmType.Equals(alarmType))
            {
                return;
            }
            if (alarmType.Equals(AlarmType.SuddenStop))
            {
                isInSuddenDown = true;
            }
            else if (alarmType.Equals(AlarmType.NoAirpressure_Check))
            {
                isNoAirpressure_Check = true;
            }
            this.alarmType = alarmType;
            if (alarmType.Equals(AlarmType.SuddenStop) || alarmType.Equals(AlarmType.NoAirpressure_Check) || alarmType.Equals(AlarmType.AxisAlarm))
            {
                StopMove();
            }
        }


    }
}