Browse Source

预约停车入队与数据库操作,地感启动拍号牌并判断预约车辆

yc_t 6 years ago
parent
commit
e7cfbdb697

+ 2 - 2
PLCLinker/centralController/App.config

@@ -8,8 +8,8 @@
     <!--数据库信息-->
     <!--<add key="remoteDBConnStr" value="Data Source=59.175.148.85;port=3306;uid=root;pooling=true;max pool size=1024;pwd=x5;database=zxpark;CharSet=utf8;Allow Zero Datetime=true;"/>-->
     <!--<add key="remoteDBConnStr" value="Data Source=192.168.111.61;port=3306;uid=remote;pooling=true;max pool size=1024;pwd=x5;database=zxpark_cloud_2;CharSet=utf8;Allow Zero Datetime=true;"/>-->
-    <!--<add key="remoteDBConnStr" value="Data Source=127.0.0.1;port=20000;uid=root;pooling=true;max pool size=1024;pwd=yct;database=zxpark_cloud_2;CharSet=utf8;Allow Zero Datetime=true;"/>-->
-    <add key="remoteDBConnStr" value="Data Source=192.168.111.61;port=3306;uid=remote;pooling=true;max pool size=1024;pwd=x5;database=zxpark_cloud_2;CharSet=utf8;Allow Zero Datetime=true;"/>
+    <add key="remoteDBConnStr" value="Data Source=127.0.0.1;port=20000;uid=root;pooling=true;max pool size=1024;pwd=yct;database=zxpark_cloud_2;CharSet=utf8;Allow Zero Datetime=true;"/>
+    <!--<add key="remoteDBConnStr" value="Data Source=192.168.111.61;port=3306;uid=remote;pooling=true;max pool size=1024;pwd=x5;database=zxpark_cloud_2;CharSet=utf8;Allow Zero Datetime=true;"/>-->
     <add key="localDBConnStr" value="Data Source=127.0.0.1;port=20000;uid=root;pooling=true;max pool size=1024;pwd=yct;database=zxpark_local;CharSet=utf8;Allow Zero Datetime=true;"/>
     <add key="DBtimeout" value="3"/>
     <!--号牌机信息-->

+ 2 - 1
PLCLinker/centralController/FormRecordsManager.cs

@@ -62,7 +62,8 @@ namespace centralController
             catch { MessageBox.Show("日期输入异常,请重新输入"); return; }
 
             dgvx_orderRecords.Rows.Clear();
