diff --git a/plugins/mqtt/.vscode/launch.json b/plugins/mqtt/.vscode/launch.json index 0669f79b4..2b82045aa 100644 --- a/plugins/mqtt/.vscode/launch.json +++ b/plugins/mqtt/.vscode/launch.json @@ -16,7 +16,7 @@ "sourceMaps": true, "localRoot": "${workspaceFolder}/out", "remoteRoot": "/plugin/", - "type": "pwa-node" + "type": "node" } ] } \ No newline at end of file diff --git a/plugins/mqtt/package-lock.json b/plugins/mqtt/package-lock.json index 9f400c5a7..ba24a209e 100644 --- a/plugins/mqtt/package-lock.json +++ b/plugins/mqtt/package-lock.json @@ -1,20 +1,18 @@ { "name": "@scrypted/mqtt", - "version": "0.0.52", + "version": "0.0.53", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/mqtt", - "version": "0.0.52", - "hasInstallScript": true, + "version": "0.0.53", "dependencies": { "@types/node": "^16.6.1", "aedes": "^0.46.1", "axios": "^0.23.0", "mqtt": "^4.2.8", "nunjucks": "^3.2.3", - "typescript": "^4.4.3", "websocket-stream": "^5.5.2" }, "devDependencies": { @@ -41,7 +39,7 @@ }, "../../sdk": { "name": "@scrypted/sdk", - "version": "0.0.199", + "version": "0.0.202", "dev": true, "license": "ISC", "dependencies": { @@ -515,9 +513,9 @@ } }, "node_modules/minimist": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + "version": "1.2.6", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.6.tgz", + "integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==" }, "node_modules/mqemitter": { "version": "4.4.1", @@ -714,18 +712,6 @@ "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=" }, - "node_modules/typescript": { - "version": "4.4.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz", - "integrity": "sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==", - "bin": { - "tsc": "bin/tsc", - "tsserver": "bin/tsserver" - }, - "engines": { - "node": ">=4.2.0" - } - }, "node_modules/ultron": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz", @@ -1217,9 +1203,9 @@ } }, "minimist": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + "version": "1.2.6", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.6.tgz", + "integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==" }, "mqemitter": { "version": "4.4.1", @@ -1364,11 +1350,6 @@ "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=" }, - "typescript": { - "version": "4.4.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz", - "integrity": "sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==" - }, "ultron": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz", diff --git a/plugins/mqtt/package.json b/plugins/mqtt/package.json index 014b1eb25..edcb4d3a6 100644 --- a/plugins/mqtt/package.json +++ b/plugins/mqtt/package.json @@ -33,7 +33,6 @@ "axios": "^0.23.0", "mqtt": "^4.2.8", "nunjucks": "^3.2.3", - "typescript": "^4.4.3", "websocket-stream": "^5.5.2" }, "devDependencies": { @@ -41,5 +40,5 @@ "@scrypted/common": "file:../../common", "@types/nunjucks": "^3.2.0" }, - "version": "0.0.52" + "version": "0.0.53" } diff --git a/plugins/mqtt/src/autodiscovery/autodiscovery.ts b/plugins/mqtt/src/autodiscovery/autodiscovery.ts index 5ce0273f5..13bc724a9 100644 --- a/plugins/mqtt/src/autodiscovery/autodiscovery.ts +++ b/plugins/mqtt/src/autodiscovery/autodiscovery.ts @@ -57,8 +57,6 @@ typeMap.set('binary_sensor', { // } export class MqttAutoDiscoveryProvider extends MqttDeviceBase implements DeviceProvider { - client: MqttClient; - pathname: string; devices = new Map(); constructor(nativeId: string) { diff --git a/plugins/mqtt/src/main.ts b/plugins/mqtt/src/main.ts index 05368df13..ae1bc996f 100644 --- a/plugins/mqtt/src/main.ts +++ b/plugins/mqtt/src/main.ts @@ -165,14 +165,14 @@ class MqttPublisherMixin extends SettingsMixinDeviceBase { if (typeof str === 'object') str = JSON.stringify(str); - this.client.publish(`${this.pathname}/${this.id}/${property}`, str?.toString() || ''); + this.client.publish(`${this.pathname}/${property}`, str?.toString() || ''); } else { let str = eventData; if (typeof str === 'object') str = JSON.stringify(str); - this.client.publish(`${this.pathname}/${this.id}/${eventDetails.eventInterface}`, str?.toString() || ''); + this.client.publish(`${this.pathname}/${eventDetails.eventInterface}`, str?.toString() || ''); } }) } @@ -204,14 +204,13 @@ class MqttPublisherMixin extends SettingsMixinDeviceBase { async putMixinSetting(key: string, value: string | number | boolean) { if (key === 'url') { - let url = value.toString(); - if (!url.endsWith('/')) - url += '/'; - this.storage.setItem(key, url); + this.storage.setItem(key, value.toString()); } else { this.storage.setItem(key, value.toString()); } + this.client.end(); + this.connectClient(); } connectClient() { @@ -225,15 +224,16 @@ class MqttPublisherMixin extends SettingsMixinDeviceBase { url = new URL(urlString); username = this.storage.getItem('username') || undefined; password = this.storage.getItem('password') || undefined; + this.pathname = url.pathname.substring(1); } else { const tcpPort = this.provider.storage.getItem('tcpPort') || ''; username = this.provider.storage.getItem('username') || undefined; password = this.provider.storage.getItem('password') || undefined; url = new URL(`mqtt://localhost:${tcpPort}/scrypted`); + this.pathname = `${url.pathname.substring(1)}/${this.id}`; } - this.pathname = url.pathname.substring(1); const urlWithoutPath = new URL(url); urlWithoutPath.pathname = ''; @@ -252,7 +252,7 @@ class MqttPublisherMixin extends SettingsMixinDeviceBase { if (typeof str === 'object') str = JSON.stringify(str); - client.publish(`${this.pathname}/${this.id}/${prop}`, str?.toString() || ''); + client.publish(`${this.pathname}/${prop}`, str?.toString() || ''); } } }) @@ -395,7 +395,7 @@ class MqttProvider extends ScryptedDeviceBase implements DeviceProvider, Setting nativeId, name: name, interfaces: [ScryptedInterface.Scriptable, - ScryptedInterface.Settings, + ScryptedInterface.Settings, '@scrypted/mqtt' ], type: ScryptedDeviceType.Unknown,