终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

499 satır
21 KiB

  1. using BPASmartClient.Compiler;
  2. using BPASmartClient.MessageCommunication;
  3. using BPASmartClient.MessageCommunication.MsgControl;
  4. using BPASmartClient.MessageName;
  5. using BPASmartClient.MessageName.EnumHelp;
  6. using BPASmartClient.MessageName.发送消息Model;
  7. using BPASmartClient.MessageName.接收消息Model;
  8. using BPASmartClient.MessageName.接收消息Model.滚动线;
  9. using Newtonsoft.Json;
  10. using System;
  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.CustomerControls
  28. {
  29. /// <summary>
  30. /// 滚筒线
  31. /// NewConveyorBelt.xaml 的交互逻辑
  32. /// </summary>
  33. public partial class NewConveyorBelt : UserControl, IExecutable
  34. {
  35. #region 临时变量
  36. TextBlock textBlock = null;
  37. Path Path_mp = null;
  38. Path Path_cb = null;
  39. Storyboard storyboard = new Storyboard();
  40. Storyboard storyboard1 = new Storyboard();
  41. #endregion
  42. public event EventHandler PropertyChange; //声明一个事件
  43. public NewConveyorBelt()
  44. {
  45. InitializeComponent();
  46. Width = 1200;
  47. Height = 400;
  48. this.SizeChanged += ConveyorBelt_SizeChanged;
  49. ConveyorBeltWidth = 70;
  50. Direction = 0;
  51. StrokeBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00BEFA"));
  52. StrokeDashArray = new DoubleCollection { 1.5, 1.5 };
  53. StrokeFillBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00BEFA"));
  54. StrokeThickness = 2;
  55. this.Loaded += NewConveyorBelt_Loaded; ;
  56. }
  57. private void NewConveyorBelt_Loaded(object sender, RoutedEventArgs e)
  58. {
  59. EventReceiveNameList.CollectionChanged += EventNameList_CollectionChanged;
  60. EventSendNameList.CollectionChanged += EventSendNameList_CollectionChanged;
  61. }
  62. private void EventSendNameList_CollectionChanged(object? sender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  63. {
  64. if (EventSendNameList.Count > 0)
  65. {
  66. try
  67. {
  68. EventSendNameListStr = JsonConvert.SerializeObject(EventSendNameList);
  69. }
  70. catch (Exception ex)
  71. {
  72. }
  73. }
  74. }
  75. private void EventNameList_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  76. {
  77. if (EventReceiveNameList.Count > 0)
  78. {
  79. try
  80. {
  81. EventReceiveNameListStr = JsonConvert.SerializeObject(EventReceiveNameList);
  82. }
  83. catch (Exception ex)
  84. {
  85. }
  86. }
  87. }
  88. public string ControlType => "滚动线";
  89. private bool isExecuteState;
  90. public bool IsExecuteState
  91. {
  92. get { return isExecuteState; }
  93. set
  94. {
  95. isExecuteState = value;
  96. if (IsExecuteState)
  97. {
  98. IsEnabled = true;
  99. Register();
  100. Style = null;
  101. }
  102. }
  103. }
  104. #region 内部函数
  105. public void VisualStateManagerData()
  106. {
  107. storyboard.RepeatBehavior = RepeatBehavior.Forever;
  108. DoubleAnimation dbAscending = new DoubleAnimation
  109. {
  110. From = 0,
  111. To = 100,
  112. Duration = new Duration(new TimeSpan(0, 0, 20)),
  113. };
  114. storyboard.Children.Add(dbAscending);
  115. Storyboard.SetTarget(dbAscending, Path_mp);
  116. Storyboard.SetTargetProperty(dbAscending, new PropertyPath("StrokeDashOffset"));
  117. storyboard1.RepeatBehavior = RepeatBehavior.Forever;
  118. DoubleAnimation dbAscending1 = new DoubleAnimation
  119. {
  120. From = 0,
  121. To = -100,
  122. Duration = new Duration(new TimeSpan(0, 0, 20)),
  123. };
  124. storyboard1.Children.Add(dbAscending1);
  125. Storyboard.SetTarget(dbAscending1, Path_mp);
  126. Storyboard.SetTargetProperty(dbAscending1, new PropertyPath("StrokeDashOffset"));
  127. Refursh();
  128. }
  129. private void ConveyorBelt_SizeChanged(object sender, SizeChangedEventArgs e)
  130. {
  131. //查找
  132. if (!(Path_cb != null && Path_mp != null))
  133. {
  134. foreach (Path tb in FindVisualChildren<Path>(this))
  135. {
  136. // do something with tb here
  137. if (tb.Tag != null)
  138. {
  139. if (tb.Tag.ToString() == "cb")
  140. {
  141. Path_cb = tb;
  142. }
  143. else if (tb.Tag.ToString() == "mp")
  144. {
  145. Path_mp = tb;
  146. }
  147. }
  148. }
  149. VisualStateManagerData();
  150. }
  151. if (textBlock == null)
  152. {
  153. foreach (TextBlock tb in FindVisualChildren<TextBlock>(this))
  154. {
  155. // do something with tb here
  156. if (tb.Tag != null)
  157. {
  158. if (tb.Tag.ToString() == "标题")
  159. {
  160. textBlock = tb;
  161. }
  162. }
  163. }
  164. }
  165. if (this.Height > 0 && this.Width > 0)
  166. {
  167. try
  168. {
  169. //传送带外边框绘制
  170. PathGeometry geometry = new PathGeometry();
  171. PathFigure pathFigure = new PathFigure();
  172. pathFigure.StartPoint = new Point(this.Height / 2, 0);
  173. pathFigure.Segments.Add(new ArcSegment(new Point(this.Height / 2, this.Height), new Size(this.Height / 2, this.Height / 2), 0, false, SweepDirection.Counterclockwise, true));
  174. pathFigure.Segments.Add(new LineSegment(new Point(this.Width, this.Height), true));
  175. pathFigure.Segments.Add(new LineSegment(new Point(this.Width, this.Height - ConveyorBeltWidth), true));
  176. double innerCircle = (this.Height - (ConveyorBeltWidth * 2)) / 2;//内圆半径
  177. pathFigure.Segments.Add(new LineSegment(new Point(ConveyorBeltWidth + innerCircle, this.Height - ConveyorBeltWidth), true));
  178. pathFigure.Segments.Add(new ArcSegment(new Point(ConveyorBeltWidth + innerCircle, ConveyorBeltWidth), new Size(innerCircle, innerCircle), 0, false, SweepDirection.Clockwise, true));
  179. pathFigure.Segments.Add(new LineSegment(new Point(this.Width, ConveyorBeltWidth), true));
  180. pathFigure.Segments.Add(new LineSegment(new Point(this.Width, 0), true));
  181. pathFigure.Segments.Add(new LineSegment(new Point(this.Height / 2, 0), true));
  182. geometry.Figures.Add(pathFigure);
  183. Path_cb.Data = geometry;
  184. //传送带内部皮带绘制
  185. PathGeometry geometry1 = new PathGeometry();
  186. PathFigure pathFigure1 = new PathFigure();
  187. pathFigure1.StartPoint = new Point(this.Width, ConveyorBeltWidth / 2);
  188. double innerCircle1 = (this.Height - ConveyorBeltWidth) / 2;//内圆半径
  189. pathFigure1.Segments.Add(new LineSegment(new Point(innerCircle1 + ConveyorBeltWidth / 2, ConveyorBeltWidth / 2), true));
  190. pathFigure1.Segments.Add(new ArcSegment(new Point(innerCircle1 + ConveyorBeltWidth / 2, this.Height - ConveyorBeltWidth / 2), new Size(innerCircle1, innerCircle1), 0, false, SweepDirection.Counterclockwise, true));
  191. pathFigure1.Segments.Add(new LineSegment(new Point(this.Width, this.Height - ConveyorBeltWidth / 2), true));
  192. geometry1.Figures.Add(pathFigure1);
  193. Path_mp.Data = geometry1;
  194. }
  195. catch (Exception ex)
  196. {
  197. }
  198. }
  199. }
  200. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  201. {
  202. if (depObj != null)
  203. {
  204. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  205. {
  206. DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
  207. if (child != null && child is T)
  208. {
  209. yield return (T)child;
  210. }
  211. foreach (T childOfChild in FindVisualChildren<T>(child))
  212. {
  213. yield return childOfChild;
  214. }
  215. }
  216. }
  217. }
  218. #endregion
  219. #region 属性
  220. private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  221. {
  222. (d as NewConveyorBelt)?.Refursh();
  223. }
  224. public DoubleCollection StrokeDashArray
  225. {
  226. get { return (DoubleCollection)GetValue(StrokeDashArrayProperty); }
  227. set { SetValue(StrokeDashArrayProperty, value); }
  228. }
  229. public static readonly DependencyProperty StrokeDashArrayProperty =
  230. DependencyProperty.Register("StrokeDashArray", typeof(DoubleCollection), typeof(NewConveyorBelt),
  231. new PropertyMetadata(default, new PropertyChangedCallback(OnPropertyChanged)));
  232. public double ConveyorBeltWidth
  233. {
  234. get { return (double)GetValue(ConveyorBeltWidthProperty); }
  235. set { SetValue(ConveyorBeltWidthProperty, value); }
  236. }
  237. public static readonly DependencyProperty ConveyorBeltWidthProperty =
  238. DependencyProperty.Register("ConveyorBeltWidth", typeof(double), typeof(NewConveyorBelt),
  239. new PropertyMetadata(50.0, new PropertyChangedCallback(OnPropertyChanged)));
  240. public Brush StrokeFillBrush
  241. {
  242. get { return (Brush)GetValue(StrokeFillBrushProperty); }
  243. set { SetValue(StrokeFillBrushProperty, value); }
  244. }
  245. public static readonly DependencyProperty StrokeFillBrushProperty =
  246. DependencyProperty.Register("StrokeFillBrush", typeof(Brush), typeof(NewConveyorBelt),
  247. new PropertyMetadata(Brushes.LightBlue, new PropertyChangedCallback(OnPropertyChanged)));
  248. public Brush StrokeBrush
  249. {
  250. get { return (Brush)GetValue(StrokeBrushProperty); }
  251. set { SetValue(StrokeBrushProperty, value); }
  252. }
  253. public static readonly DependencyProperty StrokeBrushProperty =
  254. DependencyProperty.Register("StrokeBrush", typeof(Brush), typeof(NewConveyorBelt),
  255. new PropertyMetadata(Brushes.LightBlue, new PropertyChangedCallback(OnPropertyChanged)));
  256. public double StrokeThickness
  257. {
  258. get { return (double)GetValue(StrokeThicknessProperty); }
  259. set { SetValue(StrokeThicknessProperty, value); }
  260. }
  261. public static readonly DependencyProperty StrokeThicknessProperty =
  262. DependencyProperty.Register("StrokeThickness", typeof(double), typeof(NewConveyorBelt),
  263. new PropertyMetadata(1.0, new PropertyChangedCallback(OnPropertyChanged)));
  264. [Category("值设定")]
  265. public int Direction
  266. {
  267. get { return (int)GetValue(DirectionProperty); }
  268. set { SetValue(DirectionProperty, value); }
  269. }
  270. public static readonly DependencyProperty DirectionProperty =
  271. DependencyProperty.Register("Direction", typeof(int), typeof(NewConveyorBelt),
  272. new PropertyMetadata(0, new PropertyChangedCallback(OnPropertyChanged)));
  273. [Category("值设定")]
  274. public string Text
  275. {
  276. get { return (string)GetValue(TextProperty); }
  277. set { SetValue(TextProperty, value); }
  278. }
  279. public static readonly DependencyProperty TextProperty =
  280. DependencyProperty.Register("Text", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(new PropertyChangedCallback(onTargetChanged)));
  281. private static void onTargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as NewConveyorBelt)?.TargetRefresh();
  282. [Category("主题订阅")]
  283. public string ConveyorBeltLeft
  284. {
  285. get { return (string)GetValue(ConveyorBeltLeftProperty); }
  286. set { SetValue(ConveyorBeltLeftProperty, value); }
  287. }
  288. public static readonly DependencyProperty ConveyorBeltLeftProperty =
  289. DependencyProperty.Register("ConveyorBeltLeft", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(string.Empty));
  290. [Category("主题订阅")]
  291. public string ConveyorBeltRight
  292. {
  293. get { return (string)GetValue(ConveyorBeltRightProperty); }
  294. set { SetValue(ConveyorBeltRightProperty, value); }
  295. }
  296. public static readonly DependencyProperty ConveyorBeltRightProperty =
  297. DependencyProperty.Register("ConveyorBeltRight", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(string.Empty));
  298. [Category("主题订阅")]
  299. public string ConveyorBeltStop
  300. {
  301. get { return (string)GetValue(ConveyorBeltStopProperty); }
  302. set { SetValue(ConveyorBeltStopProperty, value); }
  303. }
  304. public static readonly DependencyProperty ConveyorBeltStopProperty =
  305. DependencyProperty.Register("ConveyorBeltStop", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(string.Empty));
  306. [Category("名称[自动生成]")]
  307. public string EventReceiveNameListStr
  308. {
  309. get { return (string)GetValue(EventReceiveNameListStrProperty); }
  310. set { SetValue(EventReceiveNameListStrProperty, value); }
  311. }
  312. public static readonly DependencyProperty EventReceiveNameListStrProperty =
  313. DependencyProperty.Register("EventReceiveNameListStr",typeof(string),typeof(NewConveyorBelt),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onEventReceiveNameListStrChanged)));
  314. private static void onEventReceiveNameListStrChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as NewConveyorBelt)?.ReceiveNameRefresh();
  315. [Category("消息接收名称集合")]
  316. public ObservableCollection<EventReceiveMessage> EventReceiveNameList
  317. {
  318. get { return (ObservableCollection<EventReceiveMessage>)GetValue(EventReceiveNameListProperty); }
  319. set { SetValue(EventReceiveNameListProperty, value); }
  320. }
  321. public static readonly DependencyProperty EventReceiveNameListProperty =
  322. DependencyProperty.Register("EventReceiveNameList", typeof(ObservableCollection<EventReceiveMessage>), typeof(NewConveyorBelt), new PropertyMetadata(new ObservableCollection<EventReceiveMessage>(), onEventNameListChanged));
  323. private static void onEventNameListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as NewConveyorBelt)?.RunNameRefresh();
  324. [Category("名称[自动生成]")]
  325. private string EventSendNameListStr
  326. {
  327. get { return (string)GetValue(EventSendNameListStrProperty); }
  328. set { SetValue(EventSendNameListStrProperty,value); }
  329. }
  330. private static readonly DependencyProperty EventSendNameListStrProperty =
  331. DependencyProperty.Register("EventSendNameListStr",typeof(string),typeof(NewConveyorBelt),new PropertyMetadata(string.Empty));
  332. [Category("消息发送名称集合")]
  333. private ObservableCollection<EventSendMessage> EventSendNameList
  334. {
  335. get { return (ObservableCollection<EventSendMessage>)GetValue(EventSendNameListProperty); }
  336. set { SetValue(EventSendNameListProperty,value); }
  337. }
  338. private static readonly DependencyProperty EventSendNameListProperty =
  339. DependencyProperty.Register("EventSendNameList",typeof(ObservableCollection<EventSendMessage>),typeof(NewConveyorBelt),new PropertyMetadata(new ObservableCollection<EventSendMessage>(),new PropertyChangedCallback(onEventNameListChanged)));
  340. #endregion
  341. #region 属性变更事件
  342. List<string> MessageNameL = null;
  343. public void Register()
  344. {
  345. if (!string.IsNullOrEmpty(EventReceiveNameListStr))
  346. {
  347. try
  348. {
  349. EventReceiveNameList = JsonConvert.DeserializeObject<ObservableCollection<EventReceiveMessage>>(EventReceiveNameListStr);
  350. }
  351. catch (Exception ex)
  352. {
  353. }
  354. }
  355. if (!string.IsNullOrEmpty(EventSendNameListStr))
  356. {
  357. try
  358. {
  359. EventSendNameList = JsonConvert.DeserializeObject<ObservableCollection<EventSendMessage>>(EventSendNameListStr);
  360. }
  361. catch (Exception ex)
  362. {
  363. }
  364. }
  365. List<string> MessageNameNew = EventReceiveNameList?.Select(o => o.MeaageName.ToString())?.Distinct()?.ToList();
  366. if (MessageNameL == null || !MessageNameNew.SequenceEqual(MessageNameL))
  367. {
  368. MessageNameL?.ForEach(o => Class_InnerMessageBus.GetInstance().RemoveMessage(o,"NewConveyorBeltEventHandler"));
  369. MessageNameNew?.ForEach(x => Class_InnerMessageBus.GetInstance().ListenMessage(this,x,"NewConveyorBeltEventHandler"));
  370. MessageNameL = MessageNameNew;
  371. }
  372. }
  373. public void NewConveyorBeltEventHandler(object sender, InnerMessageEventArgs e)
  374. {
  375. try
  376. {
  377. if (IsExecuteState = true)
  378. {
  379. if (e.obj_MessageObj is RunEnumModel)//接收到运行消息
  380. {
  381. RunEnumModel mode = (RunEnumModel)e.obj_MessageObj;
  382. var msg = EventReceiveNameList?.ToList().Find(par => par.MeaageName.ToString() == e.str_MessageStr && par.MeaageID == mode.MessageID);
  383. //必对消息号:
  384. if (msg != null)
  385. {
  386. Direction = mode.Run == RunEnum.Run ? 1 : ((mode.Run == RunEnum.Run_Left) ? 2 : 0);
  387. }
  388. }
  389. else if (e.obj_MessageObj is ConveyorBeltMessageModel)//接收到数据模型
  390. {
  391. ConveyorBeltMessageModel mode = (ConveyorBeltMessageModel)e.obj_MessageObj;
  392. var msg = EventReceiveNameList?.ToList().Find(par => par.MeaageName.ToString() == e.str_MessageStr && par.MeaageID == mode.MessageID);
  393. //必对消息号:
  394. if (msg != null)
  395. {
  396. Text = mode.Title;
  397. }
  398. }
  399. }
  400. }
  401. catch (Exception ex)
  402. {
  403. }
  404. }
  405. private void Refursh()
  406. {
  407. if (Direction == 1)
  408. {
  409. storyboard.Begin();
  410. storyboard1.Stop();
  411. }
  412. else if (Direction == 2)
  413. {
  414. storyboard.Stop();
  415. storyboard1.Begin();
  416. }
  417. else
  418. {
  419. storyboard.Stop();
  420. storyboard1.Stop();
  421. }
  422. }
  423. /// <summary>
  424. /// 目标属性刷新
  425. /// </summary>
  426. public void TargetRefresh()
  427. {
  428. if (textBlock != null )
  429. {
  430. textBlock.Text = Text;
  431. }
  432. }
  433. public void RunNameRefresh()
  434. {
  435. }
  436. /// <summary>
  437. /// 接收数据改变
  438. /// </summary>
  439. public void ReceiveNameRefresh()
  440. {
  441. if (!string.IsNullOrEmpty(EventReceiveNameListStr))
  442. {
  443. try
  444. {
  445. EventReceiveNameList = JsonConvert.DeserializeObject<ObservableCollection<EventReceiveMessage>>(EventReceiveNameListStr);
  446. }
  447. catch (Exception ex)
  448. {
  449. }
  450. }
  451. }
  452. #endregion
  453. }
  454. }