-            List<object[]> list = Monitor.Monitor.GetOrderRecords(license, start.ToString("yyyy-MM-dd"), end.AddDays(1).ToString("yyyy-MM-dd"));
+            List<object[]> list = Monitor.Monitor.GetOrderRecords(true, license, start.ToString("yyyy-MM-dd"), end.AddDays(1).ToString("yyyy-MM-dd"));
+            //if (list.Count != 0) { Console.WriteLine("id:"+(int)(UInt32)list[0][0]); }
             List<object[]>.Enumerator listEnumer = list.GetEnumerator();
             while (listEnumer.MoveNext())
             {

+ 6 - 2
PLCLinker/centralController/Monitor/Monitor.cs

@@ -427,7 +427,7 @@ namespace Monitor
         /// 返回预约记录信息
         /// </summary>
         /// <returns></returns>
-        public static List<object[]> GetOrderRecords(string license="", string startTime = "", string endTime = "")
+        public static List<object[]> GetOrderRecords(bool localDB, string license="", string startTime = "", string endTime = "")
         {
             DateTime now = DateTime.Now;
             List<object[]> result = new List<object[]>();
@@ -449,7 +449,11 @@ namespace Monitor
             {
                 lock (localDBOper)
                 {
-                    MySqlDataReader reader = localDBOper.Query(getOrderRecordsSql);
+                    MySqlDataReader reader;
+                    if (localDB)
+                        reader = localDBOper.Query(getOrderRecordsSql);
+                    else
+                        reader = remoteDBOper.Query(getOrderRecordsSql);
                     try
                     {
                         while (reader != null && reader.Read())

+ 2 - 2
PLCLinker/centralController/Monitor/SystemInitializer.cs

@@ -84,8 +84,8 @@ namespace Monitor
 
             //初始化web对象
             Monitor.SetNotification("初始化云端连接...");
-            //Monitor.webServer = new CentralForWeb();
-            Monitor.webServer = new MyWebServer();
+            Monitor.webServer = new CentralForWeb();
+            //Monitor.webServer = new MyWebServer();
             initStatus = initStatus && Monitor.webServer.Start(Monitor.webPort);
             //webServer初始化结束,进入状态4
             if (!initStatus) { return; }

+ 210 - 155
PLCLinker/centralController/Terminal/Terminal.cs

@@ -16,9 +16,16 @@ namespace Terminal
     public class Terminal
     {
         public static List<TerminalStru> terminalInfo = new List<PLCS7.TerminalStru>();
+        /// <summary>
+        /// 记录地面停车位id与获取号牌映射
+        /// </summary>
         private static Dictionary<int, string> idLicMap = new Dictionary<int, string>();
+        /// <summary>
+        /// 取车地面位id与是否已计费标记映射
+        /// </summary>
         public static Dictionary<int, bool> termUsedMap = new Dictionary<int, bool>();
         private static Dictionary<int, FetchInfo> fetchMap = new Dictionary<int, FetchInfo>();
+        private static List<object> lockList = new List<object>();
         public static bool isClosing = false;
 
         public void FeeCal()
@@ -396,105 +403,108 @@ namespace Terminal
                 }
             }
         }
+        #region 早期获取号牌方法,已弃用
         /// <summary>
         /// 停车流程,收到号牌机启动后操作过程
         /// </summary>
-        private static void ParkNumSubProcess()
-        {
-            //int numMachineLaunch = Monitor.Monitor.mainBlockInfo.numMachineLaunch;
-            //if (numMachineLaunch != 0)
-            //{
-            for (int i = 0; i < terminalInfo.Count; i++)
-            {
-                //启动指令与终端id匹配
-                //if (numMachineLaunch == terminalInfo[i].terminalID)
-                if (terminalInfo[i].numMachineLaunch == 1 && !idLicMap.ContainsKey(terminalInfo[i].terminalID) && terminalInfo[i].licVerification == 0)
-                {
-                    int numReceivedStatus = 0;//1获得,2终止
-                    TerminalStru term = terminalInfo[i];
-                    if (term.terminalStatus == (short)1)
-                    {
-                        string license = Monitor.Monitor.numMachineLinker.GetLicensePlate(terminalInfo[i].terminalID);
-                        Monitor.Monitor.SetNotification("启动" + terminalInfo[i].terminalID + "号号牌机,号牌:" + license);
-                        //未获得号牌,告知PLC终止,告诉终端提示用户重新操作
-                        if (license == "")
-                        {
-                            MainBlockStru mb = new MainBlockStru
-                            {
-                                licenseReceived = (short)2
-                            };
-                            Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
-                            TerminalStru ts = new TerminalStru
-                            {
-                                terminalID = (short)terminalInfo[i].terminalID,
-                                paymentStatus = -1,
-                                parkingFee = -1,
-                                userType = -1,
-                                licVerification = (short)2
-                            };
-                            Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
-                            Log.WriteLog(LogType.process, LogFile.ERROR, "未获得号牌");
-                        }
-                        else
-                        {
-                            //记录或更新当前号牌
-                            lock (idLicMap)
-                            {
-                                if (idLicMap.ContainsKey(terminalInfo[i].terminalID))
-                                {
-                                    idLicMap[terminalInfo[i].terminalID] = license;
-                                }
-                                else
-                                {
-                                    idLicMap.Add(terminalInfo[i].terminalID, license);
-                                }
-                            }
-                            Log.WriteLog(LogType.process, LogFile.ERROR, "号牌" + license + "已记录");
+        //private static void ParkNumSubProcess()
+        //{
+        //    //int numMachineLaunch = Monitor.Monitor.mainBlockInfo.numMachineLaunch;
+        //    //if (numMachineLaunch != 0)
+        //    //{
+        //    for (int i = 0; i < terminalInfo.Count; i++)
+        //    {
+        //        //启动指令与终端id匹配
+        //        //if (numMachineLaunch == terminalInfo[i].terminalID)
+        //        if (terminalInfo[i].numMachineLaunch == 1 && !idLicMap.ContainsKey(terminalInfo[i].terminalID) && terminalInfo[i].licVerification == 0)
+        //        {
+        //            int numReceivedStatus = 0;//1获得,2终止
+        //            TerminalStru term = terminalInfo[i];
+        //            if (term.terminalStatus == (short)1)
+        //            {
+        //                string license = Monitor.Monitor.numMachineLinker.GetLicensePlate(terminalInfo[i].terminalID);
+        //                Monitor.Monitor.SetNotification("启动" + terminalInfo[i].terminalID + "号号牌机,号牌:" + license);
+        //                //未获得号牌,告知PLC终止,告诉终端提示用户重新操作
+        //                if (license == "")
+        //                {
+        //                    MainBlockStru mb = new MainBlockStru
+        //                    {
+        //                        licenseReceived = (short)2
+        //                    };
+        //                    Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
+        //                    TerminalStru ts = new TerminalStru
+        //                    {
+        //                        terminalID = (short)terminalInfo[i].terminalID,
+        //                        paymentStatus = -1,
+        //                        parkingFee = -1,
+        //                        userType = -1,
+        //                        licVerification = (short)2
+        //                    };
+        //                    Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
+        //                    Log.WriteLog(LogType.process, LogFile.ERROR, "未获得号牌");
+        //                }
+        //                else
+        //                {
+        //                    //记录或更新当前号牌
+        //                    lock (idLicMap)
+        //                    {
+        //                        if (idLicMap.ContainsKey(terminalInfo[i].terminalID))
+        //                        {
+        //                            idLicMap[terminalInfo[i].terminalID] = license;
+        //                        }
+        //                        else
+        //                        {
+        //                            idLicMap.Add(terminalInfo[i].terminalID, license);
+        //                        }
+        //                    }
+        //                    Log.WriteLog(LogType.process, LogFile.ERROR, "号牌" + license + "已记录");
+
+        //                    TerminalStru ts = new TerminalStru
+        //                    {
+        //                        terminalID = (short)terminalInfo[i].terminalID,
+        //                        paymentStatus = -1,
+        //                        parkingFee = -1,
+        //                        userType = -1
+        //                    };
+        //                    //注册用户
+        //                    if (term.btnStatus == (short)2)
+        //                    {
+        //                        int userID = term.licenseCodeA;
+        //                        string userLicense = GetLicenseFromTerm(term);
+        //                        //与云端数据比对
+        //                        ValidateUserInfo(ts, userID, userLicense, out numReceivedStatus);
+        //                        if (numReceivedStatus == 2)
+        //                        {
+        //                            idLicMap.Remove(terminalInfo[i].terminalID);
+        //                        }
+        //                    }
+        //                    //非注册用户
+        //                    else if (term.btnStatus == (short)1)
+        //                    {
+        //                        numReceivedStatus = 1;
+        //                        ts.licVerification = 1;
+        //                        Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
+        //                    }
+        //                    //无论是否注册,皆告知PLC,已获取号牌或比对异常、终止流程
+        //                    MainBlockStru mb = new MainBlockStru
+        //                    {
+        //                        licenseReceived = (short)numReceivedStatus
+        //                    };
+        //                    Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
+        //                    //号牌获取结果被清零才跳出
+        //                    Monitor.Monitor.SetNotification("已写入号牌获取结果,等待号牌机启动指令清零");
+        //                }
+        //                while (term.numMachineLaunch != 0)
+        //                {
+        //                    Thread.Sleep(1000);
+        //                }
+        //                Monitor.Monitor.SetNotification("号牌机启动指令已被清零");
+        //            }
+        //        }
+        //    }
+        //}
+        #endregion
 
-                            TerminalStru ts = new TerminalStru
-                            {
-                                terminalID = (short)terminalInfo[i].terminalID,
-                                paymentStatus = -1,
-                                parkingFee = -1,
-                                userType = -1
-                            };
-                            //注册用户
-                            if (term.btnStatus == (short)2)
-                            {
-                                int userID = term.licenseCodeA;
-                                string userLicense = GetLicenseFromTerm(term);
-                                //与云端数据比对
-                                ValidateUserInfo(ts, userID, userLicense, out numReceivedStatus);
-                                if (numReceivedStatus == 2)
-                                {
-                                    idLicMap.Remove(terminalInfo[i].terminalID);
-                                }
-                            }
-                            //非注册用户
-                            else if (term.btnStatus == (short)1)
-                            {
-                                numReceivedStatus = 1;
-                                ts.licVerification = 1;
-                                Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
-                            }
-                            //无论是否注册,皆告知PLC,已获取号牌或比对异常、终止流程
-                            MainBlockStru mb = new MainBlockStru
-                            {
-                                licenseReceived = (short)numReceivedStatus
-                            };
-                            Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
-                            //号牌获取结果被清零才跳出
-                            Monitor.Monitor.SetNotification("已写入号牌获取结果,等待号牌机启动指令清零");
-                        }
-                        while (Monitor.Monitor.mainBlockInfo.numMachineLaunch != 0)
-                        {
-                            Thread.Sleep(1000);
-                        }
-                        Monitor.Monitor.SetNotification("号牌机启动指令已被清零");
-                    }
-                }
-            }
-        }
         /// <summary>
         /// 停车流程,收到PLC停车完成信号后操作过程
         /// </summary>
@@ -594,7 +604,99 @@ namespace Terminal
                 }
             }
         }
