cnytech

bldkiosk.py

Jul 20th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.92 KB | None | 0 0
  1. """ Python Console Kiosk Client """
  2. # Import modules 'cnytech.py' needs to be in python path or same folder as this script
  3. import socket
  4. import time
  5. import cnytech
  6. from cnytech import Color
  7.  
  8. # Set global constants - These should be changed depending on actual server IP or HOSTNAME and if PORT is not default 16600
  9. CARD_STACK = ('462', '413', '415') # Change or add card stack prefix as needed
  10. HOST_IP = '192.168.0.20' # hostname can be ip or valid host ie. BLD-SERVER
  11. HOST_PORT = 16600 # port the BLD Legacy Verifone host is listening on
  12. SOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Happy Sock
  13. TERMINATION_DESIRED = "THATS ALL FOLKS" # Just some mundane handling of the program termination
  14. BLD_SERVER = (HOST_IP, HOST_PORT) # Server IP , Listening Port
  15.  
  16. # Set console title
  17. cnytech.ConsoleTitle('BLD Loyalty Kiosk - Python Version 1.5') # Clears window and sets title of console
  18.  
  19. # Connect to BLD Verifone Legacy Host
  20. try:
  21.     print('{0}Connecting...{1}'.format(Color.RED, Color.END))
  22.     SOCK.connect(BLD_SERVER)
  23. except TimeoutError:
  24.     print('{0}Server Not Responding{1}'.format(Color.RED, Color.END))
  25.     cnytech.sys.exit(1)
  26.  
  27. def input_loyalty_number():
  28.     """ validate loyalty input """
  29.  
  30.     while True:
  31.         loyalty_input = input('\nEnter Loyalty Card # (Enter to Exit) ').strip()
  32.         if not loyalty_input:
  33.             if cnytech.ynChoice('\nDo you want to exit?', default="yes"):
  34.                 return TERMINATION_DESIRED
  35.         elif not loyalty_input.isdigit():
  36.             print('\nLoyalty Number must contain digits only\n')
  37.         elif len(loyalty_input) != 11:
  38.             print('\nLoyalty Number must be 11 digits in length\n')
  39.         elif not loyalty_input.startswith(CARD_STACK):
  40.             print('\nLoyalty Number not in card stack - {0}\n'.format(CARD_STACK))
  41.         else:
  42.             return loyalty_input
  43.  
  44. def query_results_message(response, loyalty_number):
  45.     """ Format the results message that will be displayed in the console """
  46.    
  47.     if response[7] == '1': # Check for status in position 7. If 1, show discounts and market basket data. Otherwise NO DISCOUNT FOR YOU!
  48.         msg_length = len(response)
  49.         print('\nLoyalty Card {1} has a total Price Per Gallon discount of ${0}'.format(response[9], loyalty_number))
  50.         print('\nTotal Market Basket Discount ${0} | Total Reward Item Discount ${1} | Total Card Discount ${2}'.format(response[13], response[11], response[15]))
  51.         print('\nTotal Market Basket to date ${0} | Amount towards next Market Basket discount ${1}'.format(response[17], response[18]))
  52.         print('\nMarket Basket amount required to spend ${0} for next discount of ${1}'.format(response[19], response[20]))
  53.         if msg_length > 23: #Check to see if there are expiration dates in the received message. If not do not try to print.
  54.             expires = cnytech.dt.datetime.strptime(response[22][4:], '%Y%m%d') # change the date response to date format
  55.             expiry_date = cnytech.dt.datetime.strftime(expires, '%m/%d/%Y') # change the date to a formated date of mm/dd/YYYY
  56.             print('\nDiscounts Begin to Expire on {0}'.format(expiry_date))
  57.     else: # If status is anything else but 1 display NO DISCOUNT
  58.         print('\n{0}NO DISCOUNT{1}\n'.format(Color.RED, Color.END))
  59.  
  60. def loyalty_status(loyalty_number):
  61.     """ Do a loyalty status inquiry to the BLD Verifone Legacy Socket Server """
  62.  
  63.     # Set variables for Query Message
  64.     message_id = int(time.time()) # Sent to server as number of seconds from 1/1/1970. Used to identify messages in and out
  65.     now_date = time.strftime('%Y%m%d') # Todays date sent to server as YYYYMMDD packed '20170510'
  66.     now_time = time.strftime('%H%M%S') # Time sent to server as HHMMSS packed '123249'
  67.     # Send Message to Verifone Legacy Server
  68.     try:
  69.         # Send data message
  70.         # Example message string - """ 6003,1498050423,20170621,090703,0,1,0,46211111111 """
  71.         message_str = '{0},{1},{2},{3},{4},{5},{6},{7}'.format(6003, message_id, now_date, now_time, 0, 1, 0, loyalty_number)
  72.         message = str.encode(message_str)
  73.         SOCK.sendall(message)
  74.         # Look for the response
  75.         amount_received = 0
  76.         amount_expected = len(message)
  77.         while amount_received < amount_expected:
  78.             data = SOCK.recv(10240)
  79.             amount_received += len(data)
  80.             received_msg = data.decode()
  81.             response = received_msg.split(',')
  82.             #response is split at ','
  83.     finally:
  84.         # Display the results
  85.         query_results_message(response, loyalty_number)
  86.  
  87. def loyalty_main():
  88.     """ Main loop for Loyalty Client """
  89.  
  90.     while True:
  91.         loyalty_number = input_loyalty_number()
  92.         if loyalty_number is TERMINATION_DESIRED:
  93.             print('\nProgram Terminated... Closing Socket Connection to Server\n')
  94.             SOCK.close()
  95.             break
  96.         loyalty_status(loyalty_number)
  97.  
  98. # Main
  99. loyalty_main()
Advertisement
Add Comment
Please, Sign In to add comment