1
1
#!/usr/bin/env python
2
+
2
3
# Copyright (C) 2017 Google Inc.
3
4
#
4
5
# Licensed under the Apache License, Version 2.0 (the "License");
12
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
14
# See the License for the specific language governing permissions and
14
15
# limitations under the License.
16
+
17
+
15
18
from __future__ import print_function
19
+
16
20
import argparse
17
21
import os .path
18
22
import json
23
+
19
24
import google .auth .transport .requests
20
25
import google .oauth2 .credentials
26
+
21
27
from google .assistant .library import Assistant
22
28
from google .assistant .library .event import EventType
23
29
from google .assistant .library .file_helpers import existing_file
30
+
31
+
24
32
DEVICE_API_URL = 'https://embeddedassistant.googleapis.com/v1alpha2'
25
33
26
34
@@ -41,14 +49,18 @@ def process_device_actions(event, device_id):
41
49
42
50
def process_event (event , device_id ):
43
51
"""Pretty prints events.
52
+
44
53
Prints all events that occur with two spaces between each new
45
54
conversation and a single space between turns of a conversation.
55
+
46
56
Args:
47
57
event(event.Event): The current event to process.
48
58
"""
49
59
if event .type == EventType .ON_CONVERSATION_TURN_STARTED :
50
60
print ()
61
+
51
62
print (event )
63
+
52
64
if (event .type == EventType .ON_CONVERSATION_TURN_FINISHED and
53
65
event .args and not event .args ['with_follow_on_turn' ]):
54
66
print ()
@@ -59,8 +71,10 @@ def process_event(event, device_id):
59
71
60
72
def register_device (project_id , credentials , device_model_id , device_id ):
61
73
"""Register the device if needed.
74
+
62
75
Registers a new assistant device if an instance with the given id
63
76
does not already exists for this model.
77
+
64
78
Args:
65
79
project_id(str): The project ID used to register device instance.
66
80
credentials(google.oauth2.credentials.Credentials): The Google
@@ -79,6 +93,7 @@ def register_device(project_id, credentials, device_model_id, device_id):
79
93
r = session .post (base_url , data = json .dumps ({
80
94
'id' : device_id ,
81
95
'model_id' : device_model_id ,
96
+ 'client_type' : 'SDK_LIBRARY'
82
97
}))
83
98
if r .status_code != 200 :
84
99
raise Exception ('failed to register device: ' + r .text )
@@ -98,22 +113,27 @@ def main():
98
113
help = 'Path to store and read OAuth2 credentials' )
99
114
parser .add_argument ('--device_model_id' , type = str ,
100
115
metavar = 'DEVICE_MODEL_ID' , required = True ,
101
- help = 'The device model ID registered with Google. ' )
116
+ help = 'The device model ID registered with Google' )
102
117
parser .add_argument ('--project_id' , type = str ,
103
118
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
+
106
122
args = parser .parse_args ()
107
123
with open (args .credentials , 'r' ) as f :
108
124
credentials = google .oauth2 .credentials .Credentials (token = None ,
109
125
** json .load (f ))
126
+
110
127
with Assistant (credentials , args .device_model_id ) as assistant :
111
128
events = assistant .start ()
129
+
112
130
print ('device_model_id:' , args .device_model_id + '\n ' +
113
131
'device_id:' , assistant .device_id + '\n ' )
132
+
114
133
if args .project_id :
115
134
register_device (args .project_id , credentials ,
116
135
args .device_model_id , assistant .device_id )
136
+
117
137
for event in events :
118
138
process_event (event , assistant .device_id )
119
139
0 commit comments