1
1
#!/usr/bin/env python
2
-
3
2
# Copyright (C) 2017 Google Inc.
4
3
#
5
4
# Licensed under the Apache License, Version 2.0 (the "License");
13
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
13
# See the License for the specific language governing permissions and
15
14
# limitations under the License.
16
-
17
-
18
15
from __future__ import print_function
19
-
20
16
import argparse
21
17
import os .path
22
18
import json
23
-
19
+ import google . auth . transport . requests
24
20
import google .oauth2 .credentials
25
-
26
21
from google .assistant .library import Assistant
27
22
from google .assistant .library .event import EventType
28
23
from google .assistant .library .file_helpers import existing_file
24
+ DEVICE_API_URL = 'https://embeddedassistant.googleapis.com/v1alpha2'
29
25
30
26
31
- def process_event (event ):
32
- """Pretty prints events.
27
+ def process_device_actions (event , device_id ):
28
+ if 'inputs' in event .args :
29
+ for i in event .args ['inputs' ]:
30
+ if i ['intent' ] == 'action.devices.EXECUTE' :
31
+ for c in i ['payload' ]['commands' ]:
32
+ for device in c ['devices' ]:
33
+ if device ['id' ] == device_id :
34
+ if 'execution' in c :
35
+ for e in c ['execution' ]:
36
+ if e ['params' ]:
37
+ yield e ['command' ], e ['params' ]
38
+ else :
39
+ yield e ['command' ], None
33
40
41
+
42
+ def process_event (event , device_id ):
43
+ """Pretty prints events.
34
44
Prints all events that occur with two spaces between each new
35
45
conversation and a single space between turns of a conversation.
36
-
37
46
Args:
38
47
event(event.Event): The current event to process.
39
48
"""
40
49
if event .type == EventType .ON_CONVERSATION_TURN_STARTED :
41
50
print ()
42
-
43
51
print (event )
44
-
45
52
if (event .type == EventType .ON_CONVERSATION_TURN_FINISHED and
46
53
event .args and not event .args ['with_follow_on_turn' ]):
47
54
print ()
55
+ if event .type == EventType .ON_DEVICE_ACTION :
56
+ for command , params in process_device_actions (event , device_id ):
57
+ print ('Do command' , command , 'with params' , str (params ))
58
+
59
+
60
+ def register_device (project_id , credentials , device_model_id , device_id ):
61
+ """Register the device if needed.
62
+ Registers a new assistant device if an instance with the given id
63
+ does not already exists for this model.
64
+ Args:
65
+ project_id(str): The project ID used to register device instance.
66
+ credentials(google.oauth2.credentials.Credentials): The Google
67
+ OAuth2 credentials of the user to associate the device
68
+ instance with.
69
+ device_model_id: The registered device model ID.
70
+ device_id: The device ID of the new instance.
71
+ """
72
+ base_url = '/' .join ([DEVICE_API_URL , 'projects' , project_id , 'devices' ])
73
+ device_url = '/' .join ([base_url , device_id ])
74
+ session = google .auth .transport .requests .AuthorizedSession (credentials )
75
+ r = session .get (device_url )
76
+ print (device_url , r .status_code )
77
+ if r .status_code == 404 :
78
+ print ('Registering....' , end = '' , flush = True )
79
+ r = session .post (base_url , data = json .dumps ({
80
+ 'id' : device_id ,
81
+ 'model_id' : device_model_id ,
82
+ }))
83
+ if r .status_code != 200 :
84
+ raise Exception ('failed to register device: ' + r .text )
85
+ print ('\r Device registered.' )
48
86
49
87
50
88
def main ():
@@ -58,14 +96,26 @@ def main():
58
96
'credentials.json'
59
97
),
60
98
help = 'Path to store and read OAuth2 credentials' )
99
+ parser .add_argument ('--device_model_id' , type = str ,
100
+ metavar = 'DEVICE_MODEL_ID' , required = True ,
101
+ help = 'The device model ID registered with Google.' )
102
+ parser .add_argument ('--project_id' , type = str ,
103
+ metavar = 'PROJECT_ID' , required = False ,
104
+ help = 'The project ID used to register device '
105
+ + 'instances.' )
61
106
args = parser .parse_args ()
62
107
with open (args .credentials , 'r' ) as f :
63
108
credentials = google .oauth2 .credentials .Credentials (token = None ,
64
109
** json .load (f ))
65
-
66
- with Assistant (credentials ) as assistant :
67
- for event in assistant .start ():
68
- process_event (event )
110
+ with Assistant (credentials , args .device_model_id ) as assistant :
111
+ events = assistant .start ()
112
+ print ('device_model_id:' , args .device_model_id + '\n ' +
113
+ 'device_id:' , assistant .device_id + '\n ' )
114
+ if args .project_id :
115
+ register_device (args .project_id , credentials ,
116
+ args .device_model_id , assistant .device_id )
117
+ for event in events :
118
+ process_event (event , assistant .device_id )
69
119
70
120
71
121
if __name__ == '__main__' :
0 commit comments