Forráskód Böngészése

核心线程优化

kingwang1995 7 éve
szülő
commit
86635395f3
2 módosított fájl, 173 hozzáadás és 124 törlés
  1. 2 2
      parkMonitor/App.config
  2. 171 122
      parkMonitor/server/CoreThread/AbstractCmd.cs

+ 2 - 2
parkMonitor/App.config

@@ -16,7 +16,7 @@
   </Equipments>
   <appSettings>
     <!--数据库连接配置文件-->
-    <add key="SqlConnectionLocation" value="Data Source=127.0.0.1;port=20000;uid=root;pooling=true;pwd=;database=zxpark;CharSet=utf8;Allow Zero Datetime=true;" />
+    <add key="SqlConnectionLocation" value="Data Source=127.0.0.1;port=20000;uid=root;pooling=true;pwd=jingwang1995;database=zxpark;CharSet=utf8;Allow Zero Datetime=true;" />
     <!--<add key="SqlConnectionStr" value="Data Source=52.77.33.102;port=3306;uid=Ubuntu1;pooling=true;pwd=12345678;database=zxpark;CharSet=utf8;Allow Zero Datetime=true;" />-->
 <add key="SqlConnectionStr" value="Data Source=59.175.148.85;port=3306;uid=root;pooling=true;pwd=x5;database=zxpark;CharSet=utf8;Allow Zero Datetime=true;" /> 
 <!--日志写入地址配置文件-->
@@ -63,7 +63,7 @@
     <add key="licenseTime" value="30" />
     <add key="userTime" value="30" />
     <!--上位机(Web线程)IP地址及配置serverScoket的端口-->
-    <add key="WebConfig" value="192.168.111.17:9000" />
+    <add key="WebConfig" value="192.168.111.84:9000" />
     <!--<add key ="WebConfig" value="127.0.0.1:9000"/>-->
 
     <!--车库入口-->

+ 171 - 122
parkMonitor/server/CoreThread/AbstractCmd.cs

@@ -27,9 +27,9 @@ namespace parkMonitor.server.CoreThread
         public IEquipments queuingThread { set; get; }
         public DBOperation oper { set; get; }
         public IEquipments NumMachine { set; get; }
-        public CEntrance c { set; get; }
+        public CEntrance cEntrance { set; get; }
         public AllotParkingSpace aps { set; get; }
-        public abstract void executeCmd(Command queueCmd);
+
         public int parking_startRobot_address { get; set; }
         public int fetching_startRobot_address { get; set; }
         public DBLocationOperator locationOper { get; set; }
@@ -62,7 +62,7 @@ namespace parkMonitor.server.CoreThread
             //数据库
             oper = new DBOperation();
             //车位分配
-            c = new CEntrance();
+            cEntrance = new CEntrance();
             aps = new AllotParkingSpace();
             robot = new Robot()
             {
@@ -72,9 +72,10 @@ namespace parkMonitor.server.CoreThread
             };
             locationOper = new DBLocationOperator();
         }
