This commit is contained in:
tfarcraw 2026-08-04 13:57:49 +08:00
parent d98e2480d5
commit 6cab601d25
7 changed files with 991 additions and 0 deletions

View File

@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
using PLCSharp.Core.Common; using PLCSharp.Core.Common;
using PLCSharp.VVMs.Authority; using PLCSharp.VVMs.Authority;
using PLCSharp.VVMs.Connects; using PLCSharp.VVMs.Connects;
using PLCSharp.VVMs.Cylinders;
using PLCSharp.VVMs.GlobalVariables; using PLCSharp.VVMs.GlobalVariables;
using PLCSharp.VVMs.Homepage; using PLCSharp.VVMs.Homepage;
using PLCSharp.VVMs.MotionController; using PLCSharp.VVMs.MotionController;
@ -128,6 +129,11 @@ namespace PLCSharp.Models
/// </summary> /// </summary>
public DbSet<RobotMatrix> RobotMatrices { get; set; } public DbSet<RobotMatrix> RobotMatrices { get; set; }
/// <summary>
/// 气缸
/// </summary>
public DbSet<Cylinder> Cylinders { get; set; }
/// <summary> /// <summary>
/// 错误Logs /// 错误Logs
/// </summary> /// </summary>

View File

@ -1,6 +1,7 @@
using PLCSharp.Core.Prism; using PLCSharp.Core.Prism;
using PLCSharp.Core.Tools; using PLCSharp.Core.Tools;
using PLCSharp.VVMs.Connects; using PLCSharp.VVMs.Connects;
using PLCSharp.VVMs.Cylinders;
using PLCSharp.VVMs.GlobalVariables; using PLCSharp.VVMs.GlobalVariables;
using PLCSharp.VVMs.ModeState; using PLCSharp.VVMs.ModeState;
using PLCSharp.VVMs.MotionController; using PLCSharp.VVMs.MotionController;
@ -85,6 +86,9 @@ namespace PLCSharp.Models
RobotModel = container.Resolve<RobotModel>(); RobotModel = container.Resolve<RobotModel>();
VisionsModel.Init(this); VisionsModel.Init(this);
RobotModel.Init(this); RobotModel.Init(this);
CylindersModel = container.Resolve<CylinderModel>();
CylindersModel.Init(this);
bkgWorker = new BackgroundWorker { WorkerSupportsCancellation = true }; bkgWorker = new BackgroundWorker { WorkerSupportsCancellation = true };
bkgWorker.DoWork += BackgroundWork; bkgWorker.DoWork += BackgroundWork;
bkgWorker.RunWorkerAsync(); bkgWorker.RunWorkerAsync();
@ -164,6 +168,11 @@ namespace PLCSharp.Models
/// </summary> /// </summary>
public RobotModel RobotModel { get; set; } public RobotModel RobotModel { get; set; }
/// <summary>
/// 气缸模型
/// </summary>
public CylinderModel CylindersModel { get; set; }
/// <summary> /// <summary>
/// 全局变量 /// 全局变量
/// </summary> /// </summary>

View File

