예제 #1
0
 public void UpdateFromSettingsNode(SettingsNode node)
 {
     if (node.ExistsAndHasAValue <int>("Index"))
     {
         Index = node["Index"].GetValueAs <int>();
     }
     if (node.ExistsAndHasAValue <HGAStatus>("Hga_Status"))
     {
         Hga_Status = node["Hga_Status"].GetValueAs <HGAStatus>();
     }
 }
예제 #2
0
 /// <summary>
 /// Return the value from the node that matches nodelabel or return the defaultValue
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="node"></param>
 /// <param name="nodeLabel"></param>
 /// <param name="defaultValue"></param>
 /// <returns></returns>
 public static T UpdateFromNode <T>(SettingsNode node, string nodeLabel, T defaultValue)
 {
     if (node.ExistsAndHasAValue <T>(nodeLabel))
     {
         return(node[nodeLabel].GetValueAs <T>());
     }
     else
     {
         return(defaultValue);
     }
 }
예제 #3
0
        /// <summary>
        /// Configures the device based on the specified device settings. This is called before <see cref="IPart"/> creation.
        /// </summary>
        public void Configure(SettingsNode settings)
        {
            string configChannelList = "Out,Out,Out,Out";

            if (settings.ExistsAndHasAValue <string>("ConfigChannels"))
            {
                configChannelList = settings["ConfigChannels"].GetValueAs <string>();
            }

            string[] configChannels = configChannelList.Split(',');
            int      configCount    = Math.Min(configChannels.Length, _numConfigChannels);

            for (int i = 0; i < _numConfigChannels; i++)
            {
                _configOuts[i] = true;
            }

            for (int i = 0; i < configCount; i++)
            {
                _configOuts[i] = (configChannels[i].TrimStart().StartsWith("I", StringComparison.CurrentCultureIgnoreCase));
            }

            // Can we do this through software, or only jumpers?
        }
예제 #4
0
        public virtual IList <string> CreateParts(SettingsNode settings, out IList <TPart> parts)
        {
            List <string> forcedNames = new List <string>();

            parts = new List <TPart>();

            foreach (SettingsNode node in settings.Nodes)
            {
                if (node == null || !PartsNodeNames.Contains(node.Name))
                {
                    continue;
                }

                foreach (SettingsNode partNode in node.Nodes)
                {
                    TPart  part       = default(TPart);
                    string deviceName = DefaultDeviceName;

                    if (SettingsNode.ExistsAndHasAValue(partNode["Device"]))
                    {
                        deviceName = partNode["Device"].GetValueAs <string>();
                    }
                    else if (SettingsNode.ExistsAndHasAValue(partNode["Controller"]))
                    {
                        deviceName = partNode["Controller"].GetValueAs <string>();
                    }

                    // Allow Simulated to represent the current hardware component simulated type
                    if (string.Equals(deviceName, "Simulated", StringComparison.CurrentCultureIgnoreCase) ||
                        string.Equals(deviceName, "Simulate", StringComparison.CurrentCultureIgnoreCase))
                    {
                        deviceName = GetSimDeviceType().Name;
                    }

                    TDevice device = GetDevice(deviceName);

                    if (device == null)
                    {
                        string simName = GetSimDeviceType().Name;

                        if (!String.Equals(deviceName, simName, StringComparison.CurrentCultureIgnoreCase))
                        {
                            Log.Error(this, "No device found with the name '{0}'. Switching to a Simulated {1} device instead.", deviceName, Name);
                        }

                        device = GetSimDevice(deviceName);
                    }

                    string partName = partNode.Name;

                    try
                    {
                        if (!device.Connected)
                        {
                            device.Open();
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(this, "{0} could not connect to {1} device '{2}' on port {3}. {4}", typeof(TPart).Name, device.GetType().Name, device.Name, device.Port, ex);
                        Log.Error(this, "Switching {0} '{1}' to simulated device instead.", typeof(TPart).Name, partName);
                        device = GetSimDevice(deviceName);
                    }

                    part = CreatePart(partNode, device);

                    if (!String.Equals(device.Name, deviceName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        forcedNames.Add(part.Name);
                    }

                    parts.Add(part);
                }
            }

            return(forcedNames);
        }
예제 #5
0
        /// <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);
        }