19
19
import logging
20
20
import os
21
21
import os .path
22
+ import sys
22
23
import uuid
23
24
24
25
import click
@@ -307,7 +308,7 @@ def main(api_endpoint, credentials, project,
307
308
logging .error ('Error loading credentials: %s' , e )
308
309
logging .error ('Run google-oauthlib-tool to initialize '
309
310
'new OAuth 2.0 credentials.' )
310
- return
311
+ sys . exit ( - 1 )
311
312
312
313
# Create an authorized gRPC channel.
313
314
grpc_channel = google .auth .transport .grpc .secure_authorized_channel (
@@ -370,12 +371,16 @@ def onoff(on):
370
371
device_id = device ['id' ]
371
372
device_model_id = device ['model_id' ]
372
373
except Exception as e :
373
- logging .error ( 'Error loading device config : %s' % e )
374
+ logging .warning ( 'Device config not found : %s' % e )
374
375
logging .info ('Registering device' )
375
376
if not device_model_id :
376
- logging .error ('--device-model-id parameter required '
377
+ logging .error ('Option --device-model-id required '
377
378
'when registrering a model.' )
378
- return
379
+ sys .exit (- 1 )
380
+ if not project :
381
+ logging .error ('Option --project required '
382
+ 'when registrering a model.' )
383
+ sys .exit (- 1 )
379
384
device_base_url = (
380
385
'https://%s/v1alpha2/projects/%s/devices' % (api_endpoint ,
381
386
project )
@@ -391,7 +396,8 @@ def onoff(on):
391
396
r = session .post (device_base_url , data = json .dumps (payload ))
392
397
if r .status_code != 200 :
393
398
logging .error ('Failed to register device: %s' , r .text )
394
- return
399
+ sys .exit (- 1 )
400
+ logging .info ('Device registered: %s' , device_id )
395
401
os .makedirs (os .path .dirname (device_config ), exist_ok = True )
396
402
with open (device_config , 'w' ) as f :
397
403
json .dump (payload , f )
0 commit comments