private bool IsWithinTimeWindow(WateringScheduleItem item) { DateTime now = DateTime.UtcNow; HourMinuteTime time = this.ExtractHour(item.StartTime); char dayOfWeekNumber = Char.Parse(((int)now.DayOfWeek).ToString()); if (item.WateringDays.Contains(dayOfWeekNumber)) { if (item.LastRun == null || now - item.LastRun.StartTime > new TimeSpan(0, 10, 0)) { DateTime transposedTime = now; TimeSpan ts = new TimeSpan(time.Hour, time.Minute, 0); transposedTime = transposedTime.Date + ts; bool isWithinTimeWindow = (now - transposedTime >= new TimeSpan(0) && now - transposedTime < new TimeSpan(0, 1, 0)); if (isWithinTimeWindow) { return(true); } } } return(false); }
public WateringScheduleDto(WateringScheduleItem item) { this.waterNow = new WaterNowDto { duration = item.Duration, level = item.Level }; this.startTime = item.StartTime; this.notifyBeforeMinutes = item.NotifiyBeforeMinutes; this.wateringDays = item.WateringDays; this.isEnabled = item.IsEnabled; }
private bool HasNotBeenTriggered(WateringScheduleItem item) { // TODO: these operations may be slow when dealing with a lot of schedules. Find a better way to store them int notificationInterval = item.NotifiyBeforeMinutes; DateTime now = DateTime.UtcNow; ScheduledRun lastRun = item.LastRun; if (lastRun != null && (now - lastRun.StartTime).TotalMinutes > notificationInterval || lastRun == null) { return(true); } return(false); }
public bool ToggleSchedule([FromBody] Level level) { User user = this._context.Users.Include(u => u.WateringSchedules).FirstOrDefault(u => u.Id == Misc.GetIdFromClaimsPrincipal(User)); WateringScheduleItem schedule = user.WateringSchedules.FirstOrDefault((sc) => { return(sc.Level == level); }); if (schedule != null) { schedule.IsEnabled = !schedule.IsEnabled; this._context.SaveChanges(); return(schedule.IsEnabled); } throw new Exception("Trying to update a schedule that doesn't exist"); }
public void WaterNow([FromBody] WaterNowDto dto) { this._logger.LogInformation("CommandControllerWaterNow"); if (dto == null) { throw new Exception("Could not parse the object"); } User usr = this._context.Users .Include(u => u.DeviceTokens) .Include(u => u.WateringSchedules) .FirstOrDefault(u => u.Id == Misc.GetIdFromClaimsPrincipal(User)); if (usr != null) { string deviceToken = usr.DeviceTokens.Count > 0 ? usr.DeviceTokens[0].Token : null; if (deviceToken == null) { this._logger.LogError("Command_NoToken", "User: {0}", usr.Id); return; // TODO: return some error to the customer so they know it's bad } this._dispatcher.WaterNow(this, new WaterNowArgs { WaterNow = dto, Token = deviceToken }); ScheduledRun newRun = new ScheduledRun { Id = Guid.NewGuid(), Duration = dto.duration, Level = dto.level, StartTime = DateTime.Now }; usr.ScheduledRuns.Add(newRun); WateringScheduleItem wateringSchedule = usr.WateringSchedules.FirstOrDefault(ws => ws.Level == dto.level); if (wateringSchedule != null) { wateringSchedule.LastRun = newRun; } this._context.SaveChanges(); } else { // TODO: the user doesn't exist. There's something wrong with Identity. // tell the user to try again, then log out/log in, then email us if none of this works } }
public void CancelWatering([FromBody] Level level) { this._logger.LogInformation("CommandControllerCancelWatering"); User usr = this._context.Users .Include(u => u.DeviceTokens) .Include(u => u.WateringSchedules) .FirstOrDefault(u => u.Id == Misc.GetIdFromClaimsPrincipal(User)); if (usr != null) { string deviceToken = usr.DeviceTokens.Count > 0 ? usr.DeviceTokens[0].Token : null; if (deviceToken == null) { this._logger.LogError("Command_NoToken", "User: {0}", usr.Id); return; // TODO: return some error to the customer so they know it's bad } this._dispatcher.CancelWatering(this, new CancelWateringArgs { Level = level, Token = deviceToken }); WateringScheduleItem wateringSchedule = usr.WateringSchedules.FirstOrDefault(ws => ws.Level == level); if (wateringSchedule != null) { if (wateringSchedule.LastRun != null) { DateTime endTime = wateringSchedule.LastRun.StartTime.AddSeconds(wateringSchedule.LastRun.Duration); if (endTime > DateTime.UtcNow) { wateringSchedule.LastRun.Duration = (int)(endTime - DateTime.UtcNow).TotalSeconds; } } this._context.SaveChanges(); } } else { // TODO: the user doesn't exist. There's something wrong with Identity. // tell the user to try again, then log out/log in, then email us if none of this works } }
public bool UpdateSchedule([FromBody] WateringScheduleDto dto) { if (dto == null) { throw new Exception("Could not parse object"); } WateringScheduleItem tempItem = new WateringScheduleItem { Id = Guid.NewGuid(), Duration = dto.waterNow.duration, Level = dto.waterNow.level, WateringDays = dto.wateringDays, StartTime = dto.startTime, NotifiyBeforeMinutes = dto.notifyBeforeMinutes, IsEnabled = dto.isEnabled }; User usr = this._context.Users.Include(u => u.WateringSchedules).FirstOrDefault(u => u.Id == Misc.GetIdFromClaimsPrincipal(User)); WateringScheduleItem schedule = usr.WateringSchedules.FirstOrDefault(w => w.Level == dto.waterNow.level); if (schedule == null) { // this is the first time the schedule is set. usr.WateringSchedules.Add(tempItem); } else { schedule.Duration = tempItem.Duration; schedule.WateringDays = tempItem.WateringDays; schedule.StartTime = tempItem.StartTime; schedule.NotifiyBeforeMinutes = tempItem.NotifiyBeforeMinutes; schedule.IsEnabled = tempItem.IsEnabled; } this._context.SaveChanges(); return(true); }