|
- 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
- {
- /// <summary>
- /// 主函数
- /// </summary>
- public class Main
- {
- #region 接口继承变量
- /// <summary>
- /// Redis连接信息
- /// </summary>
- public string RedisConnection { get; set; }
- /// <summary>
- /// 业务名称
- /// </summary>
- private string _Name = string.Empty;
- public string Name
- {
- get { return _Name; }
- set
- {
- _Name = value;
- ThreadServer();
- }
- }
- /// <summary>
- /// 是否自动重启(出现健康检查未通过时)
- /// </summary>
- public bool AutoRestart { get; set; }
- #endregion
-
- #region 自建变量
- /// <summary>
- /// 是否运行
- /// </summary>
- public bool IsRunning { get; set; }
- /// <summary>
- /// Redis是否运行
- /// </summary>
- public bool IsRunningReids => RedisHelper.GetInstance.IsConnected();
- /// <summary>
- /// 主函数
- /// </summary>
- 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();
- }
- /// <summary>
- /// 设备数据
- /// </summary>
- public List<RedisDataModel> reeisDatas { get; set; } = new List<RedisDataModel>();
- /// <summary>
- /// 告警数据
- /// </summary>
- public List<RedisDataModel> reeisDatasAic { get; set; } = new List<RedisDataModel>();
- #endregion
-
- #region 线程处理函数
- /// <summary>
- /// 线程服务
- /// </summary>
- public void ThreadServer()
- {
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- try
- {
- if (IsRunning && IsRunningReids)
- {
- //1.读取Redis变量
- List<RedisDataModel> Values = RedisHelper.GetInstance.Read<List<RedisDataModel>>($"{Name}[Device]").Content;
- List<RedisDataModel> reeisDatasAic = RedisHelper.GetInstance.Read<List<RedisDataModel>>($"{Name}[Alarm]").Content;
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"{Name}:线程服务异常,原因:{ex.Message}");
- }
- Thread.Sleep(100);
- }), $"{Name},线程服务");
- }
- #endregion
-
- #region 启动、停止、健康检查
- /// <summary>
- /// 开始服务
- /// </summary>
- public void Start()
- {
- try
- {
- //连接MQTT、Redis
- Connection();
- IsRunning = true;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"{Name}:MQTT或者Redis启动时,连接失败,原因:{ex.Message}");
- }
- }
- /// <summary>
- /// 停止服务
- /// </summary>
- public void Stop()
- {
- try
- {
- //0.设置运行标志
- IsRunning = false;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"{Name}:MQTT或者Redis停止时异常,原因:{ex.Message}");
- }
- }
- /// <summary>
- /// 健康检查
- /// </summary>
- /// <param name="msg"></param>
- /// <returns></returns>
- 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 调用事件
- /// <summary>
- /// 初始化连接Redis MQTT
- /// </summary>
- 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
- }
-
-
- /// <summary>
- /// Redis 数据存储格式
- /// </summary>
- public class RedisDataModel
- {
- public string VarName { get; set; }
- public string VarVaule { get; set; }
- public EDataType DataType { get; set; }
- }
-
- /// <summary>
- /// 数据类型枚举
- /// </summary>
- public enum EDataType
- {
- Bool = 1,
- Byte = 2,
- Int = 3,
- Word = 4,
- Dint = 5,
- Dword = 6,
- Float = 7,
- Double = 8,
- String = 9,
- }
- }
|