コード例 #1
0
        /// <summary>
        /// Service start
        /// </summary>
        protected override void Start()
        {
            if (_state == null)
            {
                _state = new ChrUm6OrientationSensorState();
                _state.ChrUm6OrientationSensorConfig          = new ChrUm6OrientationSensorConfig();
                _state.ChrUm6OrientationSensorConfig.CommPort = 0;

                SaveState(_state);
            }
            else
            {
                // Clear old Chr readings
                _state.Quaternion = null;
            }

            _httpUtilities = DsspHttpUtilitiesService.Create(Environment);

            if (_state.ChrUm6OrientationSensorConfig == null)
            {
                _state.ChrUm6OrientationSensorConfig = new ChrUm6OrientationSensorConfig();
            }

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

            _chrConnection = new ChrConnection(_state.ChrUm6OrientationSensorConfig, _chrDataPort);

            SpawnIterator(ConnectToChrUm6OrientationSensor);

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

            mainInterleave.CombineWith(new Interleave(new ExclusiveReceiverGroup(
                                                          Arbiter.Receive <short[]>(true, _chrDataPort, DataReceivedHandler),
                                                          Arbiter.Receive <Exception>(true, _chrDataPort, ExceptionHandler),
                                                          Arbiter.Receive <string>(true, _chrDataPort, MessageHandler)
                                                          ),
                                                      new ConcurrentReceiverGroup()));

            //base.Start(); -- can't have it here, we already started mainInterleave and added to directory via DirectoryInsert
        }
コード例 #2
0
        public IEnumerator <ITask> HttpPostHandler(HttpPost httpPost)
        {
            // Use helper to read form data
            ReadFormData readForm = new ReadFormData(httpPost);

            _httpUtilities.Post(readForm);

            // Wait for result
            Activate(Arbiter.Choice(readForm.ResultPort,
                                    delegate(NameValueCollection parameters)
            {
                if (!string.IsNullOrEmpty(parameters["Action"]) &&
                    parameters["Action"] == "ChrUm6OrientationSensorConfig"
                    )
                {
                    if (parameters["buttonOk"] == "Search")
                    {
                        FindChrConfig findConfig = new FindChrConfig();
                        _mainPort.Post(findConfig);
                        Activate(
                            Arbiter.Choice(
                                Arbiter.Receive <ChrUm6OrientationSensorConfig>(false, findConfig.ResponsePort,
                                                                                delegate(ChrUm6OrientationSensorConfig response)
                        {
                            HttpPostSuccess(httpPost);
                        }),
                                Arbiter.Receive <Fault>(false, findConfig.ResponsePort,
                                                        delegate(Fault f)
                        {
                            HttpPostFailure(httpPost, f);
                        })
                                )
                            );
                    }
                    else if (parameters["buttonOk"] == "Connect")
                    {
                        ChrUm6OrientationSensorConfig config = (ChrUm6OrientationSensorConfig)_state.ChrUm6OrientationSensorConfig.Clone();
                        int port;
                        if (int.TryParse(parameters["CommPort"], out port) && port >= 0)
                        {
                            config.CommPort = port;
                            config.PortName = "COM" + port.ToString();
                        }

                        int baud;
                        if (int.TryParse(parameters["BaudRate"], out baud) && ChrConnection.ValidBaudRate(baud))
                        {
                            config.BaudRate = baud;
                        }

                        Configure configure = new Configure(config);
                        _mainPort.Post(configure);
                        Activate(
                            Arbiter.Choice(
                                Arbiter.Receive <DefaultUpdateResponseType>(false, configure.ResponsePort,
                                                                            delegate(DefaultUpdateResponseType response)
                        {
                            HttpPostSuccess(httpPost);
                        }),
                                Arbiter.Receive <Fault>(false, configure.ResponsePort,
                                                        delegate(Fault f)
                        {
                            HttpPostFailure(httpPost, f);
                        })
                                )
                            );
                    }
                    else if (parameters["buttonOk"] == "Refresh Data")
                    {
                        HttpPostSuccess(httpPost);
                    }
                    else if (parameters["buttonOk"] == "Set Accelerometer Reference Vector")
                    {
                        ChrUm6OrientationSensorCommand cmd = new ChrUm6OrientationSensorCommand()
                        {
                            Command = "SetAccelRefVector"
                        };
                        SendChrUm6OrientationSensorCommand sCmd = new SendChrUm6OrientationSensorCommand(cmd);

                        _mainPort.Post(sCmd);
                        Activate(
                            Arbiter.Choice(
                                Arbiter.Receive <DefaultUpdateResponseType>(false, sCmd.ResponsePort,
                                                                            delegate(DefaultUpdateResponseType response)
                        {
                            HttpPostSuccess(httpPost);
                        }),
                                Arbiter.Receive <Fault>(false, sCmd.ResponsePort,
                                                        delegate(Fault f)
                        {
                            HttpPostFailure(httpPost, f);
                        })
                                )
                            );
                    }
                    else if (parameters["buttonOk"] == "Set Magnetometer Reference Vector")
                    {
                        ChrUm6OrientationSensorCommand cmd = new ChrUm6OrientationSensorCommand()
                        {
                            Command = "SetMagnRefVector"
                        };
                        SendChrUm6OrientationSensorCommand sCmd = new SendChrUm6OrientationSensorCommand(cmd);

                        _mainPort.Post(sCmd);
                        Activate(
                            Arbiter.Choice(
                                Arbiter.Receive <DefaultUpdateResponseType>(false, sCmd.ResponsePort,
                                                                            delegate(DefaultUpdateResponseType response)
                        {
                            HttpPostSuccess(httpPost);
                        }),
                                Arbiter.Receive <Fault>(false, sCmd.ResponsePort,
                                                        delegate(Fault f)
                        {
                            HttpPostFailure(httpPost, f);
                        })
                                )
                            );
                    }
                    else if (parameters["buttonOk"] == "Zero Rate Gyros")
                    {
                        ChrUm6OrientationSensorCommand cmd = new ChrUm6OrientationSensorCommand()
                        {
                            Command = "ZeroRateGyros"
                        };
                        SendChrUm6OrientationSensorCommand sCmd = new SendChrUm6OrientationSensorCommand(cmd);

                        _mainPort.Post(sCmd);
                        Activate(
                            Arbiter.Choice(
                                Arbiter.Receive <DefaultUpdateResponseType>(false, sCmd.ResponsePort,
                                                                            delegate(DefaultUpdateResponseType response)
                        {
                            HttpPostSuccess(httpPost);
                        }),
                                Arbiter.Receive <Fault>(false, sCmd.ResponsePort,
                                                        delegate(Fault f)
                        {
                            HttpPostFailure(httpPost, f);
                        })
                                )
                            );
                    }
                }
                else
                {
                    HttpPostFailure(httpPost, null);
                }
            },
                                    delegate(Exception Failure)
            {
                LogError(Failure.Message);
            })
                     );
            yield break;
        }