فهرست منبع

停车流程完善,取车计费部分完成

yc_t 6 سال پیش
والد
کامیت
70469c391f

BIN
PLCLinker/.vs/PLCLinker/v15/.suo


BIN
PLCLinker/.vs/PLCLinker/v15/Server/sqlite3/storage.ide


+ 41 - 11
PLCLinker/centralController/Monitor/SystemInitializer.cs

@@ -6,10 +6,12 @@ using System.Text;
 using System.Threading.Tasks;
 using BroadcastModule;
 using DatabaseDLL;
+using MySql.Data.MySqlClient;
 using NumMachine;
 using parkMonitor.tools;
 using PLCS7;
 using S7.Net;
+using Terminal;
 using WebServer;
 
 namespace Monitor
@@ -48,7 +50,7 @@ namespace Monitor
                 Monitor.allInOneMachinePort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("allInOneMachinePort"));
                 //webServer端口
                 Monitor.webPort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("webPort"));
-                
+
                 Monitor.garageID = Convert.ToInt32(ConfigurationManager.AppSettings.Get("garageID"));
                 //配置文件读取结束,进入状态1
                 Monitor.initializeState = 1;
@@ -56,10 +58,10 @@ namespace Monitor
             }
             catch (Exception e) { Console.WriteLine("初始化," + e.Message); return; }
 
-            initStatus = initStatus && PLCInit(retryCount);
+            //initStatus = initStatus && PLCInit(retryCount);
             //PLC初始化结束,进入状态2
-            if (!initStatus) { return; }
-            else { Monitor.initializeState = 2;}
+            //if (!initStatus) { return; }
+            //else { Monitor.initializeState = 2; }
 
             //初始化数据库对象
             Monitor.remoteDBOper = new DBOperation(remoteDBConnStr, DBtimeout);
@@ -77,18 +79,46 @@ namespace Monitor
             if (!initStatus) { return; }
             else { Monitor.initializeState = 4; }
 
+            ////*******测试数据库操作
+            string sql = "select vehicleTypeID,numberPlate from vehicle where numberPlate = '鄂A11111';";
+            MySqlDataReader reader = null;
+            object[] objs = null;
+            try
+            {
+                reader = Monitor.remoteDBOper.Query(sql);
+                if (reader!= null && reader.Read())
+                {
+                    objs = new object[reader.FieldCount]; 
+                    reader.GetValues(objs);
+                    Console.WriteLine("-------------");
+                    for (int i = 0; i < objs.Length; i++)
+                    {
+                        Console.WriteLine(objs[i] + ",");
+                    }
+                }
+            }
+            catch (Exception e) { Console.WriteLine(e.Message); }
+
+            //初始化计费策略
+            PaymentScheme.ins = PaymentScheme.GetCurrentPaymentScheme(1);
             //初始化号牌机对象
             Monitor.numMachineLinker = new NumMachineLinker(flpHandle);
             Monitor.numMachineLinker.Start();
             //初始化显示板对象,显示板udp面向无连接
-            //Monitor.allInOneMachine = new BroadcastBoard(Monitor.allInOneMachineIP, Monitor.allInOneMachinePort);
-            //Monitor.allInOneMachine.Refresh();
+            Monitor.allInOneMachine = new BroadcastBoard(Monitor.allInOneMachineIP, Monitor.allInOneMachinePort);
+            Monitor.allInOneMachine.UpdateTime();
+            Monitor.allInOneMachine.Play(0, BroadcastBoard.PlayMode.download, "欢迎光临");
+            Monitor.allInOneMachine.Play(1, BroadcastBoard.PlayMode.download, "智象停车");
+            Monitor.allInOneMachine.Play(2, BroadcastBoard.PlayMode.download, "date");
+            Monitor.allInOneMachine.Play(3, BroadcastBoard.PlayMode.download, "time");
+            Monitor.allInOneMachine.Refresh();
             //系统初始化结束,进入状态5
             if (!initStatus) { return; }
             else { Monitor.initializeState = 5; }
             mt.EndTiming();
