mqtt: respect custom mqtt urls

This commit is contained in:
Koushik Dutta
2022-08-09 17:24:47 -07:00
parent 67ca3946c8
commit a81079de5c
5 changed files with 20 additions and 42 deletions

View File

@@ -16,7 +16,7 @@
"sourceMaps": true,
"localRoot": "${workspaceFolder}/out",
"remoteRoot": "/plugin/",
"type": "pwa-node"
"type": "node"
}
]
}

View File

@@ -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",

View File

@@ -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"
}

View File

@@ -57,8 +57,6 @@ typeMap.set('binary_sensor', {
// }
export class MqttAutoDiscoveryProvider extends MqttDeviceBase implements DeviceProvider {
client: MqttClient;
pathname: string;
devices = new Map<string, MqttAutoDiscoveryDevice>();
constructor(nativeId: string) {

View File

@@ -165,14 +165,14 @@ class MqttPublisherMixin extends SettingsMixinDeviceBase<any> {
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<any> {
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<any> {
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<any> {
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,