- self.await_prompt()
- return (code, text)
-
- def handle_event(self, code, text):
- pass
-
- def authed_message(self, message):
- if self.challenge == None:
- return message
- crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
- crc = do_crc(self.secret, crc)
- crc = do_crc(message, crc)
- return message+'|'+('%04x'%crc)
-
- def ping(self):
- self.wfh.write('PING\n')
- (code, string) = self.get_response()
- return (code == '000', code, string)
-
- def vend(self, item):
- if not re.search('^[0-9][0-9]$', item):
- return (False, 'Invalid item requested (%s)'%item)
- self.wfh.write(self.authed_message(('V%s\n'%item)+'\n'))
- (code, string) = self.get_response()
- return (code, string)
-
- def beep(self, duration = None, synchronous = True):
- msg = 'B'
- if synchronous: msg += 'S'
- if duration != None:
- if duration > 255: duration = 255
- if duration < 1: duration = 1
- msg += '%02x'%duration
- self.wfh.write(msg+'\n')
- (code, string) = self.get_response()
- return (code == '500', code, string)
-
- def silence(self, duration = None, synchronous = True):
- msg = 'C'
- if synchronous: msg += 'S'
- if duration != None:
- if duration > 255: duration = 255
- if duration < 1: duration = 1
- msg += '%02x'%duration
- self.wfh.write(msg+'\n')
- (code, string) = self.get_response()
- # FIXME: workaround a bug in rom W. should be just: return (code == '500', code, string)
- return (code == '500' or code == '501', code, string)
-
- def display(self, string):
- if len(string) > 10:
- string = string[0:10]
- self.wfh.write('D'+string+'\n')
- (code, string) = self.get_response()
- return (code == '300', code, string)
+ query = 'SELECT vend_failed(%s)'%id
+ self.db.query(query).getresult()
+
+ def handle_events(self):
+ notifier = self.db.getnotify()
+ while notifier is not None:
+ self.process_requests()
+ notify = self.db.getnotify()
+
+def scroll_options(username, mk, welcome = False):
+ if welcome:
+ msg = [(center('WELCOME'), False, 0.8),
+ (center(username), False, 0.8)]
+ else:
+ msg = []
+ choices = ' '*10+'CHOICES: '
+ coke_machine = file('/home/other/coke/coke_contents')
+ cokes = coke_machine.readlines()
+ for c in cokes:
+ c = c.strip()
+ (slot_num, price, slot_name) = c.split(' ', 2)
+ if slot_name == 'dead': continue
+ choices += '%s8-%s (%sc) '%(slot_num, slot_name, price)
+ choices += '55-DOOR '
+ choices += 'OR A SNACK. '
+ choices += '99 TO READ AGAIN.'
+ msg.append((choices, False, None))
+ mk.set_messages(msg)
+
+def get_pin(uid):
+ try:
+ info = pwd.getpwuid(uid)
+ except KeyError:
+ return None
+ if info.pw_dir == None: return False
+ pinfile = os.path.join(info.pw_dir, '.pin')
+ try:
+ s = os.stat(pinfile)
+ except OSError:
+ return None
+ if s.st_mode & 077:
+ return None
+ try:
+ f = file(pinfile)
+ except IOError:
+ return None
+ pinstr = f.readline()
+ f.close()
+ if not re.search('^'+'[0-9]'*PIN_LENGTH+'$', pinstr):
+ return None
+ return int(pinstr)
+
+def has_good_pin(uid):
+ return get_pin(uid) != None
+
+def verify_user_pin(uid, pin):
+ if get_pin(uid) == pin:
+ info = pwd.getpwuid(uid)
+ return info.pw_name
+ else:
+ return None
+
+def door_open_mode(vending_machine):
+ print "Entering open door mode"
+ v.display("DOOR OPEN")
+ while True:
+ e = v.next_event()
+ if e == None: break
+ (event, params) = e
+ if event == DOOR:
+ if params == 1: # door closed
+ v.display("BYE BYE!")
+ sleep(1)
+ return
+
+def center(str):
+ LEN = 10
+ return ' '*((LEN-len(str))/2)+str
+
+class MessageKeeper:
+ def __init__(self, vendie):
+ # Each element of scrolling_message should be a 3-tuple of
+ # ('message', True/False if it is to be repeated, time to display)
+ self.scrolling_message = []
+ self.v = vendie
+ self.next_update = None
+
+ def set_message(self, string):
+ self.scrolling_message = [(string, False, None)]
+ self.update_display(True)
+
+ def set_messages(self, strings):
+ self.scrolling_message = strings
+ self.update_display(True)
+
+ def update_display(self, forced = False):
+ if not forced and self.next_update != None and time() < self.next_update:
+ return
+ if len(self.scrolling_message) > 0:
+ if len(self.scrolling_message[0][0]) > 10:
+ (m, r, t) = self.scrolling_message[0]
+ a = []
+ exp = HorizScroll(m).expand(padding = 10)
+ if t == None:
+ t = 0.1
+ else:
+ t = t / len(exp)
+ for x in exp:
+ a.append((x, r, t))
+ del self.scrolling_message[0]
+ self.scrolling_message = a + self.scrolling_message
+ newmsg = self.scrolling_message[0]
+ if newmsg[2] != None:
+ self.next_update = time() + newmsg[2]
+ else:
+ self.next_update = None
+ self.v.display(self.scrolling_message[0][0])
+ if self.scrolling_message[0][1]:
+ self.scrolling_message.append(self.scrolling_message[0])
+ del self.scrolling_message[0]
+
+ def done(self):
+ return len(self.scrolling_message) == 0