一位开发者正在尝试修改 “mirror-quick-start” Google Glass Mirror API 示例,以响应用户“回复”操作。他能够使用该示例显示一个可操作的卡片,其中内置“回复”操作。他希望用户能够通过读取科学仪器的数据来回复,然后他能够绘制数据并返回一个卡片给用户。然而,他遇到了一个问题:如何获取用户“回复”的值。
2、解决方案
1. 订阅时间线
def _insert_a600_subscription(self):
"""Attempt to register to a600 updates"""
subscription = {
"collection" : 'timeline',
"userToken" : self.userid,
"callbackUrl":"https://myapp_on_appengine.appspot.com/logA600",
}
try:
self.mirror_service.subscriptions().insert(body=subscription).execute()
logging.info("SUBSCRIBED")
except errors.HttpError, error:
print 'An error occurred: %s' % e
2. 插入带操作的项目
def _insert_item_with_action(self):
"""Insert a timeline item user can reply to."""
logging.info('Inserting timeline item')
body = {
'creator': {
'displayName': 'Python Starter Project',
'id': 'PYTHON_STARTER_PROJECT'
},
'text': 'A600 at current time:',
'id':'a600val',
'notification': {'level': 'DEFAULT'},
'menuItems': [{'action': 'REPLY',
}],
}
# self.mirror_service is initialized in util.auth_required.
self.mirror_service.timeline().insert(body=body).execute()
return 'A timeline item with action has been inserted.'
3. 创建用于处理 callbackURL 端点的请求处理程序
class A600Handler(webapp2.RequestHandler):
@util.auth_required
def post(self):
"""Process the value of A600 received and return a plot"""
logging.info("Received POST to logA600")
@util.auth_required
def get(self):
"""Process the value of A600 received and return a plot"""
logging.info("Received GET to this logA600")
MAIN_ROUTES = [
('/', MainHandler),('/logA600',A600Handler),
]
4. 从 callbackURL 端点接收通知
当用户对时间线卡片进行回复时,Google Glass 将向 callbackURL 端点发送一个 POST 请求。该请求将包含有关回复的信息,例如回复的文本和其他相关数据。
5. 处理收到的通知
收到通知后,开发者就可以处理回复的值,并根据需要采取相应的操作。例如,他可以将回复的值保存到数据库,或者将其可视化并以卡片的形式返回给用户。
6. 移出 @util.auth_required 装饰器
class A600Handler(webapp2.RequestHandler):
def post(self):
"""Process the value of A600 received and return a plot"""
logging.info("Received POST to logA600")
def get(self):
"""Process the value of A600 received and return a plot"""
logging.info("Received GET to this logA600")
MAIN_ROUTES = [
('/', MainHandler),('/logA600',A600Handler),
]
7. 测试
通过以上步骤,开发者就可以收到来自 Google Glass 的“回复”通知,并根据需要进行处理。为了测试,他可以将卡片发送到自己的 Glass 设备,进行回复,并检查是否能够在 callbackURL 端点收到通知。