From 1a51e4d8c5a54fa655b7f012c91201c934e46909 Mon Sep 17 00:00:00 2001 From: Brandon Watson Date: Tue, 28 Dec 2021 14:48:02 -0500 Subject: [PATCH 1/7] Successfully added tsyringe to project --- package-lock.json | 266 +++++++++++------------ package.json | 8 +- src/DataProviders/HarmonyDataProvider.ts | 67 +++--- src/platform.ts | 141 +++++++----- src/settings.ts | 2 +- tsconfig.json | 4 +- 6 files changed, 264 insertions(+), 224 deletions(-) diff --git a/package-lock.json b/package-lock.json index c3da723..8e099e6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -866,20 +866,22 @@ } }, "@homebridge/ciao": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@homebridge/ciao/-/ciao-1.1.0.tgz", - "integrity": "sha512-fpFUy/1PQ5eMPiTdRewoA/gnFCLBkBlNsFJpHO8/Wk/p1fQM0YLMDdyvR956CF0QdArhz2SBB3VeGmgVkOrLFw==", + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@homebridge/ciao/-/ciao-1.1.3.tgz", + "integrity": "sha512-p9WgcSYUj3rtC1g3ywJpKxvIZXPkkv88JxbuW6idMHrUOqDMJlWIsWF0yXynQf8Z28gA0j6AJN9EnAr+hg5gNA==", + "dev": true, "requires": { - "debug": "^4.3.1", + "debug": "^4.3.2", "fast-deep-equal": "^3.1.3", - "source-map-support": "^0.5.19", - "tslib": "^2.0.3" + "source-map-support": "^0.5.20", + "tslib": "^2.3.1" }, "dependencies": { "debug": { - "version": "4.3.1", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", - "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", + "version": "4.3.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.3.tgz", + "integrity": "sha512-/zxw5+vh1Tfv+4Qn7a5nsbcJKPaSvCDhojn6FEl9vupwK2VCSDtEiEtqr8DFtzYFOdz63LBkxec7DYuc2jon6Q==", + "dev": true, "requires": { "ms": "2.1.2" } @@ -887,12 +889,14 @@ "fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", - "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", + "dev": true }, "tslib": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.0.3.tgz", - "integrity": "sha512-uZtkfKblCEQtZKBF6EBXVZeQNl82yqtDQdv+eck8u7tdPxjLu2/lp5/uPW+um2tpuxINHWy3GhiccY7QgEaVHQ==" + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", + "integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==", + "dev": true } } }, @@ -995,20 +999,22 @@ } }, "browserslist": { - "version": "4.12.0", - "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.12.0.tgz", - "integrity": "sha512-UH2GkcEDSI0k/lRkuDSzFl9ZZ87skSy9w2XAn1MsZnL+4c4rqbBd3e82UWHbYDpztABrPBhZsTEeuxVfHppqDg==", + "version": "4.19.1", + "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.19.1.tgz", + "integrity": "sha512-u2tbbG5PdKRTUoctO3NBD8FQ5HdPh1ZXPHzp1rwaa5jTc+RV9/+RlWiAIKmjRPQF+xbGM9Kklj5bZQFa2s/38A==", "requires": { - "caniuse-lite": "^1.0.30001043", - "electron-to-chromium": "^1.3.413", - "node-releases": "^1.1.53", - "pkg-up": "^2.0.0" + "caniuse-lite": "^1.0.30001286", + "electron-to-chromium": "^1.4.17", + "escalade": "^3.1.1", + "node-releases": "^2.0.1", + "picocolors": "^1.0.0" } }, "buffer-from": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", - "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==" + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", + "dev": true }, "builtin-modules": { "version": "1.1.1", @@ -1017,9 +1023,9 @@ "dev": true }, "caniuse-lite": { - "version": "1.0.30001048", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001048.tgz", - "integrity": "sha512-g1iSHKVxornw0K8LG9LLdf+Fxnv7T1Z+mMsf0/YYLclQX4Cd522Ap0Lrw6NFqHgezit78dtyWxzlV2Xfc7vgRg==" + "version": "1.0.30001294", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001294.tgz", + "integrity": "sha512-LiMlrs1nSKZ8qkNhpUf5KD0Al1KCBE3zaT7OLOwEkagXMEDij98SiOovn9wxVGQpklk9vVC/pUSqgYmkmKOS8g==" }, "caseless": { "version": "0.12.0", @@ -1065,7 +1071,8 @@ "commander": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/commander/-/commander-5.1.0.tgz", - "integrity": "sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==" + "integrity": "sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==", + "dev": true }, "concat-map": { "version": "0.0.1", @@ -1128,9 +1135,10 @@ } }, "decimal.js": { - "version": "10.2.1", - "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.2.1.tgz", - "integrity": "sha512-KaL7+6Fw6i5A2XSnsbhm/6B+NuEA7TZ4vqxnd5tXz9sbKtrN9Srj8ab4vKVdK8YAqZO9P1kg45Y6YLoduPf+kw==" + "version": "10.3.1", + "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.3.1.tgz", + "integrity": "sha512-V0pfhfr8suzyPGOx3nmq4aHqabehUZn6Ch9kyFpV79TGDTWFmHqUqXdabR7QHqxzrYolF4+tVmJhUG4OURg5dQ==", + "dev": true }, "define-properties": { "version": "1.1.3", @@ -1161,9 +1169,9 @@ } }, "electron-to-chromium": { - "version": "1.3.427", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.427.tgz", - "integrity": "sha512-/rG5G7Opcw68/Yrb4qYkz07h3bESVRJjUl4X/FrKLXzoUJleKm6D7K7rTTz8V5LUWnd+BbTOyxJX2XprRqHD8A==" + "version": "1.4.29", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.29.tgz", + "integrity": "sha512-N2Jbwxo5Rum8G2YXeUxycs1sv4Qme/ry71HG73bv8BvZl+I/4JtRgK/En+ST/Wh/yF1fqvVCY4jZBgMxnhjtBA==" }, "es-abstract": { "version": "1.17.5", @@ -1222,6 +1230,11 @@ "ext": "^1.1.2" } }, + "escalade": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", + "integrity": "sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==" + }, "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", @@ -1282,15 +1295,8 @@ "fast-srp-hap": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/fast-srp-hap/-/fast-srp-hap-2.0.2.tgz", - "integrity": "sha512-wABhZRrFhlovqJQ1HygOUB4R6WZW2hmlpvVYh2dVCy8BPLabDrB/Tu6XI3B4QfmhtHk8s1OeiFqJHY7FBsphug==" - }, - "find-up": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz", - "integrity": "sha1-RdG35QbHF93UgndaK3eSCjwMV6c=", - "requires": { - "locate-path": "^2.0.0" - } + "integrity": "sha512-wABhZRrFhlovqJQ1HygOUB4R6WZW2hmlpvVYh2dVCy8BPLabDrB/Tu6XI3B4QfmhtHk8s1OeiFqJHY7FBsphug==", + "dev": true }, "forever-agent": { "version": "0.6.1", @@ -1319,9 +1325,10 @@ "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" }, "futoin-hkdf": { - "version": "1.3.2", - "resolved": "https://registry.npmjs.org/futoin-hkdf/-/futoin-hkdf-1.3.2.tgz", - "integrity": "sha512-3EVi3ETTyJg5PSXlxLCaUVVn0pSbDf62L3Gwxne7Uq+d8adOSNWQAad4gg7WToHkcgnCJb3Wlb1P8r4Evj4GPw==" + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/futoin-hkdf/-/futoin-hkdf-1.3.3.tgz", + "integrity": "sha512-oR75fYk3B3X9/B02Y6vusrBKucrpC6VjxhRL+C6B7FwUpuSRHbhBNG3AZbcE/xPyJmEQWsyqUFp3VeNNbA3S7A==", + "dev": true }, "gensync": { "version": "1.0.0-beta.1", @@ -1356,11 +1363,12 @@ "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==" }, "hap-nodejs": { - "version": "0.8.3", - "resolved": "https://registry.npmjs.org/hap-nodejs/-/hap-nodejs-0.8.3.tgz", - "integrity": "sha512-1bX0UPmIfthYgF43M0rhNsyLayPVXAPvqgwUBfTUNSw4XuesepfbZFnhT8eYZqqClQorKaZr6FeHWw785FQ1Tg==", + "version": "0.8.5", + "resolved": "https://registry.npmjs.org/hap-nodejs/-/hap-nodejs-0.8.5.tgz", + "integrity": "sha512-4OnIc3oYXHSmafsuBuY1sBbQaMy8n2uRnEPS9ZnD032cOvaVz2IZA3/rE5LEHsxgDvo+oiLxcdqXz0ho38Odxw==", + "dev": true, "requires": { - "@homebridge/ciao": "~1.1.0", + "@homebridge/ciao": "~1.1.2", "debug": "^4.1.1", "decimal.js": "^10.2.0", "fast-srp-hap": "2.0.2", @@ -1373,7 +1381,8 @@ "tweetnacl": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz", - "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==" + "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==", + "dev": true } } }, @@ -1421,13 +1430,14 @@ "integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg==" }, "homebridge": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/homebridge/-/homebridge-1.2.4.tgz", - "integrity": "sha512-uyhLgJumhivAQrauoH5EfxKC2hcX2r8fC2ckAScBx3M7/+XuobHVNYF7yNND69mlg+Jd/KBfaF5zS3ckVl807g==", + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/homebridge/-/homebridge-1.2.5.tgz", + "integrity": "sha512-tiJi31Ereo75pPhXz9DVLM8lXW8bopXo5aEmBU8elwvfkyTXlGc2dToz9dnc+Vt8ls7jsQAIEl6MkGWrQ7XqtA==", + "dev": true, "requires": { "chalk": "^4.1.0", "commander": "5.1.0", - "hap-nodejs": "~0.8.3", + "hap-nodejs": "~0.8.5", "node-persist": "^0.0.11", "qrcode-terminal": "^0.12.0", "semver": "^7.3.2", @@ -1438,14 +1448,16 @@ "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, "requires": { "color-convert": "^2.0.1" } }, "chalk": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz", - "integrity": "sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==", + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dev": true, "requires": { "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" @@ -1455,6 +1467,7 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, "requires": { "color-name": "~1.1.4" } @@ -1462,17 +1475,20 @@ "color-name": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true }, "has-flag": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", - "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true }, "semver": { - "version": "7.3.4", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.4.tgz", - "integrity": "sha512-tCfb2WLjqFAtXn4KEdxIhalnRtoKFN7nAwj0B3ZXCbQloV2tq5eDbcTmT68JJD3nRJq24/XgxtQKFIpQdtvmVw==", + "version": "7.3.5", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz", + "integrity": "sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ==", + "dev": true, "requires": { "lru-cache": "^6.0.0" } @@ -1481,6 +1497,7 @@ "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, "requires": { "has-flag": "^4.0.0" } @@ -1524,7 +1541,8 @@ "ip": { "version": "1.1.5", "resolved": "https://registry.npmjs.org/ip/-/ip-1.1.5.tgz", - "integrity": "sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=" + "integrity": "sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=", + "dev": true }, "is-callable": { "version": "1.1.5", @@ -1588,9 +1606,9 @@ "integrity": "sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==" }, "json-schema": { - "version": "0.2.3", - "resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz", - "integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM=" + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.4.0.tgz", + "integrity": "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA==" }, "json-schema-traverse": { "version": "0.4.1", @@ -1611,13 +1629,13 @@ } }, "jsprim": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz", - "integrity": "sha1-MT5mvB5cwG5Di8G3SZwuXFastqI=", + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.2.tgz", + "integrity": "sha512-P2bSOMAc/ciLz6DzgjVlGJP9+BrJWu5UDGK70C2iweC5QBIeFf0ZXRvGjEj2uYgrY2MkAAhsSWHDWlFtEroZWw==", "requires": { "assert-plus": "1.0.0", "extsprintf": "1.3.0", - "json-schema": "0.2.3", + "json-schema": "0.4.0", "verror": "1.10.0" } }, @@ -1634,19 +1652,10 @@ "leven": "^3.1.0" } }, - "locate-path": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-2.0.0.tgz", - "integrity": "sha1-K1aLJl7slExtnA3pw9u7ygNUzY4=", - "requires": { - "p-locate": "^2.0.0", - "path-exists": "^3.0.0" - } - }, "lodash": { - "version": "4.17.15", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" }, "loose-envify": { "version": "1.4.0", @@ -1660,6 +1669,7 @@ "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "dev": true, "requires": { "yallist": "^4.0.0" } @@ -1695,6 +1705,7 @@ "version": "0.5.5", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", + "dev": true, "requires": { "minimist": "^1.2.5" } @@ -1718,15 +1729,16 @@ "version": "0.0.11", "resolved": "https://registry.npmjs.org/node-persist/-/node-persist-0.0.11.tgz", "integrity": "sha1-1m66Pr72IPB5Uw+nsTB2qQZmWHQ=", + "dev": true, "requires": { "mkdirp": "~0.5.1", "q": "~1.1.1" } }, "node-releases": { - "version": "1.1.53", - "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-1.1.53.tgz", - "integrity": "sha512-wp8zyQVwef2hpZ/dJH7SfSrIPD6YoJz6BDQDpGEkcA0s3LpAQoxBIYmfIq6QAhC1DhwsyCgTaTTcONwX8qzCuQ==" + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.1.tgz", + "integrity": "sha512-CqyzN6z7Q6aMeF/ktcMVTzhAHCEpf8SOarwpzpf8pNBY2k5/oM34UHldUwp8VKI7uxct2HxSRdJjBaZeESzcxA==" }, "oauth-sign": { "version": "0.9.0", @@ -1763,32 +1775,6 @@ "wrappy": "1" } }, - "p-limit": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-1.3.0.tgz", - "integrity": "sha512-vvcXsLAJ9Dr5rQOPk7toZQZJApBl2K4J6dANSsEuh6QI41JYcsS/qhTGa9ErIUUgK3WNQoJYvylxvjqmiqEA9Q==", - "requires": { - "p-try": "^1.0.0" - } - }, - "p-locate": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-2.0.0.tgz", - "integrity": "sha1-IKAQOyIqcMj9OcwuWAaA893l7EM=", - "requires": { - "p-limit": "^1.1.0" - } - }, - "p-try": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/p-try/-/p-try-1.0.0.tgz", - "integrity": "sha1-y8ec26+P1CKOE/Yh8rGiN8GyB7M=" - }, - "path-exists": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", - "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" - }, "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", @@ -1796,22 +1782,19 @@ "dev": true }, "path-parse": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", - "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==" + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", + "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==" }, "performance-now": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=" }, - "pkg-up": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/pkg-up/-/pkg-up-2.0.0.tgz", - "integrity": "sha1-yBmscoBZpGHKscOImivjxJoATX8=", - "requires": { - "find-up": "^2.1.0" - } + "picocolors": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", + "integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==" }, "private": { "version": "0.1.8", @@ -1846,18 +1829,25 @@ "q": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/q/-/q-1.1.2.tgz", - "integrity": "sha1-Y1fikSBnAdmfGXq4TlforRlvKok=" + "integrity": "sha1-Y1fikSBnAdmfGXq4TlforRlvKok=", + "dev": true }, "qrcode-terminal": { "version": "0.12.0", "resolved": "https://registry.npmjs.org/qrcode-terminal/-/qrcode-terminal-0.12.0.tgz", - "integrity": "sha512-EXtzRZmC+YGmGlDFbXKxQiMZNwCLEO6BANKXG4iCtSIM0yqc/pappSx3RIKr4r0uh5JsBckOXeKrB3Iz7mdQpQ==" + "integrity": "sha512-EXtzRZmC+YGmGlDFbXKxQiMZNwCLEO6BANKXG4iCtSIM0yqc/pappSx3RIKr4r0uh5JsBckOXeKrB3Iz7mdQpQ==", + "dev": true }, "qs": { "version": "6.5.2", "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" }, + "reflect-metadata": { + "version": "0.1.13", + "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz", + "integrity": "sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==" + }, "regenerate": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/regenerate/-/regenerate-1.4.0.tgz", @@ -2012,9 +2002,10 @@ "integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=" }, "source-map-support": { - "version": "0.5.19", - "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz", - "integrity": "sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==", + "version": "0.5.21", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", + "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", + "dev": true, "requires": { "buffer-from": "^1.0.0", "source-map": "^0.6.0" @@ -2023,7 +2014,8 @@ "source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true } } }, @@ -2117,8 +2109,7 @@ "tslib": { "version": "1.11.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.11.1.tgz", - "integrity": "sha512-aZW88SY8kQbU7gpV19lN24LtXh/yD4ZZg6qieAJDDg+YBsJcSmLGK9QpnUjAKVG/xefmvJGd1WUmfpT/g6AJGA==", - "dev": true + "integrity": "sha512-aZW88SY8kQbU7gpV19lN24LtXh/yD4ZZg6qieAJDDg+YBsJcSmLGK9QpnUjAKVG/xefmvJGd1WUmfpT/g6AJGA==" }, "tslint": { "version": "5.20.1", @@ -2158,6 +2149,14 @@ "tslib": "^1.8.1" } }, + "tsyringe": { + "version": "4.6.0", + "resolved": "https://registry.npmjs.org/tsyringe/-/tsyringe-4.6.0.tgz", + "integrity": "sha512-BMQAZamSfEmIQzH8WJeRu1yZGQbPSDuI9g+yEiKZFIcO46GPZuMOC2d0b52cVBdw1d++06JnDSIIZvEnogMdAw==", + "requires": { + "tslib": "^1.9.3" + } + }, "tunnel-agent": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", @@ -2185,9 +2184,9 @@ } }, "typescript": { - "version": "3.8.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz", - "integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==", + "version": "3.9.10", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.10.tgz", + "integrity": "sha512-w6fIxVE/H1PkLKcCPsFqKE7Kv7QUwhU8qQY2MueZXWx5cPZdwFupLgKK3vntcK98BtNHZtAF4LA/yl2a7k8R6Q==", "dev": true }, "unicode-canonical-property-names-ecmascript": { @@ -2288,7 +2287,8 @@ "yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", - "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true } } } diff --git a/package.json b/package.json index ab99b8e..bde70d7 100644 --- a/package.json +++ b/package.json @@ -28,10 +28,11 @@ "node": ">=7.6.0" }, "devDependencies": { + "homebridge": "^1.2.5", "rollup-plugin-typescript": "^1.0.1", "tslib": "^1.10.0", "tslint": "^5.17.0", - "typescript": "^3.5.1" + "typescript": "^3.9.10" }, "dependencies": { "@babel/core": "^7.4.5", @@ -39,7 +40,8 @@ "@babel/preset-typescript": "^7.3.3", "@types/node": "^12.0.7", "harmony-websocket": "^1.1.0", - "homebridge": "^1.1.16", - "request": "^2.88.0" + "reflect-metadata": "^0.1.13", + "request": "^2.88.0", + "tsyringe": "^4.6.0" } } diff --git a/src/DataProviders/HarmonyDataProvider.ts b/src/DataProviders/HarmonyDataProvider.ts index 150b39a..1273fdb 100644 --- a/src/DataProviders/HarmonyDataProvider.ts +++ b/src/DataProviders/HarmonyDataProvider.ts @@ -7,6 +7,9 @@ import { IHub } from "../Models/Config/IHub"; import { IDeviceConfig } from "../Models/Config/IDeviceConfig"; import { HarmonyDevice } from "../Models/HarmonyDevice"; import { HarmonyHub } from "../Models/HarmonyHub"; +import { IConfig } from "../Models/Config"; +import { inject, injectable } from "tsyringe"; +import { Logger, Logging } from "homebridge"; const Harmony = require("harmony-websocket"); @@ -21,8 +24,8 @@ interface IHarmonyDataProviderProps { matrix: IMatrix; } +@injectable() class HarmonyDataProvider extends EventEmitter { - private _log: any; private _hubsByDevice: { [deviceName: string]: string } = {}; private _hubs: { [hubName: string]: HarmonyHub } = {}; // private _devicesByHub: { [hubName: string]: { [deviceName: string]: HarmonyDevice } } = {}; @@ -32,16 +35,18 @@ class HarmonyDataProvider extends EventEmitter { private _matrix: IMatrix; - constructor(props: IHarmonyDataProviderProps) { + constructor( + @inject("IConfig") private _config: IConfig, + @inject("log") private _log: Logging + ) { super(); - this._log = props.log; - this._matrix = props.matrix; - props.deviceConfigs.forEach((deviceConfig: IDeviceConfig) => { + this._matrix = _config.Matrix; + _config.Devices.forEach((deviceConfig: IDeviceConfig) => { this._hubsByDevice[deviceConfig.Name] = deviceConfig.Hub; }); // this._deviceConfigs = props.deviceConfigs; - this.connect(props.hubs); + this.connect(_config.Hubs); } // public get devicesByHub(): { [hubName: string]: { [deviceName: string]: HarmonyDevice } } { @@ -104,7 +109,7 @@ class HarmonyDataProvider extends EventEmitter { controlUnitName: string, activity: IActivity ) => { - this._log( + this._log.info( `Starting activity ${activity.DisplayName} for controlUnit: ${controlUnitName}` ); let lastActivity: IActivity | undefined = undefined; @@ -127,7 +132,7 @@ class HarmonyDataProvider extends EventEmitter { devicesToTurnOn.map(async (device: HarmonyDevice) => { if (device && device.name) { if (!device.on) { - this._log(`Turning on device ${device.name}`); + this._log.info(`Turning on device ${device.name}`); await device.powerOn(); } } @@ -175,11 +180,12 @@ class HarmonyDataProvider extends EventEmitter { //Build potential list of devices to turn off if (lastActivity) { - let devicesToTurnOff: Array = lastActivity.DeviceSetupList.map( - (value: IDeviceSetupItem): HarmonyDevice => { - return this.getDeviceFromName(value.DeviceName); - } - ); + let devicesToTurnOff: Array = + lastActivity.DeviceSetupList.map( + (value: IDeviceSetupItem): HarmonyDevice => { + return this.getDeviceFromName(value.DeviceName); + } + ); //remove devices that will be used for next activity from list //delete array[index] is stupid because it just nulls out the index. But now i have to deal with nulls @@ -201,7 +207,7 @@ class HarmonyDataProvider extends EventEmitter { controlUnitName ); - this._log( + this._log.info( `Sanatized devices to turn off: ${JSON.stringify( devicesToTurnOff.map((e) => (e ? e.name : "")) )}` @@ -212,7 +218,7 @@ class HarmonyDataProvider extends EventEmitter { devicesToTurnOff.map(async (device: HarmonyDevice) => { if (device) { if (device.on) { - this._log(`Turning off device ${device.name}`); + this._log.info(`Turning off device ${device.name}`); await device.powerOff(); } } @@ -323,11 +329,10 @@ class HarmonyDataProvider extends EventEmitter { public getDeviceFromName(deviceName: string): HarmonyDevice { let device: HarmonyDevice | undefined; try { - device = this._hubs[this._hubsByDevice[deviceName]].getDeviceByName( - deviceName - ); + device = + this._hubs[this._hubsByDevice[deviceName]].getDeviceByName(deviceName); } catch (err) { - this._log(`Error retrieving device from hub: ${err}`); + this._log.info(`Error retrieving device from hub: ${err}`); } return device!; @@ -350,19 +355,17 @@ class HarmonyDataProvider extends EventEmitter { private connect = async (hubs: Array) => { let readyCount = 0; await Promise.all( - hubs.map( - async (hub: IHub): Promise => { - const newHarmonyHub = new HarmonyHub(hub.Name, hub.Ip, this._log); - this._hubs[hub.Name] = newHarmonyHub; - newHarmonyHub.on("Ready", () => { - readyCount++; - if (readyCount === Object.keys(this._hubs).length) { - this.emit("Ready"); - } - }); - await newHarmonyHub.initialize(); - } - ) + hubs.map(async (hub: IHub): Promise => { + const newHarmonyHub = new HarmonyHub(hub.Name, hub.Ip, this._log.info); + this._hubs[hub.Name] = newHarmonyHub; + newHarmonyHub.on("Ready", () => { + readyCount++; + if (readyCount === Object.keys(this._hubs).length) { + this.emit("Ready"); + } + }); + await newHarmonyHub.initialize(); + }) ); }; diff --git a/src/platform.ts b/src/platform.ts index 27c9144..552dc7c 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -1,3 +1,4 @@ +import "reflect-metadata"; import { API, Characteristic, @@ -12,9 +13,8 @@ import { Sequence } from "./Accessories/Sequence"; import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider"; import { IConfig, IControlUnit, IDeviceButton } from "./Models/Config"; import { ISequence } from "./Models/Config/ISequence"; -import { HarmonyDevice } from "./Models/HarmonyDevice"; -import { HarmonyHub } from "./Models/HarmonyHub"; import { PLATFORM_NAME, PLUGIN_NAME } from "./settings"; +import { container } from "tsyringe"; export class Platform implements DynamicPlatformPlugin { constructor( @@ -23,57 +23,58 @@ export class Platform implements DynamicPlatformPlugin { public readonly api: API ) { this.log.debug("Finished initializing platform:", config.name); - this.config = config as unknown as IConfig; + this.register(); + //construct data provider - const dataProvider = new HarmonyDataProvider({ - hubs: this.config.Hubs, - deviceConfigs: this.config.Devices, - matrix: this.config.Matrix, - log: this.log, - }); + // const dataProvider = new HarmonyDataProvider({ + // hubs: this.config.Hubs, + // deviceConfigs: this.config.Devices, + // matrix: this.config.Matrix, + // log: this.log, + // }); let didFinishLaunching = false; this.api.on("didFinishLaunching", async () => { log.debug("Executed didFinishLaunching callback"); + this.discoverControlUnitAccessories(); + this.discoverDeviceButtonAccessories(); + this.discoverSequenceAccessories(); + this.pruneAccessories(); didFinishLaunching = true; }); - this.dataProvider = null; + // this.dataProvider = null; if (this.config) { //construct data provider - this.dataProvider = new HarmonyDataProvider({ - hubs: this.config.Hubs, - deviceConfigs: this.config.Devices, - matrix: this.config.Matrix, - log: this.log, - }); - + // this.dataProvider = new HarmonyDataProvider({ + // hubs: this.config.Hubs, + // deviceConfigs: this.config.Devices, + // matrix: this.config.Matrix, + // log: this.log, + // }); //Emit devices if requested - - this.dataProvider.on("Ready", () => { - this.log.info("All hubs connected"); - this.discoverControlUnitAccessories(dataProvider); - this.discoverDeviceButtonAccessories(dataProvider); - this.discoverSequenceAccessories(dataProvider); - this.pruneAccessories(); - - if (this.config.EmitDevicesOnStartup) { - const hubs = this.dataProvider!.hubs; - Object.values(hubs).forEach((hub: HarmonyHub) => { - const deviceDictionary = hub.devices; - this.log.info(`${hub.hubName}`); - - Object.values(deviceDictionary).forEach((device: HarmonyDevice) => { - this.log.info(` ${device.name} : ${device.id}`); - Object.keys(device.commands).forEach((command: string) => { - this.log.info(` ${command}`); - }); - }); - }); - } - }); + // this.dataProvider.on("Ready", () => { + // this.log.info("All hubs connected"); + // this.discoverControlUnitAccessories(dataProvider); + // this.discoverDeviceButtonAccessories(dataProvider); + // this.discoverSequenceAccessories(dataProvider); + // this.pruneAccessories(); + // if (this.config.EmitDevicesOnStartup) { + // const hubs = this.dataProvider!.hubs; + // Object.values(hubs).forEach((hub: HarmonyHub) => { + // const deviceDictionary = hub.devices; + // this.log.info(`${hub.hubName}`); + // Object.values(deviceDictionary).forEach((device: HarmonyDevice) => { + // this.log.info(` ${device.name} : ${device.id}`); + // Object.keys(device.commands).forEach((command: string) => { + // this.log.info(` ${command}`); + // }); + // }); + // }); + // } + // }); } } @@ -84,15 +85,12 @@ export class Platform implements DynamicPlatformPlugin { // this is used to track restored cached accessories public readonly accessories: PlatformAccessory[] = []; public config: IConfig; - public dataProvider: HarmonyDataProvider | null; /** * Discover new control unit accessories * @param dataProvider */ - private discoverControlUnitAccessories( - dataProvider: HarmonyDataProvider - ): void { + private discoverControlUnitAccessories(): void { this.config.ControlUnits.forEach((unit: IControlUnit) => { const uuid = this.api.hap.uuid.generate(unit.DisplayName); const existingAccessory = this.accessories.find((e) => e.UUID === uuid); @@ -103,7 +101,12 @@ export class Platform implements DynamicPlatformPlugin { existingAccessory.displayName ); - new ControlUnit(this, existingAccessory, dataProvider, unit.Activities); + new ControlUnit( + this, + existingAccessory, + container.resolve(HarmonyDataProvider), + unit.Activities + ); this.api.publishExternalAccessories(PLUGIN_NAME, [existingAccessory]); console.log("Publishing external accessory: " + uuid); @@ -116,7 +119,12 @@ export class Platform implements DynamicPlatformPlugin { accessory.context["DeviceName"] = unit.DisplayName; accessory.context["Type"] = typeof ControlUnit.name; - new ControlUnit(this, accessory, dataProvider, unit.Activities); + new ControlUnit( + this, + accessory, + container.resolve(HarmonyDataProvider), + unit.Activities + ); this.api.publishExternalAccessories(PLUGIN_NAME, [accessory]); console.log("Publishing external accessory: " + uuid); @@ -128,9 +136,7 @@ export class Platform implements DynamicPlatformPlugin { * Discover new device button accessories * @param dataProvider */ - private discoverDeviceButtonAccessories( - dataProvider: HarmonyDataProvider - ): void { + private discoverDeviceButtonAccessories(): void { this.config.DeviceButtons.forEach((button: IDeviceButton) => { const uuid = this.api.hap.uuid.generate(button.DisplayName); const existingAccessory = this.accessories.find((e) => e.UUID === uuid); @@ -140,7 +146,12 @@ export class Platform implements DynamicPlatformPlugin { existingAccessory.displayName ); - new DeviceButton(this, existingAccessory, dataProvider, button); + new DeviceButton( + this, + existingAccessory, + container.resolve(HarmonyDataProvider), + button + ); this.api.updatePlatformAccessories([existingAccessory]); } else { this.log.info("Adding new accessory: " + button.DisplayName); @@ -151,7 +162,12 @@ export class Platform implements DynamicPlatformPlugin { accessory.context["DeviceName"] = button.DisplayName; accessory.context["Type"] = typeof DeviceButton; - new DeviceButton(this, accessory, dataProvider, button); + new DeviceButton( + this, + accessory, + container.resolve(HarmonyDataProvider), + button + ); this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [ accessory, @@ -165,7 +181,7 @@ export class Platform implements DynamicPlatformPlugin { * Discover new sequence accessories * @param dataProvider */ - private discoverSequenceAccessories(dataProvider: HarmonyDataProvider): void { + private discoverSequenceAccessories(): void { this.config.Sequences.forEach((sequence: ISequence) => { const uuid = this.api.hap.uuid.generate(sequence.DisplayName); const existingAccessory = this.accessories.find((e) => e.UUID === uuid); @@ -175,7 +191,12 @@ export class Platform implements DynamicPlatformPlugin { existingAccessory.displayName ); - new Sequence(this, existingAccessory, dataProvider, sequence); + new Sequence( + this, + existingAccessory, + container.resolve(HarmonyDataProvider), + sequence + ); this.api.updatePlatformAccessories([existingAccessory]); } else { this.log.info("Adding new accessory: " + sequence.DisplayName); @@ -186,7 +207,12 @@ export class Platform implements DynamicPlatformPlugin { accessory.context["DeviceName"] = sequence.DisplayName; accessory.context["Type"] = typeof "Sequence"; - new Sequence(this, accessory, dataProvider, sequence); + new Sequence( + this, + accessory, + container.resolve(HarmonyDataProvider), + sequence + ); this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [ accessory, @@ -222,4 +248,11 @@ export class Platform implements DynamicPlatformPlugin { // add the restored accessory to the accessories cache so we can track if it has already been registered this.accessories.push(accessory); } + + private register(): void { + container.register(Platform, { useValue: this }); + container.register("IConfig", { useValue: this.config }); + container.register("log", { useValue: this.log }); + container.registerSingleton(HarmonyDataProvider); + } } diff --git a/src/settings.ts b/src/settings.ts index 5daca0d..1841070 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -6,4 +6,4 @@ export const PLATFORM_NAME = "HarmonyHubMatrix"; /** * This must match the name of your plugin as defined the package.json */ -export const PLUGIN_NAME = "homebridge-harmony-control"; +export const PLUGIN_NAME = "@watsonb8/homebridge-harmony-control"; diff --git a/tsconfig.json b/tsconfig.json index 4e9341a..b86bfdd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,9 @@ "outDir": "./bin", "strict": true, "sourceMap": true, - "skipLibCheck": true + "skipLibCheck": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true }, "include": ["src"], "exclude": ["**/node_modules", "**/__tests__/*"] From c65853220eaefc692f7b787da371765c3a4f3868 Mon Sep 17 00:00:00 2001 From: Brandon Watson Date: Tue, 28 Dec 2021 16:37:40 -0500 Subject: [PATCH 2/7] Renamed files. Refactored control unit (untested) --- src/Accessories/ControlUnit.ts | 25 ++- src/Accessories/DeviceButton.ts | 4 +- src/Accessories/Sequence.ts | 6 +- src/Accessories/index.ts | 4 +- src/DataProviders/HarmonyDataProvider.ts | 35 ++- src/DataProviders/harmonyDataProvider2.ts | 105 +++++++++ src/DataProviders/stateDataProvider.ts | 31 +++ src/Models/Config/IActivity.ts | 11 - src/Models/Config/activity.ts | 10 + src/Models/Config/{IConfig.ts => config.ts} | 12 +- .../{IDeviceButton.ts => deviceButton.ts} | 0 .../{IDeviceConfig.ts => deviceConfig.ts} | 0 ...IDeviceSetupItem.ts => deviceSetupItem.ts} | 0 src/Models/Config/{IHub.ts => hub.ts} | 0 src/Models/Config/index.ts | 10 +- src/Models/Config/{IMatrix.ts => matrix.ts} | 0 .../Config/{ISequence.ts => sequence.ts} | 0 src/Models/HarmonyDevice.ts | 167 +++++++------- src/Models/HarmonyHub.ts | 138 ++++++------ src/Models/activityState.ts | 5 + src/Models/{IDevice.ts => device.ts} | 0 src/Models/index.ts | 2 +- src/Services/activityService.ts | 209 ++++++++++++++++++ src/Services/commandService.ts | 71 ++++++ src/Services/volumeService.ts | 44 ++++ src/Util/index.ts | 4 +- src/platform.ts | 63 ++---- 27 files changed, 707 insertions(+), 249 deletions(-) create mode 100644 src/DataProviders/harmonyDataProvider2.ts create mode 100644 src/DataProviders/stateDataProvider.ts delete mode 100644 src/Models/Config/IActivity.ts create mode 100644 src/Models/Config/activity.ts rename src/Models/Config/{IConfig.ts => config.ts} (58%) rename src/Models/Config/{IDeviceButton.ts => deviceButton.ts} (100%) rename src/Models/Config/{IDeviceConfig.ts => deviceConfig.ts} (100%) rename src/Models/Config/{IDeviceSetupItem.ts => deviceSetupItem.ts} (100%) rename src/Models/Config/{IHub.ts => hub.ts} (100%) rename src/Models/Config/{IMatrix.ts => matrix.ts} (100%) rename src/Models/Config/{ISequence.ts => sequence.ts} (100%) create mode 100644 src/Models/activityState.ts rename src/Models/{IDevice.ts => device.ts} (100%) create mode 100644 src/Services/activityService.ts create mode 100644 src/Services/commandService.ts create mode 100644 src/Services/volumeService.ts diff --git a/src/Accessories/ControlUnit.ts b/src/Accessories/ControlUnit.ts index bce2a5d..9dba6ac 100644 --- a/src/Accessories/ControlUnit.ts +++ b/src/Accessories/ControlUnit.ts @@ -1,8 +1,11 @@ import { PlatformAccessory, RemoteController, Service } from "homebridge"; -import HarmonyDataProvider from "../DataProviders/HarmonyDataProvider"; +import HarmonyDataProvider from "../DataProviders/harmonyDataProvider"; import { IActivity } from "../Models/Config"; import { Platform } from "../platform"; -import callbackify from "../Util/Callbackify"; +import { ActivityService } from "../Services/activityService"; +import { CommandService } from "../Services/commandService"; +import { VolumeService } from "../Services/volumeService"; +import callbackify from "../Util/callbackify"; /** * Enum describing remote key presses from homebridge. @@ -27,7 +30,9 @@ export class ControlUnit { constructor( private readonly _platform: Platform, private readonly _accessory: PlatformAccessory, - private _dataProvider: HarmonyDataProvider, + private _activityService: ActivityService, + private _commandService: CommandService, + private _volumeService: VolumeService, private _activities: Array ) { this._accessory @@ -129,11 +134,11 @@ export class ControlUnit { private onSetAccessoryActive = async (value: any) => { switch (value) { case 0: - this._dataProvider.powerOff(this._accessory.displayName); + this._activityService.stopCurrentActivity(this._accessory.displayName); break; //Turn on with first activity case 1: - this._dataProvider.powerOn( + this._activityService.startActivity( this._accessory.displayName, this._activities[0] ); @@ -155,14 +160,14 @@ export class ControlUnit { * Event handler for SET remote key */ private onSetRemoteKey = async (key: any) => { - this._dataProvider.sendKeyPress(this._accessory.displayName, key); + this._commandService.sendKeyPress(this._accessory.displayName, key); }; /** * Event handler for SET active identifier characteristic */ private onSetActiveIdentifier = async (identifier: any) => { - this._dataProvider.startActivity( + this._activityService.startActivity( this._accessory.displayName, this._activities[identifier] ); @@ -172,7 +177,7 @@ export class ControlUnit { * Event handler for GET active identifier characteristic */ private onGetActiveIdentifier = async () => { - let currentActivity: IActivity = this._dataProvider.getIsActive( + let currentActivity: IActivity = this._activityService.getIsActive( this._accessory.displayName )!; let identifier: number = 0; @@ -230,10 +235,10 @@ export class ControlUnit { private onSetVolumeSelector = async (value: any) => { switch (value) { case 0: - this._dataProvider.volumeUp(this._accessory.displayName); + this._volumeService.volumeUp(this._accessory.displayName); break; case 1: - this._dataProvider.volumeDown(this._accessory.displayName); + this._volumeService.volumeDown(this._accessory.displayName); break; } }; diff --git a/src/Accessories/DeviceButton.ts b/src/Accessories/DeviceButton.ts index cf02bf4..bbd7171 100644 --- a/src/Accessories/DeviceButton.ts +++ b/src/Accessories/DeviceButton.ts @@ -1,7 +1,7 @@ import { PlatformAccessory, Service } from "homebridge"; -import HarmonyDataProvider from "../DataProviders/HarmonyDataProvider"; +import HarmonyDataProvider from "../DataProviders/harmonyDataProvider"; import { IDeviceButton } from "../Models/Config"; -import { HarmonyDevice } from "../Models/HarmonyDevice"; +import { HarmonyDevice } from "../Models/harmonyDevice"; import { Platform } from "../platform"; export class DeviceButton { diff --git a/src/Accessories/Sequence.ts b/src/Accessories/Sequence.ts index ea6992c..c7b391d 100644 --- a/src/Accessories/Sequence.ts +++ b/src/Accessories/Sequence.ts @@ -5,9 +5,9 @@ import { PlatformAccessory, Service, } from "homebridge"; -import HarmonyDataProvider from "../DataProviders/HarmonyDataProvider"; -import { ISequence } from "../Models/Config/ISequence"; -import { HarmonyDevice } from "../Models/HarmonyDevice"; +import HarmonyDataProvider from "../DataProviders/harmonyDataProvider"; +import { ISequence } from "../Models/Config/sequence"; +import { HarmonyDevice } from "../Models/harmonyDevice"; import { Platform } from "../platform"; import { sleep } from "../Util"; diff --git a/src/Accessories/index.ts b/src/Accessories/index.ts index 4c98bbf..bf68e76 100644 --- a/src/Accessories/index.ts +++ b/src/Accessories/index.ts @@ -1,2 +1,2 @@ -export { ControlUnit } from './ControlUnit'; -export { DeviceButton } from './DeviceButton'; \ No newline at end of file +export { ControlUnit } from "./controlUnit"; +export { DeviceButton } from "./deviceButton"; diff --git a/src/DataProviders/HarmonyDataProvider.ts b/src/DataProviders/HarmonyDataProvider.ts index 1273fdb..2dbdd55 100644 --- a/src/DataProviders/HarmonyDataProvider.ts +++ b/src/DataProviders/HarmonyDataProvider.ts @@ -1,12 +1,12 @@ -import { IActivity } from "../Models/Config/IActivity"; -import { IDeviceSetupItem } from "../Models/Config/IDeviceSetupItem"; -import { IInput, IMatrix, IOutput } from "../Models/Config/IMatrix"; -import { RemoteKey } from "../Accessories/ControlUnit"; +import { IActivity } from "../Models/Config/activity"; +import { IDeviceSetupItem } from "../Models/Config/deviceSetupItem"; +import { IInput, IMatrix, IOutput } from "../Models/Config/matrix"; +import { RemoteKey } from "../Accessories/controlUnit"; import { EventEmitter } from "events"; -import { IHub } from "../Models/Config/IHub"; -import { IDeviceConfig } from "../Models/Config/IDeviceConfig"; -import { HarmonyDevice } from "../Models/HarmonyDevice"; -import { HarmonyHub } from "../Models/HarmonyHub"; +import { IHub } from "../Models/Config/hub"; +import { IDeviceConfig } from "../Models/Config/deviceConfig"; +import { HarmonyDevice } from "../Models/harmonyDevice"; +import { HarmonyHub } from "../Models/harmonyHub"; import { IConfig } from "../Models/Config"; import { inject, injectable } from "tsyringe"; import { Logger, Logging } from "homebridge"; @@ -47,6 +47,7 @@ class HarmonyDataProvider extends EventEmitter { // this._deviceConfigs = props.deviceConfigs; this.connect(_config.Hubs); + this.emitInfo(); } // public get devicesByHub(): { [hubName: string]: { [deviceName: string]: HarmonyDevice } } { @@ -402,6 +403,24 @@ class HarmonyDataProvider extends EventEmitter { return devicesToTurnOn; } + + private emitInfo(): void { + //Emit devices if requested + this._log.info("All hubs connected"); + if (this._config.EmitDevicesOnStartup) { + const hubs = this.hubs; + Object.values(hubs).forEach((hub: HarmonyHub) => { + const deviceDictionary = hub.devices; + this._log.info(`${hub.hubName}`); + Object.values(deviceDictionary).forEach((device: HarmonyDevice) => { + this._log.info(` ${device.name} : ${device.id}`); + Object.keys(device.commands).forEach((command: string) => { + this._log.info(` ${command}`); + }); + }); + }); + } + } } export default HarmonyDataProvider; diff --git a/src/DataProviders/harmonyDataProvider2.ts b/src/DataProviders/harmonyDataProvider2.ts new file mode 100644 index 0000000..5defa49 --- /dev/null +++ b/src/DataProviders/harmonyDataProvider2.ts @@ -0,0 +1,105 @@ +import { Logging } from "homebridge"; +import { inject } from "tsyringe"; +import { IConfig, IMatrix } from "../Models/Config"; +import { IDeviceConfig } from "../Models/Config/deviceConfig"; +import { IHub } from "../Models/Config/hub"; +import { HarmonyDevice } from "../Models/harmonyDevice"; +import { HarmonyHub } from "../Models/harmonyHub"; + +export class HarmonyDataProvider2 { + private _matrix: IMatrix; + private _hubs: { [hubName: string]: HarmonyHub } = {}; + private _hubsByDevice: { [deviceName: string]: string } = {}; + + constructor( + @inject("IConfig") private _config: IConfig, + @inject("log") private _log: Logging + ) { + this._matrix = _config.Matrix; + _config.Devices.forEach((deviceConfig: IDeviceConfig) => { + this._hubsByDevice[deviceConfig.Name] = deviceConfig.Hub; + }); + // this._deviceConfigs = props.deviceConfigs; + + this.connect(_config.Hubs); + this.emitInfo(); + } + + public async turnOnDevices(devices: Array): Promise { + await Promise.all( + devices.map(async (device: HarmonyDevice) => { + if (device && device.name) { + if (!device.on) { + this._log.info(`Turning on device ${device.name}`); + await device.powerOn(); + } + } + }) + ); + } + + public async turnOffDevices(devices: Array) { + await Promise.all( + //Turn off devices + devices.map(async (device: HarmonyDevice) => { + if (device) { + if (device.on) { + this._log.info(`Turning off device ${device.name}`); + await device.powerOff(); + } + } + }) + ); + } + + /** + * Get the IDevice by name. + * @param deviceName The device to retrieve. + */ + public getDeviceFromName(deviceName: string): HarmonyDevice { + let device: HarmonyDevice | undefined; + try { + device = + this._hubs[this._hubsByDevice[deviceName]].getDeviceByName(deviceName); + } catch (err) { + this._log.info(`Error retrieving device from hub: ${err}`); + } + + return device!; + } + + private connect = async (hubs: Array) => { + let readyCount = 0; + await Promise.all( + hubs.map(async (hub: IHub): Promise => { + const newHarmonyHub = new HarmonyHub(hub.Name, hub.Ip, this._log.info); + this._hubs[hub.Name] = newHarmonyHub; + newHarmonyHub.on("Ready", () => { + readyCount++; + if (readyCount === Object.keys(this._hubs).length) { + // this.emit("Ready"); + } + }); + await newHarmonyHub.initialize(); + }) + ); + }; + + private emitInfo(): void { + //Emit devices if requested + this._log.info("All hubs connected"); + if (this._config.EmitDevicesOnStartup) { + const hubs = this._hubs; + Object.values(hubs).forEach((hub: HarmonyHub) => { + const deviceDictionary = hub.devices; + this._log.info(`${hub.hubName}`); + Object.values(deviceDictionary).forEach((device: HarmonyDevice) => { + this._log.info(` ${device.name} : ${device.id}`); + Object.keys(device.commands).forEach((command: string) => { + this._log.info(` ${command}`); + }); + }); + }); + } + } +} diff --git a/src/DataProviders/stateDataProvider.ts b/src/DataProviders/stateDataProvider.ts new file mode 100644 index 0000000..0e9bc13 --- /dev/null +++ b/src/DataProviders/stateDataProvider.ts @@ -0,0 +1,31 @@ +import { inject, injectable } from "tsyringe"; +import { IActivityState } from "../Models/activityState"; +import { IActivity } from "../Models/Config"; + +@injectable() +export class StateDataProvider { + private _states: { + [controlUnitName: string]: IActivityState | undefined; + } = {}; + + public updateState(activity: IActivity, controlUnitName: string): void { + this._states[controlUnitName] = { currentActivity: activity }; + } + + public removeState(controlUnitName: string): void { + this._states[controlUnitName] = undefined; + } + + public getState(controlUnitName: string) { + if (!this._states[controlUnitName]) { + return undefined; + } + return this._states[controlUnitName]!.currentActivity; + } + + public get states(): { + [controlUnitName: string]: IActivityState | undefined; + } { + return this._states; + } +} diff --git a/src/Models/Config/IActivity.ts b/src/Models/Config/IActivity.ts deleted file mode 100644 index a151d25..0000000 --- a/src/Models/Config/IActivity.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { IDeviceSetupItem } from './IDeviceSetupItem'; - -export interface IActivity { - OutputDevice: string; - VolumeDevice: string; - ControlDevice: string; - DisplayName: string; - DeviceSetupList: Array; - UseMatrix: boolean; - -} \ No newline at end of file diff --git a/src/Models/Config/activity.ts b/src/Models/Config/activity.ts new file mode 100644 index 0000000..2dd52c0 --- /dev/null +++ b/src/Models/Config/activity.ts @@ -0,0 +1,10 @@ +import { IDeviceSetupItem } from "./deviceSetupItem"; + +export interface IActivity { + OutputDevice: string; + VolumeDevice: string; + ControlDevice: string; + DisplayName: string; + DeviceSetupList: Array; + UseMatrix: boolean; +} diff --git a/src/Models/Config/IConfig.ts b/src/Models/Config/config.ts similarity index 58% rename from src/Models/Config/IConfig.ts rename to src/Models/Config/config.ts index 95832e8..21094e7 100644 --- a/src/Models/Config/IConfig.ts +++ b/src/Models/Config/config.ts @@ -1,9 +1,9 @@ -import { IMatrix } from "./IMatrix"; -import { IActivity } from "./IActivity"; -import { IDeviceButton } from "./IDeviceButton"; -import { IDeviceConfig } from "./IDeviceConfig"; -import { IHub } from "./IHub"; -import { ISequence } from "./ISequence"; +import { IMatrix } from "./matrix"; +import { IActivity } from "./activity"; +import { IDeviceButton } from "./deviceButton"; +import { IDeviceConfig } from "./deviceConfig"; +import { IHub } from "./hub"; +import { ISequence } from "./sequence"; export interface IControlUnit { DisplayName: string; diff --git a/src/Models/Config/IDeviceButton.ts b/src/Models/Config/deviceButton.ts similarity index 100% rename from src/Models/Config/IDeviceButton.ts rename to src/Models/Config/deviceButton.ts diff --git a/src/Models/Config/IDeviceConfig.ts b/src/Models/Config/deviceConfig.ts similarity index 100% rename from src/Models/Config/IDeviceConfig.ts rename to src/Models/Config/deviceConfig.ts diff --git a/src/Models/Config/IDeviceSetupItem.ts b/src/Models/Config/deviceSetupItem.ts similarity index 100% rename from src/Models/Config/IDeviceSetupItem.ts rename to src/Models/Config/deviceSetupItem.ts diff --git a/src/Models/Config/IHub.ts b/src/Models/Config/hub.ts similarity index 100% rename from src/Models/Config/IHub.ts rename to src/Models/Config/hub.ts diff --git a/src/Models/Config/index.ts b/src/Models/Config/index.ts index 5bd6c00..da105a2 100644 --- a/src/Models/Config/index.ts +++ b/src/Models/Config/index.ts @@ -1,5 +1,5 @@ -export * from './IActivity'; -export * from './IConfig'; -export * from './IDeviceButton'; -export * from './IDeviceSetupItem'; -export * from './IMatrix'; \ No newline at end of file +export * from "./activity"; +export * from "./config"; +export * from "./deviceButton"; +export * from "./deviceSetupItem"; +export * from "./matrix"; diff --git a/src/Models/Config/IMatrix.ts b/src/Models/Config/matrix.ts similarity index 100% rename from src/Models/Config/IMatrix.ts rename to src/Models/Config/matrix.ts diff --git a/src/Models/Config/ISequence.ts b/src/Models/Config/sequence.ts similarity index 100% rename from src/Models/Config/ISequence.ts rename to src/Models/Config/sequence.ts diff --git a/src/Models/HarmonyDevice.ts b/src/Models/HarmonyDevice.ts index cecece4..7fed46d 100644 --- a/src/Models/HarmonyDevice.ts +++ b/src/Models/HarmonyDevice.ts @@ -1,94 +1,93 @@ -import { ICommand } from "./IDevice"; -import { sleep } from "../Util/Sleep"; +import { ICommand } from "./device"; +import { sleep } from "../Util/sleep"; export interface IHarmonyDeviceProps { - id: string; - name: string; - harmony: any; - log: any; - commands: { [name: string]: ICommand }; + id: string; + name: string; + harmony: any; + log: any; + commands: { [name: string]: ICommand }; } export class HarmonyDevice { - private _harmony: any; - private _log: any; - private _commands: { [name: string]: ICommand } = {}; - private _on: boolean; + private _harmony: any; + private _log: any; + private _commands: { [name: string]: ICommand } = {}; + private _on: boolean; - constructor(props: IHarmonyDeviceProps) { - this.id = props.id; - this.name = props.name; - this._harmony = props.harmony; - this._on = false; - this._commands = props.commands; + constructor(props: IHarmonyDeviceProps) { + this.id = props.id; + this.name = props.name; + this._harmony = props.harmony; + this._on = false; + this._commands = props.commands; + } + + public id: string; + public name: string; + + public get on(): boolean { + return this._on; + } + + public get commands(): { [name: string]: ICommand } { + return this._commands; + } + + //Define device methods + public supportsCommand(commandName: string): boolean { + let command = this._commands[commandName]; + return command ? true : false; + } + + public getCommand(commandName: string): ICommand { + return this._commands[commandName]; + } + + public async powerOn(): Promise { + let powerOnCommand: string = "Power On"; + let powerToggleCommand: string = "Power Toggle"; + if (this.supportsCommand(powerOnCommand)) { + await this.sendCommand(powerOnCommand); + this._on = true; + } else if (this.supportsCommand(powerToggleCommand)) { + await this.sendCommand(powerToggleCommand); + this._on = true; + } + } + + public async powerOff(): Promise { + let powerOffCommand: string = "Power Off"; + let powerToggleCommand: string = "Power Toggle"; + if (this.supportsCommand(powerOffCommand)) { + await this.sendCommand(powerOffCommand); + this._on = false; + } else if (this.supportsCommand(powerToggleCommand)) { + await this.sendCommand(powerToggleCommand); + this._on = false; + } + } + + public async sendCommand(commandName: string): Promise { + let command!: ICommand; + if (this.supportsCommand(commandName)) { + command = this.getCommand(commandName); } - public id: string; - public name: string; - - public get on(): boolean { - return this._on; - } - - public get commands(): { [name: string]: ICommand } { - return this._commands; - } - - //Define device methods - public supportsCommand(commandName: string): boolean { - let command = this._commands[commandName]; - return (command) ? true : false; - } - - public getCommand(commandName: string): ICommand { - return this._commands[commandName]; - } - - public async powerOn(): Promise { - let powerOnCommand: string = "Power On"; - let powerToggleCommand: string = "Power Toggle"; - if (this.supportsCommand(powerOnCommand)) { - await this.sendCommand(powerOnCommand); - this._on = true; - } else if (this.supportsCommand(powerToggleCommand)) { - await this.sendCommand(powerToggleCommand); - this._on = true; + try { + //Execute command + //HACK to fix Harmon Kardon receiver not turning off + if (command.command === "PowerOff") { + for (let i = 0; i < 2; i++) { + await this._harmony.sendCommand(JSON.stringify(command)); } + } + await this._harmony.sendCommand(JSON.stringify(command)); + + //Sleep + await sleep(800); + } catch (err) { + this._log(`ERROR - error sending command to harmony: ${err}`); } - - public async powerOff(): Promise { - let powerOffCommand: string = "Power Off"; - let powerToggleCommand: string = "Power Toggle"; - if (this.supportsCommand(powerOffCommand)) { - await this.sendCommand(powerOffCommand); - this._on = false; - } else if (this.supportsCommand(powerToggleCommand)) { - await this.sendCommand(powerToggleCommand); - this._on = false; - } - } - - public async sendCommand(commandName: string): Promise { - let command!: ICommand; - if (this.supportsCommand(commandName)) { - command = this.getCommand(commandName); - } - - try { - //Execute command - //HACK to fix Harmon Kardon receiver not turning off - if (command.command === "PowerOff") { - for (let i = 0; i < 2; i++) { - await this._harmony.sendCommand(JSON.stringify(command)); - } - - } - await this._harmony.sendCommand(JSON.stringify(command)); - - //Sleep - await sleep(800); - } catch (err) { - this._log(`ERROR - error sending command to harmony: ${err}`); - } - } -} \ No newline at end of file + } +} diff --git a/src/Models/HarmonyHub.ts b/src/Models/HarmonyHub.ts index e390f32..49c8b77 100644 --- a/src/Models/HarmonyHub.ts +++ b/src/Models/HarmonyHub.ts @@ -1,75 +1,75 @@ -import { HarmonyDevice } from './HarmonyDevice'; +import { HarmonyDevice } from "./harmonyDevice"; const Harmony = require("harmony-websocket"); -import { ICommand } from './IDevice'; -import { EventEmitter } from 'events'; - - +import { ICommand } from "./device"; +import { EventEmitter } from "events"; export class HarmonyHub extends EventEmitter { - private _devices: { [deviceName: string]: HarmonyDevice } = {} - private _ip: string; - private _harmony: any; - private _log: any; - private _name: string; + private _devices: { [deviceName: string]: HarmonyDevice } = {}; + private _ip: string; + private _harmony: any; + private _log: any; + private _name: string; - constructor(hubName: string, ipAddress: string, log: any) { - super(); - this._ip = ipAddress; - this._log = log; - this._name = hubName; + constructor(hubName: string, ipAddress: string, log: any) { + super(); + this._ip = ipAddress; + this._log = log; + this._name = hubName; + } + + public get devices(): { [deviceName: string]: HarmonyDevice } { + return this._devices; + } + + public get hubName(): string { + return this._name; + } + + public getDeviceByName = (deviceName: string): HarmonyDevice => { + return this._devices[deviceName]; + }; + + public initialize = async () => { + this._harmony = new Harmony(); + await this._harmony.connect(this._ip); + this._harmony.on("stateDigest", (data: any) => { + console.log(data); + }); + + this._harmony.on("automationState", (data: any) => { + console.log(data); + }); + + //Gather devices + let devices: any = await this._harmony.getDevices(); + try { + await Promise.all( + //Add each to dictionary + devices.map(async (dev: any) => { + //get commands + let commands: { [name: string]: ICommand } = {}; + let deviceCommands: any = await this._harmony.getDeviceCommands( + dev.id + ); + deviceCommands.forEach((command: any) => { + commands[command.label] = command.action; + }); + this._devices[dev.label] = new HarmonyDevice({ + id: dev.id, + name: dev.label, + commands: commands, + log: this._log, + harmony: this._harmony, + }); + }) + ); + this.emit("Ready"); + } catch (err) { + this._log(`ERROR - error connecting to harmony: ${err}`); } + }; - public get devices(): { [deviceName: string]: HarmonyDevice } { - return this._devices; - } - - public get hubName(): string { - return this._name; - } - - public getDeviceByName = (deviceName: string): HarmonyDevice => { - return this._devices[deviceName]; - } - - public initialize = async () => { - this._harmony = new Harmony(); - await this._harmony.connect(this._ip); - this._harmony.on('stateDigest', (data: any) => { - console.log(data); - }); - - this._harmony.on('automationState', (data: any) => { - console.log(data); - }); - - //Gather devices - let devices: any = await this._harmony.getDevices(); - try { - await Promise.all( - //Add each to dictionary - devices.map(async (dev: any) => { - //get commands - let commands: { [name: string]: ICommand } = {}; - let deviceCommands: any = await this._harmony.getDeviceCommands(dev.id); - deviceCommands.forEach((command: any) => { - commands[command.label] = command.action; - }); - this._devices[dev.label] = new HarmonyDevice({ - id: dev.id, - name: dev.label, - commands: commands, - log: this._log, - harmony: this._harmony - }); - })); - this.emit("Ready"); - - } catch (err) { - this._log(`ERROR - error connecting to harmony: ${err}`); - } - } - - private connect = async (): Promise => { - await this._harmony.Connect(this._ip); - } -} \ No newline at end of file + private connect = async (): Promise => { + await this._harmony.Connect(this._ip); + }; +} diff --git a/src/Models/activityState.ts b/src/Models/activityState.ts new file mode 100644 index 0000000..4887ea1 --- /dev/null +++ b/src/Models/activityState.ts @@ -0,0 +1,5 @@ +import { IActivity } from "./Config"; + +export interface IActivityState { + currentActivity: IActivity; +} diff --git a/src/Models/IDevice.ts b/src/Models/device.ts similarity index 100% rename from src/Models/IDevice.ts rename to src/Models/device.ts diff --git a/src/Models/index.ts b/src/Models/index.ts index a37e649..01a9ec2 100644 --- a/src/Models/index.ts +++ b/src/Models/index.ts @@ -1 +1 @@ -export * from './IDevice'; \ No newline at end of file +export * from "./device"; diff --git a/src/Services/activityService.ts b/src/Services/activityService.ts new file mode 100644 index 0000000..afb50ea --- /dev/null +++ b/src/Services/activityService.ts @@ -0,0 +1,209 @@ +import { Logging } from "homebridge"; +import { inject, injectable } from "tsyringe"; +import { HarmonyDataProvider2 } from "../DataProviders/harmonyDataProvider2"; +import { StateDataProvider } from "../DataProviders/stateDataProvider"; +import { IActivityState } from "../Models/activityState"; +import { + IActivity, + IConfig, + IDeviceSetupItem, + IInput, + IOutput, +} from "../Models/Config"; +import { HarmonyDevice } from "../Models/harmonyDevice"; + +@injectable() +export class ActivityService { + constructor( + @inject(HarmonyDataProvider2) + private _harmonyDataProvider: HarmonyDataProvider2, + @inject(StateDataProvider) private _stateDataProvider: StateDataProvider, + @inject("IConfig") private _config: IConfig, + @inject("log") private _log: Logging + ) {} + + public startActivity = async ( + controlUnitName: string, + activity: IActivity + ) => { + this._log.info( + `Starting activity ${activity.DisplayName} for controlUnit: ${controlUnitName}` + ); + let devicesToTurnOn: Array = this.getDevicesToTurnOn( + activity, + controlUnitName + ); + + await this._harmonyDataProvider.turnOnDevices(devicesToTurnOn); + + await this.assignDeviceInput(activity); + + await this.routeInputsToOutputs(activity); + + let lastActivity = this.getCurrentActivity(controlUnitName); + + let devicesToTurnOff: Array = this.getDevicesToTurnOff( + controlUnitName, + lastActivity, + activity + ); + await this._harmonyDataProvider.turnOffDevices(devicesToTurnOff); + + this._stateDataProvider.updateState(activity, controlUnitName); + }; + + public stopCurrentActivity = async ( + controlUnitName: string + ): Promise => { + if (!this.getCurrentActivity(controlUnitName)) { + return; + } + + let lastActivity: IActivity | undefined = + this.getCurrentActivity(controlUnitName); + + let devicesToTurnOff: Array = this.getDevicesToTurnOff( + controlUnitName, + lastActivity + ); + await this._harmonyDataProvider.turnOffDevices(devicesToTurnOff); + + this._stateDataProvider.removeState(controlUnitName); + }; + + public getCurrentActivity(controlUnitName: string): IActivity | undefined { + return this._stateDataProvider.getState(controlUnitName); + } + + /** + * Return if a control unit is active + * @param controlUnitName + */ + public getIsActive(controlUnitName: string): IActivity | undefined { + return this._stateDataProvider.getState(controlUnitName); + } + + /** + * Helper function to make sure no control unit depends on device list. + * @param devicesToTurnOn The list of devices to modify. + * @param controlUnitName The name of the control unit in question. + */ + private sanitizeDeviceList( + devicesToTurnOn: Array, + controlUnitName: string + ): Array { + for (let controlUnitKey in this._stateDataProvider.states) { + //Skip self + if (controlUnitKey === controlUnitName) { + continue; + } + let currentOtherState: IActivityState = + this._stateDataProvider.states[controlUnitKey]!; + + if (currentOtherState) { + currentOtherState.currentActivity.DeviceSetupList.forEach( + (value: IDeviceSetupItem) => { + //there are devices to remove + if (devicesToTurnOn.some((e) => e && e.name === value.DeviceName)) { + let deviceToRemove: HarmonyDevice = devicesToTurnOn.filter( + (i) => i.name === value.DeviceName + )[0]; + delete devicesToTurnOn[devicesToTurnOn.indexOf(deviceToRemove)]; + } + } + ); + } + } + + return devicesToTurnOn; + } + + private getDevicesToTurnOn( + activity: IActivity, + controlUnitName: string + ): Array { + let potentialDevices = this.buildPotentialDeviceList(activity); + return this.sanitizeDeviceList(potentialDevices, controlUnitName); + } + + private getDevicesToTurnOff( + controlUnitName: string, + lastActivity?: IActivity, + nextActivity?: IActivity + ) { + let potentialDevices = lastActivity + ? this.buildPotentialDeviceList(lastActivity) + : []; + + //remove devices that will be used for next activity from list + //delete array[index] is stupid because it just nulls out the index. But now i have to deal with nulls + if (nextActivity) { + potentialDevices.forEach((device: HarmonyDevice, index: number) => { + if ( + device && + device.name && + nextActivity.DeviceSetupList.some((e) => { + return e && e.DeviceName === device.name; + }) + ) { + delete potentialDevices[index]; + } + }); + } + + return this.sanitizeDeviceList(potentialDevices, controlUnitName); + } + + private buildPotentialDeviceList(activity: IActivity): HarmonyDevice[] { + return activity.DeviceSetupList.map( + (value: IDeviceSetupItem): HarmonyDevice => { + return this._harmonyDataProvider.getDeviceFromName(value.DeviceName); + } + ); + } + + private async assignDeviceInput(activity: IActivity): Promise { + await Promise.all( + activity.DeviceSetupList.map(async (value: IDeviceSetupItem) => { + let device: HarmonyDevice = this._harmonyDataProvider.getDeviceFromName( + value.DeviceName + ); + + if (device && device.supportsCommand(`Input${value.Input}`)) { + await device.sendCommand(`Input${value.Input}`); + } + }) + ); + } + + private async routeInputsToOutputs(activity: IActivity) { + if (activity.UseMatrix) { + //get input and output + let input: IInput = this._config.Matrix.Inputs.filter( + (e) => e.InputDevice === activity.ControlDevice + )[0]; + let output: IOutput = this._config.Matrix.Outputs.filter( + (e) => e.OutputDevice === activity.OutputDevice + )[0]; + + let inputCommandName: string = `In ${input.InputNumber}`; + let outputCommandName: string = `Out ${output.OutputLetter}`; + + let matrixDevice: HarmonyDevice = + this._harmonyDataProvider.getDeviceFromName( + this._config.Matrix.DeviceName + ); + + //Route hdmi + if ( + matrixDevice.supportsCommand(inputCommandName) && + matrixDevice.supportsCommand(outputCommandName) + ) { + await matrixDevice.sendCommand(outputCommandName); + await matrixDevice.sendCommand(inputCommandName); + await matrixDevice.sendCommand(outputCommandName); + await matrixDevice.sendCommand(inputCommandName); + } + } + } +} diff --git a/src/Services/commandService.ts b/src/Services/commandService.ts new file mode 100644 index 0000000..183d4f4 --- /dev/null +++ b/src/Services/commandService.ts @@ -0,0 +1,71 @@ +import { inject } from "tsyringe"; +import { RemoteKey } from "../Accessories/controlUnit"; +import { HarmonyDataProvider2 } from "../DataProviders/harmonyDataProvider2"; +import { StateDataProvider } from "../DataProviders/stateDataProvider"; +import { IConfig } from "../Models/Config"; +import { HarmonyDevice } from "../Models/harmonyDevice"; + +export class CommandService { + constructor( + @inject(StateDataProvider) private _stateDataProvider: StateDataProvider, + @inject(HarmonyDataProvider2) + private _harmonyDataProvider: HarmonyDataProvider2 + ) {} + + /** + * Send key press for current activity. + * + * @param controlUnitName The name of the control unit to act on. + * @param key The key to send. + */ + public sendKeyPress = async (controlUnitName: string, key: any) => { + let currentActivity = this._stateDataProvider.getState(controlUnitName); + if (currentActivity) { + let commandName: string = ""; + + let device: HarmonyDevice = this._harmonyDataProvider.getDeviceFromName( + currentActivity.ControlDevice + ); + switch (key) { + case RemoteKey.ARROW_UP: { + commandName = "Direction Up"; + break; + } + case RemoteKey.ARROW_DOWN: { + commandName = "Direction Down"; + break; + } + case RemoteKey.ARROW_LEFT: { + commandName = "Direction Left"; + break; + } + case RemoteKey.ARROW_RIGHT: { + commandName = "Direction Right"; + break; + } + case RemoteKey.SELECT: { + commandName = "Select"; + break; + } + case RemoteKey.PLAY_PAUSE: { + commandName = "Pause"; + break; + } + case RemoteKey.INFORMATION: { + commandName = "Menu"; + break; + } + case RemoteKey.BACK: { + commandName = "Back"; + break; + } + case RemoteKey.EXIT: { + commandName = "Back"; + break; + } + } + + await device.sendCommand(commandName); + } + }; +} diff --git a/src/Services/volumeService.ts b/src/Services/volumeService.ts new file mode 100644 index 0000000..8b66b2f --- /dev/null +++ b/src/Services/volumeService.ts @@ -0,0 +1,44 @@ +import { Logging } from "homebridge"; +import { inject } from "tsyringe"; +import { HarmonyDataProvider2 } from "../DataProviders/harmonyDataProvider2"; +import { StateDataProvider } from "../DataProviders/stateDataProvider"; +import { IConfig } from "../Models/Config"; +import { HarmonyDevice } from "../Models/harmonyDevice"; + +export class VolumeService { + constructor( + @inject(StateDataProvider) private _stateDataProvider: StateDataProvider, + @inject(HarmonyDataProvider2) + private _harmonyDataProvider: HarmonyDataProvider2 + ) {} + + /** + * Turn the volume up for the current running activity. + */ + public volumeUp = async (controlUnitName: string) => { + let volumeUpCommand: string = "Volume Up"; + let currentActivity = this._stateDataProvider.getState(controlUnitName); + if (currentActivity) { + let volumeDevice: HarmonyDevice = + this._harmonyDataProvider.getDeviceFromName( + currentActivity.VolumeDevice + ); + await volumeDevice.sendCommand(volumeUpCommand); + } + }; + + /** + * Volume down for current running activity. + */ + public volumeDown = async (controlUnitName: string) => { + let volumeDownCommand: string = "Volume Down"; + let currentActivity = this._stateDataProvider.getState(controlUnitName); + if (currentActivity) { + let volumeDevice: HarmonyDevice = + this._harmonyDataProvider.getDeviceFromName( + currentActivity.VolumeDevice + ); + await volumeDevice.sendCommand(volumeDownCommand); + } + }; +} diff --git a/src/Util/index.ts b/src/Util/index.ts index 19f0e10..fcebc7c 100644 --- a/src/Util/index.ts +++ b/src/Util/index.ts @@ -1,2 +1,2 @@ -export * from "./Callbackify"; -export * from "./Sleep"; +export * from "./callbackify"; +export * from "./sleep"; diff --git a/src/platform.ts b/src/platform.ts index 552dc7c..a747284 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -9,12 +9,17 @@ import { Service, } from "homebridge"; import { ControlUnit, DeviceButton } from "./Accessories"; -import { Sequence } from "./Accessories/Sequence"; -import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider"; +import { Sequence } from "./Accessories/sequence"; +import HarmonyDataProvider from "./DataProviders/harmonyDataProvider"; import { IConfig, IControlUnit, IDeviceButton } from "./Models/Config"; -import { ISequence } from "./Models/Config/ISequence"; +import { ISequence } from "./Models/Config/sequence"; import { PLATFORM_NAME, PLUGIN_NAME } from "./settings"; import { container } from "tsyringe"; +import { HarmonyDataProvider2 } from "./DataProviders/harmonyDataProvider2"; +import { StateDataProvider } from "./DataProviders/stateDataProvider"; +import { CommandService } from "./Services/commandService"; +import { ActivityService } from "./Services/activityService"; +import { VolumeService } from "./Services/volumeService"; export class Platform implements DynamicPlatformPlugin { constructor( @@ -26,14 +31,6 @@ export class Platform implements DynamicPlatformPlugin { this.config = config as unknown as IConfig; this.register(); - //construct data provider - // const dataProvider = new HarmonyDataProvider({ - // hubs: this.config.Hubs, - // deviceConfigs: this.config.Devices, - // matrix: this.config.Matrix, - // log: this.log, - // }); - let didFinishLaunching = false; this.api.on("didFinishLaunching", async () => { log.debug("Executed didFinishLaunching callback"); @@ -43,39 +40,6 @@ export class Platform implements DynamicPlatformPlugin { this.pruneAccessories(); didFinishLaunching = true; }); - - // this.dataProvider = null; - - if (this.config) { - //construct data provider - // this.dataProvider = new HarmonyDataProvider({ - // hubs: this.config.Hubs, - // deviceConfigs: this.config.Devices, - // matrix: this.config.Matrix, - // log: this.log, - // }); - //Emit devices if requested - // this.dataProvider.on("Ready", () => { - // this.log.info("All hubs connected"); - // this.discoverControlUnitAccessories(dataProvider); - // this.discoverDeviceButtonAccessories(dataProvider); - // this.discoverSequenceAccessories(dataProvider); - // this.pruneAccessories(); - // if (this.config.EmitDevicesOnStartup) { - // const hubs = this.dataProvider!.hubs; - // Object.values(hubs).forEach((hub: HarmonyHub) => { - // const deviceDictionary = hub.devices; - // this.log.info(`${hub.hubName}`); - // Object.values(deviceDictionary).forEach((device: HarmonyDevice) => { - // this.log.info(` ${device.name} : ${device.id}`); - // Object.keys(device.commands).forEach((command: string) => { - // this.log.info(` ${command}`); - // }); - // }); - // }); - // } - // }); - } } public readonly Service: typeof Service = this.api.hap.Service; @@ -104,7 +68,9 @@ export class Platform implements DynamicPlatformPlugin { new ControlUnit( this, existingAccessory, - container.resolve(HarmonyDataProvider), + container.resolve(ActivityService), + container.resolve(CommandService), + container.resolve(VolumeService), unit.Activities ); @@ -122,7 +88,9 @@ export class Platform implements DynamicPlatformPlugin { new ControlUnit( this, accessory, - container.resolve(HarmonyDataProvider), + container.resolve(ActivityService), + container.resolve(CommandService), + container.resolve(VolumeService), unit.Activities ); @@ -253,6 +221,9 @@ export class Platform implements DynamicPlatformPlugin { container.register(Platform, { useValue: this }); container.register("IConfig", { useValue: this.config }); container.register("log", { useValue: this.log }); + container.registerSingleton(StateDataProvider); + container.registerSingleton(CommandService); container.registerSingleton(HarmonyDataProvider); + container.registerSingleton(HarmonyDataProvider2); } } From 4fd9341cea577a2d7f689dff963a3816eeb44298 Mon Sep 17 00:00:00 2001 From: Brandon Watson Date: Tue, 28 Dec 2021 18:03:51 -0500 Subject: [PATCH 3/7] Update files --- .../{ControlUnit.ts => controlUnit.ts} | 12 +++++------ .../{DeviceButton.ts => deviceButton.ts} | 6 +++--- src/Accessories/{Sequence.ts => sequence.ts} | 8 ++++---- ...DataProvider.ts => harmonyDataProvider.ts} | 18 ++++++++--------- src/DataProviders/harmonyDataProvider2.ts | 10 +++++----- src/DataProviders/stateDataProvider.ts | 4 ++-- src/Models/HarmonyDevice.ts | 2 +- src/Models/activityState.ts | 2 +- src/Services/activityService.ts | 10 +++++----- src/Services/commandService.ts | 10 +++++----- src/Services/volumeService.ts | 8 ++++---- src/Util/{Callbackify.ts => callbackify.ts} | 0 src/platform.ts | 20 +++++++++---------- 13 files changed, 55 insertions(+), 55 deletions(-) rename src/Accessories/{ControlUnit.ts => controlUnit.ts} (95%) rename src/Accessories/{DeviceButton.ts => deviceButton.ts} (95%) rename src/Accessories/{Sequence.ts => sequence.ts} (93%) rename src/DataProviders/{HarmonyDataProvider.ts => harmonyDataProvider.ts} (96%) rename src/Util/{Callbackify.ts => callbackify.ts} (100%) diff --git a/src/Accessories/ControlUnit.ts b/src/Accessories/controlUnit.ts similarity index 95% rename from src/Accessories/ControlUnit.ts rename to src/Accessories/controlUnit.ts index 9dba6ac..4c69b33 100644 --- a/src/Accessories/ControlUnit.ts +++ b/src/Accessories/controlUnit.ts @@ -1,11 +1,11 @@ import { PlatformAccessory, RemoteController, Service } from "homebridge"; -import HarmonyDataProvider from "../DataProviders/harmonyDataProvider"; -import { IActivity } from "../Models/Config"; +import HarmonyDataProvider from "../dataProviders/harmonyDataProvider"; +import { IActivity } from "../models/config"; import { Platform } from "../platform"; -import { ActivityService } from "../Services/activityService"; -import { CommandService } from "../Services/commandService"; -import { VolumeService } from "../Services/volumeService"; -import callbackify from "../Util/callbackify"; +import { ActivityService } from "../services/activityService"; +import { CommandService } from "../services/commandService"; +import { VolumeService } from "../services/volumeService"; +import callbackify from "../util/callbackify"; /** * Enum describing remote key presses from homebridge. diff --git a/src/Accessories/DeviceButton.ts b/src/Accessories/deviceButton.ts similarity index 95% rename from src/Accessories/DeviceButton.ts rename to src/Accessories/deviceButton.ts index bbd7171..1c363d2 100644 --- a/src/Accessories/DeviceButton.ts +++ b/src/Accessories/deviceButton.ts @@ -1,7 +1,7 @@ import { PlatformAccessory, Service } from "homebridge"; -import HarmonyDataProvider from "../DataProviders/harmonyDataProvider"; -import { IDeviceButton } from "../Models/Config"; -import { HarmonyDevice } from "../Models/harmonyDevice"; +import HarmonyDataProvider from "../dataProviders/harmonyDataProvider"; +import { IDeviceButton } from "../models/config"; +import { HarmonyDevice } from "../models/harmonyDevice"; import { Platform } from "../platform"; export class DeviceButton { diff --git a/src/Accessories/Sequence.ts b/src/Accessories/sequence.ts similarity index 93% rename from src/Accessories/Sequence.ts rename to src/Accessories/sequence.ts index c7b391d..7b05620 100644 --- a/src/Accessories/Sequence.ts +++ b/src/Accessories/sequence.ts @@ -5,11 +5,11 @@ import { PlatformAccessory, Service, } from "homebridge"; -import HarmonyDataProvider from "../DataProviders/harmonyDataProvider"; -import { ISequence } from "../Models/Config/sequence"; -import { HarmonyDevice } from "../Models/harmonyDevice"; +import HarmonyDataProvider from "../dataProviders/harmonyDataProvider"; +import { ISequence } from "../models/config/sequence"; +import { HarmonyDevice } from "../models/harmonyDevice"; import { Platform } from "../platform"; -import { sleep } from "../Util"; +import { sleep } from "../util"; export class Sequence { private _devices: { [deviceName: string]: HarmonyDevice }; diff --git a/src/DataProviders/HarmonyDataProvider.ts b/src/DataProviders/harmonyDataProvider.ts similarity index 96% rename from src/DataProviders/HarmonyDataProvider.ts rename to src/DataProviders/harmonyDataProvider.ts index 2dbdd55..63ff8ac 100644 --- a/src/DataProviders/HarmonyDataProvider.ts +++ b/src/DataProviders/harmonyDataProvider.ts @@ -1,13 +1,13 @@ -import { IActivity } from "../Models/Config/activity"; -import { IDeviceSetupItem } from "../Models/Config/deviceSetupItem"; -import { IInput, IMatrix, IOutput } from "../Models/Config/matrix"; -import { RemoteKey } from "../Accessories/controlUnit"; +import { IActivity } from "../models/config/activity"; +import { IDeviceSetupItem } from "../models/config/deviceSetupItem"; +import { IInput, IMatrix, IOutput } from "../models/config/matrix"; +import { RemoteKey } from "../accessories/controlUnit"; import { EventEmitter } from "events"; -import { IHub } from "../Models/Config/hub"; -import { IDeviceConfig } from "../Models/Config/deviceConfig"; -import { HarmonyDevice } from "../Models/harmonyDevice"; -import { HarmonyHub } from "../Models/harmonyHub"; -import { IConfig } from "../Models/Config"; +import { IHub } from "../models/config/hub"; +import { IDeviceConfig } from "../models/config/deviceConfig"; +import { HarmonyDevice } from "../models/harmonyDevice"; +import { HarmonyHub } from "../models/harmonyHub"; +import { IConfig } from "../models/config"; import { inject, injectable } from "tsyringe"; import { Logger, Logging } from "homebridge"; diff --git a/src/DataProviders/harmonyDataProvider2.ts b/src/DataProviders/harmonyDataProvider2.ts index 5defa49..c6705cf 100644 --- a/src/DataProviders/harmonyDataProvider2.ts +++ b/src/DataProviders/harmonyDataProvider2.ts @@ -1,10 +1,10 @@ import { Logging } from "homebridge"; import { inject } from "tsyringe"; -import { IConfig, IMatrix } from "../Models/Config"; -import { IDeviceConfig } from "../Models/Config/deviceConfig"; -import { IHub } from "../Models/Config/hub"; -import { HarmonyDevice } from "../Models/harmonyDevice"; -import { HarmonyHub } from "../Models/harmonyHub"; +import { IConfig, IMatrix } from "../models/config"; +import { IDeviceConfig } from "../models/config/deviceConfig"; +import { IHub } from "../models/config/hub"; +import { HarmonyDevice } from "../models/harmonyDevice"; +import { HarmonyHub } from "../models/harmonyHub"; export class HarmonyDataProvider2 { private _matrix: IMatrix; diff --git a/src/DataProviders/stateDataProvider.ts b/src/DataProviders/stateDataProvider.ts index 0e9bc13..e00860b 100644 --- a/src/DataProviders/stateDataProvider.ts +++ b/src/DataProviders/stateDataProvider.ts @@ -1,6 +1,6 @@ import { inject, injectable } from "tsyringe"; -import { IActivityState } from "../Models/activityState"; -import { IActivity } from "../Models/Config"; +import { IActivityState } from "../models/activityState"; +import { IActivity } from "../models/config"; @injectable() export class StateDataProvider { diff --git a/src/Models/HarmonyDevice.ts b/src/Models/HarmonyDevice.ts index 7fed46d..1b6c394 100644 --- a/src/Models/HarmonyDevice.ts +++ b/src/Models/HarmonyDevice.ts @@ -1,5 +1,5 @@ import { ICommand } from "./device"; -import { sleep } from "../Util/sleep"; +import { sleep } from "../util/sleep"; export interface IHarmonyDeviceProps { id: string; diff --git a/src/Models/activityState.ts b/src/Models/activityState.ts index 4887ea1..96db22e 100644 --- a/src/Models/activityState.ts +++ b/src/Models/activityState.ts @@ -1,4 +1,4 @@ -import { IActivity } from "./Config"; +import { IActivity } from "./config"; export interface IActivityState { currentActivity: IActivity; diff --git a/src/Services/activityService.ts b/src/Services/activityService.ts index afb50ea..adf6910 100644 --- a/src/Services/activityService.ts +++ b/src/Services/activityService.ts @@ -1,16 +1,16 @@ import { Logging } from "homebridge"; import { inject, injectable } from "tsyringe"; -import { HarmonyDataProvider2 } from "../DataProviders/harmonyDataProvider2"; -import { StateDataProvider } from "../DataProviders/stateDataProvider"; -import { IActivityState } from "../Models/activityState"; +import { HarmonyDataProvider2 } from "../dataProviders/harmonyDataProvider2"; +import { StateDataProvider } from "../dataProviders/stateDataProvider"; +import { IActivityState } from "../models/activityState"; import { IActivity, IConfig, IDeviceSetupItem, IInput, IOutput, -} from "../Models/Config"; -import { HarmonyDevice } from "../Models/harmonyDevice"; +} from "../models/config"; +import { HarmonyDevice } from "../models/harmonyDevice"; @injectable() export class ActivityService { diff --git a/src/Services/commandService.ts b/src/Services/commandService.ts index 183d4f4..d1fc2fd 100644 --- a/src/Services/commandService.ts +++ b/src/Services/commandService.ts @@ -1,9 +1,9 @@ import { inject } from "tsyringe"; -import { RemoteKey } from "../Accessories/controlUnit"; -import { HarmonyDataProvider2 } from "../DataProviders/harmonyDataProvider2"; -import { StateDataProvider } from "../DataProviders/stateDataProvider"; -import { IConfig } from "../Models/Config"; -import { HarmonyDevice } from "../Models/harmonyDevice"; +import { RemoteKey } from "../accessories/controlUnit"; +import { HarmonyDataProvider2 } from "../dataProviders/harmonyDataProvider2"; +import { StateDataProvider } from "../dataProviders/stateDataProvider"; +import { IConfig } from "../models/config"; +import { HarmonyDevice } from "../models/harmonyDevice"; export class CommandService { constructor( diff --git a/src/Services/volumeService.ts b/src/Services/volumeService.ts index 8b66b2f..917d404 100644 --- a/src/Services/volumeService.ts +++ b/src/Services/volumeService.ts @@ -1,9 +1,9 @@ import { Logging } from "homebridge"; import { inject } from "tsyringe"; -import { HarmonyDataProvider2 } from "../DataProviders/harmonyDataProvider2"; -import { StateDataProvider } from "../DataProviders/stateDataProvider"; -import { IConfig } from "../Models/Config"; -import { HarmonyDevice } from "../Models/harmonyDevice"; +import { HarmonyDataProvider2 } from "../dataProviders/harmonyDataProvider2"; +import { StateDataProvider } from "../dataProviders/stateDataProvider"; +import { IConfig } from "../models/config"; +import { HarmonyDevice } from "../models/harmonyDevice"; export class VolumeService { constructor( diff --git a/src/Util/Callbackify.ts b/src/Util/callbackify.ts similarity index 100% rename from src/Util/Callbackify.ts rename to src/Util/callbackify.ts diff --git a/src/platform.ts b/src/platform.ts index a747284..52e3942 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -8,18 +8,18 @@ import { PlatformConfig, Service, } from "homebridge"; -import { ControlUnit, DeviceButton } from "./Accessories"; -import { Sequence } from "./Accessories/sequence"; -import HarmonyDataProvider from "./DataProviders/harmonyDataProvider"; -import { IConfig, IControlUnit, IDeviceButton } from "./Models/Config"; -import { ISequence } from "./Models/Config/sequence"; +import { ControlUnit, DeviceButton } from "./accessories"; +import { Sequence } from "./accessories/sequence"; +import HarmonyDataProvider from "./dataProviders/harmonyDataProvider"; +import { IConfig, IControlUnit, IDeviceButton } from "./models/config"; +import { ISequence } from "./models/config/sequence"; import { PLATFORM_NAME, PLUGIN_NAME } from "./settings"; import { container } from "tsyringe"; -import { HarmonyDataProvider2 } from "./DataProviders/harmonyDataProvider2"; -import { StateDataProvider } from "./DataProviders/stateDataProvider"; -import { CommandService } from "./Services/commandService"; -import { ActivityService } from "./Services/activityService"; -import { VolumeService } from "./Services/volumeService"; +import { HarmonyDataProvider2 } from "./dataProviders/harmonyDataProvider2"; +import { StateDataProvider } from "./dataProviders/stateDataProvider"; +import { CommandService } from "./services/commandService"; +import { ActivityService } from "./services/activityService"; +import { VolumeService } from "./services/volumeService"; export class Platform implements DynamicPlatformPlugin { constructor( From 506c17074648973b4f105470134367c0d03265a6 Mon Sep 17 00:00:00 2001 From: Brandon Watson Date: Tue, 28 Dec 2021 18:14:23 -0500 Subject: [PATCH 4/7] Renaming files --- src/{Accessories => accessories}/controlUnit.ts | 0 src/{Accessories => accessories}/deviceButton.ts | 0 src/{Accessories => accessories}/index.ts | 0 src/{Accessories => accessories}/sequence.ts | 0 src/{DataProviders => dataProviders}/harmonyDataProvider.ts | 0 src/{DataProviders => dataProviders}/harmonyDataProvider2.ts | 0 src/{DataProviders => dataProviders}/stateDataProvider.ts | 0 src/{Models => models}/activityState.ts | 0 src/{Models/Config => models/config}/activity.ts | 0 src/{Models/Config => models/config}/config.ts | 0 src/{Models/Config => models/config}/deviceButton.ts | 0 src/{Models/Config => models/config}/deviceConfig.ts | 0 src/{Models/Config => models/config}/deviceSetupItem.ts | 0 src/{Models/Config => models/config}/hub.ts | 0 src/{Models/Config => models/config}/index.ts | 0 src/{Models/Config => models/config}/matrix.ts | 0 src/{Models/Config => models/config}/sequence.ts | 0 src/{Models => models}/device.ts | 0 src/{Models/HarmonyDevice.ts => models/harmonyDevice.ts} | 0 src/{Models/HarmonyHub.ts => models/harmonyHub.ts} | 0 src/{Models => models}/index.ts | 0 src/{Services => services}/activityService.ts | 0 src/{Services => services}/commandService.ts | 0 src/{Services => services}/volumeService.ts | 0 src/{Util => util}/callbackify.ts | 0 src/{Util => util}/index.ts | 0 src/{Util/Sleep.ts => util/sleep.ts} | 0 27 files changed, 0 insertions(+), 0 deletions(-) rename src/{Accessories => accessories}/controlUnit.ts (100%) rename src/{Accessories => accessories}/deviceButton.ts (100%) rename src/{Accessories => accessories}/index.ts (100%) rename src/{Accessories => accessories}/sequence.ts (100%) rename src/{DataProviders => dataProviders}/harmonyDataProvider.ts (100%) rename src/{DataProviders => dataProviders}/harmonyDataProvider2.ts (100%) rename src/{DataProviders => dataProviders}/stateDataProvider.ts (100%) rename src/{Models => models}/activityState.ts (100%) rename src/{Models/Config => models/config}/activity.ts (100%) rename src/{Models/Config => models/config}/config.ts (100%) rename src/{Models/Config => models/config}/deviceButton.ts (100%) rename src/{Models/Config => models/config}/deviceConfig.ts (100%) rename src/{Models/Config => models/config}/deviceSetupItem.ts (100%) rename src/{Models/Config => models/config}/hub.ts (100%) rename src/{Models/Config => models/config}/index.ts (100%) rename src/{Models/Config => models/config}/matrix.ts (100%) rename src/{Models/Config => models/config}/sequence.ts (100%) rename src/{Models => models}/device.ts (100%) rename src/{Models/HarmonyDevice.ts => models/harmonyDevice.ts} (100%) rename src/{Models/HarmonyHub.ts => models/harmonyHub.ts} (100%) rename src/{Models => models}/index.ts (100%) rename src/{Services => services}/activityService.ts (100%) rename src/{Services => services}/commandService.ts (100%) rename src/{Services => services}/volumeService.ts (100%) rename src/{Util => util}/callbackify.ts (100%) rename src/{Util => util}/index.ts (100%) rename src/{Util/Sleep.ts => util/sleep.ts} (100%) diff --git a/src/Accessories/controlUnit.ts b/src/accessories/controlUnit.ts similarity index 100% rename from src/Accessories/controlUnit.ts rename to src/accessories/controlUnit.ts diff --git a/src/Accessories/deviceButton.ts b/src/accessories/deviceButton.ts similarity index 100% rename from src/Accessories/deviceButton.ts rename to src/accessories/deviceButton.ts diff --git a/src/Accessories/index.ts b/src/accessories/index.ts similarity index 100% rename from src/Accessories/index.ts rename to src/accessories/index.ts diff --git a/src/Accessories/sequence.ts b/src/accessories/sequence.ts similarity index 100% rename from src/Accessories/sequence.ts rename to src/accessories/sequence.ts diff --git a/src/DataProviders/harmonyDataProvider.ts b/src/dataProviders/harmonyDataProvider.ts similarity index 100% rename from src/DataProviders/harmonyDataProvider.ts rename to src/dataProviders/harmonyDataProvider.ts diff --git a/src/DataProviders/harmonyDataProvider2.ts b/src/dataProviders/harmonyDataProvider2.ts similarity index 100% rename from src/DataProviders/harmonyDataProvider2.ts rename to src/dataProviders/harmonyDataProvider2.ts diff --git a/src/DataProviders/stateDataProvider.ts b/src/dataProviders/stateDataProvider.ts similarity index 100% rename from src/DataProviders/stateDataProvider.ts rename to src/dataProviders/stateDataProvider.ts diff --git a/src/Models/activityState.ts b/src/models/activityState.ts similarity index 100% rename from src/Models/activityState.ts rename to src/models/activityState.ts diff --git a/src/Models/Config/activity.ts b/src/models/config/activity.ts similarity index 100% rename from src/Models/Config/activity.ts rename to src/models/config/activity.ts diff --git a/src/Models/Config/config.ts b/src/models/config/config.ts similarity index 100% rename from src/Models/Config/config.ts rename to src/models/config/config.ts diff --git a/src/Models/Config/deviceButton.ts b/src/models/config/deviceButton.ts similarity index 100% rename from src/Models/Config/deviceButton.ts rename to src/models/config/deviceButton.ts diff --git a/src/Models/Config/deviceConfig.ts b/src/models/config/deviceConfig.ts similarity index 100% rename from src/Models/Config/deviceConfig.ts rename to src/models/config/deviceConfig.ts diff --git a/src/Models/Config/deviceSetupItem.ts b/src/models/config/deviceSetupItem.ts similarity index 100% rename from src/Models/Config/deviceSetupItem.ts rename to src/models/config/deviceSetupItem.ts diff --git a/src/Models/Config/hub.ts b/src/models/config/hub.ts similarity index 100% rename from src/Models/Config/hub.ts rename to src/models/config/hub.ts diff --git a/src/Models/Config/index.ts b/src/models/config/index.ts similarity index 100% rename from src/Models/Config/index.ts rename to src/models/config/index.ts diff --git a/src/Models/Config/matrix.ts b/src/models/config/matrix.ts similarity index 100% rename from src/Models/Config/matrix.ts rename to src/models/config/matrix.ts diff --git a/src/Models/Config/sequence.ts b/src/models/config/sequence.ts similarity index 100% rename from src/Models/Config/sequence.ts rename to src/models/config/sequence.ts diff --git a/src/Models/device.ts b/src/models/device.ts similarity index 100% rename from src/Models/device.ts rename to src/models/device.ts diff --git a/src/Models/HarmonyDevice.ts b/src/models/harmonyDevice.ts similarity index 100% rename from src/Models/HarmonyDevice.ts rename to src/models/harmonyDevice.ts diff --git a/src/Models/HarmonyHub.ts b/src/models/harmonyHub.ts similarity index 100% rename from src/Models/HarmonyHub.ts rename to src/models/harmonyHub.ts diff --git a/src/Models/index.ts b/src/models/index.ts similarity index 100% rename from src/Models/index.ts rename to src/models/index.ts diff --git a/src/Services/activityService.ts b/src/services/activityService.ts similarity index 100% rename from src/Services/activityService.ts rename to src/services/activityService.ts diff --git a/src/Services/commandService.ts b/src/services/commandService.ts similarity index 100% rename from src/Services/commandService.ts rename to src/services/commandService.ts diff --git a/src/Services/volumeService.ts b/src/services/volumeService.ts similarity index 100% rename from src/Services/volumeService.ts rename to src/services/volumeService.ts diff --git a/src/Util/callbackify.ts b/src/util/callbackify.ts similarity index 100% rename from src/Util/callbackify.ts rename to src/util/callbackify.ts diff --git a/src/Util/index.ts b/src/util/index.ts similarity index 100% rename from src/Util/index.ts rename to src/util/index.ts diff --git a/src/Util/Sleep.ts b/src/util/sleep.ts similarity index 100% rename from src/Util/Sleep.ts rename to src/util/sleep.ts From c22a8a032557685cce1e4fe055a4ef9851823922 Mon Sep 17 00:00:00 2001 From: Brandon Watson Date: Tue, 28 Dec 2021 18:38:08 -0500 Subject: [PATCH 5/7] Successfully split up data provider --- src/accessories/controlUnit.ts | 3 +- src/accessories/deviceButton.ts | 2 +- src/accessories/sequence.ts | 2 +- src/dataProviders/harmonyDataProvider.ts | 360 ++-------------------- src/dataProviders/harmonyDataProvider2.ts | 105 ------- src/platform.ts | 7 +- src/services/activityService.ts | 6 +- src/services/commandService.ts | 10 +- src/services/volumeService.ts | 11 +- 9 files changed, 38 insertions(+), 468 deletions(-) delete mode 100644 src/dataProviders/harmonyDataProvider2.ts diff --git a/src/accessories/controlUnit.ts b/src/accessories/controlUnit.ts index 4c69b33..219ea31 100644 --- a/src/accessories/controlUnit.ts +++ b/src/accessories/controlUnit.ts @@ -1,5 +1,4 @@ import { PlatformAccessory, RemoteController, Service } from "homebridge"; -import HarmonyDataProvider from "../dataProviders/harmonyDataProvider"; import { IActivity } from "../models/config"; import { Platform } from "../platform"; import { ActivityService } from "../services/activityService"; @@ -151,7 +150,7 @@ export class ControlUnit { */ private onGetAccessoryActive = async () => { //@ts-ignore - return this._dataProvider.getIsActive(this._accessory.displayName) + return this._activityService.getIsActive(this._accessory.displayName) ? this._platform.Characteristic.Active.ACTIVE : this._platform.Characteristic.Active.INACTIVE; }; diff --git a/src/accessories/deviceButton.ts b/src/accessories/deviceButton.ts index 1c363d2..b456e48 100644 --- a/src/accessories/deviceButton.ts +++ b/src/accessories/deviceButton.ts @@ -1,5 +1,5 @@ import { PlatformAccessory, Service } from "homebridge"; -import HarmonyDataProvider from "../dataProviders/harmonyDataProvider"; +import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider"; import { IDeviceButton } from "../models/config"; import { HarmonyDevice } from "../models/harmonyDevice"; import { Platform } from "../platform"; diff --git a/src/accessories/sequence.ts b/src/accessories/sequence.ts index 7b05620..9f09dd9 100644 --- a/src/accessories/sequence.ts +++ b/src/accessories/sequence.ts @@ -5,7 +5,7 @@ import { PlatformAccessory, Service, } from "homebridge"; -import HarmonyDataProvider from "../dataProviders/harmonyDataProvider"; +import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider"; import { ISequence } from "../models/config/sequence"; import { HarmonyDevice } from "../models/harmonyDevice"; import { Platform } from "../platform"; diff --git a/src/dataProviders/harmonyDataProvider.ts b/src/dataProviders/harmonyDataProvider.ts index 63ff8ac..e8eaada 100644 --- a/src/dataProviders/harmonyDataProvider.ts +++ b/src/dataProviders/harmonyDataProvider.ts @@ -1,46 +1,20 @@ -import { IActivity } from "../models/config/activity"; -import { IDeviceSetupItem } from "../models/config/deviceSetupItem"; -import { IInput, IMatrix, IOutput } from "../models/config/matrix"; -import { RemoteKey } from "../accessories/controlUnit"; -import { EventEmitter } from "events"; -import { IHub } from "../models/config/hub"; +import { Logging } from "homebridge"; +import { inject, injectable } from "tsyringe"; +import { IConfig } from "../models/config"; import { IDeviceConfig } from "../models/config/deviceConfig"; +import { IHub } from "../models/config/hub"; import { HarmonyDevice } from "../models/harmonyDevice"; import { HarmonyHub } from "../models/harmonyHub"; -import { IConfig } from "../models/config"; -import { inject, injectable } from "tsyringe"; -import { Logger, Logging } from "homebridge"; - -const Harmony = require("harmony-websocket"); - -interface IActivityState { - currentActivity: IActivity; -} - -interface IHarmonyDataProviderProps { - hubs: Array; - deviceConfigs: Array; - log: any; - matrix: IMatrix; -} @injectable() -class HarmonyDataProvider extends EventEmitter { - private _hubsByDevice: { [deviceName: string]: string } = {}; +export class HarmonyDataProvider { private _hubs: { [hubName: string]: HarmonyHub } = {}; - // private _devicesByHub: { [hubName: string]: { [deviceName: string]: HarmonyDevice } } = {}; - private _states: { - [controlUnitName: string]: IActivityState | undefined; - } = {}; - - private _matrix: IMatrix; + private _hubsByDevice: { [deviceName: string]: string } = {}; constructor( @inject("IConfig") private _config: IConfig, @inject("log") private _log: Logging ) { - super(); - this._matrix = _config.Matrix; _config.Devices.forEach((deviceConfig: IDeviceConfig) => { this._hubsByDevice[deviceConfig.Name] = deviceConfig.Hub; }); @@ -50,87 +24,9 @@ class HarmonyDataProvider extends EventEmitter { this.emitInfo(); } - // public get devicesByHub(): { [hubName: string]: { [deviceName: string]: HarmonyDevice } } { - // return this._devicesByHub; - // } - - public get hubs(): { [hubName: string]: HarmonyHub } { - return this._hubs; - } - - /** - * Power on all devices in an activity. - */ - public powerOn = async (controlUnitName: string, activity: IActivity) => { - //Only power on if not alread on - let currentActivity = this._states[controlUnitName] - ? this._states[controlUnitName]!.currentActivity - : undefined; - if (!currentActivity) { - await this.startActivity(controlUnitName, activity); - } - }; - - /** - * Power off all devices in an activity that aren't being used. - */ - public powerOff = async (controlUnitName: string) => { - if (!this._states[controlUnitName]) { - return; - } - //Build potential list of devices to turn off - let devicesToTurnOff: Array = this._states[ - controlUnitName - ]!.currentActivity.DeviceSetupList.map( - (value: IDeviceSetupItem): HarmonyDevice => { - return this.getDeviceFromName(value.DeviceName); - } - ); - - //Resolve device conflicts with other controlUnits - devicesToTurnOff = this.sanitizeDeviceList( - devicesToTurnOff, - controlUnitName - ); - - //Turn off devices - devicesToTurnOff.forEach(async (device: HarmonyDevice) => { - if (device) { - await device.powerOff(); - } - }); - - this._states[controlUnitName] = undefined; - }; - - /** - * Start an activity - */ - public startActivity = async ( - controlUnitName: string, - activity: IActivity - ) => { - this._log.info( - `Starting activity ${activity.DisplayName} for controlUnit: ${controlUnitName}` - ); - let lastActivity: IActivity | undefined = undefined; - if (this._states[controlUnitName]) { - lastActivity = this._states[controlUnitName]!.currentActivity; - } - - //Build potential list of devices to to turn on - let devicesToTurnOn: Array = activity.DeviceSetupList.map( - (value: IDeviceSetupItem): HarmonyDevice => { - return this.getDeviceFromName(value.DeviceName); - } - ); - - //Resolve device conflicts with other controlUnits - devicesToTurnOn = this.sanitizeDeviceList(devicesToTurnOn, controlUnitName); - - //Turn on devices + public async turnOnDevices(devices: Array): Promise { await Promise.all( - devicesToTurnOn.map(async (device: HarmonyDevice) => { + devices.map(async (device: HarmonyDevice) => { if (device && device.name) { if (!device.on) { this._log.info(`Turning on device ${device.name}`); @@ -139,188 +35,20 @@ class HarmonyDataProvider extends EventEmitter { } }) ); + } - //Assign correct input + public async turnOffDevices(devices: Array) { await Promise.all( - activity.DeviceSetupList.map(async (value: IDeviceSetupItem) => { - let device: HarmonyDevice = this.getDeviceFromName(value.DeviceName); - - if (device && device.supportsCommand(`Input${value.Input}`)) { - await device.sendCommand(`Input${value.Input}`); + //Turn off devices + devices.map(async (device: HarmonyDevice) => { + if (device) { + if (device.on) { + this._log.info(`Turning off device ${device.name}`); + await device.powerOff(); + } } }) ); - - if (activity.UseMatrix) { - //get input and output - let input: IInput = this._matrix.Inputs.filter( - (e) => e.InputDevice === activity.ControlDevice - )[0]; - let output: IOutput = this._matrix.Outputs.filter( - (e) => e.OutputDevice === activity.OutputDevice - )[0]; - - let inputCommandName: string = `In ${input.InputNumber}`; - let outputCommandName: string = `Out ${output.OutputLetter}`; - - let matrixDevice: HarmonyDevice = this.getDeviceFromName( - this._matrix.DeviceName - ); - - //Route hdmi - if ( - matrixDevice.supportsCommand(inputCommandName) && - matrixDevice.supportsCommand(outputCommandName) - ) { - await matrixDevice.sendCommand(outputCommandName); - await matrixDevice.sendCommand(inputCommandName); - await matrixDevice.sendCommand(outputCommandName); - await matrixDevice.sendCommand(inputCommandName); - } - } - - //Build potential list of devices to turn off - if (lastActivity) { - let devicesToTurnOff: Array = - lastActivity.DeviceSetupList.map( - (value: IDeviceSetupItem): HarmonyDevice => { - return this.getDeviceFromName(value.DeviceName); - } - ); - - //remove devices that will be used for next activity from list - //delete array[index] is stupid because it just nulls out the index. But now i have to deal with nulls - devicesToTurnOff.forEach((device: HarmonyDevice, index: number) => { - if ( - device && - device.name && - activity.DeviceSetupList.some((e) => { - return e && e.DeviceName === device.name; - }) - ) { - delete devicesToTurnOff[index]; - } - }); - - //Resolve device conflicts with other controlUnits - devicesToTurnOff = this.sanitizeDeviceList( - devicesToTurnOff, - controlUnitName - ); - - this._log.info( - `Sanatized devices to turn off: ${JSON.stringify( - devicesToTurnOff.map((e) => (e ? e.name : "")) - )}` - ); - - await Promise.all( - //Turn off devices - devicesToTurnOff.map(async (device: HarmonyDevice) => { - if (device) { - if (device.on) { - this._log.info(`Turning off device ${device.name}`); - await device.powerOff(); - } - } - }) - ); - } - - //Assign current activity - this._states[controlUnitName] = { currentActivity: activity }; - }; - - /** - * Turn the volume up for the current running activity. - */ - public volumeUp = async (controlUnitName: string) => { - let volumeUpCommand: string = "Volume Up"; - if (this._states[controlUnitName]) { - let volumeDevice: HarmonyDevice = this.getDeviceFromName( - this._states[controlUnitName]!.currentActivity.VolumeDevice - ); - await volumeDevice.sendCommand(volumeUpCommand); - } - }; - - /** - * Volume down for current running activity. - */ - public volumeDown = async (controlUnitName: string) => { - let volumeDownCommand: string = "Volume Down"; - if (this._states[controlUnitName]) { - let volumeDevice: HarmonyDevice = this.getDeviceFromName( - this._states[controlUnitName]!.currentActivity.VolumeDevice - ); - await volumeDevice.sendCommand(volumeDownCommand); - } - }; - - /** - * Send key press for current activity. - * - * @param controlUnitName The name of the control unit to act on. - * @param key The key to send. - */ - public sendKeyPress = async (controlUnitName: string, key: any) => { - if (this._states[controlUnitName]) { - let commandName: string = ""; - - let device: HarmonyDevice = this.getDeviceFromName( - this._states[controlUnitName]!.currentActivity.ControlDevice - ); - switch (key) { - case RemoteKey.ARROW_UP: { - commandName = "Direction Up"; - break; - } - case RemoteKey.ARROW_DOWN: { - commandName = "Direction Down"; - break; - } - case RemoteKey.ARROW_LEFT: { - commandName = "Direction Left"; - break; - } - case RemoteKey.ARROW_RIGHT: { - commandName = "Direction Right"; - break; - } - case RemoteKey.SELECT: { - commandName = "Select"; - break; - } - case RemoteKey.PLAY_PAUSE: { - commandName = "Pause"; - break; - } - case RemoteKey.INFORMATION: { - commandName = "Menu"; - break; - } - case RemoteKey.BACK: { - commandName = "Back"; - break; - } - case RemoteKey.EXIT: { - commandName = "Back"; - break; - } - } - - await device.sendCommand(commandName); - } - }; - - /** - * Return if a control unit is active - * @param controlUnitName - */ - public getIsActive(controlUnitName: string): IActivity | undefined { - return this._states[controlUnitName] - ? this._states[controlUnitName]!.currentActivity - : undefined; } /** @@ -339,20 +67,6 @@ class HarmonyDataProvider extends EventEmitter { return device!; } - // /** - // * Gets device button commands - // * @param deviceCommandName The device command name - // * @param deviceName The device name - // */ - // public getCommand(deviceCommandName: string, deviceName: string): ICommand | undefined { - // const device: HarmonyDevice = this.getDeviceFromName(deviceName); - // if (device && device.supportsCommand(deviceCommandName)) { - // return device.getCommand(deviceCommandName); - // } else { - // return undefined; - // } - // } - private connect = async (hubs: Array) => { let readyCount = 0; await Promise.all( @@ -362,7 +76,7 @@ class HarmonyDataProvider extends EventEmitter { newHarmonyHub.on("Ready", () => { readyCount++; if (readyCount === Object.keys(this._hubs).length) { - this.emit("Ready"); + // this.emit("Ready"); } }); await newHarmonyHub.initialize(); @@ -370,45 +84,11 @@ class HarmonyDataProvider extends EventEmitter { ); }; - /** - * Helper function to make sure no control unit depends on device list. - * @param devicesToTurnOn The list of devices to modify. - * @param controlUnitName The name of the control unit in question. - */ - private sanitizeDeviceList( - devicesToTurnOn: Array, - controlUnitName: string - ): Array { - for (let controlUnitKey in this._states) { - //Skip self - if (controlUnitKey === controlUnitName) { - continue; - } - let currentOtherState: IActivityState = this._states[controlUnitKey]!; - - if (currentOtherState) { - currentOtherState.currentActivity.DeviceSetupList.forEach( - (value: IDeviceSetupItem) => { - //there are devices to remove - if (devicesToTurnOn.some((e) => e && e.name === value.DeviceName)) { - let deviceToRemove: HarmonyDevice = devicesToTurnOn.filter( - (i) => i.name === value.DeviceName - )[0]; - delete devicesToTurnOn[devicesToTurnOn.indexOf(deviceToRemove)]; - } - } - ); - } - } - - return devicesToTurnOn; - } - private emitInfo(): void { //Emit devices if requested this._log.info("All hubs connected"); if (this._config.EmitDevicesOnStartup) { - const hubs = this.hubs; + const hubs = this._hubs; Object.values(hubs).forEach((hub: HarmonyHub) => { const deviceDictionary = hub.devices; this._log.info(`${hub.hubName}`); @@ -422,5 +102,3 @@ class HarmonyDataProvider extends EventEmitter { } } } - -export default HarmonyDataProvider; diff --git a/src/dataProviders/harmonyDataProvider2.ts b/src/dataProviders/harmonyDataProvider2.ts deleted file mode 100644 index c6705cf..0000000 --- a/src/dataProviders/harmonyDataProvider2.ts +++ /dev/null @@ -1,105 +0,0 @@ -import { Logging } from "homebridge"; -import { inject } from "tsyringe"; -import { IConfig, IMatrix } from "../models/config"; -import { IDeviceConfig } from "../models/config/deviceConfig"; -import { IHub } from "../models/config/hub"; -import { HarmonyDevice } from "../models/harmonyDevice"; -import { HarmonyHub } from "../models/harmonyHub"; - -export class HarmonyDataProvider2 { - private _matrix: IMatrix; - private _hubs: { [hubName: string]: HarmonyHub } = {}; - private _hubsByDevice: { [deviceName: string]: string } = {}; - - constructor( - @inject("IConfig") private _config: IConfig, - @inject("log") private _log: Logging - ) { - this._matrix = _config.Matrix; - _config.Devices.forEach((deviceConfig: IDeviceConfig) => { - this._hubsByDevice[deviceConfig.Name] = deviceConfig.Hub; - }); - // this._deviceConfigs = props.deviceConfigs; - - this.connect(_config.Hubs); - this.emitInfo(); - } - - public async turnOnDevices(devices: Array): Promise { - await Promise.all( - devices.map(async (device: HarmonyDevice) => { - if (device && device.name) { - if (!device.on) { - this._log.info(`Turning on device ${device.name}`); - await device.powerOn(); - } - } - }) - ); - } - - public async turnOffDevices(devices: Array) { - await Promise.all( - //Turn off devices - devices.map(async (device: HarmonyDevice) => { - if (device) { - if (device.on) { - this._log.info(`Turning off device ${device.name}`); - await device.powerOff(); - } - } - }) - ); - } - - /** - * Get the IDevice by name. - * @param deviceName The device to retrieve. - */ - public getDeviceFromName(deviceName: string): HarmonyDevice { - let device: HarmonyDevice | undefined; - try { - device = - this._hubs[this._hubsByDevice[deviceName]].getDeviceByName(deviceName); - } catch (err) { - this._log.info(`Error retrieving device from hub: ${err}`); - } - - return device!; - } - - private connect = async (hubs: Array) => { - let readyCount = 0; - await Promise.all( - hubs.map(async (hub: IHub): Promise => { - const newHarmonyHub = new HarmonyHub(hub.Name, hub.Ip, this._log.info); - this._hubs[hub.Name] = newHarmonyHub; - newHarmonyHub.on("Ready", () => { - readyCount++; - if (readyCount === Object.keys(this._hubs).length) { - // this.emit("Ready"); - } - }); - await newHarmonyHub.initialize(); - }) - ); - }; - - private emitInfo(): void { - //Emit devices if requested - this._log.info("All hubs connected"); - if (this._config.EmitDevicesOnStartup) { - const hubs = this._hubs; - Object.values(hubs).forEach((hub: HarmonyHub) => { - const deviceDictionary = hub.devices; - this._log.info(`${hub.hubName}`); - Object.values(deviceDictionary).forEach((device: HarmonyDevice) => { - this._log.info(` ${device.name} : ${device.id}`); - Object.keys(device.commands).forEach((command: string) => { - this._log.info(` ${command}`); - }); - }); - }); - } - } -} diff --git a/src/platform.ts b/src/platform.ts index 52e3942..7bed9bf 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -10,12 +10,11 @@ import { } from "homebridge"; import { ControlUnit, DeviceButton } from "./accessories"; import { Sequence } from "./accessories/sequence"; -import HarmonyDataProvider from "./dataProviders/harmonyDataProvider"; import { IConfig, IControlUnit, IDeviceButton } from "./models/config"; import { ISequence } from "./models/config/sequence"; import { PLATFORM_NAME, PLUGIN_NAME } from "./settings"; import { container } from "tsyringe"; -import { HarmonyDataProvider2 } from "./dataProviders/harmonyDataProvider2"; +import { HarmonyDataProvider } from "./dataProviders/harmonyDataProvider"; import { StateDataProvider } from "./dataProviders/stateDataProvider"; import { CommandService } from "./services/commandService"; import { ActivityService } from "./services/activityService"; @@ -221,9 +220,9 @@ export class Platform implements DynamicPlatformPlugin { container.register(Platform, { useValue: this }); container.register("IConfig", { useValue: this.config }); container.register("log", { useValue: this.log }); + container.registerSingleton(HarmonyDataProvider); + container.registerSingleton(ActivityService); container.registerSingleton(StateDataProvider); container.registerSingleton(CommandService); - container.registerSingleton(HarmonyDataProvider); - container.registerSingleton(HarmonyDataProvider2); } } diff --git a/src/services/activityService.ts b/src/services/activityService.ts index adf6910..3cff169 100644 --- a/src/services/activityService.ts +++ b/src/services/activityService.ts @@ -1,6 +1,6 @@ import { Logging } from "homebridge"; import { inject, injectable } from "tsyringe"; -import { HarmonyDataProvider2 } from "../dataProviders/harmonyDataProvider2"; +import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider"; import { StateDataProvider } from "../dataProviders/stateDataProvider"; import { IActivityState } from "../models/activityState"; import { @@ -15,8 +15,8 @@ import { HarmonyDevice } from "../models/harmonyDevice"; @injectable() export class ActivityService { constructor( - @inject(HarmonyDataProvider2) - private _harmonyDataProvider: HarmonyDataProvider2, + @inject(HarmonyDataProvider) + private _harmonyDataProvider: HarmonyDataProvider, @inject(StateDataProvider) private _stateDataProvider: StateDataProvider, @inject("IConfig") private _config: IConfig, @inject("log") private _log: Logging diff --git a/src/services/commandService.ts b/src/services/commandService.ts index d1fc2fd..e666274 100644 --- a/src/services/commandService.ts +++ b/src/services/commandService.ts @@ -1,15 +1,15 @@ -import { inject } from "tsyringe"; +import { inject, injectable } from "tsyringe"; import { RemoteKey } from "../accessories/controlUnit"; -import { HarmonyDataProvider2 } from "../dataProviders/harmonyDataProvider2"; +import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider"; import { StateDataProvider } from "../dataProviders/stateDataProvider"; -import { IConfig } from "../models/config"; import { HarmonyDevice } from "../models/harmonyDevice"; +@injectable() export class CommandService { constructor( @inject(StateDataProvider) private _stateDataProvider: StateDataProvider, - @inject(HarmonyDataProvider2) - private _harmonyDataProvider: HarmonyDataProvider2 + @inject(HarmonyDataProvider) + private _harmonyDataProvider: HarmonyDataProvider ) {} /** diff --git a/src/services/volumeService.ts b/src/services/volumeService.ts index 917d404..0c7f8cb 100644 --- a/src/services/volumeService.ts +++ b/src/services/volumeService.ts @@ -1,15 +1,14 @@ -import { Logging } from "homebridge"; -import { inject } from "tsyringe"; -import { HarmonyDataProvider2 } from "../dataProviders/harmonyDataProvider2"; +import { inject, injectable } from "tsyringe"; +import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider"; import { StateDataProvider } from "../dataProviders/stateDataProvider"; -import { IConfig } from "../models/config"; import { HarmonyDevice } from "../models/harmonyDevice"; +@injectable() export class VolumeService { constructor( @inject(StateDataProvider) private _stateDataProvider: StateDataProvider, - @inject(HarmonyDataProvider2) - private _harmonyDataProvider: HarmonyDataProvider2 + @inject(HarmonyDataProvider) + private _harmonyDataProvider: HarmonyDataProvider ) {} /** From e6e3d45b5b20432374c79e20927a384686c58533 Mon Sep 17 00:00:00 2001 From: Brandon Watson Date: Tue, 28 Dec 2021 20:11:13 -0500 Subject: [PATCH 6/7] Removing business logic from models. First pass at overrides feature --- src/accessories/deviceButton.ts | 12 +++- src/accessories/sequence.ts | 4 +- src/dataProviders/harmonyDataProvider.ts | 81 +++++++++++++++++++++--- src/models/config/deviceConfig.ts | 7 +- src/models/harmonyDevice.ts | 54 ++-------------- src/models/harmonyHub.ts | 19 ++++++ src/services/activityService.ts | 37 +++++++---- src/services/commandService.ts | 4 +- src/services/volumeService.ts | 18 +++--- 9 files changed, 148 insertions(+), 88 deletions(-) diff --git a/src/accessories/deviceButton.ts b/src/accessories/deviceButton.ts index b456e48..ddf8574 100644 --- a/src/accessories/deviceButton.ts +++ b/src/accessories/deviceButton.ts @@ -69,7 +69,7 @@ export class DeviceButton { //Get device command if we don't have it if (!this._device) { - this._device = this._dataProvider.getDeviceFromName( + this._device = this._dataProvider.getDeviceByName( this._deviceInfo.DeviceName ); } @@ -82,12 +82,18 @@ export class DeviceButton { //change state if stateful if (this._deviceInfo.IsStateful && this._buttonState != newState) { this._buttonState = newState; - await this._device.sendCommand(this._deviceInfo.ButtonName); + await this._dataProvider.sendCommand( + this._deviceInfo.ButtonName, + this._device + ); return callback(); } else if (!this._deviceInfo.IsStateful) { //Send the number of configured key presses for (let i = 0; i < this._deviceInfo.NumberOfKeyPresses; i++) { - await this._device.sendCommand(this._deviceInfo.ButtonName); + await this._dataProvider.sendCommand( + this._deviceInfo.ButtonName, + this._device + ); } this._switchService diff --git a/src/accessories/sequence.ts b/src/accessories/sequence.ts index 9f09dd9..a92a246 100644 --- a/src/accessories/sequence.ts +++ b/src/accessories/sequence.ts @@ -59,7 +59,7 @@ export class Sequence { if (!deviceName) { continue; } - const device = this._dataProvider.getDeviceFromName(deviceName); + const device = this._dataProvider.getDeviceByName(deviceName); if (device) { this._devices[deviceName] = device; } else { @@ -87,7 +87,7 @@ export class Sequence { step.DeviceCommand && device.supportsCommand(step.DeviceCommand) ) { - await device.sendCommand(step.DeviceCommand); + await this._dataProvider.sendCommand(step.DeviceCommand, device); } else { this._platform.log.warn( `Attempted to execute command ${step.DeviceCommand} on device ${step.DeviceName} but the device or command was not found` diff --git a/src/dataProviders/harmonyDataProvider.ts b/src/dataProviders/harmonyDataProvider.ts index e8eaada..b88a169 100644 --- a/src/dataProviders/harmonyDataProvider.ts +++ b/src/dataProviders/harmonyDataProvider.ts @@ -1,65 +1,83 @@ import { Logging } from "homebridge"; import { inject, injectable } from "tsyringe"; +import { ICommand } from "../models"; import { IConfig } from "../models/config"; import { IDeviceConfig } from "../models/config/deviceConfig"; import { IHub } from "../models/config/hub"; import { HarmonyDevice } from "../models/harmonyDevice"; import { HarmonyHub } from "../models/harmonyHub"; +import { sleep } from "../util"; @injectable() export class HarmonyDataProvider { private _hubs: { [hubName: string]: HarmonyHub } = {}; - private _hubsByDevice: { [deviceName: string]: string } = {}; + private _deviceConfigByName: { [deviceName: string]: IDeviceConfig } = {}; constructor( @inject("IConfig") private _config: IConfig, @inject("log") private _log: Logging ) { _config.Devices.forEach((deviceConfig: IDeviceConfig) => { - this._hubsByDevice[deviceConfig.Name] = deviceConfig.Hub; + this._deviceConfigByName[deviceConfig.Name] = deviceConfig; }); - // this._deviceConfigs = props.deviceConfigs; this.connect(_config.Hubs); this.emitInfo(); } - public async turnOnDevices(devices: Array): Promise { + public async powerOnDevices(devices: Array): Promise { await Promise.all( devices.map(async (device: HarmonyDevice) => { if (device && device.name) { if (!device.on) { this._log.info(`Turning on device ${device.name}`); - await device.powerOn(); + await this.powerOnDevice(device); } } }) ); } - public async turnOffDevices(devices: Array) { + public async powerOffDevices(devices: Array) { await Promise.all( //Turn off devices devices.map(async (device: HarmonyDevice) => { if (device) { if (device.on) { this._log.info(`Turning off device ${device.name}`); - await device.powerOff(); + await this.powerOffDevice(device); } } }) ); } + public async sendCommand( + commandName: string, + harmonyDevice: HarmonyDevice + ): Promise { + let command!: ICommand; + + commandName = this.getOverrideCommand(commandName, harmonyDevice); + + if (harmonyDevice.supportsCommand(commandName)) { + command = harmonyDevice.getCommand(commandName); + } + const hub = this.getHubByDevice(harmonyDevice); + await hub.sendCommand(command); + } + /** * Get the IDevice by name. * @param deviceName The device to retrieve. */ - public getDeviceFromName(deviceName: string): HarmonyDevice { + public getDeviceByName(deviceName: string): HarmonyDevice { let device: HarmonyDevice | undefined; try { device = - this._hubs[this._hubsByDevice[deviceName]].getDeviceByName(deviceName); + this._hubs[this._deviceConfigByName[deviceName].Hub].getDeviceByName( + deviceName + ); } catch (err) { this._log.info(`Error retrieving device from hub: ${err}`); } @@ -101,4 +119,49 @@ export class HarmonyDataProvider { }); } } + + private getHubByDevice(device: HarmonyDevice) { + return this._hubs[this._deviceConfigByName[device.name].Hub]; + } + + private async powerOnDevice(harmonyDevice: HarmonyDevice): Promise { + let powerOnCommand: string = "Power On"; + let powerToggleCommand: string = "Power Toggle"; + if (harmonyDevice.supportsCommand(powerOnCommand)) { + await this.sendCommand(powerOnCommand, harmonyDevice); + harmonyDevice.on = true; + } else if (harmonyDevice.supportsCommand(powerToggleCommand)) { + await this.sendCommand(powerToggleCommand, harmonyDevice); + harmonyDevice.on = true; + } else { + await this.sendCommand(powerOnCommand, harmonyDevice); + } + } + + private async powerOffDevice(harmonyDevice: HarmonyDevice): Promise { + let powerOffCommand: string = "Power Off"; + let powerToggleCommand: string = "Power Toggle"; + if (harmonyDevice.supportsCommand(powerOffCommand)) { + await this.sendCommand(powerOffCommand, harmonyDevice); + harmonyDevice.on = false; + } else if (harmonyDevice.supportsCommand(powerToggleCommand)) { + await this.sendCommand(powerToggleCommand, harmonyDevice); + harmonyDevice.on = false; + } + } + + private getOverrideCommand( + commandName: string, + harmonyDevice: HarmonyDevice + ) { + const deviceConfig: IDeviceConfig = + this._deviceConfigByName[harmonyDevice.name]; + if (!deviceConfig.Overrides) { + return commandName; + } + const overrideCommand = deviceConfig.Overrides.find( + (e) => e.Command == commandName + ); + return overrideCommand ? overrideCommand.Override : commandName; + } } diff --git a/src/models/config/deviceConfig.ts b/src/models/config/deviceConfig.ts index 97b74a0..116b3eb 100644 --- a/src/models/config/deviceConfig.ts +++ b/src/models/config/deviceConfig.ts @@ -1,4 +1,5 @@ export interface IDeviceConfig { - Name: string; - Hub: string; -} \ No newline at end of file + Name: string; + Hub: string; + Overrides: Array<{ Command: string; Override: string }>; +} diff --git a/src/models/harmonyDevice.ts b/src/models/harmonyDevice.ts index 1b6c394..aefc87e 100644 --- a/src/models/harmonyDevice.ts +++ b/src/models/harmonyDevice.ts @@ -10,15 +10,12 @@ export interface IHarmonyDeviceProps { } export class HarmonyDevice { - private _harmony: any; - private _log: any; private _commands: { [name: string]: ICommand } = {}; private _on: boolean; constructor(props: IHarmonyDeviceProps) { this.id = props.id; this.name = props.name; - this._harmony = props.harmony; this._on = false; this._commands = props.commands; } @@ -30,6 +27,10 @@ export class HarmonyDevice { return this._on; } + public set on(value: boolean) { + this._on = value; + } + public get commands(): { [name: string]: ICommand } { return this._commands; } @@ -43,51 +44,4 @@ export class HarmonyDevice { public getCommand(commandName: string): ICommand { return this._commands[commandName]; } - - public async powerOn(): Promise { - let powerOnCommand: string = "Power On"; - let powerToggleCommand: string = "Power Toggle"; - if (this.supportsCommand(powerOnCommand)) { - await this.sendCommand(powerOnCommand); - this._on = true; - } else if (this.supportsCommand(powerToggleCommand)) { - await this.sendCommand(powerToggleCommand); - this._on = true; - } - } - - public async powerOff(): Promise { - let powerOffCommand: string = "Power Off"; - let powerToggleCommand: string = "Power Toggle"; - if (this.supportsCommand(powerOffCommand)) { - await this.sendCommand(powerOffCommand); - this._on = false; - } else if (this.supportsCommand(powerToggleCommand)) { - await this.sendCommand(powerToggleCommand); - this._on = false; - } - } - - public async sendCommand(commandName: string): Promise { - let command!: ICommand; - if (this.supportsCommand(commandName)) { - command = this.getCommand(commandName); - } - - try { - //Execute command - //HACK to fix Harmon Kardon receiver not turning off - if (command.command === "PowerOff") { - for (let i = 0; i < 2; i++) { - await this._harmony.sendCommand(JSON.stringify(command)); - } - } - await this._harmony.sendCommand(JSON.stringify(command)); - - //Sleep - await sleep(800); - } catch (err) { - this._log(`ERROR - error sending command to harmony: ${err}`); - } - } } diff --git a/src/models/harmonyHub.ts b/src/models/harmonyHub.ts index 49c8b77..0d2c9e4 100644 --- a/src/models/harmonyHub.ts +++ b/src/models/harmonyHub.ts @@ -2,6 +2,7 @@ import { HarmonyDevice } from "./harmonyDevice"; const Harmony = require("harmony-websocket"); import { ICommand } from "./device"; import { EventEmitter } from "events"; +import { sleep } from "../util"; export class HarmonyHub extends EventEmitter { private _devices: { [deviceName: string]: HarmonyDevice } = {}; @@ -72,4 +73,22 @@ export class HarmonyHub extends EventEmitter { private connect = async (): Promise => { await this._harmony.Connect(this._ip); }; + + public async sendCommand(command: ICommand): Promise { + try { + //Execute command + //HACK to fix Harmon Kardon receiver not turning off + if (command.command === "PowerOff") { + for (let i = 0; i < 2; i++) { + await this._harmony.sendCommand(JSON.stringify(command)); + } + } + await this._harmony.sendCommand(JSON.stringify(command)); + + //Sleep + await sleep(800); + } catch (err) { + this._log(`ERROR - error sending command to harmony: ${err}`); + } + } } diff --git a/src/services/activityService.ts b/src/services/activityService.ts index 3cff169..4dca0fb 100644 --- a/src/services/activityService.ts +++ b/src/services/activityService.ts @@ -34,7 +34,7 @@ export class ActivityService { controlUnitName ); - await this._harmonyDataProvider.turnOnDevices(devicesToTurnOn); + await this._harmonyDataProvider.powerOnDevices(devicesToTurnOn); await this.assignDeviceInput(activity); @@ -47,7 +47,7 @@ export class ActivityService { lastActivity, activity ); - await this._harmonyDataProvider.turnOffDevices(devicesToTurnOff); + await this._harmonyDataProvider.powerOffDevices(devicesToTurnOff); this._stateDataProvider.updateState(activity, controlUnitName); }; @@ -66,7 +66,7 @@ export class ActivityService { controlUnitName, lastActivity ); - await this._harmonyDataProvider.turnOffDevices(devicesToTurnOff); + await this._harmonyDataProvider.powerOffDevices(devicesToTurnOff); this._stateDataProvider.removeState(controlUnitName); }; @@ -157,7 +157,7 @@ export class ActivityService { private buildPotentialDeviceList(activity: IActivity): HarmonyDevice[] { return activity.DeviceSetupList.map( (value: IDeviceSetupItem): HarmonyDevice => { - return this._harmonyDataProvider.getDeviceFromName(value.DeviceName); + return this._harmonyDataProvider.getDeviceByName(value.DeviceName); } ); } @@ -165,12 +165,15 @@ export class ActivityService { private async assignDeviceInput(activity: IActivity): Promise { await Promise.all( activity.DeviceSetupList.map(async (value: IDeviceSetupItem) => { - let device: HarmonyDevice = this._harmonyDataProvider.getDeviceFromName( + let device: HarmonyDevice = this._harmonyDataProvider.getDeviceByName( value.DeviceName ); if (device && device.supportsCommand(`Input${value.Input}`)) { - await device.sendCommand(`Input${value.Input}`); + await this._harmonyDataProvider.sendCommand( + `Input${value.Input}`, + device + ); } }) ); @@ -190,7 +193,7 @@ export class ActivityService { let outputCommandName: string = `Out ${output.OutputLetter}`; let matrixDevice: HarmonyDevice = - this._harmonyDataProvider.getDeviceFromName( + this._harmonyDataProvider.getDeviceByName( this._config.Matrix.DeviceName ); @@ -199,10 +202,22 @@ export class ActivityService { matrixDevice.supportsCommand(inputCommandName) && matrixDevice.supportsCommand(outputCommandName) ) { - await matrixDevice.sendCommand(outputCommandName); - await matrixDevice.sendCommand(inputCommandName); - await matrixDevice.sendCommand(outputCommandName); - await matrixDevice.sendCommand(inputCommandName); + await this._harmonyDataProvider.sendCommand( + outputCommandName, + matrixDevice + ); + await this._harmonyDataProvider.sendCommand( + inputCommandName, + matrixDevice + ); + await this._harmonyDataProvider.sendCommand( + outputCommandName, + matrixDevice + ); + await this._harmonyDataProvider.sendCommand( + inputCommandName, + matrixDevice + ); } } } diff --git a/src/services/commandService.ts b/src/services/commandService.ts index e666274..be4e841 100644 --- a/src/services/commandService.ts +++ b/src/services/commandService.ts @@ -23,7 +23,7 @@ export class CommandService { if (currentActivity) { let commandName: string = ""; - let device: HarmonyDevice = this._harmonyDataProvider.getDeviceFromName( + let device: HarmonyDevice = this._harmonyDataProvider.getDeviceByName( currentActivity.ControlDevice ); switch (key) { @@ -65,7 +65,7 @@ export class CommandService { } } - await device.sendCommand(commandName); + await this._harmonyDataProvider.sendCommand(commandName, device); } }; } diff --git a/src/services/volumeService.ts b/src/services/volumeService.ts index 0c7f8cb..5a48961 100644 --- a/src/services/volumeService.ts +++ b/src/services/volumeService.ts @@ -19,10 +19,11 @@ export class VolumeService { let currentActivity = this._stateDataProvider.getState(controlUnitName); if (currentActivity) { let volumeDevice: HarmonyDevice = - this._harmonyDataProvider.getDeviceFromName( - currentActivity.VolumeDevice - ); - await volumeDevice.sendCommand(volumeUpCommand); + this._harmonyDataProvider.getDeviceByName(currentActivity.VolumeDevice); + await this._harmonyDataProvider.sendCommand( + volumeUpCommand, + volumeDevice + ); } }; @@ -34,10 +35,11 @@ export class VolumeService { let currentActivity = this._stateDataProvider.getState(controlUnitName); if (currentActivity) { let volumeDevice: HarmonyDevice = - this._harmonyDataProvider.getDeviceFromName( - currentActivity.VolumeDevice - ); - await volumeDevice.sendCommand(volumeDownCommand); + this._harmonyDataProvider.getDeviceByName(currentActivity.VolumeDevice); + await this._harmonyDataProvider.sendCommand( + volumeDownCommand, + volumeDevice + ); } }; } From 51441a706d67f594abe65877dd666315db95c4e8 Mon Sep 17 00:00:00 2001 From: Brandon Watson Date: Tue, 28 Dec 2021 20:16:43 -0500 Subject: [PATCH 7/7] Cleanup --- src/accessories/controlUnit.ts | 2 +- src/dataProviders/harmonyDataProvider.ts | 1 - src/dataProviders/stateDataProvider.ts | 2 +- src/services/activityService.ts | 26 +++++++++++++----------- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/accessories/controlUnit.ts b/src/accessories/controlUnit.ts index 219ea31..f6def08 100644 --- a/src/accessories/controlUnit.ts +++ b/src/accessories/controlUnit.ts @@ -1,4 +1,4 @@ -import { PlatformAccessory, RemoteController, Service } from "homebridge"; +import { PlatformAccessory, Service } from "homebridge"; import { IActivity } from "../models/config"; import { Platform } from "../platform"; import { ActivityService } from "../services/activityService"; diff --git a/src/dataProviders/harmonyDataProvider.ts b/src/dataProviders/harmonyDataProvider.ts index b88a169..17ae67f 100644 --- a/src/dataProviders/harmonyDataProvider.ts +++ b/src/dataProviders/harmonyDataProvider.ts @@ -6,7 +6,6 @@ import { IDeviceConfig } from "../models/config/deviceConfig"; import { IHub } from "../models/config/hub"; import { HarmonyDevice } from "../models/harmonyDevice"; import { HarmonyHub } from "../models/harmonyHub"; -import { sleep } from "../util"; @injectable() export class HarmonyDataProvider { diff --git a/src/dataProviders/stateDataProvider.ts b/src/dataProviders/stateDataProvider.ts index e00860b..f4d89b8 100644 --- a/src/dataProviders/stateDataProvider.ts +++ b/src/dataProviders/stateDataProvider.ts @@ -1,4 +1,4 @@ -import { inject, injectable } from "tsyringe"; +import { injectable } from "tsyringe"; import { IActivityState } from "../models/activityState"; import { IActivity } from "../models/config"; diff --git a/src/services/activityService.ts b/src/services/activityService.ts index 4dca0fb..8549684 100644 --- a/src/services/activityService.ts +++ b/src/services/activityService.ts @@ -100,19 +100,21 @@ export class ActivityService { let currentOtherState: IActivityState = this._stateDataProvider.states[controlUnitKey]!; - if (currentOtherState) { - currentOtherState.currentActivity.DeviceSetupList.forEach( - (value: IDeviceSetupItem) => { - //there are devices to remove - if (devicesToTurnOn.some((e) => e && e.name === value.DeviceName)) { - let deviceToRemove: HarmonyDevice = devicesToTurnOn.filter( - (i) => i.name === value.DeviceName - )[0]; - delete devicesToTurnOn[devicesToTurnOn.indexOf(deviceToRemove)]; - } - } - ); + if (!currentOtherState) { + return devicesToTurnOn; } + + currentOtherState.currentActivity.DeviceSetupList.forEach( + (value: IDeviceSetupItem) => { + //there are devices to remove + if (devicesToTurnOn.some((e) => e && e.name === value.DeviceName)) { + let deviceToRemove: HarmonyDevice = devicesToTurnOn.filter( + (i) => i.name === value.DeviceName + )[0]; + delete devicesToTurnOn[devicesToTurnOn.indexOf(deviceToRemove)]; + } + } + ); } return devicesToTurnOn;