//                           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.
-------------------------------------------------------------------------*/

using Raytheon.Instruments;
using System;
using Raytheon.Instruments.Dmm;
using Raytheon.Units;
using Agilent.AgM918x.Interop;
using NLog;
using Raytheon.Common;
using System.IO.Ports;

namespace Raytheon.Instruments
{
	/// <summary>
	/// A keysight implementation of the DMM interface
	/// </summary>
	public class DMMKeysightM9183 : IDmm
	{
		#region PrivateMemberVariables

		private AgM918x _dmm;
		private string _name;
		private readonly string _address;
		private readonly string _options;
		private MeasurementFunction _lastType;
		private double _lastRange;
		private double _lastResolution;
		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 PrivateFunctions

		/// <summary>
		/// Dispose of this objects resources
		/// </summary>
		/// <param name="disposing"></param>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed", MessageId = "_34461A")]
		protected virtual void Dispose(bool disposing)
		{
			try
			{
				if (disposing)
				{
					if (_state == State.Ready)
					{
						_dmm.Utility.Reset();

						_dmm.Close();

						_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
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="range"></param>
		/// <param name="resolution"></param>
		/// <param name="currentType"></param>
		/// <returns></returns>
		private double ReadCurrent(double range, double resolution, MeasurementFunction currentType)
		{
			bool shallWeUseAutoRange = false;

			if (range < 0)
			{
				shallWeUseAutoRange = true;
				// set the starting range for auto range to 2 micro amps
				range = 0.000002;
			}

			if (currentType == MeasurementFunction.ACCurrent)
			{
				return _dmm.ACCurrent.Measure(range, resolution, shallWeUseAutoRange);
			}
			else if (currentType == MeasurementFunction.DCCurrent)
			{
				return _dmm.DCCurrent.Measure(range, resolution, shallWeUseAutoRange);
			}
			else
			{
				throw new Exception("only ac or dc current is acceptable for param type: " + currentType.ToString());
			}
		}

		/// <summary>
		/// Reads frequency from the dmm
		/// </summary>
		/// <param name="range">The range to use on the DMM</param>
		/// <param name="resolution">The resolution to use on the DMM</param>
		/// <param name="voltRange">The voltage range for the measurement</param>
		/// <param name="numReads">The number of reads to make</param>
		/// <returns>The frequency or average of frequencies read</returns>
		private double ReadFrequency(double range, double resolution, double voltRange, double numReads)
		{
			bool shallWeUseAutoRange = false;

			if (range < 0)
			{
				shallWeUseAutoRange = true;
				// set the starting range for auto range to 10
				range = 10;
			}

			return _dmm.Frequency.Measure(range, shallWeUseAutoRange, voltRange);
		}

		/// <summary>
		/// Reads resistance
		/// </summary>
		/// <param name="range">The range to use on the DMM</param>
		/// <param name="resolution">The resolution to use on the DMM</param>\
		/// <param name="dmmResistanceType"></param>
		/// <returns>The resistance</returns>
		private double ReadResistance(double range, double resolution, MeasurementFunction resistanceType)
		{
			bool shallWeUseAutoRange = false;

			if (range < 0)
			{
				shallWeUseAutoRange = true;
				// set the starting range for auto range to 10
				range = 10;
			}

			if (resistanceType == MeasurementFunction.FourWireResistance)
			{
				return _dmm.Resistance4Wire.Measure(range, resolution, shallWeUseAutoRange);
			}
			else if (resistanceType == MeasurementFunction.TwoWireResistance)
			{
				return _dmm.Resistance.Measure(range, resolution, shallWeUseAutoRange);
			}
			else
			{
				throw new Exception("only 4 or 2 write resistance is acceptable for param type: " + resistanceType.ToString());
			}
		}

		/// <summary>
		/// Reads voltage
		/// </summary>
		/// <param name="range">The range to use on the DMM</param>
		/// <param name="resolution">The resolution to use on the DMM</param>
		/// <returns></returns>
		private double ReadVoltage(double range, double resolution, MeasurementFunction voltageType)
		{
			bool shallWeUseAutoRange = false;

			if (range < 0)
			{
				shallWeUseAutoRange = true;
				// set the starting range for auto range to 10
				range = 10;
			}

			if (voltageType == MeasurementFunction.ACVolts)
			{
				return _dmm.ACVoltage.Measure(range, resolution, shallWeUseAutoRange);
			}
			else if (voltageType == MeasurementFunction.DCVolts)
			{
				return _dmm.DCVoltage.Measure(range, resolution, shallWeUseAutoRange);
			}
			else
			{
				throw new Exception("only ac or dc volt is acceptable for param type: " + voltageType.ToString());
			}
		}

		/// <summary>
		/// Convert scpi data to string
		/// </summary>
		/// <param name="data"></param>
		/// <returns></returns>
		private string ConvertToString(ref byte[] data)
		{
			string rsp = System.Text.Encoding.ASCII.GetString(data);
			return rsp;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="numbers"></param>
		/// <returns></returns>
		private double GetFrequencyAvg(string numbers)
		{
			string[] numArr = numbers.Split(',');
			double retVal = 0;

			foreach (string val in numArr)
			{
				retVal += Util.ConvertStringToDouble(val);
			}

			retVal = retVal / numArr.Length;

			return retVal;
		}

		#endregion

		#region PublicFunctions

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

			_logger = logger;

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

			_address = _configuration.GetConfigurationValue("DMMKeysightM9183", "Address", "");
			_options = _configuration.GetConfigurationValue("DMMKeysightM9183", "Options", "");

			_lastType = 0;
			_lastRange = 0;
			_lastResolution = 0;

			//created in Initialize()
			_dmm = null;

			_state = State.Uninitialized;
			_selfTestResult = SelfTestResult.Unknown;
		}

		/// <summary>
		/// The constructor which opens the handle to the DMM and performs a self test on the instrument
		/// </summary>
		/// <param name="name">The name of this dmm</param>
		/// <param name="address">The address of the DMM</param>
		public DMMKeysightM9183(string name, string address, string options)
		{
			_name = name;
			_address = address;
			_options = options.Trim();
			_lastType = 0;
			_lastRange = 0;
			_lastResolution = 0;

			//created in Initialize()
			_dmm = null;
			_logger = LogManager.GetCurrentClassLogger();

			_state = State.Uninitialized;
			_selfTestResult = SelfTestResult.Unknown;
		}

		/// <summary>
		/// The Finalizer which will release resources if required
		/// </summary>
		~DMMKeysightM9183()
		{
			Dispose(false);
		}

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

		/// <summary>
		/// 
		/// </summary>
		/// <param name="type"></param>
		/// <param name="range"></param>
		/// <param name="resolution"></param>
		public void ConfigureCurrentMeasurement(MeasurementFunction type, Current range, Current resolution)
		{
			if (type != MeasurementFunction.ACCurrent && type != MeasurementFunction.DCCurrent)
			{
				throw new Exception("only AC or DC Current types are acceptable for param type: " + _lastType.ToString());
			}

			_lastType = type;
			_lastRange = range.Amps;
			_lastResolution = resolution.Amps;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="type"></param>
		/// <param name="range"></param>
		/// <param name="resolution"></param>
		public void ConfigureFrequencyMeasurement(MeasurementFunction type, Frequency range, Frequency resolution)
		{
			if (type != MeasurementFunction.Frequency)
			{
				throw new Exception("only frequency is acceptable for param type: " + _lastType.ToString());
			}

			_lastType = type;
			_lastRange = range.Hertz;
			_lastResolution = resolution.Hertz;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="type"></param>
		/// <param name="range"></param>
		/// <param name="resolution"></param>
		public void ConfigureResistanceMeasurement(MeasurementFunction type, Resistance range, Resistance resolution)
		{
			if (type != MeasurementFunction.FourWireResistance && type != MeasurementFunction.TwoWireResistance)
			{
				throw new Exception("only FourWireResistance or TwoWireResistance is acceptable for param type: " + _lastType.ToString());
			}

			_lastType = type;
			_lastRange = range.Ohms;
			_lastResolution = resolution.Ohms;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="type"></param>
		/// <param name="range"></param>
		/// <param name="resolution"></param>
		public void ConfigureVoltageMeasurement(MeasurementFunction type, Voltage range, Voltage resolution)
		{
			if (type != MeasurementFunction.DCVolts && type != MeasurementFunction.ACVolts)
			{
				throw new Exception("only ac or dc volt is acceptable for param type: " + type.ToString());
			}

			_lastType = type;
			_lastRange = range.Volts;
			_lastResolution = resolution.Volts;
		}

		/// <summary>
		/// 
		/// </summary>
		public string DetailedStatus
		{
			get
			{
				return "This is a Keysight Dmm";
			}
		}

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

			set
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// Dispose of this objects resources
		/// </summary>
		public void Dispose()
		{
			try
			{
				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>
		/// 
		/// </summary>
		public InstrumentMetadata Info
		{
			get
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void Initialize()
		{
			if (_state == State.Uninitialized)
			{
				_dmm = new AgM918x();

				if (_options != "" && _options.ToUpper() != "NONE")
				{
					_dmm.Initialize(_address, false, true, _options);
				}
				else
				{
					_dmm.Initialize(_address, false, true);
				}

				//_selfTestResult = PerformSelfTest();
				_state = State.Ready;
			}
			else
			{
				throw new Exception("expected the state to be Uninitialized, state was: " + _state.ToString());
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public MeasurementFunction MeasurementType
		{
			get
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="timeout"></param>
		/// <returns></returns>
		public Current MeasureCurrent(int timeout)
		{
			if (_lastType != MeasurementFunction.DCCurrent && _lastType != MeasurementFunction.ACCurrent)
			{
				throw new Exception("only DC or AC Current is acceptable for param type: " + _lastType.ToString());
			}

			return Current.FromAmps(ReadCurrent(_lastRange, _lastResolution, _lastType));
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="timeout"></param>
		/// <returns></returns>
		public Frequency MeasureFrequency(int timeout)
		{
			if (_lastType != MeasurementFunction.Frequency)
			{
				throw new Exception("only frequency is acceptable for param type: " + _lastType.ToString());
			}

			return Frequency.FromHertz(ReadFrequency(_lastRange, _lastResolution, _lastRange, 1));
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="timeout"></param>
		/// <returns></returns>
		public Resistance MeasureResistance(int timeout)
		{
			if (_lastType != MeasurementFunction.FourWireResistance && _lastType != MeasurementFunction.TwoWireResistance)
			{
				throw new Exception("only FourWireResistance or TwoWireResistance is acceptable for param type: " + _lastType.ToString());
			}

			return Resistance.FromOhms(ReadResistance(_lastRange, _lastResolution, _lastType));
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="timeout"></param>
		/// <returns></returns>
		public Voltage MeasureVoltage(int timeout)
		{
			if (_lastType != MeasurementFunction.DCVolts && _lastType != MeasurementFunction.ACVolts)
			{
				throw new Exception("only ac or dc volt is acceptable for param type: " + _lastType.ToString());
			}

			return Voltage.FromVolts(ReadVoltage(_lastRange, _lastResolution, _lastType));
		}

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

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public SelfTestResult PerformSelfTest()
		{
			int testResult = 0;
			string result = "";
			_dmm.Utility.SelfTest(ref testResult, ref result);

			// Set measurement input to backplane after performing self test.  Default is front panel.
			// _dmm.Measurement.In = VTEXDmmInputSelectEnum.VTEXDmmInputSelectInternal;

			if (testResult > 0)
			{
				_selfTestResult = Raytheon.Instruments.SelfTestResult.Fail;
				throw new Exception("self test failed with an Error Code: " + testResult + " and Error Message: " + result);
			}

			_selfTestResult = Raytheon.Instruments.SelfTestResult.Pass;

			return _selfTestResult;
		}

		/// <summary>
		/// 
		/// </summary>
		public void Reset()
		{
			_dmm.Utility.Reset();
		}

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

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

		/// <summary>
		/// 
		/// </summary>
		public void Shutdown()
		{
			if (_state == State.Ready)
			{
				_dmm.Utility.Reset();

				_dmm.Close();

				_state = State.Uninitialized;
			}
		}

		#endregion
	}
}
