//******************************************************************************************************// AverageFrequencyViewModel.cs - Gbtc//// Copyright © 2010, Grid Protection Alliance. All Rights Reserved.//// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See// the NOTICE file distributed with this work for additional information regarding copyright ownership.// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may// not use this file except in compliance with the License. You may obtain a copy of the License at://// http://www.opensource.org/licenses/MIT//// Unless agreed to in writing, the subject software distributed under the License is distributed on an// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the// License for the specific language governing permissions and limitations.//// Code Modification History:// ----------------------------------------------------------------------------------------------------// 08/20/2012 - Stephen C. Wills// Generated original version of source code.////****************************************************************************************************** using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;using System.Linq;using System.Windows;using GSF;using GSF.Data;using GSF.PhasorProtocols.UI.DataModels;using GSF.TimeSeries.UI;using GSF.TimeSeries.UI.Commands;using GSF.TimeSeries.UI.DataModels; // Namespace name changed from OneSecondFrequencyAverager to AverageFrequencyUI// because WPF has difficulties resolving between the namespace and the class// which, unfortunately, have the same name.namespace AverageFrequencyUI{ /// <summary> /// View model for the <see cref="AverageFrequencyUserControl"/>. /// </summary> public class AverageFrequencyViewModel : DependencyObject, INotifyPropertyChanged { #region [ Members ] // Nested Types /// <summary> /// Represents a mapping between a frequency input definition /// and an average frequency output definition. /// </summary> public class IOMapping { private string m_signalReference; private string m_inputKey; private string m_outputKey; /// <summary> /// Gets or sets the signal reference of the frequency input. /// </summary> public string SignalReference { get { return m_signalReference; } set { m_signalReference = value; } } /// <summary> /// Gets or sets the measurement key of the frequency input. /// </summary> public string InputKey { get { return m_inputKey; } set { m_inputKey = value; } } /// <summary> /// Gets or sets the measurement key of the frequency output. /// </summary> public string OutputKey { get { return m_outputKey; } set { m_outputKey = value; } } } /// <summary> /// Represents a frequency measurement which can be selected /// as an input to the average frequency calculator. /// </summary> public class FrequencyMeasurement { private string m_signalReference; private string m_key; /// <summary> /// Gets or sets the signal reference of the frequency measurement. /// </summary> public string SignalReference { get { return m_signalReference; } set { m_signalReference = value; } } /// <summary> /// Gets or sets the measurement key of the frequency measurement. /// </summary> public string Key { get { return m_key; } set { m_key = value; } } } // Data model for the average frequency user interface. private class DataModel { public readonly List<Device> Devices; public readonly List<Measurement> Measurements; public DataModel() { using (AdoDataConnection database = new AdoDataConnection(CommonFunctions.DefaultSettingsCategory)) { Devices = Device.Load(database, Device.LoadKeys(database)).ToList(); Measurements = Measurement.GetMeasurements(database, "WHERE SignalAcronym = 'FREQ'").ToList(); } } } // Events /// <summary> /// Occurs when a property value changes. /// </summary> public event PropertyChangedEventHandler PropertyChanged; // Fields private ObservableCollection<string> m_virtualDeviceNames; private ObservableCollection<IOMapping> m_ioMappings; private ObservableCollection<FrequencyMeasurement> m_frequencyMeasurements; private Adapter m_calculator; private string m_selectedVirtualDeviceName; private string m_oldVirtualDeviceName; private IOMapping m_selectedIOMapping; private FrequencyMeasurement m_selectedFrequencyMeasurement; private readonly RelayCommand m_addSelectedCommand; private readonly RelayCommand m_removeSelectedCommand; private DataModel m_dataModel; private bool m_ignorePropertyNotify; #endregion #region [ Constructors ] /// <summary> /// Creates a new instance of the <see cref="AverageFrequencyViewModel"/> class. /// </summary> public AverageFrequencyViewModel() { m_addSelectedCommand = new RelayCommand(AddSelected); m_removeSelectedCommand = new RelayCommand(RemoveSelected); PropertyChanged += (sender, args) => HandlePropertyChanged(args.PropertyName); } #endregion #region [ Properties ] /// <summary> /// Gets or sets the collection of virtual device names in the system. /// </summary> public ObservableCollection<string> VirtualDeviceNames { get { return m_virtualDeviceNames; } set { m_virtualDeviceNames = value; OnPropertyChanged("VirtualDeviceNames"); } } /// <summary> /// Gets or sets the collection of I/O mappings used by the average frequency calculator. /// </summary> public ObservableCollection<IOMapping> IOMappings { get { return m_ioMappings; } set { m_ioMappings = value; OnPropertyChanged("IOMappings"); } } /// <summary> /// Gets or sets the collection of frequency measurements in the system. /// </summary> public ObservableCollection<FrequencyMeasurement> FrequencyMeasurements { get { return m_frequencyMeasurements; } set { m_frequencyMeasurements = value; OnPropertyChanged("FrequencyMeasurements"); } } /// <summary> /// Gets or sets the calculator to be configured. /// </summary> public Adapter Calculator { get { return m_calculator; } set { m_calculator = value; OnPropertyChanged("Calculator"); } } /// <summary> /// Gets or sets the name of the virtual device to associate output measurements with. /// </summary> public string SelectedVirtualDeviceName { get { return m_selectedVirtualDeviceName; } set { m_oldVirtualDeviceName = m_selectedVirtualDeviceName; m_selectedVirtualDeviceName = value; OnPropertyChanged("SelectedVirtualDeviceName"); } } /// <summary> /// Gets or sets the I/O mapping the user currently has selected. /// </summary> public IOMapping SelectedIOMapping { get { return m_selectedIOMapping; } set { m_selectedIOMapping = value; OnPropertyChanged("SelectedIOMapping"); } } /// <summary> /// Gets or sets the frequency measurement the user currently has selected. /// </summary> public FrequencyMeasurement SelectedFrequencyMeasurement { get { return m_selectedFrequencyMeasurement; } set { m_selectedFrequencyMeasurement = value; OnPropertyChanged("SelectedFrequencyMeasurement"); } } /// <summary> /// Gets the command that is invoked when adding an I/O mapping for the selected frequency measurement. /// </summary> public RelayCommand AddSelectedCommand { get { return m_addSelectedCommand; } } /// <summary> /// Gets the command that is invoked when removing an I/O mapping from the calculator. /// </summary> public RelayCommand RemoveSelectedCommand { get { return m_removeSelectedCommand; } } #endregion #region [ Methods ] /// <summary> /// Called when the view is loaded. /// </summary> public virtual void Load() { IEnumerable<string> virtualDeviceNames; try { m_dataModel = new DataModel(); virtualDeviceNames = m_dataModel.Devices .Where(device => device.ProtocolName == "Virtual Device") .Select(device => device.Acronym); VirtualDeviceNames = new ObservableCollection<string>(virtualDeviceNames); OnPropertyChanged("Calculator"); } catch (Exception ex) { MessageBox.Show("ERROR - Unable to load measurements from database due to exception: " + ex.Message, "Load", MessageBoxButton.OK); } } /// <summary> /// Called when the view is unloaded. /// </summary> public virtual void Unload() { string virtualDeviceNameKey; if ((object)m_calculator != null) { virtualDeviceNameKey = string.Format("AverageFrequency.{0}.VirtualDeviceName", m_calculator.AdapterName); if (!string.IsNullOrEmpty(m_selectedVirtualDeviceName)) IsolatedStorageManager.WriteToIsolatedStorage(virtualDeviceNameKey, m_selectedVirtualDeviceName); } } /// <summary> /// Handles change in a property once per call stack. /// </summary> /// <param name="propertyName">The name of the property that was changed.</param> protected virtual void HandlePropertyChangedOnce(string propertyName) { switch (propertyName) { case "Calculator": if ((object)m_dataModel != null) { SelectVirtualDeviceName(); UpdateIOMappings(); UpdateFrequencyMeasurements(); } break; case "SelectedVirtualDeviceName": // Check to see if a change was made to the value of the property if (m_selectedVirtualDeviceName == m_oldVirtualDeviceName) return; // Ask the user to confirm their selection if (ConfirmVirtualDeviceSelection()) { UpdateVirtualDevice(); } else { // User chose not to confirm; restore the old name and notify the view Dispatcher.BeginInvoke(new Action(() => { m_selectedVirtualDeviceName = m_oldVirtualDeviceName; OnPropertyChanged("SelectedVirtualDeviceName"); })); } break; } } // Calls HandlePropertyChangedOnce in such a way that prevents infinite recursion when modifying other properties. private void HandlePropertyChanged(string propertyName) { if (m_ignorePropertyNotify) return; m_ignorePropertyNotify = true; HandlePropertyChangedOnce(propertyName); m_ignorePropertyNotify = false; } #region [ User adds a frequency ] // Adds the selected measurement to the average calculator. private void AddSelected() { if ((object)m_calculator == null) return; if ((object)m_selectedFrequencyMeasurement == null) return; CreateOutputMeasurement(); AddNewMapping(); Load(); } // Creates a measurement to define the average frequency signal. private void CreateOutputMeasurement() { Device virtualDevice = null; Measurement inputMeasurement; Measurement outputMeasurement; if (!string.IsNullOrEmpty(m_selectedVirtualDeviceName)) { virtualDevice = m_dataModel.Devices .Single(dev => dev.Acronym == m_selectedVirtualDeviceName); } inputMeasurement = m_dataModel.Measurements .Single(measurement => measurement.ID == m_selectedFrequencyMeasurement.Key); outputMeasurement = new Measurement { HistorianID = inputMeasurement.HistorianID, DeviceID = ((object)virtualDevice != null) ? virtualDevice.ID : (int?)null, PointTag = string.Format("{0}_{1}", m_calculator.AdapterName, inputMeasurement.PointTag), SignalTypeID = inputMeasurement.SignalTypeID, SignalReference = string.Format("{0}-FQ", m_selectedVirtualDeviceName ?? m_calculator.AdapterName), Description = string.Format("Average {0}", inputMeasurement.Description), Internal = true, Subscribed = false, Enabled = true }; Measurement.Save(null, outputMeasurement); } // Adds a new mapping between the input and output measurements to the connection string. private void AddNewMapping() { Dictionary<string, string> settings; string inputMeasurementKeys; string outputMeasurements; settings = m_calculator.ConnectionString.ToNonNullString().ParseKeyValuePairs(); if (!settings.TryGetValue("inputMeasurementKeys", out inputMeasurementKeys)) inputMeasurementKeys = string.Empty; if (!settings.TryGetValue("outputMeasurements", out outputMeasurements)) outputMeasurements = string.Empty; inputMeasurementKeys += ";" + m_selectedFrequencyMeasurement.Key; outputMeasurements += ";" + GetNewMeasurementKey();Lines 1–500 of 723. Load more
//****************************************************************************************************** // AverageFrequencyViewModel.cs - Gbtc // // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 08/20/2012 - Stephen C. Wills // Generated original version of source code. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Windows; using GSF; using GSF.Data; using GSF.PhasorProtocols.UI.DataModels; using GSF.TimeSeries.UI; using GSF.TimeSeries.UI.Commands; using GSF.TimeSeries.UI.DataModels; // Namespace name changed from OneSecondFrequencyAverager to AverageFrequencyUI // because WPF has difficulties resolving between the namespace and the class // which, unfortunately, have the same name. namespace AverageFrequencyUI { /// <summary> /// View model for the <see cref="AverageFrequencyUserControl"/>. /// </summary> public class AverageFrequencyViewModel : DependencyObject, INotifyPropertyChanged { #region [ Members ] // Nested Types /// <summary> /// Represents a mapping between a frequency input definition /// and an average frequency output definition. /// </summary> public class IOMapping { private string m_signalReference; private string m_inputKey; private string m_outputKey; /// <summary> /// Gets or sets the signal reference of the frequency input. /// </summary> public string SignalReference { get { return m_signalReference; } set { m_signalReference = value; } } /// <summary> /// Gets or sets the measurement key of the frequency input. /// </summary> public string InputKey { get { return m_inputKey; } set { m_inputKey = value; } } /// <summary> /// Gets or sets the measurement key of the frequency output. /// </summary> public string OutputKey { get { return m_outputKey; } set { m_outputKey = value; } } } /// <summary> /// Represents a frequency measurement which can be selected /// as an input to the average frequency calculator. /// </summary> public class FrequencyMeasurement { private string m_signalReference; private string m_key; /// <summary> /// Gets or sets the signal reference of the frequency measurement. /// </summary> public string SignalReference { get { return m_signalReference; } set { m_signalReference = value; } } /// <summary> /// Gets or sets the measurement key of the frequency measurement. /// </summary> public string Key { get { return m_key; } set { m_key = value; } } } // Data model for the average frequency user interface. private class DataModel { public readonly List<Device> Devices; public readonly List<Measurement> Measurements; public DataModel() { using (AdoDataConnection database = new AdoDataConnection(CommonFunctions.DefaultSettingsCategory)) { Devices = Device.Load(database, Device.LoadKeys(database)).ToList(); Measurements = Measurement.GetMeasurements(database, "WHERE SignalAcronym = 'FREQ'").ToList(); } } } // Events /// <summary> /// Occurs when a property value changes. /// </summary> public event PropertyChangedEventHandler PropertyChanged; // Fields private ObservableCollection<string> m_virtualDeviceNames; private ObservableCollection<IOMapping> m_ioMappings; private ObservableCollection<FrequencyMeasurement> m_frequencyMeasurements; private Adapter m_calculator; private string m_selectedVirtualDeviceName; private string m_oldVirtualDeviceName; private IOMapping m_selectedIOMapping; private FrequencyMeasurement m_selectedFrequencyMeasurement; private readonly RelayCommand m_addSelectedCommand; private readonly RelayCommand m_removeSelectedCommand; private DataModel m_dataModel; private bool m_ignorePropertyNotify; #endregion #region [ Constructors ] /// <summary> /// Creates a new instance of the <see cref="AverageFrequencyViewModel"/> class. /// </summary> public AverageFrequencyViewModel() { m_addSelectedCommand = new RelayCommand(AddSelected); m_removeSelectedCommand = new RelayCommand(RemoveSelected); PropertyChanged += (sender, args) => HandlePropertyChanged(args.PropertyName); } #endregion #region [ Properties ] /// <summary> /// Gets or sets the collection of virtual device names in the system. /// </summary> public ObservableCollection<string> VirtualDeviceNames { get { return m_virtualDeviceNames; } set { m_virtualDeviceNames = value; OnPropertyChanged("VirtualDeviceNames"); } } /// <summary> /// Gets or sets the collection of I/O mappings used by the average frequency calculator. /// </summary> public ObservableCollection<IOMapping> IOMappings { get { return m_ioMappings; } set { m_ioMappings = value; OnPropertyChanged("IOMappings"); } } /// <summary> /// Gets or sets the collection of frequency measurements in the system. /// </summary> public ObservableCollection<FrequencyMeasurement> FrequencyMeasurements { get { return m_frequencyMeasurements; } set { m_frequencyMeasurements = value; OnPropertyChanged("FrequencyMeasurements"); } } /// <summary> /// Gets or sets the calculator to be configured. /// </summary> public Adapter Calculator { get { return m_calculator; } set { m_calculator = value; OnPropertyChanged("Calculator"); } } /// <summary> /// Gets or sets the name of the virtual device to associate output measurements with. /// </summary> public string SelectedVirtualDeviceName { get { return m_selectedVirtualDeviceName; } set { m_oldVirtualDeviceName = m_selectedVirtualDeviceName; m_selectedVirtualDeviceName = value; OnPropertyChanged("SelectedVirtualDeviceName"); } } /// <summary> /// Gets or sets the I/O mapping the user currently has selected. /// </summary> public IOMapping SelectedIOMapping { get { return m_selectedIOMapping; } set { m_selectedIOMapping = value; OnPropertyChanged("SelectedIOMapping"); } } /// <summary> /// Gets or sets the frequency measurement the user currently has selected. /// </summary> public FrequencyMeasurement SelectedFrequencyMeasurement { get { return m_selectedFrequencyMeasurement; } set { m_selectedFrequencyMeasurement = value; OnPropertyChanged("SelectedFrequencyMeasurement"); } } /// <summary> /// Gets the command that is invoked when adding an I/O mapping for the selected frequency measurement. /// </summary> public RelayCommand AddSelectedCommand { get { return m_addSelectedCommand; } } /// <summary> /// Gets the command that is invoked when removing an I/O mapping from the calculator. /// </summary> public RelayCommand RemoveSelectedCommand { get { return m_removeSelectedCommand; } } #endregion #region [ Methods ] /// <summary> /// Called when the view is loaded. /// </summary> public virtual void Load() { IEnumerable<string> virtualDeviceNames; try { m_dataModel = new DataModel(); virtualDeviceNames = m_dataModel.Devices .Where(device => device.ProtocolName == "Virtual Device") .Select(device => device.Acronym); VirtualDeviceNames = new ObservableCollection<string>(virtualDeviceNames); OnPropertyChanged("Calculator"); } catch (Exception ex) { MessageBox.Show("ERROR - Unable to load measurements from database due to exception: " + ex.Message, "Load", MessageBoxButton.OK); } } /// <summary> /// Called when the view is unloaded. /// </summary> public virtual void Unload() { string virtualDeviceNameKey; if ((object)m_calculator != null) { virtualDeviceNameKey = string.Format("AverageFrequency.{0}.VirtualDeviceName", m_calculator.AdapterName); if (!string.IsNullOrEmpty(m_selectedVirtualDeviceName)) IsolatedStorageManager.WriteToIsolatedStorage(virtualDeviceNameKey, m_selectedVirtualDeviceName); } } /// <summary> /// Handles change in a property once per call stack. /// </summary> /// <param name="propertyName">The name of the property that was changed.</param> protected virtual void HandlePropertyChangedOnce(string propertyName) { switch (propertyName) { case "Calculator": if ((object)m_dataModel != null) { SelectVirtualDeviceName(); UpdateIOMappings(); UpdateFrequencyMeasurements(); } break; case "SelectedVirtualDeviceName": // Check to see if a change was made to the value of the property if (m_selectedVirtualDeviceName == m_oldVirtualDeviceName) return; // Ask the user to confirm their selection if (ConfirmVirtualDeviceSelection()) { UpdateVirtualDevice(); } else { // User chose not to confirm; restore the old name and notify the view Dispatcher.BeginInvoke(new Action(() => { m_selectedVirtualDeviceName = m_oldVirtualDeviceName; OnPropertyChanged("SelectedVirtualDeviceName"); })); } break; } } // Calls HandlePropertyChangedOnce in such a way that prevents infinite recursion when modifying other properties. private void HandlePropertyChanged(string propertyName) { if (m_ignorePropertyNotify) return; m_ignorePropertyNotify = true; HandlePropertyChangedOnce(propertyName); m_ignorePropertyNotify = false; } #region [ User adds a frequency ] // Adds the selected measurement to the average calculator. private void AddSelected() { if ((object)m_calculator == null) return; if ((object)m_selectedFrequencyMeasurement == null) return; CreateOutputMeasurement(); AddNewMapping(); Load(); } // Creates a measurement to define the average frequency signal. private void CreateOutputMeasurement() { Device virtualDevice = null; Measurement inputMeasurement; Measurement outputMeasurement; if (!string.IsNullOrEmpty(m_selectedVirtualDeviceName)) { virtualDevice = m_dataModel.Devices .Single(dev => dev.Acronym == m_selectedVirtualDeviceName); } inputMeasurement = m_dataModel.Measurements .Single(measurement => measurement.ID == m_selectedFrequencyMeasurement.Key); outputMeasurement = new Measurement { HistorianID = inputMeasurement.HistorianID, DeviceID = ((object)virtualDevice != null) ? virtualDevice.ID : (int?)null, PointTag = string.Format("{0}_{1}", m_calculator.AdapterName, inputMeasurement.PointTag), SignalTypeID = inputMeasurement.SignalTypeID, SignalReference = string.Format("{0}-FQ", m_selectedVirtualDeviceName ?? m_calculator.AdapterName), Description = string.Format("Average {0}", inputMeasurement.Description), Internal = true, Subscribed = false, Enabled = true }; Measurement.Save(null, outputMeasurement); } // Adds a new mapping between the input and output measurements to the connection string. private void AddNewMapping() { Dictionary<string, string> settings; string inputMeasurementKeys; string outputMeasurements; settings = m_calculator.ConnectionString.ToNonNullString().ParseKeyValuePairs(); if (!settings.TryGetValue("inputMeasurementKeys", out inputMeasurementKeys)) inputMeasurementKeys = string.Empty; if (!settings.TryGetValue("outputMeasurements", out outputMeasurements)) outputMeasurements = string.Empty; inputMeasurementKeys += ";" + m_selectedFrequencyMeasurement.Key; outputMeasurements += ";" + GetNewMeasurementKey();