@ -0,0 +1,388 @@
using PLCSharp.Core.Common;
using PLCSharp.VVMs.MotionController;
using Prism.Mvvm;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace PLCSharp.VVMs.Cylinders
{
/// <summary>
/// 气缸感应类型
/// </summary>
public enum SensorType
{
None,
OnlyA,
OnlyB,
Two,
}
/// <summary>
/// 气缸
/// </summary>
public class Cylinder : BindableBase
{
/// <summary>
/// 唯一标识
/// </summary>
[Key]
public Guid ID { get; set; } = Guid.NewGuid();
/// <summary>
/// 关联的控制器ID
/// </summary>
public Guid ControllerID { get; set; }
private string _Name;
/// <summary>
/// 气缸名称
/// </summary>
public string Name
{
get { return _Name; }
set
{
if (_Name != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _Name, value);
}
}
private SensorType _SensorType;
/// <summary>
/// 感应类型
/// </summary>
public SensorType SensorType
{
get { return _SensorType; }
set
{
if (_SensorType != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _SensorType, value);
}
}
private ushort _IOController;
/// <summary>
///
/// </summary>
public ushort IOController
{
get { return _IOController; }
set { SetProperty(ref _IOController, value); }
}
private string _InA;
/// <summary>
/// A位感应输入点
/// </summary>
public string InA
{
get { return _InA; }
set
{
if (_InA != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _InA, value);
}
}
private string _InB;
/// <summary>
/// B位感应输入点
/// </summary>
public string InB
{
get { return _InB; }
set
{
if (_InB != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _InB, value);
}
}
private string _OutA;
/// <summary>
/// A位输出点(DQ 名称或序号)
/// </summary>
public string OutA
{
get { return _OutA; }
set
{
if (_OutA != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _OutA, value);
}
}
private string _OutB;
/// <summary>
/// B位输出点(DQ 名称或序号)
/// </summary>
public string OutB
{
get { return _OutB; }
set
{
if (_OutB != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _OutB, value);
}
}
private bool _OffWhenStopped;
/// <summary>
/// 有停止命令时关闭输出
/// </summary>
public bool OffWhenStopped
{
get { return _OffWhenStopped; }
set
{
if (_OffWhenStopped != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _OffWhenStopped, value);
}
}
private bool _OffWhenInPosition;
/// <summary>
/// 到位后关闭输出
/// </summary>
public bool OffWhenInPosition
{
get { return _OffWhenInPosition; }
set
{
if (_OffWhenInPosition != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _OffWhenInPosition, value);
}
}
private bool _Enabled = true;
/// <summary>
/// 启用
/// </summary>
public bool Enabled
{
get { return _Enabled; }
set { SetProperty(ref _Enabled, value); }
}
private bool _NoInterlock;
/// <summary>
/// 不需要互锁
/// </summary>
public bool NoInterlock
{
get { return _NoInterlock; }
set { SetProperty(ref _NoInterlock, value); }
}
private double _ATimeout;
/// <summary>
///
/// </summary>
public double ATimeout
{
get { return _ATimeout; }
set { SetProperty(ref _ATimeout, value); }
}
private double _BTimeout;
/// <summary>
///
/// </summary>
public double BTimeout
{
get { return _BTimeout; }
set { SetProperty(ref _BTimeout, value); }
}
private double _ADelay;
/// <summary>
/// 感应到位延时
/// </summary>
public double ADelay
{
get { return _ADelay; }
set { SetProperty(ref _ADelay, value); }
}
private double _BDelay;
/// <summary>
/// 感应到位延时
/// </summary>
public double BDelay
{
get { return _BDelay; }
set { SetProperty(ref _BDelay, value); }
}
private string _Comment;
/// <summary>
/// 备注
/// </summary>
public string Comment
{
get { return _Comment; }
set
{
if (_Comment != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _Comment, value);
}
}
private string _Prompt;
#region NotMapped Properties
private string _Cmd;
/// <summary>
/// 指令 "A"/"B"/"Stop"
/// </summary>
[NotMapped]
public string Cmd
{
get { return _Cmd; }
set { SetProperty(ref _Cmd, value); }
}
/// <summary>
/// 动作流程
/// </summary>
[NotMapped]
public FlowModel Flow { get; set; } = new();
/// <summary>
/// 提示(已修改,请保存)
/// </summary>
[NotMapped]
public string Prompt
{
get { return _Prompt; }
set { SetProperty(ref _Prompt, value); }
}
private bool _StatusAIn;
/// <summary>
/// A位感应状态
/// </summary>
[NotMapped]
public bool StatusAIn
{
get { return _StatusAIn; }
set { SetProperty(ref _StatusAIn, value); }
}
private bool _StatusBIn;
/// <summary>
/// B位感应状态
/// </summary>
[NotMapped]
public bool StatusBIn
{
get { return _StatusBIn; }
set { SetProperty(ref _StatusBIn, value); }
}
private bool _StatusAOut;
/// <summary>
/// A位输出状态
/// </summary>
[NotMapped]
public bool StatusAOut
{
get { return _StatusAOut; }
set { SetProperty(ref _StatusAOut, value); }
}
private bool _StatusBOut;
/// <summary>
/// A位输出状态
/// </summary>
[NotMapped]
public bool StatusBOut
{
get { return _StatusBOut; }
set { SetProperty(ref _StatusAOut, value); }
}
private string _FaultCode;
/// <summary>
/// 故障码
/// </summary>
[NotMapped]
public string FaultCode
{
get { return _FaultCode; }
set { SetProperty(ref _FaultCode, value); }
}
[NotMapped]
public bool AComplit { get; set; }
[NotMapped]
public bool BComplit { get; set; }
[NotMapped]
public DI DIA { get; set; }
[NotMapped]
public DI DIB { get; set; }
[NotMapped]
public DQ DQA { get; set; }
[NotMapped]
public DQ DQB { get; set; }
[NotMapped]
public DateTime ADelayTimer { get; set; } = DateTime.Now ;
[NotMapped]
public DateTime BDelayTimer { get; set; } = DateTime.Now;
#endregion
}
}

