From 9519d0962fb2772cce85653f53d140b8aa657ab2 Mon Sep 17 00:00:00 2001
From: wells.liu <wells.liu@broconcentric.com>
Date: 星期一, 13 七月 2020 09:40:17 +0800
Subject: [PATCH] 数据库迁移

---
 src/Bro.M071.Process/M071Process_MotionCard.cs |  354 ++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 293 insertions(+), 61 deletions(-)

diff --git a/src/Bro.M071.Process/M071Process_MotionCard.cs b/src/Bro.M071.Process/M071Process_MotionCard.cs
index 06474aa..2821708 100644
--- a/src/Bro.M071.Process/M071Process_MotionCard.cs
+++ b/src/Bro.M071.Process/M071Process_MotionCard.cs
@@ -19,56 +19,103 @@
         public Timer ResetTimer = null;
         const int FULLRESETTIME = 5;
 
+        object machineStateLock = new object();
         MachineState machineState = MachineState.Unknown;
         public MachineState MachineState
         {
             get => machineState;
             set
             {
+                if (machineState == value)
+                    return;
+
                 machineState = value;
 
                 switch (machineState)
                 {
                     case MachineState.Ready:
-                        SwitchBeep(false);
-                        SwitchLightRed(false);
-                        SwitchLightYellow(false);
+                        lock (machineStateLock)
+                        {
+                            SwitchBeep(false);
+                            SwitchLightRed(false);
+                            SwitchLightYellow(false);
+                        }
 
                         Task.Run(() =>
                         {
+
                             while (MachineState == MachineState.Ready)
                             {
-                                SwitchLightGreen(true);
-                                Thread.Sleep(1000);
-                                SwitchLightGreen(false);
+                                lock (machineStateLock)
+                                {
+                                    SwitchLightGreen(true);
+                                    Thread.Sleep(1000);
+                                    SwitchLightGreen(false);
+                                    Thread.Sleep(1000);
+                                }
                             }
                         });
 
                         break;
                     case MachineState.Running:
-                        SwitchBeep(false);
-                        SwitchLightRed(false);
-                        SwitchLightYellow(false);
-                        SwitchLightGreen(true);
+                        lock (machineStateLock)
+                        {
+                            SwitchBeep(false);
+                            SwitchLightRed(false);
+                            SwitchLightYellow(false);
+                            SwitchLightGreen(true);
+                        }
                         break;
                     case MachineState.Alarm:
-                        SwitchBeep(true);
-                        SwitchLightRed(true);
-                        SwitchLightYellow(false);
-                        SwitchLightGreen(false);
+                        lock (machineStateLock)
+                        {
+                            SwitchBeep(true);
+                            SwitchLightRed(true);
+                            SwitchLightYellow(false);
+                            SwitchLightGreen(false);
+                        }
                         break;
                     case MachineState.Pause:
-                        SwitchBeep(false);
-                        SwitchLightRed(false);
+                        lock (machineStateLock)
+                        {
+                            SwitchBeep(false);
+                            SwitchLightRed(false);
+                        }
                         Task.Run(() =>
                         {
                             while (MachineState == MachineState.Pause)
                             {
-                                SwitchLightYellow(true);
-                                SwitchLightGreen(true);
-                                Thread.Sleep(1000);
-                                SwitchLightYellow(false);
-                                SwitchLightGreen(false);
+                                lock (machineStateLock)
+                                {
+                                    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(() =>
+                        {
+                            while (MachineState == MachineState.Resetting)
+                            {
+                                lock (machineStateLock)
+                                {
+
+                                    SwitchLightYellow(true);
+                                    Thread.Sleep(1000);
+                                    SwitchLightYellow(false);
+                                    Thread.Sleep(1000);
+                                }
                             }
                         });
                         break;
@@ -83,7 +130,7 @@
         private void MotionCardSettingCheck()
         {
             IDevice device = DeviceCollection.FirstOrDefault(u => u is IMotionCard);
-            if (device.InitialConfig is MotionCardInitialConfigBase iConfig)
+            if (device?.InitialConfig is MotionCardInitialConfigBase iConfig)
             {
                 outputCtrlCard = device as MotionCardBase;
 
@@ -143,6 +190,16 @@
             }
         }
 
+        [ProcessMethod("MotionCardBase", "GotoReadyPosition", "杩愬姩鍒伴澶囦綅缃�", InvokeType.TestInvoke)]
+        public ProcessResponse GotoReadyPosition(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
+        {
+            MotionCardDefaultRun("GotoReadyPosition", ref opConfig, ref invokeDevice);
+
+            LogAsync(DateTime.Now, "杩愬姩鍒伴澶囦綅缃畬鎴�", "");
+
+            return new ProcessResponse(true);
+        }
+
         [ProcessMethod("MotionCardBase", "Reset", "绠�鍗曞浣嶆搷浣�", InvokeType.TestInvoke)]
         public ProcessResponse Reset(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
         {
@@ -164,28 +221,45 @@
             //    }
             //}
 
-            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;
-
             if (ResetTimer == null)
             {
                 ResetTimer = new Timer(FullReset, null, -1, -1);
             }
 
-            if (opConfig.InputPara.Count > 0)
+            if (opConfig?.InputPara != null && opConfig.InputPara.Count > 0)
             {
                 //澶у浣嶄俊鍙�
-                ResetTimer.Change(-1, opConfig.InputPara[0] == 1 ? FULLRESETTIME * 1000 : -1);
+                ResetTimer.Change(opConfig.InputPara[0] == 1 ? FULLRESETTIME * 1000 : -1, -1);
+
+                if (opConfig.InputPara[0] == 0)
+                    return new ProcessResponse(true);
             }
+
+            if (!IsAllowedWork)
+            {
+                LogAsync(DateTime.Now, $"{SafetyMsg}锛屽浣嶅け璐�", "");
+                return new ProcessResponse(false);
+            }
+
+            MotionCardDefaultRun("Reset", ref opConfig, ref invokeDevice);
+            (invokeDevice as MotionCardBase).ResetAlarm();
+
+            RaisedAlarm("");
+            MachineState = MachineState.Ready;
+
+            if (IsEmergencyStopped)
+            {
+                RaisedAlarm("鎬ュ仠鎸夐挳鏈仮澶嶏紝璇锋墽琛屽ぇ澶嶄綅");
+                MachineState = MachineState.Alarm;
+            }
+
+
+
+            //if (opConfig.InputPara?.Count > 0)
+            //{
+            //    //澶у浣嶄俊鍙�
+            //    ResetTimer.Change(-1, opConfig.InputPara[0] == 1 ? FULLRESETTIME * 1000 : -1);
+            //}
 
             //if (invokeDevice is MotionCardBase motionCard)
             //{
@@ -199,10 +273,18 @@
 
         private void FullReset(object state)
         {
-            FullReset(null, null, null);
+            try
+            {
+                FullReset(null, null, null);
+            }
+            catch (Exception ex)
+            {
+                ExceptionRaisedInMonitor(ex);
+            }
         }
 
-        [ProcessMethod("MotionCardOperationConfigCollection", "FullReset", "澶у浣嶆搷浣�", InvokeType.TestInvoke)]
+        //[ProcessMethod("MotionCardOperationConfigCollection", "FullReset", "澶у浣嶆搷浣�", InvokeType.TestInvoke)]
+        [ProcessMethod("MotionCardBase", "FullReset", "澶у浣嶆搷浣�", InvokeType.TestInvoke)]
         public ProcessResponse FullReset(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
         {
             //if (opConfig == null)
@@ -228,6 +310,7 @@
             //    motionCard.Run(opConfig);
             //}
 
+            MachineState = MachineState.Resetting;
             MotionCardDefaultRun("FullReset", ref opConfig, ref invokeDevice);
 
             productionList.ForEach(u => u.Dispose());
@@ -235,7 +318,15 @@
 
             OnFullResetDone?.Invoke();
 
+            isFullResetCovered = true;
+
             LogAsync(DateTime.Now, "澶у浣嶅姩浣滃畬鎴�", "");
+
+            RaisedAlarm("");
+            MachineState = MachineState.Ready;
+
+            GotoReadyPosition(null, null, null);
+
             return new ProcessResponse(true);
         }
 
@@ -249,36 +340,123 @@
             WaitHandle = new ManualResetEvent(true),
             WaitResult = true,
         };
-
-        [ProcessMethod("", "PauseJob", "鏆傚仠娴佺▼", InvokeType.TestInvoke)]
-        public ProcessResponse PauseJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
+        MachineState _machineStateBeforePause = MachineState.Unknown;
+        List<MachineState> _statesAllowPause = new List<MachineState>() { MachineState.Running, MachineState.Ready, MachineState.Pause };
+        [ProcessMethod("", "SwitchJobStatus", "娴佺▼鐘舵�佸垏鎹�", InvokeType.TestInvoke)]
+        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 (opConfig.InputPara[0] == 10)
+            {
+                isToPause = false;
+            }
+            else if (opConfig.InputPara[0] == 11)
+            {
+                isToPause = true;
+            }
+
+            if (isToPause == null)
+            {
+                if (!_pauseHandle.WaitResult)
+                {
+                    #region 鏉垮崱鏆傚仠鍔ㄤ綔
+                    motionDevice.SetImmediatePause();
+                    #endregion
+
+                    _pauseHandle.WaitHandle.Reset();
+                    _pauseHandle.WaitResult = true;
+                    _machineStateBeforePause = MachineState;
+                    MachineState = MachineState.Pause;
+                }
+                else if (!_pauseHandle.WaitResult)
+                {
+                    #region 鏉垮崱鎭㈠鍔ㄤ綔
+                    motionDevice.ResetImmediatePause();
+                    #endregion
+
+                    _pauseHandle.WaitHandle.Set();
+                    _pauseHandle.WaitResult = false;
+                    MachineState = _machineStateBeforePause;
+                }
+            }
+            else
+            {
+                if (isToPause.Value)
+                {
+                    if (!_pauseHandle.WaitResult)
+                    {
+                        #region 鏉垮崱鏆傚仠鍔ㄤ綔
+                        motionDevice.SetImmediatePause();
+                        #endregion
+
+                        _pauseHandle.WaitHandle.Reset();
+                        _pauseHandle.WaitResult = true;
+                        MachineState = MachineState.Pause;
+                    }
+                }
+                else
+                {
+                    if (!_pauseHandle.WaitResult)
+                    {
+                        #region 鏉垮崱鎭㈠鍔ㄤ綔
+                        motionDevice.ResetImmediatePause();
+                        #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)
-        {
-            if (_pauseHandle.WaitResult)
-            {
-                #region 鏉垮崱鎭㈠鍔ㄤ綔
-                #endregion
+        ////[ProcessMethod("", "PauseJob", "鏆傚仠娴佺▼", InvokeType.TestInvoke)]
+        //public ProcessResponse PauseJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
+        //{
+        //    if (!_pauseHandle.WaitResult)
+        //    {
+        //        #region 鏉垮崱鏆傚仠鍔ㄤ綔
+        //        #endregion
 
-                _pauseHandle.WaitHandle.Set();
-            }
+        //        _pauseHandle.WaitHandle.Reset();
+        //    }
 
-            _pauseHandle.WaitResult = !_pauseHandle.WaitResult;
-            return new ProcessResponse(_pauseHandle.WaitResult);
-        }
+        //    _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)]
@@ -423,7 +601,7 @@
         private void MotionCardDefaultRun(string methodCode, ref IOperationConfig opConfig, ref IDevice invokeDevice)
         {
             IMonitorSet monitorSet = null;
-            if (opConfig == null)
+            if (opConfig == null || !(opConfig is MotionCardOperationConfigBase))
             {
                 monitorSet = Config.MonitorSetCollection.FirstOrDefault(u => u.MethodCode == methodCode);
                 if (monitorSet == null)
@@ -443,8 +621,21 @@
 
             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);
+                }
             }
+        }
+
+        bool IsAllowedWork
+        {
+            get => !(IsSafetyBeamTrigged || IsSafetyDoorTrigged || IsEmergencyStopped);
+        }
+        string SafetyMsg
+        {
+            get => $"{(IsSafetyBeamTrigged ? "瀹夊叏鍏夊箷" : "")}{(IsSafetyDoorTrigged ? " 瀹夊叏闂�" : "")}{(IsEmergencyStopped ? " 鎬ュ仠鎸夐挳" : "")}瑙﹀彂涓�";
         }
 
         #region 瀹夊叏闂� & 瀹夊叏鍏夌嚎
@@ -482,6 +673,9 @@
         [ProcessMethod("", "SafetyBeamSignal", "瀹夊叏鍏夊箷淇″彿鐩戞帶锛屾甯窸N锛孫FF鏃舵姤璀�", InvokeType.TestInvoke)]
         public ProcessResponse SafetyBeamSignal(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
         {
+            if (MachineState != MachineState.Running && MachineState != MachineState.Alarm)
+                return new ProcessResponse(true);
+
             if (opConfig.InputPara == null || opConfig.InputPara.Count == 0)
                 throw new ProcessException("瀹夊叏鍏夊箷鐩戞帶鏈厤缃緭鍏ヤ俊鍙�");
 
@@ -496,5 +690,43 @@
             return new ProcessResponse(true);
         }
         #endregion
+
+        #region 鎬ュ仠
+        bool isEmergencyStopped = false;
+        bool isFullResetCovered = true;
+        public bool IsEmergencyStopped
+        {
+            get => isEmergencyStopped && isFullResetCovered;
+            set
+            {
+                if (value)
+                {
+                    isFullResetCovered = false;
+                    isEmergencyStopped = true;
+                }
+                else
+                {
+                    isEmergencyStopped = false;
+                }
+            }
+        }
+
+        [ProcessMethod("", "EmergencyStop", "鎬ュ仠鎸夐挳琚媿涓�", InvokeType.TestInvoke)]
+        public ProcessResponse EmergencyStop(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
+        {
+            if (opConfig.InputPara == null || opConfig.InputPara.Count == 0)
+                throw new ProcessException("鎬ュ仠鎸夐挳鏈厤缃緭鍏ヤ俊鍙�");
+
+            IsEmergencyStopped = opConfig.InputPara[0] == 0;
+
+            if (IsEmergencyStopped)
+            {
+                RaisedAlarm("鎬ュ仠鎸夐挳琚媿涓�");
+                MachineState = MachineState.Alarm;
+            }
+
+            return new ProcessResponse(true);
+        }
+        #endregion
     }
 }

--
Gitblit v1.8.0