Example #1
0
        //Handles all HTTP POST's
        /// <summary>
        ///
        ///
        /// //ZONE/[zone]/on - currently running or not?
        /// //ZONE/[zone]/off - currently running or not?
        /// //PROGRAM/PAUSE/[minutes]  pause current running program for this number of minutes
        /// //PROGRAM/UNPAUSE
        /// //PROGRAM/STOP
        /// //PROGRAM/RUNNEXTZONE
        /// //PROGRAM/START/[program index]  run the program at index given (zero based)
        ///
        /// //SET/ZONELIST   - sets the zone information
        /// //SET/PROGRAMDATA  - sets the program data
        /// </summary>
        /// <param name="request"></param>
        /// <param name="os"></param>
        /// <returns></returns>
        private async Task WritePostResponseAsync(string request, IOutputStream os, string json)
        {
            bool urlFound = false;

            byte[] bodyArray   = null;
            string responseMsg = "";

            string requestUpper = request.ToUpper();

            if (requestUpper.StartsWith("/ZONE/"))
            {
                Tuple <int, string> zc = ParseZoneCommand(requestUpper);
                int    zone            = zc.Item1;
                string cmd             = zc.Item2;
                switch (cmd)
                {
                case "ON":
                {
                    urlFound = true;
                    bool stat = sprinklerController.SetZoneOn(zone);
                    responseMsg = stat ? "ON" : "OFF";
                    break;
                }

                case "OFF":
                {
                    urlFound = true;
                    bool stat = sprinklerController.SetZoneOff(zone);
                    responseMsg = stat ? "OFF" : "ON";
                    break;
                }
                }
            }
            else if (requestUpper.StartsWith("/PROGRAM/"))
            {
                try
                {
                    Tuple <string, string> zc = ParseProgramCommand(requestUpper);
                    string cmd = zc.Item1;
                    switch (cmd)
                    {
                    case "PAUSE":
                        urlFound = true;
                        int minutes = int.Parse(zc.Item2);
                        programController.PauseProgram(minutes);
                        responseMsg = "true";
                        break;

                    case "UNPAUSE":
                        urlFound = true;
                        programController.UnpauseProgram();
                        responseMsg = "true";
                        break;

                    case "STOP":
                        urlFound = true;
                        programController.StopProgram();
                        responseMsg = "true";
                        break;

                    case "RUNNEXTZONE":
                        urlFound = true;
                        programController.RunNextZone();
                        responseMsg = "true";
                        break;

                    case "START":
                        urlFound = true;
                        int progIndex = int.Parse(zc.Item2);
                        programController.StartProgram(progIndex);
                        responseMsg = "true";
                        break;
                        //case "ENABLE":
                        //    urlFound = true;
                        //    // need program id!!!
                        //    Boolean ON = bool.Parse (zc.Item2);
                        //    programController.EnableProgram(on);
                        //    responseMsg = "true";
                        //    break;
                    }
                }
                catch (Exception ex)
                {
                    responseMsg = MakeJsonErrorMsg("Error", ex);
                    //swallow throw;
                }
            }
            else if (requestUpper.StartsWith("/SET/"))
            {
                try
                {
                    Tuple <string, string> zc = ParseProgramCommand(requestUpper);
                    string cmd = zc.Item1;
                    switch (cmd)
                    {
                    case "ZONELIST":
                        urlFound    = true;
                        responseMsg = UpdateZoneList(json);
                        break;

                    case "PROGRAMDATA":
                        urlFound    = true;
                        responseMsg = UpdateProgramData(json);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    responseMsg = MakeJsonErrorMsg("Error", ex);
                    //swallow throw;
                }
            }

            bodyArray = Encoding.UTF8.GetBytes(responseMsg);
            await WriteResponseAsync(request.ToUpper(), responseMsg, urlFound, bodyArray, os);
        }