Exemplo n.º 1
0
        /// <summary>
        /// Saves <see cref="Company"/> information to database.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="powerCalculation">Information about <see cref="PowerCalculation"/>.</param>
        /// <returns>String, for display use, indicating success.</returns>
        public static string Save(AdoDataConnection database, PowerCalculation powerCalculation)
        {
            bool   createdConnection = false;
            string query;

            try
            {
                createdConnection = CreateConnection(ref database);

                if (powerCalculation.ID == 0)
                {
                    query = "INSERT INTO PowerCalculation (CircuitDescription, VoltageAngleSignalID, VoltageMagSignalID, CurrentAngleSignalID, CurrentMagSignalID, ActivePowerOutputSignalID, ReactivePowerOutputSignalID, ApparentPowerOutputSignalID, Enabled, NodeID) " +
                            "VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9})";

                    database.ExecuteNonQuery(DefaultTimeout, query, powerCalculation.CircuitDescription, powerCalculation.VoltagePhasor.AngleMeasurement.SignalID,
                                             powerCalculation.VoltagePhasor.MagnitudeMeasurement.SignalID, powerCalculation.CurrentPhasor.AngleMeasurement.SignalID, powerCalculation.CurrentPhasor.MagnitudeMeasurement.SignalID,
                                             (object)powerCalculation.ActivePowerOutputMeasurement?.SignalID ?? DBNull.Value, (object)powerCalculation.ReactivePowerOutputMeasurement?.SignalID ?? DBNull.Value, (object)powerCalculation.ApparentPowerOutputMeasurement?.SignalID ?? DBNull.Value,
                                             powerCalculation.Enabled, database.CurrentNodeID());
                }
                else
                {
                    query = "UPDATE PowerCalculation SET CircuitDescription = {0}, VoltageAngleSignalID = {1}, VoltageMagSignalID = {2}, CurrentAngleSignalID = {3}, CurrentMagSignalID = {4}, " +
                            "activePowerOutputSignalID = {5}, ReactivePowerOutputSignalID = {6}, ApparentPowerOutputSignalID = {7}, Enabled = {8} WHERE ID = {9}";

                    database.ExecuteNonQuery(DefaultTimeout, query, powerCalculation.CircuitDescription,
                                             database.Guid(powerCalculation.VoltagePhasor.AngleMeasurement.SignalID), powerCalculation.VoltagePhasor.MagnitudeMeasurement.SignalID, powerCalculation.CurrentPhasor.AngleMeasurement.SignalID, powerCalculation.CurrentPhasor.MagnitudeMeasurement.SignalID,
                                             (object)powerCalculation.ActivePowerOutputMeasurement?.SignalID ?? DBNull.Value, (object)powerCalculation.ReactivePowerOutputMeasurement?.SignalID ?? DBNull.Value, (object)powerCalculation.ApparentPowerOutputMeasurement?.SignalID ?? DBNull.Value, powerCalculation.Enabled, powerCalculation.ID);
                }

                return("Power Calculation information saved successfully");
            }
            finally
            {
                if (createdConnection && database != null)
                {
                    database.Dispose();
                }
            }
        }
