123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652 |
- using Terminal;
- using DatabaseDLL;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using PLCS7;
- using System;
- using System.Text;
- using MySql.Data.MySqlClient;
- using System.Threading;
- namespace Terminal
- {
- public class Terminal : ITerminalDisplay
- {
- public static List<TerminalStru> terminalInfo = new List<PLCS7.TerminalStru>();
- public static Dictionary<int, string> idLicMap = new Dictionary<int, string>();
- public static bool isClosing = false;
- public void FeeCal()
- {
- }
- public void GetTerminalState()
- {
- }
- public List<object> GetParkingRecords()
- {
- return null;
- }
- public List<object> GetOrderRecords()
- {
- return null;
- }
- private object SearchPaymentScheme()
- {
- return null;
- }
- private void UpdateParkingRecords(int Object)
- {
- }
- /// <summary>
- /// 从终端结构体中获得车牌号
- /// </summary>
- /// <param name="ts"></param>
- /// <returns></returns>
- private 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());
- return header + identityA + identityB;
- }
- /// <summary>
- /// 更新所有车位信息
- /// </summary>
- private void UpdateAllParkingSpace()
- {
- string findParkingSpace = "select parkingSpaceID from parkingspace;";
- MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(findParkingSpace);
- HashSet<int> recordsIDSet = new HashSet<int>();
- if (reader != null)
- {
- while (reader.Read())
- {
- if (reader.HasRows)
- {
- recordsIDSet.Add(reader.GetInt32("parkingSpaceID"));
- }
- }
- }
- 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
- {
- Monitor.Monitor.localDBOper.UpdateTransaction(updateSpaceList);
- Monitor.Monitor.localDBOper.Insert(insertSpaceList);
- }
- catch (Exception e) { Console.WriteLine("更新本地所有车位异常" + e.Message); }
- }
- /// <summary>
- /// 更新车辆状态
- /// </summary>
- /// <param name="lic"></param>
- /// <param name="state"></param>
- private void UpdateVehicle(string lic, int state, int parkingRecordsID, bool park, bool remote)
- {
- MySqlDataReader reader;
- //查询车辆是否在车辆表中
- string checkVehicleState = "select * from vehicle where numberPlate = '" + lic + "';";
- if (!remote)
- {
- reader = Monitor.Monitor.localDBOper.Query(checkVehicleState);
- }
- else
- {
- reader = Monitor.Monitor.remoteDBOper.Query(checkVehicleState);
- }
- if (reader != null)
- {
- //更新车辆状态
- string updateVehicleState = "";
- if (park)
- {
- updateVehicleState = "update vehicle set vehiclepParkState = " + state + " ,parkingRecordsID = " + parkingRecordsID + " where numberPlate = '" + lic + "';";
- }
- else
- {
- updateVehicleState = "update vehicle set vehiclepParkState = " + state + " where numberPlate = '" + lic + "';";
- }
- List<string> list = new List<string>();
- list.Add(updateVehicleState);
- if (!remote)
- {
- Monitor.Monitor.localDBOper.UpdateTransaction(list);
- }
- else
- {
- Monitor.Monitor.localDBOper.UpdateTransaction(list);
- }
- }
- else
- {
- //插入车辆
- string insertVehicleWithState = "";
- if (park)
- {
- insertVehicleWithState = "insert into vehicle (numberPlate,vehicleTypeID,vehiclepParkState,parkingRecordsID) values " +
- "('" + lic + "',NULL,'" + state + "'," + parkingRecordsID + ");";
- }
- else
- {
- 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>
- private 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)0)
- {
- 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
- {
- paymentStatus = -1,
- parkingFee = -1,
- userType = -1,
- licVerification = (short)2
- };
- Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.central);
- }
- else
- {
- //记录或更新当前号牌
- lock (idLicMap)
- {
- if (idLicMap.ContainsKey(numMachineLaunch))
- {
- idLicMap[numMachineLaunch] = license;
- }
- else
- {
- idLicMap.Add(numMachineLaunch, license);
- }
- }
- //注册
- if (term.btnStatus == (short)0)
- {
- int userID = term.licenseCodeA;
- string userLicense = GetLicenseFromTerm(term);
- TerminalStru ts = new TerminalStru
- {
- paymentStatus = -1,
- parkingFee = -1,
- userType = -1
- };
- //与云端数据比对
- string checkNetSql = "select * from user where userID=1;";
- if (Monitor.Monitor.remoteDBOper.Query(checkNetSql) != null)
- {
- string userInfoCheckSql = "select * from usercarrelation where userID = '" + userID + "'and numberPlate = '" + userLicense + "';";
- MySqlDataReader reader = Monitor.Monitor.remoteDBOper.Query(userInfoCheckSql);
- if (reader == null)
- {
- 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)numReceivedStatus
- };
- Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// 停车流程,收到PLC停车完成信号后操作过程
- /// </summary>
- private 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;
- //插入停车记录
- 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);
- //查询记录ID号
- string findRecordSql = "select parkingRecordsID from parkingrecords where receiptNum = " + term.receiptNum + ";";
- MySqlDataReader reader = Monitor.Monitor.localDBOper.Query(findRecordSql);
- int parkingRecordsID = 0;
- try {
- if (reader!=null && reader.Read()) { parkingRecordsID = reader.GetInt32("parkingRecordsID"); }
- } catch (Exception) { break; };
- //更新本地车辆表
- UpdateVehicle(license, parkingRecordsID, 1, true, false);
- //更新本地车位
- UpdateAllParkingSpace();
- //注册用户加入云端操作
- if (term.btnStatus == 0)
- {
- //string
- //Monitor.Monitor.remoteDBOper()
- }
- }
- }
- }
- }
- /// <summary>
- /// 停车相关操作
- /// 1.读取到号牌机启动指令,启动号牌机并根据是否注册用户进行操作
- /// 非注册拿到号牌后记录并告知plc
- /// 注册用户,核对号牌与用户关联信息
- /// 2.停车流程结束,写数据库
- /// </summary>
- private void ParkProcess()
- {
- //1.号牌机启动
- Task.Factory.StartNew(() =>
- {
- while (!isClosing)
- {
- ParkNumSubProcess();
- Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
- }
- });
- Task.Factory.StartNew(() =>
- {
- while (!isClosing)
- {
- ParkCompleteSubProcess();
- Thread.Sleep(Monitor.Monitor.plcRefreshInterval);
- }
- });
- }
- 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);
- }
- }
-
- }
- }
- public void Start()
- {
- //Task.Factory.StartNew(() =>
- //{
- //});
- }
- public 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()
- {
- 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;
- }
- }
- }
|