-            Console.WriteLine("初始化耗时:"+mt.GetInterval().TotalSeconds);
-            switch(Monitor.initializeState){
+            Console.WriteLine("初始化耗时:" + mt.GetInterval().TotalSeconds);
+            switch (Monitor.initializeState)
+            {
                 case 0:
                     Console.WriteLine("配置文件读写异常");
                     break;
@@ -122,7 +152,7 @@ namespace Monitor
             {
                 if (Monitor.PLC == null)
                 {
-                    Monitor.PLC = new PLCLinker(CpuType.S71500, Monitor.plcIPAddr, (short)Monitor.plcRack, (short)Monitor.plcSlot, 
+                    Monitor.PLC = new PLCLinker(CpuType.S71500, Monitor.plcIPAddr, (short)Monitor.plcRack, (short)Monitor.plcSlot,
                         Int32.Parse(Monitor.plcDatablockConfig[0]), Int32.Parse(Monitor.plcDatablockConfig[1]), Int32.Parse(Monitor.plcDatablockConfig[2]),
                         Monitor.plcTerminalCount, Monitor.plcParkingSpaceCount);
                 }
@@ -140,9 +170,9 @@ namespace Monitor
         internal void Stop()
         {
             //关闭号牌机
-            if (Monitor.numMachineLinker != null) { Monitor.numMachineLinker.Stop();}
+            if (Monitor.numMachineLinker != null) { Monitor.numMachineLinker.Stop(); }
             //关闭webServer
-            if (Monitor.webServer != null) { Monitor.webServer.Stop();}
+            if (Monitor.webServer != null) { Monitor.webServer.Stop(); }
             //关闭数据库
             if (Monitor.remoteDBOper != null) { Monitor.remoteDBOper.DBClose(); }
             if (Monitor.localDBOper != null) { Monitor.localDBOper.DBClose(); }

+ 289 - 18
PLCLinker/centralController/Terminal/Terminal.cs

@@ -106,7 +106,7 @@ namespace Terminal
                 Monitor.Monitor.localDBOper.UpdateTransaction(updateSpaceList);
                 Monitor.Monitor.localDBOper.Insert(insertSpaceList);
             }
-            catch (Exception e) { Console.WriteLine("更新本地所有车位异常"+e.Message); }
+            catch (Exception e) { Console.WriteLine("更新本地所有车位异常" + e.Message); }
         }
         /// <summary>
         /// 更新车辆状态
@@ -189,11 +189,29 @@ namespace Terminal
                     //启动指令与终端id匹配
                     if (numMachineLaunch == terminalInfo[i].terminalID)
                     {
+                        int numReceivedStatus = 0;//1获得,2终止
                         TerminalStru term = terminalInfo[i];
                         if (term.terminalStatus == (short)0)
                         {
                             string license = Monitor.Monitor.numMachineLinker.GetLicensePlate(numMachineLaunch);
-                            if (license != "")
+                            //未获得号牌,告知PLC终止,告诉终端提示用户重新操作
+                            if (license == "")
+                            {
+                                MainBlockStru mb = new MainBlockStru
+                                {
+                                    licenseReceived = (short)2
+                                };
+                                Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
+                                TerminalStru ts = new TerminalStru
+                                {
+                                    paymentStatus = -1,
+                                    parkingFee = -1,
+                                    userType = -1,
+                                    licVerification = (short)2
+                                };
+                                Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
+                            }
+                            else
                             {
                                 //记录或更新当前号牌
                                 lock (idLicMap)
@@ -228,34 +246,30 @@ namespace Terminal
                                         {
                                             ts.licVerification = 2;//验证失败
                                             Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
+                                            numReceivedStatus = 2;
                                         }
                                         else
                                         {
                                             ts.licVerification = 1;//验证成功
                                             Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
+                                            numReceivedStatus = 1;
                                         }
                                     }
                                     else
                                     {
                                         ts.licVerification = 1;//网络异常,跳过验证
                                         Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
+                                        numReceivedStatus = 1;
                                     }
-                                    //告知PLC获取号牌
-                                    MainBlockStru mb = new MainBlockStru
-                                    {
-                                        licenseReceived = (short)1
-                                    };
-                                    Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
+
                                 }
-                                //非注册
-                                else
+                                //无论是否注册,皆告知PLC,已获取号牌或比对异常、终止流程
+                                MainBlockStru mb = new MainBlockStru
                                 {
-                                    MainBlockStru mb = new MainBlockStru
-                                    {
-                                        licenseReceived = (short)1
-                                    };
-                                    Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
-                                }
+                                    licenseReceived = (short)numReceivedStatus
+                                };
+                                Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
+
                             }
                         }
                     }
