private static void Server_RequestReceived(object sender, NHttp.HttpRequestEventArgs e)
        {
            string RequestString = "";

            if (e.Request.HttpMethod == "GET")
            {
                //Ignore all GET requests
                //Show server message
                using (var writer = new StreamWriter(e.Response.OutputStream))
                {
                    writer.Write("<h2>Welcome to ISS NUS Http Server</h2>\n" + "Total post requests served since last startup: " + RequestCount.ToString());
                }
            }

            if (e.Request.HttpMethod == "POST" && e.Request.InputStream != null)
            {
                RequestCount += 1;
                Sys.Http.DialogFlow.Request RequestData;
                using (StreamReader reader = new StreamReader(e.Request.InputStream, Encoding.UTF8))
                {
                    RequestString = reader.ReadToEnd();
                }

                Request.CreateRequestObject(RequestString);
                Sys.Http.DialogFlow.Response Response = new Sys.Http.DialogFlow.Response();

                string        ResponseText = Response.GetResponse(Request.Data);
                StringBuilder sb           = new StringBuilder();
                sb.Append("{\"fulfillmentText\": \"" + ResponseText + "\" }");


                using (var writer = new StreamWriter(e.Response.OutputStream))
                {
                    writer.Write(sb.ToString());
                }
            }
        }
Beispiel #2
0
        // ==========================================
        //  HANDLE HTTPSERVER
        // ==========================================

        public virtual bool HandleCustomRequest(NHttp.HttpRequestEventArgs e, StreamWriter writer)
        {
            // Status
            String status = e.Request.Params.Get("status");

            if (status != null)
            {
                if (WSRSpeakerManager.GetInstance().Speaking)
                {
                    writer.Write("speaking");
                }
            }

            // Askme
            var values = System.Web.HttpUtility.ParseQueryString(e.Request.Url.Query);
            var mgr    = WSRSpeechManager.GetInstance();

            String[] grammar = values.GetValues("grammar");
            String[] tags    = values.GetValues("tags");
            WSRSpeechManager.GetInstance().DynamicGrammar(grammar, tags);

            // Stop Music
            String pause = e.Request.Params.Get("pause");

            if (pause != null)
            {
                WSRSpeakerManager.GetInstance().Stop(pause);
            }

            // Play Music
            String mp3 = e.Request.Params.Get("play");

            if (mp3 != null)
            {
                WSRSpeakerManager.GetInstance().Play(mp3, e.Request.Params.Get("sync") == null);
            }

            // Recognize
            String audio = e.Request.Params.Get("recognize");

            if (audio != null)
            {
                WSRSpeechManager.GetInstance().RecognizeFile(audio);
            }

            // Listening
            String listen = e.Request.Params.Get("listen");

            if (listen != null)
            {
                WSRSpeechManager.GetInstance().Listening = bool.Parse(listen);
            }

            // Recognize File
            NHttp.HttpPostedFile file = e.Request.Files.Get("recognize");
            if (file != null)
            {
                byte[] data = null;
                using (var reader = new BinaryReader(file.InputStream)) {
                    data = reader.ReadBytes(file.ContentLength);
                }
                var path = WSRConfig.GetInstance().audio + "/" + file.FileName;
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                File.WriteAllBytes(path, data);
            }

            // Text To Speech
            String tts = e.Request.Params.Get("tts");

            if (tts != null)
            {
                tts = e.Server.HtmlDecode(tts);
                WSRSpeakerManager.GetInstance().Speak(tts, e.Request.Params.Get("sync") == null);
            }

            // Text To Speech - Stop
            String notts = e.Request.Params.Get("notts");

            if (notts != null)
            {
                WSRSpeakerManager.GetInstance().ShutUp();
            }

            // Text To Speech - Stop
            String restart = e.Request.Params.Get("restart");

            if (restart != null)
            {
                Restart();
            }

            // Set Context
            String ctxt = e.Request.Params.Get("context");

            if (ctxt != null)
            {
                WSRSpeechManager.GetInstance().SetContext(new List <string>(ctxt.Split(',')));
                WSRSpeechManager.GetInstance().SetContextTimeout();
                WSRSpeechManager.GetInstance().ForwardContext();
            }

            // Process
            String activate = e.Request.Params.Get("activate");

            if (activate != null)
            {
                WSRKeyboard.GetInstance().ActivateApp(activate);
            }

            String run   = e.Request.Params.Get("run");
            String param = e.Request.Params.Get("runp");

            if (run != null)
            {
                WSRKeyboard.GetInstance().RunApp(run, param);
            }

            // Keyboard
            String keyMod = e.Request.Params.Get("keyMod");
            String key    = e.Request.Params.Get("keyPress");

            if (key != null)
            {
                WSRKeyboard.GetInstance().SimulateKey(key, 0, keyMod);
            }

            key = e.Request.Params.Get("keyDown");
            if (key != null)
            {
                WSRKeyboard.GetInstance().SimulateKey(key, 1, keyMod);
            }

            key = e.Request.Params.Get("keyUp");
            if (key != null)
            {
                WSRKeyboard.GetInstance().SimulateKey(key, 2, keyMod);
            }

            String keyText = e.Request.Params.Get("keyText");

            if (keyText != null)
            {
                WSRKeyboard.GetInstance().SimulateTextEntry(keyText);
            }

            // Recognize String
            String emulate = e.Request.Params.Get("emulate");

            if (emulate != null)
            {
                WSRSpeechManager.GetInstance().RecognizeString(emulate);
                writer.Write(WSRSpeakerManager.GetInstance().SpeechBuffer);
            }
            return(false);
        }
Beispiel #3
0
        // ==========================================
        //  HANDLE HTTPSERVER
        // ==========================================

        public override bool HandleCustomRequest(NHttp.HttpRequestEventArgs e, StreamWriter writer)
        {
            if (base.HandleCustomRequest(e, writer))
            {
                return(true);
            }

            // Parse Result's Photo
            if (e.Request.Params.Get("picture") != null)
            {
                String path = ActiveSensor().TakePicture("medias/");
                e.Response.ContentType = "image/jpeg";
                Bitmap bmp = (Bitmap)Bitmap.FromFile(path);
                bmp.Save(e.Response.OutputStream, ImageFormat.Jpeg);

                return(true);
            }

            // Return Last Height Active Sensor
            var height = e.Request.Params.Get("height");

            if (height != null)
            {
                double h = WSRProfileManager.GetInstance().Heigth;
                if (height == "tts")
                {
                    WSRSpeakerManager.GetInstance().Speak(h + " mètres", false);
                }
                else
                {
                    writer.Write("" + h);
                }
                return(true);
            }

            // Face recognition
            String facereco = e.Request.Params.Get("face");

            if (facereco != null)
            {
                facereco = e.Server.HtmlDecode(facereco);
                if ("start".Equals(facereco))
                {
                    WSRConfig.GetInstance().StandByFace = false;
                }
                else if ("stop".Equals(facereco))
                {
                    WSRConfig.GetInstance().StandByFace = true;
                }
            }

            // Gesture recognition
            String gesture = e.Request.Params.Get("gesture");

            if (gesture != null)
            {
                gesture = e.Server.HtmlDecode(gesture);
                if ("start".Equals(gesture))
                {
                    WSRConfig.GetInstance().StandByGesture = false;
                }
                else if ("stop".Equals(gesture))
                {
                    WSRConfig.GetInstance().StandByGesture = true;
                }
            }
            return(false);
        }