From ca90514165f19be219b22453458471a6ee22b8d9 Mon Sep 17 00:00:00 2001 From: fyf Date: Sat, 24 Sep 2022 15:40:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0redis=E6=8E=A7=E4=BB=B6?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmartClient.DATABUS/Class_DataBus.cs | 21 ++--- .../CustomerControls/Silos.xaml.cs | 10 +-- .../CustomerControls/SwitchButton.cs | 28 ++----- .../CustomerControls/TheButton.xaml.cs | 10 +-- .../CustomerControls/TheDataGrid.xaml.cs | 8 +- .../CustomerControls/TheListBox.xaml.cs | 8 +- .../CustomerControls/TheRedis.xaml.cs | 77 ++++++++++++++++--- .../CustomerControls/TheTextBlock.cs | 10 +-- .../CustomerControls/TheTextBox.cs | 21 ++--- .../CustomerControls/TheToggleButton.xaml.cs | 46 ++--------- BeDesignerSCADA/ViewModel/MainViewModel.cs | 14 ++-- 11 files changed, 119 insertions(+), 134 deletions(-) diff --git a/BPASmartClient.DATABUS/Class_DataBus.cs b/BPASmartClient.DATABUS/Class_DataBus.cs index 6261db7d..45eb89e0 100644 --- a/BPASmartClient.DATABUS/Class_DataBus.cs +++ b/BPASmartClient.DATABUS/Class_DataBus.cs @@ -26,9 +26,14 @@ namespace BPASmartClient.DATABUS #region 实时数据->大数据量 /// - /// 设备数据 + /// 缓存Redis数据 /// - public ConcurrentDictionary> Dic_DeviceData = new ConcurrentDictionary>(); + public ConcurrentDictionary> Dic_RedisData = new ConcurrentDictionary>(); + /// + /// 缓存Redis数据相关类型 + /// + public ConcurrentDictionary> Dic_RedisDataType = new ConcurrentDictionary>(); + /// /// API数据 /// @@ -36,19 +41,7 @@ namespace BPASmartClient.DATABUS #endregion #region 配置数据 - - - //Json.Read(); #endregion } - - - public class DeviceDataModel - { - public string VarName { get; set; } - public string VarVaule { get; set; } - public string DataType { get; set; } - } - } \ No newline at end of file diff --git a/BPASmartClient.SCADAControl/CustomerControls/Silos.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/Silos.xaml.cs index 8bc7d2d8..7f8914fb 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/Silos.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/Silos.xaml.cs @@ -505,14 +505,10 @@ namespace BPASmartClient.SCADAControl.CustomerControls string[] str= item.Value.Replace("{Binding ","").Replace("}","").Split("."); if (str.Length > 1) { - if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) + if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0])) { - Dictionary b= Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; - if (b!=null && b.ContainsKey(str[1])) - { - object _value = b[str[1]].VarVaule; - this.GetType().GetProperty(item.Key).SetValue(this,_value); - } + Dictionary b= Class_DataBus.GetInstance().Dic_RedisData[str[0]]; + if (b!=null && b.ContainsKey(str[1])) this.GetType().GetProperty(item.Key).SetValue(this, b[str[1]]); } } } diff --git a/BPASmartClient.SCADAControl/CustomerControls/SwitchButton.cs b/BPASmartClient.SCADAControl/CustomerControls/SwitchButton.cs index 73a3a4af..694a3384 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/SwitchButton.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/SwitchButton.cs @@ -201,24 +201,12 @@ namespace BPASmartClient.SCADAControl.CustomerControls string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split("."); if (str.Length > 1) { - if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) + if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0])) { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; + Dictionary b = Class_DataBus.GetInstance().Dic_RedisData[str[0]]; if (b != null && b.ContainsKey(str[1])) { - object _value = b[str[1]].VarVaule; - bool _checked = false; - try - { - _checked = bool.Parse(_value.ToString()); - } - catch (Exception ex) - { - _checked = false; - } - EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType),b[str[1]].DataType); - SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0],VarName = str[1],Value = _checked.ToString(),DataType = eDataType }); - this.GetType().GetProperty("IsChecked").SetValue(this,_checked); + this.GetType().GetProperty("IsChecked").SetValue(this, b[str[1]]); } } } @@ -239,13 +227,13 @@ namespace BPASmartClient.SCADAControl.CustomerControls string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split("."); if (str.Length > 1) { - if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) + if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0]))//包含设备名称 { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; - if (b != null && b.ContainsKey(str[1])) + Dictionary blx = Class_DataBus.GetInstance().Dic_RedisDataType[str[0]]; + + if (blx != null && blx.ContainsKey(str[1])) { - object _value = b[str[1]].VarVaule; - EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType),b[str[1]].DataType); + EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType), blx[str[1]]); SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0],VarName = str[1],Value = this.IsChecked.ToString(),DataType = eDataType }); } } diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheButton.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheButton.xaml.cs index cb754e44..ee8c2d10 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheButton.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheButton.xaml.cs @@ -121,14 +121,10 @@ namespace BPASmartClient.SCADAControl.CustomerControls string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split("."); if (str.Length > 1) { - if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) + if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0])) { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; - if (b != null && b.ContainsKey(str[1])) - { - object _value = b[str[1]].VarVaule; - this.GetType().GetProperty(item.Key).SetValue(this,_value); - } + Dictionary b = Class_DataBus.GetInstance().Dic_RedisData[str[0]]; + if (b != null && b.ContainsKey(str[1])) this.GetType().GetProperty(item.Key).SetValue(this, b[str[1]]); } } } diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs index c2f27879..085a040f 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs @@ -128,13 +128,11 @@ namespace BPASmartClient.SCADAControl.CustomerControls string[] str = DataSouceInformation.Replace("{Binding ", "").Replace("}", "").Split("."); if (str.Length > 1) { - if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) + if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0])) { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; + Dictionary b = Class_DataBus.GetInstance().Dic_RedisData[str[0]]; if (b != null && b.ContainsKey(str[1])) - { - FDataSouce = b[str[1]].VarVaule; - } + FDataSouce = b[str[1]].ToString(); } } } diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheListBox.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheListBox.xaml.cs index 910091db..c5e0174b 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheListBox.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheListBox.xaml.cs @@ -256,13 +256,11 @@ namespace BPASmartClient.SCADAControl.CustomerControls string[] str = DataSouceInformation.Replace("{Binding ", "").Replace("}", "").Split("."); if (str.Length > 1) { - if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) + if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0])) { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; + Dictionary b = Class_DataBus.GetInstance().Dic_RedisData[str[0]]; if (b != null && b.ContainsKey(str[1])) - { - FDataSouce = b[str[1]].VarVaule; - } + FDataSouce = b[str[1]].ToString(); } } } diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheRedis.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheRedis.xaml.cs index 446eab74..c89ad9c3 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheRedis.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheRedis.xaml.cs @@ -163,26 +163,34 @@ namespace BPASmartClient.SCADAControl.CustomerControls if (!string.IsNullOrEmpty(DeviceName)) { RedisValue obj = fRedisClient.RedisGet(DeviceName); - List str = JsonConvert.DeserializeObject>(obj.ToString()); - Dictionary keys = new Dictionary(); - str?.ForEach(par => + List str = JsonConvert.DeserializeObject>(obj.ToString()); + Dictionary keyValues = new Dictionary(); + Dictionary keyValuesType = new Dictionary(); + str?.ForEach(jon => { - keys[par.VarName] = par; + keyValues[jon.VarName] = GetValuesType(jon.DataType, jon.VarVaule); + keyValuesType[jon.VarName] = jon.DataType.ToString(); }); - Class_DataBus.GetInstance().Dic_DeviceData[DeviceName] = keys; + Class_DataBus.GetInstance().Dic_RedisData[DeviceName] = keyValues; + Class_DataBus.GetInstance().Dic_RedisDataType[DeviceName] = keyValuesType; if (PropertyChange != null) PropertyChange(this,null); } else { fRedisClient.GetKeys()?.ToList().ForEach(par => { - - List str = JsonConvert.DeserializeObject>(par.Value); - Dictionary keys = new Dictionary(); - str?.ForEach(par => + //所有变量集合 + //PublishInfo + List str = JsonConvert.DeserializeObject>(par.Value); + Dictionary keyValues = new Dictionary(); + Dictionary keyValuesType = new Dictionary(); + str?.ForEach(jon => { - keys[par.VarName] = par; + keyValues[jon.VarName] = GetValuesType(jon.DataType, jon.VarVaule); + keyValuesType[jon.VarName] = jon.DataType.ToString(); }); - Class_DataBus.GetInstance().Dic_DeviceData[par.Key] = keys; + Class_DataBus.GetInstance().Dic_RedisData[par.Key] = keyValues; + Class_DataBus.GetInstance().Dic_RedisDataType[par.Key] = keyValuesType; + }); if (PropertyChange != null) PropertyChange(this,null); } @@ -194,6 +202,53 @@ namespace BPASmartClient.SCADAControl.CustomerControls } } + /// + /// 根据变量获取变量实际值 + /// + /// + /// + /// + public object GetValuesType(EDataType eData,string Value) + { + try + { + if (!string.IsNullOrEmpty(Value)) + { + switch (eData) + { + case EDataType.Bool: + return Convert.ToBoolean(Value); + break; + case EDataType.Byte: + return Convert.ToByte(Value); + break; + case EDataType.Int: + return Convert.ToInt16(Value); + break; + case EDataType.Word: + return Convert.ToUInt16(Value); + break; + case EDataType.Dint: + return Convert.ToInt32(Value); + break; + case EDataType.Dword: + return Convert.ToUInt32(Value); + break; + case EDataType.Float: + return Convert.ToSingle(Value); + break; + default: + break; + } + } + return null; + } + catch (Exception ex) + { + return null; + } + } + public void Start() => timer.Start(); public void Stop() => timer.Stop(); diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheTextBlock.cs b/BPASmartClient.SCADAControl/CustomerControls/TheTextBlock.cs index 6fb14d28..10176eec 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheTextBlock.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheTextBlock.cs @@ -121,14 +121,10 @@ namespace BPASmartClient.SCADAControl.CustomerControls string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split("."); if (str.Length > 1) { - if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) + if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0])) { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; - if (b != null && b.ContainsKey(str[1])) - { - object _value = b[str[1]].VarVaule; - this.GetType().GetProperty(item.Key).SetValue(this,_value); - } + Dictionary b = Class_DataBus.GetInstance().Dic_RedisData[str[0]]; + if (b != null && b.ContainsKey(str[1])) this.GetType().GetProperty(item.Key).SetValue(this, b[str[1]]); } } } diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheTextBox.cs b/BPASmartClient.SCADAControl/CustomerControls/TheTextBox.cs index 52b64ed5..5a0a51b1 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheTextBox.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheTextBox.cs @@ -71,14 +71,13 @@ namespace BPASmartClient.SCADAControl.CustomerControls string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split("."); if (str.Length > 1) { - if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) + if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0])) { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; - if (b != null && b.ContainsKey(str[1])) + Dictionary blx = Class_DataBus.GetInstance().Dic_RedisDataType[str[0]]; + if (blx != null && blx.ContainsKey(str[1])) { - object _value = b[str[1]].VarVaule; - EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType),b[str[1]].DataType); - SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0],VarName = str[1],Value = Text,DataType = eDataType }); + EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType), blx[str[1]]); + SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0], VarName = str[1], Value = Text, DataType = eDataType }); } } } @@ -180,14 +179,10 @@ namespace BPASmartClient.SCADAControl.CustomerControls string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split("."); if (str.Length > 1) { - if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) + if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0])) { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; - if (b != null && b.ContainsKey(str[1])) - { - object _value = b[str[1]].VarVaule; - this.GetType().GetProperty(item.Key).SetValue(this,_value); - } + Dictionary b = Class_DataBus.GetInstance().Dic_RedisData[str[0]]; + if (b != null && b.ContainsKey(str[1])) this.GetType().GetProperty(item.Key).SetValue(this, b[str[1]]); } } } diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheToggleButton.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheToggleButton.xaml.cs index 9daa1436..2753d721 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheToggleButton.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheToggleButton.xaml.cs @@ -134,8 +134,6 @@ namespace BPASmartClient.SCADAControl.CustomerControls timer.Start(); } Click += TheToggleButton_Click; - Checked += TheCheckBox_Checked; - Unchecked += TheCheckBox_Unchecked; } @@ -155,26 +153,10 @@ namespace BPASmartClient.SCADAControl.CustomerControls string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split("."); if (str.Length > 1) { - if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) + if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0])) { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; - if (b != null && b.ContainsKey(str[1])) - { - object _value = b[str[1]].VarVaule; - bool _checked = false; - try - { - _checked = bool.Parse(_value.ToString()); - } - catch (Exception ex) - { - _checked = false; - } - EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType),b[str[1]].DataType); - SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0],VarName = str[1],Value = _checked.ToString(),DataType = eDataType }); - this.GetType().GetProperty("IsChecked").SetValue(this,_checked); - - } + Dictionary b = Class_DataBus.GetInstance().Dic_RedisData[str[0]]; + if (b != null && b.ContainsKey(str[1])) this.GetType().GetProperty("IsChecked").SetValue(this, b[str[1]]); } } } @@ -194,14 +176,13 @@ namespace BPASmartClient.SCADAControl.CustomerControls string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split("."); if (str.Length > 1) { - if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) + if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0])) { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; - if (b != null && b.ContainsKey(str[1])) + Dictionary blx = Class_DataBus.GetInstance().Dic_RedisDataType[str[0]]; + if (blx != null && blx.ContainsKey(str[1])) { - object _value = b[str[1]].VarVaule; - EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType),b[str[1]].DataType); - SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0],VarName = str[1],Value = "False",DataType = eDataType }); + EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType), blx[str[1]]); + SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0], VarName = str[1], Value = IsChecked.ToString(), DataType = eDataType }); } } } @@ -210,16 +191,5 @@ namespace BPASmartClient.SCADAControl.CustomerControls else Config.GetInstance().RunJsScipt(UnCheckedExec); timer.Start(); } - - - private void TheCheckBox_Unchecked(object sender, RoutedEventArgs e) - { - - } - - private void TheCheckBox_Checked(object sender, RoutedEventArgs e) - { - - } } } diff --git a/BeDesignerSCADA/ViewModel/MainViewModel.cs b/BeDesignerSCADA/ViewModel/MainViewModel.cs index 3084891c..86a4a605 100644 --- a/BeDesignerSCADA/ViewModel/MainViewModel.cs +++ b/BeDesignerSCADA/ViewModel/MainViewModel.cs @@ -297,13 +297,13 @@ namespace BeDesignerSCADA.ViewModel /// private void Executable_PropertyChange(object? sender,EventArgs e) { - System.Windows.Controls.Control content = CanSelectedItem as System.Windows.Controls.Control; - System.Reflection.PropertyInfo info = content.GetType().GetProperty("GenerateData"); - var propName = info?.GetValue(content,null); - PropeObject.GetType().GetProperty("数据结果").SetValue(PropeObject,propName); - DevNameList = new System.Collections.ObjectModel.ObservableCollection(); - DevValueList = new System.Collections.ObjectModel.ObservableCollection(); - Class_DataBus.GetInstance().Dic_DeviceData.Keys?.ToList().ForEach(key => { DevNameList.Add(key); }); + //System.Windows.Controls.Control content = CanSelectedItem as System.Windows.Controls.Control; + //System.Reflection.PropertyInfo info = content.GetType().GetProperty("GenerateData"); + //var propName = info?.GetValue(content,null); + //PropeObject.GetType().GetProperty("数据结果").SetValue(PropeObject,propName); + //DevNameList = new System.Collections.ObjectModel.ObservableCollection(); + //DevValueList = new System.Collections.ObjectModel.ObservableCollection(); + //Class_DataBus.GetInstance().Dic_DeviceData.Keys?.ToList().ForEach(key => { DevNameList.Add(key); }); } /// /// 修改属性后