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", 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; 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) => { 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 {