123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- 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<ParamCheckServer> 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<ServerInfo> CurrInfo = new List<ServerInfo>();//记录错误信息
- 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);
- }
- /// <summary>
- /// 启动RMS系统
- /// </summary>
- 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<ServerInfo> GetInfo()
- {
- if (CurrInfo.Count <= 100)
- return CurrInfo;
- List<ServerInfo> templist = new List<ServerInfo>();
- 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;
- }
- }
- }
- }
|