using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; using System.Reflection; using NLog; using Raytheon.Common; namespace Raytheon.Instruments { [ExportInstrumentFactory(ModelNumber = "CommDeviceTcpClientFactory")] public class CommDeviceTcpClientFactory : IInstrumentFactory { private readonly List _supportedInterfaces = new List(); private readonly IConfigurationManager _configurationManager; private const string DefaultConfigPath = @"C:\ProgramData\Raytheon\InstrumentManagerService"; private static string DefaultPath; public CommDeviceTcpClientFactory(string defaultConfigPath = DefaultConfigPath) : this(null, defaultConfigPath) { } /// /// CommDeviceTcpClientFactory injection constructor /// [ImportingConstructor] public CommDeviceTcpClientFactory([Import(AllowDefault = false)] IConfigurationManager configManager, [Import(AllowDefault = true)] string defaultConfigPath = null) { DefaultPath = defaultConfigPath; if (LogManager.Configuration == null) { var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder + "\\nlog.config"); } _configurationManager = configManager ?? GetConfigurationManager(); _supportedInterfaces.Add(typeof(ICommDevice)); } /// /// Gets the instrument /// /// /// public IInstrument GetInstrument(string name) { try { return new CommDeviceTcpClient(name, _configurationManager); } catch (Exception) { throw; } } /// /// Gets the instrument /// /// /// public object GetInstrument(string name, bool simulateHw) { try { return new CommDeviceTcpClient(name, _configurationManager); } catch (Exception) { throw; } } /// /// Gets supported interfaces /// /// public ICollection GetSupportedInterfaces() { return _supportedInterfaces.ToArray(); } /// /// returns configuration based on the predefined path or default path c:/ProgramData/Raytheon/InstrumentManagerService /// /// private static IConfigurationManager GetConfigurationManager() { return string.IsNullOrEmpty(DefaultPath) ? new RaytheonConfigurationManager() : new RaytheonConfigurationManager(DefaultPath); } } }