View File

@ -0,0 +1,188 @@
using PLCSharp.Core.Prism;
using PLCSharp.Models;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
namespace PLCSharp.VVMs.Cylinders
{
/// <summary>
/// 气缸模型
/// </summary>
[Model]
public class CylinderModel : ModelBase
{
/// <summary>
/// 气缸模型
/// </summary>
public CylinderModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService)
{
bkgWorker = new BackgroundWorker { WorkerSupportsCancellation = true };
bkgWorker.DoWork += BackgroundWork;
if (!bkgWorker.IsBusy)
bkgWorker.RunWorkerAsync();
}
private readonly BackgroundWorker bkgWorker;
private void BackgroundWork(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker)sender;
while (!worker.CancellationPending)
{
Thread.Sleep(50);
try
{
// 快照遍历,避免与 UI 线程增删气缸产生集合并发问题
var list = Cylinders.ToList();
for (int i = 0; i < list.Count; i++)
{
if (list[i] == null
|| list[i].Enabled == false)//未启的气缸不进行处理
continue;
var controller = GlobalModel.ControllersModel.Controllers.FirstOrDefault(c => c.ControllerNo == list[i].IOController);
if (controller == null) continue;
// 更新 DI DQ
if (list[i].SensorType == SensorType.Two || list[i].SensorType == SensorType.OnlyA)
{
if (string.IsNullOrEmpty(list[i].InA) == false)
{
if (list[i].DIA == null || list[i].DIA.Name != list[i].InA)
list[i].DIA = controller.DI.FirstOrDefault(d => d.Name == list[i].InA);
}
}
if (string.IsNullOrEmpty(list[i].OutA) == false)
{
if (list[i].DQA == null || list[i].DQA.Name != list[i].OutA)
list[i].DQA = controller.DQ.FirstOrDefault(d => d.Name == list[i].OutA);
}
if (list[i].SensorType == SensorType.Two || list[i].SensorType == SensorType.OnlyB)
{
if (string.IsNullOrEmpty(list[i].InB) == false)
{
if (list[i].DIB == null || list[i].DIB.Name != list[i].InB)
list[i].DIB = controller.DI.FirstOrDefault(d => d.Name == list[i].InB);
}
}
if (string.IsNullOrEmpty(list[i].OutB) == false)
{
if (list[i].DQB == null || list[i].DQB.Name != list[i].OutB)
list[i].DQB = controller.DQ.FirstOrDefault(d => d.Name == list[i].OutB);
}
//气缸功能块
FB(list[i]);
}
}
catch (Exception ex)
{
SendErr(ex.Message);
}
}
}
private void FB(Cylinder cylinder)
{
//如果气缸没有传感器 则判断气缸输出延时是否完成
if (cylinder.SensorType == SensorType.None || cylinder.SensorType == SensorType.OnlyB)
{
if (cylinder.DQA != null && cylinder.DQA.Status)
{
cylinder.AComplit = cylinder.ADelayTimer.AddSeconds(cylinder.ADelay) < DateTime.Now;
}
else
{
cylinder.AComplit = false;
cylinder.ADelayTimer = DateTime.Now;
}
}
if (cylinder.SensorType == SensorType.None || cylinder.SensorType == SensorType.OnlyA)
{
if (cylinder.DQB != null && cylinder.DQB.Status)
{
cylinder.BComplit = cylinder.BDelayTimer.AddSeconds(cylinder.BDelay) < DateTime.Now;
}
else
{
cylinder.BComplit = false;
cylinder.BDelayTimer = DateTime.Now;
}
}
//如果气缸有传感器 则判断传感器倒位延时是否完成
if (cylinder.SensorType == SensorType.Two || cylinder.SensorType == SensorType.OnlyA)
{
if (cylinder.DIA != null && cylinder.DIA.Status)
{
cylinder.AComplit = cylinder.ADelayTimer.AddSeconds(cylinder.ADelay) < DateTime.Now;
}
else
{
cylinder.AComplit = false;
cylinder.ADelayTimer = DateTime.Now;
}
}
if (cylinder.SensorType == SensorType.Two || cylinder.SensorType == SensorType.OnlyB)
{
if (cylinder.DIB != null && cylinder.DIB.Status)
{
cylinder.BComplit = cylinder.BDelayTimer.AddSeconds(cylinder.BDelay) < DateTime.Now;
}
else
{
cylinder.BComplit = false;
cylinder.BDelayTimer = DateTime.Now;
}
}
}
/// <summary>
/// 全局模型
/// </summary>
public GlobalModel GlobalModel { get; set; }
private ObservableCollection<Cylinder> _Cylinders = [];
/// <summary>
/// 气缸列表
/// </summary>
public ObservableCollection<Cylinder> Cylinders
{
get { return _Cylinders; }
set { SetProperty(ref _Cylinders, value); }
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="globalModel">全局模型</param>
public void Init(GlobalModel globalModel)
{
GlobalModel = globalModel;
foreach (var item in _DatasContext.Cylinders)
{
Cylinders.Add(item);
}
}
}
}

