Example #1
0
        /// <summary>
        /// Subscribe to appropriate sensor type and port on NXT
        /// </summary>
        private void SubscribeToNXT()
        {
            // Create a notification port
            legoNXT.LegoNxtOperations _notificationPort = new legoNXT.LegoNxtOperations();

            //create a custom subscription request
            legoNXT.CustomSubscribeRequestType request = new legoNXT.CustomSubscribeRequestType();

            //select only the sensor and port we want
            //NOTE: this name must match the NXT sensor name. (see NXT readme)
            request.Sensors = new Collection <legoNXT.SensorDefinition>();
            legoNXT.SensorDefinition sensor = new legoNXT.SensorDefinition();
            sensor.Type = legoNXT.SensorDefinition.SensorType.Encoder;
            sensor.Port = _state.HardwareIdentifier;
            request.Sensors.Add(sensor);

            //Subscribe to the NXT and wait for a response
            Activate(
                Arbiter.Choice(_legoPort.SelectiveSubscribe(request, _notificationPort),
                               delegate(SubscribeResponseType Rsp)
            {
                //update our state with subscription status
                _subscribed = true;

                LogInfo(sensor.Type + sensor.Port + " subscription success");

                //Subscription was successful, start listening for sensor change notifications
                _mainInterleave.CombineWith(new Interleave(
                                                new ExclusiveReceiverGroup(
                                                    Arbiter.Receive <legoNXT.Configure>(true, _notificationPort, SensorNotificationHandler)
                                                    ),
                                                new ConcurrentReceiverGroup()
                                                ));

                //Activate(

                //);
            },
                               delegate(Fault F)
            {
                LogError(sensor.Type + sensor.Port + "subscription failed");
            }
                               )
                );
        }
Example #2
0
        /// <summary>
        /// Subscribe to appropriate sensor type and port on NXT
        /// </summary>
        private void SubscribeToNXT()
        {
            // Create a notification port
            legoNXT.LegoNxtOperations _notificationPort = new legoNXT.LegoNxtOperations();

            //create a custom subscription request
            legoNXT.CustomSubscribeRequestType request = new legoNXT.CustomSubscribeRequestType();

            //select only the sensor and port we want
            //NOTE: this name must match the NXT sensor name. (see NXT readme)
            request.Sensors = new Collection <legoNXT.SensorDefinition>();

            legoNXT.SensorDefinition sensor1 = new legoNXT.SensorDefinition();
            sensor1.Type = legoNXT.SensorDefinition.SensorType.Motor;
            sensor1.Port = _state.Motor1Port;
            request.Sensors.Add(sensor1);

            legoNXT.SensorDefinition sensor2 = new legoNXT.SensorDefinition();
            sensor2.Type = legoNXT.SensorDefinition.SensorType.Motor;
            sensor2.Port = _state.Motor2Port;
            request.Sensors.Add(sensor2);

            //Subscribe to the NXT and wait for a response
            Activate(
                Arbiter.Choice(_legoPort.SelectiveSubscribe(request, _notificationPort),
                               delegate(SubscribeResponseType Rsp)
            {
                //update our state with subscription status
                _subscribed = true;

                LogInfo("lego backup subscription success");

                //Subscription was successful, start listening for sensor change notifications
                Activate(
                    Arbiter.Receive <legoNXT.Configure>(true, _notificationPort, SensorNotificationHandler)
                    );
            },
                               delegate(Fault F)
            {
                LogError(sensor1.Type + sensor1.Port + "subscription failed");
            }
                               )
                );
        }
Example #3
0
        protected override void Start()
        {
            //configure default state
            if (_state == null)
            {
                _state = new LegoNxtBatteryState();
                _state.PollDelayTime   = 10000; //10 seconds
                _state.MaxBatteryPower = 9.0;

                SaveState(_state);
            }

            // Listen on the main port for requests and call the appropriate handler.
            Interleave mainInterleave = ActivateDsspOperationHandlers();

            //listen on alternate service port for requests and call the appropriate handler.
            mainInterleave.CombineWith(new Interleave(
                                           new TeardownReceiverGroup(
                                               Arbiter.Receive <DsspDefaultDrop>(false, _batteryPort, DefaultDropHandler)
                                               ),
                                           new ExclusiveReceiverGroup(
                                               Arbiter.ReceiveWithIterator <battery.Replace>(true, _batteryPort, ReplaceHandler),
                                               Arbiter.ReceiveWithIterator <battery.Subscribe>(true, _batteryPort, SubscribeHandler)
                                               ),
                                           new ConcurrentReceiverGroup(
                                               Arbiter.ReceiveWithIterator <battery.Get>(true, _batteryPort, GetHandler),
                                               Arbiter.Receive <DsspDefaultLookup>(true, _batteryPort, DefaultLookupHandler)
                                               )
                                           ));

            // Publish the service to the local Node Directory
            DirectoryInsert();

            // display HTTP service Uri
            LogInfo(LogGroups.Console, "Service uri: ");

            //wait until the nxt is connected to start polling for the battery level

            // Create a temporary notification port
            legoNXT.LegoNxtOperations _notificationPort = new legoNXT.LegoNxtOperations();

            Activate(
                Arbiter.Choice(_legoPort.Subscribe(_notificationPort),
                               delegate(SubscribeResponseType Rsp)
            {
                LogInfo("Lego NXT battery initial subscription success.  Waiting for NXT to connect.");

                //Subscription was successful, start listening for sensor change notifications
                Activate(
                    Arbiter.Receive <legoNXT.Configure>(false, _notificationPort,
                                                        delegate(legoNXT.Configure msg)
                {
                    //start polling
                    LogInfo("NXT Battery service notified of NXT connection");
                    _timerPort.Post(DateTime.Now);
                    Activate(Arbiter.Receive(true, _timerPort, TimerHandler));
                })
                    );
            },
                               delegate(Fault F)
            {
                LogError("Lego NXT battery initial subscription failed");
            }
                               )
                );
        }