/// <summary>
        /// Gets the coin dispenser.
        /// </summary>
        /// <returns>Coin dispenser</returns>
        private static ICoinDispenser GetCoinDispenser()
        {
            ICoinDispenser dispenser = CoinDispenser.Initialise();

            dispenser.SerialPortName = GetCOMPortName();
            return(dispenser);
        }
        /// <summary>
        /// Gets the status.
        /// </summary>
        /// <returns>DispenserStatus</returns>
        public static DispenserStatus GetStatus(out string errorDesc, out uint iErrorcode)
        {
            DispenserStatus status        = DispenserStatus.NotAvailable;
            ICoinDispenser  coinDispenser = null;

            errorDesc  = string.Empty;
            iErrorcode = 1;
            try
            {
                coinDispenser = GetCoinDispenser();
                CoinDispStatus dispStatus = coinDispenser.GetStatus();

                if (dispStatus != null)
                {
                    if (IsSucceeded(dispStatus.ErrorCode))
                    {
                        status = DispenserStatus.Available;
                    }
                    errorDesc  = dispStatus.ErrDesc;
                    iErrorcode = dispStatus.ErrorCode;
                }
                else
                {
                    errorDesc = "Unable to connect cash dispenser.";
                }

                LogManager.WriteLog("|=> (GetStatus) : " + errorDesc, LogManager.enumLogLevel.Info);
            }
            //catch (System.IO.IOException ex)
            //{
            //    ExceptionManager.Publish(ex);
            //}
            catch (Exception ex)
            {
                ExceptionManager.Publish(ex);
            }
            finally
            {
                coinDispenser = null;
                if (string.IsNullOrEmpty(errorDesc))
                {
                    errorDesc = "Unable to connect cash dispenser.";
                }
            }

            return(status);
        }
