private AssociateDoorRequestViewModel GetAssociateDoorRequestModel(DoorRead read) { return(new AssociateDoorRequestViewModel { DoorReadId = read.Id, DoorId = read.DoorId, Timestamp = read.Timestamp.Value, DoorName = read.Door.Name == "Unknown" ? read.Door.MacAddress : read.Door.Name, TokenValue = read.TokenValue, AvailablePeople = DataContext.People .Where(p => !p.IsDeleted && (p.TokenValue == null || p.TokenValue == string.Empty)) .OrderBy(p => p.Name) .Select(p => new SelectListItem { Text = p.Name, Value = p.Id.ToString() }) .ToList() }); }
public ActionResult <ValidateDoorUnlockResponseModel> Validate(ValidateDoorUnlockRequestModel model) { if (model == null) { return(Json(new ValidateDoorUnlockResponseModel { IsUnlockAllowed = false, Message = "No model provided" })); } if (string.IsNullOrEmpty(model.MacAddress)) { return(Json(new ValidateDoorUnlockResponseModel { IsUnlockAllowed = false, Message = "No device mac address provided" })); } if (string.IsNullOrEmpty(model.TokenValue)) { return(Json(new ValidateDoorUnlockResponseModel { IsUnlockAllowed = false, Message = "No scan token provided" })); } if (model.TokenValue != null) { model.TokenValue = model.TokenValue.Trim().ToUpper(); } var scope = Settings.UseSqlServer ? new TransactionScope(TransactionScopeAsyncFlowOption.Enabled) : null; var door = DataContext.Doors.FirstOrDefault(d => d.MacAddress == model.MacAddress); if (door == null) { return(Json(new ValidateDoorUnlockResponseModel { IsUnlockAllowed = false, Message = "Invalid door mac address" })); } if (door.IsDeleted) { return(Json(new ValidateDoorUnlockResponseModel { IsUnlockAllowed = false, Message = "Invalid door" })); } door.LastReadTimestamp = DateTime.Now; var person = DataContext.People .Include(p => p.PersonDoors) .FirstOrDefault(p => p.TokenValue == model.TokenValue); bool isAccessAllowed = person != null && !person.IsDeleted && person.PersonDoors != null && person.PersonDoors.Any(pd => pd.DoorId == door.Id && !pd.IsDeleted); var read = new DoorRead { IsSuccess = (short)(isAccessAllowed ? 1 : 0), DoorId = door.Id, PersonId = person != null ? person.Id : (int?)null, Timestamp = DateTime.Now, TokenValue = model.TokenValue }; var lastRead = DataContext.DoorReads.OrderByDescending(dr => dr.Timestamp).FirstOrDefault(); //if the last read is for the same token and it was under a minute ago, don't log again if (lastRead == null || lastRead.TokenValue != model.TokenValue || (DateTime.Now - lastRead.Timestamp) > TimeSpan.FromMinutes(1)) { DataContext.DoorReads.Add(read); } DataContext.SaveChanges(); var doorReadId = read.Id; if (person != null) { person.LastSeenTimestamp = DateTime.Now; } if (scope != null) { scope.Complete(); } if (person == null) { return(Json(new ValidateDoorUnlockResponseModel { DoorReadId = doorReadId, IsUnlockAllowed = false, Message = "Token does not match a person" })); } else { return(Json(new ValidateDoorUnlockResponseModel { DoorReadId = doorReadId, IsUnlockAllowed = isAccessAllowed, MatchedPerson = person.ConvertTo <ValidateDoorUnlockPersonModel>(), Message = "Success" })); } }