Ejemplo n.º 1
0
        /// <summary>
        /// Creates and saves new <see cref="OutputStreamMeasurement"/> into the database.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="outputStreamID">ID of the output stream.</param>
        /// <param name="measurements">Collection of measurements to be added.</param>
        /// <returns>String, for display use, indicating success.</returns>
        public static string AddMeasurements(AdoDataConnection database, int outputStreamID, ObservableCollection <Measurement> measurements)
        {
            bool createdConnection = false;

            try
            {
                createdConnection = CreateConnection(ref database);

                foreach (Measurement measurement in measurements)
                {
                    OutputStreamMeasurement outputStreamMeasurement = new OutputStreamMeasurement
                    {
                        NodeID          = (Guid)database.CurrentNodeID(),
                        AdapterID       = outputStreamID,
                        HistorianID     = measurement.HistorianID,
                        PointID         = measurement.PointID,
                        SignalReference = measurement.SignalReference
                    };

                    Save(database, outputStreamMeasurement);
                }

                return("Output stream measurements added successfully.");
            }
            finally
            {
                if (createdConnection && database != null)
                {
                    database.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves <see cref="OutputStreamDevice"/> information to database.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="outputStreamDevice">Information about <see cref="OutputStreamDevice"/>.</param>
        /// <returns>String, for display use, indicating success.</returns>
        public static string Save(AdoDataConnection database, OutputStreamDevice outputStreamDevice)
        {
            bool   createdConnection = false;
            string query;

            try
            {
                createdConnection = CreateConnection(ref database);

                if (outputStreamDevice.ID == 0)
                {
                    query = database.ParameterizedQueryString("INSERT INTO OutputStreamDevice (NodeID, AdapterID, IDCode, Acronym, BpaAcronym, Name, " +
                                                              "PhasorDataFormat, FrequencyDataFormat, AnalogDataFormat, CoordinateFormat, LoadOrder, Enabled, UpdatedBy, UpdatedOn, CreatedBy, CreatedOn)" +
                                                              "VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15})", "nodeID", "adapterID", "idCode", "acronym",
                                                              "bpaAcronym", "name", "phasorDataFormat", "frequencyDataFormat", "analogDataFormat", "coordinateFormat", "loadOrder", "enabled",
                                                              "updatedBy", "updatedOn", "createdBy", "createdOn");

                    database.Connection.ExecuteNonQuery(query, DefaultTimeout, database.CurrentNodeID(), outputStreamDevice.AdapterID, outputStreamDevice.IDCode,
                                                        outputStreamDevice.Acronym, outputStreamDevice.BpaAcronym.ToNotNull(), outputStreamDevice.Name, outputStreamDevice.PhasorDataFormat.ToNotNull(),
                                                        outputStreamDevice.FrequencyDataFormat.ToNotNull(), outputStreamDevice.AnalogDataFormat.ToNotNull(), outputStreamDevice.CoordinateFormat.ToNotNull(),
                                                        outputStreamDevice.LoadOrder, database.Bool(outputStreamDevice.Enabled), CommonFunctions.CurrentUser,
                                                        database.UtcNow, CommonFunctions.CurrentUser, database.UtcNow);
                }
                else
                {
                    OutputStreamDevice originalDevice = GetOutputStreamDevice(database, "WHERE ID = " + outputStreamDevice.ID);

                    query = database.ParameterizedQueryString("UPDATE OutputStreamDevice SET NodeID = {0}, AdapterID = {1}, IDCode = {2}, Acronym = {3}, BpaAcronym = {4}, " +
                                                              "Name = {5}, PhasorDataFormat = {6}, FrequencyDataFormat = {7}, AnalogDataFormat = {8}, CoordinateFormat = {9}, LoadOrder = {10}, Enabled = {11}, " +
                                                              " UpdatedBy = {12}, UpdatedOn = {13} WHERE ID = {14}", "nodeID", "adapterID", "idCode", "acronym", "bpaAcronym", "name",
                                                              "phasorDataFormat", "frequencyDataFormat", "analogDataFormat", "coordinateFormat", "loadOrder", "enabled", "updatedBy", "updatedOn", "id");

                    database.Connection.ExecuteNonQuery(query, DefaultTimeout, database.Guid(outputStreamDevice.NodeID), outputStreamDevice.AdapterID, outputStreamDevice.IDCode,
                                                        outputStreamDevice.Acronym, outputStreamDevice.BpaAcronym.ToNotNull(), outputStreamDevice.Name, outputStreamDevice.PhasorDataFormat.ToNotNull(),
                                                        outputStreamDevice.FrequencyDataFormat.ToNotNull(), outputStreamDevice.AnalogDataFormat.ToNotNull(), outputStreamDevice.CoordinateFormat.ToNotNull(),
                                                        outputStreamDevice.LoadOrder, database.Bool(outputStreamDevice.Enabled), CommonFunctions.CurrentUser,
                                                        database.UtcNow, outputStreamDevice.ID);

                    if (originalDevice != null && originalDevice.Acronym != outputStreamDevice.Acronym)
                    {
                        IList <int> keys = OutputStreamMeasurement.LoadKeys(database, originalDevice.AdapterID);

                        foreach (OutputStreamMeasurement measurement in OutputStreamMeasurement.Load(database, keys))
                        {
                            measurement.SignalReference = measurement.SignalReference.Replace(originalDevice.Acronym + "-", outputStreamDevice.Acronym + "-");
                            OutputStreamMeasurement.Save(database, measurement);
                        }
                    }
                }

                return("OutputStreamDevice information saved successfully");
            }
            finally
            {
                if (createdConnection && database != null)
                {
                    database.Dispose();
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads <see cref="OutputStreamMeasurement"/> information as an <see cref="ObservableCollection{T}"/> style list.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="keys">Keys of the measurement to be loaded from the database</param>
        /// <returns>Collection of <see cref="OutputStreamMeasurement"/>.</returns>
        public static ObservableCollection <OutputStreamMeasurement> Load(AdoDataConnection database, IList <int> keys)
        {
            bool createdConnection = false;

            try
            {
                createdConnection = CreateConnection(ref database);

                string query;
                string commaSeparatedKeys;

                OutputStreamMeasurement[] outputStreamMeasurementList = null;
                DataTable outputStreamMeasurementTable;
                int       id;

                if ((object)keys != null && keys.Count > 0)
                {
                    commaSeparatedKeys = keys.Select(key => "" + key.ToString() + "").Aggregate((str1, str2) => str1 + "," + str2);
                    query = string.Format("SELECT NodeID, AdapterID, ID, HistorianID, PointID, SignalReference, SourcePointTag, HistorianAcronym " +
                                          "FROM OutputStreamMeasurementDetail WHERE ID IN ({0})", commaSeparatedKeys);

                    outputStreamMeasurementTable = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout);
                    outputStreamMeasurementList  = new OutputStreamMeasurement[outputStreamMeasurementTable.Rows.Count];

                    foreach (DataRow row in outputStreamMeasurementTable.Rows)
                    {
                        id = row.ConvertField <int>("ID");

                        outputStreamMeasurementList[keys.IndexOf(id)] = new OutputStreamMeasurement()
                        {
                            NodeID             = database.Guid(row, "NodeID"),
                            AdapterID          = row.ConvertField <int>("AdapterID"),
                            ID                 = id,
                            HistorianID        = row.ConvertNullableField <int>("HistorianID"),
                            PointID            = row.ConvertField <int>("PointID"),
                            SignalReference    = row.Field <string>("SignalReference"),
                            m_sourcePointTag   = row.Field <string>("SourcePointTag"),
                            m_historianAcronym = row.Field <string>("HistorianAcronym")
                        };
                    }
                }

                return(new ObservableCollection <OutputStreamMeasurement>(outputStreamMeasurementList ?? new OutputStreamMeasurement[0]));
            }
            finally
            {
                if (createdConnection && database != null)
                {
                    database.Dispose();
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Saves <see cref="OutputStreamMeasurement"/> information to database.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="outputStreamMeasurement">Information about <see cref="OutputStreamMeasurement"/>.</param>
        /// <returns>String, for display use, indicating success.</returns>
        public static string Save(AdoDataConnection database, OutputStreamMeasurement outputStreamMeasurement)
        {
            bool   createdConnection = false;
            string query;

            try
            {
                createdConnection = CreateConnection(ref database);

                if (outputStreamMeasurement.ID == 0)
                {
                    query = database.ParameterizedQueryString("INSERT INTO OutputStreamMeasurement (NodeID, AdapterID, HistorianID, PointID, SignalReference, " +
                                                              " UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8})",
                                                              "nodeID", "adapterID", "historianID", "pointID", "signalReference", "updatedBy", "updatedOn", "createdBy",
                                                              "createdOn");

                    database.Connection.ExecuteNonQuery(query, DefaultTimeout, outputStreamMeasurement.NodeID == Guid.Empty ? database.CurrentNodeID() : database.Guid(outputStreamMeasurement.NodeID),
                                                        outputStreamMeasurement.AdapterID, outputStreamMeasurement.HistorianID.ToNotNull(), outputStreamMeasurement.PointID, outputStreamMeasurement.SignalReference,
                                                        CommonFunctions.CurrentUser, database.UtcNow, CommonFunctions.CurrentUser, database.UtcNow);
                }
                else
                {
                    query = database.ParameterizedQueryString("UPDATE OutputStreamMeasurement SET NodeID = {0}, AdapterID = {1}, HistorianID = {2}, PointID = {3}, " +
                                                              "SignalReference = {4}, UpdatedBy = {5}, UpdatedOn = {6} WHERE ID = {7}", "nodeID", "adapterID",
                                                              "historianID", "pointID", "signalReference", "updatedBy", "updatedOn", "id");

                    database.Connection.ExecuteNonQuery(query, DefaultTimeout, database.Guid(outputStreamMeasurement.NodeID), outputStreamMeasurement.AdapterID,
                                                        outputStreamMeasurement.HistorianID.ToNotNull(), outputStreamMeasurement.PointID, outputStreamMeasurement.SignalReference,
                                                        CommonFunctions.CurrentUser, database.UtcNow, outputStreamMeasurement.ID);
                }

                return("OutputStreamMeasurement information saved successfully");
            }
            finally
            {
                if (createdConnection && database != null)
                {
                    database.Dispose();
                }
            }
        }
Ejemplo n.º 5
0
        private static OutputStreamMeasurement GetOutputMeasurementDetails(AdoDataConnection database, string signalReference, int adapterID)
        {
            bool createdConnection = false;

            try
            {
                createdConnection = CreateConnection(ref database);

                string  query = database.ParameterizedQueryString("SELECT * FROM OutputStreamMeasurement WHERE SignalReference = {0} AND AdapterID = {1}", "signalReference", "adapterID");
                DataRow row   = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout, signalReference, adapterID).Rows[0];

                OutputStreamMeasurement outputStreamMeasurement = new OutputStreamMeasurement
                {
                    NodeID          = row.ConvertField <Guid>("NodeID"),
                    AdapterID       = row.Field <int>("AdapterID"),
                    ID              = row.Field <int>("ID"),
                    HistorianID     = row.Field <int>("HistorianID"),
                    PointID         = row.Field <int>("PointID"),
                    SignalReference = row.ConvertField <string>("SignalReference"),
                    CreatedOn       = row.ConvertField <DateTime>("CreatedOn"),
                    CreatedBy       = row.Field <string>("CreatedBy"),
                    UpdatedOn       = row.ConvertField <DateTime>("UpdatedOn"),
                    UpdatedBy       = row.Field <string>("UpdatedBy")
                };

                return(outputStreamMeasurement);
            }
            catch (Exception ex)
            {
                CommonFunctions.LogException(database, "OutputStreamDevicePhasor.GetOutputMeasurementDetails", ex);
                throw;
            }
            finally
            {
                if (createdConnection && database != null)
                {
                    database.Dispose();
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Adds multiple devices to output steam.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="outputStreamID">ID of the output stream to which devices needs to be added.</param>
        /// <param name="devices">Collection of devices to be added.</param>
        /// <param name="addDigitals">Boolean flag indicating if analogs should be added.</param>
        /// <param name="addAnalogs">Boolean flag indicating if digirals should be added.</param>
        /// <returns>String, for display use, indicating success.</returns>
        public static string AddDevices(AdoDataConnection database, int outputStreamID, ObservableCollection <Device> devices, bool addDigitals, bool addAnalogs)
        {
            bool createdConnection = false;

            try
            {
                createdConnection = CreateConnection(ref database);

                foreach (Device device in devices)
                {
                    OutputStreamDevice outputStreamDevice = new OutputStreamDevice();
                    outputStreamDevice.NodeID     = device.NodeID;
                    outputStreamDevice.AdapterID  = outputStreamID;
                    outputStreamDevice.Acronym    = device.Acronym.Substring(device.Acronym.LastIndexOf("!", StringComparison.Ordinal) + 1);
                    outputStreamDevice.BpaAcronym = string.Empty;
                    outputStreamDevice.Name       = device.Name;
                    outputStreamDevice.LoadOrder  = device.LoadOrder;
                    outputStreamDevice.Enabled    = true;
                    outputStreamDevice.IDCode     = device.AccessID;
                    Save(database, outputStreamDevice);

                    outputStreamDevice = GetOutputStreamDevice(database, "WHERE Acronym = '" + outputStreamDevice.Acronym + "' AND AdapterID = " + outputStreamID);

                    if ((object)outputStreamDevice != null)
                    {
                        IList <int> keys = Phasor.LoadKeys(database, device.ID);
                        ObservableCollection <Phasor> phasors = Phasor.Load(database, keys);
                        foreach (Phasor phasor in phasors)
                        {
                            OutputStreamDevicePhasor outputStreamDevicePhasor = new OutputStreamDevicePhasor();
                            outputStreamDevicePhasor.NodeID = device.NodeID;
                            outputStreamDevicePhasor.OutputStreamDeviceID = outputStreamDevice.ID;
                            outputStreamDevicePhasor.Label     = phasor.Label;
                            outputStreamDevicePhasor.Type      = phasor.Type;
                            outputStreamDevicePhasor.Phase     = phasor.Phase;
                            outputStreamDevicePhasor.LoadOrder = phasor.SourceIndex;
                            OutputStreamDevicePhasor.Save(database, outputStreamDevicePhasor);
                        }

                        ObservableCollection <Measurement> measurements = Measurement.Load(database, device.ID);
                        int analogIndex = 0;
                        foreach (Measurement measurement in measurements)
                        {
                            if (measurement.SignalAcronym != "STAT" && measurement.SignalAcronym != "QUAL")
                            {
                                measurement.SignalReference = measurement.SignalReference.Substring(measurement.SignalReference.LastIndexOf("!", StringComparison.Ordinal) + 1);

                                if ((measurement.SignalAcronym != "ALOG" && measurement.SignalAcronym != "DIGI") || (measurement.SignalAcronym == "ALOG" && addAnalogs) || (measurement.SignalAcronym == "DIGI" && addDigitals))
                                {
                                    OutputStreamMeasurement outputStreamMeasurement = new OutputStreamMeasurement();
                                    outputStreamMeasurement.NodeID          = device.NodeID;
                                    outputStreamMeasurement.AdapterID       = outputStreamID;
                                    outputStreamMeasurement.HistorianID     = measurement.HistorianID;
                                    outputStreamMeasurement.PointID         = measurement.PointID;
                                    outputStreamMeasurement.SignalReference = measurement.SignalReference;
                                    OutputStreamMeasurement.Save(database, outputStreamMeasurement);
                                }

                                if (addAnalogs && measurement.SignalAcronym == "ALOG")
                                {
                                    OutputStreamDeviceAnalog outputStreamDeviceAnalog = new OutputStreamDeviceAnalog();
                                    outputStreamDeviceAnalog.NodeID = device.NodeID;
                                    outputStreamDeviceAnalog.OutputStreamDeviceID = outputStreamDevice.ID;
                                    outputStreamDeviceAnalog.Label = string.IsNullOrEmpty(measurement.AlternateTag) ? device.Acronym.Length > 12 ? device.Acronym.Substring(0, 12) + ":A" + analogIndex : device.Acronym + ":A" + analogIndex : measurement.AlternateTag; // measurement.PointTag;

                                    int charIndex = measurement.SignalReference.LastIndexOf("-", StringComparison.Ordinal);
                                    int signalIndex;

                                    if (charIndex >= 0 && charIndex + 3 < measurement.SignalReference.Length && int.TryParse(measurement.SignalReference.Substring(charIndex + 3), out signalIndex))
                                    {
                                        outputStreamDeviceAnalog.LoadOrder = signalIndex;
                                    }
                                    else
                                    {
                                        outputStreamDeviceAnalog.LoadOrder = 999;
                                    }

                                    OutputStreamDeviceAnalog.Save(database, outputStreamDeviceAnalog);
                                    analogIndex++;
                                }
                                else if (addDigitals && measurement.SignalAcronym == "DIGI")
                                {
                                    OutputStreamDeviceDigital outputStreamDeviceDigital = new OutputStreamDeviceDigital();
                                    outputStreamDeviceDigital.NodeID = device.NodeID;
                                    outputStreamDeviceDigital.OutputStreamDeviceID = outputStreamDevice.ID;
                                    outputStreamDeviceDigital.Label = string.IsNullOrEmpty(measurement.AlternateTag) ? DefaultDigitalLabel : measurement.AlternateTag;     // measurement.PointTag;

                                    int charIndex = measurement.SignalReference.LastIndexOf("-", StringComparison.Ordinal);
                                    int signalIndex;

                                    if (charIndex >= 0 && charIndex + 3 < measurement.SignalReference.Length && int.TryParse(measurement.SignalReference.Substring(charIndex + 3), out signalIndex))
                                    {
                                        outputStreamDeviceDigital.LoadOrder = signalIndex;
                                    }
                                    else
                                    {
                                        outputStreamDeviceDigital.LoadOrder = 999;
                                    }

                                    OutputStreamDeviceDigital.Save(database, outputStreamDeviceDigital);
                                }
                            }
                        }
                    }
                }

                return("Output Stream Device(s) added successfully!");
            }
            finally
            {
                if (createdConnection && database != null)
                {
                    database.Dispose();
                }
            }
        }
Ejemplo n.º 7
0
        private static OutputStreamMeasurement GetOutputMeasurementDetails(AdoDataConnection database, string signalReference, int adapterID)
        {
            bool createdConnection = false;

            try
            {
                createdConnection = CreateConnection(ref database);

                string query = database.ParameterizedQueryString("SELECT * FROM OutputStreamMeasurement WHERE SignalReference = {0} AND AdapterID = {1}", "signalReference", "adapterID");
                DataRow row = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout, signalReference, adapterID).Rows[0];

                OutputStreamMeasurement outputStreamMeasurement = new OutputStreamMeasurement
                    {
                    NodeID = row.ConvertField<Guid>("NodeID"),
                    AdapterID = row.Field<int>("AdapterID"),
                    ID = row.Field<int>("ID"),
                    HistorianID = row.Field<int>("HistorianID"),
                    PointID = row.Field<int>("PointID"),
                    SignalReference = row.ConvertField<string>("SignalReference"),
                    CreatedOn = row.ConvertField<DateTime>("CreatedOn"),
                    CreatedBy = row.Field<string>("CreatedBy"),
                    UpdatedOn = row.ConvertField<DateTime>("UpdatedOn"),
                    UpdatedBy = row.Field<string>("UpdatedBy")
                };

                return outputStreamMeasurement;
            }
            catch (Exception ex)
            {
                CommonFunctions.LogException(database, "OutputStreamDevicePhasor.GetOutputMeasurementDetails", ex);
                throw;
            }
            finally
            {
                if (createdConnection && database != null)
                    database.Dispose();
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Deletes specified <see cref="OutputStreamDevicePhasor"/> record and its associated measurements from database.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="outputStreamDevicePhasorID">ID of the record to be deleted.</param>
        /// <returns>String, for display use, indicating success.</returns>
        public static string Delete(AdoDataConnection database, int outputStreamDevicePhasorID)
        {
            bool createdConnection = false;

            int adapterID;
            int outputStreamDeviceID;
            int deletedSignalReferenceIndex;
            int presentDevicePhasorCount;

            string angleSignalReference;
            string angleSignalReferenceBase;
            string magnitudeSignalReference;
            string magnitudeSignalReferenceBase;

            string previousAngleSignalReference;
            string previousMagnitudeSignalReference;
            string nextAngleSignalReference        = string.Empty;
            string nextMagnitudeSignalReference    = string.Empty;
            string lastAffectedMeasurementsMessage = string.Empty;

            try
            {
                createdConnection = CreateConnection(ref database);

                // Setup current user context for any delete triggers
                CommonFunctions.SetCurrentUserContext(database);

                GetDeleteMeasurementDetails(database, outputStreamDevicePhasorID, out angleSignalReference, out magnitudeSignalReference, out adapterID, out outputStreamDeviceID);

                // Delete angle/magnitude of measurements if they exist
                database.Connection.ExecuteNonQuery(database.ParameterizedQueryString("DELETE FROM OutputStreamMeasurement WHERE SignalReference = {0} AND AdapterID = {1}", "signalReference", "adapterID"), DefaultTimeout, angleSignalReference, adapterID);
                database.Connection.ExecuteNonQuery(database.ParameterizedQueryString("DELETE FROM OutputStreamMeasurement WHERE SignalReference = {0} AND AdapterID = {1}", "signalReference", "adapterID"), DefaultTimeout, magnitudeSignalReference, adapterID);
                presentDevicePhasorCount = Convert.ToInt32(database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT COUNT(*) FROM OutputStreamDevicePhasor WHERE OutputStreamDeviceID = {0}", "outputStreamDeviceID"), DefaultTimeout, outputStreamDeviceID));

                // Using signal reference angle/mag deleted build the next signal reference (increment by 1)
                int.TryParse(Regex.Match(magnitudeSignalReference, @"\d+$").Value, out deletedSignalReferenceIndex);
                angleSignalReferenceBase     = Regex.Replace(angleSignalReference, @"\d+$", "");
                magnitudeSignalReferenceBase = Regex.Replace(magnitudeSignalReference, @"\d+$", "");

                for (int i = deletedSignalReferenceIndex; i < presentDevicePhasorCount; i++)
                {
                    // We will be modifying the measurements with signal reference index i+1 to have signal refrence index i.
                    previousAngleSignalReference     = string.Format("{0}{1}", angleSignalReferenceBase, i);
                    nextAngleSignalReference         = string.Format("{0}{1}", angleSignalReferenceBase, i + 1);
                    previousMagnitudeSignalReference = string.Format("{0}{1}", magnitudeSignalReferenceBase, i);
                    nextMagnitudeSignalReference     = string.Format("{0}{1}", magnitudeSignalReferenceBase, i + 1);

                    // For angle...
                    // Obtain details of measurements after the deleted measurements, then modify the signal reference (decrement by 1) and put it back
                    OutputStreamMeasurement outputStreamMeasurement = GetOutputMeasurementDetails(database, nextAngleSignalReference, adapterID);
                    outputStreamMeasurement.SignalReference = previousAngleSignalReference;
                    OutputStreamMeasurement.Save(database, outputStreamMeasurement);

                    // For magnitude...
                    outputStreamMeasurement = GetOutputMeasurementDetails(database, nextMagnitudeSignalReference, adapterID);
                    outputStreamMeasurement.SignalReference = previousMagnitudeSignalReference;
                    OutputStreamMeasurement.Save(database, outputStreamMeasurement);
                }

                database.Connection.ExecuteNonQuery(database.ParameterizedQueryString("DELETE FROM OutputStreamDevicePhasor WHERE ID = {0}", "outputStreamDevicePhasorID"), DefaultTimeout, outputStreamDevicePhasorID);

                return("OutputStreamDevicePhasor deleted successfully");
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrEmpty(nextMagnitudeSignalReference))
                {
                    lastAffectedMeasurementsMessage = string.Format("{0}(Last affected measurements: {1} {2})", Environment.NewLine, nextMagnitudeSignalReference, nextAngleSignalReference);
                }

                CommonFunctions.LogException(database, "OutputStreamDevicePhasor.Delete", ex);
                MessageBoxResult dialogResult = MessageBox.Show(string.Format("Could not delete or modify measurements.{0}Do you still wish to delete this Phasor?{1}", Environment.NewLine, lastAffectedMeasurementsMessage), "", MessageBoxButton.YesNo);

                if (dialogResult == MessageBoxResult.Yes)
                {
                    database.Connection.ExecuteNonQuery(database.ParameterizedQueryString("DELETE FROM OutputStreamDevicePhasor WHERE ID = {0}", "outputStreamDevicePhasorID"), DefaultTimeout, outputStreamDevicePhasorID);
                    return("OutputStreamDevicePhasor deleted successfully but failed to modify all measurements ");
                }
                else
                {
                    Exception exception = ex.InnerException ?? ex;
                    return(string.Format("Delete OutputStreamDevicePhasor was unsuccessful: {0}", exception.Message));
                }
            }
            finally
            {
                if (createdConnection && database != null)
                {
                    database.Dispose();
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Creates and saves new <see cref="OutputStreamMeasurement"/> into the database.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="outputStreamID">ID of the output stream.</param>
        /// <param name="measurements">Collection of measurements to be added.</param>
        /// <returns>String, for display use, indicating success.</returns>
        public static string AddMeasurements(AdoDataConnection database, int outputStreamID, ObservableCollection<Measurement> measurements)
        {
            bool createdConnection = false;

            try
            {
                createdConnection = CreateConnection(ref database);

                foreach (Measurement measurement in measurements)
                {
                    OutputStreamMeasurement outputStreamMeasurement = new OutputStreamMeasurement
                    {
                        NodeID = CommonFunctions.CurrentNodeID(),
                        AdapterID = outputStreamID,
                        HistorianID = measurement.HistorianID,
                        PointID = measurement.PointID,
                        SignalReference = measurement.SignalReference
                    };

                    Save(database, outputStreamMeasurement);
                }

                return "Output stream measurements added successfully.";
            }
            finally
            {
                if (createdConnection && database != null)
                    database.Dispose();
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Saves <see cref="OutputStreamMeasurement"/> information to database.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="outputStreamMeasurement">Information about <see cref="OutputStreamMeasurement"/>.</param>        
        /// <returns>String, for display use, indicating success.</returns>
        public static string Save(AdoDataConnection database, OutputStreamMeasurement outputStreamMeasurement)
        {
            bool createdConnection = false;
            string query;

            try
            {
                createdConnection = CreateConnection(ref database);

                if (outputStreamMeasurement.ID == 0)
                {
                    query = database.ParameterizedQueryString("INSERT INTO OutputStreamMeasurement (NodeID, AdapterID, HistorianID, PointID, SignalReference, " +
                        " UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8})",
                        "nodeID", "adapterID", "historianID", "pointID", "signalReference", "updatedBy", "updatedOn", "createdBy",
                        "createdOn");

                    database.Connection.ExecuteNonQuery(query, DefaultTimeout, outputStreamMeasurement.NodeID == Guid.Empty ? database.CurrentNodeID() : database.Guid(outputStreamMeasurement.NodeID),
                        outputStreamMeasurement.AdapterID, outputStreamMeasurement.HistorianID.ToNotNull(), outputStreamMeasurement.PointID, outputStreamMeasurement.SignalReference,
                        CommonFunctions.CurrentUser, database.UtcNow, CommonFunctions.CurrentUser, database.UtcNow);
                }
                else
                {
                    query = database.ParameterizedQueryString("UPDATE OutputStreamMeasurement SET NodeID = {0}, AdapterID = {1}, HistorianID = {2}, PointID = {3}, " +
                        "SignalReference = {4}, UpdatedBy = {5}, UpdatedOn = {6} WHERE ID = {7}", "nodeID", "adapterID",
                        "historianID", "pointID", "signalReference", "updatedBy", "updatedOn", "id");

                    database.Connection.ExecuteNonQuery(query, DefaultTimeout, database.Guid(outputStreamMeasurement.NodeID), outputStreamMeasurement.AdapterID,
                        outputStreamMeasurement.HistorianID.ToNotNull(), outputStreamMeasurement.PointID, outputStreamMeasurement.SignalReference,
                        CommonFunctions.CurrentUser, database.UtcNow, outputStreamMeasurement.ID);
                }

                return "OutputStreamMeasurement information saved successfully";
            }
            finally
            {
                if (createdConnection && database != null)
                    database.Dispose();
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Loads <see cref="OutputStreamMeasurement"/> information as an <see cref="ObservableCollection{T}"/> style list.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="keys">Keys of the measurement to be loaded from the database</param>
        /// <returns>Collection of <see cref="OutputStreamMeasurement"/>.</returns>
        public static ObservableCollection<OutputStreamMeasurement> Load(AdoDataConnection database, IList<int> keys)
        {
            bool createdConnection = false;

            try
            {
                createdConnection = CreateConnection(ref database);

                string query;
                string commaSeparatedKeys;

                OutputStreamMeasurement[] outputStreamMeasurementList = null;
                DataTable outputStreamMeasurementTable;
                int id;

                if ((object)keys != null && keys.Count > 0)
                {
                    commaSeparatedKeys = keys.Select(key => "" + key.ToString() + "").Aggregate((str1, str2) => str1 + "," + str2);
                    query = string.Format("SELECT NodeID, AdapterID, ID, HistorianID, PointID, SignalReference, SourcePointTag, HistorianAcronym " +
                        "FROM OutputStreamMeasurementDetail WHERE ID IN ({0})", commaSeparatedKeys);

                    outputStreamMeasurementTable = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout);
                    outputStreamMeasurementList = new OutputStreamMeasurement[outputStreamMeasurementTable.Rows.Count];

                    foreach (DataRow row in outputStreamMeasurementTable.Rows)
                    {
                        id = row.ConvertField<int>("ID");

                        outputStreamMeasurementList[keys.IndexOf(id)] = new OutputStreamMeasurement()
                        {
                            NodeID = database.Guid(row, "NodeID"),
                            AdapterID = row.ConvertField<int>("AdapterID"),
                            ID = id,
                            HistorianID = row.ConvertNullableField<int>("HistorianID"),
                            PointID = row.ConvertField<int>("PointID"),
                            SignalReference = row.Field<string>("SignalReference"),
                            m_sourcePointTag = row.Field<string>("SourcePointTag"),
                            m_historianAcronym = row.Field<string>("HistorianAcronym")
                        };
                    }
                }

                return new ObservableCollection<OutputStreamMeasurement>(outputStreamMeasurementList ?? new OutputStreamMeasurement[0]);
            }
            finally
            {
                if (createdConnection && database != null)
                    database.Dispose();
            }
        }
        /// <summary>
        /// Adds multiple devices to output steam.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="outputStreamID">ID of the output stream to which devices needs to be added.</param>
        /// <param name="devices">Collection of devices to be added.</param>
        /// <param name="addDigitals">Boolean flag indicating if analogs should be added.</param>
        /// <param name="addAnalogs">Boolean flag indicating if digirals should be added.</param>
        /// <returns>String, for display use, indicating success.</returns>
        public static string AddDevices(AdoDataConnection database, int outputStreamID, ObservableCollection<Device> devices, bool addDigitals, bool addAnalogs)
        {
            bool createdConnection = false;

            try
            {
                createdConnection = CreateConnection(ref database);

                foreach (Device device in devices)
                {
                    OutputStreamDevice outputStreamDevice = new OutputStreamDevice();
                    outputStreamDevice.NodeID = device.NodeID;
                    outputStreamDevice.AdapterID = outputStreamID;
                    outputStreamDevice.Acronym = device.Acronym.Substring(device.Acronym.LastIndexOf("!", StringComparison.Ordinal) + 1);
                    outputStreamDevice.BpaAcronym = string.Empty;
                    outputStreamDevice.Name = device.Name;
                    outputStreamDevice.LoadOrder = device.LoadOrder;
                    outputStreamDevice.Enabled = true;
                    outputStreamDevice.IDCode = device.AccessID;
                    Save(database, outputStreamDevice);

                    outputStreamDevice = GetOutputStreamDevice(database, "WHERE Acronym = '" + outputStreamDevice.Acronym + "' AND AdapterID = " + outputStreamID);

                    if ((object)outputStreamDevice != null)
                    {
                        IList<int> keys = Phasor.LoadKeys(database, device.ID);
                        ObservableCollection<Phasor> phasors = Phasor.Load(database, keys);
                        foreach (Phasor phasor in phasors)
                        {
                            OutputStreamDevicePhasor outputStreamDevicePhasor = new OutputStreamDevicePhasor();
                            outputStreamDevicePhasor.NodeID = device.NodeID;
                            outputStreamDevicePhasor.OutputStreamDeviceID = outputStreamDevice.ID;
                            outputStreamDevicePhasor.Label = phasor.Label;
                            outputStreamDevicePhasor.Type = phasor.Type;
                            outputStreamDevicePhasor.Phase = phasor.Phase;
                            outputStreamDevicePhasor.LoadOrder = phasor.SourceIndex;
                            OutputStreamDevicePhasor.Save(database, outputStreamDevicePhasor);
                        }

                        ObservableCollection<Measurement> measurements = Measurement.Load(database, device.ID);
                        int analogIndex = 0;
                        foreach (Measurement measurement in measurements)
                        {
                            if (measurement.SignalAcronym != "STAT" && measurement.SignalAcronym != "QUAL")
                            {
                                measurement.SignalReference = measurement.SignalReference.Substring(measurement.SignalReference.LastIndexOf("!", StringComparison.Ordinal) + 1);

                                if ((measurement.SignalAcronym != "ALOG" && measurement.SignalAcronym != "DIGI") || (measurement.SignalAcronym == "ALOG" && addAnalogs) || (measurement.SignalAcronym == "DIGI" && addDigitals))
                                {
                                    OutputStreamMeasurement outputStreamMeasurement = new OutputStreamMeasurement();
                                    outputStreamMeasurement.NodeID = device.NodeID;
                                    outputStreamMeasurement.AdapterID = outputStreamID;
                                    outputStreamMeasurement.HistorianID = measurement.HistorianID;
                                    outputStreamMeasurement.PointID = measurement.PointID;
                                    outputStreamMeasurement.SignalReference = measurement.SignalReference;
                                    OutputStreamMeasurement.Save(database, outputStreamMeasurement);
                                }

                                if (addAnalogs && measurement.SignalAcronym == "ALOG")
                                {
                                    OutputStreamDeviceAnalog outputStreamDeviceAnalog = new OutputStreamDeviceAnalog();
                                    outputStreamDeviceAnalog.NodeID = device.NodeID;
                                    outputStreamDeviceAnalog.OutputStreamDeviceID = outputStreamDevice.ID;
                                    outputStreamDeviceAnalog.Label = string.IsNullOrEmpty(measurement.AlternateTag) ? device.Acronym.Length > 12 ? device.Acronym.Substring(0, 12) + ":A" + analogIndex : device.Acronym + ":A" + analogIndex : measurement.AlternateTag; // measurement.PointTag;                                    

                                    int charIndex = measurement.SignalReference.LastIndexOf("-", StringComparison.Ordinal);
                                    int signalIndex;

                                    if (charIndex >= 0 && charIndex + 3 < measurement.SignalReference.Length && int.TryParse(measurement.SignalReference.Substring(charIndex + 3), out signalIndex))
                                        outputStreamDeviceAnalog.LoadOrder = signalIndex;
                                    else
                                        outputStreamDeviceAnalog.LoadOrder = 999;

                                    OutputStreamDeviceAnalog.Save(database, outputStreamDeviceAnalog);
                                    analogIndex++;
                                }
                                else if (addDigitals && measurement.SignalAcronym == "DIGI")
                                {
                                    OutputStreamDeviceDigital outputStreamDeviceDigital = new OutputStreamDeviceDigital();
                                    outputStreamDeviceDigital.NodeID = device.NodeID;
                                    outputStreamDeviceDigital.OutputStreamDeviceID = outputStreamDevice.ID;
                                    outputStreamDeviceDigital.Label = string.IsNullOrEmpty(measurement.AlternateTag) ? DefaultDigitalLabel : measurement.AlternateTag;     // measurement.PointTag;

                                    int charIndex = measurement.SignalReference.LastIndexOf("-", StringComparison.Ordinal);
                                    int signalIndex;

                                    if (charIndex >= 0 && charIndex + 3 < measurement.SignalReference.Length && int.TryParse(measurement.SignalReference.Substring(charIndex + 3), out signalIndex))
                                        outputStreamDeviceDigital.LoadOrder = signalIndex;
                                    else
                                        outputStreamDeviceDigital.LoadOrder = 999;

                                    OutputStreamDeviceDigital.Save(database, outputStreamDeviceDigital);
                                }
                            }
                        }
                    }
                }

                return "Output Stream Device(s) added successfully!";
            }
            finally
            {
                if (createdConnection && database != null)
                    database.Dispose();
            }
        }