Пример #1
0
 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))
Пример #2
0
    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()
Пример #3
0
    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()
Пример #4
0
 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)
Пример #5
0
 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
Пример #6
0
 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]])
Пример #7
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()
Пример #8
0
    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)
Пример #9
0
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__))
Пример #10
0
    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
Пример #11
0
    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)
Пример #12
0
 def handle_pin(self, str):
     putil.trace("{}:handle_pin({})".format(type(self).__name__, str))
Пример #13
0
 def handle_undo(self):
     putil.trace("{}:handle_undo({})".format(type(self).__name__, ""))
Пример #14
0
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()
Пример #15
0
def undo():
    if not Foodputer.order:
        putil.trace("trying to undo a non-cart")
        return
    Foodputer.order.undo_order()
Пример #16
0
 def handle_pin(self, str):
     putil.trace("shop smart, shop k-mart")
     Pin_check.pin = str
     Foodputer.set_state(pin_check)
Пример #17
0
 def on_entry(self):
     putil.trace("GUI enter: {}".format(type(self).__name__))
Пример #18
0
 def abort(self):
     putil.trace("kill validator")
     self.alive = 0
Пример #19
0
 def on_entry(self):
     putil.trace("new shopper")
     GUI.clear_messages()
Пример #20
0
 def abort(self):
     putil.trace("Killing the fetcher")
     self.alive = 0
Пример #21
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):
Пример #22
0
 def handle_rfid(self, str):
     putil.trace("Velcome user")
     rfid_check.nr = str
     Foodputer.set_state(rfid_check)
Пример #23
0
def validate_accountbalance(data):
    amount = float(data['total'])
    putil.trace("Amount to see is {}".format(amount))
    return amount < 42;
Пример #24
0
 def on_entry(self):
     GUI.start_pincheck()
     self.validator = Hal.Validator(self)
     putil.trace("Check pin and order and everything")
     self.validator.start()
Пример #25
0
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;
Пример #26
0
def add_item(item):
    if not Foodputer.order:
        putil.trace("trying to put things in non-cart")
        return
    Foodputer.order.add_item(item)
Пример #27
0
 def on_exit(self):
     putil.trace("GUI exit: {}".format(type(self).__name__))
Пример #28
0
 def on_entry(self):
     GUI.wait_for_rfid()
     self.fetcher = Hal.id_fetcher(self)
     putil.trace("get the rfid from hal")
     self.fetcher.start()