// returns the last outstanding request; status in (processing, vending) and within a timeout interval public static VendingRequest LastOutstandingRequest() { using (klick_vending_machineEntities context = new klick_vending_machineEntities()) { int processingTimeoutSeconds = 30; int vendingTimeoutSeconds = 60; // request in status 'processing' VendingRequest request = ( from r in context.VendingRequests where r.Status == "processing" && SqlFunctions.DateDiff("second", r.RequestDate, DateTime.Now) < processingTimeoutSeconds orderby r.RequestDate descending select r).FirstOrDefault(); if (request != null) { return(request); } request = ( from r in context.VendingRequests where r.Status == "vending" && SqlFunctions.DateDiff("second", r.VendStartDate, DateTime.Now) < vendingTimeoutSeconds orderby r.VendStartDate descending select r).FirstOrDefault(); return(request); } }
// Called when the user has pushed some coordinates private static string RequestItem(string coordinateString) { // Get the last card scan CardScan scan = CardScan.LastCardScan(); if (scan == null) { return MSG_ERROR_NO_CARD; } VendingRequest lastRequest = VendingRequest.LastOutstandingRequest(); string error = null; string successMessage = MSG_VEND_ITEM; int x = -1; int y = -1; string requestStatus = null; using (klick_vending_machineEntities context = new klick_vending_machineEntities()) { CardScanResult scanResult = scan.CardScanResult; // create a new vending request VendingRequest request = new VendingRequest() { CardScanResultID = scanResult.CardScanResultID, Status = STATUS_PROCESSING, RequestDate = DateTime.Now, Coordinates = coordinateString }; context.VendingRequests.Add(request); context.SaveChanges(); try { // Error checking block if (lastRequest != null) { // We found a vending request with that is either processing or vending within a timeout interval // Cannot vend again at this point error = MSG_ERROR_VENDING_IN_PROGRESS; } else if (scan.HasTimedOut) { // We've timed out if (DateTime.Now.Subtract(scan.ScanDate).TotalMinutes > 5) { // Last scan was a while ago - just tell the user to scan first, so they don't get confused error = MSG_ERROR_NO_CARD; } else { // Display timeout error message error = MSG_ERROR_CARD_TIMEOUT; } } else if (scanResult.Status != "valid") { // Invalid card! error = MSG_ERROR_INVALID_CARD; } else if (scan.HasVended(request.VendingRequestID)) { // We've already vended an item for this card scan. Sneaky! error = MSG_ERROR_ALREADY_VENDED; } else if (coordinateString == "TIMEOUT") { // Timed out before anything was vended string DefaultTimeoutPath = "smb_warning.wav"; PlaySound(GetConfigPath("timeout", DefaultTimeoutPath)); error = MSG_ERROR_CARD_TIMEOUT; } else if (!GetCoordinates(coordinateString, out x, out y)) { // Unable to parse coordinates error = MSG_ERROR_INVALID_COORDS; } else { requestStatus = GetItemStatusType(x, y); if (requestStatus == null) { error = MSG_ERROR_INVALID_ITEM; } } request.X = x; request.Y = y; } catch (Exception e) { error = MSG_ERROR_SYSTEM_FAIL; Error.CreateError(e); } if (error == null) { // TODO: consider what happens if the message to the Arduino fails? request.Status = requestStatus; request.VendStartDate = DateTime.Now; // Adjust the success message if necessary if (requestStatus == STATUS_TAKING_PHOTO) { successMessage = MSG_TAKING_PHOTO; } else if (requestStatus == STATUS_PLAYING_SOUND) { successMessage = MSG_PLAYING_SOUND; } } else { request.Status = "failed"; request.ErrorMessage = error; } context.SaveChanges(); } Logger.WriteLine("Returning! " + (error == null ? MSG_VEND_ITEM : error)); // Finally, success! Vend it. Or display the error instead if there is one. return (error == null ? successMessage : error); }