From 9d34b41fdab2ec8c3cc05a119ab8295dffc80f76 Mon Sep 17 00:00:00 2001 From: Mitchell Pomery Date: Sat, 4 Apr 2015 12:10:58 +0800 Subject: [PATCH] Implimenting the abstracted dispense class into the code --- VendServer/VendServer.py | 116 ++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 62 deletions(-) diff --git a/VendServer/VendServer.py b/VendServer/VendServer.py index 6a7af6b..f642dc4 100755 --- a/VendServer/VendServer.py +++ b/VendServer/VendServer.py @@ -164,38 +164,33 @@ class VendServer(): _pin_pin = '----' _last_card_id = -1 + dispense = None + """ Show information to the user as to what can be dispensed. """ def scroll_options(self, username, mk, welcome = False): # If the user has just logged in, show them their balance if welcome: - # Balance checking - acct, unused = Popen(['dispense', 'acct', username], close_fds=True, stdout=PIPE).communicate() - # this is fucking appalling - balance = acct[acct.find("$")+1:acct.find("(")].strip() + balance = dispense.getBalance() msg = [(self.center('WELCOME'), False, TEXT_SPEED), - (self.center(username), False, TEXT_SPEED), + (self.center(self.dispense.getUsername()), False, TEXT_SPEED), (self.center(balance), False, TEXT_SPEED),] else: msg = [] choices = ' '*10+'CHOICES: ' # Show what is in the coke machine + # Need to update this so it uses the abstracted system cokes = [] - for i in range(0, 7): - args = ('dispense', 'iteminfo', 'coke:%i' % i) - info, unused = Popen(args, close_fds=True, stdout=PIPE).communicate() - m = re.match("\s*[a-z]+:\d+\s+(\d+)\.(\d\d)\s+([^\n]+)", info) - cents = int(m.group(1))*100 + int(m.group(2)) - cokes.append('%i %i %s' % (i, cents, m.group(3))); + for i in ['08', '18', '28', '38', '48', '58', '68']: + cokes.append((i, self.dispense.getItemInfo(i))) for c in cokes: - c = c.strip() - (slot_num, price, slot_name) = c.split(' ', 2) - if slot_name == 'dead': continue - choices += '%s-(%sc)-%s8 '%(slot_name, price, slot_num) + if c[1][0] == 'dead': + continue + choices += '%s-(%sc)-%s8 '%(c[1][0], c[1][1], c[0]) # Show the final few options choices += '55-DOOR ' @@ -210,6 +205,7 @@ class VendServer(): """ Verify the users pin """ + """ def _check_pin(self, uid, pin): print "_check_pin('",uid,"',---)" if uid != self._pin_uid: @@ -219,7 +215,7 @@ class VendServer(): logging.info('getting pin for uid %d: user not in password file'%uid) return None if info.pw_dir == None: return False - pinfile = os.path.join(info.pw_dir, '.pin') + pinfile = os.path.join(info.pw_dir, '.pin') try: s = os.stat(pinfile) except OSError: @@ -248,10 +244,12 @@ class VendServer(): else: logging.info("Pin incorrect for %d",uid) return pin == int(pinstr) + """ """ Check if the users account has been disabled """ + """ def acct_is_disabled(self, name=None): if name == None: name = self._pin_uname @@ -263,16 +261,20 @@ class VendServer(): if 'internal' in flags: return True return False + """ """ Check that the user has a valid pin set """ + """ def has_good_pin(self, uid): return self._check_pin(uid, None) != None + """ """ Verify the users pin. """ + """ def verify_user_pin(self, uid, pin, skip_pin_check=False): if skip_pin_check or self._check_pin(uid, pin) == True: info = pwd.getpwuid(uid) @@ -287,6 +289,7 @@ class VendServer(): else: logging.info('refused pin for uid %d'%(uid)) return None + """ """ In here just for fun. @@ -511,7 +514,7 @@ class VendServer(): self.vstatus.last_timeout_refresh = None else: # Price check mode. - self.price_check() + self.dispense.getItemInfo(self.vstatus.cur_selection) self.vstatus.cur_selection = '' self.vstatus.time_to_autologout = None self.vstatus.last_timeout_refresh = None @@ -579,6 +582,7 @@ class VendServer(): """ Find the price of an item. """ + """ def price_check(): if self.vstatus.cur_selection[1] == '8': args = ('dispense', 'iteminfo', 'coke:' + self.vstatus.cur_selection[0]) @@ -592,6 +596,7 @@ class VendServer(): price, shortname, name = get_snack( '--' ) dollarprice = "$%.2f" % ( price / 100.0 ) self.v.display(self.vstatus.cur_selection+' - %s'%dollarprice) + """ """ Triggered when the user presses a button while entering their pin. @@ -611,8 +616,8 @@ class VendServer(): self.vstatus.cur_pin += chr(key + ord('0')) self.vstatus.mk.set_message('PIN: '+'X'*len(self.vstatus.cur_pin)) if len(self.vstatus.cur_pin) == PIN_LENGTH: - self.vstatus.username = self.verify_user_pin(int(self.vstatus.cur_user), int(self.vstatus.cur_pin)) - if self.vstatus.username: + self.dispense.authUserIdPin(self.vstatus.cur_user, self.vstatus.cur_pin) + if self.dispense.getUsername(): self.v.beep(0, False) self.vstatus.cur_selection = '' self.vstatus.change_state(STATE_GET_SELECTION) @@ -702,6 +707,7 @@ class VendServer(): return + """ if not self.has_good_pin(uid): logging.info('user '+self.vstatus.cur_user+' has a bad PIN') self.vstatus.mk.set_messages( @@ -712,8 +718,9 @@ class VendServer(): self.reset_idler(3) return - - if self.acct_is_disabled(): + """ + + if self.dispense.isDisabled(): logging.info('user '+self.vstatus.cur_user+' is disabled') self.vstatus.mk.set_messages( [(' '*11+'ACCOUNT DISABLED'+' '*11, False, 3)]) @@ -921,47 +928,37 @@ class VendServer(): self._last_card_id = card_id - try: - self.vstatus.cur_user = get_uid(card_id) - logging.info('Mapped card id to uid %s'%self.vstatus.cur_user) - self.vstatus.username = get_uname(self.vstatus.cur_user) - if self.acct_is_disabled(self.vstatus.username): - self.vstatus.username = '-disabled-' - except ValueError: - self.vstatus.username = None - if self.vstatus.username == '-disabled-': + self.dispense.authMifareCard(card_id) + logging.info('Mapped card id to uid %s'%self.dispense.getUsername()) + if not self.dispense.isLoggedIn(): self.v.beep(40, False) self.vstatus.mk.set_messages( - [(self.center('ACCT DISABLED'), False, 1.0), + [(self.center('BAD CARD'), False, 1.0), (self.center('SORRY'), False, 0.5)]) self.vstatus.cur_user = '' self.vstatus.cur_pin = '' - self.vstatus.username = None + self._last_card_id = -1 self.reset_idler(2) return - elif self.vstatus.username: - self.v.beep(0, False) - self.vstatus.cur_selection = '' - self.vstatus.change_state(STATE_GET_SELECTION) - self.scroll_options(self.vstatus.username, self.vstatus.mk, True) - return - else: + elif self.dispense.isDisabled(): self.v.beep(40, False) self.vstatus.mk.set_messages( - [(self.center('BAD CARD'), False, 1.0), + [(self.center('ACCT DISABLED'), False, 1.0), (self.center('SORRY'), False, 0.5)]) - self.vstatus.cur_user = '' - self.vstatus.cur_pin = '' - self._last_card_id = -1 - + self.dispense.logOut() self.reset_idler(2) return + else: + self.vstatus.cur_selection = '' + self.vstatus.change_state(STATE_GET_SELECTION) + self.scroll_options(self.vstatus.username, self.vstatus.mk, True) + return """ Triggered when a user swipes their card and the machine is logged in. """ - def handle_mifare_add_user_event(self, evnt, params): + def handle_mifare_add_user_event(self, event, params): card_id = params # Translate card_id into uid. @@ -969,25 +966,19 @@ class VendServer(): return self._last_card_id = card_id - - try: - if get_uid(card_id) != None: - self.vstatus.mk.set_messages( - [(self.center('ALREADY'), False, 0.5), - (self.center('ENROLLED'), False, 0.5)]) - # scroll_options(vstatus.username, vstatus.mk) - return - except ValueError: - pass - - logging.info('Enrolling card %s to uid %s (%s)'%(card_id, self.vstatus.cur_user, self.vstatus.username)) - self.set_card_id(self.vstatus.cur_user, self.card_id) - self.vstatus.mk.set_messages( - [(self.center('CARD'), False, 0.5), - (self.center('ENROLLED'), False, 0.5)]) + res = self.dispense.addCard(card_id) - # scroll_options(vstatus.username, vstatus.mk) + if get_uid(card_id) != None: + self.vstatus.mk.set_messages( + [(self.center('ALREADY'), False, 0.5), + (self.center('ENROLLED'), False, 0.5)]) + else: + logging.info('Enrolling card %s to uid %s (%s)'%(card_id, self.vstatus.cur_user, self.vstatus.username)) + self.set_card_id(self.vstatus.cur_user, self.card_id) + self.vstatus.mk.set_messages( + [(self.center('CARD'), False, 0.5), + (self.center('ENROLLED'), False, 0.5)]) def return_to_idle(self, event, params): self.reset_idler() @@ -1050,6 +1041,7 @@ class VendServer(): def run_forever(self, rfh, wfh, options, cf): self.v = VendingMachine(rfh, wfh, USE_MIFARE) + self.dispense = Dispense() self.vstatus = VendState(self.v) self.create_state_table() -- 2.20.1