public bool ChangeClimatIfNeeded(string appUserId, DateTime dateTime, Guid roomId) { Room room = _dbContext.Rooms.Where(rm => rm.Id == roomId) .Include(r => r.ClimatLogs) .ThenInclude(cl => cl.HowOften) .FirstOrDefault(t => t.AppUserId == appUserId); ClimatLog climatLog = room.ClimatLogs.FirstOrDefault(cl => cl.IsRepeatable != true && IsDateTimesEquals(cl.Date, dateTime)); if (climatLog != null) { UpdateRoomClimatState(climatLog); return(true); } List <ClimatLog> climatLogs = room.ClimatLogs.Where(cl => cl.IsRepeatable == true).ToList(); climatLog = CheckIfClimatChangesNeeded(climatLogs, dateTime); if (climatLog != null) { UpdateRoomClimatState(climatLog); return(true); } _dbContext.SaveChanges(); return(false); }
public void UpdateRoomClimatState(ClimatLog climatLog) { _dbContext.Rooms.FirstOrDefault(r => r.Id == climatLog.RoomId).CurrentTemperature = climatLog.Temperature; _dbContext.Rooms.FirstOrDefault(r => r.Id == climatLog.RoomId).CurrentAirHumidity = climatLog.AirHumidity; _dbContext.SaveChanges(); }
private ClimatLog CheckIfClimatChangesNeeded(List <ClimatLog> climatLogs, DateTime dateTime) { ClimatLog result = null; List <int> dayOfWeeksId = new List <int>() { 3, 4, 5, 6, 7, 8, 9 }; foreach (ClimatLog climatLog in climatLogs) { if (IsDateTimesEquals(dateTime, climatLog.Date) && _dbContext.ClimatLogs.FirstOrDefault(tl => tl.RoomId == climatLog.RoomId && climatLog.Date == tl.Date) == null) { result = climatLog; } if (climatLog.HowOften.Id == 2 && IsTimesEquals(dateTime, climatLog.Date)) { result = climatLog; } else if (climatLog.HowOften.Id == 10 && IsDayOfWeekWeekDay(dateTime) && IsTimesEquals(dateTime, climatLog.Date)) { result = climatLog; } else if (climatLog.HowOften.Id == 11 && !IsDayOfWeekWeekDay(dateTime) && IsTimesEquals(dateTime, climatLog.Date)) { result = climatLog; } else if (dayOfWeeksId.Contains(climatLog.HowOften.Id) && IsDayOfWeekAndTimeEquals(climatLog.Date, dateTime)) { result = climatLog; } } return(result); }
public void ChangeClimat(ClimatLog climatLog) { _dbContext.ClimatLogs.Add(climatLog); if (IsDateTimesEquals(climatLog.Date, DateTime.Now)) { UpdateRoomClimatState(climatLog); } _dbContext.SaveChanges(); }
public JsonResult ChangeClimat(RequestClimatLog requestClimatLog) { Room room = _roomRepository.GetRoomById(requestClimatLog.RoomId); if (room != null) { ClimatLog climatLog = RequestClimatLogToClimatLog(requestClimatLog); _roomRepository.ChangeClimat(climatLog); return(new JsonResult(new { message = "ok", coffeDeviceState = RoomStateById(requestClimatLog.RoomId) })); } else { return(new JsonResult(new { message = "error", })); } }