瀏覽代碼

超时回滚机制完善,待数据库可操作后进行测试

yc_t 7 年之前
父節點
當前提交
0368f979fd
共有 2 個文件被更改,包括 177 次插入147 次删除
  1. 175 143
      parkMonitor/server/CoreThread/AbstractCmd.cs
  2. 2 4
      parkMonitor/server/PLCLinker/PLCLinker.cs

+ 175 - 143
parkMonitor/server/CoreThread/AbstractCmd.cs

@@ -79,7 +79,7 @@ namespace parkMonitor.server.CoreThread
             locationOper = new DBLocationOperator();
         }
         public abstract void executeCmd(Command queueCmd);
-        public void WaitForRobotResource(int robotID)
+        public bool WaitForRobotResource(int robotID)
         {
             PLCMessage PLCMsg = null;
             MyTimer mt = new MyTimer();
@@ -123,7 +123,7 @@ namespace parkMonitor.server.CoreThread
                 }
                 mt.EndTiming();
                 int activationCount = 0;
-                if (mt.IsLonger(5, 1, false,out activationCount))
+                if (mt.IsLonger(5, 1, false, out activationCount))
                 {
                     if (activationCount == 1)
                     {
@@ -133,43 +133,77 @@ namespace parkMonitor.server.CoreThread
                     {
                         mt.rolledBack = true;
                         UILogServer.ins.info("启动机械手资源超时,启动回滚");
-                        //回退
+                        return false;
                     }
                 }
             }
+            return true;
         }
-    }
 
-    /// <summary>
-    /// 停车命令处理类
-    /// </summary>
-    public class StopCmd : AbstractCmd
-    {
-        /// <summary>
+        /// /// <summary>
         /// 流程回滚函数
         /// </summary>
         /// <param name="queueCmd"></param>
         /// <param name="parkingSpaceID"></param>
-        private void rollback(Command queueCmd, int parkingSpaceID)
+        /// <param name="parkingSpaceUpdated"></param>
+        /// <param name="isParking"></param>
+        public void Rollback(Command queueCmd, int parkingSpaceID, bool parkingSpaceUpdated, bool isParking)
         {
             //命令回退
             queueCmd.returnedCount += 1;
             queuingThread.SetMessage(queueCmd);
-            //复位总车位数,车辆状态,车位状态
+            //复位车辆状态;若已更新过车位则还需复位总车位数,车位状态
             if (!queueCmd.manual)
             {
                 int freeSpaceCount = oper.getGarageFreeSpace(queueCmd.garageID);
-                freeSpaceCount = freeSpaceCount + 1;
-                oper.UpdateGarageFreeSpace(freeSpaceCount, queueCmd.garageID);
-                oper.UpdateVehicleParkState(queueCmd.LicenseNum, 0);
-                oper.UpdateParkingSpaceState(parkingSpaceID, 0);
+                if (isParking)
+                {
+                    oper.UpdateVehicleParkState(queueCmd.LicenseNum, 0);
+                    if (parkingSpaceUpdated)
+                    {
+                        freeSpaceCount = freeSpaceCount + 1;
+                        oper.UpdateGarageFreeSpace(freeSpaceCount, queueCmd.garageID);
+                        oper.UpdateParkingSpaceState(parkingSpaceID, 0);
+                    }
+                }
+                else
+                {
+                    oper.UpdateVehicleParkState(queueCmd.LicenseNum, 1);
+                    if (parkingSpaceUpdated)
+                    {
+                        freeSpaceCount = freeSpaceCount - 1;
+                        oper.UpdateGarageFreeSpace(freeSpaceCount, queueCmd.garageID);
+                        oper.UpdateParkingSpaceState(parkingSpaceID, 1);
+                    }
+                }
             }
             else
             {
-                locationOper.UpdateVehicleParkState(queueCmd.LicenseNum, 0);
-                locationOper.UpdateParkingSpaceState(parkingSpaceID, 0);
+                if (isParking)
+                {
+                    locationOper.UpdateVehicleParkState(queueCmd.LicenseNum, 0);
+                    if (parkingSpaceUpdated)
+                    {
+                        locationOper.UpdateParkingSpaceState(parkingSpaceID, 0);
+                    }
+                }
+                else
+                {
+                    locationOper.UpdateVehicleParkState(queueCmd.LicenseNum, 1);
+                    if (parkingSpaceUpdated)
+                    {
+                        locationOper.UpdateParkingSpaceState(parkingSpaceID, 1);
+                    }
+                }
             }
         }
+    }
+
+    /// <summary>
+    /// 停车命令处理类
+    /// </summary>
+    public class StopCmd : AbstractCmd
+    {
         /// <summary>
         /// 命令中号牌与对应号牌机中当前号牌比对,确认车辆
         /// </summary>
@@ -232,11 +266,11 @@ namespace parkMonitor.server.CoreThread
             {
                 if (!queueCmd.manual)
                 {
-                    ppp = aps.MallocParkingSpace(cEntrance, oper,queueCmd);//自动 
+                    ppp = aps.MallocParkingSpace(cEntrance, oper, queueCmd);//自动 
                 }
                 else
                 {
-                    ppp = aps.MallocParkingSpace(cEntrance, locationOper,queueCmd);//手动
+                    ppp = aps.MallocParkingSpace(cEntrance, locationOper, queueCmd);//手动
                 }
                 if (ppp != null)
                 {
@@ -255,8 +289,8 @@ namespace parkMonitor.server.CoreThread
                     if (MyTimer.restart && !mt.rolledBack)
                     {
                         mt.rolledBack = true;
-                        UILogServer.ins.info("回退");
-                        //return null;
+                        UILogServer.ins.error("超时未获得车位,指令退回");
+                        return null;
                     }
                 }
             }
@@ -336,11 +370,10 @@ namespace parkMonitor.server.CoreThread
                     if (MyTimer.restart && !mt.rolledBack)
                     {
                         mt.rolledBack = true;
-                        UILogServer.ins.info("回退");
-                        //return null;
-                        //回退
+                        UILogServer.ins.error("超时未获取激光数据,指令退回");
+                        return null;
                     }
-                    
+
                 }
             }
             return data;