@@ -312,7 +326,9 @@ namespace Terminal
                         string findRecordSql = "select parkingRecordsID from parkingrecords where receiptNum = " + term.receiptNum + ";";
                         MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(findRecordSql);
                         int parkingRecordsID = 0;
-                        try { parkingRecordsID = reader.GetInt32("parkingRecordsID"); } catch (Exception) { break; };
+                        try {
+                            if (reader!=null && reader.Read()) { parkingRecordsID = reader.GetInt32("parkingRecordsID"); }
+                        } catch (Exception) { break; };
                         //更新本地车辆表
                         UpdateVehicle(license, parkingRecordsID, 1, true, false);
                         //更新本地车位
@@ -346,7 +362,8 @@ namespace Terminal
                 }
             });
 
-            Task.Factory.StartNew(()=> {
+            Task.Factory.StartNew(() =>
+            {
                 while (!isClosing)
                 {
                     ParkCompleteSubProcess();
@@ -354,12 +371,108 @@ namespace Terminal
                 }
             });
         }
+
+        private int FeeCalc(PaymentScheme scheme, DateTime parkTime, TimeSpan orderTimeLength)
+        {
+            try
+            {
+                int orderFee = scheme.bookCharge * (int)orderTimeLength.TotalHours;
+                int parkFee = 0;
+                int hours = (int)(DateTime.Now - parkTime).TotalHours;
+                switch (scheme.schemeType)
+                {
+                    //按时计费
+                    case 1:
+                        if (hours > scheme.firstChargeTime)
+                        {
+                            parkFee = Math.Min(scheme.firstCharge + scheme.intervalCharge * hours / scheme.chargeInterval, scheme.upperBound);
+                        }
+                        else
+                        {
+                            parkFee = scheme.firstCharge;
+                        }
+                        break;
+                    //按次计费
+                    case 2:
+                        int overnightCount = hours / 24;
+                        parkFee = scheme.eachCharge + overnightCount * scheme.overnightCharge;
+                        break;
+                    //按时间段计费
+                    case 3:
+                        TimeSpan currentTimeOfDay = DateTime.Now.TimeOfDay;
+                        TimeSpan parkTimeOfDay = parkTime.TimeOfDay;
+                        TimeSpan startTimeOfDay = DateTime.Parse(scheme.startChargeTime).TimeOfDay;
+                        TimeSpan endTimeOfDay = DateTime.Parse(scheme.endChargeTime).TimeOfDay;
+                        double countingHoursOfDay = endTimeOfDay.TotalHours - startTimeOfDay.TotalHours;
+                        int days = (int)((DateTime.Now - parkTime).TotalHours - currentTimeOfDay.TotalHours + parkTimeOfDay.TotalHours);
+                        int previousOffset = (int)(-(Math.Max(0, parkTimeOfDay.TotalHours - startTimeOfDay.TotalHours)));
+                        int currentOffset = (int)Math.Min((currentTimeOfDay.TotalHours - startTimeOfDay.TotalHours), countingHoursOfDay);
+                        parkFee = (days * (int)countingHoursOfDay + previousOffset + currentOffset) * scheme.chargeStandard;
+                        break;
+                }
+                return parkFee;
+            }
+            catch (Exception e) { Console.WriteLine("费用计算异常"); return -1; }
+        }
+
         /// <summary>
         /// 取车相关操作
         /// 
         /// </summary>
         private void FetchProcess()
         {
+            int fetchState = Monitor.Monitor.mainBlockInfo.fetchingRunning;
+            int receiptNum = 0;
+            TerminalStru ts = new TerminalStru();
+            for (int i = 0; i < terminalInfo.Count; i++)
+            {
+                if (terminalInfo[i].terminalID == Monitor.Monitor.mainBlockInfo.terminalID)
+                {
+                    ts = terminalInfo[i];
+                    receiptNum = ts.receiptNum;
+                    break;
+                }
+            }
+            //取车状态且凭证号不为空,查询数据库计费
+            if (fetchState == 1 && receiptNum != 0)
+            {
+                int userID = 0;
+                string numberPlate = "";
+                string realParkTime = "";
+                //1.根据凭证号查询停车记录
+                string parkRecordsSql = "select userID,numberPlate,realParkTime from parkingrecords where receiptNum = "+ receiptNum + ";";
+                try
+                {
+                    MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(parkRecordsSql);
+                    if(reader!=null && reader.Read())
+                    {
+                        object[] receiver = new object[3];
+                        reader.GetValues(receiver);
+                        userID = (int)receiver[0];
+                        numberPlate = (string)receiver[1];
+                        realParkTime = (string)receiver[2];
+                    }
+                }
+                catch (Exception e) { Console.WriteLine(e.Message); }
+                //2.根据号牌查询预约记录
+                //3.获取停车时刻并计费发送给PLC,暂时认为皆为普通用户
+                DateTime parkTime = DateTime.Parse(realParkTime);
+                if (PaymentScheme.ins != null)
+                {
+                    int fee = FeeCalc(PaymentScheme.ins, parkTime, new TimeSpan(0));
+                    if (Monitor.Monitor.PLC != null)
+                    {
+                        TerminalStru FeeMsg = new TerminalStru {
+                            paymentStatus = (short)-1,
+                            licVerification = (short)-1,
+                            parkingFee = (short)fee,
+                            userType = (short)1
+                        };
+                        Monitor.Monitor.PLC.WriteToPLC(FeeMsg,PLCDataType.central);
+                    }
+                }
+                
+            }
 
         }
 
@@ -376,6 +489,164 @@ namespace Terminal
             isClosing = true;
         }
     }
