def undo_order(self): if len(self.buy_list) < 1: putil.trace("cant undo no more") return item = self.buy_list.pop() self.remove_item(item) GUI.info("Removed {} from cart".format(item.name))
def do_POST(self): print "got a POST request" length = int(self.headers.getheader('content-length')) indata = self.rfile.read(length) data = json.loads(indata) # You now have a dictionary of the post data putil.trace(data) ret = {} #return value if not validate_order(data): putil.trace("status: HAL.DENY") ret['status'] = "{}".format(Hal.DENY) elif not validate_accountbalance(data): putil.trace("nofounds") putil.trace("status: HAL.NOFOUNDS") ret['status'] = "{}".format(Hal.NOFUNDS) else: putil.trace("status: HAL.ACCEPT") ret['status'] = Hal.ACCEPT self.send_response(200) self.send_header('Content-type', 'text/json') self.end_headers() self.wfile.write(json.dumps(ret)); self.wfile.close()
def handle_hal(self, json_data): """handles the dict generated from parsing the json data fields: status: Hal.ACCEPT ect as int REQUIRED """ status = int(json_data['status']) print "status: ", status if status == Hal.ACCEPT: Foodputer.set_state(start) GUI.accepted_order() elif status == Hal.DENY: if Pin_check.tries_left == 0: self.handle_abort() else: Foodputer.set_state(ordering) GUI.wrong_pin(Pin_check.tries_left) Pin_check.tries_left -= 1; elif status == Hal.NOFUNDS: Foodputer.set_state(ordering) GUI.not_enough_money(); else: putil.trace("no such return is nice {}".format(status)); self.handle_abort()
def handle_barcode(self, str): putil.trace("ding") item = Products.get_from_barcode(str) if item == None: GUI.warn("Unknown product cannot buy here") return GUI.info("Added {} - \"{}\"".format(item.name, item.text)) Foodputer.add_item(item)
def remove_item(self, item): if not item.id in self.cart: putil.trace("NO such item in cart") return cur = self.cart[item.id] cnt = cur[1] - 1 if cnt == 0: del self.cart[item.id] else: self.cart[item.id] = (item, cnt) #update cnt
def read_productlist(): """read a csv text file with barcodes and descriptions Store in ascociative array, mapping barcode -> product """ for line in open(productfile): if line.startswith("#"): continue p = line.split(",") Product.atlas[p[0]] = Product(p[1], p[2], float(p[3]), p[0]) putil.trace(Product.atlas[p[0]])
def handle_hal(self, resp): putil.trace("rfid check data: \"{}\"".format(resp)) if not resp or len(resp) < 2: putil.trace("HAL doesnt know user or no contact") Foodputer.set_state(start) GUI.no_rfid() return Foodputer.new_order( resp['user'], resp['token']) Foodputer.set_state(ordering) Pin_check.tries_left = 3; GUI.valid_rfid()
def run(self): nr = self.rfid.nr #for debugging and mocking if (LOCAL): data = {'user': "******", 'token':"TokenToken"} if nr != "r2" else None time.sleep(1) self.rfid.handle_hal(data) return data = None try: resp = urllib2.urlopen("{}/{}".format(URL, nr)) print resp.info() data = json.loads(resp.read()) except (urllib2.URLError, urllib2.HTTPError), e: putil.trace("could not contact hal!!") self.alive = False; self.rfid.handle_fail(e)
def handle_input(str): """Is is RFID, BARCODE og PIN and what action to take""" #TODO move input accept to gui? if len(str) < 1 or not Foodputer.accept_input: return if str == "a": Foodputer.state.handle_abort() elif str == "u": Foodputer.state.handle_undo() elif is_rfid(str): Foodputer.state.handle_rfid(str) elif is_barcode(str): Foodputer.state.handle_barcode(str) elif is_pin(str): Foodputer.state.handle_pin(str) else: print "Unknown input ", str putil.trace("main in state: {}".format(type(Foodputer.state).__name__))
def __init__(self, imgname, layout = (1,1), animrate = 1, pos = (200, 200)): pygame.init() try: img = pygame.image.load("./img/{}".format(imgname)) except pygame.error: putil.trace("Could not open ./img/{}".format(imgname)) self.img = img #img.convert() #maybe convert alpha, but to make sure the color format matches screen etc w = img.get_width() h = img.get_height() fw = w / cols(layout) fh = h / rows(layout) frames = [] for row in range(rows(layout)): for col in range(cols(layout)): r = pygame.Rect((col*fw, row*fh), (fw, fh)) frames.append(self.img.subsurface(r)) self.frames = frames self.cur_frame = 0 self.walltime = 0 self.animrate = animrate self.pos = pos
def run(self): pin = self.pincheck.pin #for debugging and mocking if (LOCAL): data = ACCEPT if pin == "p4": data = DENY elif pin == "p3": data = NOFUNDS time.sleep(1) if self.alive: self.pincheck.handle_hal(data) return data = Foodputer.get_order() #remove token from data, but use it in sha1-digest msg = "{}{}{}{}".format(data['name'], data['total'],data['token'], pin) digest = hashlib.sha512(msg).hexdigest() data['signature'] = digest payload = json.dumps(data) ret = None try: print "PAYLOAD is: ", payload resp = urllib2.urlopen(URL, payload) print "resp info", resp.info() txt = resp.read() print "JSON is: ", txt ret = json.loads(txt) except (urllib2.URLError, urllib2.HTTPError), e: putil.trace("could not contact hal!!") self.alive = False self.pincheck.handle_fail(e)
def handle_pin(self, str): putil.trace("{}:handle_pin({})".format(type(self).__name__, str))
def handle_undo(self): putil.trace("{}:handle_undo({})".format(type(self).__name__, ""))
def set_state(s): putil.trace("set state: {}".format(type(s).__name__)) if Foodputer.state != None: Foodputer.state.on_exit() Foodputer.state = s; s.on_entry()
def undo(): if not Foodputer.order: putil.trace("trying to undo a non-cart") return Foodputer.order.undo_order()
def handle_pin(self, str): putil.trace("shop smart, shop k-mart") Pin_check.pin = str Foodputer.set_state(pin_check)
def on_entry(self): putil.trace("GUI enter: {}".format(type(self).__name__))
def abort(self): putil.trace("kill validator") self.alive = 0
def on_entry(self): putil.trace("new shopper") GUI.clear_messages()
def abort(self): putil.trace("Killing the fetcher") self.alive = 0
payload = json.dumps(data) ret = None try: print "PAYLOAD is: ", payload resp = urllib2.urlopen(URL, payload) print "resp info", resp.info() txt = resp.read() print "JSON is: ", txt ret = json.loads(txt) except (urllib2.URLError, urllib2.HTTPError), e: putil.trace("could not contact hal!!") self.alive = False self.pincheck.handle_fail(e) except: putil.trace("Other error {}".format(sys.exc_info()[0])) if self.alive: self.pincheck.handle_hal(ret) class id_fetcher(Thread): def __init__(self, rfid): Thread.__init__(self) self.rfid = rfid self.alive = 1 def abort(self):
def handle_rfid(self, str): putil.trace("Velcome user") rfid_check.nr = str Foodputer.set_state(rfid_check)
def validate_accountbalance(data): amount = float(data['total']) putil.trace("Amount to see is {}".format(amount)) return amount < 42;
def on_entry(self): GUI.start_pincheck() self.validator = Hal.Validator(self) putil.trace("Check pin and order and everything") self.validator.start()
def set_state(s): putil.trace("GUI set state: {}".format(type(s).__name__)) if Screen.state != None: Screen.state.on_exit() s.on_entry() Screen.state = s;
def add_item(item): if not Foodputer.order: putil.trace("trying to put things in non-cart") return Foodputer.order.add_item(item)
def on_exit(self): putil.trace("GUI exit: {}".format(type(self).__name__))
def on_entry(self): GUI.wait_for_rfid() self.fetcher = Hal.id_fetcher(self) putil.trace("get the rfid from hal") self.fetcher.start()