View File

@ -0,0 +1,147 @@
<UserControl x:Class="PLCSharp.VVMs.Cylinders.Cylinders"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cyl="clr-namespace:PLCSharp.VVMs.Cylinders"
xmlns:local="clr-namespace:PLCSharp.VVMs.Cylinders"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d" d:Background="White"
d:DataContext="{d:DesignInstance Type=local:CylindersViewModel}"
d:DesignHeight="800" d:DesignWidth="1000">
<UserControl.Resources>
<ObjectDataProvider x:Key="SensorType" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="cyl:SensorType" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="3*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<!-- 气缸列表 -->
<GroupBox Header="气缸">
<Grid Margin="3">
<DataGrid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualHeight}"
HorizontalAlignment="Center" VerticalAlignment="Center"
AutoGenerateColumns="False" CanUserAddRows="False"
CanUserDeleteRows="False" CanUserSortColumns="False"
ItemsSource="{Binding CylinderModel.Cylinders}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
SelectedItem="{Binding SelectedCylinder}"
Unloaded="DataGrid_Unloaded">
<DataGrid.ContextMenu>
<ContextMenu IsEnabled="True" StaysOpen="False">
<MenuItem Command="{Binding CylinderManage}" CommandParameter="New" Header="新增" />
<MenuItem Command="{Binding CylinderManage}" CommandParameter="Remove" Header="删除" />
<MenuItem Command="{Binding CylinderManage}" CommandParameter="Save" Header="保存" />
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>
<DataGridCheckBoxColumn MinWidth="60" Binding="{Binding Enabled, UpdateSourceTrigger=PropertyChanged}" Header="启用" />
<DataGridTextColumn MinWidth="80" Binding="{Binding Name, UpdateSourceTrigger=LostFocus}" Header="名称" />
<DataGridComboBoxColumn MinWidth="100"
Header="控制器"
ItemsSource="{Binding DataContext.Controllers, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
DisplayMemberPath="Name" SelectedValuePath="ID"
SelectedValueBinding="{Binding ControllerID, UpdateSourceTrigger=PropertyChanged}" />
<DataGridComboBoxColumn MinWidth="80" Header="感应类型" ItemsSource="{Binding Source={StaticResource SensorType}}" SelectedValueBinding="{Binding SensorType, UpdateSourceTrigger=PropertyChanged}" />
<DataGridCheckBoxColumn MinWidth="60" Binding="{Binding NoInterlock, UpdateSourceTrigger=PropertyChanged}" Header="不需要互锁" />
<DataGridCheckBoxColumn MinWidth="60" Binding="{Binding OffWhenInPosition, UpdateSourceTrigger=PropertyChanged}" Header="到位后关输出" />
<DataGridCheckBoxColumn MinWidth="60" Binding="{Binding OffWhenStopped, UpdateSourceTrigger=PropertyChanged}" Header="停止时关输出" />
<DataGridTextColumn MinWidth="120" Binding="{Binding Comment, UpdateSourceTrigger=LostFocus}" Header="备注" />
<DataGridTextColumn MinWidth="100" Binding="{Binding Prompt}" Header="提示" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</GroupBox>
<!-- 气缸操作区 -->
<GroupBox Grid.Row="1" Header="操作">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Margin="8,3" Padding="10,8"
Text="A位感应" FontSize="14" TextAlignment="Center" FontWeight="Bold"
VerticalAlignment="Center"
Background="{Binding SelectedCylinder.StatusAIn, Converter={StaticResource BoolToValueConverter}, ConverterParameter=Lime, FallbackValue=Gray}" />
<TextBlock Grid.Column="1"
Margin="8,3" Padding="10,8"
Text="B位感应" FontSize="14" TextAlignment="Center" FontWeight="Bold"
VerticalAlignment="Center"
Background="{Binding SelectedCylinder.StatusBIn, Converter={StaticResource BoolToValueConverter}, ConverterParameter=Lime, FallbackValue=Gray}" />
<Button Grid.Row="1" Grid.Column="0"
Width="80" Height="35" Margin="3"
FontSize="14" FontWeight="Bold"
Content="A 位"
Background="{Binding SelectedCylinder.StatusAOut, Converter={StaticResource BoolToValueConverter}, ConverterParameter=Lime, FallbackValue=Gray}"
Command="{Binding CylinderAction}"
CommandParameter="A" />
<Button Grid.Row="1" Grid.Column="1"
Width="80" Height="35" Margin="3"
FontSize="14" FontWeight="Bold"
Content="B 位"
Command="{Binding CylinderAction}"
Background="{Binding SelectedCylinder.StatusAOut, Converter={StaticResource BoolToValueConverter}, ConverterParameter=Lime, FallbackValue=Gray}"
CommandParameter="B" />
</Grid>
<Grid Grid.Column="1">
<UniformGrid Columns="2">
<TextBlock Text="控制器" />
<ComboBox IsEditable="False"
ItemsSource="{Binding Controllers}"
SelectedValue="{Binding SelectedCylinder.IOController}"
SelectedValuePath="ControllerNo" DisplayMemberPath="ControllerNo" />
<TextBlock Text="A位感应" />
<ComboBox IsEditable="False"
ItemsSource="{Binding DI}"
SelectedValue="{Binding SelectedCylinder.InA}"
SelectedValuePath="Name" DisplayMemberPath="Name" />
<TextBlock Text="B位感应" />
<ComboBox IsEditable="False"
ItemsSource="{Binding DI}"
SelectedValue="{Binding SelectedCylinder.InB}"
SelectedValuePath="Name" DisplayMemberPath="Name" />
<TextBlock Text="A位输出" />
<ComboBox IsEditable="False"
ItemsSource="{Binding DI}"
SelectedValue="{Binding SelectedCylinder.OutA}"
SelectedValuePath="Name" DisplayMemberPath="Name" />
<TextBlock Text="B位输出" />
<ComboBox IsEditable="False"
ItemsSource="{Binding DI}"
SelectedValue="{Binding SelectedCylinder.OutB}"
SelectedValuePath="Name" DisplayMemberPath="Name" />
</UniformGrid>
</Grid>
</Grid>
</GroupBox>
</Grid>
</UserControl>

