using BPA.Communication; using BPA.Helper; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace BPASmartClient.ScreenLib { /// /// 主函数 /// public class Main { #region 接口继承变量 /// /// Redis连接信息 /// public string RedisConnection { get; set; } /// /// 业务名称 /// private string _Name = string.Empty; public string Name { get { return _Name; } set { _Name = value; ThreadServer(); } } /// /// 是否自动重启(出现健康检查未通过时) /// public bool AutoRestart { get; set; } #endregion #region 自建变量 /// /// 是否运行 /// public bool IsRunning { get; set; } /// /// Redis是否运行 /// public bool IsRunningReids => RedisHelper.GetInstance.IsConnected(); /// /// 主函数 /// private static volatile Main _Instance; public static Main GetInstance => _Instance ?? (_Instance = new Main()); public Main() { RedisConnection= System.Configuration.ConfigurationManager.AppSettings["RedisConnection"].ToString(); Name = System.Configuration.ConfigurationManager.AppSettings["DeviceMC"].ToString(); } /// /// 设备数据 /// public List reeisDatas { get; set; } = new List(); /// /// 告警数据 /// public List reeisDatasAic { get; set; } = new List(); #endregion #region 线程处理函数 /// /// 线程服务 /// public void ThreadServer() { ThreadManage.GetInstance().StartLong(new Action(() => { try { if (IsRunning && IsRunningReids) { //1.读取Redis变量 List Values = RedisHelper.GetInstance.Read>($"{Name}[Device]").Content; List reeisDatasAic = RedisHelper.GetInstance.Read>($"{Name}[Alarm]").Content; } } catch (Exception ex) { MessageLog.GetInstance.ShowEx($"{Name}:线程服务异常,原因:{ex.Message}"); } Thread.Sleep(100); }), $"{Name},线程服务"); } #endregion #region 启动、停止、健康检查 /// /// 开始服务 /// public void Start() { try { //连接MQTT、Redis Connection(); IsRunning = true; } catch (Exception ex) { MessageLog.GetInstance.ShowEx($"{Name}:MQTT或者Redis启动时,连接失败,原因:{ex.Message}"); } } /// /// 停止服务 /// public void Stop() { try { //0.设置运行标志 IsRunning = false; } catch (Exception ex) { MessageLog.GetInstance.ShowEx($"{Name}:MQTT或者Redis停止时异常,原因:{ex.Message}"); } } /// /// 健康检查 /// /// /// public bool Check(out string msg) { string msgage = string.Empty; bool IsTrue = false; if (IsRunningReids && IsRunning) IsTrue = true; else { if (!IsRunningReids) msgage += "Redis断开连接."; IsTrue = false; msgage = $"{Name}:健康检查失败,原因:{msgage}"; } msg = msgage; return IsTrue; } #endregion #region 调用事件 /// /// 初始化连接Redis MQTT /// public void Connection() { try { //1.连接Redis,如果已经连接过了 那么自动不会去连接 if (!string.IsNullOrEmpty(this.RedisConnection) && this.RedisConnection.Contains(',')) { string[] rediscom = this.RedisConnection.Split(','); if (rediscom != null && rediscom.Count() == 4) RedisHelper.GetInstance.Connect(new ConfigurationOptions() { ServerAddress = $"{rediscom[0]}:{int.Parse(rediscom[1])}", Password = rediscom[2] }); } } catch (Exception ex) { MessageLog.GetInstance.ShowEx($"{Name}:初始化连接Redis MQTT,原因:{ex.Message}"); } } #endregion } /// /// Redis 数据存储格式 /// public class RedisDataModel { public string VarName { get; set; } public string VarVaule { get; set; } public EDataType DataType { get; set; } } /// /// 数据类型枚举 /// public enum EDataType { Bool = 1, Byte = 2, Int = 3, Word = 4, Dint = 5, Dword = 6, Float = 7, Double = 8, String = 9, } }