Exemplo n.º 2
0
		/// <summary>
		/// Saves <see cref="Company"/> information to database.
		/// </summary>
		/// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
		/// <param name="powerCalculation">Information about <see cref="PowerCalculation"/>.</param>        
		/// <returns>String, for display use, indicating success.</returns>
		public static string Save(AdoDataConnection database, PowerCalculation powerCalculation)
		{
			bool createdConnection = false;
			string query;

			try
			{
				createdConnection = CreateConnection(ref database);

				if (powerCalculation.ID == 0)
				{
					query = "INSERT INTO PowerCalculation (CircuitDescription, VoltageAngleSignalID, VoltageMagSignalID, CurrentAngleSignalID, CurrentMagSignalID, ActivePowerOutputSignalID, ReactivePowerOutputSignalID, ApparentPowerOutputSignalID, Enabled, NodeID) " +
						"VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9})";

					database.ExecuteNonQuery(DefaultTimeout, query, powerCalculation.CircuitDescription, powerCalculation.VoltagePhasor.AngleMeasurement.SignalID,
						powerCalculation.VoltagePhasor.MagnitudeMeasurement.SignalID, powerCalculation.CurrentPhasor.AngleMeasurement.SignalID, powerCalculation.CurrentPhasor.MagnitudeMeasurement.SignalID,
						(object)powerCalculation.ActivePowerOutputMeasurement?.SignalID ?? DBNull.Value, (object)powerCalculation.ReactivePowerOutputMeasurement?.SignalID ?? DBNull.Value, (object)powerCalculation.ApparentPowerOutputMeasurement?.SignalID ?? DBNull.Value,
						powerCalculation.Enabled, database.CurrentNodeID());
				}
				else
				{
					query = "UPDATE PowerCalculation SET CircuitDescription = {0}, VoltageAngleSignalID = {1}, VoltageMagSignalID = {2}, CurrentAngleSignalID = {3}, CurrentMagSignalID = {4}, " +
						"activePowerOutputSignalID = {5}, ReactivePowerOutputSignalID = {6}, ApparentPowerOutputSignalID = {7}, Enabled = {8} WHERE ID = {9}";

					database.ExecuteNonQuery(DefaultTimeout, query, powerCalculation.CircuitDescription,
						database.Guid(powerCalculation.VoltagePhasor.AngleMeasurement.SignalID), powerCalculation.VoltagePhasor.MagnitudeMeasurement.SignalID, powerCalculation.CurrentPhasor.AngleMeasurement.SignalID, powerCalculation.CurrentPhasor.MagnitudeMeasurement.SignalID,
						(object)powerCalculation.ActivePowerOutputMeasurement?.SignalID ?? DBNull.Value, (object)powerCalculation.ReactivePowerOutputMeasurement?.SignalID ?? DBNull.Value, (object)powerCalculation.ApparentPowerOutputMeasurement?.SignalID ?? DBNull.Value, powerCalculation.Enabled, powerCalculation.ID);
				}

				return "Power Calculation information saved successfully";
			}
			finally
			{
				if (createdConnection && database != null)
					database.Dispose();
			}
		}