+    public class PaymentScheme
+    {
+        public static PaymentScheme ins;
+        public int paymentSchemeID;
+        /// <summary>
+        /// 策略类型。1按时,2按次,3按时间段
+        /// </summary>
+        public int schemeType;
+        /// <summary>
+        /// 免费时间
+        /// </summary>
+        public int freeTime;
+        //******************** 按时收费 *******************
+        /// <summary>
+        /// 首段收费时间
+        /// </summary>
+        public int firstChargeTime;
+        /// <summary>
+        /// 首段费用
+        /// </summary>
+        public int firstCharge;
+        /// <summary>
+        /// 间隔收费时间
+        /// </summary>
+        public int chargeInterval;
+        /// <summary>
+        /// 间隔收费单价
+        /// </summary>
+        public int intervalCharge;
+        /// <summary>
+        /// 每日停车收费上限
+        /// </summary>
+        public int upperBound;
+        //******************** 按次收费 *******************
+        /// <summary>
+        /// 按次收费,每次费用
+        /// </summary>
+        public int eachCharge;
+        /// <summary>
+        /// 按次收费,加收过夜费
+        /// </summary>
+        public int overnightCharge;
+        //******************** 按时间段收费 *******************
+        /// <summary>
+        /// 按时间段收费,起始时间
+        /// </summary>
+        public string startChargeTime;
+        /// <summary>
+        /// 按时间段收费,终止时间
+        /// </summary>
+        public string endChargeTime;
+        /// <summary>
+        /// 按时间段收费,每小时费用
+        /// </summary>
+        public int chargeStandard;
+        //******************** VIP卡 *******************
+        /// <summary>
+        /// 月卡办理价格
+        /// </summary>
+        public int monthCardCharge;
+        /// <summary>
+        /// 季卡办理价格
+        /// </summary>
+        public int seasonCardCharge;
+        /// <summary>
+        /// 半年卡办理价格
+        /// </summary>
+        public int halfYearCardCharge;
+        /// <summary>
+        /// 年卡办理价格
+        /// </summary>
+        public int yearCardCharge;
+        /// <summary>
+        /// 预约小时费用
+        /// </summary>
+        public int bookCharge;
 
+        public PaymentScheme()
+        {
+            ins = new PaymentScheme();
+            ins.paymentSchemeID = 0;
+            ins.schemeType = 0;
+            ins.freeTime = 0;
+            ins.firstChargeTime = 0;
+            ins.firstCharge = 0;
+            ins.chargeInterval = 0;
+            ins.upperBound = 0;
+            ins.eachCharge = 0;
+            ins.overnightCharge = 0;
+            ins.startChargeTime = "";
+            ins.endChargeTime = "";
+            ins.chargeStandard = 0;
+            ins.monthCardCharge = 0;
+            ins.seasonCardCharge = 0;
+            ins.halfYearCardCharge = 0;
+            ins.yearCardCharge = 0;
+            ins.bookCharge = 0;
+        }
+        public PaymentScheme(int pID, int type, int freeTime, int firstChargeTime, int firstCharge, int chargeInterval, int intervalCharge,
+            int upperBound, int eachCharge, int overnightCharge, string startChargeTime, string endChargeTime, int chargeStandard,
+            int monthCardCharge, int seasonCardCharge, int halfYearCardCharge, int yearCardCharge, int bookCharge)
+        {
+            ins = new PaymentScheme();
+            ins.paymentSchemeID = pID;
+            ins.schemeType = type;
+            ins.freeTime = freeTime;
+            ins.firstChargeTime = firstChargeTime;
+            ins.firstCharge = firstCharge;
+            ins.chargeInterval = chargeInterval;
+            ins.intervalCharge = intervalCharge;
+            ins.upperBound = upperBound;
+            ins.eachCharge = eachCharge;
+            ins.overnightCharge = overnightCharge;
+            ins.startChargeTime = startChargeTime;
+            ins.endChargeTime = endChargeTime;
+            ins.chargeStandard = chargeStandard;
+            ins.monthCardCharge = monthCardCharge;
+            ins.seasonCardCharge = seasonCardCharge;
+            ins.halfYearCardCharge = halfYearCardCharge;
+            ins.yearCardCharge = yearCardCharge;
+            ins.bookCharge = bookCharge;
+        }
+        public static PaymentScheme GetCurrentPaymentScheme(int index)
+        {
+            string currentPaymentSchemeQuerySql = "select * from paymentScheme where paymentSchemeID = " + index + ";";
+            object[] paramArray = null;
+            PaymentScheme scheme = null;
+            try
+            {
+                MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(currentPaymentSchemeQuerySql);
+                if (reader != null && reader.Read())
+                {
+                    scheme = new PaymentScheme();
+                    paramArray = new object[reader.FieldCount];
+                    reader.GetValues(paramArray);
+                    scheme.paymentSchemeID = index;
+                    scheme.schemeType = (int)paramArray[1];
+                    scheme.freeTime = (int)paramArray[2];
+                    scheme.firstChargeTime = (int)paramArray[3];
+                    scheme.firstCharge = (int)paramArray[4];
+                    scheme.chargeInterval = (int)paramArray[5];
+                    scheme.intervalCharge = (int)paramArray[6];
+                    scheme.upperBound = (int)paramArray[7];
+                    scheme.eachCharge = (int)paramArray[8];
+                    scheme.overnightCharge = (int)paramArray[9];
+                    scheme.startChargeTime = (string)paramArray[10];
+                    scheme.endChargeTime = (string)paramArray[11];
+                    scheme.chargeStandard = (int)paramArray[12];
+                    scheme.monthCardCharge = (int)paramArray[13];
+                    scheme.seasonCardCharge = (int)paramArray[14];
+                    scheme.halfYearCardCharge = (int)paramArray[15];
+                    scheme.yearCardCharge = (int)paramArray[16];
+                    scheme.bookCharge = (int)paramArray[17];
+                }
+            }
+            catch (Exception e) { Console.WriteLine("读取本地计费策略," + e.Message); }
+            return scheme;
+        }
+    }
 }
 

BIN
PLCLinker/centralController/bin/Release/centralController.exe


BIN
PLCLinker/centralController/bin/Release/centralController.pdb


BIN
PLCLinker/centralController/obj/Release/centralController.exe


BIN
PLCLinker/centralController/obj/Release/centralController.pdb


+ 1 - 1
PLCLinker/socketTest/Program.cs

@@ -17,7 +17,7 @@ namespace socketTest
         {
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
-            Application.Run(new Form1());            
+            Application.Run(new Form1());    
         }
     }
 }