@@ -348,7 +381,7 @@ namespace parkMonitor.server.CoreThread
         /// <summary>
         /// 停车完成
         /// </summary>
-        private void WaitForStoreCompletionSignal(Command queueCmd, ref int parkingSpaceID, ref bool robotError, int status)
+        private bool WaitForStoreCompletionSignal(Command queueCmd, ref int parkingSpaceID, ref bool robotError, int status)
         {
             PLCMessage PLCMsg = null;
             MyTimer mt = new MyTimer();
@@ -361,7 +394,7 @@ namespace parkMonitor.server.CoreThread
                 //停车完成信号
                 if (storeStatus == status)
                 {
-                    UILogServer.ins.log("已获得停车完成信号");
+                    UILogServer.ins.log("已获得停车完成信号"+status);
                     break;
                 }
                 else if (1 == 2)//机械手异常,则退指令,写数据库归位,判断条件需之后补充
@@ -390,18 +423,19 @@ namespace parkMonitor.server.CoreThread
                     if (MyTimer.restart && !mt.rolledBack)
                     {
                         mt.rolledBack = true;
-                        UILogServer.ins.info("回退");
-                        //回退
+                        UILogServer.ins.error("等待停车完成信号超时,流程回滚");
+                        return false;
                     }
                 }
             }
+            return true;
         }
         /// <summary>
         /// 等待轮距信息
         /// </summary>
         /// <param name="frontWheelbase">前轮距</param>
         /// <param name="rearWheelbase">后轮距</param>
-        private void WaitWheelbase(ref int frontWheelbase, ref int rearWheelbase)
+        private bool WaitWheelbase(ref int frontWheelbase, ref int rearWheelbase)
         {
             PLCMessage PLCMsg = null;
             MyTimer mt = new MyTimer();
@@ -430,11 +464,12 @@ namespace parkMonitor.server.CoreThread
                     if (MyTimer.restart && !mt.rolledBack)
                     {
                         mt.rolledBack = true;
-                        UILogServer.ins.info("回退");
-                        //回退
+                        UILogServer.ins.error("超时未获取轮距雷达数据,流程回滚");
+                        return false;
                     }
                 }
             }