-
-        public void WaitForRobotResource(PLCMessage PLCMsg, int robotID)
+        public abstract void executeCmd(Command queueCmd);
+        public void WaitForRobotResource( int robotID)
         {
+            PLCMessage PLCMsg = null;
             while (true)
             {
                 lock (robot)
@@ -162,17 +163,139 @@ namespace parkMonitor.server.CoreThread
             numberMachineMessage.aNode.LicenseNum = queueCmd.LicenseNum;
             queuingThread.SetMessage(numberMachineMessage);
         }
-
-        public override void executeCmd(Command queueCmd)
+        /// <summary>
+        /// 车位资源
+        /// </summary>
+        /// <param name="queueCmd"></param>
+        /// <returns></returns>
+        private Parking_Space WaitForParkingSpaceResource(Command queueCmd)
+        {
+            //中心点
+            cEntrance.parkingEntX = parkingEntX;
+            cEntrance.parkingEntY = parkingEntY;
+            cEntrance.parkingEntZ = parkingEntZ;
+            Parking_Space ppp = new Parking_Space();
+            while (true)
+            {
+                if (!queueCmd.manual)
+                {
+                    ppp = aps.MallocParkingSpace(cEntrance, oper);//自动
+                }
+                else
+                {
+                    ppp = aps.MallocParkingSpace(cEntrance, locationOper);//手动
+                }
+                if (ppp != null)
+                {
+                    break;
+                }
+                if (isClosing)
+                {
+                    break;
+                }
+                Thread.Sleep(200);
+            }
+            return ppp;
+        }
+        private Data WaitForLaserResource(Command queueCmd, bool disappeared)
         {
             //激光数据
-            int centerX = 0;
-            int centerY = 0;
-            int angleA = 0;
-            int length = 0;
-            int width = 0;
-            int height = 0;
+            Data data = new Data();      
+            bool jumpOut = false;
+            PLCMessage PLCMsg = null;
+            jumpOut = false;
+            while (true)
+            {
+                PLCMsg = (PLCMessage)PLC.GetMessage();
+                if (PLCMsg != null)
+                {
+                    foreach (LaserMessage lm in PLCMsg.laserMsgList)
+                    {
+                        if (lm.id == queueCmd.id && lm.data != null)
+                        {
+                            lock (lm)
+                            {
+                                //判断车辆检测结果,
+                                if (disappeared)
+                                {
+                                    lm.abort_rescan = true;
+                                    jumpOut = true;
+                                    break;
+                                }
+                                //存储相应激光数据
+                                if (lm.recorded)
+                                {
+                                    data.angleA = lm.data.angleA; 
+                                    data.centerX = lm.data.centerX;
+                                    data.centerY = lm.data.centerY;
+                                    data.length = lm.data.length;
+                                    data.width = lm.data.width;
+                                    data.height = lm.data.height;
+                                    jumpOut = true;
+                                    lm.recorded = false;
+                                    break;
+                                }
+                                else if (lm.status == -1)
+                                {
+                                    LogFile logFile = LogFile.ERROR;
+                                    UILogServer.ins.error("激光" + lm.id + "连接异常");
+                                    Log.WriteLog(logFile, "激光异常");
+                                    Thread.Sleep(5000);
+                                }
+                            }
+                        }
+                    }
+                    if (jumpOut)
+                    {
+                        break;
+                    }
+                }
+                Thread.Sleep(1000);
+            }
+            return data;
+        }
+        /// <summary>
+        /// 停车完成
+        /// </summary>
+        private void WaitForStoreSignal(Command queueCmd, ref int parkingSpaceID, ref int frontWheelbase, ref int rearWheelbase, ref bool robotError)
+        {
             PLCMessage PLCMsg = null;
+            while (true)
+            {
+                Thread.Sleep(1000);
+                PLCMsg = (PLCMessage)PLC.GetMessage();
+                int storeStatus = Convert.ToInt32(PLCMsg.originalPlcList[park_completed_address].Value);
+                //停车完成信号
+                if (storeStatus == 1)
+                {
+                    frontWheelbase = Convert.ToInt32(PLCMsg.originalPlcList[frontWheelbase_address].Value);
+                    rearWheelbase = Convert.ToInt32(PLCMsg.originalPlcList[rearWheelbase_address].Value);
+                    break;
+                }
+                else if (1 == 2)//机械手异常,则退指令,写数据库归位
+                {
+                    robotError = true;
+                    queueCmd.returnedCount += 1;
+                    queuingThread.SetMessage(queueCmd);
+                    if (!queueCmd.manual)
+                    {
+                        oper.UpdateParkingSpaceState(parkingSpaceID, 0);
+                    }
+                    else
+                    {
+                        locationOper.UpdateParkingSpaceState(parkingSpaceID, 0);
+                    }
+                    break;
+                }
+            }
+        }
+        /// <summary>
+        /// 停车流程
+        /// </summary>
+        /// <param name="queueCmd"></param>
+        public override void executeCmd(Command queueCmd)
+        {        
+            //第一步:
             int status = 1;//停车
             ParkingMessage pm = new ParkingMessage();
             pm.status = status;
@@ -180,33 +303,13 @@ namespace parkMonitor.server.CoreThread
             PLC.SetMessage(pm);
             Console.WriteLine("停车,开激光:" + queueCmd.LicenseNum);
             Log.WriteLog("停车,开激光:" + queueCmd.LicenseNum);
-            UILogServer.ins.info("停车,开激光:" + queueCmd.LicenseNum);
-            c.parkingEntX = parkingEntX;
-            c.parkingEntY = parkingEntY;
-            c.parkingEntZ = parkingEntZ;
+            UILogServer.ins.info("停车,开激光:" + queueCmd.LicenseNum);   
+        
             Parking_Space ppp = new Parking_Space();
             Task parkingSpace = Task.Factory.StartNew(() =>
             {
-                while (true)
-                {
-                    if (!queueCmd.manual)
-                    {
-                        ppp = aps.MallocParkingSpace(c, oper);//自动
-                    }
-                    else
-                    {
-                        ppp = aps.MallocParkingSpace(c, locationOper);//手动
-                    }
-                    if (ppp != null)
-                    {
-                        break;
-                    }
-                    if (isClosing) { return; }
-                    Thread.Sleep(200);
-                }
+                ppp = WaitForParkingSpaceResource(queueCmd);
             });
-            //parkingSpace.Wait();
-            
             //int garageID = ppp.garageID;
             string numberPlate = queueCmd.LicenseNum;
             int userID = 0;
@@ -217,8 +320,6 @@ namespace parkMonitor.server.CoreThread
             }
             int garageID = queueCmd.garageID;
             string realParkTime = queueCmd.TimeRecord;
-
-            bool jumpOut = false;
             bool disappeared = false;
             bool stopChecking = false;
             bool robotError = false;
@@ -249,71 +350,36 @@ namespace parkMonitor.server.CoreThread
             //        t = startTime - endTime;
             //    }
             //});
-
+            Data dataReal = new Data();
             Task dataFromLaser = Task.Factory.StartNew(() =>
             {
-                while (true)
-                {
-                    jumpOut = false;
-                    PLCMsg = (PLCMessage)PLC.GetMessage();
-                    if (PLCMsg != null)
-                    {
-                        foreach (LaserMessage lm in PLCMsg.laserMsgList)
-                        {
-                            if (lm.id == queueCmd.id && lm.data != null)
-                            {
-                                lock (lm)
-                                {
-                                    //判断车辆检测结果,
-                                    if (disappeared)
-                                    {
-                                        lm.abort_rescan = true;
-                                        jumpOut = true;
-                                        break;
-                                    }
-                                    //存储相应激光数据
-                                    if (lm.recorded)
-                                    {
-                                        centerX = lm.data.centerX;
-                                        centerY = lm.data.centerY;
-                                        angleA = lm.data.angleA;
-                                        length = lm.data.length;
-                                        width = lm.data.width;
-                                        height = lm.data.height;
-                                        jumpOut = true;
-                                        lm.recorded = false;
-                                        break;
-                                    }
-                                    else if (lm.status == -1)
-                                    {
-                                        LogFile logFile = LogFile.ERROR;
-                                        UILogServer.ins.error("激光" + lm.id + "连接异常");
-                                        Log.WriteLog(logFile, "激光异常");
-                                        Thread.Sleep(5000);
-                                    }
-                                }
-                            }
-                        }
-                        if (jumpOut)
-                        {
-                            break;
-                        }
-                    }
-                    Thread.Sleep(1000);
-                }
+                dataReal = WaitForLaserResource(queueCmd, disappeared);
             });
-
-            //dataFromLaser.Wait();
-            Task.WaitAll(parkingSpace,dataFromLaser);
+            Task.WaitAll(parkingSpace, dataFromLaser);
+            //车位
             int parkingSpaceID = ppp.parkingSpaceID;
             int parkingSpaceX = ppp.parkingSpaceX;
             int parkingSpaceY = ppp.parkingSpaceY;
             int parkingSpaceZ = ppp.parkingSpaceZ;
-
+            //激光数据
+            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)
             {
                 NumReset(queueCmd);
-                oper.UpdateVehicleParkState(queueCmd.LicenseNum, 0);
+                if (!queueCmd.manual)
+                {
+                    oper.UpdateVehicleParkState(queueCmd.LicenseNum, 0);
+                }
+                else
+                {
+                    locationOper.UpdateVehicleParkState(queueCmd.LicenseNum, 0);
+                }
                 return;
             }
             stopChecking = true;//开始启动机械手,停止检测号牌