+        /// <summary>
+        /// 预约车辆检测
+        /// </summary>
+        private static void ReservedCarCheck()
+        {
+            if (lockList.Count != terminalInfo.Count)
+            {
+                lockList.Clear();
+                for (int i = 0; i < terminalInfo.Count; i++)
+                {
+                    lockList.Add(new object());
+                }
+            }
 
+            //找到停车终端,且有地感无按钮数据
+            for (int i = 0; i < terminalInfo.Count; i++)
+            {
+                if (terminalInfo[i].terminalStatus == 1 && terminalInfo[i].groundStatus == 1 && terminalInfo[i].btnStatus == 0)
+                {
+                    Task.Factory.StartNew(() =>
+                    {
+                        lock (lockList[i])
+                        {
+                            string license = "";
+                            int countdown = 2;
+                            short registered = 4;
+                            while (license == "" && countdown-- > 0)
+                            {
+                                license = Monitor.Monitor.numMachineLinker.GetLicensePlate(terminalInfo[i].terminalID);
+                            }
+                            if (license != "")
+                            {
+                                //map中加入或更新号牌,供完成时写入数据库用
+                                if (idLicMap.ContainsKey(terminalInfo[i].terminalID))
+                                {
+                                    idLicMap[terminalInfo[i].terminalID] = license;
+                                }
+                                else
+                                {
+                                    idLicMap.Add(terminalInfo[i].terminalID, license);
+                                }
+                                if (Monitor.Monitor.webServer.ReservedCarCheck(license))
+                                {
+                                    registered = 3;
+                                }
+                                #region 通过数据库查询是否预约车,方法待定
+                                //int vehicleState = -1;
+                                //string vehicleStateCheckSql = "select vehiclepParkState from vehicle where numberPlate = '" + license + "';";
+                                //MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(vehicleStateCheckSql);
+                                //if (reader != null)
+                                //{
+                                //    try
+                                //    {
+                                //        if (reader.Read())
+                                //        {
+                                //            vehicleState = reader.GetInt32("vehiclepParkState");
+                                //        }
+                                //    }
+                                //    catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "预约检测,查询车辆状态异常"); ; Console.WriteLine(e.Message); };
+                                //    try
+                                //    {
+                                //        reader.Close();
+                                //        reader.Dispose();
+                                //    }
+                                //    catch { }
+                                //}
+                                //已预约车辆入场
+                                //if (vehicleState == 4)
+                                //{
+                                //    registered = 3;
+                                //}
+                                #endregion
+                                TerminalStru ts = new TerminalStru
+                                {
+                                    terminalID = terminalInfo[i].terminalID,
+                                    paymentStatus = (short)-1,
+                                    btnStatus = registered,
+                                    cmd = (short)-1,
+                                    licenseCodeA = -1,
+                                    licenseCodeB = -1,
+                                    licenseCodeC = -1,
+                                    licenseCodeD = -1,
+                                    receiptNum = -1
+                                };
+                                Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.terminal);
+                            }
+                            Thread.Sleep(500);
+                        }
+                    });
+                    
+                }
+            }
+        }
         /// <summary>
         /// 停车相关操作
         /// 0.观测到地感信号,且按钮状态不为3,则启动号牌机获取号牌,并判断是否预约车
