|
- using BPA.Communication;
- using BPA.Helper;
- using BPA.Message;
- using IWshRuntimeLibrary;
- using Microsoft.Web.WebView2.Wpf;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- using System.Windows.Forms;
- using System.Windows.Shapes;
- using System.Xml;
-
- namespace BPASmartClient.ScreenLib
- {
- /// <summary>
- /// 主函数
- /// </summary>
- public class Main
- {
- #region 接口继承变量
- /// <summary>
- /// MQTT连接信息
- /// </summary>
- public string MQTTConnection { get; set; }
- /// <summary>
- /// 业务名称
- /// </summary>
- private string _Name = string.Empty;
- public string Name
- {
- get { return _Name; }
- set
- {
- _Name = value;
- ThreadServer();
- }
- }
- /// <summary>
- /// 是否自动重启(出现健康检查未通过时)
- /// </summary>
- public bool AutoRestart { get; set; }
- #endregion
-
- #region 自建变量
- /// <summary>
- /// 分餐机广告
- /// </summary>
- public List<AdDTO> SaasRouteReturn =new List<AdDTO> { };
- /// <summary>
- /// 是否运行
- /// </summary>
- public bool IsRunning { get; set; }
- /// <summary>
- /// Mqtt是否运行
- /// </summary>
- public bool IsRunningMqtt = false;
- /// <summary>
- /// Mqtt
- /// </summary>
- public MqttHelper mqttHelper = new MqttHelper();
- /// <summary>
- /// 主函数
- /// </summary>
- private static volatile Main _Instance;
- public static Main GetInstance => _Instance ?? (_Instance = new Main());
- public Main()
- {
- ReadPZ();
- if (PZdic.ContainsKey("MQTTConnection"))
- MQTTConnection = PZdic["MQTTConnection"];
- if (PZdic.ContainsKey("DeviceMC"))
- Name = PZdic["DeviceMC"];
- }
- /// <summary>
- /// 设备数据
- /// </summary>
- public Dictionary<ScreenDeviceType, object> mqttDatasDic = new Dictionary<ScreenDeviceType, object>();
- /// <summary>
- /// 配置数据
- /// </summary>
- public Dictionary<string,string> PZdic=new Dictionary<string,string>();
- /// <summary>
- /// Mqtt消息队列
- /// </summary>
- public ConcurrentQueue<string> msg = new ConcurrentQueue<string>();
- #endregion
-
- #region 线程处理函数
- /// <summary>
- /// 线程服务
- /// </summary>
- public void ThreadServer()
- {
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- try
- {
- if (IsRunning && IsRunningMqtt)
- {
- if (msg.Count > 0 && msg.TryDequeue(out string s))
- {
- SendScreenDataModel read= JsonConvert.DeserializeObject<SendScreenDataModel>(s);
- if (read != null && read.Value!=null)
- {
- switch (read.Name)
- {
- case ScreenDeviceType.大炒:
- mqttDatasDic[read.Name] = JsonConvert.DeserializeObject<ScreenModelMaxWok>(read.Value.ToString());
- break;
- case ScreenDeviceType.小炒:
- mqttDatasDic[read.Name] = JsonConvert.DeserializeObject<ScreenModelMinWok>(read.Value.ToString());
- break;
- case ScreenDeviceType.分餐机:
- mqttDatasDic[read.Name] = JsonConvert.DeserializeObject<ScreenModelSplitMeals>(read.Value.ToString());
- break;
- case ScreenDeviceType.煮面机:
- mqttDatasDic[read.Name] = JsonConvert.DeserializeObject<ScreenModelMorkS>(read.Value.ToString());
- break;
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"{Name}:线程服务异常,原因:{ex.Message}");
- }
- Thread.Sleep(100);
- }), $"{Name},线程服务");
- }
- #endregion
-
- #region 启动、停止、健康检查
- /// <summary>
- /// 开始服务
- /// </summary>
- public void Start()
- {
- try
- {
- ReadSass();
- //连接MQTT、Redis
- Connection();
- IsRunning = true;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"{Name}:MQTT或者Redis启动时,连接失败,原因:{ex.Message}");
- }
- }
- /// <summary>
- /// 停止服务
- /// </summary>
- public void Stop()
- {
- try
- {
- //0.设置运行标志
- IsRunning = false;
- mqttHelper.CloseConnect();
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"{Name}:MQTT或者Redis停止时异常,原因:{ex.Message}");
- }
- }
- /// <summary>
- /// 健康检查
- /// </summary>
- /// <param name="msg"></param>
- /// <returns></returns>
- public bool Check(out string msg)
- {
- string msgage = string.Empty;
- bool IsTrue = false;
- if (IsRunningMqtt && IsRunning)
- IsTrue = true;
- else
- {
- if (!IsRunningMqtt)
- msgage += "Redis断开连接.";
- IsTrue = false;
- msgage = $"{Name}:健康检查失败,原因:{msgage}";
- }
- msg = msgage;
- return IsTrue;
- }
- #endregion
-
- #region 调用事件
- /// <summary>
- /// 调用Sass接口拿到广告
- /// </summary>
- public void ReadSass()
- {
- try
- {
- string SaasRoute = System.Configuration.ConfigurationManager.AppSettings["SaasRoute"].ToString();
- string res = HttpRequestHelper.HttpGetRequest(SaasRoute);
- if (!string.IsNullOrEmpty(res))
- {
- HttpReturn httpReturn= JsonConvert.DeserializeObject<HttpReturn>(res);
- var Init =httpReturn?.data?.Devices?.Join(httpReturn?.data?.Stalls, t => t.GateId, x => x.Id, (t, x) => new AdDTO
- {
- Ad = x.Remaek,
- Device = t.Name,
- Stalls = x.Name,
- Address = t.Address,
- }).ToList();
- SaasRouteReturn= Init;
- }
- }
- catch (Exception ex)
- {
-
- }
- }
-
- /// <summary>
- /// 显示地址
- /// </summary>
- /// <param name="input"></param>
- /// <param name="view2"></param>
- public async void InitView2(string input, WebView2 view2, System.Windows.Controls.GroupBox group)
- {
- var height = 540;
- var width = 940;
- input = input.Replace("style=\"width: 100%;\"", "");
- StringBuilder sb = new StringBuilder();
- 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>");
- var html = sb.ToString();
-
- string name = $"{Application.StartupPath}\\Html\\{Name}.html";
- if (!System.IO.Directory.Exists($"{Application.StartupPath}\\Html"))
- {
- System.IO.Directory.CreateDirectory($"{Application.StartupPath}\\Html");//不存在就创建文件夹 }
- }
- if (!System.IO.File.Exists(name))
- {
- //----------生成htm文件------------------――
- try
- {
- using (StreamWriter sw = new StreamWriter(name, false, System.Text.Encoding.GetEncoding("UTF-8"))) //保存地址
- {
- sw.WriteLine(html);
- sw.Flush();
- sw.Close();
- }
- }
- catch
- {
- }
- }
- view2.Source = new Uri(name);
- }
- /// <summary>
- /// 读取配置
- /// </summary>
- public void ReadPZ()
- {
- System.Configuration.ConfigurationManager.AppSettings.AllKeys?.ToList().ForEach(key => {
- PZdic[key]= System.Configuration.ConfigurationManager.AppSettings[key].ToString();
- });
- }
- /// <summary>
- /// 初始化连接Redis MQTT
- /// </summary>
- public void Connection()
- {
- try
- {
- //2.Mqtt连接
- if (!string.IsNullOrEmpty(this.MQTTConnection) && this.MQTTConnection.Contains(','))
- {
- string[] mqttcom = this.MQTTConnection.Split(',');
- if (mqttcom != null && mqttcom.Count() == 4)
- {
- mqttHelper.Connect(mqttcom[2], mqttcom[3], mqttcom[0], int.Parse(mqttcom[1]), $"{Name}:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
- }
- }
- mqttHelper.Disconnect = new Action(() =>
- {
- IsRunningMqtt = false;
- MessageLog.GetInstance.ShowEx($"{Name}:MQTT异常断开....");
- });
- mqttHelper.ConnectOk = new Action(() =>
- {
- IsRunningMqtt = true;//连接成功
- if (Name.Contains(","))
- {
- Name.Split(',')?.ToList().ForEach(x =>
- {
- if (x == "大炒")
- {
- mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.大炒));
- }else if (x == "小炒")
- {
- mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.小炒));
- }
- else if (x == "分餐机")
- {
- mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.分餐机));
- }
- else if (x == "煮面机")
- {
- mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.煮面机));
- }
-
- });
- }
- else
- {
- if (Name == "大炒")
- {
- mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.大炒));
- }
- else if (Name == "小炒")
- {
- mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.小炒));
- }
- else if (Name == "分餐机")
- {
- mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.分餐机));
- }
- else if (Name == "煮面机")
- {
- mqttHelper.Subscrib(ScreenTOPIC.GetInstance.GetTopic(ScreenDeviceType.煮面机));
- }
- }
- });
- mqttHelper.MessageRecive = new Action<string>((s) => { msg.Enqueue(s); });
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"{Name}:初始化连接 MQTT,原因:{ex.Message}");
- }
-
- }
- #endregion
- }
- }
|