+            return true;
         }
         /// <summary>
         /// 停车流程
@@ -442,7 +477,13 @@ namespace parkMonitor.server.CoreThread
         /// <param name="queueCmd"></param>
         public override void executeCmd(Command queueCmd)
         {
-            //第一步:
+            bool disappeared = false;
+            bool stopChecking = false;
+            bool robotError = false;
+            string numberPlate = queueCmd.LicenseNum;
+            int userID = 0;
+
+            //第一步,激光与车位:
             int status = 1;//停车
             ControlMessage cm = new ControlMessage();
             cm.status = status;
@@ -451,14 +492,13 @@ namespace parkMonitor.server.CoreThread
             Log.WriteLog("停车,开激光:" + queueCmd.LicenseNum);
             UILogServer.ins.info("停车,开激光:" + queueCmd.LicenseNum);
 
+            //获取车位资源
             Parking_Space ppp = new Parking_Space();
-            Task parkingSpace = Task.Factory.StartNew(() =>
-            {
-                ppp = WaitForParkingSpaceResource(queueCmd);
-            });
-            //int garageID = ppp.garageID;
-            string numberPlate = queueCmd.LicenseNum;
-            int userID = 0;
+            //Task parkingSpace = Task.Factory.StartNew(() =>
+            //{
+            ppp = WaitForParkingSpaceResource(queueCmd);
+            //});
+
             //过期号牌判断
             if (queueCmd.userID != "")
             {
@@ -466,9 +506,7 @@ namespace parkMonitor.server.CoreThread
             }
             int garageID = queueCmd.garageID;
             string realParkTime = queueCmd.TimeRecord;
-            bool disappeared = false;
-            bool stopChecking = false;
-            bool robotError = false;
+            //定义号牌验证与机械手异常标志
             //Task checkNum = Task.Factory.StartNew(() =>
             //{
             //    DateTime startTime = DateTime.Now;
@@ -496,17 +534,21 @@ namespace parkMonitor.server.CoreThread
             //        t = startTime - endTime;
             //    }
             //});
+
             Data dataReal = new Data();
-            Task dataFromLaser = Task.Factory.StartNew(() =>
-            {
-                dataReal = WaitForLaserResource(queueCmd, disappeared);
-            });
-            Task.WaitAll(parkingSpace, dataFromLaser);
-            if (ppp == null)
+            //Task dataFromLaser = Task.Factory.StartNew(() =>
+            //{
+            dataReal = WaitForLaserResource(queueCmd, disappeared);
+            //});
+            //Task.WaitAll(parkingSpace, dataFromLaser);
+
+            //判断车位与激光资源有效性,无效则回滚
+            if (ppp == null || dataReal == null)
             {
+                Rollback(queueCmd, ppp.parkingSpaceID, false, true);
                 return;
             }
-            //车位
+            //车位赋值与写数据库
             int parkingSpaceID = ppp.parkingSpaceID;
             int parkingSpaceX = ppp.parkingSpaceX;
             int parkingSpaceY = ppp.parkingSpaceY;
@@ -518,7 +560,7 @@ namespace parkMonitor.server.CoreThread
                 //更新车库表剩余车位数
                 int freeSpaceCount = oper.getGarageFreeSpace(garageID);
                 freeSpaceCount = freeSpaceCount - 1;
-                oper.UpdateGarageFreeSpace(freeSpaceCount, garageID);     
+                oper.UpdateGarageFreeSpace(freeSpaceCount, garageID);
             }
             else
             {
@@ -527,19 +569,8 @@ namespace parkMonitor.server.CoreThread
                 //更新车库表剩余车位数
                 int freeSpaceCount = locationOper.getGarageFreeSpace(garageID);
                 freeSpaceCount = freeSpaceCount - 1;
-                locationOper.UpdateGarageFreeSpace(freeSpaceCount, garageID);         
-            }
-            if(dataReal == null)
-            {
-                //系统回滚
+                locationOper.UpdateGarageFreeSpace(freeSpaceCount, garageID);
             }
