@@ -5,7 +5,7 @@ import CBOR from "../cbor";
5
5
import Utils from "../utils" ;
6
6
import { IConnectionBuilder } from '../builder/IConnectionBuilder' ;
7
7
import { IConnection , CloudMessage } from "../connection/IConnection" ;
8
- import { IArduinoCloudClient , CloudOptions , OnMessageCallback , CloudMessageValue , isBrowserOptions } from "./IArduinoCloudClient" ;
8
+ import { IArduinoCloudClient , CloudOptions , OnMessageCallback , CloudMessageValue } from "./IArduinoCloudClient" ;
9
9
10
10
const NOOP = ( ) => null ;
11
11
export class ArduinoCloudClient implements IArduinoCloudClient {
@@ -137,11 +137,12 @@ export class ArduinoCloudClient implements IArduinoCloudClient {
137
137
} ) ;
138
138
}
139
139
140
- public sendMessage ( topic : string , message : ArrayBuffer ) : Promise < void > {
140
+ public sendMessage ( topic : string , message : string | ArrayBuffer ) : Promise < void > {
141
141
return new Promise ( ( resolve , reject ) => {
142
142
if ( ! this . connection ) return reject ( new Error ( 'send message failed: no connection found' ) ) ;
143
143
144
- this . connection . publish ( topic , Utils . toBuffer ( message ) , { qos : 1 , retain : false } ) ;
144
+ const body = Utils . isString ( message ) ? Buffer . from ( message , 'utf8' ) : message ;
145
+ this . connection . publish ( topic , Utils . toBuffer ( body ) , { qos : 1 , retain : false } ) ;
145
146
return resolve ( ) ;
146
147
} ) ;
147
148
}
@@ -150,15 +151,15 @@ export class ArduinoCloudClient implements IArduinoCloudClient {
150
151
return this . subscribe ( `/a/d/${ deviceId } /s/o` , cb ) ;
151
152
}
152
153
153
- public writeCloudMonitor ( deviceId : string , message : ArrayBuffer ) : Promise < void > {
154
+ public writeCloudMonitor ( deviceId : string , message : string | ArrayBuffer ) : Promise < void > {
154
155
return this . sendMessage ( `/a/d/${ deviceId } /s/i` , message ) ;
155
156
}
156
157
157
- public closeCloudMonitor < T > ( deviceId : string ) : Promise < void > {
158
+ public closeCloudMonitor ( deviceId : string ) : Promise < void > {
158
159
return this . unsubscribe ( `/a/d/${ deviceId } /s/o` ) ;
159
160
}
160
161
161
- public async sendProperty < T extends CloudMessageValue > ( thingId : string , name : string , value : T , timestamp : number ) : Promise < void > {
162
+ public async sendProperty < T extends CloudMessageValue > ( thingId : string , name : string , value : T , timestamp : number = new Date ( ) . getTime ( ) ) : Promise < void > {
162
163
const topic = `/a/t/${ thingId } /e/i` ;
163
164
const values = CBOR . getSenML ( name , value , timestamp , this . options . useCloudProtocolV2 , null ) ;
164
165
return this . sendMessage ( topic , CBOR . encode ( Utils . isArray ( values ) ? values : [ values ] , true ) ) ;
@@ -185,7 +186,6 @@ export class ArduinoCloudClient implements IArduinoCloudClient {
185
186
186
187
let subscription : Subscription ;
187
188
const subject = new Subject < CloudMessage > ( ) ;
188
-
189
189
this . connection . subscribe ( topic , ( err ) => {
190
190
if ( err ) throw new Error ( `subscription failed: ${ err . toString ( ) } ` ) ;
191
191
@@ -196,7 +196,6 @@ export class ArduinoCloudClient implements IArduinoCloudClient {
196
196
197
197
const originalMethod = subject . unsubscribe ;
198
198
subject . unsubscribe = ( ) => {
199
-
200
199
subscription . unsubscribe ( ) ;
201
200
originalMethod ( ) ;
202
201
}
0 commit comments