pry 2 years ago
parent
commit
5149aae2b2
9 changed files with 243 additions and 39 deletions
  1. +1
    -0
      BPASmartClient.MessageName/BPASmartClient.MessageName.csproj
  2. +80
    -1
      BPASmartClient.MessageName/EnumHelp/EnumExtensions.cs
  3. +6
    -5
      BPASmartClient.MessageName/发送消息Model/EventSendMessage.cs
  4. +25
    -5
      BPASmartClient.SCADAControl/NewConveyorBelt.xaml.cs
  5. +4
    -6
      BPASmartClient.SCADAControl/Silos.xaml
  6. +94
    -22
      BPASmartClient.SCADAControl/Silos.xaml.cs
  7. +3
    -0
      SCADA.Test/MainWindow.xaml
  8. +26
    -0
      SCADA.Test/MainWindow.xaml.cs
  9. +4
    -0
      SCADA.Test/SCADA.Test.csproj

+ 1
- 0
BPASmartClient.MessageName/BPASmartClient.MessageName.csproj View File

@@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="System.Reflection" Version="4.3.0" />
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
</ItemGroup>

</Project>

+ 80
- 1
BPASmartClient.MessageName/EnumHelp/EnumExtensions.cs View File

@@ -20,7 +20,7 @@ namespace BPASmartClient.MessageName.EnumHelp
/// 根据控件-》创建枚举类型
/// </summary>
/// <returns></returns>
public static Type CreatEnumType(UIElementCollection list)
public static object CreatEnumType(UIElementCollection list)
{
var currentDomain = AppDomain.CurrentDomain;
var assembly = Assembly.GetExecutingAssembly();
@@ -47,6 +47,75 @@ namespace BPASmartClient.MessageName.EnumHelp
}


/// <summary>
/// 动态创建枚举
/// </summary>
/// <param name="enumDictionary">枚举元素列表</param>
/// <param name="enumName">枚举名</param>
/// <returns>Enum枚举</returns>
public static Enum CreateEnum(Dictionary<string,int> enumDictionary,string enumName = "DefalutEnum")
{
if (enumDictionary == null || enumDictionary.Count <= 0)
return null;

AppDomain currentDomain = AppDomain.CurrentDomain;
AssemblyName aName = new AssemblyName("TempAssembly");
AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(aName,AssemblyBuilderAccess.Run);
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
if (string.IsNullOrEmpty(enumName))
{
enumName = "DefalutEnum";
}
EnumBuilder eb = mb.DefineEnum(enumName,TypeAttributes.Public,typeof(int));

foreach (var item in enumDictionary)
{
eb.DefineLiteral(item.Key,item.Value);
}

Type finished = eb.CreateType();
Enum eEnum = Activator.CreateInstance(finished) as Enum;
//foreach (object item in Enum.GetValues(eEnum.GetType()))
//{
// Debug.LogError(string.Format("{0}.{1} = {2}", finished, item, ((int)item)));
//}
return eEnum;
}

/// <summary>
/// 动态创建枚举
/// </summary>
/// <param name="enumDictionary">枚举元素列表</param>
/// <param name="enumName">枚举名</param>
/// <returns>Enum枚举</returns>
public static Enum CreateEnum(List<string> enumList,string enumName = "DefalutEnum")
{
if (enumList == null || enumList.Count <= 0)
return null;

AppDomain currentDomain = AppDomain.CurrentDomain;
AssemblyName aName = new AssemblyName("TempAssembly");
AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(aName,AssemblyBuilderAccess.Run);
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
if (string.IsNullOrEmpty(enumName))
{
enumName = "DefalutEnum";
}
EnumBuilder eb = mb.DefineEnum(enumName,TypeAttributes.Public,typeof(int));

for (int i = 0; i < enumList.Count; i++)
{
eb.DefineLiteral(enumList[i],i);
}
Type finished = eb.CreateType();
Enum eEnum = Activator.CreateInstance(finished) as Enum;
//foreach (object item in Enum.GetValues(eEnum.GetType()))
//{
// Debug.LogError(string.Format("{0}.{1} = {2}", finished, item, ((int)item)));
//}
return eEnum;
}

/// <summary>
/// 根据枚举int值获取枚举名称
/// </summary>
@@ -58,6 +127,14 @@ namespace BPASmartClient.MessageName.EnumHelp
return Enum.GetName(typeof(T), status);
}