@@ -606,21 +708,21 @@ namespace Terminal
         /// 
         private static void ParkProcess()
         {
-            Task.Factory.StartNew(() =>
-            {
-                while (!isClosing)
-                {
-                    ParkNumSubProcess();
-                    Thread.Sleep(Monitor.Monitor.plcRefreshInterval*5);
-                }
-            });
+            //Task.Factory.StartNew(() =>
+            //{
+            //    while (!isClosing)
+            //    {
+            //        ParkNumSubProcess();
+            //        Thread.Sleep(Monitor.Monitor.plcRefreshInterval*5);
+            //    }
+            //});
 
             Task.Factory.StartNew(() =>
             {
                 while (!isClosing)
                 {
                     ParkCompleteSubProcess();
-                    Thread.Sleep(Monitor.Monitor.plcRefreshInterval*5);
+                    Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
                 }
             });
 
@@ -629,7 +731,7 @@ namespace Terminal
                 while (!isClosing)
                 {
                     ReservedCarCheck();
-                    Thread.Sleep(Monitor.Monitor.plcRefreshInterval*5);
+                    Thread.Sleep(Monitor.Monitor.plcRefreshInterval * 5);
                 }
             });
         }
@@ -1072,59 +1174,12 @@ namespace Terminal
 
         }
 
