终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

207 lines
6.0 KiB

  1. using BPA.Communication;
  2. using BPA.Helper;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace BPASmartClient.ScreenLib
  11. {
  12. /// <summary>
  13. /// 主函数
  14. /// </summary>
  15. public class Main
  16. {
  17. #region 接口继承变量
  18. /// <summary>
  19. /// Redis连接信息
  20. /// </summary>
  21. public string RedisConnection { get; set; }
  22. /// <summary>
  23. /// 业务名称
  24. /// </summary>
  25. private string _Name = string.Empty;
  26. public string Name
  27. {
  28. get { return _Name; }
  29. set
  30. {
  31. _Name = value;
  32. ThreadServer();
  33. }
  34. }
  35. /// <summary>
  36. /// 是否自动重启(出现健康检查未通过时)
  37. /// </summary>
  38. public bool AutoRestart { get; set; }
  39. #endregion
  40. #region 自建变量
  41. /// <summary>
  42. /// 是否运行
  43. /// </summary>
  44. public bool IsRunning { get; set; }
  45. /// <summary>
  46. /// Redis是否运行
  47. /// </summary>
  48. public bool IsRunningReids => RedisHelper.GetInstance.IsConnected();
  49. /// <summary>
  50. /// 主函数
  51. /// </summary>
  52. private static volatile Main _Instance;
  53. public static Main GetInstance => _Instance ?? (_Instance = new Main());
  54. public Main() {
  55. RedisConnection= System.Configuration.ConfigurationManager.AppSettings["RedisConnection"].ToString();
  56. Name = System.Configuration.ConfigurationManager.AppSettings["DeviceMC"].ToString();
  57. }
  58. /// <summary>
  59. /// 设备数据
  60. /// </summary>
  61. public List<RedisDataModel> reeisDatas { get; set; } = new List<RedisDataModel>();
  62. /// <summary>
  63. /// 告警数据
  64. /// </summary>
  65. public List<RedisDataModel> reeisDatasAic { get; set; } = new List<RedisDataModel>();
  66. #endregion
  67. #region 线程处理函数
  68. /// <summary>
  69. /// 线程服务
  70. /// </summary>
  71. public void ThreadServer()
  72. {
  73. ThreadManage.GetInstance().StartLong(new Action(() =>
  74. {
  75. try
  76. {
  77. if (IsRunning && IsRunningReids)
  78. {
  79. //1.读取Redis变量
  80. List<RedisDataModel> Values = RedisHelper.GetInstance.Read<List<RedisDataModel>>($"{Name}[Device]").Content;
  81. List<RedisDataModel> reeisDatasAic = RedisHelper.GetInstance.Read<List<RedisDataModel>>($"{Name}[Alarm]").Content;
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. MessageLog.GetInstance.ShowEx($"{Name}:线程服务异常,原因:{ex.Message}");
  87. }
  88. Thread.Sleep(100);
  89. }), $"{Name},线程服务");
  90. }
  91. #endregion
  92. #region 启动、停止、健康检查
  93. /// <summary>
  94. /// 开始服务
  95. /// </summary>
  96. public void Start()
  97. {
  98. try
  99. {
  100. //连接MQTT、Redis
  101. Connection();
  102. IsRunning = true;
  103. }
  104. catch (Exception ex)
  105. {
  106. MessageLog.GetInstance.ShowEx($"{Name}:MQTT或者Redis启动时,连接失败,原因:{ex.Message}");
  107. }
  108. }
  109. /// <summary>
  110. /// 停止服务
  111. /// </summary>
  112. public void Stop()
  113. {
  114. try
  115. {
  116. //0.设置运行标志
  117. IsRunning = false;
  118. }
  119. catch (Exception ex)
  120. {
  121. MessageLog.GetInstance.ShowEx($"{Name}:MQTT或者Redis停止时异常,原因:{ex.Message}");
  122. }
  123. }
  124. /// <summary>
  125. /// 健康检查
  126. /// </summary>
  127. /// <param name="msg"></param>
  128. /// <returns></returns>
  129. public bool Check(out string msg)
  130. {
  131. string msgage = string.Empty;
  132. bool IsTrue = false;
  133. if (IsRunningReids && IsRunning)
  134. IsTrue = true;
  135. else
  136. {
  137. if (!IsRunningReids)
  138. msgage += "Redis断开连接.";
  139. IsTrue = false;
  140. msgage = $"{Name}:健康检查失败,原因:{msgage}";
  141. }
  142. msg = msgage;
  143. return IsTrue;
  144. }
  145. #endregion
  146. #region 调用事件
  147. /// <summary>
  148. /// 初始化连接Redis MQTT
  149. /// </summary>
  150. public void Connection()
  151. {
  152. try
  153. {
  154. //1.连接Redis,如果已经连接过了 那么自动不会去连接
  155. if (!string.IsNullOrEmpty(this.RedisConnection) && this.RedisConnection.Contains(','))
  156. {
  157. string[] rediscom = this.RedisConnection.Split(',');
  158. if (rediscom != null && rediscom.Count() == 4)
  159. RedisHelper.GetInstance.Connect(new ConfigurationOptions()
  160. {
  161. ServerAddress = $"{rediscom[0]}:{int.Parse(rediscom[1])}",
  162. Password = rediscom[2]
  163. });
  164. }
  165. }
  166. catch (Exception ex)
  167. {
  168. MessageLog.GetInstance.ShowEx($"{Name}:初始化连接Redis MQTT,原因:{ex.Message}");
  169. }
  170. }
  171. #endregion
  172. }
  173. /// <summary>
  174. /// Redis 数据存储格式
  175. /// </summary>
  176. public class RedisDataModel
  177. {
  178. public string VarName { get; set; }
  179. public string VarVaule { get; set; }
  180. public EDataType DataType { get; set; }
  181. }
  182. /// <summary>
  183. /// 数据类型枚举
  184. /// </summary>
  185. public enum EDataType
  186. {
  187. Bool = 1,
  188. Byte = 2,
  189. Int = 3,
  190. Word = 4,
  191. Dint = 5,
  192. Dword = 6,
  193. Float = 7,
  194. Double = 8,
  195. String = 9,
  196. }
  197. }