From beb0f2c826515c0a2abc9ee6eb296dd5d7199b3a Mon Sep 17 00:00:00 2001 From: xcd <834800634@qq.com> Date: 星期一, 06 七月 2020 13:24:44 +0800 Subject: [PATCH] 机台暂停/继续操作 --- src/Bro.M071.Process/M071Process_MotionCard.cs | 478 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 437 insertions(+), 41 deletions(-) diff --git a/src/Bro.M071.Process/M071Process_MotionCard.cs b/src/Bro.M071.Process/M071Process_MotionCard.cs index 98ce638..29b2055 100644 --- a/src/Bro.M071.Process/M071Process_MotionCard.cs +++ b/src/Bro.M071.Process/M071Process_MotionCard.cs @@ -19,6 +19,7 @@ public Timer ResetTimer = null; const int FULLRESETTIME = 5; + object machineStateLock = new object(); MachineState machineState = MachineState.Unknown; public MachineState MachineState { @@ -30,18 +31,157 @@ switch (machineState) { case MachineState.Ready: + lock (machineStateLock) + { + SwitchBeep(false); + SwitchLightRed(false); + SwitchLightYellow(false); + } + + Task.Run(() => + { + lock (machineStateLock) + { + while (MachineState == MachineState.Ready) + { + SwitchLightGreen(true); + Thread.Sleep(1000); + SwitchLightGreen(false); + Thread.Sleep(1000); + } + } + }); + break; case MachineState.Running: + lock (machineStateLock) + { + SwitchBeep(false); + SwitchLightRed(false); + SwitchLightYellow(false); + SwitchLightGreen(true); + } break; case MachineState.Alarm: + lock (machineStateLock) + { + SwitchBeep(true); + SwitchLightRed(true); + SwitchLightYellow(false); + SwitchLightGreen(false); + } break; case MachineState.Pause: + lock (machineStateLock) + { + SwitchBeep(false); + SwitchLightRed(false); + } + Task.Run(() => + { + lock (machineStateLock) + { + while (MachineState == MachineState.Pause) + { + SwitchLightYellow(true); + SwitchLightGreen(true); + Thread.Sleep(1000); + SwitchLightYellow(false); + SwitchLightGreen(false); + Thread.Sleep(1000); + } + } + }); + break; + case MachineState.Resetting: + lock (machineStateLock) + { + SwitchBeep(false); + SwitchLightRed(false); + SwitchLightGreen(false); + } + Task.Run(() => + { + lock (machineStateLock) + { + while (MachineState == MachineState.Resetting) + { + SwitchLightYellow(true); + Thread.Sleep(1000); + SwitchLightYellow(false); + Thread.Sleep(1000); + } + } + }); break; default: break; } OnMachineStateChanged?.Invoke(machineState); + } + } + + private void MotionCardSettingCheck() + { + IDevice device = DeviceCollection.FirstOrDefault(u => u is IMotionCard); + if (device.InitialConfig is MotionCardInitialConfigBase iConfig) + { + outputCtrlCard = device as MotionCardBase; + + var redDefinition = iConfig.IODefinitionCollection.FirstOrDefault(u => u.IOPreStatement == IOPrestatement.Light_Red); + if (redDefinition != null) + { + index_RedLight = redDefinition.IONum; + } + else + { + LogAsync(DateTime.Now, $"{iConfig.Name}鏈厤缃孩鑹茬伅杈撳嚭绱㈠紩", ""); + } + + var greenDefinition = iConfig.IODefinitionCollection.FirstOrDefault(u => u.IOPreStatement == IOPrestatement.Light_Green); + if (greenDefinition != null) + { + index_GreenLight = greenDefinition.IONum; + } + else + { + LogAsync(DateTime.Now, $"{iConfig.Name}鏈厤缃豢鑹茬伅杈撳嚭绱㈠紩", ""); + } + + var yellowDefinition = iConfig.IODefinitionCollection.FirstOrDefault(u => u.IOPreStatement == IOPrestatement.Light_Yellow); + if (yellowDefinition != null) + { + index_YellowLight = yellowDefinition.IONum; + } + else + { + LogAsync(DateTime.Now, $"{iConfig.Name}鏈厤缃粍鑹茬伅杈撳嚭绱㈠紩", ""); + } + + var beepDefinition = iConfig.IODefinitionCollection.FirstOrDefault(u => u.IOPreStatement == IOPrestatement.Beep); + if (beepDefinition != null) + { + index_Beep = beepDefinition.IONum; + } + else + { + LogAsync(DateTime.Now, $"{iConfig.Name}鏈厤缃渹楦e櫒杈撳嚭绱㈠紩", ""); + } + + var lightDefinition = iConfig.IODefinitionCollection.FirstOrDefault(u => u.IOPreStatement == IOPrestatement.Light); + if (lightDefinition != null) + { + index_NormalLight = lightDefinition.IONum; + } + else + { + LogAsync(DateTime.Now, $"{iConfig.Name}鏈厤缃棩鍏夌伅杈撳嚭绱㈠紩", ""); + } + } + else + { + LogAsync(DateTime.Now, "鏈厤缃澘鍗¤澶�", ""); } } @@ -66,7 +206,14 @@ // } //} + if (IsSafetyBeamTrigged || IsSafetyDoorTrigged) + { + LogAsync(DateTime.Now, $"{(IsSafetyDoorTrigged ? "瀹夊叏闂�" : "")}{(IsSafetyBeamTrigged ? " 瀹夊叏鍏夌嚎" : "")}瑙﹀彂涓紝澶嶄綅澶辫触", ""); + return new ProcessResponse(false); + } + MotionCardDefaultRun("Reset", ref opConfig, ref invokeDevice); + //(invokeDevice as IMotionCard).Reset(); RaisedAlarm(""); MachineState = MachineState.Ready; @@ -94,7 +241,14 @@ private void FullReset(object state) { - FullReset(null, null, null); + try + { + FullReset(null, null, null); + } + catch (Exception ex) + { + ExceptionRaisedInMonitor(ex); + } } [ProcessMethod("MotionCardOperationConfigCollection", "FullReset", "澶у浣嶆搷浣�", InvokeType.TestInvoke)] @@ -122,7 +276,7 @@ //{ // motionCard.Run(opConfig); //} - + MachineState = MachineState.Resetting; MotionCardDefaultRun("FullReset", ref opConfig, ref invokeDevice); productionList.ForEach(u => u.Dispose()); @@ -131,6 +285,8 @@ OnFullResetDone?.Invoke(); LogAsync(DateTime.Now, "澶у浣嶅姩浣滃畬鎴�", ""); + MachineState = MachineState.Ready; + return new ProcessResponse(true); } @@ -144,69 +300,255 @@ WaitHandle = new ManualResetEvent(true), WaitResult = true, }; - + MachineState _machineStateBeforePause = MachineState.Unknown; + List<MachineState> _statesAllowPause = new List<MachineState>() { MachineState.Running, MachineState.Ready, MachineState.Pause }; [ProcessMethod("", "PauseJob", "鏆傚仠娴佺▼", InvokeType.TestInvoke)] - public ProcessResponse PauseJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + public ProcessResponse SwitchJobStatus(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) { - if (!_pauseHandle.WaitResult) - { - #region 鏉垮崱鏆傚仠鍔ㄤ綔 - #endregion + if (!_statesAllowPause.Contains(MachineState)) + return new ProcessResponse(-999); - _pauseHandle.WaitHandle.Reset(); + MotionCardBase motionDevice = sourceDevice as MotionCardBase; + if (motionDevice == null) + { + motionDevice = DeviceCollection.FirstOrDefault(u => u is MotionCardBase) as MotionCardBase; } - _pauseHandle.WaitResult = !_pauseHandle.WaitResult; + if (motionDevice == null) + throw new ProcessException("鏈幏鍙栨澘鍗¤澶�"); + + bool? isToPause = null; //true 鏆傚仠 false 缁х画 + if (opConfig.InputPara != null && opConfig.InputPara.Count > 0) + { + isToPause = opConfig.InputPara[0] == 1; + } + + if (isToPause == null) + { + if (!_pauseHandle.WaitResult) + { + #region 鏉垮崱鏆傚仠鍔ㄤ綔 + motionDevice.StateChange(DeviceState.DSPause); + #endregion + + _pauseHandle.WaitHandle.Reset(); + _pauseHandle.WaitResult = true; + _machineStateBeforePause = MachineState; + MachineState = MachineState.Pause; + } + else if (!_pauseHandle.WaitResult) + { + #region 鏉垮崱鎭㈠鍔ㄤ綔 + motionDevice.StateChange(DeviceState.DSOpen); + #endregion + + _pauseHandle.WaitHandle.Set(); + _pauseHandle.WaitResult = false; + MachineState = _machineStateBeforePause; + } + } + else + { + if (isToPause.Value) + { + if (!_pauseHandle.WaitResult) + { + #region 鏉垮崱鏆傚仠鍔ㄤ綔 + motionDevice.StateChange(DeviceState.DSPause); + #endregion + + _pauseHandle.WaitHandle.Reset(); + _pauseHandle.WaitResult = true; + MachineState = MachineState.Pause; + } + } + else + { + if (!_pauseHandle.WaitResult) + { + #region 鏉垮崱鎭㈠鍔ㄤ綔 + motionDevice.StateChange(DeviceState.DSOpen); + #endregion + + _pauseHandle.WaitHandle.Set(); + _pauseHandle.WaitResult = false; + MachineState = _machineStateBeforePause; + } + } + } + return new ProcessResponse(_pauseHandle.WaitResult); } - [ProcessMethod("", "ResumeJob", "缁х画娴佺▼", InvokeType.TestInvoke)] - public ProcessResponse ResumeJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + ////[ProcessMethod("", "PauseJob", "鏆傚仠娴佺▼", InvokeType.TestInvoke)] + //public ProcessResponse PauseJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + //{ + // if (!_pauseHandle.WaitResult) + // { + // #region 鏉垮崱鏆傚仠鍔ㄤ綔 + // #endregion + + // _pauseHandle.WaitHandle.Reset(); + // } + + // _pauseHandle.WaitResult = !_pauseHandle.WaitResult; + // return new ProcessResponse(_pauseHandle.WaitResult); + //} + + ////[ProcessMethod("", "ResumeJob", "缁х画娴佺▼", InvokeType.TestInvoke)] + //public ProcessResponse ResumeJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + //{ + // if (_pauseHandle.WaitResult) + // { + // #region 鏉垮崱鎭㈠鍔ㄤ綔 + // #endregion + + // _pauseHandle.WaitHandle.Set(); + // } + + // _pauseHandle.WaitResult = !_pauseHandle.WaitResult; + // return new ProcessResponse(_pauseHandle.WaitResult); + //} + + #region 涓夎壊鐏� & 铚傞福鍣� + //[ProcessMethod("MotionCardBase", "SwitchLightRed", "鍒囨崲鎸囩ず鐏�-绾�", InvokeType.TestInvoke)] + //public ProcessResponse SwitchLightRed(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + //{ + // MotionCardDefaultRun("SwitchLightRed", ref opConfig, ref invokeDevice); + // return new ProcessResponse(true); + //} + + //[ProcessMethod("MotionCardBase", "SwitchLightYellow", "鍒囨崲鎸囩ず鐏�-榛�", InvokeType.TestInvoke)] + //public ProcessResponse SwitchLightYellow(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + //{ + // return new ProcessResponse(true); + //} + + //[ProcessMethod("MotionCardBase", "SwitchLightGreen", "鍒囨崲鎸囩ず鐏�-缁�", InvokeType.TestInvoke)] + //public ProcessResponse SwitchLightGreen(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + //{ + // return new ProcessResponse(true); + //} + + //[ProcessMethod("MotionCardBase", "SwitchBeep", "鍒囨崲铚傞福鍣�", InvokeType.TestInvoke)] + //public ProcessResponse SwitchBeep(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + //{ + // return new ProcessResponse(true); + //} + + //[ProcessMethod("MotionCardBase", "SwitchNormalLight", "鍒囨崲鏃ュ厜鐏�", InvokeType.TestInvoke)] + //public ProcessResponse SwitchNormalLight(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + //{ + // MotionCardDefaultRun("SwitchNormalLight", ref opConfig, ref invokeDevice); + + // return new ProcessResponse(true); + //} + + int index_RedLight = -1; + int index_GreenLight = -1; + int index_YellowLight = -1; + int index_Beep = -1; + int index_NormalLight = -1; + MotionCardBase outputCtrlCard = null; + + public void SwitchLightRed(bool isOn) { - if (_pauseHandle.WaitResult) + if (index_RedLight >= 0) { - #region 鏉垮崱鎭㈠鍔ㄤ綔 - #endregion - - _pauseHandle.WaitHandle.Set(); + if (outputCtrlCard != null) + { + outputCtrlCard.WriteOutput((short)index_RedLight, isOn ? IOValue.TRUE : IOValue.FALSE); + } + else + { + LogAsync(DateTime.Now, "鏈缃澘鍗¤緭鍑鸿澶�", ""); + } } - - _pauseHandle.WaitResult = !_pauseHandle.WaitResult; - return new ProcessResponse(_pauseHandle.WaitResult); + else + { + LogAsync(DateTime.Now, "绾㈣壊鐏湭閰嶇疆姝g‘杈撳嚭绱㈠紩", ""); + } } - [ProcessMethod("MotionCardBase", "SwitchLightRed", "鍒囨崲鎸囩ず鐏�-绾�", InvokeType.TestInvoke)] - public ProcessResponse SwitchLightRed(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + public void SwitchLightYellow(bool isOn) { - MotionCardDefaultRun("SwitchLightRed", ref opConfig, ref invokeDevice); - return new ProcessResponse(true); + if (index_YellowLight >= 0) + { + if (outputCtrlCard != null) + { + outputCtrlCard.WriteOutput((short)index_YellowLight, isOn ? IOValue.TRUE : IOValue.FALSE); + } + else + { + LogAsync(DateTime.Now, "鏈缃澘鍗¤緭鍑鸿澶�", ""); + } + } + else + { + LogAsync(DateTime.Now, "榛勮壊鐏湭閰嶇疆姝g‘杈撳嚭绱㈠紩", ""); + } } - [ProcessMethod("MotionCardBase", "SwitchLightYellow", "鍒囨崲鎸囩ず鐏�-榛�", InvokeType.TestInvoke)] - public ProcessResponse SwitchLightYellow(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + public void SwitchLightGreen(bool isOn) { - return new ProcessResponse(true); + if (index_GreenLight >= 0) + { + if (outputCtrlCard != null) + { + outputCtrlCard.WriteOutput((short)index_GreenLight, isOn ? IOValue.TRUE : IOValue.FALSE); + } + else + { + LogAsync(DateTime.Now, "鏈缃澘鍗¤緭鍑鸿澶�", ""); + } + } + else + { + LogAsync(DateTime.Now, "缁胯壊鐏湭閰嶇疆姝g‘杈撳嚭绱㈠紩", ""); + } } - [ProcessMethod("MotionCardBase", "SwitchLightGreen", "鍒囨崲鎸囩ず鐏�-缁�", InvokeType.TestInvoke)] - public ProcessResponse SwitchLightGreen(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + public void SwitchLight(bool isOn) { - return new ProcessResponse(true); + if (index_NormalLight >= 0) + { + if (outputCtrlCard != null) + { + outputCtrlCard.WriteOutput((short)index_NormalLight, isOn ? IOValue.TRUE : IOValue.FALSE); + } + else + { + LogAsync(DateTime.Now, "鏈缃澘鍗¤緭鍑鸿澶�", ""); + } + } + else + { + LogAsync(DateTime.Now, "鐓ф槑鐏湭閰嶇疆姝g‘杈撳嚭绱㈠紩", ""); + } } - [ProcessMethod("MotionCardBase", "SwitchBeep", "鍒囨崲铚傞福鍣�", InvokeType.TestInvoke)] - public ProcessResponse SwitchBeep(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + public void SwitchBeep(bool isOn) { - return new ProcessResponse(true); - } + if (Config.IsBeepBlocked) + return; - [ProcessMethod("MotionCardBase", "SwitchNormalLight", "鍒囨崲鏃ュ厜鐏�", InvokeType.TestInvoke)] - public ProcessResponse SwitchNormalLight(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) - { - MotionCardDefaultRun("SwitchNormalLight", ref opConfig, ref invokeDevice); - - return new ProcessResponse(true); + if (index_Beep >= 0) + { + if (outputCtrlCard != null) + { + outputCtrlCard.WriteOutput((short)index_Beep, isOn ? IOValue.TRUE : IOValue.FALSE); + } + else + { + LogAsync(DateTime.Now, "鏈缃澘鍗¤緭鍑鸿澶�", ""); + } + } + else + { + LogAsync(DateTime.Now, "铚傞福鍣ㄦ湭閰嶇疆姝g‘杈撳嚭绱㈠紩", ""); + } } + #endregion private void MotionCardDefaultRun(string methodCode, ref IOperationConfig opConfig, ref IDevice invokeDevice) { @@ -231,8 +573,62 @@ if (invokeDevice is MotionCardBase motionCard) { - motionCard.Run(opConfig); + var response = motionCard.Run(opConfig); + if (!response.Result) + { + throw new ProcessException($"{motionCard.Name}寮傚父锛寋response.Message}", null, ExceptionLevel.Fatal); + } } } + + #region 瀹夊叏闂� & 瀹夊叏鍏夌嚎 + bool isSafetyDoorTrigged = false; + bool isSafetyBeamTrigged = false; + + public bool IsSafetyDoorTrigged + { + get => !Config.IsSafetyDoorBlocked && isSafetyDoorTrigged; + set => isSafetyDoorTrigged = value; + } + public bool IsSafetyBeamTrigged + { + get => !Config.IsSafetyBeamBlocked && isSafetyBeamTrigged; + set => isSafetyBeamTrigged = value; + } + + [ProcessMethod("", "SafetyDoorSignal", "瀹夊叏闂ㄤ俊鍙风洃鎺э紝姝e父ON锛孫FF鏃舵姤璀�", InvokeType.TestInvoke)] + public ProcessResponse SafetyDoorSignal(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + { + if (opConfig.InputPara == null || opConfig.InputPara.Count == 0) + throw new ProcessException("瀹夊叏闂ㄧ洃鎺ф湭閰嶇疆杈撳叆淇″彿"); + + IsSafetyDoorTrigged = opConfig.InputPara[0] == 0; + + if (IsSafetyDoorTrigged) + { + RaisedAlarm("瀹夊叏闂ㄦ湭姝e父鍏抽棴"); + MachineState = MachineState.Alarm; + } + + return new ProcessResponse(true); + } + + [ProcessMethod("", "SafetyBeamSignal", "瀹夊叏鍏夊箷淇″彿鐩戞帶锛屾甯窸N锛孫FF鏃舵姤璀�", InvokeType.TestInvoke)] + public ProcessResponse SafetyBeamSignal(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice) + { + if (opConfig.InputPara == null || opConfig.InputPara.Count == 0) + throw new ProcessException("瀹夊叏鍏夊箷鐩戞帶鏈厤缃緭鍏ヤ俊鍙�"); + + IsSafetyBeamTrigged = opConfig.InputPara[0] == 0; + + if (IsSafetyBeamTrigged) + { + RaisedAlarm("瀹夊叏鍏夌嚎琚伄鎸�"); + MachineState = MachineState.Alarm; + } + + return new ProcessResponse(true); + } + #endregion } } -- Gitblit v1.8.0