终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

359 строки
13 KiB

  1. using BPASmartClient.Compiler;
  2. using BPASmartClient.MessageCommunication;
  3. using BPASmartClient.MessageCommunication.MsgControl;
  4. using BPASmartClient.MessageName;
  5. using BPASmartClient.MessageName.Enum.运行状态;
  6. using BPASmartClient.MessageName.接收消息Model;
  7. using BPASmartClient.MessageName.接收消息Model.物料仓;
  8. using Newtonsoft.Json;
  9. using System;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.ComponentModel;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Data;
  20. using System.Windows.Documents;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Animation;
  24. using System.Windows.Media.Imaging;
  25. using System.Windows.Navigation;
  26. using System.Windows.Shapes;
  27. namespace BPASmartClient.SCADAControl
  28. {
  29. /// <summary>
  30. /// Silos.xaml 的交互逻辑
  31. /// 物料仓
  32. /// </summary>
  33. public partial class Silos : UserControl, IExecutable
  34. {
  35. #region 临时变量
  36. TextBlock textBlockCLKZ = null;
  37. TextBlock textBlockValue=null;
  38. TextBlock textBlockText = null;
  39. TextBlock textBlockTitle = null;
  40. Ellipse ellipseControl = null;
  41. Storyboard storyboard = new Storyboard();
  42. #endregion
  43. public Silos()
  44. {
  45. InitializeComponent();
  46. this.DataContext = this;
  47. Width = 180;
  48. Height = 270;
  49. this.Loaded += Silos_Loaded;
  50. }
  51. private void Silos_Loaded(object sender, RoutedEventArgs e)
  52. {
  53. Silos_SizeChanged(null, null);
  54. EventNameList.CollectionChanged += EventNameList_CollectionChanged;
  55. }
  56. private void EventNameList_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  57. {
  58. if (EventNameList.Count > 0)
  59. {
  60. try
  61. {
  62. EventNameListStr = JsonConvert.SerializeObject(EventNameList);
  63. }
  64. catch (Exception ex)
  65. {
  66. }
  67. }
  68. }
  69. public string ControlType => "物料仓";
  70. private bool isExecuteState;
  71. public bool IsExecuteState
  72. {
  73. get { return isExecuteState; }
  74. set
  75. {
  76. isExecuteState = value;
  77. if (IsExecuteState)
  78. {
  79. IsEnabled = true;
  80. Register();
  81. Style = null;
  82. }
  83. }
  84. }
  85. #region 动画
  86. /// <summary>
  87. /// 字节改变
  88. /// </summary>
  89. /// <param name="sender"></param>
  90. /// <param name="e"></param>
  91. private void Silos_SizeChanged(object sender, SizeChangedEventArgs e)
  92. {
  93. //查找
  94. if (ellipseControl == null)
  95. {
  96. foreach (Ellipse tb in FindVisualChildren<Ellipse>(this))
  97. {
  98. // do something with tb here
  99. if (tb.Tag != null)
  100. {
  101. if (tb.Tag.ToString() == "出料圆")
  102. {
  103. ellipseControl = tb;
  104. }
  105. }
  106. }
  107. foreach (TextBlock tb in FindVisualChildren<TextBlock>(this))
  108. {
  109. // do something with tb here
  110. if (tb.Tag != null)
  111. {
  112. if (tb.Tag.ToString() == "出料控制")
  113. {
  114. textBlockCLKZ = tb;
  115. }else if (tb.Tag.ToString() == "Value")
  116. {
  117. textBlockValue = tb;
  118. }
  119. else if (tb.Tag.ToString() == "Text")
  120. {
  121. textBlockText = tb;
  122. }
  123. else if (tb.Tag.ToString() == "Title")
  124. {
  125. textBlockTitle = tb;
  126. }
  127. }
  128. }
  129. }
  130. storyboard.RepeatBehavior = RepeatBehavior.Forever;
  131. DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames();
  132. Storyboard.SetTarget(animation, ellipseControl);
  133. Storyboard.SetTargetProperty(animation, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"));
  134. animation.KeyFrames.Add(new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0))));
  135. animation.KeyFrames.Add(new EasingDoubleKeyFrame(180, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))));
  136. animation.KeyFrames.Add(new EasingDoubleKeyFrame(360, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2))));
  137. storyboard.Children.Add(animation);
  138. }
  139. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  140. {
  141. if (depObj != null)
  142. {
  143. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  144. {
  145. DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
  146. if (child != null && child is T)
  147. {
  148. yield return (T)child;
  149. }
  150. foreach (T childOfChild in FindVisualChildren<T>(child))
  151. {
  152. yield return childOfChild;
  153. }
  154. }
  155. }
  156. }
  157. #endregion
  158. #region 属性
  159. [Category("值设定")]
  160. public string Value
  161. {
  162. get { return (string)GetValue(ValueProperty); }
  163. set { SetValue(ValueProperty, value); }
  164. }
  165. public static readonly DependencyProperty ValueProperty =
  166. DependencyProperty.Register("Value", typeof(string), typeof(Silos), new PropertyMetadata(new PropertyChangedCallback(onTargetChanged)));
  167. private static void onTargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as Silos)?.TargetRefresh();
  168. [Category("值设定")]
  169. public string Text
  170. {
  171. get { return (string)GetValue(TextProperty); }
  172. set { SetValue(TextProperty, value); }
  173. }
  174. public static readonly DependencyProperty TextProperty =
  175. DependencyProperty.Register("Text", typeof(string), typeof(Silos), new PropertyMetadata(new PropertyChangedCallback(onTargetChanged)));
  176. [Category("值设定")]
  177. public string Title
  178. {
  179. get { return (string)GetValue(TitleProperty); }
  180. set { SetValue(TitleProperty, value); }
  181. }
  182. public static readonly DependencyProperty TitleProperty =
  183. DependencyProperty.Register("Title", typeof(string), typeof(Silos), new PropertyMetadata(new PropertyChangedCallback(onTargetChanged)));
  184. [Category("消息名称")]
  185. public string EventNameListStr
  186. {
  187. get { return (string)GetValue(EventNameListStrProperty); }
  188. set { SetValue(EventNameListStrProperty, value); }
  189. }
  190. public static readonly DependencyProperty EventNameListStrProperty =
  191. DependencyProperty.Register("EventNameListStr", typeof(string), typeof(Silos), new PropertyMetadata(string.Empty));
  192. [Category("消息名称集合")]
  193. public ObservableCollection<EventReceiveMessage> EventNameList
  194. {
  195. get { return (ObservableCollection<EventReceiveMessage>)GetValue(EventNameListProperty); }
  196. set { SetValue(EventNameListProperty, value); }
  197. }
  198. public static readonly DependencyProperty EventNameListProperty =
  199. DependencyProperty.Register("EventNameList", typeof(ObservableCollection<EventReceiveMessage>), typeof(Silos), new PropertyMetadata(new ObservableCollection<EventReceiveMessage>(), new PropertyChangedCallback(onEventNameListChanged)));
  200. private static void onEventNameListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as Silos)?.DataNameRefresh();
  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(Silos),
  209. new PropertyMetadata(0, new PropertyChangedCallback(OnPropertyChanged)));
  210. private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  211. {
  212. (d as Silos)?.Refresh();
  213. }
  214. #endregion
  215. #region 函数
  216. /// <summary>
  217. /// 目标属性刷新
  218. /// </summary>
  219. public void TargetRefresh()
  220. {
  221. if (textBlockValue != null && textBlockText != null && textBlockTitle != null)
  222. {
  223. textBlockValue.Text = Value;
  224. textBlockText.Text = Text;
  225. textBlockTitle.Text = Title;
  226. }
  227. }
  228. /// <summary>
  229. /// 值变化刷新
  230. /// </summary>
  231. public void Refresh()
  232. {
  233. if (ellipseControl != null)
  234. {
  235. if (Direction == 1)
  236. {
  237. ellipseControl.Visibility = Visibility.Visible;
  238. textBlockCLKZ.Visibility = Visibility.Visible;
  239. storyboard.Begin();
  240. }
  241. else if (Direction == 2)
  242. {
  243. ellipseControl.Visibility = Visibility.Collapsed;
  244. textBlockCLKZ.Visibility = Visibility.Collapsed;
  245. storyboard.Stop();
  246. }
  247. else
  248. {
  249. ellipseControl.Visibility = Visibility.Collapsed;
  250. textBlockCLKZ.Visibility = Visibility.Collapsed;
  251. storyboard.Stop();
  252. }
  253. }
  254. }
  255. /// <summary>
  256. /// 运行名称变化事件
  257. /// </summary>
  258. public void RunNameRefresh()
  259. {
  260. }
  261. /// <summary>
  262. /// 数据名称变化事件
  263. /// </summary>
  264. public void DataNameRefresh()
  265. {
  266. }
  267. #endregion
  268. #region 运行事件
  269. public void Register()
  270. {
  271. if (!string.IsNullOrEmpty(EventNameListStr))
  272. {
  273. try
  274. {
  275. EventNameList = JsonConvert.DeserializeObject<ObservableCollection<EventReceiveMessage>>(EventNameListStr);
  276. }
  277. catch (Exception ex)
  278. {
  279. }
  280. }
  281. if (EventNameList != null)
  282. {
  283. EventNameList?.ToList().ForEach(x =>
  284. {
  285. Class_InnerMessageBus.GetInstance().ListenMessage(this, x.Name.ToString(), "EventHandler");
  286. });
  287. }
  288. }
  289. /// <summary>
  290. /// 统一事件消息处理中心
  291. /// </summary>
  292. /// <param name="sender"></param>
  293. /// <param name="e"></param>
  294. public void EventHandler(object sender, InnerMessageEventArgs e)
  295. {
  296. try
  297. {
  298. if (IsExecuteState = true)//正在运行
  299. {
  300. if (e.obj_MessageObj is RunEnumModel)//接收到运行消息
  301. {
  302. RunEnumModel mode = (RunEnumModel)e.obj_MessageObj;
  303. var msg = EventNameList?.ToList().Find(par => par.Name.ToString() == e.str_MessageStr && par.MeaageID == mode.MessageID);
  304. //必对消息号:
  305. if (msg!=null)
  306. {
  307. Direction = mode.Run == RunEnum.Run ? 1 : 2;
  308. }
  309. }else if (e.obj_MessageObj is SilosMessageModel)//接收到数据模型
  310. {
  311. SilosMessageModel mode = (SilosMessageModel)e.obj_MessageObj;
  312. var msg = EventNameList?.ToList().Find(par => par.Name.ToString() == e.str_MessageStr && par.MeaageID == mode.MessageID);
  313. //必对消息号:
  314. if (msg != null)
  315. {
  316. Title = mode.Title;
  317. Value = mode.Value;
  318. }
  319. }
  320. }
  321. }
  322. catch (Exception ex)
  323. {
  324. }
  325. }
  326. #endregion
  327. }
  328. }