/// <summary> /// Initializes the specified configuration files. /// </summary> /// <param name="configFiles">The configuration files.</param> public virtual void Initialize(params string[] configFiles) { SetupLogging(); Log.Info(this, "Starting {0} hardware configuration...", Name); List <SettingsNode> settingsDocs = new List <SettingsNode>(); foreach (string file in configFiles) { if (!File.Exists(file)) { Log.Error(this, "Settings file '{0}' does not exist. Skipping devices and parts listed in file.", file); continue; } SettingsDocument doc = new SettingsDocument(file); settingsDocs.Add(doc); } if (settingsDocs.Count == 0) { Log.Error(this, "No valid settings files. Generating a default settings file template..."); SettingsDocument simDoc = GenerateSettingsTemplate(); simDoc.Save(ConfigFilePath); settingsDocs.Add(simDoc); } Dictionary <IDevice, SettingsNode> devSettings = new Dictionary <IDevice, SettingsNode>(); // Create devices foreach (SettingsNode doc in settingsDocs) { IList <SettingsNode> devSettingNodes; IList <IDevice> devices = _devMgr.FindOrCreateDevices(doc, out devSettingNodes); IList <TDevice> myDevices = devices.Where(d => d is TDevice) .Select(d => (TDevice)d) .ToList(); _devices.AddRange(myDevices); for (int i = 0; i < devices.Count; i++) { devSettings.Add(devices[i], devSettingNodes[i]); } } // Connect to all devices foreach (TDevice device in _devices) { try { if (!device.Connected) { device.Open(); } } catch (Exception ex) { Log.Error(this, "Unable to open a connect to {0} device '{1}' on port {2}. {3}", device.GetType().Name, device.Name, device.Port, ex); } } // Configure all devices foreach (KeyValuePair <IDevice, SettingsNode> kvp in devSettings) { if (!(kvp.Key is TDevice)) { continue; } TDevice myDevice = (TDevice)kvp.Key; try { myDevice.Configure(kvp.Value); } catch (Exception ex) { Log.Error(this, "Unable to configure {0} device '{1}' from . {2}", myDevice.GetType().Name, myDevice.Name, ex); } } // Create parts List <string> forcedParts = new List <string>(); foreach (SettingsNode partsDoc in settingsDocs) { IList <TPart> parts; IList <string> forced = CreateParts(partsDoc, out parts); // This handles the old initialization files if (parts.Count == 0) { string partsDocName = Path.GetFileName(partsDoc.Name); string configFileName = Path.GetFileName(ConfigFilePath); if (String.Equals(partsDocName, configFileName, StringComparison.CurrentCultureIgnoreCase)) { forced = TrySpecificInit(partsDoc, out parts); } } _parts.AddRange(parts); forcedParts.AddRange(forced); } Log.Info(this, "{0} parts defined as follows:", Name); List <IDevice> partDevices = new List <IDevice>(); foreach (TPart part in _parts) { Log.Info(this, "{0}: {1} @ {2}", part.Name, part.Device.Name, part.Device.Port); if (!partDevices.Contains(part.Device)) { partDevices.Add(part.Device); } } RegisterParts(_parts); foreach (IDevice device in partDevices) { if (!_devices.Contains(device.Name)) { _devices.Add((TDevice)device); } try { device.Initialize(); } catch (Exception ex) { Log.Error(this, "Failed to initialize {0} device. {1}", device.Name, ex.Message); } } if (forcedParts.Count > 0) { string message = String.Format("The following parts were forced into simulation mode:{0}{1}{0}Please see the log for more details.", Environment.NewLine, String.Join(Environment.NewLine, forcedParts)); Notify.PopUpError(Name + " Forcing Simulation...", message); } Log.Info(this, "Completed {0} hardware configuration", Name); }