View File

@ -0,0 +1,29 @@
using PLCSharp.Core.Prism;
using System.Windows;
using System.Windows.Controls;
namespace PLCSharp.VVMs.Cylinders
{
/// <summary>
/// 气缸页面
/// </summary>
[NavigationPage(ViewName = "Cylinders", IconKind = "\ue616",
DisplayName = "气缸 ",
UserLevel = Authority.Authority.SeniorMaintainer, Index = 9)]
public partial class Cylinders : UserControl
{
/// <summary>
/// 气缸页面
/// </summary>
public Cylinders()
{
InitializeComponent();
}
private void DataGrid_Unloaded(object sender, RoutedEventArgs e)
{
var dataGrid = sender as DataGrid;
dataGrid.CommitEdit(DataGridEditingUnit.Row, true);
}
}
}

View File

@ -0,0 +1,224 @@
using PLCSharp.Core.Prism;
using PLCSharp.VVMs.MotionController;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations.Schema;
namespace PLCSharp.VVMs.Cylinders
{
/// <summary>
/// 气缸视图模型
/// </summary>
public class CylindersViewModel : DialogAwareBase
{
/// <summary>
/// 气缸视图模型
/// </summary>
public CylindersViewModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService)
{
CylinderModel = container.Resolve<CylinderModel>();
}
/// <summary>
/// 打开对话框后要执行的
/// </summary>
/// <param name="parameters">parameters</param>
public override void OnDialogOpened(IDialogParameters parameters)
{
Controllers.Clear();
foreach (var item in CylinderModel.GlobalModel.ControllersModel.Controllers)
{
Controllers.Add(item);
}
}
/// <summary>
/// 关闭对话框后要执行的
/// </summary>
public override void OnDialogClosed()
{
}
/// <summary>
/// 气缸模型
/// </summary>
public CylinderModel CylinderModel { get; set; }
/// <summary>
/// 控制器列表
/// </summary>
public ObservableCollection<Controller> Controllers { get; set; } = [];
private Controller _SelectedController;
/// <summary>
///
/// </summary>
public Controller SelectedController
{
get { return _SelectedController; }
set
{
SetProperty(ref _SelectedController, value);
if (value != null)
{
DI.Clear();
DQ.Clear();
foreach (var item in value.DI)
{
DI.Add(item);
}
foreach (var item in value.DQ)
{
DQ.Add(item);
}
}
}
}
/// <summary>
/// DI
/// </summary>
public ObservableCollection<DI> DI { get; set; } = [];
/// <summary>
/// DQ
/// </summary>
[NotMapped]
public ObservableCollection<DQ> DQ { get; set; } = [];
private Cylinder _SelectedCylinder;
/// <summary>
/// 选中的气缸
/// </summary>
public Cylinder SelectedCylinder
{
get { return _SelectedCylinder; }
set { SetProperty(ref _SelectedCylinder, value); }
}
private DelegateCommand<string> _CylinderManage;
/// <summary>
/// 气缸管理
/// </summary>
public DelegateCommand<string> CylinderManage =>
_CylinderManage ??= new DelegateCommand<string>(ExecuteCylinderManage);
private void ExecuteCylinderManage(string cmd)
{
switch (cmd)
{
case "New":
var cylinder = new Cylinder { Name = "新气缸" };
CylinderModel.Cylinders.Add(cylinder);
break;
case "Save":
// 先统一校验名称,避免中途 return 残留已跟踪实体
foreach (var item in CylinderModel.Cylinders)
{
if (string.IsNullOrEmpty(item.Name))
{
SendInfoDialog("保存失败,气缸名称不能为空");
return;
}
}
foreach (var item in CylinderModel.Cylinders)
{
if (!CylinderModel._DatasContext.Cylinders.Any(h => h.ID == item.ID))
{
CylinderModel._DatasContext.Cylinders.Add(item);
}
else
{
var exist = CylinderModel._DatasContext.Cylinders.FirstOrDefault(c => c.ID == item.ID);
if (exist != null)
{
exist.Name = item.Name;
exist.ControllerID = item.ControllerID;
exist.SensorType = item.SensorType;
exist.InA = item.InA;
exist.InB = item.InB;
exist.OutA = item.OutA;
exist.OutB = item.OutB;
exist.OffWhenStopped = item.OffWhenStopped;
exist.OffWhenInPosition = item.OffWhenInPosition;
exist.Comment = item.Comment;
}
}
}
CylinderModel._DatasContext.Save();
foreach (var item in CylinderModel.Cylinders)
{
item.Prompt = "";
}
SendInfoDialog("气缸保存成功");
break;
case "Remove":
if (SelectedCylinder != null)
{
if (CylinderModel._DatasContext.Cylinders.Any(h => h.ID == SelectedCylinder.ID))
{
var remove = CylinderModel._DatasContext.Cylinders.FirstOrDefault(c => c.ID == SelectedCylinder.ID);
if (remove != null)
CylinderModel._DatasContext.Cylinders.Remove(remove);
}
CylinderModel.Cylinders.Remove(SelectedCylinder);
CylinderModel._DatasContext.Save();
SelectedCylinder = null;
SendInfoDialog("已删除");
}
break;
}
}
#region 气缸操作
private DelegateCommand<object> _CylinderAction;
/// <summary>
/// 气缸操作(A位/B位)
/// </summary>
public DelegateCommand<object> CylinderAction =>
_CylinderAction ??= new DelegateCommand<object>(ExecuteCylinderAction);
private void ExecuteCylinderAction(object param)
{
var cmd = param as string;
if (SelectedCylinder == null)
{
SendInfoDialog("请先选择气缸");
return;
}
if (CylinderModel.GlobalModel.ModeState.ModeEnum != ModeState.WorkMode.Manual)
{
SendInfoDialog("非手动模式禁止操作");
return;
}
switch (cmd)
{
case "A":
SelectedCylinder.Cmd = "A";
break;
case "B":
SelectedCylinder.Cmd = "B";
break;
}
}
#endregion 气缸操作
}
}