diff --git a/BPASmartClient.ScreenLib/BPASmartClient.ScreenLib.csproj b/BPASmartClient.ScreenLib/BPASmartClient.ScreenLib.csproj index 1a104f1b..c8f6e028 100644 --- a/BPASmartClient.ScreenLib/BPASmartClient.ScreenLib.csproj +++ b/BPASmartClient.ScreenLib/BPASmartClient.ScreenLib.csproj @@ -19,7 +19,7 @@ - + diff --git a/BPASmartClient.ScreenLib/Helper/Main.cs b/BPASmartClient.ScreenLib/Helper/Main.cs index 171082cf..033bfe83 100644 --- a/BPASmartClient.ScreenLib/Helper/Main.cs +++ b/BPASmartClient.ScreenLib/Helper/Main.cs @@ -1,5 +1,6 @@ using BPA.Communication; using BPA.Helper; +using BPA.Message; using Newtonsoft.Json; using System; using System.Collections.Concurrent; @@ -18,9 +19,9 @@ namespace BPASmartClient.ScreenLib { #region 接口继承变量 /// - /// Redis连接信息 + /// MQTT连接信息 /// - public string RedisConnection { get; set; } + public string MQTTConnection { get; set; } /// /// 业务名称 /// @@ -46,9 +47,13 @@ namespace BPASmartClient.ScreenLib /// public bool IsRunning { get; set; } /// - /// Redis是否运行 + /// Mqtt是否运行 /// - public bool IsRunningReids => RedisHelper.GetInstance.IsConnected(); + public bool IsRunningMqtt = false; + /// + /// Mqtt + /// + public MqttHelper mqttHelper = new MqttHelper(); /// /// 主函数 /// @@ -57,23 +62,23 @@ namespace BPASmartClient.ScreenLib public Main() { ReadPZ(); - if (PZdic.ContainsKey("RedisConnection")) - RedisConnection = PZdic["RedisConnection"]; + if (PZdic.ContainsKey("MQTTConnection")) + MQTTConnection = PZdic["MQTTConnection"]; if (PZdic.ContainsKey("DeviceMC")) Name = PZdic["DeviceMC"]; } /// /// 设备数据 /// - public Dictionary> redisDatasDic = new Dictionary>(); - /// - /// 告警数据 - /// - public Dictionary> redisAicDatasDic = new Dictionary>(); + public Dictionary> mqttDatasDic = new Dictionary>(); /// /// 配置数据 /// public Dictionary PZdic=new Dictionary(); + /// + /// Mqtt消息队列 + /// + public ConcurrentQueue msg = new ConcurrentQueue(); #endregion #region 线程处理函数 @@ -86,44 +91,17 @@ namespace BPASmartClient.ScreenLib { try { - if (IsRunning && IsRunningReids) + if (IsRunning && IsRunningMqtt) { - //1.读取Redis变量 - if (!string.IsNullOrEmpty(Name)) + if (msg.Count > 0 && msg.TryDequeue(out string s)) { - redisDatasDic = new Dictionary>(); - redisAicDatasDic = new Dictionary>(); - if (Name.Contains(",")) + SendScreenDataModel read= JsonConvert.DeserializeObject(s); + if (read != null) { - Name.Split(',')?.ToList().ForEach(x => - { - Dictionary valuePairs = new Dictionary(); - RedisHelper.GetInstance.Read>($"{x}[Device]").Content?.ToList().ForEach(k => { - valuePairs[k.VarName] = k; - }); - redisDatasDic[x]= valuePairs;//设备数据 - - Dictionary valuePairsAic = new Dictionary(); - RedisHelper.GetInstance.Read>($"{x}[Alarm]").Content?.ToList().ForEach(k => { - valuePairsAic[k.VarName] = k; - }); - redisAicDatasDic[x] = valuePairsAic;//告警数据 + read?.mqttAicDatasDic?.ToList().ForEach(data => { + mqttDatasDic[data.Key]= data.Value; }); } - else - { - Dictionary valuePairs = new Dictionary(); - RedisHelper.GetInstance.Read>($"{Name}[Device]").Content?.ToList().ForEach(k => { - valuePairs[k.VarName] = k; - }); - redisDatasDic[Name] = valuePairs;//设备数据 - - Dictionary valuePairsAic = new Dictionary(); - RedisHelper.GetInstance.Read>($"{Name}[Alarm]").Content?.ToList().ForEach(k => { - valuePairsAic[k.VarName] = k; - }); - redisAicDatasDic[Name] = valuePairsAic;//告警数据 - } } } } @@ -162,6 +140,7 @@ namespace BPASmartClient.ScreenLib { //0.设置运行标志 IsRunning = false; + mqttHelper.CloseConnect(); } catch (Exception ex) { @@ -177,11 +156,11 @@ namespace BPASmartClient.ScreenLib { string msgage = string.Empty; bool IsTrue = false; - if (IsRunningReids && IsRunning) + if (IsRunningMqtt && IsRunning) IsTrue = true; else { - if (!IsRunningReids) + if (!IsRunningMqtt) msgage += "Redis断开连接."; IsTrue = false; msgage = $"{Name}:健康检查失败,原因:{msgage}"; @@ -208,17 +187,36 @@ namespace BPASmartClient.ScreenLib { try { - //1.连接Redis,如果已经连接过了 那么自动不会去连接 - if (!string.IsNullOrEmpty(this.RedisConnection) && this.RedisConnection.Contains(',')) + //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(() => { - string[] rediscom = this.RedisConnection.Split(','); - if (rediscom != null && rediscom.Count() == 4) - RedisHelper.GetInstance.Connect(new ConfigurationOptions() + IsRunningMqtt = true;//连接成功 + if (Name.Contains(",")) + { + Name.Split(',')?.ToList().ForEach(x => { - ServerAddress = $"{rediscom[0]}:{int.Parse(rediscom[1])}", - Password = rediscom[2] + mqttHelper.Subscrib(x); }); - } + } + else + { + mqttHelper.Subscrib(Name); + } + }); + mqttHelper.MessageRecive = new Action((s) => { msg.Enqueue(s); }); } catch (Exception ex) { @@ -228,31 +226,4 @@ namespace BPASmartClient.ScreenLib } #endregion } - - - /// - /// Redis 数据存储格式 - /// - public class RedisDataModel - { - public string VarName { get; set; } - public string VarVaule { get; set; } - public EDataType DataType { get; set; } - } - - /// - /// 数据类型枚举 - /// - public enum EDataType - { - Bool = 1, - Byte = 2, - Int = 3, - Word = 4, - Dint = 5, - Dword = 6, - Float = 7, - Double = 8, - String = 9, - } } diff --git a/BPASmartClient.ScreenLib/分餐机/ScreenSplitMealsControl1.xaml b/BPASmartClient.ScreenLib/分餐机/ScreenSplitMealsControl1.xaml index 57b7f60c..de972084 100644 --- a/BPASmartClient.ScreenLib/分餐机/ScreenSplitMealsControl1.xaml +++ b/BPASmartClient.ScreenLib/分餐机/ScreenSplitMealsControl1.xaml @@ -47,23 +47,12 @@ 设备是否运行 - - - - - - - 设备当前状态 + + + 今日刷卡数 - + @@ -71,25 +60,23 @@ - - 今日刷卡数 + + 前一位刷卡人 - - 异常刷卡数 + + 当前刷卡人 - - + -