11#!/usr/bin/env python
2+
23# Copyright (C) 2017 Google Inc.
34#
45# Licensed under the Apache License, Version 2.0 (the "License");
1213# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1314# See the License for the specific language governing permissions and
1415# limitations under the License.
16+
17+
1518from __future__ import print_function
19+
1620import argparse
1721import os .path
1822import json
23+
1924import google .auth .transport .requests
2025import google .oauth2 .credentials
26+
2127from google .assistant .library import Assistant
2228from google .assistant .library .event import EventType
2329from google .assistant .library .file_helpers import existing_file
30+
31+
2432DEVICE_API_URL = 'https://embeddedassistant.googleapis.com/v1alpha2'
2533
2634
@@ -41,14 +49,18 @@ def process_device_actions(event, device_id):
4149
4250def process_event (event , device_id ):
4351 """Pretty prints events.
52+
4453 Prints all events that occur with two spaces between each new
4554 conversation and a single space between turns of a conversation.
55+
4656 Args:
4757 event(event.Event): The current event to process.
4858 """
4959 if event .type == EventType .ON_CONVERSATION_TURN_STARTED :
5060 print ()
61+
5162 print (event )
63+
5264 if (event .type == EventType .ON_CONVERSATION_TURN_FINISHED and
5365 event .args and not event .args ['with_follow_on_turn' ]):
5466 print ()
@@ -59,8 +71,10 @@ def process_event(event, device_id):
5971
6072def register_device (project_id , credentials , device_model_id , device_id ):
6173 """Register the device if needed.
74+
6275 Registers a new assistant device if an instance with the given id
6376 does not already exists for this model.
77+
6478 Args:
6579 project_id(str): The project ID used to register device instance.
6680 credentials(google.oauth2.credentials.Credentials): The Google
@@ -79,6 +93,7 @@ def register_device(project_id, credentials, device_model_id, device_id):
7993 r = session .post (base_url , data = json .dumps ({
8094 'id' : device_id ,
8195 'model_id' : device_model_id ,
96+ 'client_type' : 'SDK_LIBRARY'
8297 }))
8398 if r .status_code != 200 :
8499 raise Exception ('failed to register device: ' + r .text )
@@ -98,22 +113,27 @@ def main():
98113 help = 'Path to store and read OAuth2 credentials' )
99114 parser .add_argument ('--device_model_id' , type = str ,
100115 metavar = 'DEVICE_MODEL_ID' , required = True ,
101- help = 'The device model ID registered with Google. ' )
116+ help = 'The device model ID registered with Google' )
102117 parser .add_argument ('--project_id' , type = str ,
103118 metavar = 'PROJECT_ID' , required = False ,
104- help = 'The project ID used to register device '
105- + 'instances.' )
119+ help = ('The project ID used to register'
120+ 'device instances' ))
121+
106122 args = parser .parse_args ()
107123 with open (args .credentials , 'r' ) as f :
108124 credentials = google .oauth2 .credentials .Credentials (token = None ,
109125 ** json .load (f ))
126+
110127 with Assistant (credentials , args .device_model_id ) as assistant :
111128 events = assistant .start ()
129+
112130 print ('device_model_id:' , args .device_model_id + '\n ' +
113131 'device_id:' , assistant .device_id + '\n ' )
132+
114133 if args .project_id :
115134 register_device (args .project_id , credentials ,
116135 args .device_model_id , assistant .device_id )
136+
117137 for event in events :
118138 process_event (event , assistant .device_id )
119139
0 commit comments