//                           UNCLASSIFIED
/*-------------------------------------------------------------------------
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
COMPANY.

THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
GOVERNMENT.

UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/


// Ignore Spelling: ocp

using System;
using System.Collections.Generic;
using Raytheon.Units;
using System.Threading;
using NLog;
using Raytheon.Common;
using System.Net.Sockets;
using System.Linq;
using System.IO;
using System.Reflection;
using Raytheon.Instruments.PowerSupply;

namespace Raytheon.Instruments
{
	/// <summary>
	/// A class to control a power supply system.
	/// </summary>
	public class PowerSupplySystemSim : IPowerSupplySystem
	{
		#region PrivateClassMembers
		private SortedDictionary<string, IDCPwr> _powerModuleMap;
      private Dictionary<string, PowerSupplyModuleInfo> _powerModuleInfoDict = new Dictionary<string, PowerSupplyModuleInfo>();

      private SortedDictionary<string, double> _powerModuleInitialVoltageSetpoint;
		private string _name;
		private object _syncObj = new Object();
		private List<int> _moduleNumbersThatHaveBeenAdded;
		private State _state;
		private SelfTestResult _selfTestResult;

		/// <summary>
		/// NLog logger
		/// </summary>
		private readonly ILogger _logger;
		/// <summary>
		/// Raytheon configuration
		/// </summary>
		private readonly IConfigurationManager _configurationManager;
		private readonly IConfiguration _configuration;

		#endregion

		#region PrivateFuctions
		/// <summary>
		/// The Finalizer.
		/// </summary>
		~PowerSupplySystemSim()
		{
			Dispose(false);
		}

		/// <summary>
		/// Dispose of this object.
		/// </summary>
		/// <param name="disposing">True = currently disposing, False = not disposing.</param>
		protected virtual void Dispose(bool disposing)
		{
			try
			{
				lock (_syncObj)
				{
					if (disposing)
					{
						if (_state == State.Ready)
						{
							try
							{
								//Reset System
								Reset();
							}
							catch (Exception)
							{
							}

							try
							{
								foreach (KeyValuePair<string, IDCPwr> entry in _powerModuleMap)
								{
									entry.Value.Shutdown();
								}
							}
							catch (Exception)
							{
							}

							_state = State.Uninitialized;
						}
					}
				}
			}
			catch (Exception)
			{
				try
				{
					//ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
				}
				catch (Exception)
				{
					//Do not rethrow. Exception from error logger that has already been garbage collected
				}
			}
		}

		#endregion

		#region PublicFuctions

		/// <summary>
		/// PowerSupplySystemSim factory constructor
		/// </summary>
		/// <param name="deviceName"></param>
		/// <param name="configurationManager"></param>
		/// <param name="logger"></param>
		public PowerSupplySystemSim(string deviceName, IConfigurationManager configurationManager, ILogger logger)
		{
			try
			{
				Name = deviceName;

				_logger = logger;

				_configurationManager = configurationManager;
				_configuration = _configurationManager.GetConfiguration(Name);

				string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
				string powerSupplySystemDefPath = _configuration.GetConfigurationValue(deviceName, PowerSupply.ConfigXml.POWER_SUPPLY_SYSTEM_DEF_FILEPATH.ToString());

				if (!Path.IsPathRooted(powerSupplySystemDefPath))
					powerSupplySystemDefPath = Path.GetFullPath(Path.Combine(assemblyFolder, powerSupplySystemDefPath));

				IConfigurationFile config = new ConfigurationFile(powerSupplySystemDefPath);

				_powerModuleInitialVoltageSetpoint = new SortedDictionary<string, double>(StringComparer.InvariantCultureIgnoreCase);

				_moduleNumbersThatHaveBeenAdded = new List<int>();

				_powerModuleMap = new SortedDictionary<string, IDCPwr>(StringComparer.InvariantCultureIgnoreCase);

				string moduleDef = config.ReadValue(deviceName, PowerSupply.ConfigIni.MODULE_DEFINITION.ToString());
				List<string> powerModules = moduleDef.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries).ToList();

				double overCurrentProtection;
				double overVoltageProtection;
				double voltageSetpoint;
				double maxVoltageSetpoint;
				double minVoltageSetpoint;
				double slewRateVoltsPerSecond;
				double inRushDelaySecs;
				int moduleNumber = -1;
				for (int i = 0; i < powerModules.Count(); i++)
				{
					string moduleName = powerModules[i];

					int.TryParse(config.ReadValue($"{deviceName}.{moduleName}", PowerSupply.ConfigIni.INDEX.ToString()), out moduleNumber);
					Double.TryParse(config.ReadValue($"{deviceName}.{moduleName}", PowerSupply.ConfigIni.OCP.ToString()), out overCurrentProtection);
					Double.TryParse(config.ReadValue($"{deviceName}.{moduleName}", PowerSupply.ConfigIni.OVP.ToString()), out overVoltageProtection);
					Double.TryParse(config.ReadValue($"{deviceName}.{moduleName}", PowerSupply.ConfigIni.VOLTAGE_SETPOINT.ToString()), out voltageSetpoint);
					Double.TryParse(config.ReadValue($"{deviceName}.{moduleName}", PowerSupply.ConfigIni.MIN_VOLTAGE.ToString()), out minVoltageSetpoint);
					Double.TryParse(config.ReadValue($"{deviceName}.{moduleName}", PowerSupply.ConfigIni.MAX_VOLTAGE.ToString()), out maxVoltageSetpoint);

               try
               {
                  if (!Double.TryParse(config.ReadValue($"{Name}.{moduleName}", PowerSupply.ConfigIni.VOLTAGE_SLEW_RATE.ToString()), out slewRateVoltsPerSecond))
                     slewRateVoltsPerSecond = -1.0;
               }
               catch
               {
                  slewRateVoltsPerSecond = -1.0;
               }

               try
               {
                  if (!Double.TryParse(config.ReadValue($"{Name}.{moduleName}", PowerSupply.ConfigIni.IN_RUSH_DELAY_SECS.ToString()), out inRushDelaySecs))
                     inRushDelaySecs = -1.0;
               }
               catch
               {
                  inRushDelaySecs = -1.0;
               }

               _powerModuleInfoDict[moduleName] = new PowerSupplyModuleInfo(moduleNumber, overCurrentProtection, overVoltageProtection, voltageSetpoint, slewRateVoltsPerSecond, minVoltageSetpoint, maxVoltageSetpoint);

					// create and initialize the power module
					IDCPwr powerSupply = new PowerSupplySim(moduleName, overCurrentProtection, overVoltageProtection, voltageSetpoint, maxVoltageSetpoint, minVoltageSetpoint, inRushDelaySecs, moduleNumber);

					// remember that we have added this module
					_moduleNumbersThatHaveBeenAdded.Add(moduleNumber);

					// remember the module name
					_powerModuleMap.Add(moduleName.ToUpper(), powerSupply);

					// remember the initial voltage setpoint
					_powerModuleInitialVoltageSetpoint.Add(moduleName.ToUpper(), voltageSetpoint);
				}

				_selfTestResult = SelfTestResult.Unknown;
				_state = State.Uninitialized;
			}
			catch (Exception)
			{
				throw;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public bool ClearErrors()
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// Couple modules together
		/// </summary>
		/// <param name="moduleNameList"></param>
		public void CoupleChannels(List<string> moduleNameList)
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// 
		/// </summary>
		public string DetailedStatus
		{
			get
			{
				return "This is a Power Supply System Sim called " + _name;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool DisplayEnabled
		{
			get
			{
				throw new NotImplementedException();
			}

			set { ; }
		}

		/// <summary>
		/// Dispose of this object.
		/// </summary>
		public void Dispose()
		{
			try
			{
				lock (_syncObj)
				{
					Dispose(true);

					GC.SuppressFinalize(this);
				}
			}
			catch (Exception)
			{
				try
				{
					//ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
				}
				catch (Exception)
				{
					//Do not rethrow. Exception from error logger that has already been garbage collected
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool FrontPanelEnabled
		{
			get
			{
				throw new NotImplementedException();
			}

			set
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// Get the error code.
		/// </summary>
		/// <param name="errorCode">The error code.</param>
		/// <returns>The error description.</returns>
		public string GetErrorCode(out int errorCode)
		{
			lock (_syncObj)
			{
				errorCode = 0;
				return "";
			}
		}

		/// <summary>
		/// Get the names of the modules in this system
		/// </summary>
		/// <returns></returns>
		public List<string> GetModuleNames()
		{
			lock (_syncObj)
			{
				List<string> moduleNames = new List<string>();

				foreach (KeyValuePair<string, IDCPwr> modules in _powerModuleMap)
				{
					moduleNames.Add(modules.Key);
				}

				return moduleNames;
			}
		}

      /// <summary>
      /// Get the dictionary that contains configuration information for each module
      /// </summary>
      /// <returns></returns>
      public Dictionary<string, PowerSupplyModuleInfo> GetPowerSupplyModuleInfoDict(string powerSystem)
      {
         lock (_syncObj)
         {
            return _powerModuleInfoDict;
         }
      }

      /// <summary>
      /// Get the overcurrent protection setting.
      /// </summary>
      /// <param name="name">The module to get the overcurrent protection setting.</param>
      /// <returns>The current (Amps).</returns>
      public double GetOverCurrentSetting(string name)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::GetOverCurrentSetting() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				return _powerModuleMap[name.ToUpper()].CurrentLimit.Amps;
			}
		}

		/// <summary>
		/// Get the overvoltage protection setting.
		/// </summary>
		/// <param name="name">The module to get the overvoltage protection setting.</param>
		/// <returns>The voltage (Volts).</returns>
		public double GetOverVoltageSetting(string name)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::GetOverVoltageSetting() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				return _powerModuleMap[name.ToUpper()].OverVoltageProtection.Volts;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public double GetSlewRate(string name)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::GetSlewRate() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				throw new NotImplementedException();

				//return _powerModuleMap[name.ToUpper()].GetSlewRate();
			}
		}

		/// <summary>
		/// Get the voltage setpoint.
		/// </summary>
		/// <param name="name">The module to get the voltage setpoint setting.</param>
		/// <returns>the voltage setpoint (Volts).</returns>
		public double GetVoltageSetting(string name)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::GetVoltageSetting() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				return _powerModuleMap[name.ToUpper()].OutputVoltage.Volts;
			}
		}

		/// <summary>
		/// Group Modules Together
		/// </summary>
		/// <param name="modules"></param>
		public void GroupModules(List<string> moduleNameList)
		{
			lock (_syncObj)
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public InstrumentMetadata Info
		{
			get
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void Initialize()
		{
			// if we have not yet been initialized, go ahead and create the socket
			if (_state == State.Uninitialized)
			{
				Reset();

				PerformSelfTest();

				// initialize each module
				foreach (KeyValuePair<string, IDCPwr> powerModPair in _powerModuleMap)
				{
					powerModPair.Value.Initialize();
				}

				_state = State.Ready;
			}
			else
			{
				throw new Exception("PowerSupplySystemSim::Initialize() - expected the System " + _name + "  to be Uninitialized, state was: " + _state.ToString());
			}
		}

		/// <summary>
		/// Send a command and return the response
		/// </summary>
		/// <param name="commandString"></param>
		/// <returns></returns>
		public string IOQuery(string commandString)
		{
			Thread.Sleep(500);

			// return something
			return "1.11";
		}

		/// <summary>
		/// Send a command
		/// </summary>
		/// <param name="commandString"></param>
		public void IOWrite(string commandString)
		{
			Thread.Sleep(50);
		}

		/// <summary>
		/// Query the output state.
		/// </summary>
		/// <param name="name">The module to query.</param>
		/// <returns>The output state. True = On, False = Off.</returns>
		public bool IsOutputOn(string name)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::IsOutputOn() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				return _powerModuleMap[name.ToUpper()].Enabled;
			}
		}

		/// <summary>
		/// Control the power supply internal mechanical relay state
		/// </summary>
		/// <param name="name">The module to act on</param>
		/// <param name="shallWeConnect">True to connect, false to disconnect</param>
		public void MechanicalRelayOutputControl(string name, bool shallWeConnect)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::MechanicalRelayOutputControl() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}
			}
		}

		/// <summary>
		/// Read the current.
		/// </summary>
		/// <param name="name">The name of the module.</param>
		/// <returns>The current (Amps).</returns>
		public double MeasureCurrent(string name)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::MeasureCurrent() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				return _powerModuleMap[name.ToUpper()].MeasureCurrent().Amps;
			}
		}

		/// <summary>
		/// Read the voltage.
		/// </summary>
		/// <param name="name">The name of the module.</param>
		/// <returns>The voltage (Volts).</returns>
		public double MeasureVoltage(string name)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::MeasureVoltage() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				return _powerModuleMap[name.ToUpper()].MeasureVoltage().Volts;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Name
		{
			get
			{
				return _name;
			}
			set { _name = value; }
		}

		/// <summary>
		/// Turn the output off.
		/// </summary>
		/// <param name="name">The name of the module.</param>
		public void Off(string name)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::Off() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				_powerModuleMap[name.ToUpper()].Enabled = false;
			}
		}

		/// <summary>
		/// Turn the output on.
		/// </summary>
		/// <param name="name">The name of the module.</param>
		public void On(string name)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::On() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				_powerModuleMap[name.ToUpper()].Enabled = true;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public SelfTestResult PerformSelfTest()
		{
			_selfTestResult = SelfTestResult.Pass;

			return _selfTestResult;
		}

		/// <summary>
		/// Read the overvoltage and overcurrent protection status.
		/// </summary>
		/// <param name="name">The name of the module.</param>
		/// <returns>The binary sum of all bits (decimal value) set in the Questionable Status Enable register.</returns>
		public int ReadProtectionStatus(string name)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::ReadProtectionStatus() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				return _powerModuleMap[name.ToUpper()].ReadProtectionStatus();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public PowerData ReadPowerData(string name)
		{
			lock (_syncObj)
			{
				// voltage, voltage setpoint, current, output status
				double voltage = MeasureVoltage(name);
				double voltageSetpoint = GetVoltageSetting(name);
				double current = MeasureCurrent(name);
				bool outputStatus = IsOutputOn(name);
				int faultStatus = ReadProtectionStatus(name);
				double overVoltageProtection = GetOverVoltageSetting(name);
				double overCurrentProtection = GetOverCurrentSetting(name);

				return new PowerData(voltage, voltageSetpoint, overVoltageProtection, current, overCurrentProtection, outputStatus, faultStatus);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void Reset()
		{
			lock (_syncObj)
			{
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public SelfTestResult SelfTestResult
		{
			get
			{
				return _selfTestResult;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		public void SetInitialVoltage(string name)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::SetInitialVoltage() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				double initializeVoltage = _powerModuleInitialVoltageSetpoint[name.ToUpper()];

				SetVoltageSetpoint(name.ToUpper(), initializeVoltage);
			}
		}

		/// <summary>
		/// Set the slew rate
		/// </summary>
		/// <param name="commandedSlew">slew in volts per second</param>
		public void SetSlewRate(string name, double commandedSlew)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::SetSlewRate() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				throw new NotImplementedException();

				//_powerModuleMap[name.ToUpper()].SetSlewRate(commandedSlew);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="moduleName"></param>
		/// <param name="ocpValue"></param>
		public void SetOverCurrentProtection(string moduleName, double ocpValue)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(moduleName.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::SetOverCurrentProtection() - could not find supply: " + moduleName.ToUpper() + " In System " + _name);
				}

				_powerModuleMap[moduleName.ToUpper()].CurrentLimit = Current.FromAmps(ocpValue);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="moduleName"></param>
		/// <param name="ovpValue"></param>
		public void SetOverVoltageProtection(string moduleName, double ovpValue)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(moduleName.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::SetOverVoltageProtection() - could not find supply: " + moduleName.ToUpper() + " In System " + _name);
				}

				_powerModuleMap[moduleName.ToUpper()].OverVoltageProtection = Voltage.FromVolts(ovpValue);
			}
		}

		/// <summary>
		/// Set the voltage setpoint.
		/// </summary>
		/// <param name="name">The name of the module.</param>
		/// <param name="voltage">The desired voltage.</param>
		public void SetVoltageSetpoint(string name, double volts)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == false)
				{
					throw new Exception("PowerSupplySystemSim::SetVoltageSetpoint() - could not find supply: " + name.ToUpper() + " In System " + _name);
				}

				_powerModuleMap[name.ToUpper()].OutputVoltage = Voltage.FromVolts(volts);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void Shutdown()
		{
			if (_state == State.Ready)
			{
				string errorMsg = "";

				try
				{
					//Reset System
					Reset();
				}
				catch (Exception err)
				{
					errorMsg += err.Message + " ";
				}

				try
				{
					foreach (KeyValuePair<string, IDCPwr> entry in _powerModuleMap)
					{
						entry.Value.Shutdown();
					}
				}
				catch (Exception err)
				{
					errorMsg += err.Message + " ";
				}

				try
				{
				}
				catch (Exception err)
				{
					errorMsg += err.Message + " ";
				}

				_state = State.Uninitialized;

				if (errorMsg != "")
				{
					throw new Exception("PowerSupplySystemSim::ShutdDown() - System " + _name + " had an error: " + errorMsg);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public State Status
		{
			get
			{
				return _state;
			}
		}

		/// <summary>
		/// Turn off the watchdog capability.
		/// </summary>
		public void WatchdogDisable()
		{
			lock (_syncObj)
			{
			}
		}

		/// <summary>
		/// Turn on the watchdog capability.
		/// </summary>
		/// <param name="time">The watchdog time in seconds.</param>
		public void WatchdogEnable(uint time)
		{
			lock (_syncObj)
			{
			}
		}

		/// <summary>
		/// Adds another power supply to the Power Supply System
		/// </summary>
		/// <param name="name"></param>
		/// <param name="overCurrentProtection"></param>
		/// <param name="overVoltageProtection"></param>
		/// <param name="voltageSetpoint"></param>
		/// <param name="maxVoltageSetpoint"></param>
		/// <param name="minVoltageSetpoint"></param>
		/// <param name="moduleNumber"></param>
		/// <exception cref="Exception"></exception>
		public void AddPowerSupply(string name, double overCurrentProtection, double overVoltageProtection, double voltageSetpoint, double maxVoltageSetpoint, double minVoltageSetpoint, int moduleNumber = -1)
		{
			lock (_syncObj)
			{
				if (_powerModuleMap.ContainsKey(name.ToUpper()) == true)
				{
					throw new Exception("PowerSupplySystemSim::AddPowerSupply() - system already contains a supply named: " + name.ToUpper() + " In System " + _name);
				}

				// check to see if this index has already been added
				// would like to ask the IDCPwr object, but that functionality is not exposed in the interface
				if (_moduleNumbersThatHaveBeenAdded.Contains(moduleNumber) == true)
				{
					throw new Exception("PowerSupplySystemSim::AddPowerSupply() - module number has already been added: " + moduleNumber + " In System " + _name);
				}

				// confirm we are already initialized
				if (_state != State.Uninitialized)
				{
					throw new Exception("PowerSupplySystemSim::AddPowerSupply() - System " + _name + "  must be Uninitialized when adding power supplies. Current state is: " + _state.ToString());
				}

				// create and initialize the power module
				IDCPwr powerSupply = new PowerSupplySim(name, overCurrentProtection, overVoltageProtection, voltageSetpoint, maxVoltageSetpoint, minVoltageSetpoint, moduleNumber);

				_moduleNumbersThatHaveBeenAdded.Add(moduleNumber);

				_powerModuleMap.Add(name.ToUpper(), powerSupply);

				_powerModuleInitialVoltageSetpoint.Add(name.ToUpper(), voltageSetpoint);
			}
		}

		/// <summary>
		/// returns system name
		/// </summary>
		/// <returns></returns>
		public string GetSystemName() => Name;

		/// <summary>
		/// reads power data
		/// </summary>
		/// <param name="moduleName"></param>
		/// <param name="voltage"></param>
		/// <param name="voltageSetpoint"></param>
		/// <param name="current"></param>
		/// <param name="outputStatus"></param>
		/// <param name="faultStatus"></param>
		public void ReadPowerData(string moduleName, out double voltage, out double voltageSetpoint, out double current, out bool outputStatus, out int faultStatus)
		{
			lock (_syncObj)
			{
				// voltage, voltage setpoint, current, output status
				voltage = MeasureVoltage(moduleName);
				voltageSetpoint = GetVoltageSetting(moduleName);
				current = MeasureCurrent(moduleName);
				outputStatus = IsOutputOn(moduleName);
				faultStatus = ReadProtectionStatus(moduleName);
			}

		}

		#endregion
	}
}
