|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- 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
- {
- /// <summary>
- /// TheMQTT.xaml 的交互逻辑
- /// </summary>
- 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<Image>(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<TextBlock>(this))
- {
- if (tb.Tag != null)
- {
- if (tb.Tag.ToString() == "显示文字")
- {
- textBlock = tb;
- }
- }
- }
- }
- }
- public static IEnumerable<T> FindVisualChildren<T>(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<T>(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.FromSeconds(TimeCount);
- timer.Tick += Timer_Tick;
- timer.Start();
-
- }
- Task<bool> 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();
- }
-
- #region 属性
- [Category("事件")]
- public string Value
- {
- get { return (string)GetValue(ValueProperty); }
- set { SetValue(ValueProperty,value); }
- }
- public static readonly DependencyProperty ValueProperty =
- DependencyProperty.Register("Value",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(Value))
- {
- publishInfo.PublishModels.Add(JsonConvert.DeserializeObject<PublishModel>(Value));
- 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"));
- }
- }
- }
- /// <summary>
- /// 执行内容
- /// </summary>
- [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(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(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 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
-
- }
- }
|