终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

365 lines
14 KiB

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