public void StopCharging() { IsChargingThread = false; IsCharging = false; SqlLiteHelper.SaveTranineesInfo(_trnLicense); SqlLiteHelper.SaveChargesInfo(_chargesDisplayInfo); SqlLiteHelper.SaveTraningInfo(_trnLicense); writeToDisk(); if (StopNewExamDelegate != null) { StopNewExamDelegate(); } }
/// <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); } }