public ScheduledIrrigationView[] GetScheduled(string sN, long StartTicks, long EndTicks) { var sDate = TimeConvertor.GetDateTime(StartTicks); DateTime Start = new DateTime(sDate.Year, sDate.Month, sDate.Day, 0, 0, 0); var eDate = TimeConvertor.GetDateTime(EndTicks); DateTime End = new DateTime(eDate.Year, eDate.Month, eDate.Day, 23, 59, 59); var ScheduledIrrigation = new List <ScheduledIrrigationView>(); var ProgramList = _AdminRepository.DevicePrograms_Get(sN).Where(p => (!p.FinalEndDateLimit.HasValue || p.FinalEndDateLimit.Value > End) && (!p.FinalStartDateLimit.HasValue || p.FinalStartDateLimit.Value < Start) && (StatusType)p.StatusID == StatusType.Active).ToArray(); var MainPipe = _AdminRepository.MainPipeSettings_Get(sN); var device_WaterFactor = MainPipe != null ? MainPipe.WaterFactor : 100; var ZoneSetting = _AdminRepository.DeviceZoneSetting_Get(sN); foreach (var program in ProgramList) { var PeriodDate = new List <DateTime>(); var ExecutionTimes = new List <ExecutionTime>(); decimal WaterFactor = (program.WaterFactor != 100 ? program.WaterFactor : device_WaterFactor) * 0.01m; #region Weekly - Get the allow watering days if ((DaysExecutionType)program.DaysExecutionsTypeID == DaysExecutionType.Weekly) { var weeklyDays = _AdminRepository.WeeklyProgramSetting_Get(program.ProgramID); var tempStart = Start; while (tempStart < End) { switch (tempStart.DayOfWeek) { case DayOfWeek.Sunday: if (weeklyDays.Sunday) { PeriodDate.Add(tempStart); } break; case DayOfWeek.Monday: if (weeklyDays.Monday) { PeriodDate.Add(tempStart); } break; case DayOfWeek.Tuesday: if (weeklyDays.Tuesday) { PeriodDate.Add(tempStart); } break; case DayOfWeek.Wednesday: if (weeklyDays.Wednesday) { PeriodDate.Add(tempStart); } break; case DayOfWeek.Thursday: if (weeklyDays.Thursday) { PeriodDate.Add(tempStart); } break; case DayOfWeek.Friday: if (weeklyDays.Friday) { PeriodDate.Add(tempStart); } break; case DayOfWeek.Saturday: if (weeklyDays.Saturday) { PeriodDate.Add(tempStart); } break; } tempStart = tempStart.AddDays(1); } } #endregion #region CyclicDayProgram -StartDate and DaysInterval else if ((DaysExecutionType)program.DaysExecutionsTypeID == DaysExecutionType.Cyclic) { var CyclicSetting = _AdminRepository.CyclicDayProgram_Get(program.ProgramID); var tempStart = CyclicSetting.StartDate > Start ? CyclicSetting.StartDate : Start; tempStart = new DateTime(tempStart.Year, tempStart.Month, tempStart.Day, 0, 0, 0); while (tempStart < End) { PeriodDate.Add(tempStart); tempStart = tempStart.AddDays(CyclicSetting.DaysInterval); } } #endregion #region Fixed Time - By Execution Hours if ((HoursExecutionType)program.HoursExecutionsTypeID == HoursExecutionType.Fixed) { var ExecuTimes = _AdminRepository.ExecutionHoursProgram_Get(program.ProgramID); foreach (var item_Date in PeriodDate) { foreach (var startTime in ExecuTimes) { var initialStart = item_Date.AddSeconds(startTime.ExecTime); ExecutionTimes.Add(new ExecutionTime() { StartDate = initialStart }); } } } #endregion #region Interval time - StartTime ,Cycl and Interval Spent time else if ((HoursExecutionType)program.HoursExecutionsTypeID == HoursExecutionType.Interval) { foreach (var item_Date in PeriodDate) { var CyclesPerDay = program.HourlyCyclesPerDay; var Counter = 0; var StartTime = program.HourlyCyclesStartTime; while (Counter < CyclesPerDay) { var item_Date1 = item_Date.AddSeconds(StartTime + Counter * program.HourlyCycleTime); ExecutionTimes.Add(new ExecutionTime() { StartDate = item_Date1, Interval = Counter }); Counter++; } } } #endregion #region ExecutionTimes over ZoneInProgram var ZoneInProgram = _AdminRepository.ZonesInProgram_Get(program.ProgramID); foreach (var item_time in ExecutionTimes) { ScheduledIrrigationView item_Scheduled = new ScheduledIrrigationView() { ProgramID = program.ProgramID, StartDate = item_time.StartDate, Interval = item_time.Interval, Zones = new List <ZoneScheduledItemView>(), Name = program.Name, IsFertilizerON = program.IsFertilizerON, }; ScheduledIrrigation.Add(item_Scheduled); var startTime = item_time.StartDate; DateTime endTime = new DateTime(); var midnightTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, 0, 0, 0).AddDays(1); var totalTime = 0; foreach (var Zoneitem in ZoneInProgram) { var zoneSetting = ZoneSetting.Where(z => z.OutputNumber == Zoneitem.ZoneNumber).FirstOrDefault(); if (zoneSetting != null) { zoneSetting = new GSI.DAL.DataAccessLayer.Models.Zone.ZoneSetting() { ZoneColor = 15658734, OutputNumber = Zoneitem.ZoneNumber, Name = "Name" + Zoneitem.ZoneNumber }; } var WaterMeterSetting = _AdminRepository.WaterMeterSetting_Get(sN); var FlowTypeID = WaterMeterSetting != null ? (WaterMeter_FlowType?)WaterMeterSetting.FlowTypeID : null; var FlowValue = zoneSetting.SetupNominalFlow != null ? zoneSetting.SetupNominalFlow : zoneSetting.LastFlow; var PulseTypeID = WaterMeterSetting != null ? (WaterMeter_PulseType)WaterMeterSetting.PulseTypeID : WaterMeter_PulseType.LITER_AS_LITER; int WaterDuration_Seconds = GetDurationTime(FlowTypeID, FlowValue, PulseTypeID, (WaterProgramType)program.WaterUnitID, zoneSetting, Zoneitem); WaterDuration_Seconds = (int)(WaterDuration_Seconds * WaterFactor); endTime = startTime.AddSeconds(WaterDuration_Seconds); totalTime += WaterDuration_Seconds; if (midnightTime < endTime) { item_Scheduled = new ScheduledIrrigationView() { StartDate = midnightTime, ProgramID = item_Scheduled.ProgramID, Interval = item_time.Interval, Zones = new List <ZoneScheduledItemView>(), Name = program.Name, IsFertilizerON = program.IsFertilizerON, }; midnightTime = new DateTime(endTime.Year, endTime.Month, endTime.Day, 0, 0, 0).AddDays(1); ScheduledIrrigation.Add(item_Scheduled); } item_Scheduled.Zones.Add(new ZoneScheduledItemView() { Duration = Zoneitem.WaterDuration, FertQuant = Zoneitem.FertQuant, FertTime = Zoneitem.FertTime, WaterAfter = Zoneitem.WaterAfter, WaterBefore = Zoneitem.WaterBefore, Zone = new ZoneScheduledInfoView() { Color = zoneSetting.ZoneColor, ZoneNum = zoneSetting.OutputNumber, Name = zoneSetting.Name }, StartTime = startTime, EndTime = endTime }); startTime = endTime; } item_Scheduled.EndDate = endTime; } #endregion } return(ScheduledIrrigation.ToArray()); }