@@ -111,30 +111,10 @@ export class ArduinoCloudClient implements IArduinoCloudClient {
111
111
}
112
112
}
113
113
114
- public subscribe < T extends CloudMessageValue > ( topic : string , cb : OnMessageCallback < T > ) : Promise < void > {
115
- return new Promise ( ( resolve , reject ) => {
116
- try {
117
- this . callbacks [ topic ] = this . callbacks [ topic ] = [ ] ;
118
- this . callbacks [ topic ] . push ( cb ) ;
119
-
120
- this . subscriptions [ topic ] = this . subscriptions [ topic ] = [ ] ;
121
- this . subscriptions [ topic ] . push (
122
- this . messagesFrom ( topic )
123
- . subscribe ( v => cb ( v . value as T ) ) ) ;
114
+ public getToken ( ) : string {
115
+ if ( ! this . connection ) throw new Error ( 'send message failed: no connection found' ) ;
124
116
125
- return resolve ( ) ;
126
- } catch ( err ) {
127
- reject ( err ) ;
128
- }
129
- } ) ;
130
- }
131
-
132
- public unsubscribe ( topic : string ) : Promise < void > {
133
- return new Promise ( ( resolve , reject ) => {
134
- if ( ! this . connection ) return reject ( new Error ( 'unsubscribe failed: no connection found' ) ) ;
135
-
136
- return this . connection . unsubscribe ( topic , null , ( err ) => err ? reject ( ) : resolve ( ) ) ;
137
- } ) ;
117
+ return this . connection . token ;
138
118
}
139
119
140
120
public sendMessage ( topic : string , message : string | ArrayBuffer ) : Promise < void > {
@@ -162,7 +142,7 @@ export class ArduinoCloudClient implements IArduinoCloudClient {
162
142
public async sendProperty < T extends CloudMessageValue > ( thingId : string , name : string , value : T , timestamp : number = new Date ( ) . getTime ( ) ) : Promise < void > {
163
143
const topic = `/a/t/${ thingId } /e/i` ;
164
144
const values = SenML . parse ( name , value , timestamp , this . options . useCloudProtocolV2 , null ) ;
165
- return this . sendMessage ( topic , SenML . CBOR . encode ( Utils . isArray ( values ) ? values : [ values ] , true ) ) ;
145
+ return this . sendMessage ( topic , SenML . CBOR . encode ( Utils . isArray ( values ) ? values : [ values ] , this . options . useCloudProtocolV2 ) ) ;
166
146
}
167
147
168
148
public async onPropertyValue < T extends CloudMessageValue > ( thingId : string , name : string , cb : OnMessageCallback < T > ) : Promise < void > {
@@ -181,6 +161,32 @@ export class ArduinoCloudClient implements IArduinoCloudClient {
181
161
. subscribe ( v => cb ( v . value as T ) ) ) ;
182
162
}
183
163
164
+ private subscribe < T extends CloudMessageValue > ( topic : string , cb : OnMessageCallback < T > ) : Promise < void > {
165
+ return new Promise ( ( resolve , reject ) => {
166
+ try {
167
+ this . callbacks [ topic ] = this . callbacks [ topic ] = [ ] ;
168
+ this . callbacks [ topic ] . push ( cb ) ;
169
+
170
+ this . subscriptions [ topic ] = this . subscriptions [ topic ] = [ ] ;
171
+ this . subscriptions [ topic ] . push (
172
+ this . messagesFrom ( topic )
173
+ . subscribe ( v => cb ( v . value as T ) ) ) ;
174
+
175
+ return resolve ( ) ;
176
+ } catch ( err ) {
177
+ reject ( err ) ;
178
+ }
179
+ } ) ;
180
+ }
181
+
182
+ private unsubscribe ( topic : string ) : Promise < void > {
183
+ return new Promise ( ( resolve , reject ) => {
184
+ if ( ! this . connection ) return reject ( new Error ( 'unsubscribe failed: no connection found' ) ) ;
185
+
186
+ return this . connection . unsubscribe ( topic , null , ( err ) => err ? reject ( ) : resolve ( ) ) ;
187
+ } ) ;
188
+ }
189
+
184
190
private messagesFrom ( topic : string ) : Observable < CloudMessage > {
185
191
if ( ! this . connection ) throw new Error ( 'subscription failed: no connection found' ) ;
186
192
0 commit comments