using BPASmartClient.Compiler; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Threading; namespace BPASmartClient.SCADAControl.CustomerControls { /// /// TheMQTT.xaml 的交互逻辑 /// public partial class TheMQTT :UserControl, IExecutable, IDisposable { Image imageZC = null; Image imageYC = null; Image imageGZ = null; TextBlock textBlock = null; public TheMQTT() { 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; } } } MQTT mQTT = new MQTT(); DispatcherTimer timer = new DispatcherTimer(); public void Register() { if (Direction == 0) { try { if (!string.IsNullOrEmpty(DataSouceInformation)) { if (DataSouceInformation.Contains(",")) { string[] lj=DataSouceInformation.Split(','); if (lj.Length >= 4) { int port = 8087; port= int.Parse(lj[3]); mQTT.MqttInitAsync(lj[0],lj[1],lj[2],port,$"分布式上位机:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } } } } catch (Exception ex) { Direction = 2; } } timer.Interval = TimeSpan.FromMilliseconds(TimeCount); timer.Tick += Timer_Tick; timer.Start(); } Task isSuccess; private async void Timer_Tick(object sender,EventArgs e) { Config.GetInstance().RunJsScipt(TikcExecute); if (mQTT.client != null && mQTT.client.IsConnected) { Direction = 1; } else { Direction = 0; try { if (!string.IsNullOrEmpty(DataSouceInformation)) mQTT.MqttInitAsync("admin","fengyoufu067101!@#","124.222.238.75",61613,$"分布式上位机:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } catch (Exception ex) { Direction = 2; } } if (Direction == 1) //定时读取数据 { try { //定时读取数据 } catch (Exception ex) { } } } public void Start() => timer.Start(); public void Stop() => timer.Stop(); public void Dispose() { timer.Stop(); Direction = 0; FDataSouce = ""; if (IsRun) IsRun = false; } #region 属性 [Category("事件")] public string SendText { get { return (string)GetValue(SendTextProperty); } set { SetValue(SendTextProperty, value); } } public static readonly DependencyProperty SendTextProperty = DependencyProperty.Register("SendText", typeof(string),typeof(TheMQTT),new PropertyMetadata(string.Empty,new PropertyChangedCallback(OnValuePropertyChanged))); private static void OnValuePropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) { (d as TheMQTT)?.RefreshMQTT(); } public void RefreshMQTT() { PublishInfo publishInfo = new PublishInfo(); if (!string.IsNullOrEmpty(SendText)) { publishInfo.PublishModels.Add(JsonConvert.DeserializeObject(SendText)); if (mQTT.client != null && mQTT.client.IsConnected) mQTT.MqttPublishAsync("DistributedHostComputer/Control",JsonConvert.SerializeObject(publishInfo)); } } [Category("值设定")] public int Direction { get { return (int)GetValue(DirectionProperty); } set { SetValue(DirectionProperty,value); } } public static readonly DependencyProperty DirectionProperty = DependencyProperty.Register("Direction",typeof(int),typeof(TheMQTT), new PropertyMetadata(0,new PropertyChangedCallback(OnPropertyChanged))); private static void OnPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) { (d as TheMQTT)?.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(TheMQTT),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(TheMQTT),new PropertyMetadata(1000)); [Category("数据绑定-数据来源")] public string DataSouceInformation { get { return (string)GetValue(DataSouceInformationProperty); } set { SetValue(DataSouceInformationProperty,value); } } public static readonly DependencyProperty DataSouceInformationProperty = DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheMQTT),new PropertyMetadata("admin,fengyoufu067101!@#,124.222.238.75,61613")); [Category("数据绑定-数据来源")] public string DeviceName { get { return (string)GetValue(DeviceNameProperty); } set { SetValue(DeviceNameProperty,value); } } public static readonly DependencyProperty DeviceNameProperty = DependencyProperty.Register("DeviceName",typeof(string),typeof(TheMQTT),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(TheMQTT),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onFDataSouceChanged))); private static void onFDataSouceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheMQTT)?.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(TheMQTT),new PropertyMetadata(_code)); [Category("数据绑定")] public bool IsRun { get { return (bool)GetValue(RunProperty); } set { SetValue(RunProperty, value); } } public static readonly DependencyProperty RunProperty = DependencyProperty.Register("IsRun", typeof(bool), typeof(TheMQTT), new PropertyMetadata(false, new PropertyChangedCallback(onIsRunChanged))); private static void onIsRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheMQTT)?.IsRunRefresh(); public void IsRunRefresh() { //测试运行 if (IsRun) IsExecuteState = true; else { IsExecuteState = false; Dispose(); } } [Category("数据绑定")] public string GenerateData { get { return (string)GetValue(GenerateDataProperty); } set { SetValue(GenerateDataProperty,value); } } public static readonly DependencyProperty GenerateDataProperty = DependencyProperty.Register("GenerateData",typeof(string),typeof(TheMQTT),new PropertyMetadata(string.Empty)); #endregion } }