@@ -323,9 +389,10 @@ namespace parkMonitor.server.CoreThread
             Task robotFree = Task.Factory.StartNew(() =>
             {
                 //需要先遍历robot数组,根据id找到对应机械手
-                WaitForRobotResource(PLCMsg, robotID);
+                WaitForRobotResource(robotID);
             });
             robotFree.Wait();
+            //第二步:
             status = 2;
             pm.status = status;
             pm.RobotID = robotID;//启动对应机械手
@@ -349,29 +416,10 @@ namespace parkMonitor.server.CoreThread
             int rearWheelbase = 0;
             Task storeSignal = Task.Factory.StartNew(() =>
             {
-                while (true)
-                {
-                    Thread.Sleep(1000);
-                    PLCMsg = (PLCMessage)PLC.GetMessage();
-                    int storeStatus = Convert.ToInt32(PLCMsg.originalPlcList[park_completed_address].Value);
-                    //停车完成信号
-                    if (storeStatus == 1)
-                    {
-                        frontWheelbase = Convert.ToInt32(PLCMsg.originalPlcList[frontWheelbase_address].Value);
-                        rearWheelbase = Convert.ToInt32(PLCMsg.originalPlcList[rearWheelbase_address].Value);
-                        break;
-                    }
-                    else if (1 == 2)//机械手异常,则退指令,写数据库归位
-                    {
-                        robotError = true;
-                        queueCmd.returnedCount += 1;
-                        queuingThread.SetMessage(queueCmd);
-                        oper.UpdateParkingSpaceState(parkingSpaceID, 0);
-                        break;
-                    }
-                }
+                WaitForStoreSignal(queueCmd, ref parkingSpaceID, ref frontWheelbase, ref rearWheelbase, ref robotError);
             });
             storeSignal.Wait();