/// <summary>
/// 根据枚举名称获取枚举值
/// </summary>
public static T ToEnumValue<T>(string name)
{
return (T)Enum.Parse(typeof(T),name);
}


/// <summary>
/// 获取枚举变量值的 Description 属性
@@ -101,5 +178,7 @@ namespace BPASmartClient.MessageName.EnumHelp
}
return obj.ToString();
}


}
}

+ 6
- 5
BPASmartClient.MessageName/发送消息Model/EventSendMessage.cs View File

@@ -11,10 +11,6 @@ namespace BPASmartClient.MessageName.发送消息Model
/// </summary>
public class EventSendMessage: MessageBase
{
/// <summary>
/// 控件类型
/// </summary>
public ControlEventType EventType { get; set; }
/// <summary>
/// 控件名称
/// </summary>
@@ -26,11 +22,16 @@ namespace BPASmartClient.MessageName.发送消息Model
/// <summary>
/// 控件触发源
/// </summary>
public string ControlSource { get; set; }
public object ControlSource { get; set; }
/// <summary>
/// 控件状态
/// </summary>
public string ControlStatus { get; set; }
/// <summary>
/// 控件类型
/// </summary>
public ControlEventType EventType { get; set; }
}
/// <summary>
/// 控件类型


+ 25
- 5
BPASmartClient.SCADAControl/NewConveyorBelt.xaml.cs View File

@@ -337,7 +337,9 @@ namespace BPASmartClient.SCADAControl
set { SetValue(EventReceiveNameListStrProperty, value); }
}
public static readonly DependencyProperty EventReceiveNameListStrProperty =
DependencyProperty.Register("EventReceiveNameListStr", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(string.Empty));
DependencyProperty.Register("EventReceiveNameListStr",typeof(string),typeof(NewConveyorBelt),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onEventReceiveNameListStrChanged)));
private static void onEventReceiveNameListStrChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as NewConveyorBelt)?.ReceiveNameRefresh();

[Category("消息接收名称集合")]
public ObservableCollection<EventReceiveMessage> EventReceiveNameList
{
@@ -348,20 +350,20 @@ namespace BPASmartClient.SCADAControl
DependencyProperty.Register("EventReceiveNameList", typeof(ObservableCollection<EventReceiveMessage>), typeof(NewConveyorBelt), new PropertyMetadata(new ObservableCollection<EventReceiveMessage>(), onEventNameListChanged));
private static void onEventNameListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as NewConveyorBelt)?.RunNameRefresh();
[Category("名称[自动生成]")]
public string EventSendNameListStr
private string EventSendNameListStr
{
get { return (string)GetValue(EventSendNameListStrProperty); }
set { SetValue(EventSendNameListStrProperty,value); }
}
public static readonly DependencyProperty EventSendNameListStrProperty =
private static readonly DependencyProperty EventSendNameListStrProperty =
DependencyProperty.Register("EventSendNameListStr",typeof(string),typeof(NewConveyorBelt),new PropertyMetadata(string.Empty));
[Category("消息发送名称集合")]
public ObservableCollection<EventSendMessage> EventSendNameList
private ObservableCollection<EventSendMessage> EventSendNameList
{
get { return (ObservableCollection<EventSendMessage>)GetValue(EventSendNameListProperty); }
set { SetValue(EventSendNameListProperty,value); }
}
public static readonly DependencyProperty EventSendNameListProperty =
private static readonly DependencyProperty EventSendNameListProperty =
DependencyProperty.Register("EventSendNameList",typeof(ObservableCollection<EventSendMessage>),typeof(NewConveyorBelt),new PropertyMetadata(new ObservableCollection<EventSendMessage>(),new PropertyChangedCallback(onEventNameListChanged)));

#endregion
@@ -469,6 +471,24 @@ namespace BPASmartClient.SCADAControl
{
}

/// <summary>
/// 接收数据改变
/// </summary>
public void ReceiveNameRefresh()
{
if (!string.IsNullOrEmpty(EventReceiveNameListStr))
{
try
{
EventReceiveNameList = JsonConvert.DeserializeObject<ObservableCollection<EventReceiveMessage>>(EventReceiveNameListStr);
}
catch (Exception ex)
{

}
}
}
#endregion
}



+ 4
- 6
BPASmartClient.SCADAControl/Silos.xaml View File

