using Cksoft.Data; using Cksoft.Data.Repository; using Cksoft.Unity; using DllDiodesMesEntity; using DllEapDal; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading; namespace DllParamCheckServer { public class ParamCheckServer:IParamCheckServer { private ILogger loger = null; public ParamCheckServer(ILogger loger) { this.loger = loger; SleepTime = int.Parse(AppConfigurtaionServices.Configuration["SleepTime"]); CheckTime = int.Parse(AppConfigurtaionServices.Configuration["CheckTime"]); } private int SleepTime = 0; private int CheckTime = 0; private long CurrCount = 0; private List CurrInfo = new List();//记录错误信息 private int CurrStatus = -1;//未启动状态 private Thread CheckThread = null; private bool IsRun = false; public void SetText(string str) { this.loger.LogError(str); if (CurrInfo.Count > 100) CurrInfo.Clear(); int id = 1; if (CurrInfo.Count > 0) { id = CurrInfo.Max(t => t.ID); id++; } ServerInfo entity = new ServerInfo(); entity.ID = id; entity.Info = str; CurrInfo.Add(entity); } /// /// 启动RMS系统 /// public int Start(ref string errorinfo) { try { Stop(ref errorinfo); if (errorinfo != "") { SetText($"停止服务发生错误,错误信息为:{errorinfo}"); } int result = StartCheckThread(ref errorinfo); if(result<=0) { SetText(errorinfo); } else { SetText("启动成功。"); } CurrStatus = 1; return 1; } catch (Exception ex) { SetText(ex.Message.ToString()); return -1; } } //启动总的检查线程,此线程应该一直运行,直到程序退出 public int StartCheckThread(ref string errorinfo) { try { IsRun = true; //成功后,启动接受线程 CheckThread = new Thread(CheckThreadFun); CheckThread.Start(); return 1; } catch (Exception ex) { IsRun = false; errorinfo = ex.Message.ToString(); return -1; } } public int StopCheckThread(ref string errorinfo) { try { IsRun = false; if(CheckThread!=null) CheckThread.Abort(); CheckThread = null; return 1; } catch (Exception ex) { CheckThread = null; errorinfo = ex.Message.ToString(); return -1; } } private void CheckThreadFun() { try { while (IsRun) { ProcessBusiness(); Thread.Sleep(SleepTime); } } catch (Exception ex) { IsRun = false; loger.LogError("线程出错:" + ex.Message.ToString()); } } private int ProcessBusiness() { string errorinfo = ""; IDatabase eapdb = null; IDatabase mesdb = null; try { eapdb = DbFactory.Base("eap"); //mesdb = DbFactory.Base("mes"); ProcessBusiness busi = new ProcessBusiness(eapdb, loger,CheckTime); int result = busi.ParamCheck(ref errorinfo); if(result<=0) { loger.LogError(errorinfo); return -1; } return 1; } catch(Exception ex) { loger.LogError("ProcessBusiness出错:" + ex.Message.ToString()); return -1; } finally { if (eapdb != null) eapdb.Close(); //if (mesdb != null) // mesdb.Close(); } } public int GetStatus(ref string errorinfo) { if (CurrStatus <= 0 && CurrInfo.Count > 0) { errorinfo = CurrInfo.Last().Info; } return CurrStatus; } public long GetAccount() { return CurrCount; } public List GetInfo() { if (CurrInfo.Count <= 100) return CurrInfo; List templist = new List(); for (int i = CurrInfo.Count - 100; i < CurrInfo.Count; i++) templist.Add(CurrInfo[i]); return templist; } public int Stop(ref string errorinfo) { try { CurrStatus = -1; SetText("停止服务成功。"); return 1; } catch (Exception ex) { SetText(ex.Message.ToString()); return -1; } } public int ClearInfo(ref string errorinfo) { try { CurrInfo.Clear(); return 1; } catch (Exception ex) { SetText(ex.Message.ToString()); return -1; } } } }