-        private static void ReservedCarCheck()
+        public static void Start()
         {
-            //找到停车终端,且有地感
-            for (int i = 0; i < terminalInfo.Count; i++)
+            for (int i = 0; i < Monitor.Monitor.plcTerminalCount; i++)
             {
-                if (terminalInfo[i].terminalStatus == 1 && terminalInfo[i].groundStatus == 1 && terminalInfo[i].btnStatus == 0)
-                {
-                    string license = Monitor.Monitor.numMachineLinker.GetLicensePlate(terminalInfo[i].terminalID);
-                    int vehicleState = -1;
-                    string vehicleStateCheckSql = "select vehiclepParkState from vehicle where numberPlate = '" + license + "';";
-                    MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(vehicleStateCheckSql);
-                    if (reader != null)
-                    {
-                        try
-                        {
-                            if (reader.Read())
-                            {
-                                vehicleState = reader.GetInt32("vehiclepParkState");
-                            }
-                        }
-                        catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "预约检测,查询车辆状态异常"); ; Console.WriteLine(e.Message); };
-                        try
-                        {
-                            reader.Close();
-                            reader.Dispose();
-                        }
-                        catch { }
-                    }
-                    short registered = 4;
-                    //已预约车辆入场
-                    if (vehicleState == 4)
-                    {
-                        registered = 3;
-                    }
-                    TerminalStru ts = new TerminalStru
-                    {
-                        terminalID = terminalInfo[i].terminalID,
-                        paymentStatus = (short)-1,
-                        btnStatus = registered,
-                        cmd = (short)-1,
-                        licenseCodeA = -1,
-                        licenseCodeB = -1,
-                        licenseCodeC = -1,
-                        licenseCodeD = -1,
-                        receiptNum = -1
-                    };
-                    Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.terminal);
-                }
+                lockList.Add(new object());
             }
-        }
-
-        public static void Start()
-        {
             ParkProcess();
             FetchProcess();
         }

+ 7 - 2
PLCLinker/centralController/WebServer/CentralForWebSocketServer.cs

@@ -239,16 +239,21 @@ namespace centralController.WebServer
         /// <summary>
         /// 预约停车记录
         /// </summary>
-        public void bookParkRecord()
+        public void BookParkRecord()
         {
 
         }
         /// <summary>
         /// 预约取车记录
         /// </summary>
-        public void bookFetchRecord()
+        public void BookFetchRecord()
         {
 
         }
+
+        public bool ReservedCarCheck(string license)
+        {
+            throw new NotImplementedException();
+        }
     }
 }

+ 4 - 2
PLCLinker/centralController/WebServer/IWebServer.cs

@@ -12,9 +12,11 @@ namespace centralController.WebServer
 
         void Stop();
 
-        void bookParkRecord();
+        void BookParkRecord();
 
-        void bookFetchRecord();
+        void BookFetchRecord();
+
+        bool ReservedCarCheck(string license);
 
     }
 }

+ 187 - 9
PLCLinker/centralController/WebServer/WebServer.cs

@@ -1,4 +1,5 @@
-using nettyCommunication;
+using centralController.model;
+using nettyCommunication;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -13,26 +14,146 @@ namespace centralController.WebServer
         private Queue<MessageUTF8> reserveQueue = null;
         private object reserveLock = new object();
         private Communication comm = null;
