终端一体化运控平台
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.
 
 
 

272 rivejä
9.6 KiB

  1. using BPA.Communication;
  2. using BPA.Helper;
  3. using BPA.Message;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace BPASmartClient.ScreenLib
  13. {
  14. /// <summary>
  15. /// 主函数
  16. /// </summary>
  17. public class Main
  18. {
  19. #region 接口继承变量
  20. /// <summary>
  21. /// MQTT连接信息
  22. /// </summary>
  23. public string MQTTConnection { get; set; }
  24. /// <summary>
  25. /// 业务名称
  26. /// </summary>
  27. private string _Name = string.Empty;
  28. public string Name
  29. {
  30. get { return _Name; }
  31. set
  32. {
  33. _Name = value;
  34. ThreadServer();
  35. }
  36. }
  37. /// <summary>
  38. /// 是否自动重启(出现健康检查未通过时)
  39. /// </summary>
  40. public bool AutoRestart { get; set; }
  41. #endregion
  42. #region 自建变量
  43. /// <summary>
  44. /// 是否运行
  45. /// </summary>
  46. public bool IsRunning { get; set; }
  47. /// <summary>
  48. /// Mqtt是否运行
  49. /// </summary>
  50. public bool IsRunningMqtt = false;
  51. /// <summary>
  52. /// Mqtt
  53. /// </summary>
  54. public MqttHelper mqttHelper = new MqttHelper();
  55. /// <summary>
  56. /// 主函数
  57. /// </summary>
  58. private static volatile Main _Instance;
  59. public static Main GetInstance => _Instance ?? (_Instance = new Main());
  60. public Main()
  61. {
  62. ReadPZ();
  63. if (PZdic.ContainsKey("MQTTConnection"))
  64. MQTTConnection = PZdic["MQTTConnection"];
  65. if (PZdic.ContainsKey("DeviceMC"))
  66. Name = PZdic["DeviceMC"];
  67. }
  68. /// <summary>
  69. /// 设备数据
  70. /// </summary>
  71. public Dictionary<ScreenDeviceType, object> mqttDatasDic = new Dictionary<ScreenDeviceType, object>();
  72. /// <summary>
  73. /// 配置数据
  74. /// </summary>
  75. public Dictionary<string,string> PZdic=new Dictionary<string,string>();
  76. /// <summary>
  77. /// Mqtt消息队列
  78. /// </summary>
  79. public ConcurrentQueue<string> msg = new ConcurrentQueue<string>();
  80. #endregion
  81. #region 线程处理函数
  82. /// <summary>
  83. /// 线程服务
  84. /// </summary>
  85. public void ThreadServer()
  86. {
  87. ThreadManage.GetInstance().StartLong(new Action(() =>
  88. {
  89. try
  90. {
  91. if (IsRunning && IsRunningMqtt)
  92. {
  93. if (msg.Count > 0 && msg.TryDequeue(out string s))
  94. {
  95. SendScreenDataModel read= JsonConvert.DeserializeObject<SendScreenDataModel>(s);
  96. if (read != null && read.Value!=null)
  97. {
  98. switch (read.Name)
  99. {
  100. case ScreenDeviceType.大炒:
  101. mqttDatasDic[read.Name] = JsonConvert.DeserializeObject<ScreenModelMaxWok>(read.Value.ToString());
  102. break;
  103. case ScreenDeviceType.小炒:
  104. mqttDatasDic[read.Name] = JsonConvert.DeserializeObject<ScreenModelMinWok>(read.Value.ToString());
  105. break;
  106. case ScreenDeviceType.分餐机:
  107. mqttDatasDic[read.Name] = JsonConvert.DeserializeObject<ScreenModelSplitMeals>(read.Value.ToString());
  108. break;
  109. case ScreenDeviceType.煮面机:
  110. mqttDatasDic[read.Name] = JsonConvert.DeserializeObject<ScreenModelMorkS>(read.Value.ToString());
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. MessageLog.GetInstance.ShowEx($"{Name}:线程服务异常,原因:{ex.Message}");
  120. }
  121. Thread.Sleep(100);
  122. }), $"{Name},线程服务");
  123. }
  124. #endregion
  125. #region 启动、停止、健康检查
  126. /// <summary>
  127. /// 开始服务
  128. /// </summary>
  129. public void Start()
  130. {
  131. try
  132. {
  133. //连接MQTT、Redis
  134. Connection();
  135. IsRunning = true;
  136. }
  137. catch (Exception ex)
  138. {
  139. MessageLog.GetInstance.ShowEx($"{Name}:MQTT或者Redis启动时,连接失败,原因:{ex.Message}");
  140. }
  141. }
  142. /// <summary>
  143. /// 停止服务
  144. /// </summary>
  145. public void Stop()
  146. {
  147. try
  148. {
  149. //0.设置运行标志
  150. IsRunning = false;
  151. mqttHelper.CloseConnect();
  152. }
  153. catch (Exception ex)
  154. {
  155. MessageLog.GetInstance.ShowEx($"{Name}:MQTT或者Redis停止时异常,原因:{ex.Message}");
  156. }
  157. }
  158. /// <summary>
  159. /// 健康检查
  160. /// </summary>
  161. /// <param name="msg"></param>
  162. /// <returns></returns>
  163. public bool Check(out string msg)
  164. {
  165. string msgage = string.Empty;
  166. bool IsTrue = false;
  167. if (IsRunningMqtt && IsRunning)
  168. IsTrue = true;
  169. else
  170. {
  171. if (!IsRunningMqtt)
  172. msgage += "Redis断开连接.";
  173. IsTrue = false;
  174. msgage = $"{Name}:健康检查失败,原因:{msgage}";
  175. }
  176. msg = msgage;
  177. return IsTrue;
  178. }
  179. #endregion
  180. #region 调用事件
  181. /// <summary>
  182. /// 读取配置
  183. /// </summary>
  184. public void ReadPZ()
  185. {
  186. System.Configuration.ConfigurationManager.AppSettings.AllKeys?.ToList().ForEach(key => {
  187. PZdic[key]= System.Configuration.ConfigurationManager.AppSettings[key].ToString();
  188. });
  189. }
  190. /// <summary>
  191. /// 初始化连接Redis MQTT
  192. /// </summary>
  193. public void Connection()
  194. {
  195. try
  196. {
  197. //2.Mqtt连接
  198. if (!string.IsNullOrEmpty(this.MQTTConnection) && this.MQTTConnection.Contains(','))
  199. {
  200. string[] mqttcom = this.MQTTConnection.Split(',');
  201. if (mqttcom != null && mqttcom.Count() == 4)
  202. {
  203. mqttHelper.Connect(mqttcom[2], mqttcom[3], mqttcom[0], int.Parse(mqttcom[1]), $"{Name}:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
  204. }
  205. }
  206. mqttHelper.Disconnect = new Action(() =>
  207. {
  208. IsRunningMqtt = false;
  209. MessageLog.GetInstance.ShowEx($"{Name}:MQTT异常断开....");
  210. });
  211. mqttHelper.ConnectOk = new Action(() =>
  212. {
  213. IsRunningMqtt = true;//连接成功
  214. if (Name.Contains(","))
  215. {
  216. Name.Split(',')?.ToList().ForEach(x =>
  217. {
  218. if (x == "大炒")
  219. {
  220. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.大炒));
  221. }else if (x == "小炒")
  222. {
  223. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.小炒));
  224. }
  225. else if (x == "分餐机")
  226. {
  227. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.分餐机));
  228. }
  229. else if (x == "煮面机")
  230. {
  231. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.煮面机));
  232. }
  233. });
  234. }
  235. else
  236. {
  237. if (Name == "大炒")
  238. {
  239. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.大炒));
  240. }
  241. else if (Name == "小炒")
  242. {
  243. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.小炒));
  244. }
  245. else if (Name == "分餐机")
  246. {
  247. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.分餐机));
  248. }
  249. else if (Name == "煮面机")
  250. {
  251. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.煮面机));
  252. }
  253. }
  254. });
  255. mqttHelper.MessageRecive = new Action<string>((s) => { msg.Enqueue(s); });
  256. }
  257. catch (Exception ex)
  258. {
  259. MessageLog.GetInstance.ShowEx($"{Name}:初始化连接 MQTT,原因:{ex.Message}");
  260. }
  261. }
  262. #endregion
  263. }
  264. }