using BPASmartClient.Compiler;
using BPASmartClient.DATABUS;
using BPASmartClient.MessageCommunication;
using BPASmartClient.MessageCommunication.MsgControl;
using BPASmartClient.MessageName;
using BPASmartClient.MessageName.EnumHelp;
using BPASmartClient.MessageName.发送消息Model;
using BPASmartClient.MessageName.接收消息Model;
using BPASmartClient.MessageName.接收消息Model.物料仓;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace BPASmartClient.SCADAControl.CustomerControls
{
///
/// Silos.xaml 的交互逻辑
/// 物料仓
///
public partial class Silos :UserControl, IExecutable, IDisposable
{
#region 临时变量
TextBlock textBlockCLKZ = null;
TextBlock textBlockValue = null;
TextBlock textBlockText = null;
TextBlock textBlockTitle = null;
Ellipse ellipseControl = null;
Storyboard storyboard = new Storyboard();
object typeName;
public List typeNameList = new List();
public Enum @enum = null;
#endregion
public Silos()
{
InitializeComponent();
this.DataContext = this;
Width = 180;
Height = 270;
this.SizeChanged += Silos_SizeChanged; ;
this.Loaded += Silos_Loaded;
}
private void Silos_Loaded(object sender,RoutedEventArgs e)
{
EventReceiveNameList.CollectionChanged += EventNameList_CollectionChanged;
EventSendNameList.CollectionChanged += EventSendNameList_CollectionChanged;
}
private void EventSendNameList_CollectionChanged(object? sender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (EventSendNameList.Count > 0)
{
try
{
EventSendNameListStr = JsonConvert.SerializeObject(EventSendNameList);
}
catch (Exception ex)
{
}
}
}
private void EventNameList_CollectionChanged(object? sender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (EventReceiveNameList.Count > 0)
{
try
{
EventReceiveNameListStr = JsonConvert.SerializeObject(EventReceiveNameList);
}
catch (Exception ex)
{
}
}
}
public string ControlType => "物料仓";
private bool isExecuteState;
public bool IsExecuteState
{
get { return isExecuteState; }
set
{
isExecuteState = value;
if (IsExecuteState)
{
IsEnabled = true;
Register();
Style = null;
}
}
}
#region 动画
///
/// 加载内部变量
///
private void Silos_SizeChanged(object sender,SizeChangedEventArgs e)
{
//查找
if (ellipseControl == null)
{
foreach (Ellipse tb in FindVisualChildren(this))
{
// do something with tb here
if (tb.Tag != null)
{
if (tb.Tag.ToString() == "出料圆")
{
ellipseControl = tb;
storyboard.RepeatBehavior = RepeatBehavior.Forever;
DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames();
Storyboard.SetTarget(animation,ellipseControl);
Storyboard.SetTargetProperty(animation,new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"));
animation.KeyFrames.Add(new EasingDoubleKeyFrame(0,KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0))));
animation.KeyFrames.Add(new EasingDoubleKeyFrame(180,KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))));
animation.KeyFrames.Add(new EasingDoubleKeyFrame(360,KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2))));
storyboard.Children.Add(animation);
}
}
}
foreach (TextBlock tb in FindVisualChildren(this))
{
// do something with tb here
if (tb.Tag != null)
{
if (tb.Tag.ToString() == "出料控制")
{
textBlockCLKZ = tb;
}
else if (tb.Tag.ToString() == "Value")
{
textBlockValue = tb;
}
else if (tb.Tag.ToString() == "Text")
{
textBlockText = tb;
}
else if (tb.Tag.ToString() == "Title")
{
textBlockTitle = tb;
}
}
}
foreach (StackPanel tb in FindVisualChildren(this))
{
// do something with tb here
if (tb.Tag != null)
{
if (tb.Tag.ToString() == "ControlEvent")
{
foreach (var item in tb.Children)
{
if (item is Image)
{
(item as Image).MouseLeftButtonDown += Image_MouseLeftButtonDown;
if ((item as Image).Tag != null)
typeNameList.Add((item as Image).Tag?.ToString());
}
else if (item is Button)
{
(item as Button).Click += Button_Click;
if ((item as Button).Tag != null)
typeNameList.Add((item as Button).Tag?.ToString());
}
else if (item is TextBox)
{
(item as TextBox).TextChanged += TextBox_TextChanged;
if ((item as TextBox).Tag != null)
typeNameList.Add((item as TextBox).Tag?.ToString());
}
else if (item is CheckBox)
{
(item as CheckBox).Checked += CheckBox_Checked;
(item as CheckBox).Unchecked += CheckBox_Unchecked;
if ((item as CheckBox).Tag != null)
typeNameList.Add((item as CheckBox).Tag?.ToString());
}
}
@enum = EnumExtensions.CreateEnum(typeNameList,$"{this.Name}ControlEunm");
}
}
}
}
}
public static IEnumerable FindVisualChildren(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj,i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren(child))
{
yield return childOfChild;
}
}
}
}
#endregion
#region 属性
[Category("值设定")]
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty,value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value",typeof(string),typeof(Silos),new PropertyMetadata(new PropertyChangedCallback(onTargetChanged)));
private static void onTargetChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as Silos)?.TargetRefresh();
[Category("值设定")]
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty,value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text",typeof(string),typeof(Silos),new PropertyMetadata(new PropertyChangedCallback(onTargetChanged)));
[Category("值设定")]
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty,value); }
}
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title",typeof(string),typeof(Silos),new PropertyMetadata(new PropertyChangedCallback(onTargetChanged)));
[Category("值设定")]
public int Direction
{
get { return (int)GetValue(DirectionProperty); }
set { SetValue(DirectionProperty,value); }
}
public static readonly DependencyProperty DirectionProperty =
DependencyProperty.Register("Direction",typeof(int),typeof(Silos),
new PropertyMetadata(0,new PropertyChangedCallback(OnPropertyChanged)));
private static void OnPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
{
(d as Silos)?.Refresh();
}
[Category("名称[自动生成]")]
public string EventReceiveNameListStr
{
get { return (string)GetValue(EventReceiveNameListStrProperty); }
set { SetValue(EventReceiveNameListStrProperty,value); }
}
public static readonly DependencyProperty EventReceiveNameListStrProperty =
DependencyProperty.Register("EventReceiveNameListStr",typeof(string),typeof(Silos),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onEventReceiveNameListStrChanged)));
private static void onEventReceiveNameListStrChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as Silos)?.ReceiveNameRefresh();
[Category("消息接收名称集合")]
public ObservableCollection EventReceiveNameList
{
get { return (ObservableCollection)GetValue(EventReceiveNameListProperty); }
set { SetValue(EventReceiveNameListProperty,value); }
}
public static readonly DependencyProperty EventReceiveNameListProperty =
DependencyProperty.Register("EventReceiveNameList",typeof(ObservableCollection),typeof(Silos),new PropertyMetadata(new ObservableCollection(),new PropertyChangedCallback(onEventNameListChanged)));
[Category("名称[自动生成]")]
private string EventSendNameListStr
{
get { return (string)GetValue(EventSendNameListStrProperty); }
set { SetValue(EventSendNameListStrProperty,value); }
}
private static readonly DependencyProperty EventSendNameListStrProperty =
DependencyProperty.Register("EventSendNameListStr",typeof(string),typeof(Silos),new PropertyMetadata(string.Empty));
[Category("消息发送名称集合")]
private ObservableCollection EventSendNameList
{
get { return (ObservableCollection)GetValue(EventSendNameListProperty); }
set { SetValue(EventSendNameListProperty,value); }
}
private static readonly DependencyProperty EventSendNameListProperty =
DependencyProperty.Register("EventSendNameList",typeof(ObservableCollection),typeof(Silos),new PropertyMetadata(new ObservableCollection(),new PropertyChangedCallback(onEventNameListChanged)));
private static void onEventNameListChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as Silos)?.DataNameRefresh();
#endregion
#region 数据绑定模块
public event EventHandler PropertyChange; //声明一个事件
//[Category("数据绑定-数据来源")]
//public DataTypeEnum DataSouceType
//{
// get { return (DataTypeEnum)GetValue(DataSouceTypeProperty); }
// set { SetValue(DataSouceTypeProperty,value); }
//}
//public static readonly DependencyProperty DataSouceTypeProperty =
// DependencyProperty.Register("DataSouceType",typeof(DataTypeEnum),typeof(Silos),new PropertyMetadata(DataTypeEnum.静态数据));
[Category("数据绑定-数据来源")]
public int TimeCount
{
get { return (int)GetValue(TimeCountProperty); }
set { SetValue(TimeCountProperty,value); }
}
public static readonly DependencyProperty TimeCountProperty =
DependencyProperty.Register("TimeCount",typeof(int),typeof(Silos),new PropertyMetadata(5));
//[Category("数据绑定-数据来源")]
//public string DataSouceInformation
//{
// get { return (string)GetValue(DataSouceInformationProperty); }
// set { SetValue(DataSouceInformationProperty,value); }
//}
//public static readonly DependencyProperty DataSouceInformationProperty =
// DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(Silos),new PropertyMetadata(string.Empty));
//[Category("数据绑定-数据来源")]
//public string DeviceName
//{
// get { return (string)GetValue(DeviceNameProperty); }
// set { SetValue(DeviceNameProperty,value); }
//}
//public static readonly DependencyProperty DeviceNameProperty =
// DependencyProperty.Register("DeviceName",typeof(string),typeof(Silos),new PropertyMetadata(string.Empty));
//[Category("数据绑定-数据来源")]
//public string DeviceValuleName
//{
// get { return (string)GetValue(DeviceValuleNameProperty); }
// set { SetValue(DeviceValuleNameProperty,value); }
//}
//public static readonly DependencyProperty DeviceValuleNameProperty =
// DependencyProperty.Register("DeviceValuleName",typeof(string),typeof(Silos),new PropertyMetadata(string.Empty));
[Category("数据绑定")]
public string FDataSouce
{
get { return (string)GetValue(FDataSouceProperty); }
set { SetValue(FDataSouceProperty,value); }
}
public static readonly DependencyProperty FDataSouceProperty =
DependencyProperty.Register("FDataSouce",typeof(string),typeof(Silos),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onFDataSouceChanged)));
private static void onFDataSouceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as Silos)?.DataSouceRefresh();
public void DataSouceRefresh()
{
try
{
if (!string.IsNullOrEmpty(FDataSouce))
{
GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code,new object[] { FDataSouce });
if (PropertyChange != null)
{
PropertyChange(this,null);
}
}
}
catch (Exception ex)
{
}
}
public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n";
[Category("数据绑定")]
public string Code
{
get { return (string)GetValue(CodeProperty); }
set { SetValue(CodeProperty,value); }
}
public static readonly DependencyProperty CodeProperty =
DependencyProperty.Register("Code",typeof(string),typeof(Silos),new PropertyMetadata(_code));
[Category("数据绑定")]
public string GenerateData
{
get { return (string)GetValue(GenerateDataProperty); }
set { SetValue(GenerateDataProperty,value); }
}
public static readonly DependencyProperty GenerateDataProperty =
DependencyProperty.Register("GenerateData",typeof(string),typeof(Silos),new PropertyMetadata(string.Empty));
#endregion
#region 发送事件名称集合
[Category("事件处理")]
public MessageNameEnum 出料单击事件
{
get { return (MessageNameEnum)GetValue(出料单击事件Property); }
set { SetValue(出料单击事件Property,value); }
}
public static readonly DependencyProperty 出料单击事件Property =
DependencyProperty.Register("出料单击事件",typeof(MessageNameEnum),typeof(Silos),new PropertyMetadata(MessageNameEnum.Null));
[Category("事件处理")]
public MessageNameEnum 停止出料单击事件
{
get { return (MessageNameEnum)GetValue(停止出料单击事件Property); }
set { SetValue(停止出料单击事件Property,value); }
}
public static readonly DependencyProperty 停止出料单击事件Property =
DependencyProperty.Register("停止出料单击事件",typeof(MessageNameEnum),typeof(Silos),new PropertyMetadata(MessageNameEnum.Null));
#endregion
#region 函数
///
/// 目标属性刷新
///
public void TargetRefresh()
{
if (textBlockValue != null && textBlockText != null && textBlockTitle != null)
{
textBlockValue.Text = Value;
textBlockText.Text = Text;
textBlockTitle.Text = Title;
}
}
///
/// 值变化刷新
///
public void Refresh()
{
if (ellipseControl != null)
{
if (Direction == 1)
{
ellipseControl.Visibility = Visibility.Visible;
textBlockCLKZ.Visibility = Visibility.Visible;
storyboard.Begin();
}
else if (Direction == 2)
{
ellipseControl.Visibility = Visibility.Collapsed;
textBlockCLKZ.Visibility = Visibility.Collapsed;
storyboard.Stop();
}
else
{
ellipseControl.Visibility = Visibility.Collapsed;
textBlockCLKZ.Visibility = Visibility.Collapsed;
storyboard.Stop();
}
}
}
///
/// 运行名称变化事件
///
public void RunNameRefresh()
{
}
///
/// 数据名称变化事件
///
public void DataNameRefresh()
{
}
///
/// 接收数据改变
///
public void ReceiveNameRefresh()
{
if (!string.IsNullOrEmpty(EventReceiveNameListStr))
{
try
{
EventReceiveNameList = JsonConvert.DeserializeObject>(EventReceiveNameListStr);
}
catch (Exception ex)
{
}
}
}
#endregion
#region 运行事件
DispatcherTimer timer = new DispatcherTimer();
Dictionary propertyBing = new Dictionary();
List MessageNameL = null;
public void Register()
{
if (!string.IsNullOrEmpty(EventReceiveNameListStr))
{
try
{
EventReceiveNameList = JsonConvert.DeserializeObject>(EventReceiveNameListStr);
}
catch (Exception ex)
{
}
}
if (!string.IsNullOrEmpty(EventSendNameListStr))
{
try
{
EventSendNameList = JsonConvert.DeserializeObject>(EventSendNameListStr);
}
catch (Exception ex)
{
}
}
List MessageNameNew = EventReceiveNameList?.Select(o => o.MeaageName.ToString())?.Distinct()?.ToList();
if (MessageNameL == null || !MessageNameNew.SequenceEqual(MessageNameL))
{
MessageNameL?.ForEach(o => Class_InnerMessageBus.GetInstance().RemoveMessage(o,"SliosEventHandler"));
MessageNameNew?.ForEach(x => Class_InnerMessageBus.GetInstance().ListenMessage(this,x,"SliosEventHandler"));
MessageNameL = MessageNameNew;
}
PropertyInfo[] propertyInfos = this.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
var propName = propertyInfo?.GetValue(this,null);
if (propName is string && propName != null && propName.ToString().Contains("Binding ") && propName.ToString().Contains("."))
{
propertyBing[propertyInfo.Name] = propName.ToString();
}
}
timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
timer.Tick += Timer_Tick; ;
timer.Start();
}
private void Timer_Tick(object? sender,EventArgs e)
{
try
{
foreach (var item in propertyBing)
{
//{Binding 测试设备.VAR_A_2}
string[] str= item.Value.Replace("{Binding ","").Replace("}","").Split(".");
if (str.Length > 1)
{
if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0]))
{
Dictionary b= Class_DataBus.GetInstance().Dic_DeviceData[str[0]];
if (b!=null && b.ContainsKey(str[1]))
{
object _value = b[str[1]];
this.GetType().GetProperty(item.Key).SetValue(this,_value);
}
}
}
}
}
catch (Exception ex)
{
}
}
///
/// 统一事件消息处理中心
///
///
///
public void SliosEventHandler(object sender,InnerMessageEventArgs e)
{
try
{
if (IsExecuteState = true)//正在运行
{
if (e.obj_MessageObj is RunEnumModel)//接收到运行消息
{
RunEnumModel mode = (RunEnumModel)e.obj_MessageObj;
var msg = EventReceiveNameList?.ToList().Find(par => par.MeaageName.ToString() == e.str_MessageStr && par.MeaageID == mode.MessageID);
//必对消息号:
if (msg != null)
{
Direction = mode.Run == RunEnum.Run ? 1 : 2;
}
}
else if (e.obj_MessageObj is SilosMessageModel)//接收到数据模型
{
SilosMessageModel mode = (SilosMessageModel)e.obj_MessageObj;
var msg = EventReceiveNameList?.ToList().Find(par => par.MeaageName.ToString() == e.str_MessageStr && par.MeaageID == mode.MessageID);
//必对消息号:
if (msg != null)
{
Title = mode.Title;
Value = mode.Value;
Text = mode.Text;
}
}
}
}
catch (Exception ex)
{
}
}
#endregion
#region 发送消息事件
public void Dispose()
{
timer.Stop();
}
///
/// 按钮按下
///
///
///
private void Image_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)
{
try
{
Image image = sender as Image;
if (image.Tag != null)
{
if (image.Tag.ToString() == "出料" && 出料单击事件 != MessageNameEnum.Null)
{
EventSendMessage eventSend = new EventSendMessage();
eventSend.ControlName = this.Name;
eventSend.ControlTitle = this.Title;
eventSend.ControlSource = image.Tag.ToString();
eventSend.EventType = ControlEventType.MouseLeftButtonDown;
eventSend.MeaageName = 出料单击事件;
Class_InnerMessageBus.GetInstance().PostMessage(this,出料单击事件.ToString(),eventSend);
}
else if (image.Tag.ToString() == "停止出料" && 停止出料单击事件 != MessageNameEnum.Null)
{
EventSendMessage eventSend = new EventSendMessage();
eventSend.ControlName = this.Name;
eventSend.ControlTitle = this.Title;
eventSend.ControlSource = image.Tag.ToString();
eventSend.EventType = ControlEventType.MouseLeftButtonDown;
eventSend.MeaageName = 停止出料单击事件;
Class_InnerMessageBus.GetInstance().PostMessage(this,停止出料单击事件.ToString(),eventSend);
}
}
}
catch (Exception ex)
{
}
}
///
/// 按钮类型
///
///
///
private void Button_Click(object sender,RoutedEventArgs e)
{
}
///
/// 值改变
///
///
///
private void TextBox_TextChanged(object sender,TextChangedEventArgs e)
{
}
///
/// 选中
///
///
///
private void CheckBox_Checked(object sender,RoutedEventArgs e)
{
}
///
/// 取消选中
///
///
///
private void CheckBox_Unchecked(object sender,RoutedEventArgs e)
{
}
#endregion
}
}