From 5db531815a0578a045ec93015bd9a46d71d934d0 Mon Sep 17 00:00:00 2001 From: Patineboot Date: Wed, 29 Jun 2022 08:28:55 +0900 Subject: [PATCH] [Improve] A user specifies the wrong service name of the value of the 'service' element. --- README.md | 2 +- package-lock.json | 4 +-- package.json | 2 +- src/RunnableAccessory.js | 24 ++++++++------ src/RunnablePlatform.js | 68 +++++++++++++++++++++++++--------------- 5 files changed, 62 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 59ff110..2b83502 100644 --- a/README.md +++ b/README.md @@ -264,7 +264,7 @@ I am tired of updating my command to only fit his CMD4 anymore. 1. Update 1. Update the value of 'version' on `package.json`. - 1. Update the value of 'version' on `package-lock.json`. + 1. Update the `package-lock.json`. ```bash rm -rf node_modules rm package-lock.json diff --git a/package-lock.json b/package-lock.json index 753cb58..9657cd1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "homebridge-runnable-platform", - "version": "0.9.6", + "version": "0.9.7", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "homebridge-runnable-platform", - "version": "0.9.6", + "version": "0.9.7", "license": "CC BY-NC-ND 4.0", "devDependencies": { "homebridge": "^1.4.1" diff --git a/package.json b/package.json index bced309..bdbdc83 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "private": false, "displayName": "Homebridge RunnablePlatform", "name": "homebridge-runnable-platform", - "version": "0.9.6", + "version": "0.9.7", "description": "RunnablePlatform provide connections using your Custom-Command command between infrared devices and Homebridge.", "license": "CC BY-NC-ND 4.0", "repository": { diff --git a/src/RunnableAccessory.js b/src/RunnableAccessory.js index 27fdf23..ebd5c68 100644 --- a/src/RunnableAccessory.js +++ b/src/RunnableAccessory.js @@ -52,17 +52,23 @@ export class RunnableAccessory { constructor(accessoryConfig) { LOG.debug(`Enter ${this.constructor.name}: ${accessoryConfig} -> name: ${accessoryConfig.name}`); - this.#lock = new Lock(); + try { + this.#lock = new Lock(); - this.#accessoryConfig = accessoryConfig; + this.#accessoryConfig = accessoryConfig; - const comm = Communicator.getInstance(); - comm.on('message', (message) => { - if (message.name !== this.name) { - return; - } - this.updateCharacteristic(message.characteristic, message.value); - }); + const comm = Communicator.getInstance(); + comm.on('message', (message) => { + if (message.name !== this.name) { + return; + } + this.updateCharacteristic(message.characteristic, message.value); + }); + } + catch (error) { + LOG.info(`${this.constructor.name}: ${accessoryConfig} -> name: ${accessoryConfig.name}`); + throw error; + } } /** diff --git a/src/RunnablePlatform.js b/src/RunnablePlatform.js index 0800453..ea95091 100644 --- a/src/RunnablePlatform.js +++ b/src/RunnablePlatform.js @@ -87,23 +87,36 @@ export class RunnablePlatform { * registering any new accessories. */ HAPI.on('didFinishLaunching', () => { - if (this.#skeletonMode) { - LOG.warn('RunnablePlatform Plugin is running on the Skelton mode.'); + try { + if (this.#skeletonMode) { + LOG.warn('RunnablePlatform Plugin is running on the Skelton mode.'); + return; + } + this.#didFinishLaunching(); + } + catch (error) { + LOG.error('Unexpected error occurs.', error); return; } - this.#didFinishLaunching(); }); /** * This event is fired when homebridge got shutdown. */ HAPI.on('shutdown', () => { - if (this.#skeletonMode) { - LOG.warn('RunnablePlatform Plugin is running on the Skelton mode.'); + try { + if (this.#skeletonMode) { + LOG.warn('RunnablePlatform Plugin is running on the Skelton mode.'); + return; + } + const comm = Communicator.getInstance(); + comm.disconnect(); + LOG.info('Bye.'); + } + catch (error) { + LOG.error('Unexpected error occurs.', error); return; } - const comm = Communicator.getInstance(); - comm.disconnect(); }); // verify the my 'platform' element on the config.json file. @@ -124,24 +137,30 @@ export class RunnablePlatform { * @param {import("homebridge").PlatformAccessory} accessory */ configureAccessory(accessory) { - if (this.#skeletonMode) { - LOG.warn('RunnablePlatform Plugin is running on the Skelton mode.'); - return; - } + try { + if (this.#skeletonMode) { + LOG.warn('RunnablePlatform Plugin is running on the Skelton mode.'); + return; + } - // check the restored accessory is not described on the config file - let valid = ''; - const found = this.#findConfigAccessory(accessory.UUID); - if (found) { - this.#accessories.push(accessory); - valid = 'restored'; + // check the restored accessory is not described on the config file + let valid = ''; + const found = this.#findConfigAccessory(accessory.UUID); + if (found) { + this.#accessories.push(accessory); + valid = 'restored'; + } + else { + // remove the accessory from cache later. + this.#invalidAccessories.push(accessory); + valid = 'disposed'; + } + LOG.info(`Saved accessory: ${accessory.displayName} is ${valid}`); } - else { - // remove the accessory from cache later. - this.#invalidAccessories.push(accessory); - valid = 'VOID'; + catch (error) { + LOG.error('Unexpected error occurs.', error); + return; } - LOG.info(`Configure Accessory ${valid}: ${accessory.displayName}`); } /** @@ -224,8 +243,6 @@ export class RunnablePlatform { runnable.startService(accessory); // update the new platform accessory HAPI.updatePlatformAccessories([accessory]); - - LOG.info(`Cached Accessory name:[${runnable.name}] type:[${runnable.serviceType}]`); } // otherwise, we create a new platform accessory else { @@ -236,8 +253,9 @@ export class RunnablePlatform { // register the new platform accessory HAPI.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]); - LOG.info(`New Accessory name:[${runnable.name}] type:[${runnable.serviceType}]`); + LOG.info(`New accessory is added: name:[${runnable.name}] service:[${runnable.serviceType}]`); } + LOG.info(`accessory started. name:[${runnable.name}] service:[${runnable.serviceType}]`); } } }