-            //激光数据
-            int centerX = dataReal.centerX;
-            int centerY = dataReal.centerY;
-            int angleA = dataReal.angleA;
-            int length = dataReal.length;
-            int width = dataReal.width;
-            int height = dataReal.height;
             //号牌失效,数据库回滚
             if (disappeared)
             {
@@ -554,79 +585,83 @@ namespace parkMonitor.server.CoreThread
                 }
                 return;
             }
-            stopChecking = true;//开始启动机械手,停止检测号牌
-            UILogServer.ins.log("停止核对,准备启动机械手");
+            //开始启动机械手,停止检测号牌
+            stopChecking = true;
+            UILogServer.ins.log("停止号牌核对,准备启动机械手");
             int robotID = 0;
-            robotID = queueCmd.id / 3 + 1;
-
+            robotID = queueCmd.id / 6 + 1;
             int frontWheelbase = 0;
             int rearWheelbase = 0;
-            Task robotFree = Task.Factory.StartNew(() =>
+            //Task robotFree = Task.Factory.StartNew(() =>
+            //{
+            //需要先遍历robot数组,根据id找到对应机械手
+            if (!WaitForRobotResource(robotID))
             {
-                //需要先遍历robot数组,根据id找到对应机械手
-                WaitForRobotResource(robotID);
-            });
-            robotFree.Wait();
-            //第二步:
+                Rollback(queueCmd, parkingSpaceID, true, true);
+            }
+            //});
+            //robotFree.Wait();
+
+            //第二步,机械手:
             status = 2;
             cm.status = status;
             cm.RobotID = robotID;//启动对应机械手
-            cm.centerX = Convert.ToString(centerX);
-            cm.centerY = Convert.ToString(centerY);
-            cm.angleA = Convert.ToString(angleA);
-            cm.length = Convert.ToString(length);
-            cm.width = Convert.ToString(width);
-            cm.height = Convert.ToString(height);
+            //激光数据
+            cm.centerX = Convert.ToString(dataReal.centerX);
+            cm.centerY = Convert.ToString(dataReal.centerY);
+            cm.angleA = Convert.ToString(dataReal.angleA);
+            cm.length = Convert.ToString(dataReal.length);
+            cm.width = Convert.ToString(dataReal.width);
+            cm.height = Convert.ToString(dataReal.height);
             cm.parkingSpaceID = Convert.ToString(parkingSpaceID);
             cm.parkingSpaceX = Convert.ToString(parkingSpaceX);
             cm.parkingSpaceY = Convert.ToString(parkingSpaceY);
             cm.parkingSpaceZ = Convert.ToString(parkingSpaceZ);
             PLC.SetMessage(cm);
-
-            Console.WriteLine("停车,抓车:" + queueCmd.LicenseNum);
             Log.WriteLog("停车,抓车:" + queueCmd.LicenseNum);
             UILogServer.ins.info("停车,抓车:" + queueCmd.LicenseNum);
 
-            Task dataFromWheelbase = Task.Factory.StartNew(() =>
+            //Task dataFromWheelbase = Task.Factory.StartNew(() =>
+            //{
+            if(!WaitWheelbase(ref frontWheelbase, ref rearWheelbase))
             {
-                WaitWheelbase(ref frontWheelbase, ref rearWheelbase);
-            });
+                Rollback(queueCmd, parkingSpaceID, true, true);
+            }
+            //});
 
-            Task storeCompletionSignal = Task.Factory.StartNew(() =>
+            //Task storeCompletionSignal = Task.Factory.StartNew(() =>
+            //{
+            if(!WaitForStoreCompletionSignal(queueCmd, ref parkingSpaceID, ref robotError, 1))
             {
-                WaitForStoreCompletionSignal(queueCmd, ref parkingSpaceID, ref robotError,1);
-            });
-            //storeSignal.Wait();
-            Task.WaitAll(dataFromWheelbase, storeCompletionSignal);
-            //第三步:
+                Rollback(queueCmd, parkingSpaceID, true, true);
+            }
+            //});
+            //Task.WaitAll(dataFromWheelbase, storeCompletionSignal);
+
+            //第三步,停车完成:
             status = 3;
             cm.status = status;
             PLC.SetMessage(cm);