-        private bool isClosing;
-        public void bookFetchRecord()
+        private bool isClosing { get; set; }
+        private bool connected { get; set; }
+
+        public void BookFetchRecord()
         {
             throw new NotImplementedException();
         }
 
-        public void bookParkRecord()
+        public void BookParkRecord()
         {
             throw new NotImplementedException();
         }
 
+        private bool UpdateVehicleState(bool localDB, int state, int orderRecordsID, string license)
+        {
+            string vehicleUpdateSql = "";
+            string vehicleInsertSql = "";
+            if (state >= 0)
+            {
+                vehicleUpdateSql = "update vehicle set vehiclepParkState = " + state + " ,orderRecordsID = " + orderRecordsID + " where numberPlate = '" + license + "';";
+                vehicleInsertSql = "insert into vehicle (numberPlate,vehiclepParkState,orderRecordsID) values " +
+                        "('" + license + "'," + state + "," + orderRecordsID + ");";
+            }
+            else
+            {
+                vehicleUpdateSql = "update vehicle set orderRecordsID = " + orderRecordsID + " where numberPlate = '" + license + "';";
+                vehicleInsertSql = "insert into vehicle (numberPlate,orderRecordsID) values " +
+                        "('" + license + "'," + orderRecordsID + ");";
+            }
+            List<string> vehicleUpdateList = new List<string>();
+            List<string> vehicleInsertList = new List<string>();
+            vehicleUpdateList.Add(vehicleUpdateSql);
+            vehicleInsertList.Add(vehicleInsertSql);
+            if (localDB)
+            {
+                if (!Monitor.Monitor.localDBOper.UpdateTransaction(vehicleUpdateList))
+                {
+                    if (!Monitor.Monitor.localDBOper.Insert(vehicleInsertList))
+                        return false;
+                    else
+                        return true;
+                }
+                else return true;
+            }
+            else
+            {
+                if (!Monitor.Monitor.remoteDBOper.UpdateTransaction(vehicleUpdateList))
+                {
+                    if (!Monitor.Monitor.remoteDBOper.Insert(vehicleInsertList))
+                        return false;
+                    else
+                        return true;
+                }
+                else return true;
+            }
+        }
+
+        private bool OrderDBOperation(bool localDB, string userID, bool parking, string license, string orderTime, int orderLength)
+        {
+            //预约记录插入db
+            string orderRecordInsertSql;
+            if (parking)
+            {
+                orderRecordInsertSql = "insert into orderrecords (userID,numberPlate,garageID, bookParkTime, bookHour, bookPrice,bookState) " +
+                "VALUES (" + userID + ", '" + license + "', '" + Monitor.Monitor.garageID + "', '" + orderTime + "', " + orderLength + ",NULL, '0');";
+            }
+            else
+            {
+                orderRecordInsertSql = "insert into orderrecords (userID,numberPlate,garageID, bookFetchTime, bookHour, bookPrice,bookState) " +
+                "VALUES (" + userID + ", '" + license + "', '" + Monitor.Monitor.garageID + "', '" + orderTime + "', " + orderLength + ",NULL, '2');";
+            }
+            List<string> orderList = new List<string>();
+            orderList.Add(orderRecordInsertSql);
+            if (localDB)
+                Monitor.Monitor.localDBOper.Insert(orderList);
+            else
+                Monitor.Monitor.remoteDBOper.Insert(orderList);
+            //查询预约记录id号
+            int currentID = 0;
+            List<object[]> orderRecords = Monitor.Monitor.GetOrderRecords(localDB, license, DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"));
+            if (orderRecords.Count != 0)
+            {
+                currentID = (int)(UInt32)orderRecords[0][0];
+            }
+            else { /*反馈web,预约失败*/ return false; }
+            //更新车辆状态
+            UpdateVehicleState(localDB, parking ? 4 : 5, currentID, license);
+            return true;
+        }
+
+        private void MsgHandling(MessageUTF8 msg)
+        {
+            switch (msg.cmd)
+            {
+                //预约停
+                case "a":
+                    lock (reserveLock)
+                    {
+                        reserveQueue.Enqueue(msg);
+                    }
+                    //通知PLC减少可预约车位数
+                    //预约记录与车辆状态写入数据库
+                    OrderDBOperation(true, msg.sender, true, msg.context, msg.bookTime, 3);
+                    OrderDBOperation(false, msg.sender, true, msg.context, msg.bookTime, 3);
+                    break;
+                //预约取
+                case "b":
+                    break;
+                //更新广告
+                case "c":
+                    string adAlert = "";
+                    bool result = Monitor.Monitor.advertMgr.UpdateAdvert(out adAlert);
+                    if (!result)
+                    {
+                        Monitor.Monitor.SetNotification("广告更新失败,请尝试手动更新");
+                    }
+                    else
+                    {
+                        Monitor.Monitor.SetNotification("广告更新成功\n" + adAlert);
+                    }
+                    break;
+                default:
+                    Monitor.Monitor.SetNotification("接收到无法识别的指令");
+                    break;
+            }
+        }
+        /// <summary>
+        /// 启动消息接收,启动超时指令处理
+        /// </summary>
+        /// <param name="port"></param>
+        /// <returns></returns>
         public bool Start(int port)
         {
             isClosing = false;
-            bool connected = false;
+
             MessageUTF8 message = new MessageUTF8();
             message.context = "sending message test";
             message.cmd = "S";
             message.parkingRecordsID = 1;
-            
+
             Task.Factory.StartNew(() =>
             {
                 //初始化后与web持续连接
@@ -52,18 +173,21 @@ namespace centralController.WebServer
                     }
                     Thread.Sleep(1500);
                 }
-
+                //持续监听
                 while (!isClosing)
                 {
                     if (Connections.isAlive())
                     {
-                        if(!connected)
+                        if (!connected)
+                        {
                             comm = new Communication();
+                        }
                         connected = true;
                         if (comm != null)
                         {
                             //comm.SendMessage(message);//消息
                             MessageUTF8 msg = ((MessageUTF8)comm.ReceiveMessage());
+                            MsgHandling(msg);
                             Console.WriteLine(msg.context);
                         }
                     }
@@ -82,14 +206,68 @@ namespace centralController.WebServer
                     }
                     Thread.Sleep(1000);
                 }
-            }).Wait();
+            });
+            //处理超时预约指令
+            Task.Factory.StartNew(() =>
+            {
+                while (!isClosing)
+                {
+                    lock (reserveLock)
+                    {
+                        for (int i = 0; i < reserveQueue.Count; i++)
+                        {
+                            try
+                            {
+                                MessageUTF8 msg = reserveQueue.Dequeue();
+                                DateTime startTime = DateTime.Parse(msg.bookTime);
+                                //if ((DateTime.Now - startTime).TotalMinutes > msg.bookLength * 60)
+                                if ((DateTime.Now - startTime).TotalMinutes > 3 * 60)
+                                {
+                                    //通知PLC将可预约车位数恢复一个
+                                }
+                                else
+                                {
+                                    reserveQueue.Enqueue(msg);
+                                }
+                            }
+                            catch { }
+                        }
+                    }
+                    Thread.Sleep(5000);
+                }
+            });
             return true;
         }
 
         public void Stop()
         {
             isClosing = true;
+            Connections.close();
             //throw new NotImplementedException();
         }
