1 from DispenseInterface import DispenseInterface
5 from subprocess import Popen, PIPE
6 from LDAPConnector import get_uid,get_uname, set_card_id
9 Author: Mitchell Pomery (bobgeorge33)
11 System to connect to OpenDispense2.
12 Most of this code has been copied out of VendServer.py, then had variables updated so that it runs.
13 This is so VendServer can easily operate regardless of the current accounting backend.
14 Documentation for this code can be found inder Dispence.DispenceInterface
16 class OpenDispense(DispenseInterface):
22 def __init__(self, userId=None, username=None, loggedIn=False):
23 self._username = username
24 self._loggedIn = loggedIn
27 acct, unused = Popen(['dispense', 'acct', self._username], close_fds=True, stdout=PIPE).communicate()
28 # this is fucking appalling
29 flags = acct[acct.find("(")+1:acct.find(")")].strip()
30 if 'disabled' in flags:
32 if 'internal' in flags:
34 self._disabled = False
37 def authUserIdPin(userId, pin):
40 info = pwd.getpwuid(userId)
42 logging.info('getting pin for uid %d: user not in password file'%uid)
45 if info.pw_dir == None: return False
46 pinfile = os.path.join(info.pw_dir, '.pin')
50 logging.info('getting pin for uid %d: .pin not found in home directory'%uid)
53 logging.info('getting pin for uid %d: .pin has wrong permissions. Fixing.'%uid)
54 os.chmod(pinfile, 0600)
58 logging.info('getting pin for uid %d: I cannot read pin file'%uid)
60 pinstr = f.readline().strip()
62 if not re.search('^[0-9]{4}$', pinstr):
63 logging.info('getting pin for uid %d: %s not a good pin'%(uid,repr(pinstr)))
65 return OpenDispense(userId, info.pw_name, (int(pin)==int(pinstr)))
68 def authMifareCard(cardId):
69 return OpenDispense(get_uid(cardId), get_uname(get_uid(cardId)), True)
71 def addCard(self, cardId):
72 set_card_id(self._userId, cardId)
77 def getUsername(self):
82 acct, unused = Popen(['dispense', 'acct', self._username], close_fds=True, stdout=PIPE).communicate()
83 # this is fucking appalling
84 balance = acct[acct.find("$")+1:acct.find("(")].strip()
87 def getItemInfo(itemId):
88 itemId = OpenDispenseMapping.vendingMachineToOpenDispense(itemId)
89 args = ('dispense', 'iteminfo', itemId)
90 info, unused = Popen(args, close_fds=True, stdout=PIPE).communicate()
91 m = re.match("\s*[a-z]+:\d+\s+(\d+)\.(\d\d)\s+([^\n]+)", info)
92 cents = int(m.group(1))*100 + int(m.group(2))
93 # return (name, price in cents)
94 return (m.group(3), cents)
99 def dispenseItem(self, itemId):
100 itemId = OpenDispenseMapping.vendingMachineToOpenDispense(itemId)
104 print('dispense -u "%s" %s'%(self._username, itemId))
105 os.system('dispense -u "%s" %s'%(self._username, itemId))
109 This class abstracts the idea of item numbers.
110 It removes the need for VendServer to know the mappings between inputted numbers
111 and the equivalent itemId in OpenDispense.
113 class OpenDispenseMapping():
116 def vendingMachineToOpenDispense(itemId):
117 _mappingFile = "OpenDispenseMappings.conf"
118 fh = open(_mappingFile)
122 if line.startswith(str(itemId) + " "):
123 map = line[len(str(itemId)) + 1:].strip()