终端一体化运控平台
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

343 líneas
13 KiB

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