public DeviceNode(IDevicePort port) : base("Dev Port", InputPort.CreateMany(), OutputPort.CreateMany(OutputPort.Create("out", PortDataType.Array))) { Port = port; Port.OnBufferReady += Port_OnBufferReady; Port.SamplerateChanged += Port_SamplerateChanged; OutputPorts[0].Samplerate = Port.Samplerate; }
/// <summary> /// Finds or creates the device with the following name or type/port shorthand. /// </summary> /// <param name="nameOrShorthand">The device name or type/port shorthand.</param> /// <returns></returns> public virtual IDevice FindOrCreateDevice(string nameOrShorthand) { IDevice device = null; IDevicePort port = null; device = _devices.FirstOrDefault(d => nameOrShorthand.Equals(d.Name, StringComparison.CurrentCultureIgnoreCase)); if (device != null) { return(device); } string[] typeAndPort = nameOrShorthand.Split(' '); string modelName = typeAndPort[0]; string modelKey = modelName.ToLower(); if (typeAndPort.Length > 1) { port = CreatePort(typeAndPort[1]); } device = _devices.FirstOrDefault(d => modelName.Equals(d.GetType().Name, StringComparison.CurrentCultureIgnoreCase) && (port == null || port.Equals(d.Port))); if (device != null) { return(device); } if (!_validDeviceTypes.ContainsKey(modelKey)) { Log.Error(this, "Device '{0}' has an unresolved device model type '{1}'. Device cannot be created.", nameOrShorthand, modelName); return(null); } try { Type type = _validDeviceTypes[modelKey]; device = (IDevice)Activator.CreateInstance(type); device.Name = modelName; device.Port = port; return(device); } catch (Exception ex) { Log.Error(this, "{0} device could not be instantiated.", modelName); Log.Error(this, ex.ToString()); return(null); } }
private void P_StateChanged(IDevicePort port, DevicePortStatus state) { var activeCount = _ports.Count(p => p.Status == DevicePortStatus.Active); if (activeCount == 0) { return; } foreach (var p in _ports) { p.Samplerate = MaxSamplerate / activeCount; } }
/// <summary> /// Gets the hardware device of the specified type name and the specified port. /// </summary> /// <param name="deviceType">Type of the device.</param> /// <param name="port">The port.</param> /// <returns>The device.</returns> public virtual IDevice GetDevice(string deviceType, IDevicePort port) { foreach (IDevice device in _devices) { if (!String.Equals(device.GetType().Name, deviceType, StringComparison.CurrentCultureIgnoreCase)) { continue; } if (device.Port.Equals(port)) { return(device); } } return(null); }
public IDevicePort GetOwnedPortFromPort(IDevicePort port) { foreach (var dev in _devices) { if (dev.UniqueId.Equals(port.Owner.UniqueId)) { foreach (var p in dev.Ports) { if (p.UniqueId.Equals(port.UniqueId)) { if (p.Name == port.Name && p.Owner.Name == port.Owner.Name) { return(p); } } } } } return(null); }
private void Port_OnBufferReady(IDevicePort sender, double[] buffer) { ((DataOutputPort)OutputPorts[0]).SendData(buffer); }
private void Port_SamplerateChanged(IDevicePort sender, int newRate) { OutputPorts[0].Samplerate = newRate; }
/// <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); }