Exemplo n.º 3
0
        /// <summary>
        /// Loads <see cref="PowerCalculation"/> 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="PowerCalculation"/>.</returns>
        public static ObservableCollection <PowerCalculation> Load(AdoDataConnection database, IList <int> keys)
        {
            var createdConnection = false;

            try
            {
                createdConnection = CreateConnection(ref database);

                PowerCalculation[] calculationList = null;

                if (keys != null && keys.Count > 0)
                {
                    var commaSeparatedKeys = keys.Select(key => key.ToString()).Aggregate((str1, str2) => str1 + "," + str2);
                    var query = string.Format("select pc.ID, pc.CircuitDescription, pc.VoltageAngleSignalID, pc.VoltageMagSignalID, pc.CurrentAngleSignalID, " +
                                              "       pc.CurrentMagSignalID, pc.ActivePowerOutputSignalID, pc.ReactivePowerOutputSignalID, pc.ApparentPowerOutputSignalID, pc.Enabled, " +
                                              "	   pc.nodeid, vp.id voltagePhasorID, cp.id as currentPhasorID " +
                                              "from powercalculation pc " +
                                              "     left join measurement vm " +
                                              "	 on pc.voltagemagsignalid=vm.signalid " +
                                              "	 left join measurement cm " +
                                              "	 on pc.currentmagsignalid=cm.signalid " +
                                              "	 left join phasor vp " +
                                              "	 on vm.deviceid=vp.deviceid and vm.phasorsourceindex=vp.sourceindex " +
                                              "	 left join phasor cp " +
                                              " on cm.deviceid=cp.deviceid and cm.phasorsourceindex=cp.sourceindex WHERE pc.ID IN ({0})", commaSeparatedKeys);
                    var calculationTable = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout);
                    calculationList = new PowerCalculation[calculationTable.Rows.Count];

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

                        var voltageAngle     = database.Guid(row, "VoltageAngleSignalID");
                        var voltageMagnitude = database.Guid(row, "VoltageMagSignalID");
                        var currentAngle     = database.Guid(row, "CurrentAngleSignalID");
                        var currentMagnitude = database.Guid(row, "CurrentMagSignalID");

                        Guid?activePower = null;
                        if (!row.IsNull("ActivePowerOutputSignalID"))
                        {
                            activePower = database.Guid(row, "ActivePowerOutputSignalID");
                        }

                        Guid?reactivePower = null;
                        if (!row.IsNull("ReactivePowerOutputSignalID"))
                        {
                            reactivePower = database.Guid(row, "ReactivePowerOutputSignalID");
                        }

                        Guid?apparentPower = null;
                        if (!row.IsNull("ApparentPowerOutputSignalID"))
                        {
                            apparentPower = database.Guid(row, "ApparentPowerOutputSignalID");
                        }

                        var currentPhasorID = row.ConvertField <int>("currentPhasorID");
                        var voltagePhasorID = row.ConvertField <int>("voltagePhasorID");
                        var phasors         = Phasor.Load(database, new List <int> {
                            currentPhasorID, voltagePhasorID
                        });

                        calculationList[keys.IndexOf(id)] = new PowerCalculation
                        {
                            ID = row.ConvertField <int>("ID"),
                            CircuitDescription = row.Field <string>("CircuitDescription"),
                            Enabled            = row.ConvertField <bool>("Enabled"),
                            NodeID             = database.Guid(row, "NodeID")
                        };

                        foreach (var phasor in phasors)
                        {
                            if (phasor.ID == currentPhasorID)
                            {
                                calculationList[keys.IndexOf(id)].CurrentPhasor = phasor;
                            }
                            else if (phasor.ID == voltagePhasorID)
                            {
                                calculationList[keys.IndexOf(id)].VoltagePhasor = phasor;
                            }
                        }

                        var mkeys = new List <Guid> {
                            voltageAngle, voltageMagnitude, currentAngle, currentMagnitude
                        };
                        if (activePower != null)
                        {
                            mkeys.Add(activePower.Value);
                        }
                        if (reactivePower != null)
                        {
                            mkeys.Add(reactivePower.Value);
                        }
                        if (apparentPower != null)
                        {
                            mkeys.Add(apparentPower.Value);
                        }
                        var measurements = Measurement.LoadFromKeys(database, mkeys);
                        foreach (var measurement in measurements)
                        {
                            if (measurement.SignalID == voltageAngle)
                            {
                                calculationList[keys.IndexOf(id)].VoltagePhasor.AngleMeasurement = measurement;
                            }
                            else if (measurement.SignalID == voltageMagnitude)
                            {
                                calculationList[keys.IndexOf(id)].VoltagePhasor.MagnitudeMeasurement = measurement;
                            }
                            else if (measurement.SignalID == currentAngle)
                            {
                                calculationList[keys.IndexOf(id)].CurrentPhasor.AngleMeasurement = measurement;
                            }
                            else if (measurement.SignalID == currentMagnitude)
                            {
                                calculationList[keys.IndexOf(id)].CurrentPhasor.MagnitudeMeasurement = measurement;
                            }
                            else if (measurement.SignalID == activePower)
                            {
                                calculationList[keys.IndexOf(id)].ActivePowerOutputMeasurement = measurement;
                            }
                            else if (measurement.SignalID == reactivePower)
                            {
                                calculationList[keys.IndexOf(id)].ReactivePowerOutputMeasurement = measurement;
                            }
                            else if (measurement.SignalID == apparentPower)
                            {
                                calculationList[keys.IndexOf(id)].ApparentPowerOutputMeasurement = measurement;
                            }
                        }
                    }
                }

                return(new ObservableCollection <PowerCalculation>(calculationList ?? new PowerCalculation[0]));
            }
            finally
            {
                if (createdConnection && database != null)
                {
                    database.Dispose();
                }
            }
        }
