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

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