-            Task storeResetSignal = Task.Factory.StartNew(() =>
-            {
-                WaitForStoreCompletionSignal(queueCmd, ref parkingSpaceID, ref robotError, 0);
-            });
-            storeResetSignal.Wait();
-            robot.occupied = false;
-            //机械手异常则跳出
-            if (robotError)
+            //Task storeResetSignal = Task.Factory.StartNew(() =>
+            //{
+            if(!WaitForStoreCompletionSignal(queueCmd, ref parkingSpaceID, ref robotError, 0))
             {
-                PLC.SetMessage(new PLCNode(parking_startRobot_address.ToString(), "0"));
-                //把号牌丢回
-                queueCmd.returnedCount += 1;
-                PLC.SetMessage(queueCmd);
-                return;
+                Rollback(queueCmd, parkingSpaceID, true, true);
             }
-            //停车流程结束,将相应车牌复位
-            NumReset(queueCmd);
-            //if (disappeared)
+            //});
+            //storeResetSignal.Wait();
+            robot.occupied = false;
+            //机械手异常则回滚
+            //if (robotError)
             //{
-            //    PLC.SetMessage(new PLCNode(parking_startRobot_address.ToString(), "0"));
-            //    oper.UpdateVehicleParkState(queueCmd.LicenseNum, 0);
-            //    UILogServer.ins.info("抓车失败,状态复位");
+            //    //PLC.SetMessage(new PLCNode(parking_startRobot_address.ToString(), "0"));
+            //    //把号牌丢回
+            //    queueCmd.returnedCount += 1;
+            //    PLC.SetMessage(queueCmd);
             //    return;
             //}
-            Console.WriteLine("停车完成,状态复位");
+            //停车流程结束,将相应车牌复位
+            NumReset(queueCmd);
             Log.WriteLog("停车完成,状态复位");
             UILogServer.ins.info("停车完成,状态复位");
             //根据号牌查找车型
@@ -644,7 +679,7 @@ namespace parkMonitor.server.CoreThread
                     int parkingRecordsID = oper.InsertToParkingRecords(userID, numberPlate, parkingSpaceID, garageID, 3, realParkTime);
                     //车辆表更新车辆信息
                     oper.UpdateVehicle(numberPlate, 1, realParkTime, parkingRecordsID, parkingSpaceID, 1, frontWheelbase, rearWheelbase);
-                              
+
                     //插入消息队列表
                     oper.InsertToMessageQueue(userID, "停车成功", 1);
 
@@ -672,7 +707,7 @@ namespace parkMonitor.server.CoreThread
                     //{
                     //    locationOper.UpdateVehicle(numberPlate, 1, parkingRecordsID, parkingSpaceID, 1);
                     //}
-                               
+
                 }
             }
             //异常写入日志文件
@@ -695,7 +730,7 @@ namespace parkMonitor.server.CoreThread
     /// </summary>
     public class FetchCmd : AbstractCmd
     {
-        private void waitForFetchCompletionSignal(Command queueCmd, ref int parkingSpaceID, ref bool robotError)
+        private bool waitForFetchCompletionSignal(Command queueCmd, ref int parkingSpaceID, ref bool robotError, int status)
         {
             PLCMessage PLCMsg = null;
             MyTimer mt = new MyTimer();
@@ -706,10 +741,10 @@ namespace parkMonitor.server.CoreThread
                 PLCMsg = (PLCMessage)PLC.GetMessage();
                 int fetchingStatus = Convert.ToInt32(PLCMsg.originalPlcList[fetch_completed_address].Value);
                 //取车完成信号
-                if (fetchingStatus == 1)
+                if (fetchingStatus == status)
                 {
                     //取车完成后或可归零
-                    UILogServer.ins.log("获得取车完成信号");
+                    UILogServer.ins.log("获得取车完成信号"+status);
                     break;
                 }
                 else if (1 == 2)//机械手异常,则退指令,写数据库归位,判断条件需之后补充
@@ -738,12 +773,12 @@ namespace parkMonitor.server.CoreThread
                     if (MyTimer.restart && !mt.rolledBack)
                     {
                         mt.rolledBack = true;
-                        //回退
-                        UILogServer.ins.info("回退");
+                        UILogServer.ins.error("等待取车完成信号超时,流程回滚");
+                        return false;
                     }
                 }
             }
