public IHttpActionResult GetPairingCode(string uuid) { PairingOperation existing = PairingOperationForUUID(uuid); if (existing != null) { // return the previously generated pairing key return(Ok(existing.ID)); } // generate a new pairing key PairingOperation pairingOperation = new PairingOperation(); pairingOperation.DeviceUUID = uuid; pairingOperation.ID = GetRandomPairingCode(); while (PairingOperationExists(pairingOperation.ID)) { // make sure it is unique pairingOperation.ID = GetRandomPairingCode(); } db.PairingOperations.Add(pairingOperation); db.SaveChanges(); return(Ok(pairingOperation.ID)); }
public IHttpActionResult GetPairingStatus(string id, string uuid) { PairingOperation pairingOperation = db.PairingOperations.Find(id); if (pairingOperation == null) { return(NotFound()); } if (pairingOperation.DeviceUUID == uuid) { // only allow the status to be polled if the UUID is known return(Ok(pairingOperation)); } else { return(BadRequest()); } }
public IHttpActionResult GetFinishPairing(string id, string uuid) { PairingOperation pairingOperation = db.PairingOperations.Find(id); if (pairingOperation == null) { return(NotFound()); } if (pairingOperation.DeviceUUID == uuid) { // only allow the pairing session to be closed by the original device db.Entry(pairingOperation).State = EntityState.Deleted; db.SaveChanges(); return(Ok()); } else { return(BadRequest()); } }