public static string SavePhasor(DataConnection connection, Phasor phasor, bool isNew) { //DataConnection connection = new DataConnection(); bool createdConnection = false; try { if (connection == null) { connection = new DataConnection(); createdConnection = true; } IDbCommand command = connection.Connection.CreateCommand(); if (isNew) command.CommandText = "Insert Into Phasor (DeviceID, Label, Type, Phase, DestinationPhasorID, SourceIndex, UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) Values (@deviceID, @label, @type, @phase, " + "@destinationPhasorID, @sourceIndex, @updatedBy, @updatedOn, @createdBy, @createdOn)"; else command.CommandText = "Update Phasor Set DeviceID =@deviceID, Label = @label, Type = @type, Phase = @phase, DestinationPhasorID = @destinationPhasorID, " + "SourceIndex = @sourceIndex, UpdatedBy = @updatedBy, UpdatedOn = @updatedOn Where ID = @id"; command.Parameters.Add(AddWithValue(command, "@deviceID", phasor.DeviceID)); command.Parameters.Add(AddWithValue(command, "@label", phasor.Label)); command.Parameters.Add(AddWithValue(command, "@type", phasor.Type)); command.Parameters.Add(AddWithValue(command, "@phase", phasor.Phase)); command.Parameters.Add(AddWithValue(command, "@destinationPhasorID", phasor.DestinationPhasorID ?? (object)DBNull.Value)); command.Parameters.Add(AddWithValue(command, "@sourceIndex", phasor.SourceIndex)); command.Parameters.Add(AddWithValue(command, "@updatedBy", s_currentUser)); command.Parameters.Add(AddWithValue(command, "@updatedOn", command.Connection.ConnectionString.Contains("Microsoft.Jet.OLEDB") ? DateTime.UtcNow.Date : DateTime.UtcNow)); if (isNew) { command.Parameters.Add(AddWithValue(command, "@createdBy", s_currentUser)); command.Parameters.Add(AddWithValue(command, "@createdOn", command.Connection.ConnectionString.Contains("Microsoft.Jet.OLEDB") ? DateTime.UtcNow.Date : DateTime.UtcNow)); } else command.Parameters.Add(AddWithValue(command, "@id", phasor.ID)); command.ExecuteNonQuery(); Device device = new Device(); device = GetDeviceByDeviceID(connection, phasor.DeviceID); Measurement measurement; if (s_voltagePhasorSignalTypes == null || s_voltagePhasorSignalTypes.Rows.Count == 0) s_voltagePhasorSignalTypes = GetPhasorSignalTypes(connection, "V"); if (s_currentPhasorSignalTypes == null || s_currentPhasorSignalTypes.Rows.Count == 0) s_currentPhasorSignalTypes = GetPhasorSignalTypes(connection, "I"); if (phasor.Type == "V") s_phasorSignalTypes = s_voltagePhasorSignalTypes; else s_phasorSignalTypes = s_currentPhasorSignalTypes; Phasor addedPhasor = new Phasor(); //addedPhasor = GetPhasorByLabel(phasor.DeviceID, phasor.Label); addedPhasor = GetPhasorBySourceIndex(connection, phasor.DeviceID, phasor.SourceIndex); //we will try again just to make sure we get information back about the added phasor. As MS Access is very slow and sometimes fails to retrieve data. if (addedPhasor == null) { System.Threading.Thread.Sleep(500); //addedPhasor = GetPhasorByLabel(phasor.DeviceID, phasor.Label); addedPhasor = GetPhasorBySourceIndex(connection, phasor.DeviceID, phasor.SourceIndex); } foreach (DataRow row in s_phasorSignalTypes.Rows) { measurement = new Measurement(); measurement.HistorianID = device.HistorianID; measurement.DeviceID = device.ID; if (addedPhasor.DestinationPhasorID.HasValue) measurement.PointTag = device.CompanyAcronym + "_" + device.Acronym + "-" + GetPhasorByID(connection, addedPhasor.DeviceID, (int)addedPhasor.DestinationPhasorID).Label + ":" + device.VendorAcronym + row["Abbreviation"].ToString(); else measurement.PointTag = device.CompanyAcronym + "_" + device.Acronym + "-" + row["Suffix"].ToString() + addedPhasor.SourceIndex.ToString() + ":" + device.VendorAcronym + row["Abbreviation"].ToString(); measurement.AlternateTag = string.Empty; measurement.SignalTypeID = Convert.ToInt32(row["ID"]); measurement.PhasorSourceIndex = addedPhasor.SourceIndex; measurement.SignalReference = device.Acronym + "-" + row["Suffix"].ToString() + addedPhasor.SourceIndex.ToString(); measurement.Adder = 0.0d; measurement.Multiplier = 1.0d; measurement.Description = device.Name + " " + addedPhasor.Label + " " + device.VendorDeviceName + " " + addedPhasor.PhaseType + " " + row["Name"].ToString(); measurement.Enabled = true; if (isNew) //if it is a new phasor then add measurements as new. SaveMeasurement(connection, measurement, true); else //Check if measurement exists, if so then update them otherwise add new. { Measurement existingMeasurement = new Measurement(); existingMeasurement = GetMeasurementInfo(connection, measurement.DeviceID, row["Suffix"].ToString(), measurement.PhasorSourceIndex); if (existingMeasurement == null) SaveMeasurement(connection, measurement, true); else { measurement.SignalID = existingMeasurement.SignalID; SaveMeasurement(connection, measurement, false); } } } return "Phasor Information Saved Successfully"; } finally { if (createdConnection && connection != null) connection.Dispose(); } }
public static string SaveWizardConfigurationInfo(DataConnection connection, string nodeID, List<WizardDeviceInfo> wizardDeviceInfoList, string connectionString, int? protocolID, int? companyID, int? historianID, int? interconnectionID, int? parentID, bool skipDisableRealTimeData) { bool createdConnection = false; try { if (connection == null) { connection = new DataConnection(); createdConnection = true; } List<string> nondistinctAcronymList = new List<string>(); nondistinctAcronymList = (from item in wizardDeviceInfoList where item.Include == true group item by item.Acronym into grouped where grouped.Count() > 1 select grouped.Key).ToList(); if (nondistinctAcronymList.Count > 0) { StringBuilder sb = new StringBuilder("Duplicate Acronyms Exist"); foreach (string item in nondistinctAcronymList) { sb.AppendLine(); sb.Append(item); } throw new ArgumentException(sb.ToString()); } int loadOrder = 1; foreach (WizardDeviceInfo info in wizardDeviceInfoList) { if (info.Include) { Device device = new Device(); device.NodeID = nodeID; device.Acronym = info.Acronym; device.Name = info.Name; device.IsConcentrator = false; device.Longitude = info.Longitude; device.Latitude = info.Latitude; device.ConnectionString = parentID == null ? connectionString : string.Empty; device.ProtocolID = protocolID; device.CompanyID = companyID; device.HistorianID = historianID; device.InterconnectionID = interconnectionID; device.Enabled = true; device.VendorDeviceID = info.VendorDeviceID == null ? (int?)null : info.VendorDeviceID == 0 ? (int?)null : info.VendorDeviceID; device.ParentID = parentID; device.AccessID = info.AccessID; device.LoadOrder = loadOrder; device.SkipDisableRealTimeData = skipDisableRealTimeData; device.TimeZone = string.Empty; device.TimeAdjustmentTicks = 0; device.DataLossInterval = 5; device.MeasuredLines = 1; device.ContactList = string.Empty; device.AllowedParsingExceptions = 10; device.ParsingExceptionWindow = 5; device.DelayedConnectionInterval = 5; device.AllowUseOfCachedConfiguration = true; device.AutoStartDataParsingSequence = true; device.MeasurementReportingInterval = 100000; //If Add Digitals and Add Analogs is checked for the device then, if digitals and analogs are available i.e. count>0 then add them as measurements. int digitalCount = 0; if (info.AddDigitals && info.DigitalCount > 0) { digitalCount = info.DigitalCount; } int analogCount = 0; if (info.AddAnalogs && info.AnalogCount > 0) { analogCount = info.AnalogCount; } Device existingDevice = GetDeviceByAcronym(connection, info.Acronym); if (existingDevice != null) { device.ID = existingDevice.ID; device.TimeZone = existingDevice.TimeZone; device.TimeAdjustmentTicks = existingDevice.TimeAdjustmentTicks; device.DataLossInterval = existingDevice.DataLossInterval; device.MeasuredLines = existingDevice.MeasuredLines; device.ContactList = existingDevice.ContactList; device.AllowedParsingExceptions = existingDevice.AllowedParsingExceptions; device.ParsingExceptionWindow = existingDevice.ParsingExceptionWindow; device.DelayedConnectionInterval = existingDevice.DelayedConnectionInterval; device.AllowUseOfCachedConfiguration = existingDevice.AllowUseOfCachedConfiguration; device.AutoStartDataParsingSequence = existingDevice.AutoStartDataParsingSequence; device.MeasurementReportingInterval = existingDevice.MeasurementReportingInterval; SaveDevice(connection, device, false, digitalCount, analogCount); } else SaveDevice(connection, device, true, digitalCount, analogCount); Device addedDevice = GetDeviceByAcronym(connection, info.Acronym); int count = 1; foreach (PhasorInfo phasorInfo in info.PhasorList) { if (phasorInfo.Label.ToLower() != "unused") { Phasor phasor = new Phasor(); phasor.DeviceID = addedDevice.ID; phasor.Label = phasorInfo.Label; phasor.Type = phasorInfo.Type; phasor.Phase = phasorInfo.Phase; phasor.DestinationPhasorID = null; phasor.SourceIndex = count; Phasor existingPhasor = GetPhasorBySourceIndex(connection, addedDevice.ID, phasor.SourceIndex); if (existingPhasor != null) { phasor.ID = existingPhasor.ID; SavePhasor(connection, phasor, false); } else SavePhasor(connection, phasor, true); } count++; } } loadOrder++; } return "Configuration Information Saved Successfully"; } finally { if (createdConnection && connection != null) connection.Dispose(); } }
void SavePhasor(Phasor phasor, bool isNew) { SystemMessages sm; DataConnection connection = new DataConnection(); ; try { string result = CommonFunctions.SavePhasor(connection, phasor, isNew); //ClearForm(); sm = new SystemMessages(new Message() { UserMessage = result, SystemMessage = string.Empty, UserMessageType = MessageType.Success }, ButtonType.OkOnly); sm.Owner = Window.GetWindow(this); sm.WindowStartupLocation = WindowStartupLocation.CenterOwner; sm.ShowPopup(); GetPhasors(); GetPhasorList(); //make this newly added or updated item as default selected. So user can click initialize right away. ListBoxPhasorList.SelectedItem = ((List<Phasor>)ListBoxPhasorList.ItemsSource).Find(c => c.Label == phasor.Label); //Update Metadata in the openPDC Service. try { Device device = CommonFunctions.GetDeviceByDeviceID(connection, phasor.DeviceID); WindowsServiceClient serviceClient = ((App)Application.Current).ServiceClient; if (serviceClient != null && serviceClient.Helper.RemotingClient.CurrentState == TVA.Communication.ClientState.Connected) { if (device.HistorianID != null) { string runtimeID = CommonFunctions.GetRuntimeID(connection, "Historian", (int)device.HistorianID); CommonFunctions.SendCommandToWindowsService(serviceClient, "Invoke " + runtimeID + " RefreshMetadata"); } if (device.Enabled) //if device is enabled then send initialize command otherwise send reloadconfig command. { if (device.ParentID == null) CommonFunctions.SendCommandToWindowsService(serviceClient, "Initialize " + CommonFunctions.GetRuntimeID(connection, "Device", device.ID)); else CommonFunctions.SendCommandToWindowsService(serviceClient, "Initialize " + CommonFunctions.GetRuntimeID(connection, "Device", (int)device.ParentID)); } else CommonFunctions.SendCommandToWindowsService(serviceClient, "ReloadConfig"); //we do this to make sure all statistical measurements are in the system. CommonFunctions.SendCommandToWindowsService(serviceClient, "RefreshRoutes"); } else { sm = new SystemMessages(new openPDCManager.Utilities.Message() { UserMessage = "Failed to Perform Configuration Changes", SystemMessage = "Application is disconnected from the openPDC Service.", UserMessageType = openPDCManager.Utilities.MessageType.Information }, ButtonType.OkOnly); sm.Owner = Window.GetWindow(this); sm.WindowStartupLocation = WindowStartupLocation.CenterOwner; sm.ShowPopup(); } } catch (Exception ex) { sm = new SystemMessages(new openPDCManager.Utilities.Message() { UserMessage = "Failed to Perform Configuration Changes", SystemMessage = ex.Message, UserMessageType = openPDCManager.Utilities.MessageType.Information }, ButtonType.OkOnly); sm.Owner = Window.GetWindow(this); sm.WindowStartupLocation = WindowStartupLocation.CenterOwner; sm.ShowPopup(); CommonFunctions.LogException(null, "SavePhasor.RefreshMetadata", ex); } } catch (Exception ex) { CommonFunctions.LogException(connection, "WPF.SavePhasor", ex); sm = new SystemMessages(new Message() { UserMessage = "Failed to Save Phasor Information", SystemMessage = ex.Message, UserMessageType = MessageType.Error }, ButtonType.OkOnly); sm.Owner = Window.GetWindow(this); sm.WindowStartupLocation = WindowStartupLocation.CenterOwner; sm.ShowPopup(); } finally { if (connection != null) connection.Dispose(); } }