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 { /// /// this class handles Level Trigger edit/create functionality /// [PartCreationPolicy(CreationPolicy.Shared)] public class DigitalOutputChannelsViewModel : IDigitalOutputChannelsViewModel { /// /// The Hardware Scan view /// public IDigitalOutputChannelsView View { get; set; } private IEventAggregator _eventAggregator { get; } private IRegionManager _regionManager; private IUnityContainer UnityContainer { get; } public InteractionRequest NotificationRequest { get; } public InteractionRequest ConfirmationRequest { get; } /// /// Creates a new instance of the TechnologyDomainEditViewModel. /// /// The IDigitalOutputChannelsView. /// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed. /// The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other. /// The unityContainer. public DigitalOutputChannelsViewModel(IDigitalOutputChannelsView view, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer) { View = view; View.DataContext = this; NotificationRequest = new InteractionRequest(); ConfirmationRequest = new InteractionRequest(); _eventAggregator = eventAggregator; UnityContainer = unityContainer; _regionManager = regionManager; _eventAggregator.GetEvent().Subscribe(OnRaiseNotification); _eventAggregator.GetEvent() .Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true); _eventAggregator.GetEvent().Subscribe(OnAssignedChannelsChangedEvent, ThreadOption.PublisherThread, true); _eventAggregator.GetEvent() .Subscribe(OnHardwareScanComplete, ThreadOption.PublisherThread, true); _eventAggregator.GetEvent() .Subscribe(OnEIDComplete, ThreadOption.PublisherThread, true); } #region Methods private void OnEIDComplete(IDictionary sensorIdToChannelId) { var channelIdToSensorId = new Dictionary(); using (var e = sensorIdToChannelId.GetEnumerator()) { while (e.MoveNext()) { channelIdToSensorId[e.Current.Value] = e.Current.Key; } } _hardwareChannelIdToSensorId = channelIdToSensorId; } private void OnHardwareScanComplete(List 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(); var channelIdToChannelRecord = new Dictionary(); 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(); 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() { } /// /// Private Event handler for RaiseNotification event. /// private void OnBusyIndicatorNotification(bool eventArg) { IsBusy = eventArg; } /// /// Private Event handler for RaiseNotification event. /// 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 _hardware; private IDictionary _hardwareChannelIdToSensorId = new Dictionary(); 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 DASChannels { get; set; } = new ObservableCollection(); 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 /// /// /// Occurs when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }