From ed0c930e094c2d0ef56629df2a57c0ab3247213c Mon Sep 17 00:00:00 2001 From: Supereg Date: Sat, 18 Apr 2020 12:28:18 +0200 Subject: [PATCH] Adding warning regarding imports and updated to the latest beta for improved usage of const enums --- .../package-lock.json | 14 +++++----- accessory-example-typescript/package.json | 2 +- accessory-example-typescript/src/accessory.ts | 28 ++++++++++++++++--- .../package-lock.json | 14 +++++----- .../package.json | 2 +- .../src/dynamic-camera-platform.ts | 23 +++++++++++++-- .../src/streamingDelegate.ts | 13 +++++---- .../package-lock.json | 14 +++++----- .../package.json | 2 +- .../src/dynamic-platform.ts | 27 +++++++++++++++--- .../package-lock.json | 14 +++++----- .../package.json | 2 +- .../src/independent-platform.ts | 28 ++++++++++++++++--- .../package-lock.json | 14 +++++----- .../package.json | 2 +- .../src/static-platform.ts | 23 +++++++++++++-- .../src/switch-accessory.ts | 7 +++-- 17 files changed, 164 insertions(+), 65 deletions(-) diff --git a/accessory-example-typescript/package-lock.json b/accessory-example-typescript/package-lock.json index f576b5f..10b900c 100644 --- a/accessory-example-typescript/package-lock.json +++ b/accessory-example-typescript/package-lock.json @@ -237,9 +237,9 @@ } }, "hap-nodejs": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/hap-nodejs/-/hap-nodejs-0.6.2.tgz", - "integrity": "sha512-/LlRaYCtcwC4jBUKRJeBUdUfKEp8w/NK3PNYXaAOngQL9uVnqsvQ0Gce/mUhSXPhsxfdnxbHVT870W9OEnV6+A==", + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/hap-nodejs/-/hap-nodejs-0.6.3.tgz", + "integrity": "sha512-FH5cEBJDz1e/IzqYbGEO4PdydXrZN+vqZcvKexyRqZlP1DbPjjeboI2TbqGfftUsyZu2jz5y/NRYlS9HzEijcQ==", "dev": true, "requires": { "bonjour-hap": "3.5.4", @@ -273,14 +273,14 @@ "dev": true }, "homebridge": { - "version": "0.4.54-beta.51", - "resolved": "https://registry.npmjs.org/homebridge/-/homebridge-0.4.54-beta.51.tgz", - "integrity": "sha512-WycAihWBzWu+8ZBnVrU4wqdU0bq/cT43iKdk2+6ODhbGh/xlaxIFpMdSGX++YqwGpu7cNEL0L45Woj9sEfFYNw==", + "version": "0.4.54-beta.52", + "resolved": "https://registry.npmjs.org/homebridge/-/homebridge-0.4.54-beta.52.tgz", + "integrity": "sha512-fQKpd2khare7BTbzyEVjABWLhAeTUF1it5MrvnI/NDlzkvehPv362j8Jim1/sVyafbY+RloC0z5vFdu1nkNvoA==", "dev": true, "requires": { "chalk": "^3.0.0", "commander": "5.0.0", - "hap-nodejs": "0.6.2", + "hap-nodejs": "0.6.3", "node-persist": "^0.0.11", "qrcode-terminal": "^0.12.0", "semver": "^7.1.3", diff --git a/accessory-example-typescript/package.json b/accessory-example-typescript/package.json index 13c4536..89e0a66 100644 --- a/accessory-example-typescript/package.json +++ b/accessory-example-typescript/package.json @@ -33,6 +33,6 @@ "@types/node": "10.17.19", "typescript": "^3.8.3", "rimraf": "^3.0.2", - "homebridge": "^0.4.54-beta.51" + "homebridge": "^0.4.54-beta.52" } } diff --git a/accessory-example-typescript/src/accessory.ts b/accessory-example-typescript/src/accessory.ts index 2088d91..93dfa97 100644 --- a/accessory-example-typescript/src/accessory.ts +++ b/accessory-example-typescript/src/accessory.ts @@ -2,6 +2,7 @@ import { AccessoryConfig, AccessoryPlugin, API, + CharacteristicEventTypes, CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue, @@ -10,8 +11,27 @@ import { Service } from "homebridge"; -/** - * TODO explain +/* + * IMPORTANT NOTICE + * + * One thing you need to take care of is, that you never ever ever import anything directly from the "homebridge" module (or the "hap-nodejs" module). + * The above import block may seem like, that we do exactly that, but actually those imports are only used for types and interfaces + * and will disappear once the code is compiled to Javascript. + * In fact you can check that by running `npm run build` and opening the compiled Javascript file in the `dist` folder. + * You will notice that the file does not contain a `... = require("homebridge");` statement anywhere in the code. + * + * The contents of the above import statement MUST ONLY be used for type annotation or accessing things like CONST ENUMS, + * which is a special case as they get replaced by the actual value and do not remain as a reference in the compiled code. + * Meaning normal enums are bad, const enums can be used. + * + * You MUST NOT import anything else which remains as a reference in the code, as this will result in + * a `... = require("homebridge");` to be compiled into the final Javascript code. + * This typically leads to unexpected behavior at runtime, as in many cases it won't be able to find the module + * or will import another instance of homebridge causing collisions. + * + * To mitigate this the {@link API | Homebridge API} exposes the whole suite of HAP-NodeJS inside the `hap` property + * of the api object, which can be acquired for example in the initializer function. This reference can be stored + * like this for example and used to access all exported variables and classes from HAP-NodeJS. */ let hap: HAP; @@ -38,11 +58,11 @@ class ExampleSwitch implements AccessoryPlugin { this.switchService = new hap.Service.Switch(this.name); this.switchService.getCharacteristic(hap.Characteristic.On) - .on(hap.CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => { + .on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => { log.info("Current state of the switch was returned: " + (this.switchOn? "ON": "OFF")); callback(undefined, this.switchOn); }) - .on(hap.CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => { + .on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => { this.switchOn = value as boolean; log.info("Switch state was set to: " + (this.switchOn? "ON": "OFF")); callback(); diff --git a/bridged-camera-example-typescript/package-lock.json b/bridged-camera-example-typescript/package-lock.json index 593d8e2..a155935 100644 --- a/bridged-camera-example-typescript/package-lock.json +++ b/bridged-camera-example-typescript/package-lock.json @@ -246,9 +246,9 @@ } }, "hap-nodejs": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/hap-nodejs/-/hap-nodejs-0.6.2.tgz", - "integrity": "sha512-/LlRaYCtcwC4jBUKRJeBUdUfKEp8w/NK3PNYXaAOngQL9uVnqsvQ0Gce/mUhSXPhsxfdnxbHVT870W9OEnV6+A==", + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/hap-nodejs/-/hap-nodejs-0.6.3.tgz", + "integrity": "sha512-FH5cEBJDz1e/IzqYbGEO4PdydXrZN+vqZcvKexyRqZlP1DbPjjeboI2TbqGfftUsyZu2jz5y/NRYlS9HzEijcQ==", "dev": true, "requires": { "bonjour-hap": "3.5.4", @@ -282,14 +282,14 @@ "dev": true }, "homebridge": { - "version": "0.4.54-beta.51", - "resolved": "https://registry.npmjs.org/homebridge/-/homebridge-0.4.54-beta.51.tgz", - "integrity": "sha512-WycAihWBzWu+8ZBnVrU4wqdU0bq/cT43iKdk2+6ODhbGh/xlaxIFpMdSGX++YqwGpu7cNEL0L45Woj9sEfFYNw==", + "version": "0.4.54-beta.52", + "resolved": "https://registry.npmjs.org/homebridge/-/homebridge-0.4.54-beta.52.tgz", + "integrity": "sha512-fQKpd2khare7BTbzyEVjABWLhAeTUF1it5MrvnI/NDlzkvehPv362j8Jim1/sVyafbY+RloC0z5vFdu1nkNvoA==", "dev": true, "requires": { "chalk": "^3.0.0", "commander": "5.0.0", - "hap-nodejs": "0.6.2", + "hap-nodejs": "0.6.3", "node-persist": "^0.0.11", "qrcode-terminal": "^0.12.0", "semver": "^7.1.3", diff --git a/bridged-camera-example-typescript/package.json b/bridged-camera-example-typescript/package.json index b70468d..bcc3125 100644 --- a/bridged-camera-example-typescript/package.json +++ b/bridged-camera-example-typescript/package.json @@ -37,6 +37,6 @@ "@types/ip": "^1.1.0", "typescript": "^3.8.3", "rimraf": "^3.0.2", - "homebridge": "^0.4.54-beta.51" + "homebridge": "^0.4.54-beta.52" } } diff --git a/bridged-camera-example-typescript/src/dynamic-camera-platform.ts b/bridged-camera-example-typescript/src/dynamic-camera-platform.ts index 3c60d10..7109723 100644 --- a/bridged-camera-example-typescript/src/dynamic-camera-platform.ts +++ b/bridged-camera-example-typescript/src/dynamic-camera-platform.ts @@ -15,8 +15,27 @@ import {ExampleFFMPEGStreamingDelegate} from "./streamingDelegate"; const PLUGIN_NAME = "homebridge-bridged-camera-example"; const PLATFORM_NAME = "ExampleDynamicCameraPlatform"; -/** - * TODO explain +/* + * IMPORTANT NOTICE + * + * One thing you need to take care of is, that you never ever ever import anything directly from the "homebridge" module (or the "hap-nodejs" module). + * The above import block may seem like, that we do exactly that, but actually those imports are only used for types and interfaces + * and will disappear once the code is compiled to Javascript. + * In fact you can check that by running `npm run build` and opening the compiled Javascript file in the `dist` folder. + * You will notice that the file does not contain a `... = require("homebridge");` statement anywhere in the code. + * + * The contents of the above import statement MUST ONLY be used for type annotation or accessing things like CONST ENUMS, + * which is a special case as they get replaced by the actual value and do not remain as a reference in the compiled code. + * Meaning normal enums are bad, const enums can be used. + * + * You MUST NOT import anything else which remains as a reference in the code, as this will result in + * a `... = require("homebridge");` to be compiled into the final Javascript code. + * This typically leads to unexpected behavior at runtime, as in many cases it won't be able to find the module + * or will import another instance of homebridge causing collisions. + * + * To mitigate this the {@link API | Homebridge API} exposes the whole suite of HAP-NodeJS inside the `hap` property + * of the api object, which can be acquired for example in the initializer function. This reference can be stored + * like this for example and used to access all exported variables and classes from HAP-NodeJS. */ let hap: HAP; let Accessory: typeof PlatformAccessory; diff --git a/bridged-camera-example-typescript/src/streamingDelegate.ts b/bridged-camera-example-typescript/src/streamingDelegate.ts index f1a42f3..1dca031 100644 --- a/bridged-camera-example-typescript/src/streamingDelegate.ts +++ b/bridged-camera-example-typescript/src/streamingDelegate.ts @@ -2,7 +2,8 @@ import ip from "ip"; import {ChildProcess, spawn} from "child_process"; import { CameraController, - CameraStreamingDelegate, HAP, + CameraStreamingDelegate, + HAP, PrepareStreamCallback, PrepareStreamRequest, PrepareStreamResponse, @@ -130,7 +131,7 @@ export class ExampleFFMPEGStreamingDelegate implements CameraStreamingDelegate { const sessionId = request.sessionID; switch (request.type) { - case this.hap.StreamRequestTypes.START: + case StreamRequestTypes.START: const sessionInfo = this.pendingSessions[sessionId]; const video: VideoInfo = request.video; @@ -158,8 +159,8 @@ export class ExampleFFMPEGStreamingDelegate implements CameraStreamingDelegate { `-c:v libx264 -pix_fmt yuv420p -r ${fps} -an -sn -dn -b:v ${maxBitrate}k -bufsize ${2*maxBitrate}k -maxrate ${maxBitrate}k ` + `-payload_type ${payloadType} -ssrc ${ssrc} -f rtp `; // -profile:v ${profile} -level:v ${level} - if (cryptoSuite !== this.hap.SRTPCryptoSuites.NONE) { // actually ffmpeg just supports AES_CM_128_HMAC_SHA1_80 - videoffmpegCommand += `-srtp_out_suite ${this.hap.SRTPCryptoSuites[cryptoSuite]} -srtp_out_params ${videoSRTP} s`; + if (cryptoSuite === SRTPCryptoSuites.AES_CM_128_HMAC_SHA1_80) { // actually ffmpeg just supports AES_CM_128_HMAC_SHA1_80 + videoffmpegCommand += `-srtp_out_suite AES_CM_128_HMAC_SHA1_80 -srtp_out_params ${videoSRTP} s`; } videoffmpegCommand += `rtp://${address}:${videoPort}?rtcpport=${videoPort}&localrtcpport=${videoPort}&pkt_size=${mtu}`; @@ -207,12 +208,12 @@ export class ExampleFFMPEGStreamingDelegate implements CameraStreamingDelegate { delete this.pendingSessions[sessionId]; break; - case this.hap.StreamRequestTypes.RECONFIGURE: + case StreamRequestTypes.RECONFIGURE: // not supported by this example console.log("Received (unsupported) request to reconfigure to: " + JSON.stringify(request.video)); callback(); break; - case this.hap.StreamRequestTypes.STOP: + case StreamRequestTypes.STOP: const ffmpegProcess = this.ongoingSessions[sessionId]; try { diff --git a/dynamic-platform-example-typescript/package-lock.json b/dynamic-platform-example-typescript/package-lock.json index 0f68e52..7a33cfc 100644 --- a/dynamic-platform-example-typescript/package-lock.json +++ b/dynamic-platform-example-typescript/package-lock.json @@ -237,9 +237,9 @@ } }, "hap-nodejs": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/hap-nodejs/-/hap-nodejs-0.6.2.tgz", - "integrity": "sha512-/LlRaYCtcwC4jBUKRJeBUdUfKEp8w/NK3PNYXaAOngQL9uVnqsvQ0Gce/mUhSXPhsxfdnxbHVT870W9OEnV6+A==", + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/hap-nodejs/-/hap-nodejs-0.6.3.tgz", + "integrity": "sha512-FH5cEBJDz1e/IzqYbGEO4PdydXrZN+vqZcvKexyRqZlP1DbPjjeboI2TbqGfftUsyZu2jz5y/NRYlS9HzEijcQ==", "dev": true, "requires": { "bonjour-hap": "3.5.4", @@ -273,14 +273,14 @@ "dev": true }, "homebridge": { - "version": "0.4.54-beta.51", - "resolved": "https://registry.npmjs.org/homebridge/-/homebridge-0.4.54-beta.51.tgz", - "integrity": "sha512-WycAihWBzWu+8ZBnVrU4wqdU0bq/cT43iKdk2+6ODhbGh/xlaxIFpMdSGX++YqwGpu7cNEL0L45Woj9sEfFYNw==", + "version": "0.4.54-beta.52", + "resolved": "https://registry.npmjs.org/homebridge/-/homebridge-0.4.54-beta.52.tgz", + "integrity": "sha512-fQKpd2khare7BTbzyEVjABWLhAeTUF1it5MrvnI/NDlzkvehPv362j8Jim1/sVyafbY+RloC0z5vFdu1nkNvoA==", "dev": true, "requires": { "chalk": "^3.0.0", "commander": "5.0.0", - "hap-nodejs": "0.6.2", + "hap-nodejs": "0.6.3", "node-persist": "^0.0.11", "qrcode-terminal": "^0.12.0", "semver": "^7.1.3", diff --git a/dynamic-platform-example-typescript/package.json b/dynamic-platform-example-typescript/package.json index 1b7a875..1689d0a 100644 --- a/dynamic-platform-example-typescript/package.json +++ b/dynamic-platform-example-typescript/package.json @@ -30,6 +30,6 @@ "@types/node": "10.17.19", "typescript": "^3.8.3", "rimraf": "^3.0.2", - "homebridge": "^0.4.54-beta.51" + "homebridge": "^0.4.54-beta.52" } } diff --git a/dynamic-platform-example-typescript/src/dynamic-platform.ts b/dynamic-platform-example-typescript/src/dynamic-platform.ts index c7f0e73..6da1306 100644 --- a/dynamic-platform-example-typescript/src/dynamic-platform.ts +++ b/dynamic-platform-example-typescript/src/dynamic-platform.ts @@ -2,6 +2,7 @@ import http, {IncomingMessage, Server, ServerResponse} from "http"; import { API, APIEvent, + CharacteristicEventTypes, CharacteristicSetCallback, CharacteristicValue, DynamicPlatformPlugin, @@ -10,14 +11,32 @@ import { PlatformAccessory, PlatformAccessoryEvent, PlatformConfig, - Service, } from "homebridge"; const PLUGIN_NAME = "homebridge-dynamic-platform-example"; const PLATFORM_NAME = "ExampleDynamicPlatform"; -/** - * TODO explain +/* + * IMPORTANT NOTICE + * + * One thing you need to take care of is, that you never ever ever import anything directly from the "homebridge" module (or the "hap-nodejs" module). + * The above import block may seem like, that we do exactly that, but actually those imports are only used for types and interfaces + * and will disappear once the code is compiled to Javascript. + * In fact you can check that by running `npm run build` and opening the compiled Javascript file in the `dist` folder. + * You will notice that the file does not contain a `... = require("homebridge");` statement anywhere in the code. + * + * The contents of the above import statement MUST ONLY be used for type annotation or accessing things like CONST ENUMS, + * which is a special case as they get replaced by the actual value and do not remain as a reference in the compiled code. + * Meaning normal enums are bad, const enums can be used. + * + * You MUST NOT import anything else which remains as a reference in the code, as this will result in + * a `... = require("homebridge");` to be compiled into the final Javascript code. + * This typically leads to unexpected behavior at runtime, as in many cases it won't be able to find the module + * or will import another instance of homebridge causing collisions. + * + * To mitigate this the {@link API | Homebridge API} exposes the whole suite of HAP-NodeJS inside the `hap` property + * of the api object, which can be acquired for example in the initializer function. This reference can be stored + * like this for example and used to access all exported variables and classes from HAP-NodeJS. */ let hap: HAP; let Accessory: typeof PlatformAccessory; @@ -72,7 +91,7 @@ class ExampleDynamicPlatform implements DynamicPlatformPlugin { }); accessory.getService(hap.Service.Lightbulb)!.getCharacteristic(hap.Characteristic.On) - .on(hap.CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => { + .on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => { this.log.info("%s Light was set to: " + value); callback(); }); diff --git a/independent-platform-example-typescript/package-lock.json b/independent-platform-example-typescript/package-lock.json index 6605ee1..26e3d20 100644 --- a/independent-platform-example-typescript/package-lock.json +++ b/independent-platform-example-typescript/package-lock.json @@ -237,9 +237,9 @@ } }, "hap-nodejs": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/hap-nodejs/-/hap-nodejs-0.6.2.tgz", - "integrity": "sha512-/LlRaYCtcwC4jBUKRJeBUdUfKEp8w/NK3PNYXaAOngQL9uVnqsvQ0Gce/mUhSXPhsxfdnxbHVT870W9OEnV6+A==", + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/hap-nodejs/-/hap-nodejs-0.6.3.tgz", + "integrity": "sha512-FH5cEBJDz1e/IzqYbGEO4PdydXrZN+vqZcvKexyRqZlP1DbPjjeboI2TbqGfftUsyZu2jz5y/NRYlS9HzEijcQ==", "dev": true, "requires": { "bonjour-hap": "3.5.4", @@ -273,14 +273,14 @@ "dev": true }, "homebridge": { - "version": "0.4.54-beta.51", - "resolved": "https://registry.npmjs.org/homebridge/-/homebridge-0.4.54-beta.51.tgz", - "integrity": "sha512-WycAihWBzWu+8ZBnVrU4wqdU0bq/cT43iKdk2+6ODhbGh/xlaxIFpMdSGX++YqwGpu7cNEL0L45Woj9sEfFYNw==", + "version": "0.4.54-beta.52", + "resolved": "https://registry.npmjs.org/homebridge/-/homebridge-0.4.54-beta.52.tgz", + "integrity": "sha512-fQKpd2khare7BTbzyEVjABWLhAeTUF1it5MrvnI/NDlzkvehPv362j8Jim1/sVyafbY+RloC0z5vFdu1nkNvoA==", "dev": true, "requires": { "chalk": "^3.0.0", "commander": "5.0.0", - "hap-nodejs": "0.6.2", + "hap-nodejs": "0.6.3", "node-persist": "^0.0.11", "qrcode-terminal": "^0.12.0", "semver": "^7.1.3", diff --git a/independent-platform-example-typescript/package.json b/independent-platform-example-typescript/package.json index 0496934..d987598 100644 --- a/independent-platform-example-typescript/package.json +++ b/independent-platform-example-typescript/package.json @@ -33,6 +33,6 @@ "@types/node": "10.17.19", "typescript": "^3.8.3", "rimraf": "^3.0.2", - "homebridge": "^0.4.54-beta.51" + "homebridge": "^0.4.54-beta.52" } } diff --git a/independent-platform-example-typescript/src/independent-platform.ts b/independent-platform-example-typescript/src/independent-platform.ts index fa100d8..a845222 100644 --- a/independent-platform-example-typescript/src/independent-platform.ts +++ b/independent-platform-example-typescript/src/independent-platform.ts @@ -1,5 +1,6 @@ import { API, + CharacteristicEventTypes, CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue, @@ -13,8 +14,27 @@ import { const PLUGIN_NAME = "homebridge-independent-platform-example"; const PLATFORM_NAME = "ExampleIndependentPlatform"; -/** - * TODO explain +/* + * IMPORTANT NOTICE + * + * One thing you need to take care of is, that you never ever ever import anything directly from the "homebridge" module (or the "hap-nodejs" module). + * The above import block may seem like, that we do exactly that, but actually those imports are only used for types and interfaces + * and will disappear once the code is compiled to Javascript. + * In fact you can check that by running `npm run build` and opening the compiled Javascript file in the `dist` folder. + * You will notice that the file does not contain a `... = require("homebridge");` statement anywhere in the code. + * + * The contents of the above import statement MUST ONLY be used for type annotation or accessing things like CONST ENUMS, + * which is a special case as they get replaced by the actual value and do not remain as a reference in the compiled code. + * Meaning normal enums are bad, const enums can be used. + * + * You MUST NOT import anything else which remains as a reference in the code, as this will result in + * a `... = require("homebridge");` to be compiled into the final Javascript code. + * This typically leads to unexpected behavior at runtime, as in many cases it won't be able to find the module + * or will import another instance of homebridge causing collisions. + * + * To mitigate this the {@link API | Homebridge API} exposes the whole suite of HAP-NodeJS inside the `hap` property + * of the api object, which can be acquired for example in the initializer function. This reference can be stored + * like this for example and used to access all exported variables and classes from HAP-NodeJS. */ let hap: HAP; let Accessory: typeof PlatformAccessory; @@ -50,11 +70,11 @@ class ExampleIndependentPlatform implements IndependentPlatformPlugin { const switchService = new hap.Service.Switch(name); switchService.getCharacteristic(hap.Characteristic.On) - .on(hap.CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => { + .on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => { this.log.info("Current state of the switch was returned: " + (switchOn? "ON": "OFF")); callback(undefined, switchOn); }) - .on(hap.CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => { + .on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => { switchOn = value as boolean; this.log.info("Switch state was set to: " + (switchOn? "ON": "OFF")); callback(); diff --git a/static-platform-example-typescript/package-lock.json b/static-platform-example-typescript/package-lock.json index 7bb43fa..1c57904 100644 --- a/static-platform-example-typescript/package-lock.json +++ b/static-platform-example-typescript/package-lock.json @@ -237,9 +237,9 @@ } }, "hap-nodejs": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/hap-nodejs/-/hap-nodejs-0.6.2.tgz", - "integrity": "sha512-/LlRaYCtcwC4jBUKRJeBUdUfKEp8w/NK3PNYXaAOngQL9uVnqsvQ0Gce/mUhSXPhsxfdnxbHVT870W9OEnV6+A==", + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/hap-nodejs/-/hap-nodejs-0.6.3.tgz", + "integrity": "sha512-FH5cEBJDz1e/IzqYbGEO4PdydXrZN+vqZcvKexyRqZlP1DbPjjeboI2TbqGfftUsyZu2jz5y/NRYlS9HzEijcQ==", "dev": true, "requires": { "bonjour-hap": "3.5.4", @@ -273,14 +273,14 @@ "dev": true }, "homebridge": { - "version": "0.4.54-beta.51", - "resolved": "https://registry.npmjs.org/homebridge/-/homebridge-0.4.54-beta.51.tgz", - "integrity": "sha512-WycAihWBzWu+8ZBnVrU4wqdU0bq/cT43iKdk2+6ODhbGh/xlaxIFpMdSGX++YqwGpu7cNEL0L45Woj9sEfFYNw==", + "version": "0.4.54-beta.52", + "resolved": "https://registry.npmjs.org/homebridge/-/homebridge-0.4.54-beta.52.tgz", + "integrity": "sha512-fQKpd2khare7BTbzyEVjABWLhAeTUF1it5MrvnI/NDlzkvehPv362j8Jim1/sVyafbY+RloC0z5vFdu1nkNvoA==", "dev": true, "requires": { "chalk": "^3.0.0", "commander": "5.0.0", - "hap-nodejs": "0.6.2", + "hap-nodejs": "0.6.3", "node-persist": "^0.0.11", "qrcode-terminal": "^0.12.0", "semver": "^7.1.3", diff --git a/static-platform-example-typescript/package.json b/static-platform-example-typescript/package.json index d3ffd68..9b59806 100644 --- a/static-platform-example-typescript/package.json +++ b/static-platform-example-typescript/package.json @@ -33,6 +33,6 @@ "@types/node": "10.17.19", "typescript": "^3.8.3", "rimraf": "^3.0.2", - "homebridge": "^0.4.54-beta.51" + "homebridge": "^0.4.54-beta.52" } } diff --git a/static-platform-example-typescript/src/static-platform.ts b/static-platform-example-typescript/src/static-platform.ts index 2d41a02..969924f 100644 --- a/static-platform-example-typescript/src/static-platform.ts +++ b/static-platform-example-typescript/src/static-platform.ts @@ -3,8 +3,27 @@ import {ExampleSwitch} from "./switch-accessory"; const PLATFORM_NAME = "ExampleStaticPlatform"; -/** - * TODO explain +/* + * IMPORTANT NOTICE + * + * One thing you need to take care of is, that you never ever ever import anything directly from the "homebridge" module (or the "hap-nodejs" module). + * The above import block may seem like, that we do exactly that, but actually those imports are only used for types and interfaces + * and will disappear once the code is compiled to Javascript. + * In fact you can check that by running `npm run build` and opening the compiled Javascript file in the `dist` folder. + * You will notice that the file does not contain a `... = require("homebridge");` statement anywhere in the code. + * + * The contents of the above import statement MUST ONLY be used for type annotation or accessing things like CONST ENUMS, + * which is a special case as they get replaced by the actual value and do not remain as a reference in the compiled code. + * Meaning normal enums are bad, const enums can be used. + * + * You MUST NOT import anything else which remains as a reference in the code, as this will result in + * a `... = require("homebridge");` to be compiled into the final Javascript code. + * This typically leads to unexpected behavior at runtime, as in many cases it won't be able to find the module + * or will import another instance of homebridge causing collisions. + * + * To mitigate this the {@link API | Homebridge API} exposes the whole suite of HAP-NodeJS inside the `hap` property + * of the api object, which can be acquired for example in the initializer function. This reference can be stored + * like this for example and used to access all exported variables and classes from HAP-NodeJS. */ let hap: HAP; diff --git a/static-platform-example-typescript/src/switch-accessory.ts b/static-platform-example-typescript/src/switch-accessory.ts index bb335e7..a8561d4 100644 --- a/static-platform-example-typescript/src/switch-accessory.ts +++ b/static-platform-example-typescript/src/switch-accessory.ts @@ -5,7 +5,8 @@ import { CharacteristicValue, HAP, Logging, - Service + Service, + CharacteristicEventTypes } from "homebridge"; export class ExampleSwitch implements AccessoryPlugin { @@ -26,11 +27,11 @@ export class ExampleSwitch implements AccessoryPlugin { this.switchService = new hap.Service.Switch(name); this.switchService.getCharacteristic(hap.Characteristic.On) - .on(hap.CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => { + .on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => { log.info("Current state of the switch was returned: " + (this.switchOn? "ON": "OFF")); callback(undefined, this.switchOn); }) - .on(hap.CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => { + .on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => { this.switchOn = value as boolean; log.info("Switch state was set to: " + (this.switchOn? "ON": "OFF")); callback();