Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ Python Console Kiosk Client """
- # Import modules 'cnytech.py' needs to be in python path or same folder as this script
- import socket
- import time
- import cnytech
- from cnytech import Color
- # Set global constants - These should be changed depending on actual server IP or HOSTNAME and if PORT is not default 16600
- CARD_STACK = ('462', '413', '415') # Change or add card stack prefix as needed
- HOST_IP = '192.168.0.20' # hostname can be ip or valid host ie. BLD-SERVER
- HOST_PORT = 16600 # port the BLD Legacy Verifone host is listening on
- SOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Happy Sock
- TERMINATION_DESIRED = "THATS ALL FOLKS" # Just some mundane handling of the program termination
- BLD_SERVER = (HOST_IP, HOST_PORT) # Server IP , Listening Port
- # Set console title
- cnytech.ConsoleTitle('BLD Loyalty Kiosk - Python Version 1.5') # Clears window and sets title of console
- # Connect to BLD Verifone Legacy Host
- try:
- print('{0}Connecting...{1}'.format(Color.RED, Color.END))
- SOCK.connect(BLD_SERVER)
- except TimeoutError:
- print('{0}Server Not Responding{1}'.format(Color.RED, Color.END))
- cnytech.sys.exit(1)
- def input_loyalty_number():
- """ validate loyalty input """
- while True:
- loyalty_input = input('\nEnter Loyalty Card # (Enter to Exit) ').strip()
- if not loyalty_input:
- if cnytech.ynChoice('\nDo you want to exit?', default="yes"):
- return TERMINATION_DESIRED
- elif not loyalty_input.isdigit():
- print('\nLoyalty Number must contain digits only\n')
- elif len(loyalty_input) != 11:
- print('\nLoyalty Number must be 11 digits in length\n')
- elif not loyalty_input.startswith(CARD_STACK):
- print('\nLoyalty Number not in card stack - {0}\n'.format(CARD_STACK))
- else:
- return loyalty_input
- def query_results_message(response, loyalty_number):
- """ Format the results message that will be displayed in the console """
- if response[7] == '1': # Check for status in position 7. If 1, show discounts and market basket data. Otherwise NO DISCOUNT FOR YOU!
- msg_length = len(response)
- print('\nLoyalty Card {1} has a total Price Per Gallon discount of ${0}'.format(response[9], loyalty_number))
- print('\nTotal Market Basket Discount ${0} | Total Reward Item Discount ${1} | Total Card Discount ${2}'.format(response[13], response[11], response[15]))
- print('\nTotal Market Basket to date ${0} | Amount towards next Market Basket discount ${1}'.format(response[17], response[18]))
- print('\nMarket Basket amount required to spend ${0} for next discount of ${1}'.format(response[19], response[20]))
- if msg_length > 23: #Check to see if there are expiration dates in the received message. If not do not try to print.
- expires = cnytech.dt.datetime.strptime(response[22][4:], '%Y%m%d') # change the date response to date format
- expiry_date = cnytech.dt.datetime.strftime(expires, '%m/%d/%Y') # change the date to a formated date of mm/dd/YYYY
- print('\nDiscounts Begin to Expire on {0}'.format(expiry_date))
- else: # If status is anything else but 1 display NO DISCOUNT
- print('\n{0}NO DISCOUNT{1}\n'.format(Color.RED, Color.END))
- def loyalty_status(loyalty_number):
- """ Do a loyalty status inquiry to the BLD Verifone Legacy Socket Server """
- # Set variables for Query Message
- message_id = int(time.time()) # Sent to server as number of seconds from 1/1/1970. Used to identify messages in and out
- now_date = time.strftime('%Y%m%d') # Todays date sent to server as YYYYMMDD packed '20170510'
- now_time = time.strftime('%H%M%S') # Time sent to server as HHMMSS packed '123249'
- # Send Message to Verifone Legacy Server
- try:
- # Send data message
- # Example message string - """ 6003,1498050423,20170621,090703,0,1,0,46211111111 """
- message_str = '{0},{1},{2},{3},{4},{5},{6},{7}'.format(6003, message_id, now_date, now_time, 0, 1, 0, loyalty_number)
- message = str.encode(message_str)
- SOCK.sendall(message)
- # Look for the response
- amount_received = 0
- amount_expected = len(message)
- while amount_received < amount_expected:
- data = SOCK.recv(10240)
- amount_received += len(data)
- received_msg = data.decode()
- response = received_msg.split(',')
- #response is split at ','
- finally:
- # Display the results
- query_results_message(response, loyalty_number)
- def loyalty_main():
- """ Main loop for Loyalty Client """
- while True:
- loyalty_number = input_loyalty_number()
- if loyalty_number is TERMINATION_DESIRED:
- print('\nProgram Terminated... Closing Socket Connection to Server\n')
- SOCK.close()
- break
- loyalty_status(loyalty_number)
- # Main
- loyalty_main()
Advertisement
Add Comment
Please, Sign In to add comment