终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

353 righe
13 KiB

  1. using BPASmartClient.Compiler;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using System.Windows.Threading;
  11. namespace BPASmartClient.SCADAControl.CustomerControls
  12. {
  13. /// <summary>
  14. /// TheMQTT.xaml 的交互逻辑
  15. /// </summary>
  16. public partial class TheMQTT :UserControl, IExecutable, IDisposable
  17. {
  18. Image imageZC = null;
  19. Image imageYC = null;
  20. Image imageGZ = null;
  21. TextBlock textBlock = null;
  22. public TheMQTT()
  23. {
  24. InitializeComponent();
  25. Width = 40;
  26. Height = 40;
  27. this.SizeChanged += TheRedis_SizeChanged;
  28. }
  29. private void TheRedis_SizeChanged(object sender,SizeChangedEventArgs e)
  30. {
  31. if (textBlock == null)
  32. {
  33. foreach (Image tb in FindVisualChildren<Image>(this))
  34. {
  35. if (tb.Tag != null)
  36. {
  37. if (tb.Tag.ToString() == "正常")
  38. {
  39. imageZC = tb;
  40. }
  41. if (tb.Tag.ToString() == "运行")
  42. {
  43. imageYC = tb;
  44. }
  45. if (tb.Tag.ToString() == "故障")
  46. {
  47. imageGZ = tb;
  48. }
  49. }
  50. }
  51. foreach (TextBlock tb in FindVisualChildren<TextBlock>(this))
  52. {
  53. if (tb.Tag != null)
  54. {
  55. if (tb.Tag.ToString() == "显示文字")
  56. {
  57. textBlock = tb;
  58. }
  59. }
  60. }
  61. }
  62. }
  63. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  64. {
  65. if (depObj != null)
  66. {
  67. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  68. {
  69. DependencyObject child = VisualTreeHelper.GetChild(depObj,i);
  70. if (child != null && child is T)
  71. {
  72. yield return (T)child;
  73. }
  74. foreach (T childOfChild in FindVisualChildren<T>(child))
  75. {
  76. yield return childOfChild;
  77. }
  78. }
  79. }
  80. }
  81. public string ControlType => "控件";
  82. private bool isExecuteState;
  83. public bool IsExecuteState
  84. {
  85. get { return isExecuteState; }
  86. set
  87. {
  88. isExecuteState = value;
  89. if (IsExecuteState)
  90. {
  91. Register();
  92. //Style = null;
  93. }
  94. }
  95. }
  96. MQTT mQTT = new MQTT();
  97. DispatcherTimer timer = new DispatcherTimer();
  98. public void Register()
  99. {
  100. if (Direction == 0)
  101. {
  102. try
  103. {
  104. if (!string.IsNullOrEmpty(DataSouceInformation))
  105. {
  106. if (DataSouceInformation.Contains(","))
  107. {
  108. string[] lj=DataSouceInformation.Split(',');
  109. if (lj.Length >= 4)
  110. {
  111. int port = 8087;
  112. port= int.Parse(lj[3]);
  113. mQTT.MqttInitAsync(lj[0],lj[1],lj[2],port,$"分布式上位机:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
  114. }
  115. }
  116. }
  117. }
  118. catch (Exception ex)
  119. {
  120. Direction = 2;
  121. }
  122. }
  123. timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
  124. timer.Tick += Timer_Tick;
  125. timer.Start();
  126. }
  127. Task<bool> isSuccess;
  128. private async void Timer_Tick(object sender,EventArgs e)
  129. {
  130. Config.GetInstance().RunJsScipt(TikcExecute);
  131. if (mQTT.client != null && mQTT.client.IsConnected)
  132. {
  133. Direction = 1;
  134. }
  135. else
  136. {
  137. Direction = 0;
  138. try
  139. {
  140. if (!string.IsNullOrEmpty(DataSouceInformation))
  141. mQTT.MqttInitAsync("admin","fengyoufu067101!@#","124.222.238.75",61613,$"分布式上位机:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
  142. }
  143. catch (Exception ex)
  144. {
  145. Direction = 2;
  146. }
  147. }
  148. if (Direction == 1) //定时读取数据
  149. {
  150. try
  151. {
  152. //定时读取数据
  153. }
  154. catch (Exception ex)
  155. {
  156. }
  157. }
  158. }
  159. public void Start() => timer.Start();
  160. public void Stop() => timer.Stop();
  161. public void Dispose()
  162. {
  163. timer.Stop();
  164. Direction = 0;
  165. FDataSouce = "";
  166. if (IsRun) IsRun = false;
  167. }
  168. #region 属性
  169. [Category("事件")]
  170. public string SendText
  171. {
  172. get { return (string)GetValue(SendTextProperty); }
  173. set { SetValue(SendTextProperty, value); }
  174. }
  175. public static readonly DependencyProperty SendTextProperty =
  176. DependencyProperty.Register("SendText", typeof(string),typeof(TheMQTT),new PropertyMetadata(string.Empty,new PropertyChangedCallback(OnValuePropertyChanged)));
  177. private static void OnValuePropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
  178. {
  179. (d as TheMQTT)?.RefreshMQTT();
  180. }
  181. public void RefreshMQTT()
  182. {
  183. PublishInfo publishInfo = new PublishInfo();
  184. if (!string.IsNullOrEmpty(SendText))
  185. {
  186. publishInfo.PublishModels.Add(JsonConvert.DeserializeObject<PublishModel>(SendText));
  187. if (mQTT.client != null && mQTT.client.IsConnected)
  188. mQTT.MqttPublishAsync("DistributedHostComputer/Control",JsonConvert.SerializeObject(publishInfo));
  189. }
  190. }
  191. [Category("值设定")]
  192. public int Direction
  193. {
  194. get { return (int)GetValue(DirectionProperty); }
  195. set { SetValue(DirectionProperty,value); }
  196. }
  197. public static readonly DependencyProperty DirectionProperty =
  198. DependencyProperty.Register("Direction",typeof(int),typeof(TheMQTT),
  199. new PropertyMetadata(0,new PropertyChangedCallback(OnPropertyChanged)));
  200. private static void OnPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
  201. {
  202. (d as TheMQTT)?.Refresh();
  203. }
  204. public void Refresh()
  205. {
  206. if (textBlock != null)
  207. {
  208. if (Direction == 1)
  209. {
  210. imageZC.Visibility = Visibility.Collapsed;
  211. imageYC.Visibility = Visibility.Visible;
  212. imageGZ.Visibility = Visibility.Collapsed;
  213. textBlock.Visibility = Visibility.Visible;
  214. textBlock.Text = "运行中";
  215. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF2077EC"));
  216. }
  217. else if (Direction == 2)
  218. {
  219. imageZC.Visibility = Visibility.Collapsed;
  220. imageYC.Visibility = Visibility.Collapsed;
  221. imageGZ.Visibility = Visibility.Visible;
  222. textBlock.Visibility = Visibility.Visible;
  223. textBlock.Text = "故障";
  224. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFFF0202"));
  225. }
  226. else
  227. {
  228. imageZC.Visibility = Visibility.Visible;
  229. imageYC.Visibility = Visibility.Collapsed;
  230. imageGZ.Visibility = Visibility.Collapsed;
  231. textBlock.Visibility = Visibility.Visible;
  232. textBlock.Text = "未运行";
  233. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF02F9FF"));
  234. }
  235. }
  236. }
  237. /// <summary>
  238. /// 执行内容
  239. /// </summary>
  240. [Category("事件")]
  241. public string TikcExecute
  242. {
  243. get { return (string)GetValue(TikcExecuteProperty); }
  244. set { SetValue(TikcExecuteProperty,value); }
  245. }
  246. public static readonly DependencyProperty TikcExecuteProperty =
  247. DependencyProperty.Register("TikcExecute",typeof(string),typeof(TheMQTT),new PropertyMetadata(string.Empty));
  248. #endregion
  249. #region 数据绑定模块
  250. public event EventHandler PropertyChange; //声明一个事件
  251. [Category("数据绑定-数据来源")]
  252. public int TimeCount
  253. {
  254. get { return (int)GetValue(TimeCountProperty); }
  255. set { SetValue(TimeCountProperty,value); }
  256. }
  257. public static readonly DependencyProperty TimeCountProperty =
  258. DependencyProperty.Register("TimeCount",typeof(int),typeof(TheMQTT),new PropertyMetadata(1000));
  259. [Category("数据绑定-数据来源")]
  260. public string DataSouceInformation
  261. {
  262. get { return (string)GetValue(DataSouceInformationProperty); }
  263. set { SetValue(DataSouceInformationProperty,value); }
  264. }
  265. public static readonly DependencyProperty DataSouceInformationProperty =
  266. DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheMQTT),new PropertyMetadata("admin,fengyoufu067101!@#,124.222.238.75,61613"));
  267. [Category("数据绑定-数据来源")]
  268. public string DeviceName
  269. {
  270. get { return (string)GetValue(DeviceNameProperty); }
  271. set { SetValue(DeviceNameProperty,value); }
  272. }
  273. public static readonly DependencyProperty DeviceNameProperty =
  274. DependencyProperty.Register("DeviceName",typeof(string),typeof(TheMQTT),new PropertyMetadata(string.Empty));
  275. [Category("数据绑定")]
  276. public string FDataSouce
  277. {
  278. get { return (string)GetValue(FDataSouceProperty); }
  279. set { SetValue(FDataSouceProperty,value); }
  280. }
  281. public static readonly DependencyProperty FDataSouceProperty =
  282. DependencyProperty.Register("FDataSouce",typeof(string),typeof(TheMQTT),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onFDataSouceChanged)));
  283. private static void onFDataSouceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheMQTT)?.DataSouceRefresh();
  284. public void DataSouceRefresh()
  285. {
  286. try
  287. {
  288. //if (!string.IsNullOrEmpty(FDataSouce))
  289. {
  290. GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code,new object[] { FDataSouce });
  291. }
  292. }
  293. catch (Exception ex)
  294. {
  295. }
  296. }
  297. public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n";
  298. [Category("数据绑定")]
  299. public string Code
  300. {
  301. get { return (string)GetValue(CodeProperty); }
  302. set { SetValue(CodeProperty,value); }
  303. }
  304. public static readonly DependencyProperty CodeProperty =
  305. DependencyProperty.Register("Code",typeof(string),typeof(TheMQTT),new PropertyMetadata(_code));
  306. [Category("数据绑定")]
  307. public bool IsRun
  308. {
  309. get { return (bool)GetValue(RunProperty); }
  310. set { SetValue(RunProperty, value); }
  311. }
  312. public static readonly DependencyProperty RunProperty =
  313. DependencyProperty.Register("IsRun", typeof(bool), typeof(TheMQTT), new PropertyMetadata(false, new PropertyChangedCallback(onIsRunChanged)));
  314. private static void onIsRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheMQTT)?.IsRunRefresh();
  315. public void IsRunRefresh()
  316. {
  317. //测试运行
  318. if (IsRun)
  319. IsExecuteState = true;
  320. else
  321. {
  322. IsExecuteState = false;
  323. Dispose();
  324. }
  325. }
  326. [Category("数据绑定")]
  327. public string GenerateData
  328. {
  329. get { return (string)GetValue(GenerateDataProperty); }
  330. set { SetValue(GenerateDataProperty,value); }
  331. }
  332. public static readonly DependencyProperty GenerateDataProperty =
  333. DependencyProperty.Register("GenerateData",typeof(string),typeof(TheMQTT),new PropertyMetadata(string.Empty));
  334. #endregion
  335. }
  336. }