Exemple #1
0
 // returns the most recent card scan
 public static CardScan LastCardScan()
 {
     using (klick_vending_machineEntities context = new klick_vending_machineEntities())
     {
         CardScan cs = (
             from c in context.CardScans orderby c.ScanDate descending
             select c).FirstOrDefault();
         return(cs);
     }
 }
        private static CardScanResult GetCardScanResult(CardScan cardScan)
        {
            // now we have to create a result, and validate against the keyscan database
            using(klick_vending_machineEntities context = new klick_vending_machineEntities()) {
                CardScanResult result = new CardScanResult()
                {
                    CardScanID = cardScan.CardScanID,
                    ResultDate = DateTime.Now,
                    Status = "invalid"
                };

                context.CardScanResults.Add(result);

                try {
                    string hexID = cardScan.CardID.Replace(" ", ""); // strip out extra spaces

                    // assert correct length
                    if (hexID.Length == 16)
                    {
                        // take the last 4 octets only (this is a 16-digit string)
                        hexID = hexID.Substring(8);

                        string binaryID = String.Join(String.Empty,
                          hexID.Select(
                            c => Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0')
                          )
                        );

                        // card batch = 8 digits, starting at digit 7
                        // card id = 15 digits, starting at digit 16
                        string binaryCardBatch = binaryID.Substring(7, 8);
                        string binaryCardNumber = binaryID.Substring(16, 15);

                        // convert to decimal, then padded string (3-digit batch, 5-digit number)
                        result.CardBatch = Convert.ToInt64(binaryCardBatch, 2).ToString().PadLeft(3, '0');
                        result.CardNumber = Convert.ToInt64(binaryCardNumber, 2).ToString().PadLeft(5, '0');

                        // lookup card in keyscan database and validate, and grab first and lastname

                        SqlConnection o_sqlConnection = new SqlConnection();
                        o_sqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["keyscandb"].ConnectionString;
                        o_sqlConnection.Open();
                        SqlCommand o_sqlCommand = new SqlCommand();
                        o_sqlCommand.CommandText = "Select * from CARD where CARD_BATCH = " + result.CardBatch + " and CARD_NUMBER = " + result.CardNumber + " and isnull([CARD_VALIDFROM], getdate()-1) < Getdate() and isnull([CARD_VALIDTO], getdate() + 1) > getdate() and isnull([CARD_ARCHIVED],0) = 0 and isnull([CARD_LIMITED],0) = 0 and [CARD_DELETED] = 0";
                        o_sqlCommand.Connection = o_sqlConnection;
                        SqlDataReader o_sqlDataReader;
                        o_sqlDataReader = o_sqlCommand.ExecuteReader();
                        if (o_sqlDataReader.Read())
                        {
                            result.CardFirstName = o_sqlDataReader["CARD_FIRSTNAME"].ToString();
                            result.CardLastName = o_sqlDataReader["CARD_LASTNAME"].ToString();
                            result.Status = "valid";
                        }
                        else {
                            result.Status = "invalid";
                        }
                        o_sqlDataReader.Close();
                        o_sqlConnection.Close();

                    }
                }
                catch(Exception e)
                {
                    Logger.WriteException(e);
                    result.Status = "failed";
                   // Error.CreateError(e);
                }

                context.SaveChanges();

                return result;
            }
        }
        // returns the message for the Arduino
        private static string ProcessCardScan(CardScan cardScan)
        {
            CardScanResult result = GetCardScanResult(cardScan);

            if (result.Status == "valid")
            {
                return CommandHandler.MSG_VEND_INPUT_READY;
            }

            return CommandHandler.MSG_ERROR_INVALID_CARD;
        }