Exemplo n.º 4
0
		/// <summary>
		/// Loads <see cref="PowerCalculation"/> 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="PowerCalculation"/>.</returns>
		public static ObservableCollection<PowerCalculation> Load(AdoDataConnection database, IList<int> keys)
		{
			var createdConnection = false;

			try
			{
				createdConnection = CreateConnection(ref database);

				PowerCalculation[] calculationList = null;

				if (keys != null && keys.Count > 0)
				{
					var commaSeparatedKeys = keys.Select(key => key.ToString()).Aggregate((str1, str2) => str1 + "," + str2);
					var query = string.Format("select pc.ID, pc.CircuitDescription, pc.VoltageAngleSignalID, pc.VoltageMagSignalID, pc.CurrentAngleSignalID, " +
		 									  "       pc.CurrentMagSignalID, pc.ActivePowerOutputSignalID, pc.ReactivePowerOutputSignalID, pc.ApparentPowerOutputSignalID, pc.Enabled, " +
											  "	   pc.nodeid, vp.id voltagePhasorID, cp.id as currentPhasorID " +
											  "from powercalculation pc " +
											  "     left join measurement vm " +
											  "	 on pc.voltagemagsignalid=vm.signalid " +
											  "	 left join measurement cm " +
											  "	 on pc.currentmagsignalid=cm.signalid " +
											  "	 left join phasor vp " +
											  "	 on vm.deviceid=vp.deviceid and vm.phasorsourceindex=vp.sourceindex " +
											  "	 left join phasor cp " +
											  " on cm.deviceid=cp.deviceid and cm.phasorsourceindex=cp.sourceindex WHERE pc.ID IN ({0})", commaSeparatedKeys);
					var calculationTable = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout);
					calculationList = new PowerCalculation[calculationTable.Rows.Count];

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

						var voltageAngle = database.Guid(row, "VoltageAngleSignalID");
						var voltageMagnitude = database.Guid(row, "VoltageMagSignalID");
						var currentAngle = database.Guid(row, "CurrentAngleSignalID");
						var currentMagnitude = database.Guid(row, "CurrentMagSignalID");

						Guid? activePower = null;
						if (!row.IsNull("ActivePowerOutputSignalID"))
							activePower = database.Guid(row, "ActivePowerOutputSignalID");

						Guid? reactivePower = null;
						if (!row.IsNull("ReactivePowerOutputSignalID"))
							reactivePower = database.Guid(row, "ReactivePowerOutputSignalID");

						Guid? apparentPower = null;
						if (!row.IsNull("ApparentPowerOutputSignalID"))
							apparentPower = database.Guid(row, "ApparentPowerOutputSignalID");

						var currentPhasorID = row.ConvertField<int>("currentPhasorID");
						var voltagePhasorID = row.ConvertField<int>("voltagePhasorID");
						var phasors = Phasor.Load(database, new List<int> {currentPhasorID, voltagePhasorID});

						calculationList[keys.IndexOf(id)] = new PowerCalculation
						{
							ID = row.ConvertField<int>("ID"),
							CircuitDescription = row.Field<string>("CircuitDescription"),
							Enabled = row.ConvertField<bool>("Enabled"),
							NodeID = database.Guid(row, "NodeID")
						};

						foreach (var phasor in phasors)
						{
							if (phasor.ID == currentPhasorID)
							{
								calculationList[keys.IndexOf(id)].CurrentPhasor = phasor;
							}
							else if (phasor.ID == voltagePhasorID)
							{
								calculationList[keys.IndexOf(id)].VoltagePhasor = phasor;
							}
						}

						var mkeys = new List<Guid> { voltageAngle, voltageMagnitude, currentAngle, currentMagnitude };
						if (activePower != null) { mkeys.Add(activePower.Value); }
						if (reactivePower != null) { mkeys.Add(reactivePower.Value); }
						if (apparentPower != null) { mkeys.Add(apparentPower.Value); }
						var measurements = Measurement.LoadFromKeys(database, mkeys);
						foreach (var measurement in measurements)
						{
							if (measurement.SignalID == voltageAngle)
							{
								calculationList[keys.IndexOf(id)].VoltagePhasor.AngleMeasurement = measurement;
							}
							else if (measurement.SignalID == voltageMagnitude)
							{
								calculationList[keys.IndexOf(id)].VoltagePhasor.MagnitudeMeasurement = measurement;
							}
							else if (measurement.SignalID == currentAngle)
							{
								calculationList[keys.IndexOf(id)].CurrentPhasor.AngleMeasurement = measurement;
							}
							else if (measurement.SignalID == currentMagnitude)
							{
								calculationList[keys.IndexOf(id)].CurrentPhasor.MagnitudeMeasurement = measurement;
							}
							else if (measurement.SignalID == activePower)
							{
								calculationList[keys.IndexOf(id)].ActivePowerOutputMeasurement = measurement;
							}
							else if (measurement.SignalID == reactivePower)
							{
								calculationList[keys.IndexOf(id)].ReactivePowerOutputMeasurement = measurement;
							}
							else if (measurement.SignalID == apparentPower)
							{
								calculationList[keys.IndexOf(id)].ApparentPowerOutputMeasurement = measurement;
							}
						}
					}


				}

				return new ObservableCollection<PowerCalculation>(calculationList ?? new PowerCalculation[0]);
			}
			finally
			{
				if (createdConnection && database != null)
					database.Dispose();
			}
		}