From a4e2bee08c5ef135d15b56863613f1244dca6bc3 Mon Sep 17 00:00:00 2001 From: pirropirro Date: Thu, 27 Feb 2025 12:44:43 +0100 Subject: [PATCH 1/4] chore: add onConnect/onDisconnect hook inside baseCloudClient --- src/builder/IArduinoIoTCloudFactory.ts | 2 +- src/client/BaseCloudClient.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/builder/IArduinoIoTCloudFactory.ts b/src/builder/IArduinoIoTCloudFactory.ts index 035cc67..8095162 100644 --- a/src/builder/IArduinoIoTCloudFactory.ts +++ b/src/builder/IArduinoIoTCloudFactory.ts @@ -8,6 +8,6 @@ export type CloudFactoryOptions = (APIOptions | CredentialsOptions | BrowserOpti export interface IArduinoIoTCloudFactory { connect(options: APIOptions & Partial): Promise; - connect(options: CredentialsOptions & Partial): Promise; connect(options: BrowserOptions & Partial): Promise; + connect(options: CredentialsOptions & Partial): Promise; } diff --git a/src/client/BaseCloudClient.ts b/src/client/BaseCloudClient.ts index 0a9cff7..0789c9d 100644 --- a/src/client/BaseCloudClient.ts +++ b/src/client/BaseCloudClient.ts @@ -63,12 +63,14 @@ export class BaseCloudClient implements ICl if (err) throw new Error(`subscription failed: ${err.toString()}`); subscription = this.connection.messages.pipe(filter((v) => v.topic === topic)).subscribe((v) => subject.next(v)); + this.options.onConnected(); }); const originalMethod = subject.unsubscribe; subject.unsubscribe = () => { subscription.unsubscribe(); originalMethod(); + this.options.onDisconnect(); }; return subject; From 800824c2900458ae5c53d6ba7cda713587efc096 Mon Sep 17 00:00:00 2001 From: pirropirro Date: Thu, 27 Feb 2025 12:44:53 +0100 Subject: [PATCH 2/4] chore: remove useless log --- src/client/SinglePropertyCloudClient.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/client/SinglePropertyCloudClient.ts b/src/client/SinglePropertyCloudClient.ts index f76aa4a..acdc308 100644 --- a/src/client/SinglePropertyCloudClient.ts +++ b/src/client/SinglePropertyCloudClient.ts @@ -31,7 +31,6 @@ export class SinglePropertyCloudClient extends BaseCloudClient implements ISingl return; } - console.log('found association to thing:', thingId); this.thingId = thingId; if (this.onThingResponse) this.onThingResponse(thingId); this.subscription = this.observe(`/a/t/${this.thingId}/e/i`).subscribe((v) => { From 3486ccdf39ac5e0a65c2489af8c74a6167d5f5ce Mon Sep 17 00:00:00 2001 From: pirropirro Date: Thu, 27 Feb 2025 12:45:10 +0100 Subject: [PATCH 3/4] bugfix: fix boolean minor bug --- src/senML/index.ts | 6 +++--- src/utils/index.ts | 14 +++++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/senML/index.ts b/src/senML/index.ts index f668a4f..296127b 100644 --- a/src/senML/index.ts +++ b/src/senML/index.ts @@ -102,9 +102,9 @@ export function format(value: CloudMessageValue, name: string, timestamp: number parsed.bn = `urn:uuid:${deviceId}`; } - if (Utils.isNumber(value)) parsed.v = value; - if (Utils.isString(value)) parsed.vs = value; - if (Utils.isBoolean(value)) parsed.vb = value; + if (Utils.isNumber(value)) parsed.v = Number(value); + if (Utils.isString(value)) parsed.vs = String(value); + if (Utils.isBoolean(value)) parsed.vb = Boolean(value); return parsed; } diff --git a/src/utils/index.ts b/src/utils/index.ts index aaf9e10..fc092e4 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -11,24 +11,28 @@ export class ArduinoCloudError extends Error { } } +export function isNil(value: unknown): value is undefined | null { + return value === undefined || value == null; +} + export function isObject(value: CloudMessageValue): value is object { - return value && typeof value === 'object'; + return !isNil(value) && typeof value === 'object'; } export function isNumber(value: CloudMessageValue): value is number { - return value && typeof value === 'number'; + return !isNil(value) && typeof value === 'number'; } export function isString(value: CloudMessageValue): value is string { - return value && typeof value === 'string'; + return !isNil(value) && typeof value === 'string'; } export function isBoolean(value: CloudMessageValue): value is boolean { - return value && typeof value === 'boolean'; + return !isNil(value) && typeof value === 'boolean'; } export function isArray(value: CloudMessageValue): value is T[] { - return value && Array.isArray(value); + return !isNil(value) && Array.isArray(value); } export function isNotAnEmptyObject(value: any): boolean { From 0bdb7dd2de49fadb4b91c389d9e60634dcbade2a Mon Sep 17 00:00:00 2001 From: pirropirro Date: Thu, 27 Feb 2025 12:45:38 +0100 Subject: [PATCH 4/4] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 94ba206..2ce8d7d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "arduino-iot-js", - "version": "0.13.0", + "version": "0.14.0", "license": "GPLv3", "description": "JS module providing Arduino Create IoT Cloud Connection", "main": "./lib/index.js",