コード例 #1
0
        public void RecognizeBarcode(Bitmap bitmap)
        {
            if (state != InnerState.BORROWING && state != InnerState.RETURNING) { throw new InvalidOperationException("The user is in an invalid state!"); }

            if (bitmap == null)
            {
                //invalid request, show in the welcome page
                View.ShowWelcomePage();
                return;
            }

            try
            {
                short port = 0;
                //show a loading message
                View.ShowLoading();
                //call the web service to send the picture
                using (IServiceClientProxy proxy = new IServiceClientProxy(binding, new ProtocolVersion11()))
                {
                    SendBarcodeImage sendImageRequest = new SendBarcodeImage();
                    SendBarcodeImageResponse response = proxy.SendBarcodeImage(sendImageRequest);
                    port = response.SendBarcodeImageResult;

                    //send the image
                    string code = SendImage(port, GHI.Utilities.Bitmaps.ConvertToFile(bitmap));
                    if (code == null)
                    {
                        //the bar code was not recognized
                        //show the camera page
                        View.ShowMessageWithButton(CODE_NOT_RECOGNIZED_MSG, () => { View.ShowCamera(); });
                        return;
                    }

                    //get book info
                    GetBookInformationsResponse bookInfoResponse = proxy.GetBookInformations(new GetBookInformations() { barcode = code });
                    if (bookInfoResponse.GetBookInformationsResult.ISBN == null)
                    {
                        //the book is not in the WS database
                        View.ShowMessageWithButton(INVALID_BARCODE_MSG, () => { View.ShowCamera(); });
                        return;
                    }

                    LibraryModelSingleton.Instance.Book = bookInfoResponse.GetBookInformationsResult;
                    if (state == InnerState.BORROWING)
                    {
                        //show the book information
                        View.ShowBooKForLoan();
                    }
                    else if (state == InnerState.RETURNING)
                    {
                        //call the ws in order to get the loan information
                        GetLoanState loanStateRequest = new GetLoanState();
                        loanStateRequest.bookID = bookInfoResponse.GetBookInformationsResult.ID;
                        loanStateRequest.userID = LibraryModelSingleton.Instance.User.ID;
                        loanStateRequest.returned = false;
                        GetLoanStateResponse loanStateResponse = proxy.GetLoanState(loanStateRequest);
                        if (loanStateResponse.GetLoanStateResult.LoanStateCompositeType.Length == 0)
                        {
                            //the book was not borrowed by the user
                            View.ShowMessageWithButton(INVALID_BARCODE_MSG, () => { View.ShowWelcomePage(); });
                        }
                        else
                        {
                            //show the book information
                            LibraryModelSingleton.Instance.StartDate = loanStateResponse.GetLoanStateResult.LoanStateCompositeType[0].LoanStartDate;
                            LibraryModelSingleton.Instance.DueDate = loanStateResponse.GetLoanStateResult.LoanStateCompositeType[0].LoanExpirationDate;
                            View.ShowBooKForReturning();
                        }
                    }
                }
            }
            catch (Exception)
            { //show a message and then returns in the login page
                state = InnerState.INITIAL;
                View.ShowMessageWithButton(ERROR_MSG, () => { this.Logout(); });
            }
        }