From 99587d3c26f5b952cb7dc87a56be91b08c5d277b Mon Sep 17 00:00:00 2001
From: wells.liu <wells.liu@broconcentric.com>
Date: 星期一, 06 七月 2020 11:54:27 +0800
Subject: [PATCH] 添加轴状态监听

---
 src/Bro.Device.GTSCard/GTSCardDriver.cs |   67 +++++++++++++++++++--------------
 1 files changed, 39 insertions(+), 28 deletions(-)

diff --git a/src/Bro.Device.GTSCard/GTSCardDriver.cs b/src/Bro.Device.GTSCard/GTSCardDriver.cs
index 3368e83..adba5c9 100644
--- a/src/Bro.Device.GTSCard/GTSCardDriver.cs
+++ b/src/Bro.Device.GTSCard/GTSCardDriver.cs
@@ -76,27 +76,44 @@
         }
 
         /// <summary>
-        /// 璁惧 杩愯
+        /// 璁惧 杩愯锛堟墽琛� 鏉垮崱绯诲垪鐨勬搷浣滅殑 闆嗗悎锛�
         /// </summary>
         /// <param name="config"></param>
         /// <returns></returns>
         public override ResponseMessage Run(IOperationConfig config)
         {
             ResponseMessage responseMessage = new ResponseMessage();
-            var opConfig = config as GTSCardOperationConfig;
+            var motionCardOperationConfig = config as MotionCardOperationConfigBase;
+            foreach (var operationSet in motionCardOperationConfig.OperationCollection)
+            {
+                responseMessage = RunOperationSet(operationSet);
+                if (!responseMessage.Result)
+                {
+                    return responseMessage;
+                }
+            }
+            return responseMessage;
+        }
+
+        /// <summary>
+        /// 鎵ц 涓�涓郴鍒楃殑鎿嶄綔
+        /// </summary>
+        private ResponseMessage RunOperationSet(MotionCardOperationSet operationSet)
+        {
+            ResponseMessage responseMessage = new ResponseMessage();
             // 1.棰勬鏌�
             if (CurrentState == DeviceState.DSOpen)
             {
-                foreach (var preCheck in opConfig.PreCheckIOCollection)
+                foreach (var preCheck in operationSet.PreCheckIOCollection)
                 {
-                    int timeout = opConfig.PreCheckIOTimeout;
-                    int? ioData = null;
+                    int timeout = operationSet.PreCheckIOTimeout;
+                    IOValue? ioData = null;
                     while (CurrentState == DeviceState.DSOpen)
                     {
                         Thread.Sleep(10);
                         ioData = MonitorValues.FirstOrDefault(u => u.IONum == preCheck.IOItem.IONum && u.IOType == preCheck.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇
                         timeout -= 10;
-                        if (preCheck.CheckValue == ioData || (opConfig.PreCheckIOTimeout > 0 && timeout < 0))
+                        if (preCheck.CheckValue == ioData || (operationSet.PreCheckIOTimeout > 0 && timeout < 0))
                         {
                             break;
                         }
@@ -114,8 +131,7 @@
             // 2.鏉垮崱杩愬姩
             if (CurrentState == DeviceState.DSOpen)
             {
-                responseMessage = MoveToPoint(opConfig);
-
+                responseMessage = MoveToPoint(operationSet.MotionOperationCollection);
                 if (!responseMessage.Result)
                 {
                     return responseMessage;
@@ -126,7 +142,7 @@
             // 3.IO杈撳嚭 涓嶉渶瑕佽秴鏃�
             if (CurrentState == DeviceState.DSOpen)
             {
-                foreach (var ioOutput in opConfig.IOOutputCollection)
+                foreach (var ioOutput in operationSet.IOOutputCollection)
                 {
                     var ioData = MonitorValues.FirstOrDefault(u => u.IONum == ioOutput.IOItem.IONum && u.IOType == ioOutput.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇
                     if (ioOutput.CheckValue != ioData)
@@ -141,16 +157,16 @@
             // 4.IO纭
             if (CurrentState == DeviceState.DSOpen)
             {
-                foreach (var ioConfirm in opConfig.IOConfirmCollection)
+                foreach (var ioConfirm in operationSet.IOConfirmCollection)
                 {
-                    int timeout = opConfig.IOConfirmTimeout;
-                    int? ioData = null;
+                    int timeout = operationSet.IOConfirmTimeout;
+                    IOValue? ioData = null;
                     while (CurrentState == DeviceState.DSOpen)
                     {
                         Thread.Sleep(10);
                         ioData = MonitorValues.FirstOrDefault(u => u.IONum == ioConfirm.IOItem.IONum && u.IOType == ioConfirm.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇
                         timeout -= 10;
-                        if (ioConfirm.CheckValue == ioData || (opConfig.IOConfirmTimeout > 0 && timeout < 0))
+                        if (ioConfirm.CheckValue == ioData || (operationSet.IOConfirmTimeout > 0 && timeout < 0))
                         {
                             break;
                         }
@@ -250,19 +266,14 @@
         public override ResponseMessage MoveToPoint(IOperationConfig opConfig)
         {
             ResponseMessage responseMessage = new ResponseMessage();
-            var gtsOperationConfig = opConfig as GTSCardOperationConfig;
+            var gtsOperationCollection = opConfig as MotionOperationCollection;
             List<Task<bool>> taskList = new List<Task<bool>>();
-            //TaskFactory factory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);
-            // 濡傛灉鏄涓酱鐨勮繍鍔� 绛夋瘡涓酱杩愬姩缁撴潫
-            foreach (var movingOp in gtsOperationConfig.MovingOps)
+            foreach (var movingOp in gtsOperationCollection.MovingOps)
             {
-                //var task = factory.StartNew<bool>((op) =>
-                //{
-                //    return SingleAxisMoving(op as MovingOption);
-                //}, movingOp);
                 var task = SingleAxisMoving(movingOp);
                 taskList.Add(task);
             }
+
             Task.WaitAll(taskList.ToArray());
             responseMessage.Result = taskList.All(u => u.GetAwaiter().GetResult());
             if (!responseMessage.Result)
@@ -312,13 +323,13 @@
                     if (IConfig.AxisSettings.FirstOrDefault(a => a.AxisIndex == optionPara.AxisIndex)?.IsAxisEnabled ?? false)
                     {
                         string _position = "";
-                        string motionType = optionPara.MoveMode == EnumHelper.MotorMoveMode.Normal ? (optionPara.IsAbsolute ? "Abs" : "Rel") : optionPara.MoveMode.ToString();
+                        string motionType = optionPara.MoveMode == EnumHelper.MotionMode.Normal ? (optionPara.IsAbsolute ? "Abs" : "Rel") : optionPara.MoveMode.ToString();
 
                         _position = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff},{optionPara.AxisIndex},{motionType},{GetPosition(optionPara.AxisIndex)},{GetPrfPosition(optionPara.AxisIndex)},{optionPara.Destination},";
 
                         switch (optionPara.MoveMode)
                         {
-                            case MotorMoveMode.Normal:
+                            case MotionMode.Normal:
                                 {
                                     if (_isResetting)
                                     {
@@ -337,12 +348,12 @@
 
                                 }
                                 break;
-                            case MotorMoveMode.FindOri:
+                            case MotionMode.FindOri:
                                 {
                                     isSuccessAndStop = GoHome(optionPara);
                                 }
                                 break;
-                            case MotorMoveMode.Jog:
+                            case MotionMode.Jog:
                                 {
                                     isSuccessAndStop = JogMove(optionPara);
                                 }
@@ -756,17 +767,17 @@
         /// </summary>
         /// <param name="index">杈撳嚭鍙�,杩斿洖1-16</param>
         /// <param name="value">false琛ㄧず杈撳嚭锛宼rue琛ㄧず鍏抽棴</param>
-        public override void WriteOutput(short cardNum, short index, IOValue value)
+        public override void WriteOutput(short index, IOValue value)
         {
             short outNum = (short)(index % 100 + 1);
             if ((int)value <= 1)
             {
-                GTSCardAPI.GT_SetDoBit(cardNum, GTSCardAPI.MC_GPO, outNum, (short)value);
+                GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPO, outNum, (short)value);
             }
             else
             {
                 var currentValue = (int)MonitorValues.FirstOrDefault(u => u.IONum == outNum && u.IOType == IOType.OUTPUT).Value;
-                GTSCardAPI.GT_SetDoBit(cardNum, GTSCardAPI.MC_GPO, outNum, (short)(currentValue == 1 ? 0 : 1));
+                GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPO, outNum, (short)(currentValue == 1 ? 0 : 1));
             }
         }
 

--
Gitblit v1.8.0