@@ -71,12 +71,10 @@
Grid.RowSpan="2"
Source="/BPASmartClient.SCADAControl;component/Images/光柱.png"
Stretch="Fill" />
<StackPanel Tag="ControlEvent" HorizontalAlignment="Left" Orientation="Horizontal" Grid.Row="1" VerticalAlignment="Bottom" >
<Image Margin="10,10,0,0" Tag="出料" Source="/BPASmartClient.SCADAControl;component/Images/借出.png" Cursor="Hand" ToolTip="出料" Width="24" ></Image>
<Image Margin="10,10,0,0" Tag="停止出料" Source="/BPASmartClient.SCADAControl;component/Images/退出.png" Cursor="Hand" Width="24" ToolTip="停止出料"></Image>
<Button Margin="10,10,0,0" Tag="出料">出料</Button>
<TextBox Width="50" Tag="文本改变" TextChanged="TextBox_TextChanged" ></TextBox>
<CheckBox Checked="CheckBox_Checked" Tag="是否选中" Unchecked="CheckBox_Unchecked"></CheckBox>
<StackPanel Tag="ControlEvent" HorizontalAlignment="Right" Orientation="Vertical" Grid.Row="1" VerticalAlignment="Bottom" >
<Image Margin="20,10,0,0" Tag="出料" Source="/BPASmartClient.SCADAControl;component/Images/借出.png" Cursor="Hand" ToolTip="出料" Width="24" ></Image>
<Image Margin="20,10,0,10" Tag="停止出料" Source="/BPASmartClient.SCADAControl;component/Images/退出.png" Cursor="Hand" Width="24" ToolTip="停止出料"></Image>
</StackPanel>
</Grid>
</UserControl>

+ 94
- 22
BPASmartClient.SCADAControl/Silos.xaml.cs View File

@@ -46,6 +46,8 @@ namespace BPASmartClient.SCADAControl
Storyboard storyboard = new Storyboard();

object typeName;
public List<string> typeNameList = new List<string>();
public Enum @enum = null;
#endregion

public Silos()
@@ -62,7 +64,6 @@ namespace BPASmartClient.SCADAControl
{
EventReceiveNameList.CollectionChanged += EventNameList_CollectionChanged;
EventSendNameList.CollectionChanged += EventSendNameList_CollectionChanged;
//Loading();
}