+        /// <summary>
+        /// 预约车辆检测
+        /// </summary>
+        /// <param name="license"></param>
+        /// <returns></returns>
+        public bool ReservedCarCheck(string license)
+        {
+            lock (reserveLock)
+            {
+                for (int i = 0; i < reserveQueue.Count; i++)
+                {
+                    MessageUTF8 msg = reserveQueue.Dequeue();
+                    if (msg.context == license)
+                    {
+                        return true;
+                    }
+                    else
+                    {
+                        reserveQueue.Enqueue(msg);
+                    }
+                }
+            }
+            return false;
+        }
     }
 }

+ 1 - 1
PLCLinker/centralController/entity/AbstractMessage.cs

@@ -33,7 +33,7 @@ namespace parkMonitor.entity
         /// <summary>
         /// 预约起始时间
         /// </summary>
-        public string bookTime { get; set; }
+        public string bookTime{ get; set; }
         /// <summary>
         /// 预约时长
         /// </summary>

BIN
PLCLinker/centralController/sdk/dotNetty/nettyCommunication.dll


BIN
PLCLinker/centralController/sdk/mysql/DatabaseDLL.dll


BIN
PLCLinker/centralController/sdk/mysql/db.dll


BIN
PLCLinker/centralController/sdk/mysql/db2.dll