Skip to content

Commit 3cd91d8

Browse files
committed
Squashed commit of the following:
commit d3085bb36b8b73abd9999ebb654fa5abd9b82cf4 Author: pirropirro <[email protected]> Date: Wed Jun 7 16:12:40 2023 +0200 Bump version commit 18c7baa39174142b79aa084bcc78d44a124723f7 Author: pirropirro <[email protected]> Date: Wed Jun 7 16:11:59 2023 +0200 Add waiting to thing in device connection handler
1 parent 291d51c commit 3cd91d8

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "arduino-iot-js",
3-
"version": "0.10.0",
3+
"version": "0.11.0",
44
"license": "GPLv3",
55
"description": "JS module providing Arduino Create IoT Cloud Connection",
66
"main": "./lib/index.js",

src/builder/CredentialsClientBuilder.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export class CredentialsClientBuilder implements ICloudClientBuilder {
1616

1717
public async build(options: CredentialsOptions & CloudOptions): Promise<ISinglePropertyCloudClient> {
1818
const connection = await this.connection(options);
19-
return new SinglePropertyCloudClient(connection, options, `/a/d/${options.deviceId}/e/i`);
19+
const client = new SinglePropertyCloudClient(connection, options, `/a/d/${options.deviceId}/e/i`);
20+
return client.getThing().then(() => client);
2021
}
2122

2223
private async connection(options: CredentialsOptions & CloudOptions): Promise<IConnection> {

src/client/SinglePropertyCloudClient.ts

+20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export class SinglePropertyCloudClient extends BaseCloudClient implements ISingl
1010
private subscription: Subscription;
1111
private propertiesCbs: { [key: string]: OnMessageCallback<any>[] } = {};
1212

13+
private onThingRejection: (reason: any) => void;
14+
private onThingResponse: (value: string | PromiseLike<string>) => void;
15+
1316
constructor(connection: IConnection, options: CloudOptions, private deviceTopic: string) {
1417
super(connection, options);
1518
this.observe(this.deviceTopic).subscribe(({ value }) => this.onThingIdReceived(value as string));
@@ -23,7 +26,14 @@ export class SinglePropertyCloudClient extends BaseCloudClient implements ISingl
2326
}
2427

2528
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);
2635
this.thingId = thingId;
36+
if (this.onThingResponse) this.onThingResponse(thingId);
2737
this.subscription = this.observe(`/a/t/${this.thingId}/e/i`).subscribe((v) => {
2838
(this.propertiesCbs[v.propertyName] || []).forEach((cb) => cb(v.value));
2939
});
@@ -42,4 +52,14 @@ export class SinglePropertyCloudClient extends BaseCloudClient implements ISingl
4252
this.propertiesCbs[name] = this.propertiesCbs[name] || [];
4353
this.propertiesCbs[name].push(cb);
4454
}
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+
}
4565
}

0 commit comments

Comments
 (0)