示例#3
0
 public VendingMachine(ICoinDispenser coinDispenser, IDenominationConverter denominationConverter, ILogger <VendingMachine> logger)
 {
     _coinDispenser         = coinDispenser ?? throw new ArgumentNullException(nameof(coinDispenser));
     _denominationConverter = denominationConverter ?? throw new ArgumentNullException(nameof(denominationConverter));
     _logger = logger ?? throw new ArgumentNullException(nameof(logger));
 }
        /// <summary>
        /// Dispenses the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>DispenseResult</returns>
        public DispenseResult Dispense(decimal value)
        {
            ICoinDispenser coinDispenser  = null;
            DispenseResult result         = new DispenseResult();
            decimal        issuedValue    = value;
            decimal        rejectedValue  = 0;
            decimal?       issuedValue2   = 0;
            decimal?       rejectedValue2 = 0;
            CoinDispStatus dispStatus     = null;
            bool           success        = false;

            LogManager.WriteLog("|=> (Dispense) : " +
                                ((this.DeckType == TypeOfDeck.Lower) ? "Lower" : "Upper") + " " +
                                "Value : " + value.ToString(),
                                LogManager.enumLogLevel.Info);
            bool canUpdateToDB = false;

            try
            {
                // COM API CALL
                result.Value = value;
                try
                {
                    coinDispenser = GetCoinDispenser();
                    if (this.DeckType == TypeOfDeck.Lower)
                    {
                        dispStatus = coinDispenser.DispenseLowDeck((ushort)value);
                    }
                    else
                    {
                        dispStatus = coinDispenser.DispenseUPDeck((ushort)value);
                    }
                }
                catch (Exception ex)
                {
                    ExceptionManager.Publish(ex);
                }
                finally
                {
                    if (dispStatus != null)
                    {
                        LogManager.WriteLog("|=> (Dispense) : Error Code : " + dispStatus.ErrorCode.ToString(), LogManager.enumLogLevel.Info);
                        success       = IsSucceeded(dispStatus.ErrorCode);
                        canUpdateToDB = true;
                        string outputMessage = "Output Message => Deck => {0}, Dispensed Value => {1:F}, Rejected Value => {2:F}";

                        if (this.DeckType == TypeOfDeck.Lower)
                        {
                            issuedValue   = dispStatus.LowDispNotes;
                            rejectedValue = dispStatus.LowRejNotes;
                            try
                            {
                                outputMessage = string.Format(outputMessage, "Lower", issuedValue, rejectedValue);
                            }
                            catch { }
                        }
                        else
                        {
                            issuedValue   = dispStatus.UPDispNotes;
                            rejectedValue = dispStatus.UPRejNotes;
                            try
                            {
                                outputMessage = string.Format(outputMessage, "Upper", issuedValue, rejectedValue);
                            }
                            catch { }
                        }
                        result.ErrorDescription = dispStatus.ErrDesc;
                        LogManager.WriteLog(outputMessage, LogManager.enumLogLevel.Info);
                    }
                    else
                    {
                        result.ErrorDescription = "Unable to dispense from " + this.CassetteAlias + ".";
                        result.Failed           = true;
                    }

                    LogManager.WriteLog("|=> (Dispense) : " + result.ErrorDescription, LogManager.enumLogLevel.Info);
                    coinDispenser = null;
                }

                if (canUpdateToDB)
                {
                    // UPDATE IT INTO DATABASE
                    using (CashDispenserDBDataContext context = new CashDispenserDBDataContext(CashDispenserFactory.ExchangeConnectionString))
                    {
                        int?iResult = 0;
                        context.UpdateCashDispenserItemValues(this.CassetteID,
                                                              issuedValue,
                                                              rejectedValue,
                                                              ref issuedValue2,
                                                              ref rejectedValue2,
                                                              ref iResult);

                        if (iResult != null && iResult.HasValue)
                        {
                            result.Result = (iResult.Value > 0);
                        }
                        LogManager.WriteLog("|=> (Dispense) : Result : " + result.Result.ToString(), LogManager.enumLogLevel.Info);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Publish(ex);
            }
            finally
            {
                if (result != null)
                {
                    result.Result = success;
                    if (canUpdateToDB)
                    {
                        result.IssuedValue   = issuedValue;
                        result.RejectedValue = rejectedValue;

                        if (issuedValue2 != null && issuedValue2.HasValue)
                        {
                            this.IssuedValue = issuedValue2.Value;
                        }
                        if (rejectedValue2 != null && rejectedValue2.HasValue)
                        {
                            this.RejectedValue = rejectedValue2.Value;
                        }
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Tests the status.
        /// </summary>
        /// <param name="errorDesc">The error desc.</param>
        /// <returns>DispenserStatus</returns>
        public DispenserStatus TestStatus(out string errorDesc)
        {
            DispenserStatus status        = DispenserStatus.NotAvailable;
            ICoinDispenser  coinDispenser = null;

            errorDesc = string.Empty;
            string         deckMsg        = "Deck Type : " + ((this.DeckType == TypeOfDeck.Lower) ? "Lower" : "Upper");
            CoinDispStatus dispStatus     = null;
            bool           canUpdateToDB  = false;
            decimal        issuedValue    = 0;
            decimal        rejectedValue  = 0;
            decimal?       issuedValue2   = 0;
            decimal?       rejectedValue2 = 0;

            try
            {
                coinDispenser = GetCoinDispenser();

                if (this.DeckType == TypeOfDeck.Lower)
                {
                    dispStatus = coinDispenser.TestLowerDeck();
                }
                else
                {
                    dispStatus = coinDispenser.TestUpperDeck();
                }

                if (dispStatus != null)
                {
                    if (IsSucceeded(dispStatus.ErrorCode))
                    {
                        status = DispenserStatus.Available;
                    }
                    errorDesc = dispStatus.ErrDesc;
                }
                else
                {
                    errorDesc = "Unable to connect cash dispenser.";
                }

                LogManager.WriteLog("|=> (TestStatus) : " + deckMsg + ", " + errorDesc, LogManager.enumLogLevel.Info);
            }
            catch (Exception ex)
            {
                ExceptionManager.Publish(ex);
            }
            finally
            {
                try
                {
                    if (dispStatus != null)
                    {
                        LogManager.WriteLog("|=> (Dispense) : Error Code : " + dispStatus.ErrorCode.ToString(), LogManager.enumLogLevel.Info);
                        canUpdateToDB = true;

                        if (this.DeckType == TypeOfDeck.Lower)
                        {
                            issuedValue   = dispStatus.LowDispNotes;
                            rejectedValue = dispStatus.LowRejNotes;
                        }
                        else
                        {
                            issuedValue   = dispStatus.UPDispNotes;
                            rejectedValue = dispStatus.UPRejNotes;
                        }
                    }

                    if (canUpdateToDB)
                    {
                        // UPDATE IT INTO DATABASE
                        using (CashDispenserDBDataContext context = new CashDispenserDBDataContext(CashDispenserFactory.ExchangeConnectionString))
                        {
                            int?iResult = 0;
                            context.UpdateCashDispenserItemValues(this.CassetteID,
                                                                  issuedValue,
                                                                  rejectedValue,
                                                                  ref issuedValue2,
                                                                  ref rejectedValue2,
                                                                  ref iResult);
                            LogManager.WriteLog("|=> (TestStatus) : Result : " + (iResult.Value > 0).ToString(), LogManager.enumLogLevel.Info);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ExceptionManager.Publish(ex);
                }
                coinDispenser = null;
            }

            return(status);
        }