private IConfigurationFrame RequestConfigurationFrame(string connectionString) { using (CommonPhasorServices phasorServices = new CommonPhasorServices()) { phasorServices.StatusMessage += (sender, e) => LogStatusMessage(e.Argument.Replace("**", "")); phasorServices.ProcessException += (sender, e) => LogException(e.Argument); return(phasorServices.RequestDeviceConfiguration(connectionString)); } }
/// <summary> /// Creates a new measurement object for power calculation output measurements /// </summary> private static PowerMeasurement CreateMeasurement(string companyAcronym, string deviceAcronym, string vendorAcronym, string signalTypeAcronym, string circuitDescription, int deviceID, int?historianID, string descriptionSuffix) { PowerMeasurement measurement = new PowerMeasurement { PointTag = CommonPhasorServices.CreatePointTag(companyAcronym, deviceAcronym, vendorAcronym, signalTypeAcronym), Adder = 0, Multiplier = 1, Description = $"{circuitDescription} {descriptionSuffix}", DeviceID = deviceID, HistorianID = historianID, SignalTypeID = 10, Enabled = true, SignalID = Guid.Empty }; if (measurement.PointTag != null) { int beginIndex = measurement.PointTag.IndexOf('_'); measurement.SignalReference = measurement.PointTag.Substring(beginIndex + 1); } return(measurement); }
public string CreatePhasorPointTag(string deviceAcronym, string signalTypeAcronym, string phasorLabel, string phase, int signalIndex, int baseKV) { return(CommonPhasorServices.CreatePointTag(CompanyAcronym, deviceAcronym, null, signalTypeAcronym, phasorLabel, signalIndex, string.IsNullOrWhiteSpace(phase) ? '_' : phase.Trim()[0], baseKV)); }
public string CreateIndexedPointTag(string deviceAcronym, string signalTypeAcronym, int signalIndex) { return(CommonPhasorServices.CreatePointTag(CompanyAcronym, deviceAcronym, null, signalTypeAcronym, null, signalIndex)); }
public ConfigurationFrame LoadConfigurationFrame(string sourceData) { string connectionString = ""; IConfigurationFrame GetConfigurationFrame() { try { ConnectionSettings connectionSettings; SoapFormatter formatter = new SoapFormatter { AssemblyFormat = FormatterAssemblyStyle.Simple, TypeFormat = FormatterTypeStyle.TypesWhenNeeded, Binder = Serialization.LegacyBinder }; using (MemoryStream source = new MemoryStream(Encoding.UTF8.GetBytes(sourceData))) connectionSettings = formatter.Deserialize(source) as ConnectionSettings; if ((object)connectionSettings != null) { connectionString = connectionSettings.ConnectionString; Dictionary <string, string> connectionStringKeyValues = connectionString.ParseKeyValuePairs(); connectionString = "transportProtocol=" + connectionSettings.TransportProtocol + ";" + connectionStringKeyValues.JoinKeyValuePairs(); if ((object)connectionSettings.ConnectionParameters != null) { switch (connectionSettings.PhasorProtocol) { case PhasorProtocol.BPAPDCstream: GSF.PhasorProtocols.BPAPDCstream.ConnectionParameters bpaParameters = connectionSettings.ConnectionParameters as GSF.PhasorProtocols.BPAPDCstream.ConnectionParameters; if ((object)bpaParameters != null) { connectionString += "; iniFileName=" + bpaParameters.ConfigurationFileName + "; refreshConfigFileOnChange=" + bpaParameters.RefreshConfigurationFileOnChange + "; parseWordCountFromByte=" + bpaParameters.ParseWordCountFromByte; } break; case PhasorProtocol.FNET: GSF.PhasorProtocols.FNET.ConnectionParameters fnetParameters = connectionSettings.ConnectionParameters as GSF.PhasorProtocols.FNET.ConnectionParameters; if ((object)fnetParameters != null) { connectionString += "; timeOffset=" + fnetParameters.TimeOffset + "; stationName=" + fnetParameters.StationName + "; frameRate=" + fnetParameters.FrameRate + "; nominalFrequency=" + (int)fnetParameters.NominalFrequency; } break; case PhasorProtocol.SelFastMessage: GSF.PhasorProtocols.SelFastMessage.ConnectionParameters selParameters = connectionSettings.ConnectionParameters as GSF.PhasorProtocols.SelFastMessage.ConnectionParameters; if ((object)selParameters != null) { connectionString += "; messagePeriod=" + selParameters.MessagePeriod; } break; case PhasorProtocol.IEC61850_90_5: GSF.PhasorProtocols.IEC61850_90_5.ConnectionParameters iecParameters = connectionSettings.ConnectionParameters as GSF.PhasorProtocols.IEC61850_90_5.ConnectionParameters; if ((object)iecParameters != null) { connectionString += "; useETRConfiguration=" + iecParameters.UseETRConfiguration + "; guessConfiguration=" + iecParameters.GuessConfiguration + "; parseRedundantASDUs=" + iecParameters.ParseRedundantASDUs + "; ignoreSignatureValidationFailures=" + iecParameters.IgnoreSignatureValidationFailures + "; ignoreSampleSizeValidationFailures=" + iecParameters.IgnoreSampleSizeValidationFailures; } break; case PhasorProtocol.Macrodyne: GSF.PhasorProtocols.Macrodyne.ConnectionParameters macrodyneParameters = connectionSettings.ConnectionParameters as GSF.PhasorProtocols.Macrodyne.ConnectionParameters; if ((object)macrodyneParameters != null) { connectionString += "; protocolVersion=" + macrodyneParameters.ProtocolVersion + "; iniFileName=" + macrodyneParameters.ConfigurationFileName + "; refreshConfigFileOnChange=" + macrodyneParameters.RefreshConfigurationFileOnChange + "; deviceLabel=" + macrodyneParameters.DeviceLabel; } break; } } connectionString += "; accessID=" + connectionSettings.PmuID; connectionString += "; phasorProtocol=" + connectionSettings.PhasorProtocol; using (CommonPhasorServices phasorServices = new CommonPhasorServices()) { phasorServices.StatusMessage += (sender, e) => LogStatusMessage(e.Argument.Replace("**", "")); phasorServices.ProcessException += (sender, e) => LogException(e.Argument); return(phasorServices.RequestDeviceConfiguration(connectionString)); } } using (MemoryStream source = new MemoryStream(Encoding.UTF8.GetBytes(sourceData))) return(formatter.Deserialize(source) as IConfigurationFrame); } catch { return(new ConfigurationErrorFrame()); } } IConfigurationFrame sourceFrame = GetConfigurationFrame(); if (sourceFrame is ConfigurationErrorFrame) { return(new ConfigurationFrame()); } ConfigurationFrame derivedFrame; // Create a new simple concrete configuration frame for JSON serialization converted from equivalent configuration information int protocolID = 0, deviceID = 0, phasorID = -1; // Start phasor ID's at less than -1 since associated voltage == -1 is reserved as unselected if (!string.IsNullOrWhiteSpace(connectionString)) { Dictionary <string, string> settings = connectionString.ParseKeyValuePairs(); protocolID = GetProtocolID(settings["phasorProtocol"]); } derivedFrame = new ConfigurationFrame { IDCode = sourceFrame.IDCode, FrameRate = sourceFrame.FrameRate, ConnectionString = connectionString, ProtocolID = protocolID }; foreach (IConfigurationCell sourceCell in sourceFrame.Cells) { // Create new derived configuration cell ConfigurationCell derivedCell = new ConfigurationCell { ID = --deviceID, // Provide a negative index so any database lookup will return null ParentID = null, IDCode = sourceCell.IDCode, StationName = sourceCell.StationName, IDLabel = sourceCell.IDLabel }; // Create equivalent derived frequency definition IFrequencyDefinition sourceFrequency = sourceCell.FrequencyDefinition; if (sourceFrequency != null) { derivedCell.FrequencyDefinition = new FrequencyDefinition { Label = sourceFrequency.Label } } ; int sourceIndex = 0; // Create equivalent derived phasor definitions foreach (IPhasorDefinition sourcePhasor in sourceCell.PhasorDefinitions) { derivedCell.PhasorDefinitions.Add(new PhasorDefinition { ID = --phasorID, Label = sourcePhasor.Label, PhasorType = sourcePhasor.PhasorType.ToString(), SourceIndex = ++sourceIndex }); } // Create equivalent derived analog definitions (assuming analog type = SinglePointOnWave) foreach (IAnalogDefinition sourceAnalog in sourceCell.AnalogDefinitions) { derivedCell.AnalogDefinitions.Add(new AnalogDefinition { Label = sourceAnalog.Label, AnalogType = sourceAnalog.AnalogType.ToString() }); } // Create equivalent derived digital definitions foreach (IDigitalDefinition sourceDigital in sourceCell.DigitalDefinitions) { derivedCell.DigitalDefinitions.Add(new DigitalDefinition { Label = sourceDigital.Label }); } // Add cell to frame derivedFrame.Cells.Add(derivedCell); } derivedFrame.IsConcentrator = derivedFrame.Cells.Count > 1; return(derivedFrame); }
/// <summary> /// Saves <see cref="Phasor"/> information to database. /// </summary> /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param> /// <param name="phasor">Information about <see cref="Phasor"/>.</param> /// <param name="oldSourceIndex">The old source index of the phasor.</param> /// <param name="skipMeasurementUpdate">Skips associated measurement update if this is already being handled.</param> /// <returns>String, for display use, indicating success.</returns> public static string SaveAndReorder(AdoDataConnection database, Phasor phasor, int oldSourceIndex, bool skipMeasurementUpdate = false) { bool createdConnection = false; string query; try { createdConnection = CreateConnection(ref database); if (phasor.SourceIndex == 0) { phasor.SourceIndex = database.ExecuteScalar <int>("SELECT MAX(SourceIndex) FROM Phasor WHERE DeviceID = {0}", phasor.DeviceID) + 1; } // Since phasors could be reordered in the source device, this test could inadvertently throw an exception when it should not - so the validation has been removed //if (database.ExecuteScalar<int>("SELECT COUNT(*) FROM Phasor WHERE ID <> {0} AND DeviceID = {1} AND SourceIndex = {2}", phasor.ID, phasor.DeviceID, phasor.SourceIndex) > 0) // throw new InvalidOperationException("Phasor source index must be unique per device."); if (phasor.ID == 0) { query = database.ParameterizedQueryString("INSERT INTO Phasor (DeviceID, Label, Type, Phase, BaseKV, SourceIndex, UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) " + "VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9})", "deviceID", "label", "type", "phase", "baseKV", "sourceIndex", "updatedBy", "updatedOn", "createdBy", "createdOn"); database.Connection.ExecuteNonQuery(query, DefaultTimeout, phasor.DeviceID, phasor.Label, phasor.Type, phasor.Phase, phasor.BaseKV, phasor.SourceIndex, CommonFunctions.CurrentUser, database.UtcNow, CommonFunctions.CurrentUser, database.UtcNow); } else { query = database.ParameterizedQueryString("UPDATE Phasor SET DeviceID = {0}, Label = {1}, Type = {2}, Phase = {3}, BaseKV = {4}, SourceIndex = {5}, " + "UpdatedBy = {6}, UpdatedOn = {7} WHERE ID = {8}", "deviceID", "label", "type", "phase", "baseKV", "sourceIndex", "updatedBy", "updatedOn", "id"); database.Connection.ExecuteNonQuery(query, DefaultTimeout, phasor.DeviceID, phasor.Label, phasor.Type, phasor.Phase, phasor.BaseKV, phasor.SourceIndex, CommonFunctions.CurrentUser, database.UtcNow, phasor.ID); } // Get reference to the device to which phasor is being added. Device device = Device.GetDevice(database, "WHERE ID = " + phasor.DeviceID); // Get Phasor signal types. ObservableCollection <SignalType> signals; if (phasor.Type == "V") { signals = SignalType.GetVoltagePhasorSignalTypes(); } else { signals = SignalType.GetCurrentPhasorSignalTypes(); } // Get reference to phasor which has just been added. Phasor addedPhasor = GetPhasor(database, "WHERE DeviceID = " + phasor.DeviceID + " AND SourceIndex = " + phasor.SourceIndex); foreach (SignalType signal in signals) { Measurement measurement = Measurement.GetMeasurement(database, "WHERE DeviceID = " + phasor.DeviceID + " AND SignalTypeSuffix = '" + signal.Suffix + "' AND PhasorSourceIndex = " + oldSourceIndex); if ((object)measurement == null) { measurement = new Measurement(); measurement.DeviceID = device.ID; measurement.HistorianID = device.HistorianID; measurement.PointTag = CommonPhasorServices.CreatePointTag(device.CompanyAcronym, device.Acronym, device.VendorAcronym, signal.Acronym, addedPhasor.Label, addedPhasor.SourceIndex, addedPhasor.Phase[0], addedPhasor.BaseKV); measurement.SignalReference = device.Acronym + "-" + signal.Suffix + addedPhasor.SourceIndex; measurement.SignalTypeID = signal.ID; measurement.Description = device.Name + " " + addedPhasor.Label + " " + device.VendorDeviceName + " " + addedPhasor.Phase + " " + signal.Name; measurement.PhasorSourceIndex = addedPhasor.SourceIndex; measurement.Enabled = true; Measurement.Save(database, measurement); } else if (!skipMeasurementUpdate || addedPhasor.SourceIndex != oldSourceIndex) // || measurement.SignalTypeID != signal.ID { // Update existing record when needed or when phasor source index has changed measurement.HistorianID = device.HistorianID; measurement.PointTag = CommonPhasorServices.CreatePointTag(device.CompanyAcronym, device.Acronym, device.VendorAcronym, signal.Acronym, addedPhasor.Label, addedPhasor.SourceIndex, addedPhasor.Phase[0], addedPhasor.BaseKV); measurement.SignalReference = device.Acronym + "-" + signal.Suffix + addedPhasor.SourceIndex; measurement.SignalTypeID = signal.ID; measurement.Description = device.Name + " " + addedPhasor.Label + " " + device.VendorDeviceName + " " + addedPhasor.Phase + " " + signal.Name; measurement.PhasorSourceIndex = addedPhasor.SourceIndex; Measurement.Save(database, measurement); } } return("Phasor information saved successfully"); } finally { if (createdConnection && database != null) { database.Dispose(); } } }