From b626c22293031d3f7982bfb42a23ad24b732a912 Mon Sep 17 00:00:00 2001 From: fyf <11621@LAPTOP-04QQU0AO> Date: Fri, 16 Sep 2022 11:36:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE=E7=BB=91?= =?UTF-8?q?=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmart.Model/PublishModel.cs | 1 + BPASmartClient.Compiler/FRedisClient.cs | 14 +++- BPASmartClient.DATABUS/Class_DataBus.cs | 12 ++- .../CustomerControls/Silos.xaml.cs | 4 +- .../CustomerControls/SwitchButton.cs | 84 ++++++++++++++++++- .../CustomerControls/TheButton.xaml | 1 - .../CustomerControls/TheButton.xaml.cs | 5 +- .../CustomerControls/TheRedis.xaml.cs | 46 ++++++---- .../CustomerControls/TheTextBlock.cs | 4 +- .../CustomerControls/TheTextBox.cs | 12 +-- SCADA.Test/MainWindow.xaml.cs | 5 +- 11 files changed, 146 insertions(+), 42 deletions(-) diff --git a/BPASmart.Model/PublishModel.cs b/BPASmart.Model/PublishModel.cs index 9e008472..264ed0d8 100644 --- a/BPASmart.Model/PublishModel.cs +++ b/BPASmart.Model/PublishModel.cs @@ -10,6 +10,7 @@ namespace BPASmart.Model { public string DeviceName { get; set; } public string RealAddress { get; set; } + public string VarName { get; set; } public string Value { get; set; } public EDataType DataType { get; set; } public int Sleep { get; set; } diff --git a/BPASmartClient.Compiler/FRedisClient.cs b/BPASmartClient.Compiler/FRedisClient.cs index 8f8f583d..63d28bb4 100644 --- a/BPASmartClient.Compiler/FRedisClient.cs +++ b/BPASmartClient.Compiler/FRedisClient.cs @@ -51,6 +51,7 @@ namespace BPASmartClient.Compiler _database = _connection.GetDatabase(0);//默认使用db0 subscibe = _connection.GetSubscriber(); } + int dbi = 0; public void Connect(string connection) { _connection = ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(connection)); @@ -58,7 +59,7 @@ namespace BPASmartClient.Compiler { string[] str=connection.Split(','); string stro = str.ToList().Find(s => s.Contains("defaultDatabase=")); - int dbi = 0; + try { dbi=int.Parse(stro.Replace("defaultDatabase=","")); @@ -72,7 +73,7 @@ namespace BPASmartClient.Compiler } else { - _database = _connection.GetDatabase();//默认使用db0 + _database = _connection.GetDatabase(dbi);//默认使用db0 } subscibe = _connection.GetSubscriber(); } @@ -89,7 +90,14 @@ namespace BPASmartClient.Compiler //获取指定服务器 var server = _connection.GetServer(endPoint); //在指定服务器上使用 keys 或者 scan 命令来遍历key - foreach (var key in server.Keys(0,"设备列表:*")) + //foreach (var key in server.Keys(0,"设备列表:*")) + //{ + // //获取key对于的值 + // var val = _database.StringGet(key); + // Console.WriteLine($"key: {key}, value: {val}"); + // keys[key] = val; + //} + foreach (var key in server.Keys(dbi,"*")) { //获取key对于的值 var val = _database.StringGet(key); diff --git a/BPASmartClient.DATABUS/Class_DataBus.cs b/BPASmartClient.DATABUS/Class_DataBus.cs index 27b822fa..cd3aee39 100644 --- a/BPASmartClient.DATABUS/Class_DataBus.cs +++ b/BPASmartClient.DATABUS/Class_DataBus.cs @@ -28,11 +28,21 @@ namespace BPASmartClient.DATABUS /// /// 设备数据 /// - public ConcurrentDictionary> Dic_DeviceData = new ConcurrentDictionary>(); + public ConcurrentDictionary> Dic_DeviceData = new ConcurrentDictionary>(); /// /// API数据 /// public ConcurrentDictionary Dic_APIData = new ConcurrentDictionary(); #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 e82c3a9c..f8d7ff61 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/Silos.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/Silos.xaml.cs @@ -570,10 +570,10 @@ namespace BPASmartClient.SCADAControl.CustomerControls { if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) { - Dictionary b= Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; + Dictionary b= Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; if (b!=null && b.ContainsKey(str[1])) { - object _value = b[str[1]]; + object _value = b[str[1]].VarVaule; this.GetType().GetProperty(item.Key).SetValue(this,_value); } } diff --git a/BPASmartClient.SCADAControl/CustomerControls/SwitchButton.cs b/BPASmartClient.SCADAControl/CustomerControls/SwitchButton.cs index 6586c29d..254f0dbe 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/SwitchButton.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/SwitchButton.cs @@ -1,8 +1,10 @@ using BPASmartClient.Compiler; +using BPASmartClient.DATABUS; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; @@ -16,6 +18,7 @@ using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using System.Windows.Threading; namespace BPASmartClient.SCADAControl.CustomerControls { @@ -40,7 +43,14 @@ namespace BPASmartClient.SCADAControl.CustomerControls { DefaultStyleKeyProperty.OverrideMetadata(typeof(SwitchButton), new FrameworkPropertyMetadata(typeof(SwitchButton))); } - + [Category("事件")] + public string Value + { + get { return (string)GetValue(ValueProperty); } + set { SetValue(ValueProperty,value); } + } + public static readonly DependencyProperty ValueProperty = + DependencyProperty.Register("Value",typeof(string),typeof(SwitchButton),new PropertyMetadata(string.Empty)); /// /// 不勾选时执行代码 /// @@ -64,7 +74,14 @@ namespace BPASmartClient.SCADAControl.CustomerControls } public static readonly DependencyProperty CheckedExecProperty = DependencyProperty.Register("CheckedExec", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty)); - + [Category("数据绑定-数据来源")] + public int TimeCount + { + get { return (int)GetValue(TimeCountProperty); } + set { SetValue(TimeCountProperty,value); } + } + public static readonly DependencyProperty TimeCountProperty = + DependencyProperty.Register("TimeCount",typeof(int),typeof(SwitchButton),new PropertyMetadata(5)); protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); @@ -128,12 +145,73 @@ namespace BPASmartClient.SCADAControl.CustomerControls } } } - + /// + /// 属性刷新器 + /// + DispatcherTimer timer = new DispatcherTimer(); + /// + /// 属性绑定变量集合 + /// + Dictionary propertyBing = new Dictionary(); public void Register() { + PropertyInfo[] propertyInfos = this.GetType().GetProperties(); + foreach (PropertyInfo propertyInfo in propertyInfos) + { + var propName = propertyInfo?.GetValue(this,null); + if (propName is string && propName != null && propName.ToString().Contains("Binding ") && propName.ToString().Contains(".")) + { + propertyBing[propertyInfo.Name] = propName.ToString(); + } + } + timer.Interval = TimeSpan.FromMilliseconds(TimeCount); + timer.Tick += Timer_Tick; ; + timer.Start(); Checked += TheCheckBox_Checked; Unchecked += TheCheckBox_Unchecked; } + /// + /// 属性刷新事件 + /// + /// + /// + private void Timer_Tick(object? sender,EventArgs e) + { + try + { + foreach (var item in propertyBing) + { + //{Binding 测试设备.VAR_A_2} + string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split("."); + if (str.Length > 1) + { + if (Class_DataBus.GetInstance().Dic_DeviceData.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; + } + this.GetType().GetProperty("IsChecked").SetValue(this,_checked); + } + } + } + } + } + catch (Exception ex) + { + + } + } + private void TheCheckBox_Unchecked(object sender, RoutedEventArgs e) { Config.GetInstance().RunJsScipt(UnCheckedExec); diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheButton.xaml b/BPASmartClient.SCADAControl/CustomerControls/TheButton.xaml index e5aa9711..ee013156 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheButton.xaml +++ b/BPASmartClient.SCADAControl/CustomerControls/TheButton.xaml @@ -3,7 +3,6 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" - xmlns:local="clr-namespace:BPASmartClient.SCADAControl" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheButton.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheButton.xaml.cs index 66b56948..cb754e44 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheButton.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheButton.xaml.cs @@ -48,7 +48,6 @@ namespace BPASmartClient.SCADAControl.CustomerControls isExecuteState = value; if (IsExecuteState) { - Style = null; Register(); } } @@ -124,10 +123,10 @@ namespace BPASmartClient.SCADAControl.CustomerControls { if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; + Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; if (b != null && b.ContainsKey(str[1])) { - object _value = b[str[1]]; + object _value = b[str[1]].VarVaule; this.GetType().GetProperty(item.Key).SetValue(this,_value); } } diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheRedis.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheRedis.xaml.cs index b6f76ea3..d7e9111d 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheRedis.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheRedis.xaml.cs @@ -47,7 +47,6 @@ namespace BPASmartClient.SCADAControl.CustomerControls { foreach (Image tb in FindVisualChildren(this)) { - // do something with tb here if (tb.Tag != null) { if (tb.Tag.ToString() == "正常") @@ -69,7 +68,6 @@ namespace BPASmartClient.SCADAControl.CustomerControls foreach (TextBlock tb in FindVisualChildren(this)) { - // do something with tb here if (tb.Tag != null) { if (tb.Tag.ToString() == "显示文字") @@ -159,17 +157,38 @@ namespace BPASmartClient.SCADAControl.CustomerControls } if (Direction == 1) //定时读取数据 { - if (!string.IsNullOrEmpty(DeviceName)) + try { - RedisValue obj = fRedisClient.RedisGet(DeviceName); - FDataSouce = obj.ToString(); - List str = JsonConvert.DeserializeObject>(FDataSouce); - Dictionary keys=new Dictionary(); - str?.ForEach(par => + if (!string.IsNullOrEmpty(DeviceName)) + { + RedisValue obj = fRedisClient.RedisGet(DeviceName); + List str = JsonConvert.DeserializeObject>(obj.ToString()); + Dictionary keys = new Dictionary(); + str?.ForEach(par => + { + keys[par.VarName] = par; + }); + Class_DataBus.GetInstance().Dic_DeviceData[DeviceName] = keys; + if (PropertyChange != null) PropertyChange(this,null); + } + else { - keys[par.VarName] = par.VarVaule; - }); - Class_DataBus.GetInstance().Dic_DeviceData[DeviceName] = keys; + fRedisClient.GetKeys()?.ToList().ForEach(par => { + + List str = JsonConvert.DeserializeObject>(par.Value); + Dictionary keys = new Dictionary(); + str?.ForEach(par => + { + keys[par.VarName] = par; + }); + Class_DataBus.GetInstance().Dic_DeviceData[par.Key] = keys; + }); + if (PropertyChange != null) PropertyChange(this,null); + } + } + catch (Exception ex) + { + } } } @@ -284,10 +303,7 @@ namespace BPASmartClient.SCADAControl.CustomerControls if (!string.IsNullOrEmpty(FDataSouce)) { GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code,new object[] { FDataSouce }); - if (PropertyChange != null) - { - PropertyChange(this,null); - } + } } catch (Exception ex) diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheTextBlock.cs b/BPASmartClient.SCADAControl/CustomerControls/TheTextBlock.cs index 47f7bfc2..18eed157 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheTextBlock.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheTextBlock.cs @@ -108,10 +108,10 @@ namespace BPASmartClient.SCADAControl.CustomerControls { if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; + Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; if (b != null && b.ContainsKey(str[1])) { - object _value = b[str[1]]; + object _value = b[str[1]].VarVaule; this.GetType().GetProperty(item.Key).SetValue(this,_value); } } diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheTextBox.cs b/BPASmartClient.SCADAControl/CustomerControls/TheTextBox.cs index 1740e9fa..7317a695 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheTextBox.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheTextBox.cs @@ -35,17 +35,9 @@ namespace BPASmartClient.SCADAControl.CustomerControls ResourceDictionary languageResDic = new ResourceDictionary(); languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute); this.Resources.MergedDictionaries.Add(languageResDic); - Text = "0.01"; - //VerticalContentAlignment = VerticalAlignment.Center; - //Style = Application.Current.Resources["DesignTheTextBox"] as Style;//FindResource("DesignTheTextBox") as Style; - //Focusable = false; Height = 30; Width = 80; FontSize = 16; - //HorizontalAlignment = HorizontalAlignment.Left; - //VerticalAlignment = VerticalAlignment.Center; - //Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#a2c2e8")); - //BorderThickness=new Thickness(1); } public string ControlType => "控件"; @@ -122,10 +114,10 @@ namespace BPASmartClient.SCADAControl.CustomerControls { if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0])) { - Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; + Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; if (b != null && b.ContainsKey(str[1])) { - object _value = b[str[1]]; + object _value = b[str[1]].VarVaule; this.GetType().GetProperty(item.Key).SetValue(this,_value); } } diff --git a/SCADA.Test/MainWindow.xaml.cs b/SCADA.Test/MainWindow.xaml.cs index 560c9ae0..015f4c93 100644 --- a/SCADA.Test/MainWindow.xaml.cs +++ b/SCADA.Test/MainWindow.xaml.cs @@ -40,13 +40,14 @@ namespace SCADA.Test /// 接收消息集合 /// List eventReceiveMessages = new List(); + FRedisClient fRedisClient = new FRedisClient(); public MainWindow() { InitializeComponent(); - RedisHelper.GetInstance.ConnectAsync(String.Empty); - + fRedisClient.Connect("124.222.238.75:16000,password=123456,defaultDatabase=1"); + Dictionary sdsd= fRedisClient.GetKeys(); string _code = " public string main(string message) \n { \n //请在此填写你的代码\n\n return message; \n }\n"; string GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(_code,new object[] { "ERERERERE" });