/// <summary>
        /// Generate a Port Configuration for the specified device.
        /// </summary>
        /// <param name="model"></param>
        /// <param name="name"></param>
        /// <param name="hardwareIdentifier"></param>
        /// <param name="serviceUri"></param>
        /// <returns></returns>
        private PortConfiguration GeneratePortConfiguration(string model, string name, int hardwareIdentifier, string serviceUri)
        {
            PortConfiguration sensorConfig = new PortConfiguration(hardwareIdentifier, name, 0.0, 0.0);
            bool found = false;

            foreach (SensorConfiguration config in _state.SensorConfiguration)
            {
                if (config.DeviceModel != model)
                {
                    continue;
                }

                found = true;
                sensorConfig.SuccessRangeMin        = config.SuccessRangeMin;
                sensorConfig.SuccessRangeMax        = config.SuccessRangeMax;
                sensorConfig.AnalogSensorServiceUri = serviceUri;

                // If we found a name override, we are done.
                if (!string.IsNullOrEmpty(config.DeviceName) && config.DeviceName == name)
                {
                    break;
                }

                // otherwise, keep looking for an override.
            }

            if (!found)
            {
                return(null);
            }

            return(sensorConfig);
        }
        /// <summary>
        /// Analog Sensor Notification Handler
        /// </summary>
        /// <param name="notification"></param>
        private void AnalogSensorNotificationHandler(analog.Replace notification)
        {
            LogVerbose(LogGroups.Console, string.Format("Sensor Notification: {0} {1}", notification.Body.HardwareIdentifier, notification.Body.RawMeasurement));

            foreach (SensorRange key in _state.RuntimeConfiguration.Keys)
            {
                if (key.HardwareIdentifier != notification.Body.HardwareIdentifier)
                {
                    continue;
                }

                PortConfiguration sensorConfig      = _state.RuntimeConfiguration[key];
                string            contactSensorName = key.ContactSensorName;

                int priorIx = _contactSensorArrayState.Sensors.FindIndex(
                    delegate(bumper.ContactSensor gencs)
                {
                    return(gencs.HardwareIdentifier == notification.Body.HardwareIdentifier &&
                           gencs.Name == contactSensorName);
                });
                bool priorPressed = (priorIx < 0) ? false : _contactSensorArrayState.Sensors[priorIx].Pressed;

                // Send a ContactSensor notification here.
                bumper.ContactSensor cs = new bumper.ContactSensor(sensorConfig.HardwareIdentifier, contactSensorName);
                cs.Pressed           = sensorConfig.Pressed(notification.Body.RawMeasurement);
                sensorConfig.Contact = cs.Pressed;

                if (priorIx < 0)
                {
                    _contactSensorArrayState.Sensors.Add(cs);
                }

                if (priorIx < 0 || priorPressed != cs.Pressed)
                {
                    if (priorIx >= 0)
                    {
                        _contactSensorArrayState.Sensors[priorIx].Pressed = cs.Pressed;
                    }

                    SendNotification <bumper.Update>(_subMgrPort, new bumper.Update(cs));
                }
            }
        }
        /// <summary>
        /// Process Brick State
        /// </summary>
        /// <param name="brickState"></param>
        private IEnumerator <ITask> ProcessBrickState(brick.NxtBrickState brickState)
        {
            foreach (string key in brickState.Runtime.Devices.Keys)
            {
                brick.AttachRequest device = brickState.Runtime.Devices[key];
                if (device.Registration.DeviceType != LegoDeviceType.AnalogSensor &&
                    device.Registration.DeviceType != LegoDeviceType.DigitalSensor)
                {
                    continue;
                }

                PortSet <DsspDefaultLookup, DsspDefaultGet> lookupPort = ServiceForwarder <PortSet <DsspDefaultLookup, DsspDefaultGet> >(device.Registration.ServiceUri);
                DsspDefaultLookup lu = new DsspDefaultLookup();
                lookupPort.Post(lu);
                yield return(Arbiter.Choice(lu.ResponsePort,
                                            delegate(LookupResponse luResponse)
                {
                    foreach (PartnerType pt in luResponse.PartnerList)
                    {
                        // See if this service supports the analog sensor contract
                        if (pt.Contract == analog.Contract.Identifier)
                        {
                            // Check if we have already processed this one.
                            if (_sensorList.ContainsKey(pt.Service))
                            {
                                break;
                            }

                            string name = device.Registration.Name;
                            string model = device.Registration.DeviceModel;
                            int hardwareIdentifier = NxtCommon.HardwareIdentifier(device.Registration.Connection.Port);

                            LogVerbose(LogGroups.Console, string.Format("Configuring {0}:{1} on {2} with analog service at {3}", model, name, hardwareIdentifier, pt.Service));
                            analog.AnalogSensorOperations sensorPort = ServiceForwarder <analog.AnalogSensorOperations>(pt.Service);
                            Activate(Arbiter.Choice(sensorPort.Subscribe(_analogSensorNotificationsPort),
                                                    delegate(SubscribeResponseType response)
                            {
                                // Keep track of the subscription manager response
                                // so that we can unsubscribe later.
                                _sensorList.Add(pt.Service, response);
                            },
                                                    delegate(Fault fault)
                            {
                                LogError(LogGroups.Console, string.Format("Failure subscribing to {0} on port {1}.", model, hardwareIdentifier));
                            }));

                            foreach (SensorConfiguration cfg in _state.SensorConfiguration)
                            {
                                if (cfg.DeviceModel != model)
                                {
                                    continue;
                                }

                                SensorRange range = new SensorRange(hardwareIdentifier, model, name, cfg.RangeName);
                                PortConfiguration portConfig = new PortConfiguration(hardwareIdentifier, range.ContactSensorName, cfg.SuccessRangeMin, cfg.SuccessRangeMax);
                                portConfig.AnalogSensorServiceUri = pt.Service;

                                if (portConfig != null)
                                {
                                    _state.RuntimeConfiguration.Add(range, portConfig);
                                }
                            }
                            break;
                        }
                    }
                },
                                            delegate(Fault f) { }));
            }
        }