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

374 lines
14 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. mqttDatasDic[ScreenDeviceType.大炒] = new ScreenModelMaxWok();
  80. mqttDatasDic[ScreenDeviceType.小炒] = new ScreenModelMinWok();
  81. mqttDatasDic[ScreenDeviceType.分餐机] = new ScreenModelSplitMeals();
  82. mqttDatasDic[ScreenDeviceType.煮面机] = new ScreenModelMorkS();
  83. }
  84. /// <summary>
  85. /// 设备数据
  86. /// </summary>
  87. public Dictionary<ScreenDeviceType, object> mqttDatasDic = new Dictionary<ScreenDeviceType, object>();
  88. /// <summary>
  89. /// 配置数据
  90. /// </summary>
  91. public Dictionary<string,string> PZdic=new Dictionary<string,string>();
  92. /// <summary>
  93. /// Mqtt消息队列
  94. /// </summary>
  95. public ConcurrentQueue<string> msg = new ConcurrentQueue<string>();
  96. #endregion
  97. #region 线程处理函数
  98. /// <summary>
  99. /// 线程服务
  100. /// </summary>
  101. public void ThreadServer()
  102. {
  103. ThreadManage.GetInstance().StartLong(new Action(() =>
  104. {
  105. try
  106. {
  107. if (IsRunning && IsRunningMqtt)
  108. {
  109. if (msg.Count > 0 && msg.TryDequeue(out string s))
  110. {
  111. SendScreenDataModel read= JsonConvert.DeserializeObject<SendScreenDataModel>(s);
  112. if (read != null && read.Value!=null)
  113. {
  114. switch (read.Name)
  115. {
  116. case ScreenDeviceType.大炒:
  117. mqttDatasDic[read.Name] = JsonConvert.DeserializeObject<ScreenModelMaxWok>(read.Value.ToString());
  118. break;
  119. case ScreenDeviceType.小炒:
  120. mqttDatasDic[read.Name] = JsonConvert.DeserializeObject<ScreenModelMinWok>(read.Value.ToString());
  121. break;
  122. case ScreenDeviceType.分餐机:
  123. mqttDatasDic[read.Name] = JsonConvert.DeserializeObject<ScreenModelSplitMeals>(read.Value.ToString());
  124. break;
  125. case ScreenDeviceType.煮面机:
  126. mqttDatasDic[read.Name] = JsonConvert.DeserializeObject<ScreenModelMorkS>(read.Value.ToString());
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. MessageLog.GetInstance.ShowEx($"{Name}:线程服务异常,原因:{ex.Message}");
  136. }
  137. Thread.Sleep(100);
  138. }), $"{Name},线程服务");
  139. }
  140. #endregion
  141. #region 启动、停止、健康检查
  142. /// <summary>
  143. /// 开始服务
  144. /// </summary>
  145. public void Start()
  146. {
  147. try
  148. {
  149. ReadSass();
  150. //连接MQTT、Redis
  151. Connection();
  152. IsRunning = true;
  153. }
  154. catch (Exception ex)
  155. {
  156. MessageLog.GetInstance.ShowEx($"{Name}:MQTT或者Redis启动时,连接失败,原因:{ex.Message}");
  157. }
  158. }
  159. /// <summary>
  160. /// 停止服务
  161. /// </summary>
  162. public void Stop()
  163. {
  164. try
  165. {
  166. //0.设置运行标志
  167. IsRunning = false;
  168. mqttHelper.CloseConnect();
  169. }
  170. catch (Exception ex)
  171. {
  172. MessageLog.GetInstance.ShowEx($"{Name}:MQTT或者Redis停止时异常,原因:{ex.Message}");
  173. }
  174. }
  175. /// <summary>
  176. /// 健康检查
  177. /// </summary>
  178. /// <param name="msg"></param>
  179. /// <returns></returns>
  180. public bool Check(out string msg)
  181. {
  182. string msgage = string.Empty;
  183. bool IsTrue = false;
  184. if (IsRunningMqtt && IsRunning)
  185. IsTrue = true;
  186. else
  187. {
  188. if (!IsRunningMqtt)
  189. msgage += "Redis断开连接.";
  190. IsTrue = false;
  191. msgage = $"{Name}:健康检查失败,原因:{msgage}";
  192. }
  193. msg = msgage;
  194. return IsTrue;
  195. }
  196. #endregion
  197. #region 调用事件
  198. /// <summary>
  199. /// 调用Sass接口拿到广告
  200. /// </summary>
  201. public void ReadSass()
  202. {
  203. try
  204. {
  205. string SaasRoute = System.Configuration.ConfigurationManager.AppSettings["SaasRoute"].ToString();
  206. string res = HttpRequestHelper.HttpGetRequest(SaasRoute);
  207. if (!string.IsNullOrEmpty(res))
  208. {
  209. HttpReturn httpReturn= JsonConvert.DeserializeObject<HttpReturn>(res);
  210. var Init =httpReturn?.data?.Devices?.Join(httpReturn?.data?.Stalls, t => t.GateId, x => x.Id, (t, x) => new AdDTO
  211. {
  212. Ad = x.Remaek,
  213. Device = t.Name,
  214. Stalls = x.Name,
  215. Address = t.Address,
  216. }).ToList();
  217. SaasRouteReturn= Init;
  218. }
  219. }
  220. catch (Exception ex)
  221. {
  222. }
  223. }
  224. /// <summary>
  225. /// 显示地址
  226. /// </summary>
  227. /// <param name="input"></param>
  228. /// <param name="view2"></param>
  229. public async void InitView2(string input, WebView2 view2, System.Windows.Controls.GroupBox group)
  230. {
  231. var height = 540;
  232. var width = 940;
  233. input = input.Replace("style=\"width: 100%;\"", "");
  234. StringBuilder sb = new StringBuilder();
  235. 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>");
  236. var html = sb.ToString();
  237. string name = $"{Application.StartupPath}\\Html\\{Name}.html";
  238. if (!System.IO.Directory.Exists($"{Application.StartupPath}\\Html"))
  239. {
  240. System.IO.Directory.CreateDirectory($"{Application.StartupPath}\\Html");//不存在就创建文件夹 }
  241. }
  242. if (!System.IO.File.Exists(name))
  243. {
  244. //----------生成htm文件------------------――
  245. try
  246. {
  247. using (StreamWriter sw = new StreamWriter(name, false, System.Text.Encoding.GetEncoding("UTF-8"))) //保存地址
  248. {
  249. sw.WriteLine(html);
  250. sw.Flush();
  251. sw.Close();
  252. }
  253. }
  254. catch
  255. {
  256. }
  257. }
  258. else
  259. {
  260. string text = System.IO.File.ReadAllText(name);
  261. if (text.Replace("\r\n", "") != html)
  262. {
  263. try
  264. {
  265. using (StreamWriter sw = new StreamWriter(name, false, System.Text.Encoding.GetEncoding("UTF-8"))) //保存地址
  266. {
  267. sw.WriteLine(html);
  268. sw.Flush();
  269. sw.Close();
  270. }
  271. }
  272. catch
  273. {
  274. }
  275. }
  276. }
  277. view2.Source = new Uri(name);
  278. }
  279. /// <summary>
  280. /// 读取配置
  281. /// </summary>
  282. public void ReadPZ()
  283. {
  284. System.Configuration.ConfigurationManager.AppSettings.AllKeys?.ToList().ForEach(key => {
  285. PZdic[key]= System.Configuration.ConfigurationManager.AppSettings[key].ToString();
  286. });
  287. }
  288. /// <summary>
  289. /// 初始化连接Redis MQTT
  290. /// </summary>
  291. public void Connection()
  292. {
  293. try
  294. {
  295. //2.Mqtt连接
  296. if (!string.IsNullOrEmpty(this.MQTTConnection) && this.MQTTConnection.Contains(','))
  297. {
  298. string[] mqttcom = this.MQTTConnection.Split(',');
  299. if (mqttcom != null && mqttcom.Count() == 4)
  300. {
  301. mqttHelper.Connect(mqttcom[2], mqttcom[3], mqttcom[0], int.Parse(mqttcom[1]), $"{Name}:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
  302. }
  303. }
  304. mqttHelper.Disconnect = new Action(() =>
  305. {
  306. IsRunningMqtt = false;
  307. MessageLog.GetInstance.ShowEx($"{Name}:MQTT异常断开....");
  308. });
  309. mqttHelper.ConnectOk = new Action(() =>
  310. {
  311. IsRunningMqtt = true;//连接成功
  312. if (Name.Contains(","))
  313. {
  314. Name.Split(',')?.ToList().ForEach(x =>
  315. {
  316. if (x == "大炒")
  317. {
  318. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.大炒));
  319. }else if (x == "小炒")
  320. {
  321. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.小炒));
  322. }
  323. else if (x == "分餐机")
  324. {
  325. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.分餐机));
  326. }
  327. else if (x == "煮面机")
  328. {
  329. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.煮面机));
  330. }
  331. });
  332. }
  333. else
  334. {
  335. if (Name == "大炒")
  336. {
  337. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.大炒));
  338. }
  339. else if (Name == "小炒")
  340. {
  341. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.小炒));
  342. }
  343. else if (Name == "分餐机")
  344. {
  345. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.分餐机));
  346. }
  347. else if (Name == "煮面机")
  348. {
  349. mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.煮面机));
  350. }
  351. }
  352. });
  353. mqttHelper.MessageRecive = new Action<string>((s) => { msg.Enqueue(s); });
  354. }
  355. catch (Exception ex)
  356. {
  357. MessageLog.GetInstance.ShowEx($"{Name}:初始化连接 MQTT,原因:{ex.Message}");
  358. }
  359. }
  360. #endregion
  361. }
  362. }