1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075 |
- using Terminal;
- using db;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using PLCS7;
- using System;
- using System.Text;
- using MySql.Data.MySqlClient;
- using System.Threading;
- using BroadcastModule;
- using parkMonitor.LOG;
- namespace Terminal
- {
- public class Terminal
- {
- public static List<TerminalStru> terminalInfo = new List<PLCS7.TerminalStru>();
- public static Dictionary<int, string> idLicMap = new Dictionary<int, string>();
- public static Dictionary<int, bool> termUsedMap = new Dictionary<int, bool>();
- public static bool isClosing = false;
- public void FeeCal()
- {
- }
- public void GetTerminalState()
- {
- }
- private object SearchPaymentScheme()
- {
- return null;
- }
- private void UpdateParkingRecords(int Object)
- {
- }
- /// <summary>
- /// 从终端结构体中获得车牌号
- /// </summary>
- /// <param name="ts"></param>
- /// <returns></returns>
- private static string GetLicenseFromTerm(TerminalStru ts)
- {
- string header = Encoding.ASCII.GetString(BitConverter.GetBytes(ts.licenseCodeB));
- string identityA = Encoding.ASCII.GetString(BitConverter.GetBytes(ts.licenseCodeC));
- byte[] bytes = BitConverter.GetBytes(ts.licenseCodeD);
- List<byte> newBytes = new List<byte>();
- for (int i = 0; i < bytes.Length; i++)
- {
- if (bytes[i] != 0x00) { newBytes.Add(bytes[i]); }
- }
- string identityB = Encoding.ASCII.GetString(newBytes.ToArray());
- Log.WriteLog(LogType.process, LogFile.INFO, "已从终端" + ts.terminalID + "获得号牌:" + header + identityA + identityB);
- //根据省份对应的两位数字查找汉字
- string province = header.Substring(0, 2);
- string provinceIndex = header.Substring(2);
- string provinceQuerySql = "select NumberPlateHeader from numberplatemapping where mappedString = " + province + ";";
- lock (Monitor.Monitor.localDBOper)
- {
- object[] result = Monitor.Monitor.localDBOper.Query(provinceQuerySql);
- //MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(provinceQuerySql);
- try
- {
- //if (reader != null && reader.Read() && reader.HasRows)
- //{
- // province = reader.GetString("NumberPlateHeader");
- // //Console.WriteLine(province);
- //}
- if (result!=null && result.Length > 0) { province = (string)result[0]; }
- }
- catch { }
- //if (reader != null)
- //{
- // reader.Close();
- // reader.Dispose();
- //}
- }
- return province + provinceIndex + identityA + identityB;
- }
- /// <summary>
- /// 更新所有车位信息
- /// </summary>
- private static void UpdateAllParkingSpace()
- {
- string findParkingSpace = "select parkingSpaceID from parkingspace;";
- HashSet<int> recordsIDSet = new HashSet<int>();
- lock (Monitor.Monitor.localDBOper)
- {
- object[] result = Monitor.Monitor.localDBOper.Query(findParkingSpace);
- if(result!=null && result.Length > 0)
- {
- for (int i = 0; i < result.Length; i++)
- {
- recordsIDSet.Add((int)result[i]);
- }
- }
- //MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(findParkingSpace);
- //if (reader != null)
- //{
- // while (reader.Read())
- // {
- // if (reader.HasRows)
- // {
- // recordsIDSet.Add(reader.GetInt32("parkingSpaceID"));
- // }
- // }
- //}
- //try
- //{
- // if (reader != null)
- // {
- // reader.Close();
- // reader.Dispose();
- // }
- //}
- //catch { }
- }
- List<string> updateSpaceList = new List<string>();
- List<string> insertSpaceList = new List<string>();
- string updateParkingSpace = "";
- string insertParkingSpace = "";
- for (int i = 0; i < Monitor.Monitor.parkingSpaceInfo.Count; i++)
- {
- if (recordsIDSet.Contains(Monitor.Monitor.parkingSpaceInfo[i].parkingSpace))
- {
- updateParkingSpace = "update parkingspace set parkingSpaceX = " + Monitor.Monitor.parkingSpaceInfo[i].coordX +
- ",parkingSpaceY = " + Monitor.Monitor.parkingSpaceInfo[i].coordY + ",parkingSpaceZ = " + Monitor.Monitor.parkingSpaceInfo[i].floorNo + ",parkingSpaceState = " + Monitor.Monitor.parkingSpaceInfo[i].spaceStatus + " where (parkingSpaceID = " + Monitor.Monitor.parkingSpaceInfo[i].parkingSpace + ");";
- updateSpaceList.Add(updateParkingSpace);
- }
- else
- {
- insertParkingSpace = "insert into parkingspace (parkingSpaceID,parkingSpaceX,parkingSpaceY,parkingSpaceZ,parkingSpaceState) values (" + Monitor.Monitor.parkingSpaceInfo[i].parkingSpace + "," + Monitor.Monitor.parkingSpaceInfo[i].coordX +
- "," + Monitor.Monitor.parkingSpaceInfo[i].coordY + "," + Monitor.Monitor.parkingSpaceInfo[i].floorNo + "," + Monitor.Monitor.parkingSpaceInfo[i].spaceStatus + ");";
- insertSpaceList.Add(insertParkingSpace);
- }
- }
- try
- {
- lock (Monitor.Monitor.localDBOper)
- {
- Monitor.Monitor.localDBOper.UpdateTransaction(updateSpaceList);
- Monitor.Monitor.localDBOper.Insert(insertSpaceList);
- }
- }
- catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "更新本地所有车位异常"); Console.WriteLine("更新本地所有车位异常" + e.Message); }
- }
- /// <summary>
- /// 更新车辆状态
- /// </summary>
- /// <param name="lic"></param>
- /// <param name="state"></param>
- private static void UpdateVehicle(string lic, int state, int parkingRecordsID, bool park, bool remote, int parkingSpaceID)
- {
- if (lic != "")
- {
- //MySqlDataReader reader;
- object[] result;
- //查询车辆是否在车辆表中
- string checkVehicleState = "select * from vehicle where numberPlate = '" + lic + "';";
- if (!remote)
- {
- //reader = Monitor.Monitor.localDBOper.Query(checkVehicleState);
- result = Monitor.Monitor.localDBOper.Query(checkVehicleState);
- }
- else
- {
- //reader = Monitor.Monitor.remoteDBOper.Query(checkVehicleState);
- result = Monitor.Monitor.remoteDBOper.Query(checkVehicleState);
- }
- //if (reader != null && reader.Read() && reader.HasRows)
- //{
- // try
- // {
- // reader.Close();
- // reader.Dispose();
- // }
- // catch { }
- if(result!=null && result.Length > 0) {
- //更新车辆状态
- string updateVehicleState = "";
- string updateRemoteVehicleState = "";
- if (park)
- {
- if (parkingRecordsID > 0)
- {
- if (parkingSpaceID == 0)
- {
- updateVehicleState = "update vehicle set vehiclepParkState = " + state + " ,parkingRecordsID = " + parkingRecordsID + " where numberPlate = '" + lic + "';";
- updateRemoteVehicleState = "update vehicle set vehiclepParkState = " + state + " ,parkingRecordsID = " + parkingRecordsID + ",garageID = " + Monitor.Monitor.garageID + "where numberPlate = '" + lic + "';";
- }
- else
- {
- updateVehicleState = "update vehicle set vehiclepParkState = " + state + " ,parkingRecordsID = " + parkingRecordsID + ",parkingSpaceID = " + parkingSpaceID + " where numberPlate = '" + lic + "';";
- updateRemoteVehicleState = "update vehicle set vehiclepParkState = " + state + " ,parkingRecordsID = " + parkingRecordsID + ",parkingSpaceID = " + parkingSpaceID + ",garageID = " + Monitor.Monitor.garageID + "where numberPlate = '" + lic + "';";
- }
- }
- else
- {
- updateVehicleState = "update vehicle set vehiclepParkState = " + state + " where numberPlate = '" + lic + "';";
- updateRemoteVehicleState = updateVehicleState;
- }
- }
- else
- {
- updateVehicleState = "update vehicle set vehiclepParkState = " + state + " where numberPlate = '" + lic + "';";
- updateRemoteVehicleState = updateVehicleState;
- }
- List<string> list = new List<string>();
- list.Add(updateVehicleState);
- if (!remote)
- {
- Monitor.Monitor.localDBOper.UpdateTransaction(list);
- }
- else
- {
- Monitor.Monitor.remoteDBOper.UpdateTransaction(list);
- }
-
- }
- else
- {
- //插入车辆
- string insertVehicleWithState = "";
- if (park)
- {
- if (parkingRecordsID > 0)
- insertVehicleWithState = "insert into vehicle (numberPlate,vehicleTypeID,vehiclepParkState,parkingRecordsID) values " +
- "('" + lic + "',NULL,'" + state + "'," + parkingRecordsID + ");";
- else
- insertVehicleWithState = "insert into vehicle (numberPlate,vehicleTypeID,vehiclepParkState) values " +
- "('" + lic + "',NULL,'" + state + "');";
- }
- else
- {
- Log.WriteLog(LogType.process, LogFile.ERROR, "取车无法获得车辆信息");
- Console.WriteLine("明显异常,取车发现无车辆");
- return;
- }
- List<string> list = new List<string>();
- list.Add(insertVehicleWithState);
- if (!remote)
- {
- Monitor.Monitor.localDBOper.Insert(list);
- }
- else
- {
- Monitor.Monitor.remoteDBOper.Insert(list);
- }
- }
-
- }
- }
- /// <summary>
- /// 流程结束清除终端相关信息
- /// </summary>
- /// <param name="id"></param>
- private static void ClearTerminal(int id)
- {
- TerminalStru tsFromCentral = new TerminalStru
- {
- terminalID = (short)id,
- parkingFee = (short)32767,
- };
- TerminalStru tsFromTerminal = new TerminalStru
- {
- terminalID = (short)id,
- btnStatus = -1,
- };
- Monitor.Monitor.PLC.WriteToPLC(tsFromCentral, PLCDataType.central);
- Monitor.Monitor.PLC.WriteToPLC(tsFromTerminal, PLCDataType.terminal);
- }
- /// <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)
- {
- int numReceivedStatus = 0;//1获得,2终止
- TerminalStru term = terminalInfo[i];
- if (term.terminalStatus == (short)1)
- {
- string license = Monitor.Monitor.numMachineLinker.GetLicensePlate(numMachineLaunch);
- //未获得号牌,告知PLC终止,告诉终端提示用户重新操作
- if (license == "")
- {
- MainBlockStru mb = new MainBlockStru
- {
- licenseReceived = (short)2
- };
- Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
- TerminalStru ts = new TerminalStru
- {
- terminalID = (short)numMachineLaunch,
- 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(numMachineLaunch))
- {
- idLicMap[numMachineLaunch] = license;
- }
- else
- {
- idLicMap.Add(numMachineLaunch, license);
- }
- }
- Log.WriteLog(LogType.process, LogFile.ERROR, "号牌" + license + "已记录");
- TerminalStru ts = new TerminalStru
- {
- terminalID = (short)numMachineLaunch,
- paymentStatus = -1,
- parkingFee = -1,
- userType = -1
- };
- //注册用户
- if (term.btnStatus == (short)2)
- {
- int userID = term.licenseCodeA;
- string userLicense = GetLicenseFromTerm(term);
- //与云端数据比对
- string checkNetSql = "select * from user where userID = 1;";
- object[] result = Monitor.Monitor.remoteDBOper.Query(checkNetSql);
- //MySqlDataReader readerTest = Monitor.Monitor.remoteDBOper.Query(checkNetSql);
- //if ( readerTest!= null && readerTest.Read() && readerTest.HasRows)
- //{
- // try
- // {
- // readerTest.Close();
- // readerTest.Dispose();
- // }
- // catch { }
- if (result!=null && result.Length>0) {
- string userInfoCheckSql = "select * from usercarrelation where userID = '" + userID + "'and numberPlate = '" + userLicense + "';";
- //MySqlDataReader reader = Monitor.Monitor.remoteDBOper.Query(userInfoCheckSql);
- object[] objList = Monitor.Monitor.remoteDBOper.Query(userInfoCheckSql);
- //if (reader != null && reader.Read() && reader.HasRows && userID != 0 && userLicense != "")
- if(objList!=null && objList.Length>0 && userID != 0 && userLicense != "")
- {
- ts.licVerification = 1;//验证成功
- Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
- numReceivedStatus = 1;
- Log.WriteLog(LogType.process, LogFile.INFO, "号牌验证成功");
- }
- else
- {
- ts.licVerification = 2;//验证失败
- Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
- numReceivedStatus = 2;
- Log.WriteLog(LogType.process, LogFile.WARNING, "号牌验证失败");
- }
- //try
- //{
- // reader.Close();
- // reader.Dispose();
- //}
- //catch { }
- }
- else
- {
- ts.licVerification = 1;//网络异常,跳过验证
- Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
- numReceivedStatus = 1;
- Log.WriteLog(LogType.process, LogFile.ERROR, "网络异常,无法验证号牌");
- }
- }
- //非注册用户
- 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);
- //号牌获取结果被清零才跳出
- while (Monitor.Monitor.mainBlockInfo.numMachineLaunch != 0)
- {
- Thread.Sleep(2000);
- }
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// 停车流程,收到PLC停车完成信号后操作过程
- /// </summary>
- private static void ParkCompleteSubProcess()
- {
- int processAttrib = Monitor.Monitor.mainBlockInfo.parkingRunning;
- int processCompleted = Monitor.Monitor.mainBlockInfo.processCompleted;
- int currentTerm = Monitor.Monitor.mainBlockInfo.terminalID;
- int parkingSpaceID = 0;
- if (processAttrib == 1 && processCompleted == 1)
- {
- TerminalStru term;
- string license;
- //找到终端号,判断是否注册用户
- for (int i = 0; i < terminalInfo.Count; i++)
- {
- //拿到号牌
- if (terminalInfo[i].terminalID == currentTerm && idLicMap.TryGetValue(currentTerm, out license) && license != "")
- {
- term = terminalInfo[i];
- //找到停车位置
- for (int s = 0; s < Monitor.Monitor.parkingSpaceInfo.Count; s++)
- {
- if (Monitor.Monitor.parkingSpaceInfo[i].receiptNum == term.receiptNum)
- {
- parkingSpaceID = Monitor.Monitor.parkingSpaceInfo[i].parkingSpace;
- break;
- }
- }
- //无车位信息则跳出当前循环
- if (parkingSpaceID == 0) break;
- UpdateVehicle(license, 0, 0, true, false,0);
- UpdateVehicle(license, 0, 0, true, true,0);
- //插入停车记录
- string parkingRecordsSql = "";
- if (term.licenseCodeA != 0)
- {
- parkingRecordsSql = "INSERT INTO parkingrecords (userID, numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime,receiptNum,parkingPrice)" +
- "values ('" + term.licenseCodeA + "','" + license + "','" + parkingSpaceID + "','" + Monitor.Monitor.garageID + "',3,'" + DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "','" + term.receiptNum + "',NULL);";
- }
- else
- {
- parkingRecordsSql = "INSERT INTO parkingrecords (userID, numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime,receiptNum,parkingPrice)" +
- "values ('" + 1 + "','" + license + "','" + parkingSpaceID + "','" + Monitor.Monitor.garageID + "',3,'" + DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "','" + term.receiptNum + "',NULL);";
- }
- List<string> list = new List<string>();
- list.Add(parkingRecordsSql);
- Monitor.Monitor.localDBOper.Insert(list);
- Monitor.Monitor.remoteDBOper.Insert(list);
- Log.WriteLog(LogType.process, LogFile.INFO, "停车记录已插入");
- ////注册用户记录插入云端
- //if (term.btnStatus == 0)
- //{
- //Monitor.Monitor.remoteDBOper.Insert(list);
- //}
- //查询记录ID号
- string findRecordSql = "select parkingRecordsID from parkingrecords where receiptNum = " + term.receiptNum + ";";
- object[] localRecords = Monitor.Monitor.localDBOper.Query(findRecordSql);
- object[] remoteRecords = Monitor.Monitor.remoteDBOper.Query(findRecordSql);
- //MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(findRecordSql);
- //MySqlDataReader remoteReader = Monitor.Monitor.remoteDBOper.Query(findRecordSql);
- int parkingRecordsID = 0;
- int remoteParkingRecordsID = 0;
- try
- {
- //if (reader != null && reader.Read())
- //{
- // parkingRecordsID = reader.GetInt32("parkingRecordsID");
- // try
- // {
- // if (reader != null)
- // {
- // reader.Close();
- // reader.Dispose();
- // }
- // }
- // catch { }
- //}
- //if (remoteReader != null && remoteReader.Read())
- //{
- // remoteParkingRecordsID = remoteReader.GetInt32("parkingRecordsID");
- // try
- // {
- // if (remoteReader != null)
- // {
- // remoteReader.Close();
- // remoteReader.Dispose();
- // }
- // }
- // catch { }
- //}
- if(localRecords!=null && localRecords.Length > 0) { parkingRecordsID = (int)localRecords[0]; }
- if (remoteRecords != null && remoteRecords.Length > 0) { remoteParkingRecordsID = (int)remoteRecords[0]; }
- }
- catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "查询停车记录id失败"); ; Console.WriteLine(e.Message); };
- //更新本地车辆表
- if (parkingRecordsID != 0)
- {
- UpdateVehicle(license, 1, parkingRecordsID, true, false, parkingSpaceID);
- }
- if (remoteParkingRecordsID != 0)
- {
- UpdateVehicle(license, 1, remoteParkingRecordsID, true, true, parkingSpaceID);
- }
- Log.WriteLog(LogType.process, LogFile.INFO, "停车记录表已更新");
- //更新本地车位
- UpdateAllParkingSpace();
- while (Monitor.Monitor.mainBlockInfo.processCompleted != 0)
- {
- Thread.Sleep(1000);
- }
- //中控清除车牌、凭证号、号牌验证等信息
- ClearTerminal(term.terminalID);
- }
- }
- }
- }
- /// <summary>
- /// 停车相关操作
- /// 1.读取到号牌机启动指令,启动号牌机并根据是否注册用户进行操作
- /// 非注册拿到号牌后记录并告知plc
- /// 注册用户,核对号牌与用户关联信息
- /// 2.停车流程结束,写数据库
- /// </summary>
- ///
- private static void ParkProcess()
- {
- Task.Factory.StartNew(() =>
- {
- while (!isClosing)
- {
- ParkNumSubProcess();
- Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
- }
- });
- Task.Factory.StartNew(() =>
- {
- while (!isClosing)
- {
- ParkCompleteSubProcess();
- Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
- }
- });
- }
- /// <summary>
- /// 根据凭证号查询停车记录
- /// </summary>
- /// <param name="receipt"></param>
- /// <param name="recordID"></param>
- /// <param name="userID"></param>
- /// <param name="numberPlate"></param>
- /// <param name="realParkTime"></param>
- private static bool FindLocalParkingRecord(int receipt, out int recordID, out int userID, out string numberPlate, out string realParkTime)
- {
- recordID = 0;
- userID = 0;
- numberPlate = "";
- realParkTime = "";
- string parkRecordsSql = "select parkingRecordsID,userID,numberPlate,realParkTime from parkingrecords where receiptNum = " + receipt + ";";
- try
- {
- object[] result = Monitor.Monitor.localDBOper.Query(parkRecordsSql);
- if(result!=null && result.Length >= 4)
- {
- recordID = (int)result[0];
- userID = (int)result[1];
- numberPlate = (string)result[2];
- realParkTime = (string)result[3];
- }
- //MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(parkRecordsSql);
- //if (reader != null && reader.Read())
- //{
- // object[] receiver = new object[4];
- // reader.GetValues(receiver);
- // recordID = (int)receiver[0];
- // userID = (int)receiver[1];
- // numberPlate = (string)receiver[2];
- // realParkTime = (string)receiver[3];
- //}
- //if (reader != null)
- //{
- // reader.Close();
- // reader.Dispose();
- //}
- }
- catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "根据凭证号查询停车记录失败"); Console.WriteLine(e.Message); return false; }
- Log.WriteLog(LogType.process, LogFile.INFO, "根据凭证号查询停车记录成功");
- return true;
- }
- /// <summary>
- /// 预约与停车费用计算
- /// </summary>
- /// <param name="scheme"></param>
- /// <param name="parkTime"></param>
- /// <param name="orderTimeLength"></param>
- /// <returns></returns>
- private static 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;
- }
- Log.WriteLog(LogType.process, LogFile.INFO, "停车费用" + parkFee);
- return parkFee;
- }
- catch (Exception e) { Console.WriteLine("费用计算异常"); return -1; }
- }
- /// <summary>
- /// 取车计费子操作
- /// </summary>
- private static void FetchFeeCalcSubProcess()
- {
- int fetchState = Monitor.Monitor.mainBlockInfo.fetchingRunning;
- int receiptNum = 0;
- int termIndex = -1;
- TerminalStru ts = new TerminalStru();
- for (int i = 0; i < terminalInfo.Count; i++)
- {
- if (terminalInfo[i].terminalID == Monitor.Monitor.mainBlockInfo.terminalID)
- {
- ts = terminalInfo[i];
- termIndex = ts.terminalID;
- receiptNum = ts.receiptNum;
- break;
- }
- }
- //取车状态、凭证号不为空,查询数据库计费
- if (termIndex != -1 && termUsedMap.Count >= termIndex && termUsedMap.TryGetValue(termIndex, out bool value) && !value && fetchState == 1 && receiptNum != 0)
- {
- termUsedMap[termIndex] = true;
- Console.WriteLine("终端编号" + termIndex);
- Task.Factory.StartNew(() =>
- {
- int parkingRecordsID = 0;
- int remoteParkingRecordsID = 0;
- int userID = 0;
- string numberPlate = "";
- string realParkTime = "";
- //1.根据凭证号查询停车记录
- if (!FindLocalParkingRecord(receiptNum, out parkingRecordsID, out userID, out numberPlate, out realParkTime))
- {
- return;
- }
- string remoteParkRecordsSql = "select parkingRecordsID from parkingrecords where receiptNum = " + receiptNum + ";";
- try
- {
- object[] result = Monitor.Monitor.remoteDBOper.Query(remoteParkRecordsSql);
- if(result!=null && result.Length > 0) { remoteParkingRecordsID = (int)result[0]; }
- //MySqlDataReader reader = Monitor.Monitor.remoteDBOper.Query(remoteParkRecordsSql);
- //if (reader != null && reader.Read())
- //{
- // object[] receiver = new object[1];
- // reader.GetValues(receiver);
- // remoteParkingRecordsID = (int)receiver[0];
- //}
- //if (reader != null)
- //{
- // reader.Close();
- // reader.Dispose();
- //}
- }
- catch (Exception e) { Console.WriteLine(e.Message); }
- //本地存在号牌,后续操作
- if (numberPlate != "")
- {
- //2.根据号牌查询车辆月卡信息与预约记录
- int orderRecordsID = 0;
- int monthCardType = -1;
- string monthCardTime = "";
- TimeSpan monthCardTimeLength = new TimeSpan(0);
- TimeSpan orderTimeLength = new TimeSpan(0);
- //本地VIP信息
- string vipInfoSql = "select orderRecordsID,monthCardType,monthCardTime from vehicle where numberPlate = '" + numberPlate + "';";
- try
- {
- object[] result = Monitor.Monitor.localDBOper.Query(vipInfoSql);
- //MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(vipInfoSql);
- //if (reader != null && reader.Read())
- if (result!=null && result.Length>=3)
- {
- //object[] receiver = new object[3];
- //reader.GetValues(receiver);
- //if (!receiver[0].GetType().Equals(typeof(DBNull)))
- //{
- // orderRecordsID = (int)receiver[0];
- //}
- //if (!receiver[1].GetType().Equals(typeof(DBNull)))
- //{
- // monthCardType = (int)(UInt32)receiver[1];
- //}
- //if (!receiver[2].GetType().Equals(typeof(DBNull)))
- //{
- // monthCardTime = (string)receiver[2];
- //}
- if (!result[0].GetType().Equals(typeof(DBNull)))
- {
- orderRecordsID = (int)result[0];
- }
- if (!result[1].GetType().Equals(typeof(DBNull)))
- {
- monthCardType = (int)(UInt32)result[1];
- }
- if (!result[2].GetType().Equals(typeof(DBNull)))
- {
- monthCardTime = (string)result[2];
- }
- monthCardTimeLength = DateTime.Parse(monthCardTime) - DateTime.Now;
- if (monthCardTimeLength.TotalHours <= 0)
- {
- monthCardType = 0;
- }
- Log.WriteLog(LogType.process, LogFile.INFO, "已获取本地预约与月卡信息");
- }
- //if (reader != null)
- //{
- // reader.Close();
- // reader.Dispose();
- //}
- }
- catch (Exception e) { Console.WriteLine(e.Message); }
- //云端VIP信息
- try
- {
- object[] result = Monitor.Monitor.remoteDBOper.Query(vipInfoSql);
- //MySqlDataReader reader = Monitor.Monitor.remoteDBOper.Query(vipInfoSql);
- //if (reader != null && reader.Read())
- if(result!=null && result.Length>=3)
- {
- //object[] receiver = new object[3];
- //reader.GetValues(receiver);
- //int cardType = (int)(UInt32)receiver[1];
- //string CardTime = (string)receiver[2];
- int cardType = (int)(UInt32)result[1];
- string CardTime = (string)result[2];
- TimeSpan t = DateTime.Parse(CardTime) - DateTime.Now;
- //云端
- if (t.TotalHours > 0 && cardType > monthCardType)
- {
- monthCardType = cardType;
- monthCardTimeLength = t;
- }
- Log.WriteLog(LogType.process, LogFile.INFO, "已获取云端预约与月卡信息");
- }
- //if (reader != null)
- //{
- // reader.Close();
- // reader.Dispose();
- //}
- }
- catch (Exception e) { Console.WriteLine(e.Message); }
- //有预约记录,判断记录有效性,有效则获取预约时长
- if (orderRecordsID != 0)
- {
- //修改orderTimeLength
- }
- //3.获取停车时刻,根据用户类型计费发送给PLC,暂认为无预约
- DateTime parkTime = DateTime.Parse(realParkTime);
- DateTime currentTime = DateTime.Now;
- int fee = 32767;
- if (PaymentScheme.ins != null && monthCardType != -1)
- {
- if (monthCardTimeLength.TotalHours > 0)
- {
- fee = 0;
- }
- else
- {
- fee = FeeCalc(PaymentScheme.ins, parkTime, orderTimeLength);
- }
- try
- {
- if (Monitor.Monitor.PLC != null)
- {
- TerminalStru FeeMsg = new TerminalStru();
- FeeMsg.terminalID = (short)termIndex;
- FeeMsg.paymentStatus = (short)-1;
- FeeMsg.licVerification = (short)-1;
- FeeMsg.parkingFee = (short)fee;
- FeeMsg.userType = (short)(monthCardType + 1);
- Console.WriteLine(FeeMsg);
- Monitor.Monitor.PLC.WriteToPLC(FeeMsg, PLCDataType.central);
- Log.WriteLog(LogType.process, LogFile.INFO, "停车费用已发送至PLC");
- }
- }
- catch { Console.WriteLine("error"); }
- }
- //4.等待终端获得收费完成信号且将凭证号清除后,清除收费完成信号并将信息写入数据库
- while (!isClosing && termIndex != -1)
- {
- if (terminalInfo[termIndex - 1].receiptNum == 0)
- {
- Log.WriteLog(LogType.process, LogFile.INFO, "检测到凭证号已被清除");
- TerminalStru fetchReset = new TerminalStru
- {
- terminalID = (short)termIndex,
- paymentStatus = (short)0,
- licVerification = (short)0,
- parkingFee = (short)32767,
- userType = (short)0
- };
- Monitor.Monitor.PLC.WriteToPLC(fetchReset, PLCDataType.central);
- //更新停车记录与车辆状态
- string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + currentTime.ToString("yyyy-MM-dd HH:mm:ss") +
- "',parkingPrice = " + fee + " where receiptNum = '" + receiptNum + "';";
- List<string> list = new List<string>();
- list.Add(updateParkingRecordsSql);
- lock (Monitor.Monitor.localDBOper)
- {
- Monitor.Monitor.localDBOper.UpdateTransaction(list);
- UpdateVehicle(numberPlate, 0, parkingRecordsID, false, false, 0);
- }
- lock (Monitor.Monitor.remoteDBOper)
- {
- Monitor.Monitor.remoteDBOper.UpdateTransaction(list);
- UpdateVehicle(numberPlate, 0, remoteParkingRecordsID, false, true, 0);
- }
- break;
- }
- Thread.Sleep(1000);
- }
- }
- termUsedMap[termIndex] = false;
- });
- }
- }
- /// <summary>
- /// 取车相关操作
- ///
- /// </summary>
- private static void FetchProcess()
- {
- Task.Factory.StartNew(() =>
- {
- while (!isClosing)
- {
- FetchFeeCalcSubProcess();
- Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
- }
- });
- }
- public static void Start()
- {
- ParkProcess();
- FetchProcess();
- }
- public static void Stop()
- {
- 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()
- {
- this.paymentSchemeID = 0;
- this.schemeType = 0;
- this.freeTime = 0;
- this.firstChargeTime = 0;
- this.firstCharge = 0;
- this.chargeInterval = 0;
- this.upperBound = 0;
- this.eachCharge = 0;
- this.overnightCharge = 0;
- this.startChargeTime = "";
- this.endChargeTime = "";
- this.chargeStandard = 0;
- this.monthCardCharge = 0;
- this.seasonCardCharge = 0;
- this.halfYearCardCharge = 0;
- this.yearCardCharge = 0;
- this.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)((UInt32)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];
- }
- if (reader != null)
- {
- reader.Close();
- reader.Dispose();
- }
- }
- catch (Exception e) { Console.WriteLine("读取本地计费策略," + e.Message); }
- return scheme;
- }
- }
- }
|