ParamCheckServer.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using DllDiodesMesEntity;
  5. using DllEapDal;
  6. using Microsoft.Extensions.Logging;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading;
  11. namespace DllParamCheckServer
  12. {
  13. public class ParamCheckServer:IParamCheckServer
  14. {
  15. private ILogger loger = null;
  16. public ParamCheckServer(ILogger<ParamCheckServer> loger)
  17. {
  18. this.loger = loger;
  19. SleepTime = int.Parse(AppConfigurtaionServices.Configuration["SleepTime"]);
  20. CheckTime = int.Parse(AppConfigurtaionServices.Configuration["CheckTime"]);
  21. }
  22. private int SleepTime = 0;
  23. private int CheckTime = 0;
  24. private long CurrCount = 0;
  25. private List<ServerInfo> CurrInfo = new List<ServerInfo>();//记录错误信息
  26. private int CurrStatus = -1;//未启动状态
  27. private Thread CheckThread = null;
  28. private bool IsRun = false;
  29. public void SetText(string str)
  30. {
  31. this.loger.LogError(str);
  32. if (CurrInfo.Count > 100)
  33. CurrInfo.Clear();
  34. int id = 1;
  35. if (CurrInfo.Count > 0)
  36. {
  37. id = CurrInfo.Max(t => t.ID);
  38. id++;
  39. }
  40. ServerInfo entity = new ServerInfo();
  41. entity.ID = id;
  42. entity.Info = str;
  43. CurrInfo.Add(entity);
  44. }
  45. /// <summary>
  46. /// 启动RMS系统
  47. /// </summary>
  48. public int Start(ref string errorinfo)
  49. {
  50. try
  51. {
  52. Stop(ref errorinfo);
  53. if (errorinfo != "")
  54. {
  55. SetText($"停止服务发生错误,错误信息为:{errorinfo}");
  56. }
  57. int result = StartCheckThread(ref errorinfo);
  58. if(result<=0)
  59. {
  60. SetText(errorinfo);
  61. }
  62. else
  63. {
  64. SetText("启动成功。");
  65. }
  66. CurrStatus = 1;
  67. return 1;
  68. }
  69. catch (Exception ex)
  70. {
  71. SetText(ex.Message.ToString());
  72. return -1;
  73. }
  74. }
  75. //启动总的检查线程,此线程应该一直运行,直到程序退出
  76. public int StartCheckThread(ref string errorinfo)
  77. {
  78. try
  79. {
  80. IsRun = true;
  81. //成功后,启动接受线程
  82. CheckThread = new Thread(CheckThreadFun);
  83. CheckThread.Start();
  84. return 1;
  85. }
  86. catch (Exception ex)
  87. {
  88. IsRun = false;
  89. errorinfo = ex.Message.ToString();
  90. return -1;
  91. }
  92. }
  93. public int StopCheckThread(ref string errorinfo)
  94. {
  95. try
  96. {
  97. IsRun = false;
  98. if(CheckThread!=null)
  99. CheckThread.Abort();
  100. CheckThread = null;
  101. return 1;
  102. }
  103. catch (Exception ex)
  104. {
  105. CheckThread = null;
  106. errorinfo = ex.Message.ToString();
  107. return -1;
  108. }
  109. }
  110. private void CheckThreadFun()
  111. {
  112. try
  113. {
  114. while (IsRun)
  115. {
  116. ProcessBusiness();
  117. Thread.Sleep(SleepTime);
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. IsRun = false;
  123. loger.LogError("线程出错:" + ex.Message.ToString());
  124. }
  125. }
  126. private int ProcessBusiness()
  127. {
  128. string errorinfo = "";
  129. IDatabase eapdb = null;
  130. IDatabase mesdb = null;
  131. try
  132. {
  133. eapdb = DbFactory.Base("eap");
  134. //mesdb = DbFactory.Base("mes");
  135. ProcessBusiness busi = new ProcessBusiness(eapdb, loger,CheckTime);
  136. int result = busi.ParamCheck(ref errorinfo);
  137. if(result<=0)
  138. {
  139. loger.LogError(errorinfo);
  140. return -1;
  141. }
  142. return 1;
  143. }
  144. catch(Exception ex)
  145. {
  146. loger.LogError("ProcessBusiness出错:" + ex.Message.ToString());
  147. return -1;
  148. }
  149. finally
  150. {
  151. if (eapdb != null)
  152. eapdb.Close();
  153. //if (mesdb != null)
  154. // mesdb.Close();
  155. }
  156. }
  157. public int GetStatus(ref string errorinfo)
  158. {
  159. if (CurrStatus <= 0 && CurrInfo.Count > 0)
  160. {
  161. errorinfo = CurrInfo.Last().Info;
  162. }
  163. return CurrStatus;
  164. }
  165. public long GetAccount()
  166. {
  167. return CurrCount;
  168. }
  169. public List<ServerInfo> GetInfo()
  170. {
  171. if (CurrInfo.Count <= 100)
  172. return CurrInfo;
  173. List<ServerInfo> templist = new List<ServerInfo>();
  174. for (int i = CurrInfo.Count - 100; i < CurrInfo.Count; i++)
  175. templist.Add(CurrInfo[i]);
  176. return templist;
  177. }
  178. public int Stop(ref string errorinfo)
  179. {
  180. try
  181. {
  182. CurrStatus = -1;
  183. SetText("停止服务成功。");
  184. return 1;
  185. }
  186. catch (Exception ex)
  187. {
  188. SetText(ex.Message.ToString());
  189. return -1;
  190. }
  191. }
  192. public int ClearInfo(ref string errorinfo)
  193. {
  194. try
  195. {
  196. CurrInfo.Clear();
  197. return 1;
  198. }
  199. catch (Exception ex)
  200. {
  201. SetText(ex.Message.ToString());
  202. return -1;
  203. }
  204. }
  205. }
  206. }