private void CatchUpNextRun(ScriptSchedule schedule, TimeSpan interval)
 {
     while (schedule.NextRun < Time.Now)
     {
         schedule.NextRun += interval;
     }
 }
        private void ScptSchdlMnuItm_Click(object sender, EventArgs e)
        {
            var Form = new ScriptSchedule();

            Form.ShowDialog(this);
            Form.Dispose();
        }
        public void Init()
        {
            _testData = new TestData();

            _savedScript = new SavedScript()
            {
                Id = Guid.NewGuid()
            };

            _schedule1 = new()
            {
                CreatedAt = Time.Now,
                CreatorId = _testData.User1.Id,
                Devices   = new List <Device>()
                {
                    _testData.Device1
                },
                DeviceGroups = new List <DeviceGroup>()
                {
                    new DeviceGroup()
                    {
                        Devices = new List <Device>()
                        {
                            _testData.Device2
                        }
                    }
                },
                Interval       = RepeatInterval.Hourly,
                Name           = "_scheduleName",
                Id             = 5,
                NextRun        = Time.Now.AddMinutes(1),
                OrganizationID = _testData.User1.OrganizationID,
                SavedScriptId  = _savedScript.Id
            };

            var scriptSchedules = new List <ScriptSchedule>()
            {
                _schedule1
            };

            _dataService = new Mock <IDataService>();
            _dataService.Setup(x => x.GetScriptSchedulesDue()).Returns(Task.FromResult(scriptSchedules));
            _dataService.Setup(x => x.GetDevices(It.Is <IEnumerable <string> >(x =>
                                                                               x.Contains(_testData.Device1.ID) &&
                                                                               x.Contains(_testData.Device2.ID)
                                                                               ))).Returns(new List <Device>()
            {
                _testData.Device1, _testData.Device2
            });

            _circuitConnection = new Mock <ICircuitConnection>();
            _logger            = new Mock <ILogger <ScriptScheduleDispatcher> >();
            _dispatcher        = new ScriptScheduleDispatcher(_dataService.Object, _circuitConnection.Object, _logger.Object);
        }
        private bool AdvanceSchedule(ScriptSchedule schedule)
        {
            if (schedule is null)
            {
                return(false);
            }

            if (schedule.NextRun > Time.Now)
            {
                return(false);
            }

            switch (schedule.Interval)
            {
            case RepeatInterval.Hourly:
                CatchUpNextRun(schedule, TimeSpan.FromHours(1));
                break;

            case RepeatInterval.Daily:
                CatchUpNextRun(schedule, TimeSpan.FromDays(1));
                break;

            case RepeatInterval.Weekly:
                CatchUpNextRun(schedule, TimeSpan.FromDays(7));
                break;

            case RepeatInterval.Monthly:
                for (var i = 0; schedule.NextRun < Time.Now; i++)
                {
                    schedule.NextRun = schedule.StartAt.AddMonths(i);
                }
                break;

            default:
                return(false);
            }

            return(true);
        }