private void EventSendNameList_CollectionChanged(object? sender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
@@ -176,26 +177,38 @@ namespace BPASmartClient.SCADAControl
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<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
@@ -248,6 +261,20 @@ namespace BPASmartClient.SCADAControl
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
{
@@ -255,7 +282,9 @@ namespace BPASmartClient.SCADAControl
set { SetValue(EventReceiveNameListStrProperty,value); }
}
public static readonly DependencyProperty EventReceiveNameListStrProperty =
DependencyProperty.Register("EventReceiveNameListStr",typeof(string),typeof(Silos),new PropertyMetadata(string.Empty));
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<EventReceiveMessage> EventReceiveNameList
{
@@ -265,37 +294,41 @@ namespace BPASmartClient.SCADAControl
public static readonly DependencyProperty EventReceiveNameListProperty =
DependencyProperty.Register("EventReceiveNameList",typeof(ObservableCollection<EventReceiveMessage>),typeof(Silos),new PropertyMetadata(new ObservableCollection<EventReceiveMessage>(),new PropertyChangedCallback(onEventNameListChanged)));
[Category("名称[自动生成]")]
public string EventSendNameListStr
private string EventSendNameListStr
{
get { return (string)GetValue(EventSendNameListStrProperty); }
set { SetValue(EventSendNameListStrProperty,value); }
}
public static readonly DependencyProperty EventSendNameListStrProperty =
private static readonly DependencyProperty EventSendNameListStrProperty =
DependencyProperty.Register("EventSendNameListStr",typeof(string),typeof(Silos),new PropertyMetadata(string.Empty));
[Category("消息发送名称集合")]
public ObservableCollection<EventSendMessage> EventSendNameList
private ObservableCollection<EventSendMessage> EventSendNameList
{
get { return (ObservableCollection<EventSendMessage>)GetValue(EventSendNameListProperty); }
set { SetValue(EventSendNameListProperty,value); }
}
public static readonly DependencyProperty EventSendNameListProperty =
private static readonly DependencyProperty EventSendNameListProperty =
DependencyProperty.Register("EventSendNameList",typeof(ObservableCollection<EventSendMessage>),typeof(Silos),new PropertyMetadata(new ObservableCollection<EventSendMessage>(),new PropertyChangedCallback(onEventNameListChanged)));
private static void onEventNameListChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as Silos)?.DataNameRefresh();
#endregion

[Category("值设定")]
public int Direction
#region 发送事件名称集合
[Category("事件处理")]
public MessageNameEnum 出料单击事件
{
get { return (int)GetValue(DirectionProperty); }
set { SetValue(DirectionProperty,value); }
get { return (MessageNameEnum)GetValue(出料单击事件Property); }
set { SetValue(出料单击事件Property,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)
public static readonly DependencyProperty 出料单击事件Property =
DependencyProperty.Register("出料单击事件",typeof(MessageNameEnum),typeof(Silos),new PropertyMetadata(MessageNameEnum.Null));
[Category("事件处理")]
public MessageNameEnum 停止出料单击事件
{
(d as Silos)?.Refresh();
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 函数
@@ -356,6 +389,24 @@ namespace BPASmartClient.SCADAControl
{

}

/// <summary>
/// 接收数据改变
/// </summary>
public void ReceiveNameRefresh()
{
if (!string.IsNullOrEmpty(EventReceiveNameListStr))
{
try
{
EventReceiveNameList = JsonConvert.DeserializeObject<ObservableCollection<EventReceiveMessage>>(EventReceiveNameListStr);
}
catch (Exception ex)
{

}
}
}
#endregion

#region 运行事件
@@ -384,14 +435,15 @@ namespace BPASmartClient.SCADAControl

}
}

List<string> MessageNameNew = EventReceiveNameList?.Select(o => o.MeaageName.ToString())?.Distinct()?.ToList();
if (MessageNameL==null || !MessageNameNew.SequenceEqual(MessageNameL))
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;
}
}
/// <summary>
/// 统一事件消息处理中心
@@ -446,7 +498,29 @@ namespace BPASmartClient.SCADAControl
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)
{
@@ -495,7 +569,5 @@ namespace BPASmartClient.SCADAControl

}
#endregion


}
}

+ 3
- 0
SCADA.Test/MainWindow.xaml View File

@@ -16,11 +16,14 @@
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<Button Click="Button_Click">加载文件</Button>

<local:RunCanvas Grid.Row="1" x:Name="runCanvas"/>

<TextBox x:Name="LogShow" Grid.Row="2" TextWrapping="Wrap" Foreground="#FFF100C7" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" FontFamily="宋体" Background="#FF282B29" FontSize="10"></TextBox>
<Grid Grid.Column="1" Grid.RowSpan="2">

<Border BorderBrush="AntiqueWhite" BorderThickness="3">


+ 26
- 0
SCADA.Test/MainWindow.xaml.cs View File

@@ -1,9 +1,12 @@
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 Microsoft.Win32;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
@@ -43,9 +46,32 @@ namespace SCADA.Test
xxnc.ItemsSource = System.Enum.GetNames(typeof(MessageNameEnum))?.ToList();
mlname.ItemsSource = System.Enum.GetNames(typeof(MessageNameEnum))?.ToList();
xx.ItemsSource = System.Enum.GetNames(typeof(RunEnum))?.ToList();

Class_InnerMessageBus.GetInstance().ListenMessage(this,MessageNameEnum.SendMessageName.ToString(),"MainWindowHandler");
}

public void MainWindowHandler(object sender,InnerMessageEventArgs e)
{
try
{
if (e.obj_MessageObj is EventSendMessage)//接收到运行消息
{
EventSendMessage mode = (EventSendMessage)e.obj_MessageObj;
LogShowText(JsonConvert.SerializeObject(mode));
}
}
catch (Exception ex)
{

}
}

#region 加载数据
public void LogShowText(string str)
{
LogShow.Text += str + "\n";
}

/// <summary>
/// 加载数据
/// </summary>


+ 4
- 0
SCADA.Test/SCADA.Test.csproj View File

@@ -7,6 +7,10 @@
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.Compiler\BPASmartClient.Compiler.csproj" />
<ProjectReference Include="..\BPASmartClient.MessageCommunication\BPASmartClient.MessageCommunication.csproj" />


Loading…
Cancel
Save