17
17
import concurrent .futures
18
18
import json
19
19
import logging
20
+ import os
20
21
import os .path
22
+ import uuid
21
23
22
24
import click
23
25
import grpc
@@ -215,12 +217,26 @@ def gen_converse_requests(self):
215
217
default = os .path .join (click .get_app_dir ('google-oauthlib-tool' ),
216
218
'credentials.json' ),
217
219
help = 'Path to read OAuth2 credentials.' )
218
- @click .option ('--device-model-id' , required = True ,
220
+ @click .option ('--project' ,
221
+ metavar = '<project>' ,
222
+ help = ('Google Developer Project ID used for registration '
223
+ 'if --device-id is not specified' ))
224
+ @click .option ('--device-model-id' ,
219
225
metavar = '<device model id>' ,
220
- help = 'Unique device model identifier.' )
221
- @click .option ('--device-id' , required = True ,
226
+ help = (('Unique device model identifier, '
227
+ 'if not specifed, it is read from --device-config' )))
228
+ @click .option ('--device-id' ,
222
229
metavar = '<device id>' ,
223
- help = 'Unique registered device instance identifier.' )
230
+ help = (('Unique registered device instance identifier, '
231
+ 'if not specified, it is read from --device-config, '
232
+ 'if no device_config found: a new device is registered '
233
+ 'using a unique id and a new device config is saved' )))
234
+ @click .option ('--device-config' , show_default = True ,
235
+ metavar = '<device config>' ,
236
+ default = os .path .join (
237
+ click .get_app_dir ('googlesamples-assistant' ),
238
+ 'device_config.json' ),
239
+ help = 'Path to save and restore the device configuration' )
224
240
@click .option ('--verbose' , '-v' , is_flag = True , default = False ,
225
241
help = 'Verbose logging.' )
226
242
@click .option ('--input-audio-file' , '-i' ,
@@ -247,7 +263,7 @@ def gen_converse_requests(self):
247
263
default = audio_helpers .DEFAULT_AUDIO_DEVICE_BLOCK_SIZE ,
248
264
metavar = '<audio block size>' , show_default = True ,
249
265
help = ('Block size in bytes for each audio device '
250
- 'read and write operation.. ' ))
266
+ 'read and write operation.' ))
251
267
@click .option ('--audio-flush-size' ,
252
268
default = audio_helpers .DEFAULT_AUDIO_DEVICE_FLUSH_SIZE ,
253
269
metavar = '<audio flush size>' , show_default = True ,
@@ -258,7 +274,8 @@ def gen_converse_requests(self):
258
274
help = 'gRPC deadline in seconds' )
259
275
@click .option ('--once' , default = False , is_flag = True ,
260
276
help = 'Force termination after a single conversation.' )
261
- def main (api_endpoint , credentials , device_model_id , device_id , verbose ,
277
+ def main (api_endpoint , credentials , project ,
278
+ device_model_id , device_id , device_config , verbose ,
262
279
input_audio_file , output_audio_file ,
263
280
audio_sample_rate , audio_sample_width ,
264
281
audio_iter_size , audio_block_size , audio_flush_size ,
@@ -348,6 +365,39 @@ def onoff(on):
348
365
else :
349
366
logging .info ('Turning device off' )
350
367
368
+ if not device_id or not device_model_id :
369
+ try :
370
+ with open (device_config ) as f :
371
+ device = json .load (f )
372
+ device_id = device ['id' ]
373
+ device_model_id = device ['model_id' ]
374
+ except Exception as e :
375
+ logging .error ('Error loading device config: %s' % e )
376
+ logging .info ('Registering device' )
377
+ if not device_model_id :
378
+ logging .error ('--device-model-id parameter required '
379
+ 'when registrering a model.' )
380
+ return
381
+ device_base_url = (
382
+ 'https://%s/v1alpha2/projects/%s/devices' % (api_endpoint ,
383
+ project )
384
+ )
385
+ device_id = str (uuid .uuid1 ())
386
+ payload = {
387
+ 'id' : device_id ,
388
+ 'model_id' : device_model_id
389
+ }
390
+ session = google .auth .transport .requests .AuthorizedSession (
391
+ credentials
392
+ )
393
+ r = session .post (device_base_url , data = json .dumps (payload ))
394
+ if r .status_code != 200 :
395
+ logging .error ('Failed to register device: %s' , r .text )
396
+ return
397
+ os .makedirs (os .path .dirname (device_config ), exist_ok = True )
398
+ with open (device_config , 'w' ) as f :
399
+ json .dump (payload , f )
400
+
351
401
with SampleAssistant (device_model_id , device_id , conversation_stream ,
352
402
grpc_channel , grpc_deadline ,
353
403
device_handler ) as assistant :
0 commit comments