Example #1
0
        public static void SetSchedule(SerialInputItem item)
        {
            _serialUI.Stop();

            switch (item.Context)
            {
            case 0:
                _serialUI.Store.Clear();
                _serialUI.AddDisplayItem(Divider);
                _serialUI.AddDisplayItem("Set Heater Weekly Schedule:\r\n");
                _serialUI.AddInputItem(new SerialInputItem {
                    Label = GetDayOfWeekSelectionString(), Callback = SetSchedule, Context = 1, StoreKey = "sched.dow"
                });
                break;

            case 1:
                var timeslotList = new ArrayList();
                var dow          = ToInt("sched.dow");
                if (dow >= 0 && dow <= 6)
                {
                    _serialUI.AddDisplayItem("Select '" + _schedule.DayList[dow] + "' timeslot\r\n");
                    var slotCount = _schedule.GetDayTimeSlotList((DayOfWeek)dow, timeslotList);
                    slotCount -= 1;
                    _serialUI.Store["sched.maxSlot"] = slotCount.ToString();
                    _serialUI.AddDisplayItem(timeslotList);
                    var slotSelection = "Timeslot (0-" + slotCount.ToString() + ")?";
                    _serialUI.AddInputItem(new SerialInputItem {
                        Label = slotSelection, Callback = SetSchedule, Context = 2, StoreKey = "sched.timeSlot"
                    });
                }
                else
                {
                    Log("Invalid week day input.");
                    _serialUI.Store.Clear();
                    RefreshMainMenu(null);
                    return;
                }
                break;

            case 2:
                var maxSlot  = ToInt("sched.maxSlot");
                var timeSlot = ToInt("sched.timeSlot");
                if (timeSlot >= 0 && timeSlot <= maxSlot)
                {
                    _serialUI.AddInputItem(new SerialInputItem {
                        Label = "Begin Hour (00-23)?", Callback = SetSchedule, Context = 3, StoreKey = "sched.beginHour"
                    });
                }
                else
                {
                    Log("Invalid timeslot input.");
                    _serialUI.Store.Clear();
                    RefreshMainMenu(null);
                    return;
                }
                break;

            case 3:
                _serialUI.AddInputItem(new SerialInputItem {
                    Label = "End Hour (00-23)?", Callback = SetSchedule, Context = 4, StoreKey = "sched.endHour"
                });
                break;

            case 4:
                _serialUI.AddInputItem(new SerialInputItem {
                    Label = "Is this timeslot the same all week (Y/N)?", Callback = SetSchedule, Context = 5, StoreKey = "sched.sameAllWeek"
                });
                break;

            case 5:
                var dayOfWeek     = ToInt("sched.dow");
                var timeSlotIndex = ToInt("sched.timeSlot");
                var beginHour     = ToInt("sched.beginHour");
                var endHour       = ToInt("sched.endHour");
                var sameAllWeek   = (string)_serialUI.Store["sched.sameAllWeek"];

                var validTimeSlot = true;

                if ((beginHour >= 0 && beginHour <= 23) && (endHour >= 0 && endHour <= 23))
                {
                    if (beginHour > endHour && endHour == 0 || beginHour <= endHour)
                    {
                        validTimeSlot = true;
                    }
                    else
                    {
                        validTimeSlot = false;
                    }
                }
                else
                {
                    validTimeSlot = false;
                }

                if (validTimeSlot == false)
                {
                    Log("Invalid Begin/End hour input.");
                    Log("Hours must be between 0 and 23.");
                    Log("Begin hour must be <= to End hour except if End hour is 0.");
                    _serialUI.Store.Clear();
                    RefreshMainMenu(null);
                    return;
                }

                if (sameAllWeek.ToLower() == "y")
                {
                    _schedule.SetTimeSlot((DayOfWeek)dayOfWeek, timeSlotIndex, beginHour, endHour, true);
                }
                else
                {
                    _schedule.SetTimeSlot((DayOfWeek)dayOfWeek, timeSlotIndex, beginHour, endHour, false);
                }

                SaveSchedule();

                _serialUI.Store.Clear();
                RefreshMainMenu(null);
                return;
            }

            _serialUI.Go();
        }