/// <summary>
        /// main sprinkler controller thread, starts programs and advances sets
        /// </summary>
        private async void ControllerThread()
        {
            // wait for a bit on startup before running out thread
            Task.Delay(5000).Wait();
            System.Diagnostics.Debug.WriteLine("SprinklerProgramController started");

            while (true)
            {
                if (RunningProgram == null)
                {
                    //check if we need to start a program
                    RunningProgram = StartProgramIfTime();
                }

                if (RunningProgram != null)
                {
                    if (isPaused)
                    {
                        // check if paused time is up
                        if (DateTime.Now > ZonePauseStopTime)
                        {
                            isPaused          = false;
                            ZonePauseStopTime = DateTime.MaxValue;
                            // restart the stopped zone...
                            Controller.SetZoneOn(RunningZone);
                        }
                        // just figure out how much longer it needs to wait
                        TimeSpan diff = ZonePauseStopTime - DateTime.Now;
                        zonePauseSecondsLeft = (int)diff.TotalSeconds;
                    }

                    // Advance zone as needed
                    if (!isPaused)
                    {
                        StartZoneIfTime();
                    }
                }

                await Task.Delay(1000);
            }
        }
Ejemplo n.º 2
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);
        }