EquipBase.cs 23.1 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
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public abstract class EquipBase : KTK_Store
    {
        /// <summary>
        /// 当前设备下游的设备ID
        /// </summary>
        public int OutDeviceId = 0;
        /// <summary>
        /// 当前设备上游的设备ID
        /// </summary>
        public int InDeviceId = 0;
        public bool MoveStop = false;
        /// <summary>
        /// 当前容器编码
        /// </summary>
        public string CurContainerId = "";
        /// <summary>
        /// 上一个容器编码
        /// </summary>
        public string PreContainerId = "";
        /// <summary>
        /// 设备信息
        /// </summary>
        public string EquipInfo
        {
            get
            {
                if (this is StationEquip)
                    return $"[TaskInCnt={ContainerManager.GetInTaskCnt(DeviceID)}][TaskOutCnt={ContainerManager.GetOutTaskCnt(DeviceID)}][Next={GetOutEquip()?.Name ?? ""}][{containerArrived}]";
                else
                    return $"[Pre={GetInEquip()?.Name ?? ""}][Next={GetOutEquip()?.Name ?? ""}][{containerArrived}]";
            }
        }//[{GetInEquip()?.Name}]
        public List<string> RFIDIP = new List<string>();
        public LineMoveInfo SecondMoveInfo = null;
        public bool IsDebug = false;
        public ConcurrentQueue<InOutParam> waitOutStoreList = new ConcurrentQueue<InOutParam>();
        public List<InOutParam> waitInStoreList = new List<InOutParam>();
        internal object waitInListLock = "";
        public delegate void TrayProcessEnd(int swNum, int trayNum);

        protected abstract void BaseTimerProcess();

        protected abstract void BoxProcess();
        protected override void mainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            TimerProcess();
        }
        public DateTime LastAlarmTime = DateTime.Now;
        public override void Alarm(LineAlarmType alarmType)
        {
            if (alarmType.Equals(LineAlarmType.None).Equals(false))
            {
                LastAlarmTime = DateTime.Now;
            }
            if (this.alarmType.Equals(alarmType))
            {
                return;
            }
            if (alarmType.Equals(LineAlarmType.SuddenStop))
            {
                isInSuddenDown = true;
            }
            else if (alarmType.Equals(LineAlarmType.NoAirCheck))
            {
                isNoAirCheck = true;
            }
            this.alarmType = alarmType;
            if (alarmType.Equals(LineAlarmType.SuddenStop) || alarmType.Equals(LineAlarmType.NoAirCheck)
                || alarmType.Equals(LineAlarmType.AxisAlarm) || alarmType.Equals(LineAlarm.AxisErrorStop))
            {
                StopMove(alarmType + "报警");
            }
        }
        protected void StartEquip()
        {
            OpenRfid();
        }
        protected void StopEquip()
        {
            WarnMsg = "";
            CloseRfid();
        }
        public void OpenRfid()
        {
            Task.Factory.StartNew(() =>
            {
                foreach (var item in RFIDIP)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        RFIDManager.Open(item);
                    }
                }
            });
        }
        public void CloseRfid()
        {
            foreach (var item in RFIDIP)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    RFIDManager.Close(item);
                }
            }
        }
        protected void ClearTrayRFID()
        {

        }
        /// <summary>
        /// 获取当前设备下游设备信息
        /// </summary>
        /// <returns></returns>
        protected EquipBase GetOutEquip()
        {
            if (OutDeviceId > 0)
            {
                return LineManager.Line.AllEquipMap[OutDeviceId];
            }
            return null;
        }
        /// <summary>
        /// 获取当前设备上游设备信息
        /// </summary>
        /// <returns></returns>
        protected EquipBase GetInEquip()
        {
            if (InDeviceId > 0)
            {
                return LineManager.Line.AllEquipMap[InDeviceId];
            }
            return null;
        }
        public virtual void CheckAlarmProcess(LineMoveInfo moveInfo, LineAlarmType alarmType)
        {

        }
        protected virtual void CloseAllDO()
        {
            List<string> doList = new List<string>(baseConfig.DOList.Keys);
            foreach (string doType in doList)
            {
                if (IOValue(doType).Equals(IO_VALUE.HIGH))
                {
                    IOMove(doType, IO_VALUE.LOW);
                }
            }
        }
        protected int TimerMaxSeconds = 3;
        object timerProLock = new object();
        public void TimerProcess()
        {
            if (Monitor.TryEnter(timerProLock, 300))
            {
                try
                {
                    BaseTimerProcess();
                }
                catch (Exception ex)
                {
                    LogUtil.error(Name + "TimerProcess出错:", ex);
                }
                finally
                {
                    Monitor.Exit(timerProLock);
                }
            }
        }
        protected DateTime preIoTimerOutTime = DateTime.Now;
        protected virtual void IOTimeOutProcess()
        {
            try
            {
                TimeSpan span = DateTime.Now - preIoTimerOutTime;
                if (span.TotalSeconds > 1 && alarmType.Equals(LineAlarmType.IoSingleTimeOut))
                {
                    preIoTimerOutTime = DateTime.Now;

                    if (runStatus < LineRunStatus.Runing || isInSuddenDown || isNoAirCheck)
                    {
                        return;
                    }
                    //若BOX和出料都没有在等待Io的过程中则此Io超时异常可能已经处理过
                    if (MoveInfo.MoveType.Equals(LineMoveType.None) && SecondMoveInfo.MoveType.Equals(LineMoveType.None) && MoveInfo.IsInWait.Equals(false) && SecondMoveInfo.IsInWait.Equals(false))
                    {
                        LogUtil.info(Name + "清理信号超时报警【" + WarnMsg + "】 ");
                        alarmType = LineAlarmType.None;
                        SetWarnMsg("");
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("IOTimeOutProcess出错:", ex);
            }
        }
        protected override void BusyMoveProcess()
        {
            try
            {
                if (MoveStop)
                {
                    return;
                }

                if (MoveInfo.MoveType.Equals(LineMoveType.Box))
                {
                    BoxProcess();
                }
                if (MoveInfo.MoveType.Equals(LineMoveType.InStore) || this.SecondMoveInfo.MoveType.Equals(LineMoveType.InStore))
                {
                    InStoreProcess();
                }
                else if (MoveInfo.MoveType.Equals(LineMoveType.OutStore) || SecondMoveInfo.MoveType.Equals(LineMoveType.OutStore))
                {
                    OutStoreProcess();
                }
                else if (MoveInfo.MoveType.Equals(LineMoveType.RHome))
                {
                    ResetProcess();
                }
                else if (MoveInfo.MoveType.Equals(LineMoveType.Reset))
                {
                    ResetProcess();
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + " BusyMoveProcess 出错:" + ex.ToString());
            }
        }
        //protected void MoveTimeOut(LineMoveInfo move, string msg)
        //{ 
        //    WarnMsg = move.Name + "[" + move.MoveStep + "] "+ msg + " [" +FormUtil.GetSpanStr( move.StepSpan()) + "]";
        //    int logId = DeviceID * 10000 + (int)move.MoveStep;
        //    LogUtil.error(WarnMsg, logId);
        //    Alarm(LineAlarmType.IoSingleTimeOut);
        //}
        //protected void ClearTimeoutAlarm(string msg)
        //{
        //    if (isInSuddenDown || isNoAirCheck)
        //    {
        //        return;
        //    }
        //    if (WarnMsg.Contains(msg) && alarmType.Equals(LineAlarmType.IoSingleTimeOut))
        //    {
        //        LogUtil.info(Name + "清理信号超时报警【" + WarnMsg + "】 ");
        //        alarmType = LineAlarmType.None;
        //        SetWarnMsg("");
        //    }
        //}
        protected bool CanStartRun()
        {
            string canResult = LineManager.Line.CanStart();
            if (String.IsNullOrEmpty(canResult).Equals(false))
            {
                SetWarnMsg(Name + canResult);
                return false;
            }
            return true;
        }
        protected void ResetClearData()
        {
            TimerMaxSeconds = 10;
            SetWarnMsg("");
            alarmType = LineAlarmType.None;
            isInSuddenDown = false;
            isNoAirCheck = false;
            PreContainerId = "";
            CurContainerId = "";
        }
        /// <summary>
        /// 更新分拣台rfid
        /// </summary>
        /// <returns></returns>
        internal RFIDData UpdateStationCode()
        {
            RFIDData ShelfId_Station = new RFIDData();
            try
            {
                if (RFIDIP != null && RFIDIP.Count > 0)
                    //判断料架的编码是否正确
                    ShelfId_Station = RFIDManager.ReadRFID(RFIDIP[1]);
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "UpdateStationCode 出错:", ex);
            }
            return ShelfId_Station;
        }
        internal bool GetStationCode(out string rfid)
        {
            rfid = "";
            RFIDData ShelfId_Station = new RFIDData();
            try
            {
                if (RFIDIP != null && RFIDIP.Count > 0)
                {
                    //判断料架的编码是否正确
                    ShelfId_Station = RFIDManager.ReadRFID(RFIDIP[1]);
                    rfid = ShelfId_Station.StrData;
                    return ShelfId_Station.IsValidated();
                }

            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "UpdateStationCode 出错:", ex);
            }
            return false;
        }
        internal bool UpdateContainerCode()
        {
            RFIDData rfidData = RFIDManager.GetContainerCode(Name, DeviceID, 0);
            if (rfidData != null && rfidData.IsValidated())
            {
                rfidData = RFIDManager.GetContainerCode(Name, DeviceID, 0);
                if (rfidData != null && rfidData.IsValidated())
                {
                    PreContainerId = CurContainerId;
                    CurContainerId = rfidData.ToContainerId();
                    ContainerManager.AddOrUpdateContainerInfoWithPosid(CurContainerId);
                }
            }
            else
            {
                PreContainerId = CurContainerId;
                CurContainerId = "";
            }
            if (RFIDIP == null || RFIDIP.Count == 0) { }
            else
            {
                LogUtil.LOGGER.Info(Name + " IP [" + string.Join(",", RFIDManager.GetRFIP(DeviceID).ToArray()) + "] 当前容器 [" + CurContainerId + "]" + $"[{rfidData.IsValidated()}]" + " 上个容器 [" + PreContainerId + "] ");
            }

            return true;
        }

        #region CheckWait处理

        private DateTime preCheckAxisTime = DateTime.Now;
        protected DateTime preRWTime = DateTime.Now;
        internal void CheckWait(LineMoveInfo moveInfo)
        {
            try
            {
                List<WaitResultInfo> list = moveInfo.WaitList;
                if (list.Count <= 0)
                {
                    moveInfo.EndStepWait();
                    return;
                }

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

                        if (!wait.IsEnd)
                        {
                            //if (wait.IoType.Equals(IO_Type.SL_Line_Run))
                            //{
                            //    IOMove(wait.IoType, wait.IoValue);
                            //    LogInfo(" [" + moveInfo.MoveType + "][" + moveInfo.MoveStep + "]重写DO: " + wait.ToStr());
                            //}
                            TimeSpan rwSpan = DateTime.Now - preRWTime;
                            //一分钟还未检测到
                            if (span.TotalSeconds > LineManager.Config.IOSingle_TimerOut && alarmType <= LineAlarmType.IoSingleTimeOut)
                            {
                                ConfigIO io = baseConfig.getWaitIO(wait.IoType);
                                string warnmsg = moveInfo.Name + "[" + moveInfo.MoveType + "][" + moveInfo.MoveStep + "] 等待" + NotOkMsg + " 超时 " + Math.Round(span.TotalSeconds, 1) + "秒";
                                SetWarnMsg(warnmsg, moveInfo.GetStepDes() + "_超时报警", moveInfo);
                                LogUtil.error(WarnMsg, DeviceID * 1000 + 13);
                                if (NoAlarm())
                                {
                                    Alarm(LineAlarmType.IoSingleTimeOut);
                                    CheckAlarmProcess(moveInfo, LineAlarmType.IoSingleTimeOut);
                                }
                            }
                            if (rwSpan.TotalSeconds > 5 && span.TotalSeconds > 6 && span.TotalSeconds < LineManager.Config.IOSingle_TimerOut * 2)
                            {
                                preRWTime = DateTime.Now;
                                string msg = moveInfo.Name + " [" + moveInfo.MoveType + "][" + moveInfo.MoveStep + "] " + 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 (!moveInfo.OneWaitCanEndStep)
                            {
                                isOk = false;
                                break;
                            }
                        }
                    }
                    else if (wait.WaitType.Equals(WaitEnum.W003_Time))
                    {
                        wait.IsEnd = (span.TotalMilliseconds >= wait.TimeMSeconds);
                    }
                    else
                    {
                        wait.IsEnd = CheckWaitResult(moveInfo, wait);
                    }
                    //else if (wait.WaitType.Equals(10))
                    //{
                    //    wait.IsEnd = LineManager.Line.SideWay34HasTray().Equals(false);
                    //}
                    if (wait.IsEnd)
                    {
                        if (moveInfo.OneWaitCanEndStep)
                        {
                            isOk = true;
                            break;
                        }
                    }
                    else
                    {
                        if (!moveInfo.OneWaitCanEndStep)
                        {
                            isOk = false;
                            break;
                        }
                    }
                }
                if (isOk)
                {
                    moveInfo.EndStepWait();
                    ClearStepAlarm(moveInfo.GetStepDes());
                }
                else if (span.TotalSeconds > moveInfo.TimeOutSeconds)
                {
                    int second = 10;
                    //if (IsLowAlarm(moveInfo))
                    //{
                    //    second = 30;
                    //}
                    second = (int)(span.TotalSeconds / moveInfo.TimeOutSeconds) * 10;
                    if (second > 120)
                    {
                        second = 120;
                    }
                    else if (second < 10)
                    {
                        second = 10;
                    }
                    string msg = moveInfo.Name + "[" + moveInfo.MoveType + "][" + moveInfo.MoveStep + "]等待" + NotOkMsg
                      + "超时[" + FormUtil.GetSpanStr(span) + "]";

                    string type = moveInfo.GetStepDes() + "_" + "超时报警";
                    //判断是等待抓料直接返回
                    List<WaitResultInfo> noTrayAlarm = (from m in list where m.WaitType.Equals(WaitEnum.W1000_NoTrayAlarm) select m).ToList();
                    if (noTrayAlarm.Count > 0)
                    {
                        msg = moveInfo.Name + "[" + moveInfo.MoveType + "][" + moveInfo.MoveStep + "]未抓起料盘,请取走料盘后复位[" + FormUtil.GetSpanStr(span) + "]";
                        type = MoveInfo.GetStepDes() + "_" + "未抓起料";
                    }

                    //LogUtil.error(WarnMsg, DeviceID * 1000 + 15, second);
                    SetWarnMsg(msg, type, moveInfo, second);
                    Alarm(LineAlarmType.IoSingleTimeOut);
                    CheckAlarmProcess(moveInfo, LineAlarmType.IoSingleTimeOut);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(moveInfo.Name + " [" + moveInfo.MoveStep + "] CheckWait 出错:", ex);
            }
        }
        //private bool IsLowAlarm(LineMoveInfo moveInfo)
        //{
        //    //托盘卡的信号不报警,只提示
        //    //  [_进仓_14-SMove] [CheckFixture] [MO_17_Stop2Check] 等待[X462 - 进仓14阻挡2托盘检测 - StopCylinder_Check2 = LOW] 超时[181.7]秒
        //    if (moveInfo.MoveType.Equals(LineMoveType.Box) &&
        //        (moveInfo.IsStep(LineMoveStep.MIO_00_Stop1Down) ||
        //        moveInfo.IsStep(LineMoveStep.MIO_02_TrayCheck) ||
        //          moveInfo.IsStep(LineMoveStep.MO_17_Stop2Check)))
        //    {
        //        return true;
        //    }
        //    return false;
        //}
        protected virtual bool CheckWaitResult(LineMoveInfo moveInfo, WaitResultInfo wait)
        {
            return false;
        }

        #endregion

        public virtual string GetMoveStr()
        {
            string msg = "";
            if (!(RFIDIP == null || RFIDIP.Count == 0))
            {
                msg += "当前容器:" + CurContainerId + "  上个容器:" + PreContainerId + "\n";
            }
            msg += "runS:  " + runStatus + "\n";
            msg += "alarm:  " + alarmType + " " + LastAlarmTime.ToLongTimeString() + "\n";
            msg += "Move:" + MoveInfo.MoveType + "  " + MoveInfo.SLog + "\n";
            msg += "SMove:" + SecondMoveInfo.MoveType + "  " + SecondMoveInfo.MoveStep + "\n";
            msg += "MoveMsg:" + LogMsg + "\n";
            return msg;
        }
        internal void MoveEndS()
        {
            runStatus = LineRunStatus.Runing;
            MoveInfo.EndMove();
        }
        public string GetColorDes()
        {
            return
                  "橘色:复位中\r\n" +
                "蓝色:正常待机中\r\n" +
                  "绿色:忙碌中\r\n" +
                "珊瑚色:信号超时报警\r\n" +
                "红色:严重报警\r\n";
        }
        public Color GetShowColor()
        {
            if (runStatus.Equals(LineRunStatus.Wait))
            {
                if (IsDebug)
                {
                    return Color.LightGray;
                }
                else
                {
                    return Color.White;
                }
            }
            else if (alarmType.Equals(LineAlarmType.IoSingleTimeOut))
            {
                return Color.LightCoral;
            }
            else if (alarmType.Equals(LineAlarmType.None).Equals(false))
            {
                return Color.Red;
            }
            else if (runStatus.Equals(LineRunStatus.HomeMoving) || runStatus.Equals(LineRunStatus.Reset))
            {
                return Color.Orange;
            }
            else if (runStatus.Equals(LineRunStatus.Busy))
            {
                return Color.LimeGreen;
            }
            else if (runStatus.Equals(LineRunStatus.Runing))
            {
                return Color.LightBlue;
            }
            return Color.White;
        }
        /// <summary>
        /// 上游容器到达标志
        /// </summary>
        protected bool containerArrived = false;
        /// <summary>
        /// 等待下游空闲计时器
        /// </summary>
        public Stopwatch waitNextIdleWatch = new Stopwatch();
        public virtual bool BoxCheck() { return false; }
        public virtual bool IsContainerArrived() { return containerArrived; }
        /// <summary>
        /// 重置下游容器到达标志,由上有客户端操作
        /// </summary>
        public virtual void ResetContarinerArrivedState()
        {
            containerArrived = false;
        }
        /// <summary>
        /// 设备是否空闲
        /// </summary>
        /// <param name="type">空闲类型</param>
        /// <returns></returns>
        public virtual bool IsIdle(int type = 0) { return true; }
        /// <summary>
        /// 开始移动任务
        /// </summary>
        /// <param name="type">移动类型编号</param>
        public virtual void StartMove(int type = 0) { }
    }
}