Skip to content

Commit 79fe50c

Browse files
committedJun 18, 2020
Fix minor bugs
1 parent df79564 commit 79fe50c

File tree

5 files changed

+45
-33
lines changed

5 files changed

+45
-33
lines changed
 

‎src/client/ArduinoCloudClient.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,30 +111,10 @@ export class ArduinoCloudClient implements IArduinoCloudClient {
111111
}
112112
}
113113

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');
124116

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;
138118
}
139119

140120
public sendMessage(topic: string, message: string | ArrayBuffer): Promise<void> {
@@ -162,7 +142,7 @@ export class ArduinoCloudClient implements IArduinoCloudClient {
162142
public async sendProperty<T extends CloudMessageValue>(thingId: string, name: string, value: T, timestamp: number = new Date().getTime()): Promise<void> {
163143
const topic = `/a/t/${thingId}/e/i`;
164144
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));
166146
}
167147

168148
public async onPropertyValue<T extends CloudMessageValue>(thingId: string, name: string, cb: OnMessageCallback<T>): Promise<void> {
@@ -181,6 +161,32 @@ export class ArduinoCloudClient implements IArduinoCloudClient {
181161
.subscribe(v => cb(v.value as T)));
182162
}
183163

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+
184190
private messagesFrom(topic: string): Observable<CloudMessage> {
185191
if (!this.connection) throw new Error('subscription failed: no connection found');
186192

‎src/client/IArduinoCloudClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ export interface IArduinoCloudClient {
3434
connect(options: CloudOptions): Promise<IConnection>;
3535
reconnect(): Promise<void>;
3636
disconnect(): Promise<void>;
37+
3738
updateToken(newToken: string): Promise<void>;
39+
getToken(): string;
3840

39-
subscribe<T extends CloudMessageValue>(topic: string, cb: OnMessageCallback<T>): Promise<void>;
40-
unsubscribe(topic: string): Promise<void>;
4141
sendMessage(topic: string, message: string): Promise<void>;
4242
sendMessage(topic: string, message: ArrayBuffer): Promise<void>;
4343

‎src/connection/Connection.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const BaseConnectionOptions: Partial<ConnectionOptions> = {
1616
}
1717

1818
export class Connection implements IConnection {
19-
public messages?: Observable<CloudMessage>;
19+
public token: string;
20+
public messages: Observable<CloudMessage>;
21+
2022
private _client: mqtt.MqttClient;
21-
2223
private get client(): mqtt.MqttClient {
2324
return this._client;
2425
}
@@ -46,6 +47,7 @@ export class Connection implements IConnection {
4647

4748
const connection = new Connection();
4849
connection.client = mqtt.connect(`wss://${host}:${port}/mqtt`, { ...BaseConnectionOptions, ...options });
50+
connection.token = token;
4951
return connection
5052
}
5153

‎src/connection/IConnection.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11

22
import { Observable } from "rxjs";
3-
import { MqttClient, IClientOptions, OnConnectCallback, OnMessageCallback, OnPacketCallback, OnErrorCallback, CloseCallback, IClientReconnectOptions, PacketCallback, IClientPublishOptions, IClientSubscribeOptions, ClientSubscribeCallback } from 'mqtt';
3+
import {
4+
IClientOptions, OnConnectCallback, OnMessageCallback, OnPacketCallback,
5+
OnErrorCallback, CloseCallback, IClientReconnectOptions, PacketCallback, IClientPublishOptions
6+
} from 'mqtt';
47

58
import { CloudMessageValue } from "../client/IArduinoCloudClient";
69

710
export type ConnectionOptions = IClientOptions;
811
export type CloudMessage = { topic: string; propertyName?: string; value: CloudMessageValue };
912

1013
export interface IConnection {
14+
token?: string;
15+
messages?: Observable<CloudMessage>;
16+
1117
on(event: "connect", cb: OnConnectCallback): IConnection;
1218
on(event: "message", cb: OnMessageCallback): IConnection;
1319
on(event: "packetsend" | "packetreceive", cb: OnPacketCallback): IConnection;
@@ -26,7 +32,5 @@ export interface IConnection {
2632
publish(topic: any, message: any, opts?: any, callback?: any): IConnection;
2733

2834
subscribe(topic: any, callback?: any): IConnection;
29-
30-
messages?: Observable<CloudMessage>
3135
}
3236

‎src/senML/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ function nameFrom(property: SenML | string[]): string {
1717
return isPropertyValue(property) ? property.n : property[0]
1818
}
1919

20-
function toString(value: SenML[]): string {
21-
const encoded = CBOR.encode(value);
20+
function toString(value: SenML[], numericKeys?: boolean): string {
21+
const encoded = CBOR.encode(value, numericKeys);
2222
return Utils.arrayBufferToBase64(encoded);
2323
};
2424

0 commit comments

Comments
 (0)
Please sign in to comment.