@@ -10,6 +10,9 @@ export class SinglePropertyCloudClient extends BaseCloudClient implements ISingl
10
10
private subscription : Subscription ;
11
11
private propertiesCbs : { [ key : string ] : OnMessageCallback < any > [ ] } = { } ;
12
12
13
+ private onThingRejection : ( reason : any ) => void ;
14
+ private onThingResponse : ( value : string | PromiseLike < string > ) => void ;
15
+
13
16
constructor ( connection : IConnection , options : CloudOptions , private deviceTopic : string ) {
14
17
super ( connection , options ) ;
15
18
this . observe ( this . deviceTopic ) . subscribe ( ( { value } ) => this . onThingIdReceived ( value as string ) ) ;
@@ -23,7 +26,14 @@ export class SinglePropertyCloudClient extends BaseCloudClient implements ISingl
23
26
}
24
27
25
28
private onThingIdReceived ( thingId : string ) : void {
29
+ if ( ! thingId ) {
30
+ if ( this . onThingRejection ) this . onThingRejection ( new Error ( 'Error: no thing associated' ) ) ;
31
+ return ;
32
+ }
33
+
34
+ console . log ( 'found association to thing:' , thingId ) ;
26
35
this . thingId = thingId ;
36
+ if ( this . onThingResponse ) this . onThingResponse ( thingId ) ;
27
37
this . subscription = this . observe ( `/a/t/${ this . thingId } /e/i` ) . subscribe ( ( v ) => {
28
38
( this . propertiesCbs [ v . propertyName ] || [ ] ) . forEach ( ( cb ) => cb ( v . value ) ) ;
29
39
} ) ;
@@ -42,4 +52,14 @@ export class SinglePropertyCloudClient extends BaseCloudClient implements ISingl
42
52
this . propertiesCbs [ name ] = this . propertiesCbs [ name ] || [ ] ;
43
53
this . propertiesCbs [ name ] . push ( cb ) ;
44
54
}
55
+
56
+ public async getThing ( ) : Promise < string > {
57
+ if ( this . thingId ) return this . thingId ;
58
+
59
+ return new Promise < string > ( ( res , rej ) => {
60
+ this . onThingResponse = res ;
61
+ this . onThingRejection = rej ;
62
+ setTimeout ( ( ) => rej ( new Error ( 'Error: no thing associated' ) ) , 10000 ) ;
63
+ } ) ;
64
+ }
45
65
}
0 commit comments