2026-03-30 17:09:29 +09:00

232 lines
10 KiB
C#

using Aurora;
using Aurora.Api;
using Aurora.Api.Control;
using Aurora.Api.Manager;
using Aurora.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
namespace AuroraIO.View
{
public class DIValue : NotifyPropertyChangedBase
{
public string Address { get; set; }
public string ID { get; set; }
private string _description;
public string Description { get => _description; set => SetProperty(ref _description, value); }
private bool _dIStatus;
public bool DIStatus { get => _dIStatus; set => SetProperty(ref _dIStatus, value); }
}
public class DOValue : NotifyPropertyChangedBase
{
public string Address { get; set; }
public string ID { get; set; }
private string _description;
public string Description { get => _description; set => SetProperty(ref _description, value); }
private bool _dOStatus;
public bool DOStatus { get => _dOStatus; set => SetProperty(ref _dOStatus, value); }
}
public class AIValue : NotifyPropertyChangedBase
{
public string Address { get; set; }
public string ID { get; set; }
private string _description;
public string Description { get => _description; set => SetProperty(ref _description, value); }
private string _AIStatus;
public string AIStatus { get => _AIStatus; set => SetProperty(ref _AIStatus, value); }
}
public class AOValue : NotifyPropertyChangedBase
{
public string Address { get; set; }
public string ID { get; set; }
private string _description;
public string Description { get => _description; set => SetProperty(ref _description, value); }
private string _AOStatus;
public string AOStatus { get => _AOStatus; set => SetProperty(ref _AOStatus, value); }
}
public class UCIOInterfaceViewModel : NotifyPropertyChangedBase
{
private static readonly Logger Log = new Logger("IOBoardInterfaceView");
#region Binding Property
private readonly IDeviceManager _devManager;
private readonly IODevice _IO;
private ObservableCollection<DIValue> _DigitalInputsView = new ObservableCollection<DIValue>();
public ObservableCollection<DIValue> DigitalInputsView { get => _DigitalInputsView; set => SetProperty(ref _DigitalInputsView, value); }
private ObservableCollection<DOValue> _DigitalOutputsView = new ObservableCollection<DOValue>();
public ObservableCollection<DOValue> DigitalOutputsView { get => _DigitalOutputsView; set => SetProperty(ref _DigitalOutputsView, value); }
private string _SelectedDOutputItem;
public string SelectedDOutputItem { get => _SelectedDOutputItem; set => SetProperty(ref _SelectedDOutputItem, value); }
private int _SelectedDOutputIndex;
public int SelectedDOutputIndex { get => _SelectedDOutputIndex; set => SetProperty(ref _SelectedDOutputIndex, value); }
private ObservableCollection<AIValue> _AnalogInputsView = new ObservableCollection<AIValue>();
public ObservableCollection<AIValue> AnalogInputsView { get => _AnalogInputsView; set => SetProperty(ref _AnalogInputsView, value); }
private ObservableCollection<AOValue> _AnalogOutputsView = new ObservableCollection<AOValue>();
public ObservableCollection<AOValue> AnalogOutputsView { get => _AnalogOutputsView; set => SetProperty(ref _AnalogOutputsView, value); }
private string _SelectedWOutputItem;
public string SelectedWOutputItem { get => _SelectedWOutputItem; set => SetProperty(ref _SelectedWOutputItem, value); }
private int _SelectedWOutputIndex;
public int SelectedWOutputIndex { get => _SelectedWOutputIndex; set => SetProperty(ref _SelectedWOutputIndex, value); }
private short _InputWORD;
public short InputWORD { get => _InputWORD; set => SetProperty(ref _InputWORD, value); }
public ICommand CMD_SETBIT { get; }
public ICommand CMD_SETWORD { get; }
public ICommand CMD_VisibleChanged { get; }
#endregion
private readonly DispatcherTimer InterfaceViewTimer;
public UCIOInterfaceViewModel(IAuroraEnvironment aurora, string deviceId)
{
_devManager = aurora.DeviceManager;
_IO = _devManager[deviceId] as IODevice;
CMD_SETBIT = new DelegateCommand(SetBit);
CMD_VisibleChanged = new DelegateCommand(VisibleChanged);
InterfaceViewTimer = new DispatcherTimer();
InterfaceViewTimer.Tick += OnInterfaceViewMonitoring;
InterfaceViewTimer.Interval = TimeSpan.FromMilliseconds(100);
}
private void OnInterfaceViewMonitoring(object sender, EventArgs e)
{
if (!_IO.IsActivated || _IO.IsSimul)
return;
try {
//Device에서 MonitorPlcInterfaceMap함수로 PlcDIOValues를 업데이트해주니깐 버퍼에서 상태를 받아오는것으로 구현..
//Read Digital Inputs
bool[] arrDI = new bool[_IO._iOUnit.GetDICount()];
_IO._iOUnit.GetDiArray(ref arrDI);
bool[] arrDO = new bool[_IO._iOUnit.GetDOCount()];
_IO._iOUnit.GetDoArray(ref arrDO);
foreach (var ioValue in DigitalInputsView) {
ioValue.DIStatus = Convert.ToBoolean(arrDI[Convert.ToInt32(ioValue.Address)]);
}
//Read Digital Outputs
foreach (var ioValue in DigitalOutputsView) {
ioValue.DOStatus = Convert.ToBoolean(arrDO[Convert.ToInt32(ioValue.Address)]);
}
} catch (Exception ex) {
Log.E(ex.Message);
}
}
private void VisibleChanged(object obj)
{
bool visible = (bool)obj;
if (visible) {
if (_IO.IsActivated) {
InitializeView();
InterfaceViewTimer?.Start();
}
} else {
ClearView();
InterfaceViewTimer?.Stop();
}
}
public void SetBit(object obj)
{
try
{
if (DigitalOutputsView.Count > 0)
{
bool targetDOStatus = (DigitalOutputsView[SelectedDOutputIndex].DOStatus == true) ? false : true;
string selectedDOAddr = DigitalOutputsView[SelectedDOutputIndex].Address;
string selectedDODescripiton = DigitalOutputsView[SelectedDOutputIndex].Description;
var result = MessageDialog.Show(Application.Current.MainWindow, "IO Board Interface", $"Do you want to change DO of {selectedDODescripiton}?",
MessageDialog.Type.AffirmativeAndNegative);
if (result == MessageDialog.Result.Negative)
return;
_IO._iOUnit.WriteDoBit(Convert.ToInt32(selectedDOAddr), targetDOStatus);
}
else
{
Log.E(string.Format("DigitalOutputsView is null"));
}
}
catch (Exception ex)
{
Log.E(ex.Message);
}
}
private void ClearView()
{
DigitalInputsView.Clear();
DigitalOutputsView.Clear();
AnalogInputsView.Clear();
AnalogOutputsView.Clear();
}
private void InitializeView()
{
InitializeView(_IO._iOUnit.ListDI, InfoIO.IoType.DI);
InitializeView(_IO._iOUnit.ListDO, InfoIO.IoType.DO);
InitializeView(_IO._iOUnit.ListAI, InfoIO.IoType.AI);
InitializeView(_IO._iOUnit.ListAO, InfoIO.IoType.AO);
}
private void InitializeView(List<InfoIO> listIO, InfoIO.IoType type)
{
try {
//switch (type)
//{
// case InfoIO.IoType.DI:
// DigitalInputsView.Clear();
// break;
// case InfoIO.IoType.DO:
// DigitalOutputsView.Clear();
// break;
// case InfoIO.IoType.AI:
// AnalogInputsView.Clear();
// break;
// case InfoIO.IoType.AO:
// AnalogOutputsView.Clear();
// break;
//}
foreach (InfoIO item in listIO) {
switch (type) {
case InfoIO.IoType.DI:
DIValue dIItem = new DIValue();
dIItem.Address = item.Address;
dIItem.Description = item.Description;
dIItem.ID = item.ID;
DigitalInputsView.Add(dIItem);
break;
case InfoIO.IoType.DO:
DOValue dOItem = new DOValue();
dOItem.Address = item.Address;
dOItem.Description = item.Description;
dOItem.ID = item.ID;
DigitalOutputsView.Add(dOItem);
break;
case InfoIO.IoType.AI:
AIValue wIItem = new AIValue();
wIItem.Address = item.Address;
wIItem.Description = item.Description;
wIItem.ID = item.ID;
AnalogInputsView.Add(wIItem);
break;
case InfoIO.IoType.AO:
AOValue wOItem = new AOValue();
wOItem.Address = item.Address;
wOItem.Description = item.Description;
wOItem.ID = item.ID;
AnalogOutputsView.Add(wOItem);
break;
}
}
} catch (Exception ex) {
Log.E(ex.Message);
}
}
}
}