using BPASmart.Model; using BPASmartClient.Compiler; using BPASmartClient.DATABUS; using BPASmartClient.MessageName.EnumHelp; using BPASmartClient.SCADAControl; using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace BPASmartClient.SCADAControl.CustomerControls { /// /// TheRedis.xaml 的交互逻辑 /// public partial class TheRedis :UserControl, IExecutable, IDisposable { Image imageZC = null; Image imageYC = null; Image imageGZ = null; TextBlock textBlock = null; FRedisClient fRedisClient = new FRedisClient(); public TheRedis() { InitializeComponent(); Width = 40; Height = 40; this.SizeChanged += TheRedis_SizeChanged; } private void TheRedis_SizeChanged(object sender,SizeChangedEventArgs e) { if (textBlock == null) { foreach (Image tb in FindVisualChildren(this)) { if (tb.Tag != null) { if (tb.Tag.ToString() == "正常") { imageZC = tb; } if (tb.Tag.ToString() == "运行") { imageYC = tb; } if (tb.Tag.ToString() == "故障") { imageGZ = tb; } } } foreach (TextBlock tb in FindVisualChildren(this)) { if (tb.Tag != null) { if (tb.Tag.ToString() == "显示文字") { textBlock = tb; } } } } } public static IEnumerable FindVisualChildren(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj,i); if (child != null && child is T) { yield return (T)child; } foreach (T childOfChild in FindVisualChildren(child)) { yield return childOfChild; } } } } public string ControlType => "控件"; private bool isExecuteState; public bool IsExecuteState { get { return isExecuteState; } set { isExecuteState = value; if (IsExecuteState) { Register(); Style = null; } } } DispatcherTimer timer = new DispatcherTimer(); public void Register() { if (Direction == 0) { try { if (!string.IsNullOrEmpty(DataSouceInformation)) { fRedisClient.Connect(DataSouceInformation); } } catch (Exception ex) { Direction = 2; } } timer.Interval = TimeSpan.FromSeconds(TimeCount); timer.Tick += Timer_Tick; timer.Start(); } Task isSuccess; private async void Timer_Tick(object sender,EventArgs e) { Config.GetInstance().RunJsScipt(TikcExecute); if (fRedisClient._connection != null && fRedisClient._connection.IsConnected) { Direction = 1; } else { Direction = 0; try { if (!string.IsNullOrEmpty(DataSouceInformation)) fRedisClient.Connect(DataSouceInformation); } catch (Exception ex) { Direction = 2; } } if (Direction == 1) //定时读取数据 { try { 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 { 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) { } } } public void Start() => timer.Start(); public void Stop() => timer.Stop(); public void Dispose() { timer.Stop(); } #region 属性 [Category("值设定")] public int Direction { get { return (int)GetValue(DirectionProperty); } set { SetValue(DirectionProperty,value); } } public static readonly DependencyProperty DirectionProperty = DependencyProperty.Register("Direction",typeof(int),typeof(TheRedis), new PropertyMetadata(0,new PropertyChangedCallback(OnPropertyChanged))); private static void OnPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) { (d as TheRedis)?.Refresh(); } public void Refresh() { if (textBlock != null) { if (Direction == 1) { imageZC.Visibility = Visibility.Collapsed; imageYC.Visibility = Visibility.Visible; imageGZ.Visibility = Visibility.Collapsed; textBlock.Visibility = Visibility.Visible; textBlock.Text = "运行中"; textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF2077EC")); } else if (Direction == 2) { imageZC.Visibility = Visibility.Collapsed; imageYC.Visibility = Visibility.Collapsed; imageGZ.Visibility = Visibility.Visible; textBlock.Visibility = Visibility.Visible; textBlock.Text = "故障"; textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFFF0202")); } else { imageZC.Visibility = Visibility.Visible; imageYC.Visibility = Visibility.Collapsed; imageGZ.Visibility = Visibility.Collapsed; textBlock.Visibility = Visibility.Visible; textBlock.Text = "未运行"; textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF02F9FF")); } } } /// /// 执行内容 /// [Category("事件")] public string TikcExecute { get { return (string)GetValue(TikcExecuteProperty); } set { SetValue(TikcExecuteProperty,value); } } public static readonly DependencyProperty TikcExecuteProperty = DependencyProperty.Register("TikcExecute",typeof(string),typeof(TheRedis),new PropertyMetadata(string.Empty)); #endregion #region 数据绑定模块 public event EventHandler PropertyChange; //声明一个事件 [Category("数据绑定-数据来源")] public int TimeCount { get { return (int)GetValue(TimeCountProperty); } set { SetValue(TimeCountProperty,value); } } public static readonly DependencyProperty TimeCountProperty = DependencyProperty.Register("TimeCount",typeof(int),typeof(TheRedis),new PropertyMetadata(5)); [Category("数据绑定-数据来源")] public string DataSouceInformation { get { return (string)GetValue(DataSouceInformationProperty); } set { SetValue(DataSouceInformationProperty,value); } } public static readonly DependencyProperty DataSouceInformationProperty = DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheRedis),new PropertyMetadata("124.222.238.75:16000,password=123456,defaultDatabase=1")); [Category("数据绑定-数据来源")] public string DeviceName { get { return (string)GetValue(DeviceNameProperty); } set { SetValue(DeviceNameProperty,value); } } public static readonly DependencyProperty DeviceNameProperty = DependencyProperty.Register("DeviceName",typeof(string),typeof(TheRedis),new PropertyMetadata(string.Empty)); [Category("数据绑定")] public string FDataSouce { get { return (string)GetValue(FDataSouceProperty); } set { SetValue(FDataSouceProperty,value); } } public static readonly DependencyProperty FDataSouceProperty = DependencyProperty.Register("FDataSouce",typeof(string),typeof(TheRedis),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onFDataSouceChanged))); private static void onFDataSouceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheRedis)?.DataSouceRefresh(); public void DataSouceRefresh() { try { if (!string.IsNullOrEmpty(FDataSouce)) { GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code,new object[] { FDataSouce }); } } catch (Exception ex) { } } public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n"; [Category("数据绑定")] public string Code { get { return (string)GetValue(CodeProperty); } set { SetValue(CodeProperty,value); } } public static readonly DependencyProperty CodeProperty = DependencyProperty.Register("Code",typeof(string),typeof(TheRedis),new PropertyMetadata(_code)); [Category("数据绑定")] public string GenerateData { get { return (string)GetValue(GenerateDataProperty); } set { SetValue(GenerateDataProperty,value); } } public static readonly DependencyProperty GenerateDataProperty = DependencyProperty.Register("GenerateData",typeof(string),typeof(TheRedis),new PropertyMetadata(string.Empty)); #endregion } }