当前位置: 首页>>代码示例>>Python>>正文


Python Display.print_checkout_info方法代码示例

本文整理汇总了Python中display.Display.print_checkout_info方法的典型用法代码示例。如果您正苦于以下问题:Python Display.print_checkout_info方法的具体用法?Python Display.print_checkout_info怎么用?Python Display.print_checkout_info使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在display.Display的用法示例。


在下文中一共展示了Display.print_checkout_info方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import print_checkout_info [as 别名]
class TagHandler:
    # scan_interval_time out can be used to set the maximum time allowed between 
    # two taps to be considered a check in/out request
    def __init__(self, verbose = False, scan_interval_timeout = constants.SCAN_INTERVAL_TIMEOUT):
        self.scan_interval_timeout = scan_interval_timeout
        self.last_tag = None
        self.last_tag_type = None
        self.current_tag_type = None
        self.last_read_tstamp = -1
        self.display = Display()

    # This may need to be changed depending which tags you associate to staff versus devices
    def is_human_tag(self, tag):
        if len(str(tag)) > 10:
            return True
        return False 

    def query_api(self, url, postdata = None):
        headers = {}
        if postdata:
            postdata = urllib.urlencode(postdata)
        req = urllib2.Request(url, postdata, headers)

        response = None
        code = None
        f = None
       
        try:
            f = urllib2.urlopen(req)
            code = f.getcode()
            resp = f.read()
            response = json.loads(resp)
        except urllib2.URLError as e:
            if hasattr(e, 'reason'):
                response = e.reason
                code = -1
            elif hasattr(e, 'code'):
                response = e.msg
                code = e.code
        except ValueError:  
            response = 'Decoding JSON has failed'
            code = 500
            print resp
        finally:
            if f:
                f.close()
        return response, code

    def _process_checkinout(self, tag_a, tag_b):
        #A Human must be sent to the API call for checkout
        if(self.last_tag_type == "human"):
            data = {"tag_human" : tag_a, "tag_thing" : tag_b}
        elif(self.current_tag_type == "human"):
            data =  {"tag_human" : tag_b, "tag_thing" : tag_a}
        else:
            print "Who are you?"
            return

        url = constants.CHECKINOUT_ENDPOINT % (data['tag_human'], data['tag_thing'])
        resp, code = self.query_api(url)
        if code != 200:
            self._handle_api_exception(resp, code, data)
        else:
            self.display.print_checkout_info(resp)
            ## THis should clear the last tag scanned
            self.last_read_tstamp = (time.time() - self.scan_interval_timeout) 

    def _process_read(self, tag):
        url = constants.WHATIS_ENDPOINT % tag
        resp, code = self.query_api(url)
        if code == 200:
            if not resp.get("error"):
                self.current_tag_type = resp.get("type")
                self.display.print_whatis_info(resp)
            else:
                # We're only going to allow human tags to be updated from client
                user_input = self.display.tag_not_found(tag, self.is_human_tag(tag))
                if user_input is not None:
                    self.associate_human_to_tag(user_input, tag)
            return
        self._handle_api_exception(resp, code, {"tag" : tag})

    def dispatch(self, tag):
        now = time.time()
        self.display.display_on()

        #Find out if this is a human, device, book or other tag
        self._process_read(tag)

        #If we have both a human and thing, lets check in/out
        if (now - self.last_read_tstamp) < self.scan_interval_timeout and self.current_tag_type != self.last_tag_type:
            self._process_checkinout(self.last_tag, tag)
        else:
            #Set last tag data so we have this for next read
            self.last_read_tstamp = now
            self.last_tag = tag
            self.last_tag_type = self.current_tag_type 

    def associate_human_to_tag(self, username, tag):
        url = constants.ADDHUMAN_ENDPOINT % (username, tag)
#.........这里部分代码省略.........
开发者ID:armuk,项目名称:rfid-checkout,代码行数:103,代码来源:tag_handler.py


注:本文中的display.Display.print_checkout_info方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。