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

325 lines
12 KiB

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