Beispiel #1
0
        public FacetData(INamedList responseHeader)
        {
            if (responseHeader.IsNullOrEmpty()) return;
            Queries = responseHeader.GetOrDefault<INamedList>("facet_queries");

            var fieldsList = responseHeader.GetOrDefault<INamedList>("facet_fields");
            if (fieldsList != null)
            {
                Fields=new NamedList();
                for (int i = 0; i < fieldsList.Count; i++)
                {
                    var innerList = fieldsList.GetOrDefault<IList>(i);
                    if (innerList == null) continue;
                    var list = new NamedList();
                    for (int j = 0; j < innerList.Count; j = j + 2)
                    {
                        var valKey = innerList[j] as string;
                        if (valKey != null)
                            list.Add(valKey, innerList[j + 1]);
                    }

                    Fields.Add(fieldsList.GetKey(i), list);
                }
            }

            //Fields = responseHeader.GetOrDefault<INamedList>("facet_fields");
            Dates = responseHeader.GetOrDefault<INamedList>("facet_dates");
            Ranges = responseHeader.GetOrDefault<INamedList>("facet_ranges");
        }
Beispiel #2
0
        /// <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);
        }
        /// <summary>
        /// Finds or creates the device specified in the settings node tree.
        /// </summary>
        /// <param name="deviceNode">The device node.</param>
        /// <returns></returns>
        public virtual IDevice FindOrCreateDevice(SettingsNode deviceNode)
        {
            string deviceName = deviceNode.Name;

            if (deviceNode.ExistsAndHasAValue <string>("Name"))
            {
                deviceName = deviceNode["Name"].GetValueAs <string>();
            }

            if (!deviceNode.ContainsName("Model"))
            {
                Log.Error(this, "Device '{0}' has an undefined model type. Ensure that the device settings include a 'Model' node.", deviceName);
                return(null);
            }

            string modelName = deviceNode["Model"].GetValueAs <string>();
            string modelKey  = modelName.ToLower();

            if (!_validDeviceTypes.ContainsKey(modelKey))
            {
                Log.Error(this, "Device '{0}' has an unresolved device model type '{1}'. Device cannot be created.", deviceName, modelName);
                return(null);
            }

            IDevice     device = null;
            IDevicePort port   = null;

            device = _devices.FirstOrDefault(d => deviceName.Equals(d.Name, StringComparison.CurrentCultureIgnoreCase));

            if (device == null)
            {
                SettingsNode portNode = deviceNode["Port"];
                if (portNode == null)
                {
                    Log.Error(this, "Device '{0}' is using the default port.", deviceName);
                }
                else
                {
                    try
                    {
                        Log.Info(this, "Device '{0}' discovering port settings...", deviceName);

                        port = CreatePort(portNode);

                        Log.Info(this, "Device '{0}' port settings = {1}", deviceName, port);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(this, "Device '{0}' has invalid port settings. {1}", deviceName, ex);
                        Log.Error(this, "Device '{0}' is switching to the default port.", deviceName);
                    }

                    device = _devices.FirstOrDefault(d =>
                                                     modelName.Equals(d.GetType().Name, StringComparison.CurrentCultureIgnoreCase) &&
                                                     port.Equals(d.Port));

                    if (device != null)
                    {
                        Log.Error(this, "Device '{0}' has the same port settings as Device '{1}'.", deviceName, device.Name);
                    }
                }

                try
                {
                    Type type = _validDeviceTypes[modelKey];
                    device = (IDevice)Activator.CreateInstance(type);
                }
                catch (Exception ex)
                {
                    Log.Error(this, "Device '{0}' could not be instantiated.", deviceName);
                    Log.Error(this, ex.ToString());
                    return(null);
                }

                device.Name = deviceName;
                device.Port = port;

                _devices.Add(device);
            }

            if (deviceNode.ExistsAndHasAValue <bool>("Logging"))
            {
                device.Logging = deviceNode["Logging"].GetValueAs <bool>();
            }
            else if (deviceNode.ExistsAndHasAValue <bool>("Log"))
            {
                device.Logging = deviceNode["Log"].GetValueAs <bool>();
            }

            if (device.Logging)
            {
                Log.Info(this, "Device '{0}' logging is enabled.", deviceName);
            }

            return(device);
        }