终端一体化运控平台
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

350 linhas
13 KiB

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