303 lines
11 KiB
C#
303 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel;
|
|
using System.ComponentModel.Composition;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Data;
|
|
using DTS.Common.Events;
|
|
using DTS.Common.Events.TTSImport;
|
|
using DTS.Common.Interface.DataRecorders;
|
|
using DTS.Common.Interface.TestSetups.Imports.TTS.DOChannels;
|
|
using DTS.Common.Interface.TestSetups.Imports.TTS.ReadFile;
|
|
using Prism.Events;
|
|
using Unity;
|
|
using DTS.Common.Interactivity;
|
|
using Prism.Regions;
|
|
using Prism.Commands;
|
|
using TTSImport.Model;
|
|
using TTSImport.Resources;
|
|
|
|
// ReSharper disable CheckNamespace
|
|
// ReSharper disable MemberCanBePrivate.Global
|
|
// ReSharper disable InconsistentNaming
|
|
|
|
namespace TTSImport
|
|
{
|
|
/// <summary>
|
|
/// this class handles Level Trigger edit/create functionality
|
|
/// </summary>
|
|
[PartCreationPolicy(CreationPolicy.Shared)]
|
|
public class DigitalOutputChannelsViewModel : IDigitalOutputChannelsViewModel
|
|
{
|
|
/// <summary>
|
|
/// The Hardware Scan view
|
|
/// </summary>
|
|
public IDigitalOutputChannelsView View { get; set; }
|
|
|
|
private IEventAggregator _eventAggregator { get; }
|
|
private IRegionManager _regionManager;
|
|
private IUnityContainer UnityContainer { get; }
|
|
|
|
public InteractionRequest<Notification> NotificationRequest { get; }
|
|
public InteractionRequest<Confirmation> ConfirmationRequest { get; }
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the TechnologyDomainEditViewModel.
|
|
/// </summary>
|
|
/// <param name="view">The IDigitalOutputChannelsView.</param>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="eventAggregator">The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other.</param>
|
|
/// <param name="unityContainer">The unityContainer.</param>
|
|
public DigitalOutputChannelsViewModel(IDigitalOutputChannelsView view, IRegionManager regionManager,
|
|
IEventAggregator eventAggregator, IUnityContainer unityContainer)
|
|
{
|
|
View = view;
|
|
View.DataContext = this;
|
|
|
|
NotificationRequest = new InteractionRequest<Notification>();
|
|
ConfirmationRequest = new InteractionRequest<Confirmation>();
|
|
|
|
_eventAggregator = eventAggregator;
|
|
UnityContainer = unityContainer;
|
|
_regionManager = regionManager;
|
|
_eventAggregator.GetEvent<RaiseNotification>().Subscribe(OnRaiseNotification);
|
|
_eventAggregator.GetEvent<BusyIndicatorChangeNotification>()
|
|
.Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true);
|
|
_eventAggregator.GetEvent<AssignedChannelsChangedEvent>().Subscribe(OnAssignedChannelsChangedEvent,
|
|
ThreadOption.PublisherThread, true);
|
|
_eventAggregator.GetEvent<TTSImportHardwareScanFinishedEvent>()
|
|
.Subscribe(OnHardwareScanComplete, ThreadOption.PublisherThread, true);
|
|
_eventAggregator.GetEvent<EIDMappingEvent>()
|
|
.Subscribe(OnEIDComplete, ThreadOption.PublisherThread, true);
|
|
}
|
|
|
|
#region Methods
|
|
private void OnEIDComplete(IDictionary<string, string> sensorIdToChannelId)
|
|
{
|
|
var channelIdToSensorId = new Dictionary<string, string>();
|
|
using (var e = sensorIdToChannelId.GetEnumerator())
|
|
{
|
|
while (e.MoveNext())
|
|
{
|
|
channelIdToSensorId[e.Current.Value] = e.Current.Key;
|
|
}
|
|
}
|
|
_hardwareChannelIdToSensorId = channelIdToSensorId;
|
|
}
|
|
private void OnHardwareScanComplete(List<IDASHardware> hardware)
|
|
{
|
|
_hardware = hardware;
|
|
}
|
|
|
|
private void OnAssignedChannelsChangedEvent(ITTSSetup setup)
|
|
{
|
|
if (!Application.Current.Dispatcher.CheckAccess())
|
|
{
|
|
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
|
{
|
|
OnAssignedChannelsChangedEvent(setup);
|
|
}));
|
|
return;
|
|
}
|
|
_setup = setup;
|
|
if (null == _hardware || null == _setup) { return; }
|
|
var channels = new ObservableCollection<DASChannel>();
|
|
var channelIdToChannelRecord = new Dictionary<string, ITTSChannelRecord>();
|
|
foreach (var ch in _setup.Channels)
|
|
{
|
|
if (!ch.IsDigitalOutput) { continue; }
|
|
if (null == ch.HardwareChannel) { continue; }
|
|
if (ch.ChannelCode == TTSChannelRecord.NONE) { continue; }
|
|
channelIdToChannelRecord[ch.HardwareChannel.GetId()] = ch;
|
|
}
|
|
var channelIdToDASChannel = new Dictionary<string, DASChannel>();
|
|
foreach (var das in _hardware)
|
|
{
|
|
var ichannels = das.GetIHardwareChannels();
|
|
foreach (var ch in ichannels)
|
|
{
|
|
if (!ch.IsDigitalOut) { continue; }
|
|
var newChannel = new DASChannel(ch, _setup);
|
|
if (_hardwareChannelIdToSensorId.ContainsKey(ch.GetId()))
|
|
{
|
|
newChannel.EID = _hardwareChannelIdToSensorId[ch.GetId()];
|
|
}
|
|
channels.Add(newChannel);
|
|
channelIdToDASChannel[newChannel.HardwareChannel.GetId()] = newChannel;
|
|
if (channelIdToChannelRecord.ContainsKey(ch.GetId()))
|
|
{
|
|
newChannel.SetITTSChannelRecord(channelIdToChannelRecord[ch.GetId()]);
|
|
newChannel.Channel.ChannelCode = $"Digital Out {ch.ToString()}";
|
|
newChannel.Channel.SensorSerialNumber = newChannel.Channel.ChannelCode;
|
|
}
|
|
}
|
|
}
|
|
|
|
using (var enumChannels = channelIdToDASChannel.GetEnumerator())
|
|
{
|
|
while (enumChannels.MoveNext())
|
|
{
|
|
if (null == enumChannels.Current.Value.Channel)
|
|
{
|
|
//CREATE new channel
|
|
var ch = new TTSChannelRecord();
|
|
ch.ChannelCode = $"Digital Out {enumChannels.Current.Value.HardwareChannel?.ToString()}";
|
|
ch.IsChannelCodeValid = true;
|
|
ch.IsDigitalOutput = true;
|
|
ch.SensorEU = "V";
|
|
ch.HardwareChannel = enumChannels.Current.Value.HardwareChannel;
|
|
//ch.SensorSerialNumber = ch.ChannelCode;
|
|
enumChannels.Current.Value.SetITTSChannelRecord(ch);
|
|
}
|
|
}
|
|
}
|
|
|
|
DASChannels = channels;
|
|
OnPropertyChanged("DASChannels");
|
|
CollectionViewSource.GetDefaultView(DASChannels)?.Refresh();
|
|
}
|
|
public void Cleanup()
|
|
{
|
|
}
|
|
|
|
public Task CleanupAsync()
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
}
|
|
|
|
public void Initialize(object parameter)
|
|
{
|
|
}
|
|
|
|
public void Initialize(object parameter, object model)
|
|
{
|
|
}
|
|
|
|
public Task InitializeAsync()
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task InitializeAsync(object parameter)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public void Activated()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Private Event handler for RaiseNotification event.
|
|
/// </summary>
|
|
private void OnBusyIndicatorNotification(bool eventArg)
|
|
{
|
|
IsBusy = eventArg;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Private Event handler for RaiseNotification event.
|
|
/// </summary>
|
|
private void OnRaiseNotification(NotificationContentEventArgs eventArgsWithTitle)
|
|
{
|
|
// The NotificationRequest.Raise triggers the Invoke() method of the PopupWindowAction object to show the NotificationWindow window
|
|
// Notification object expects a NotificationContentEventArgsWithoutTitle object and a Title string.
|
|
var eventArgsWithoutTitle = new NotificationContentEventArgs(eventArgsWithTitle.Message, "",
|
|
eventArgsWithTitle.Image, string.Empty);
|
|
|
|
NotificationRequest.Raise(new Notification
|
|
{
|
|
Content = eventArgsWithoutTitle,
|
|
Title = eventArgsWithTitle.Title
|
|
});
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
private ITTSSetup _setup;
|
|
private IList<IDASHardware> _hardware;
|
|
private IDictionary<string, string> _hardwareChannelIdToSensorId = new Dictionary<string, string>();
|
|
public bool IsDirty { get; private set; }
|
|
private bool _isBusy;
|
|
|
|
public bool IsBusy
|
|
{
|
|
get => _isBusy;
|
|
set
|
|
{
|
|
_isBusy = value;
|
|
OnPropertyChanged("IsBusy");
|
|
}
|
|
}
|
|
|
|
private bool _isMenuIncluded;
|
|
|
|
public bool IsMenuIncluded
|
|
{
|
|
get => _isMenuIncluded;
|
|
set
|
|
{
|
|
_isMenuIncluded = value;
|
|
OnPropertyChanged("IsMenuIncluded");
|
|
}
|
|
}
|
|
|
|
private bool _isNavigationIncluded;
|
|
|
|
public bool IsNavigationIncluded
|
|
{
|
|
get => _isNavigationIncluded;
|
|
set
|
|
{
|
|
_isNavigationIncluded = value;
|
|
OnPropertyChanged("IsNavigationIncluded");
|
|
}
|
|
}
|
|
public ObservableCollection<DASChannel> DASChannels { get; set; } = new ObservableCollection<DASChannel>();
|
|
|
|
private DASChannel _selectedDASChannel;
|
|
public DASChannel SelectedDASChannel
|
|
{
|
|
get => _selectedDASChannel;
|
|
set => _selectedDASChannel = value;
|
|
}
|
|
|
|
public string EnableOrDisableText
|
|
{
|
|
get
|
|
{
|
|
if (SelectedDASChannel?.Channel == null)
|
|
{
|
|
return StringResources.Analog_Enable;
|
|
}
|
|
return SelectedDASChannel.Channel.Disabled
|
|
? StringResources.Analog_Enable
|
|
: StringResources.Analog_Disable;
|
|
}
|
|
}
|
|
#endregion Properties
|
|
|
|
#region Commands
|
|
|
|
#endregion
|
|
|
|
/// <inheritdoc />
|
|
/// <summary>
|
|
/// Occurs when a property value changes.
|
|
/// </summary>
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
public void OnPropertyChanged(string propertyName)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
}
|
|
}
|