public bool StartCharge(string autoType) { _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "准备开始计费,开始检测许可信息……"); _lincenseState = _trnLicense.CheckLicense(autoType); SqlLiteHelper.SaveTranineesInfo(_trnLicense); if (_lincenseState == LicenseState.Normal) { _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "该许可信息有效……"); if (_currentInfoControl == null) { _currentInfoControl = new ChargingControl(); _currentInfoControl.Show(); } else { _currentInfoControl.Show(); } IsChargerControlHide = false; _chargesDisplayInfo = new ChargesInfo { PidNo = _trnLicense.PidNo, Mode = _trnLicense.ChargeMode, SeqNo = Guid.NewGuid().ToString("N").ToUpper(), CurrentMileage = _trnLicense.MileageLmt, CurrentMinutes = _trnLicense.TimeLmt, SurplusTimes = _trnLicense.TriesLmt }; _currentInfoControl.CallShowTrainerName(_trnLicense.Name); _currentInfoControl.CallShowTrainerPhoto(_trnLicense.Photo); _currentInfoControl.CallChangesShowInfo(_chargesDisplayInfo); IsChargingThread = true; Thread chargingThread = new Thread(ChargingThread); chargingThread.Start(); return(true); } else { IsChargingThread = false; IsCharging = false; _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "许可信息无效,失效类型:" + _lincenseState); return(false); } }
/// <summary> /// Saves the charges information. /// </summary> /// <param name="info">The information.</param> /// <returns></returns> public static int SaveChargesInfo(ChargesInfo info) { if (info != null) { List <SQLiteParameter> cmdparams = new List <SQLiteParameter> { new SQLiteParameter("sfzmhm", info.PidNo), new SQLiteParameter("autoid", info.AutoId), new SQLiteParameter("starttime", info.StartTime), new SQLiteParameter("endtime", info.EndTime), new SQLiteParameter("mode", info.Mode), new SQLiteParameter("currentmileage", info.CurrentMileage), new SQLiteParameter("currentminutes", info.CurrentMinutes), new SQLiteParameter("surplustimes", info.SurplusTimes) }; return(SqlLiteHelper.ExecuteNonQuery("insert into ChargeProcInfo (sfzmhm,autoid,starttime,endtime,mode,currentmileage,currentminutes,surplustimes) values (@sfzmhm,@autoid,@starttime,@endtime,@mode,@currentmileage,@currentminutes,@surplustimes)", cmdparams)); } return(0); }
/// <summary> /// Stops the train. /// </summary> /// <param name="isSave">if set to <c>true</c> [is save Train Info].</param> public void StopTrain(bool isSave) { if (!IsChargerControlHide) { IsRunning = false; IsChargingThread = false; IsCharging = false; HideChargeControl(); if (ResetUiViewDelegate != null) { ResetUiViewDelegate(); } if (isSave) { SqlLiteHelper.SaveTranineesInfo(_trnLicense); SqlLiteHelper.SaveChargesInfo(_chargesDisplayInfo); SqlLiteHelper.SaveTraningInfo(_trnLicense); writeToDisk(); } IsChargerControlHide = true; } }
private void ChargingThread() { while (IsChargingThread) { Thread.Sleep(300); double spanTime = 2; if (IsCharging && _lincenseState == LicenseState.Normal) { DateTime lastHandleTime = DateTime.Now; double totalMileage = _trnLicense.MileageLmt; double totalTime = _trnLicense.TimeLmt; _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "计费前里程剩余:" + totalMileage + ",次数剩余:" + _trnLicense.TriesLmt + ",时间剩余:" + totalTime + ",当前计费模式:" + _trnLicense.ChargeMode); SqlLiteHelper.SaveChargesInfo(_chargesDisplayInfo); _chargesDisplayInfo.StartTime = startTime.ToString("yyyy-MM-dd HH:mm:ss"); _trnLicense.TriesLmt = _trnLicense.TriesLmt - 1; detailTries += 1; if (_trnLicense.ChargeMode == "Tries") { if (_trnLicense.TriesLmt < 0) { _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "训练次数非法,准备终止训练……"); StopCharging(); if (StopDelegate != null) { StopDelegate(); } continue; } } else { SqlLiteHelper.SaveChargesInfo(_chargesDisplayInfo); writeToDisk(); } while (IsCharging && _lincenseState == LicenseState.Normal) { DateTime now = DateTime.Now; TimeSpan ts = now - lastHandleTime; double diff = Math.Round((now - startTime).TotalHours, 4); cacheTime = totalTime - diff; if (_trnLicense.ChargeMode == "Time") { _trnLicense.TimeLmt = cacheTime; } _trnLicense.MileageLmt = totalMileage - Math.Round(_trnMileage, 4); _trnLicense.TriesLmt = _trnLicense.TriesLmt; if (_trnLicense.TrainDetail != null) { _trnLicense.TrainDetail.TrainTime = detailTime + diff; _trnLicense.TrainDetail.TrainMileage = detailMileage + _trnMileage; _trnLicense.TrainDetail.TrainTries = detailTries; _trnLicense.TrainDetail.AutoId = AutoId; } _chargesDisplayInfo.SurplusTimes = _trnLicense.TriesLmt; _chargesDisplayInfo.CurrentMinutes = cacheTime; _chargesDisplayInfo.CurrentMileage = _trnLicense.MileageLmt; string chargeMode = _trnLicense.ChargeMode; if (chargeMode != null) { switch (chargeMode) { case "Time": { if (_trnLicense.TimeLmt > 0.0) { if (ts.TotalMinutes >= spanTime) { lastHandleTime = now; SqlLiteHelper.SaveChargesInfo(_chargesDisplayInfo); writeToDisk(); clearPostBackData(); } } else { StopCharging(); if (StopDelegate != null) { StopDelegate(); } } break; } case "Mileage": { if (cacheTime <= 0.0 || _trnLicense.MileageLmt > 0.0) { if (ts.TotalMinutes >= spanTime) { lastHandleTime = now; SqlLiteHelper.SaveChargesInfo(_chargesDisplayInfo); writeToDisk(); clearPostBackData(); } } else { StopCharging(); if (StopDelegate != null) { StopDelegate(); } } break; } case "Tries": { if (_trnLicense.TriesLmt >= 0) { if (cacheTime <= 0.0) { StopCharging(); _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "时间耗尽,训练已停止!"); if (_trnLicense.TriesLmt <= 0) { if (StopDelegate != null) { StopDelegate(); } } continue; } if (ts.TotalMinutes >= spanTime) { lastHandleTime = now; SqlLiteHelper.SaveChargesInfo(_chargesDisplayInfo); writeToDisk(); clearPostBackData(); } } else { StopCharging(); if (StopDelegate != null) { StopDelegate(); } } break; } default: { if (_trnLicense.TimeLmt > 0.0) { if (ts.TotalMinutes >= 5.0) { lastHandleTime = now; SqlLiteHelper.SaveChargesInfo(_chargesDisplayInfo); writeToDisk(); } } else { StopCharging(); if (StopDelegate != null) { StopDelegate(); } } break; } } } if (_currentInfoControl != null) { _currentInfoControl.CallChangesShowInfo(_chargesDisplayInfo); } Thread.Sleep(800); } } } }
private void CheckTrainProcThread() { bool canGo = false; bool isEnd = false; while (IsRunning) { if (_trainProcQueue.Count > 0) { _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "发现" + _trainProcQueue.Count + "条过程数据,开始处理……"); TrainProc tp; if (!_trainProcQueue.TryDequeue(out tp) || tp == null) { throw new InvalidOperationException("队列处理失败"); } _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "过程数据:" + tp.Code + "," + tp.Type); if (tp.Code == "10000" && tp.Type == "S") { cacheTime = 0; detailTime = 0; detailMileage = 0; detailTries = 0; startTime = DateTime.Now; canGo = true; isEnd = false; if (_trnLicense.TrainDetail == null) { _trnLicense.TrainDetail = new TrainDetail { TrainStartTs = startTime }; } else { detailMileage = _trnLicense.TrainDetail.TrainMileage; detailTime = _trnLicense.TrainDetail.TrainTime; detailTries = _trnLicense.TrainDetail.TrainTries; } if (_trnLicense.TrainDetail.TrainProcList == null) { _trnLicense.TrainDetail.TrainProcList = new List <TrainProc>(); } SaveTrainingInfo(tp); SqlLiteHelper.SaveChargesInfo(_chargesDisplayInfo); SqlLiteHelper.SaveTraningInfo(_trnLicense); writeToDisk(); _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "过程数据为10000,状态为S,初始化状态信息……"); IsCharging = true; _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "状态信息初始化完毕,开启训练线程……"); continue; } if (!canGo) { if (tp.Code == "10000" && tp.Type == "T") { StopCharging(); _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "训练未开始,关闭训练!"); } else { _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "训练未开始,当前数据无效,已丢弃!"); } continue; } if (tp.Code == "10000" && tp.Type == "T") { if (!IsRunning) { continue; } if (_trnLicense.TrainDetail != null) { _trnLicense.TrainDetail.TrainEndTs = DateTime.Now; } _lincenseState = LicenseState.Invaild; _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + @"过程数据为10000,状态为T,准备保存状态信息……"); SaveTrainingInfo(tp); StopTrain(true); _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "本次状态信息保存完毕,关闭训练界面,返回验证界面……"); continue; } if (tp.Code == "10000" && tp.Type == "E") { if (!isEnd) { isEnd = true; SaveTrainingInfo(tp); if (_trnLicense.ChargeMode == "Tries") { _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + @"过程数据为10000,状态为E,计费模式为[次数],检查许可信息……"); if (_trnLicense.TriesLmt <= 0) { _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "训练次数为0,停止计费……"); StopCharging(); if (StopDelegate != null) { StopDelegate(); } continue; } } StopCharging(); _sw.WriteLine(DateTime.Now.ToLocalTime() + ":" + "本次训练过程信息保存完毕,本次训练结束……"); } continue; } SaveTrainingInfo(tp); SqlLiteHelper.SaveChargesInfo(_chargesDisplayInfo); SqlLiteHelper.SaveTraningInfo(_trnLicense); writeToDisk(); } Thread.Sleep(100); } }
/// <summary> /// Creates the charg proc information. /// </summary> private static void CreateChargProcInfo() { SqlLiteHelper.CreatTabel("ChargeProcInfo", "CREATE TABLE ChargeProcInfo ( sfzmhm VARCHAR( 30 ) NOT NULL,autoid VARCHAR( 10 ),starttime VARCHAR( 25 ),endtime VARCHAR( 25 ),mode VARCHAR( 20 ),currentmileage NUMERIC( 20 ),currentminutes NUMERIC( 20 ),surplustimes INT );"); }
/// <summary> /// Creates the traning proc information. /// </summary> private static void CreateTraningProcInfo() { SqlLiteHelper.CreatTabel("TraningProcInfo", "CREATE TABLE TraningProcInfo (sfzmhm VARCHAR(30) NOT NULL,currenttime VARCHAR( 10 ),procinfo TEXT);"); }
/// <summary> /// Creates the traninees table. /// </summary> private static void CreateTranineesTable() { SqlLiteHelper.CreatTabel("Traninees", "CREATE TABLE Traninees (sfzmhm VARCHAR(30) NOT NULL,currenttime VARCHAR( 10 ),licenseinfo TEXT);"); }