Ejemplo n.º 1
0
 public PlayTone(PlayToneBody b)
 {
     base.Body = b;
 }
Ejemplo n.º 2
0
        public IEnumerator <ITask> HttpPostHandler(HttpPost httpPost)
        {
            // Use helper to read form data
            ReadFormData readForm = new ReadFormData(httpPost.Body.Context);

            _httpUtilities.Post(readForm);

            // Wait for result
            Activate(Arbiter.Choice(readForm.ResultPort,
                                    delegate(NameValueCollection parameters)
            {
                if (!string.IsNullOrEmpty(parameters["Action"]) &&
                    parameters["Action"] == "ScribblerConfig")
                {
                    if (parameters["buttonOk"] == "Change" && _state.Connected)
                    {
                        SetNameBody newname    = new SetNameBody(parameters["Name"]);
                        SetName newnamemessage = new SetName(newname);
                        _mainPort.Post(newnamemessage);
                        Activate(
                            Arbiter.Choice(
                                Arbiter.Receive <DefaultUpdateResponseType>(false, newnamemessage.ResponsePort,
                                                                            delegate(DefaultUpdateResponseType response)
                        {
                            HttpPostSuccess(httpPost);
                        }),
                                Arbiter.Receive <Fault>(false, newnamemessage.ResponsePort,
                                                        delegate(Fault f)
                        {
                            HttpPostFailure(httpPost, f.Reason[0].Value);
                        })
                                )
                            );
                    }
                    else if (parameters["buttonOk"] == "Connect" && _state.Connected)
                    {
                        //close down this connection to make a new connection below

                        PollTimer.Close();

                        System.Threading.Thread.Sleep(100);

                        _scribblerCom.Close();

                        _state.Connected = false;
                    }

                    if (parameters["buttonOk"] == "Connect" && !_state.Connected)
                    {
                        int port = 0;
                        int.TryParse(parameters["ComPort"], out port);
                        string name = parameters["Name"];
                        if (!string.IsNullOrEmpty(name) && name.Length > 8)
                        {
                            name = name.Substring(0, 8);
                        }

                        _state.ComPort   = port;
                        _state.RobotName = name;

                        //open Scribbler Communications port
                        if (ConnectToScribbler())
                        {
                            // Listen for a single Serial port request with an acknowledgement
                            Activate(Arbiter.ReceiveWithIterator <SendScribblerCommand>(false, _scribblerComPort, SendScribblerCommandHandler));

                            PollTimer           = new System.Timers.Timer();
                            PollTimer.Interval  = TimerDelay;
                            PollTimer.AutoReset = true;
                            PollTimer.Elapsed  += new System.Timers.ElapsedEventHandler(PollTimer_Elapsed);
                            PollTimer.Start();

                            //play startup tone
                            PlayToneBody startTone   = new PlayToneBody(200, 1000, 2000);
                            PlayTone playToneMessage = new PlayTone(startTone);
                            _mainPort.Post(playToneMessage);

                            Activate(
                                Arbiter.Choice(
                                    Arbiter.Receive <DefaultUpdateResponseType>(false, playToneMessage.ResponsePort,
                                                                                delegate(DefaultUpdateResponseType response)
                            {
                                HttpPostSuccess(httpPost);
                            }),
                                    Arbiter.Receive <Fault>(false, playToneMessage.ResponsePort,
                                                            delegate(Fault f)
                            {
                                HttpPostFailure(httpPost, f.Reason[0].Value);
                            })
                                    )
                                );
                        }
                        else
                        {
                            HttpPostFailure(httpPost, "Connection to Scribbler failed");
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(parameters["Action"]) &&
                         parameters["Action"] == "ScribblerSensors")
                {
                    if (parameters["buttonOk"] == "Poll" && _state.Connected)
                    {
                        ScribblerCommand cmd         = new ScribblerCommand((byte)ScribblerHelper.Commands.GET_ALL);
                        SendScribblerCommand sendcmd = new SendScribblerCommand(cmd);
                        _scribblerComPort.Post(sendcmd);
                        Activate(
                            Arbiter.Choice(
                                Arbiter.Receive <ScribblerResponse>(false, sendcmd.ResponsePort,
                                                                    delegate(ScribblerResponse response)
                        {
                            HttpPostSuccess(httpPost);
                        }),
                                Arbiter.Receive <Fault>(false, sendcmd.ResponsePort,
                                                        delegate(Fault f)
                        {
                            HttpPostFailure(httpPost, f.Reason[0].Value);
                        })
                                )
                            );
                    }
                }
                else if (!string.IsNullOrEmpty(parameters["Action"]) &&
                         parameters["Action"] == "ScribblerMotors")
                {
                    if (parameters["buttonOk"] == "Set" && _state.Connected)
                    {
                        int left  = _state.MotorLeft;
                        int right = _state.MotorRight;
                        int.TryParse(parameters["LeftMotor"], out left);
                        int.TryParse(parameters["RightMotor"], out right);

                        SetMotorsBody setMotorsBody = new SetMotorsBody(left, right);
                        SetMotors setMotorsRequest  = new SetMotors(setMotorsBody);

                        _mainPort.Post(setMotorsRequest);

                        Activate(
                            Arbiter.Choice(
                                Arbiter.Receive <DefaultUpdateResponseType>(false, setMotorsRequest.ResponsePort,
                                                                            delegate(DefaultUpdateResponseType response)
                        {
                            HttpPostSuccess(httpPost);
                        }),
                                Arbiter.Receive <Fault>(false, setMotorsRequest.ResponsePort,
                                                        delegate(Fault f)
                        {
                            HttpPostFailure(httpPost, f.Reason[0].Value);
                        })
                                )
                            );
                    }
                    else if (parameters["buttonOk"] == "All Stop" && _state.Connected)
                    {
                        SetMotorsBody setMotorsBody = new SetMotorsBody(100, 100);
                        SetMotors setMotorsRequest  = new SetMotors(setMotorsBody);

                        _mainPort.Post(setMotorsRequest);

                        Activate(
                            Arbiter.Choice(
                                Arbiter.Receive <DefaultUpdateResponseType>(false, setMotorsRequest.ResponsePort,
                                                                            delegate(DefaultUpdateResponseType response)
                        {
                            HttpPostSuccess(httpPost);
                        }),
                                Arbiter.Receive <Fault>(false, setMotorsRequest.ResponsePort,
                                                        delegate(Fault f)
                        {
                            HttpPostFailure(httpPost, f.Reason[0].Value);
                        })
                                )
                            );
                    }
                }
                else if (!string.IsNullOrEmpty(parameters["Action"]) &&
                         parameters["Action"] == "ScribblerLEDs")
                {
                    if (parameters["buttonOk"] == "Set" && _state.Connected)
                    {
                        bool left   = ((parameters["LeftLED"] ?? "off") == "on");
                        bool center = ((parameters["CenterLED"] ?? "off") == "on");
                        bool right  = ((parameters["RightLED"] ?? "off") == "on");

                        SetAllLedsBody leds   = new SetAllLedsBody(left, center, right);
                        SetAllLEDs setAllLeds = new SetAllLEDs(leds);
                        _mainPort.Post(setAllLeds);

                        Activate(
                            Arbiter.Choice(
                                Arbiter.Receive <DefaultUpdateResponseType>(false, setAllLeds.ResponsePort,
                                                                            delegate(DefaultUpdateResponseType response)
                        {
                            HttpPostSuccess(httpPost);
                        }),
                                Arbiter.Receive <Fault>(false, setAllLeds.ResponsePort,
                                                        delegate(Fault f)
                        {
                            HttpPostFailure(httpPost, f.Reason[0].Value);
                        })
                                )
                            );
                    }
                }
                else if (!string.IsNullOrEmpty(parameters["Action"]) &&
                         parameters["Action"] == "ScribblerSpeaker")
                {
                    if (parameters["buttonOk"] == "Play" && _state.Connected)
                    {
                        int tone1    = 0;
                        int tone2    = 0;
                        int duration = 0;
                        int.TryParse(parameters["Tone1"], out tone1);
                        int.TryParse(parameters["Tone2"], out tone2);
                        int.TryParse(parameters["Duration"], out duration);

                        PlayToneBody playTone    = new PlayToneBody(duration, tone1, tone2);
                        PlayTone playToneMessage = new PlayTone(playTone);
                        _mainPort.Post(playToneMessage);

                        Activate(
                            Arbiter.Choice(
                                Arbiter.Receive <DefaultUpdateResponseType>(false, playToneMessage.ResponsePort,
                                                                            delegate(DefaultUpdateResponseType response)
                        {
                            HttpPostSuccess(httpPost);
                        }),
                                Arbiter.Receive <Fault>(false, playToneMessage.ResponsePort,
                                                        delegate(Fault f)
                        {
                            HttpPostFailure(httpPost, f.Reason[0].Value);
                        })
                                )
                            );
                    }
                }
                else
                {
                    HttpPostFailure(httpPost, "Unknown Http Post");
                }
            },
                                    delegate(Exception Failure)
            {
                LogError(Failure.Message);
            })
                     );
            yield break;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Service Start
        /// </summary>
        protected override void Start()
        {
            if (_state == null)
            {
                //initialize state
                _state           = new ScribblerState();
                _state.ComPort   = 0;
                _state.RobotName = null;

                //motors initially stopped
                _state.MotorLeft  = 100;
                _state.MotorRight = 100;

                SaveState(_state);
            }

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

            //open Scribbler Communications port
            if (ConnectToScribbler())
            {
                // Listen for a single Serial port request with an acknowledgement
                Activate(Arbiter.ReceiveWithIterator <SendScribblerCommand>(false, _scribblerComPort, SendScribblerCommandHandler));

                PollTimer           = new System.Timers.Timer();
                PollTimer.Interval  = TimerDelay;
                PollTimer.AutoReset = true;
                PollTimer.Elapsed  += new System.Timers.ElapsedEventHandler(PollTimer_Elapsed);
                PollTimer.Start();


                //play startup tone
                PlayToneBody startTone = new PlayToneBody(200, 1000, 2000);
                _mainPort.Post(new PlayTone(startTone));

                //debug
                //ScribblerCommand cmd = new ScribblerCommand((byte)ScribblerHelper.Commands.GET_INFO);
                //SendScribblerCommand sendcmd = new SendScribblerCommand(cmd);
                //_scribblerComPort.Post(sendcmd);


                //fix state
                _state.MotorLeft  = 100;
                _state.MotorRight = 100;
                _state.LEDLeft    = false;
                _state.LEDRight   = false;
                _state.LEDCenter  = false;
            }
            else
            {
                //no scribbler found. Open state page for manual settings.
                //OpenServiceInBrowser();
            }


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

            //for HttpPost
            _httpUtilities = DsspHttpUtilitiesService.Create(Environment);

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

            //add custom handlers to interleave
            mainInterleave.CombineWith(new Interleave(
                                           new TeardownReceiverGroup(),
                                           new ExclusiveReceiverGroup(
                                               Arbiter.ReceiveWithIterator <SetMotors>(true, _mainPort, SetMotorHandler),
                                               Arbiter.ReceiveWithIterator <SetLED>(true, _mainPort, SetLEDHandler),
                                               Arbiter.ReceiveWithIterator <SetAllLEDs>(true, _mainPort, SetAllLEDsHandler),
                                               Arbiter.ReceiveWithIterator <PlayTone>(true, _mainPort, PlayToneHandler),
                                               Arbiter.ReceiveWithIterator <SetName>(true, _mainPort, SetNameHandler),
                                               Arbiter.ReceiveWithIterator <ScribblerResponseMessage>(true, _mainPort, ScribblerResponseHandler)
                                               ),
                                           new ConcurrentReceiverGroup()
                                           ));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Http Post Handler.  Handles http form inputs
        /// </summary>
        //[ServiceHandler(ServiceHandlerBehavior.Concurrent)]
        public IEnumerator<ITask> HttpPostHandler(HttpPost httpPost)
        {
            // Use helper to read form data
            ReadFormData readForm = new ReadFormData(httpPost);
            _httpUtilities.Post(readForm);

            // Read form data
            NameValueCollection parameters = null;
            yield return Arbiter.Choice(readForm.ResultPort,
                delegate(NameValueCollection p) { parameters = p; },
                delegate(Exception e) { throw new Exception("Error reading form data", e); });

            // Act on form data
            if (!string.IsNullOrEmpty(parameters["Action"])
                  && parameters["Action"] == "ScribblerConfig")
            {
                if (parameters["buttonOk"] == "Change" && _state.Connected)
                {
                    SetNameBody newname = new SetNameBody(parameters["Name"]);
                    SetName newnamemessage = new SetName(newname);
                    _mainPort.PostUnknownType(newnamemessage);
                    Activate(
                        Arbiter.Choice(
                            Arbiter.Receive<DefaultUpdateResponseType>(false, newnamemessage.ResponsePort,
                                delegate(DefaultUpdateResponseType response)
                                {
                                    HttpPostSuccess(httpPost);
                                }),
                            Arbiter.Receive<Fault>(false, newnamemessage.ResponsePort,
                                delegate(Fault f)
                                {
                                    HttpPostFailure(httpPost, f.Reason[0].Value);
                                })
                        )
                    );

                }
                else if (parameters["buttonOk"] == "Connect" && _state.Connected)
                {
                    //close down this connection to make a new connection below

                    PollTimer.Close();

                    System.Threading.Thread.Sleep(100);

                    _scribblerCom.Close();

                    _state.Connected = false;

                    //HttpPostSuccess(httpPost);
                }

                if (parameters["buttonOk"] == "Connect" && !_state.Connected)
                {
                    int port = 0;
                    int.TryParse(parameters["ComPort"], out port);
                    string name = parameters["Name"];
                    if (!string.IsNullOrEmpty(name) && name.Length > 8)
                        name = name.Substring(0, 8);

                    _state.ComPort = port;
                    _state.RobotName = name;

                    //open Scribbler Communications port
                    LogInfo("connecting to scribbler...");
                    Reconnect rec = new Reconnect();
                    _mainPort.PostUnknownType(rec);
                    yield return Arbiter.Choice(rec.ResponsePort,
                        delegate(DefaultUpdateResponseType r)
                        {
                            LogInfo("connected, sending http reply");
                            HttpPostSuccess(httpPost);
                            LogInfo("http reply sent");
                        },
                        delegate(Fault f)
                        {
                            httpPost.ResponsePort.Post(f);
                        });
                }
            }
            else if (!string.IsNullOrEmpty(parameters["Action"])
                  && parameters["Action"] == "ScribblerSensors")
            {
                if (parameters["buttonOk"] == "Poll" && _state.Connected)
                {
                    ScribblerCommand cmd = new ScribblerCommand(ScribblerHelper.Commands.GET_ALL);
                    SendScribblerCommand sendcmd = new SendScribblerCommand(cmd);
                    _scribblerComPort.Post(sendcmd);
                    Activate(
                        Arbiter.Choice(
                            Arbiter.Receive<ScribblerResponse>(false, sendcmd.ResponsePort,
                                delegate(ScribblerResponse response)
                                {
                                    HttpPostSuccess(httpPost);
                                }),
                            Arbiter.Receive<Fault>(false, sendcmd.ResponsePort,
                                delegate(Fault f)
                                {
                                    HttpPostFailure(httpPost, f.Reason[0].Value);
                                })
                        )
                    );
                }
            }
            else if (!string.IsNullOrEmpty(parameters["Action"])
                && parameters["Action"] == "ScribblerMotors")
            {
                if (parameters["buttonOk"] == "Set" && _state.Connected)
                {
                    int left = _state.MotorLeft;
                    int right = _state.MotorRight;
                    int.TryParse(parameters["LeftMotor"], out left);
                    int.TryParse(parameters["RightMotor"], out right);

                    SetMotorsBody setMotorsBody = new SetMotorsBody(left, right);
                    SetMotors setMotorsRequest = new SetMotors(setMotorsBody);

                    _mainPort.PostUnknownType(setMotorsRequest);

                    Activate(
                        Arbiter.Choice(
                            Arbiter.Receive<DefaultUpdateResponseType>(false, setMotorsRequest.ResponsePort,
                                delegate(DefaultUpdateResponseType response)
                                {
                                    HttpPostSuccess(httpPost);
                                }),
                            Arbiter.Receive<Fault>(false, setMotorsRequest.ResponsePort,
                                delegate(Fault f)
                                {
                                    HttpPostFailure(httpPost, f.Reason[0].Value);
                                })
                        )
                    );
                }
                else if (parameters["buttonOk"] == "All Stop" && _state.Connected)
                {
                    SetMotorsBody setMotorsBody = new SetMotorsBody(100, 100);
                    SetMotors setMotorsRequest = new SetMotors(setMotorsBody);

                    _mainPort.PostUnknownType(setMotorsRequest);

                    Activate(
                        Arbiter.Choice(
                            Arbiter.Receive<DefaultUpdateResponseType>(false, setMotorsRequest.ResponsePort,
                                delegate(DefaultUpdateResponseType response)
                                {
                                    HttpPostSuccess(httpPost);
                                }),
                            Arbiter.Receive<Fault>(false, setMotorsRequest.ResponsePort,
                                delegate(Fault f)
                                {
                                    HttpPostFailure(httpPost, f.Reason[0].Value);
                                })
                        )
                    );
                }
            }
            else if (!string.IsNullOrEmpty(parameters["Action"])
                && parameters["Action"] == "ScribblerLEDs")
            {
                if (parameters["buttonOk"] == "Set" && _state.Connected)
                {
                    bool left = ((parameters["LeftLED"] ?? "off") == "on");
                    bool center = ((parameters["CenterLED"] ?? "off") == "on");
                    bool right = ((parameters["RightLED"] ?? "off") == "on");

                    SetAllLedsBody leds = new SetAllLedsBody(left, center, right);
                    SetAllLEDs setAllLeds = new SetAllLEDs(leds);
                    _mainPort.PostUnknownType(setAllLeds);

                    Activate(
                        Arbiter.Choice(
                            Arbiter.Receive<DefaultUpdateResponseType>(false, setAllLeds.ResponsePort,
                                delegate(DefaultUpdateResponseType response)
                                {
                                    HttpPostSuccess(httpPost);
                                }),
                            Arbiter.Receive<Fault>(false, setAllLeds.ResponsePort,
                                delegate(Fault f)
                                {
                                    HttpPostFailure(httpPost, f.Reason[0].Value);
                                })
                        )
                    );

                }
            }
            else if (!string.IsNullOrEmpty(parameters["Action"])
                && parameters["Action"] == "ScribblerSpeaker")
            {
                if (parameters["buttonOk"] == "Play" && _state.Connected)
                {
                    int tone1 = 0;
                    int tone2 = 0;
                    int duration = 0;
                    int.TryParse(parameters["Tone1"], out tone1);
                    int.TryParse(parameters["Tone2"], out tone2);
                    int.TryParse(parameters["Duration"], out duration);

                    PlayToneBody playTone = new PlayToneBody(duration, tone1, tone2);
                    PlayTone playToneMessage = new PlayTone(playTone);
                    _mainPort.PostUnknownType(playToneMessage);

                    Activate(
                        Arbiter.Choice(
                            Arbiter.Receive<DefaultUpdateResponseType>(false, playToneMessage.ResponsePort,
                                delegate(DefaultUpdateResponseType response)
                                {
                                    HttpPostSuccess(httpPost);
                                }),
                            Arbiter.Receive<Fault>(false, playToneMessage.ResponsePort,
                                delegate(Fault f)
                                {
                                    HttpPostFailure(httpPost, f.Reason[0].Value);
                                })
                        )
                    );
                }
            }
            else
            {
                HttpPostFailure(httpPost, "Unknown Http Post");
            }
            yield break;
        }