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/Accessories/index.ts b/src/Accessories/index.ts deleted file mode 100644 index 4c98bbf..0000000 --- a/src/Accessories/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { ControlUnit } from './ControlUnit'; -export { DeviceButton } from './DeviceButton'; \ No newline at end of file diff --git a/src/DataProviders/HarmonyDataProvider.ts b/src/DataProviders/HarmonyDataProvider.ts deleted file mode 100644 index 150b39a..0000000 --- a/src/DataProviders/HarmonyDataProvider.ts +++ /dev/null @@ -1,404 +0,0 @@ -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 { 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"; - -const Harmony = require("harmony-websocket"); - -interface IActivityState { - currentActivity: IActivity; -} - -interface IHarmonyDataProviderProps { - hubs: Array; - deviceConfigs: Array; - log: any; - matrix: IMatrix; -} - -class HarmonyDataProvider extends EventEmitter { - private _log: any; - private _hubsByDevice: { [deviceName: string]: string } = {}; - private _hubs: { [hubName: string]: HarmonyHub } = {}; - // private _devicesByHub: { [hubName: string]: { [deviceName: string]: HarmonyDevice } } = {}; - private _states: { - [controlUnitName: string]: IActivityState | undefined; - } = {}; - - private _matrix: IMatrix; - - constructor(props: IHarmonyDataProviderProps) { - super(); - this._log = props.log; - this._matrix = props.matrix; - props.deviceConfigs.forEach((deviceConfig: IDeviceConfig) => { - this._hubsByDevice[deviceConfig.Name] = deviceConfig.Hub; - }); - // this._deviceConfigs = props.deviceConfigs; - - this.connect(props.hubs); - } - - // 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( - `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 - await Promise.all( - devicesToTurnOn.map(async (device: HarmonyDevice) => { - if (device && device.name) { - if (!device.on) { - this._log(`Turning on device ${device.name}`); - await device.powerOn(); - } - } - }) - ); - - //Assign correct input - 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}`); - } - }) - ); - - 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( - `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(`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; - } - - /** - * 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(`Error retrieving device from hub: ${err}`); - } - - 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( - 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(); - } - ) - ); - }; - - /** - * 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; - } -} - -export default HarmonyDataProvider; 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/IDeviceConfig.ts b/src/Models/Config/IDeviceConfig.ts deleted file mode 100644 index 97b74a0..0000000 --- a/src/Models/Config/IDeviceConfig.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface IDeviceConfig { - Name: string; - Hub: string; -} \ No newline at end of file diff --git a/src/Models/Config/index.ts b/src/Models/Config/index.ts deleted file mode 100644 index 5bd6c00..0000000 --- a/src/Models/Config/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -export * from './IActivity'; -export * from './IConfig'; -export * from './IDeviceButton'; -export * from './IDeviceSetupItem'; -export * from './IMatrix'; \ No newline at end of file diff --git a/src/Models/HarmonyDevice.ts b/src/Models/HarmonyDevice.ts deleted file mode 100644 index cecece4..0000000 --- a/src/Models/HarmonyDevice.ts +++ /dev/null @@ -1,94 +0,0 @@ -import { ICommand } from "./IDevice"; -import { sleep } from "../Util/Sleep"; - -export interface IHarmonyDeviceProps { - 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; - - 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); - } - - 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 deleted file mode 100644 index e390f32..0000000 --- a/src/Models/HarmonyHub.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { HarmonyDevice } from './HarmonyDevice'; -const Harmony = require("harmony-websocket"); -import { ICommand } from './IDevice'; -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; - - 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}`); - } - } - - private connect = async (): Promise => { - await this._harmony.Connect(this._ip); - } -} \ No newline at end of file diff --git a/src/Models/index.ts b/src/Models/index.ts deleted file mode 100644 index a37e649..0000000 --- a/src/Models/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './IDevice'; \ No newline at end of file diff --git a/src/Util/index.ts b/src/Util/index.ts deleted file mode 100644 index 19f0e10..0000000 --- a/src/Util/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./Callbackify"; -export * from "./Sleep"; diff --git a/src/Accessories/ControlUnit.ts b/src/accessories/controlUnit.ts similarity index 88% rename from src/Accessories/ControlUnit.ts rename to src/accessories/controlUnit.ts index bce2a5d..f6def08 100644 --- a/src/Accessories/ControlUnit.ts +++ b/src/accessories/controlUnit.ts @@ -1,8 +1,10 @@ -import { PlatformAccessory, RemoteController, Service } from "homebridge"; -import HarmonyDataProvider from "../DataProviders/HarmonyDataProvider"; -import { IActivity } from "../Models/Config"; +import { PlatformAccessory, Service } from "homebridge"; +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 +29,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 +133,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] ); @@ -146,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; }; @@ -155,14 +159,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 +176,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 +234,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 similarity index 86% rename from src/Accessories/DeviceButton.ts rename to src/accessories/deviceButton.ts index cf02bf4..ddf8574 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 { @@ -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/index.ts b/src/accessories/index.ts new file mode 100644 index 0000000..bf68e76 --- /dev/null +++ b/src/accessories/index.ts @@ -0,0 +1,2 @@ +export { ControlUnit } from "./controlUnit"; +export { DeviceButton } from "./deviceButton"; diff --git a/src/Accessories/Sequence.ts b/src/accessories/sequence.ts similarity index 88% rename from src/Accessories/Sequence.ts rename to src/accessories/sequence.ts index ea6992c..a92a246 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/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"; +import { sleep } from "../util"; export class Sequence { private _devices: { [deviceName: string]: HarmonyDevice }; @@ -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 new file mode 100644 index 0000000..17ae67f --- /dev/null +++ b/src/dataProviders/harmonyDataProvider.ts @@ -0,0 +1,166 @@ +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"; + +@injectable() +export class HarmonyDataProvider { + private _hubs: { [hubName: string]: HarmonyHub } = {}; + private _deviceConfigByName: { [deviceName: string]: IDeviceConfig } = {}; + + constructor( + @inject("IConfig") private _config: IConfig, + @inject("log") private _log: Logging + ) { + _config.Devices.forEach((deviceConfig: IDeviceConfig) => { + this._deviceConfigByName[deviceConfig.Name] = deviceConfig; + }); + + this.connect(_config.Hubs); + this.emitInfo(); + } + + 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 this.powerOnDevice(device); + } + } + }) + ); + } + + 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 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 getDeviceByName(deviceName: string): HarmonyDevice { + let device: HarmonyDevice | undefined; + try { + device = + this._hubs[this._deviceConfigByName[deviceName].Hub].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}`); + }); + }); + }); + } + } + + 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/dataProviders/stateDataProvider.ts b/src/dataProviders/stateDataProvider.ts new file mode 100644 index 0000000..f4d89b8 --- /dev/null +++ b/src/dataProviders/stateDataProvider.ts @@ -0,0 +1,31 @@ +import { 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/activityState.ts b/src/models/activityState.ts new file mode 100644 index 0000000..96db22e --- /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/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/deviceConfig.ts b/src/models/config/deviceConfig.ts new file mode 100644 index 0000000..116b3eb --- /dev/null +++ b/src/models/config/deviceConfig.ts @@ -0,0 +1,5 @@ +export interface IDeviceConfig { + Name: string; + Hub: string; + Overrides: Array<{ Command: string; Override: string }>; +} 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 new file mode 100644 index 0000000..da105a2 --- /dev/null +++ b/src/models/config/index.ts @@ -0,0 +1,5 @@ +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/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/harmonyDevice.ts b/src/models/harmonyDevice.ts new file mode 100644 index 0000000..aefc87e --- /dev/null +++ b/src/models/harmonyDevice.ts @@ -0,0 +1,47 @@ +import { ICommand } from "./device"; +import { sleep } from "../util/sleep"; + +export interface IHarmonyDeviceProps { + id: string; + name: string; + harmony: any; + log: any; + commands: { [name: string]: ICommand }; +} + +export class HarmonyDevice { + private _commands: { [name: string]: ICommand } = {}; + private _on: boolean; + + constructor(props: IHarmonyDeviceProps) { + this.id = props.id; + this.name = props.name; + this._on = false; + this._commands = props.commands; + } + + public id: string; + public name: string; + + public get on(): boolean { + return this._on; + } + + public set on(value: boolean) { + this._on = value; + } + + 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]; + } +} diff --git a/src/models/harmonyHub.ts b/src/models/harmonyHub.ts new file mode 100644 index 0000000..0d2c9e4 --- /dev/null +++ b/src/models/harmonyHub.ts @@ -0,0 +1,94 @@ +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 } = {}; + 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; + } + + 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); + }; + + 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/models/index.ts b/src/models/index.ts new file mode 100644 index 0000000..01a9ec2 --- /dev/null +++ b/src/models/index.ts @@ -0,0 +1 @@ +export * from "./device"; diff --git a/src/platform.ts b/src/platform.ts index 27c9144..7bed9bf 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -1,3 +1,4 @@ +import "reflect-metadata"; import { API, Characteristic, @@ -7,14 +8,17 @@ 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/ISequence"; -import { HarmonyDevice } from "./Models/HarmonyDevice"; -import { HarmonyHub } from "./Models/HarmonyHub"; +import { ControlUnit, DeviceButton } from "./accessories"; +import { Sequence } from "./accessories/sequence"; +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 { HarmonyDataProvider } from "./dataProviders/harmonyDataProvider"; +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( @@ -23,58 +27,18 @@ export class Platform implements DynamicPlatformPlugin { public readonly api: API ) { this.log.debug("Finished initializing platform:", config.name); - this.config = config as unknown as IConfig; - //construct data provider - const dataProvider = new HarmonyDataProvider({ - hubs: this.config.Hubs, - deviceConfigs: this.config.Devices, - matrix: this.config.Matrix, - log: this.log, - }); + this.register(); 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; - - 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; @@ -84,15 +48,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 +64,14 @@ export class Platform implements DynamicPlatformPlugin { existingAccessory.displayName ); - new ControlUnit(this, existingAccessory, dataProvider, unit.Activities); + new ControlUnit( + this, + existingAccessory, + container.resolve(ActivityService), + container.resolve(CommandService), + container.resolve(VolumeService), + unit.Activities + ); this.api.publishExternalAccessories(PLUGIN_NAME, [existingAccessory]); console.log("Publishing external accessory: " + uuid); @@ -116,7 +84,14 @@ 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(ActivityService), + container.resolve(CommandService), + container.resolve(VolumeService), + unit.Activities + ); this.api.publishExternalAccessories(PLUGIN_NAME, [accessory]); console.log("Publishing external accessory: " + uuid); @@ -128,9 +103,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 +113,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 +129,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 +148,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 +158,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 +174,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 +215,14 @@ 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); + container.registerSingleton(ActivityService); + container.registerSingleton(StateDataProvider); + container.registerSingleton(CommandService); + } } diff --git a/src/services/activityService.ts b/src/services/activityService.ts new file mode 100644 index 0000000..8549684 --- /dev/null +++ b/src/services/activityService.ts @@ -0,0 +1,226 @@ +import { Logging } from "homebridge"; +import { inject, injectable } from "tsyringe"; +import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider"; +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(HarmonyDataProvider) + private _harmonyDataProvider: HarmonyDataProvider, + @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.powerOnDevices(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.powerOffDevices(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.powerOffDevices(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) { + 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; + } + + 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.getDeviceByName(value.DeviceName); + } + ); + } + + private async assignDeviceInput(activity: IActivity): Promise { + await Promise.all( + activity.DeviceSetupList.map(async (value: IDeviceSetupItem) => { + let device: HarmonyDevice = this._harmonyDataProvider.getDeviceByName( + value.DeviceName + ); + + if (device && device.supportsCommand(`Input${value.Input}`)) { + await this._harmonyDataProvider.sendCommand( + `Input${value.Input}`, + device + ); + } + }) + ); + } + + 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.getDeviceByName( + this._config.Matrix.DeviceName + ); + + //Route hdmi + if ( + matrixDevice.supportsCommand(inputCommandName) && + matrixDevice.supportsCommand(outputCommandName) + ) { + 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 new file mode 100644 index 0000000..be4e841 --- /dev/null +++ b/src/services/commandService.ts @@ -0,0 +1,71 @@ +import { inject, injectable } from "tsyringe"; +import { RemoteKey } from "../accessories/controlUnit"; +import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider"; +import { StateDataProvider } from "../dataProviders/stateDataProvider"; +import { HarmonyDevice } from "../models/harmonyDevice"; + +@injectable() +export class CommandService { + constructor( + @inject(StateDataProvider) private _stateDataProvider: StateDataProvider, + @inject(HarmonyDataProvider) + private _harmonyDataProvider: HarmonyDataProvider + ) {} + + /** + * 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.getDeviceByName( + 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 this._harmonyDataProvider.sendCommand(commandName, device); + } + }; +} diff --git a/src/services/volumeService.ts b/src/services/volumeService.ts new file mode 100644 index 0000000..5a48961 --- /dev/null +++ b/src/services/volumeService.ts @@ -0,0 +1,45 @@ +import { inject, injectable } from "tsyringe"; +import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider"; +import { StateDataProvider } from "../dataProviders/stateDataProvider"; +import { HarmonyDevice } from "../models/harmonyDevice"; + +@injectable() +export class VolumeService { + constructor( + @inject(StateDataProvider) private _stateDataProvider: StateDataProvider, + @inject(HarmonyDataProvider) + private _harmonyDataProvider: HarmonyDataProvider + ) {} + + /** + * 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.getDeviceByName(currentActivity.VolumeDevice); + await this._harmonyDataProvider.sendCommand( + volumeUpCommand, + volumeDevice + ); + } + }; + + /** + * 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.getDeviceByName(currentActivity.VolumeDevice); + await this._harmonyDataProvider.sendCommand( + volumeDownCommand, + volumeDevice + ); + } + }; +} 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/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 new file mode 100644 index 0000000..fcebc7c --- /dev/null +++ b/src/util/index.ts @@ -0,0 +1,2 @@ +export * from "./callbackify"; +export * from "./sleep"; 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 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__/*"]