-
+            return true;
         }
 
         public override void executeCmd(Command queueCmd)
@@ -779,11 +814,14 @@ namespace parkMonitor.server.CoreThread
 
             int robotID = 0;
             robotID = parkingSpaceID / 15 + 1;
-            Task robotFree = Task.Factory.StartNew(() =>
+            //Task robotFree = Task.Factory.StartNew(() =>
+            //{
+            if (!WaitForRobotResource(robotID))
             {
-                WaitForRobotResource(robotID);
-            });
-            robotFree.Wait();
+                Rollback(queueCmd, parkingSpaceID, true, false);
+            }
+            //});
+            //robotFree.Wait();
             //取车
             int status = 4;
             cm.status = status;
@@ -802,12 +840,15 @@ namespace parkMonitor.server.CoreThread
             Log.WriteLog("取车:" + queueCmd.LicenseNum);
             UILogServer.ins.info("取车:" + queueCmd.LicenseNum);
             //等待PLC取车完成信号
-            Task FetchCompletionSignal = Task.Factory.StartNew(() =>
+            //Task FetchCompletionSignal = Task.Factory.StartNew(() =>
+            //{
+            if(!waitForFetchCompletionSignal(queueCmd, ref parkingSpaceID, ref robotError,1))
             {
-                waitForFetchCompletionSignal(queueCmd,ref parkingSpaceID,ref robotError);
-            });
+                Rollback(queueCmd, parkingSpaceID, true, false);
+            }
+            //});
             //Task.WaitAll(signalFromPLC);
-            FetchCompletionSignal.Wait();
+            //FetchCompletionSignal.Wait();
             //取车完成
             status = 5;
             ControlMessage cm2 = new ControlMessage();
@@ -815,25 +856,16 @@ namespace parkMonitor.server.CoreThread
             //先手动赋值
             cm2.RobotID = 1;
             PLC.SetMessage(cm2);
-            Task signalResetFromPLC = Task.Factory.StartNew(() =>
+            //Task signalResetFromPLC = Task.Factory.StartNew(() =>
+            //{
+            if (!waitForFetchCompletionSignal(queueCmd, ref parkingSpaceID, ref robotError, 0))
             {
-                while (true)
-                {
-                    Thread.Sleep(2000);
-                    PLCMsg = (PLCMessage)PLC.GetMessage();
-                    int fetchingStatus = Convert.ToInt32(PLCMsg.originalPlcList[fetch_completed_address].Value);
-                    //取车完成复位信号
-                    if (fetchingStatus == 0)
-                    {
-                        //取车完成后或可归零
-                        break;
-                    }
-                }
-            });
+                Rollback(queueCmd, parkingSpaceID, true, false);
+            }
+            //});
             //Task.WaitAll(signalFromPLC);
-            signalResetFromPLC.Wait();
+            //signalResetFromPLC.Wait();
             robot.occupied = false;
-            Console.WriteLine("号牌:" + queueCmd.LicenseNum + "取车完成");
             Log.WriteLog("号牌:" + queueCmd.LicenseNum + "取车完成");
             UILogServer.ins.info("号牌:" + queueCmd.LicenseNum + "取车完成");
             //数据库更新

+ 2 - 4
parkMonitor/server/PLCLinker/PLCLinker.cs

@@ -477,8 +477,7 @@ namespace parkMonitor.server
                     if (MyTimer.restart && !mt.rolledBack)
                     {
                         mt.rolledBack = true;
-                        UILogServer.ins.info("回退");
-                        //回退
+                        UILogServer.ins.error("启动前超时未能获取摆扫激光心跳,请检查设备");
                     }
                 }
             }
@@ -835,8 +834,7 @@ namespace parkMonitor.server
                                                         if (MyTimer.restart && !mt.rolledBack)
                                                         {
                                                             mt.rolledBack = true;
-                                                            UILogServer.ins.info("回退");
-                                                            //回退
+                                                            UILogServer.ins.error("发起重测前超时未能获取摆扫激光心跳,请检查设备");
                                                         }
                                                     }
                                                 }