+            //第三步:
             status = 3;
             pm.status = status;
             PLC.SetMessage(pm);
@@ -410,7 +458,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.UpdateVehicle(numberPlate, 1, realParkTime, parkingRecordsID, parkingSpaceID, 1, frontWheelbase, rearWheelbase);
                     //更新车库表剩余车位数
                     int freeSpaceCount = oper.getGarageFreeSpace(garageID);
                     freeSpaceCount = freeSpaceCount - 1;
@@ -432,7 +480,7 @@ namespace parkMonitor.server.CoreThread
                 else
                 {
                     //插入停车记录表
-                    int parkingRecordsID = locationOper.InsertToParkingRecords(1,userID, numberPlate, parkingSpaceID, garageID, 3, realParkTime);
+                    int parkingRecordsID = locationOper.InsertToParkingRecords(1, userID, numberPlate, parkingSpaceID, garageID, 3, realParkTime);
                     //判断车辆表是否存在车辆
                     bool isNumberPlateFromVehicle = locationOper.IsNumberPlateFromVehicle(numberPlate);
                     if (isNumberPlateFromVehicle == false)
@@ -481,17 +529,17 @@ namespace parkMonitor.server.CoreThread
             int parkingSpaceX = fslist.parkingSpaceX;
             int parkingSpaceY = fslist.parkingSpaceY;
             int parkingSpaceZ = fslist.parkingSpaceZ;
-            int status = 4;//取车
-
             ParkingMessage pm = new ParkingMessage();
+            
             int robotID = 0;
             robotID = parkingSpaceID / 15 + 1;
             Task robotFree = Task.Factory.StartNew(() =>
             {
-                WaitForRobotResource(PLCMsg, robotID);
+                WaitForRobotResource(robotID);
             });
             robotFree.Wait();
-
+            //取车
+            int status = 4;
             pm.status = status;
             //先手动赋值
             pm.RobotID = 1;
@@ -525,6 +573,7 @@ namespace parkMonitor.server.CoreThread
             });
             //Task.WaitAll(signalFromPLC);
             signalFromPLC.Wait();
+            //取车完成
             status = 5;
             ParkingMessage pm2 = new ParkingMessage();
             pm2.status = status;
@@ -569,7 +618,7 @@ namespace parkMonitor.server.CoreThread
                 //更新车辆表车辆停车状态
                 locationOper.UpdateVehicleParkState(queueCmd.LicenseNum, 0);
                 //更新停车记录表
-                locationOper.UpdateParkingRecords(0,6, queueCmd.TimeRecord, queueCmd.parkingRecordsID);
+                locationOper.UpdateParkingRecords(0, 6, queueCmd.TimeRecord, queueCmd.parkingRecordsID);
             }
         }
     }