Skip to content

Commit 3109c2c

Browse files
author
ticaki
committedMar 9, 2025
update admin - tweak mqtt server
1 parent 096a1a0 commit 3109c2c

File tree

5 files changed

+110
-107
lines changed

5 files changed

+110
-107
lines changed
 

‎README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ Sind natürlich alles Dataitems
306306
### **WORK IN PROGRESS**
307307
-->
308308
### **WORK IN PROGRESS**
309-
- (ticaki)
309+
- (ticaki) check if port is free for mqtt server - and move it at start
310+
- (ticaki) check and tweak role info
311+
- (ticaki) fixed: entities and grid icons/text
310312
- (ticaki) tools for tasmota added to admin
311313
- (ticaki) tweak panel response time
312314
- (ticaki) 'switch' added

‎admin/jsonConfig.json

+13-1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,18 @@
276276
"lg": 4,
277277
"xl": 4
278278
},
279+
"_tasmotaName":{
280+
"type": "text",
281+
"label": "_tasmotaName",
282+
"validator": "",
283+
"hidden": "data._selectTool !== '1'",
284+
"default": "",
285+
"xs": 12,
286+
"sm": 12,
287+
"md": 6,
288+
"lg": 4,
289+
"xl": 4
290+
},
279291
"_tasmotaTopic": {
280292
"type": "text",
281293
"label": "tasmotaTopic",
@@ -290,7 +302,7 @@
290302
"_tasmotaSendTo": {
291303
"type": "sendTo",
292304
"label": "tasmotaSendTo",
293-
"jsonData": "{\"tasmotaIP\": \"${data._tasmotaIP}\", \"tasmotaTopic\":\"${data._tasmotaTopic}\", \"mqttServer\": \"${data.mqttServer}\", \"mqttIp\": \"${data.mqttIp}\", \"mqttPort\": \"${data.mqttPort}\", \"mqttUsername\": \"${data.mqttUsername}\", \"mqttPassword\": \"${data.mqttPassword}\", \"internalServerIp\": \"${data.internalServerIp}\" }",
305+
"jsonData": "{ \"tasmotaName\": \"${data._tasmotaName}\", \"tasmotaIP\": \"${data._tasmotaIP}\", \"tasmotaTopic\":\"${data._tasmotaTopic}\", \"mqttServer\": \"${data.mqttServer}\", \"mqttIp\": \"${data.mqttIp}\", \"mqttPort\": \"${data.mqttPort}\", \"mqttUsername\": \"${data.mqttUsername}\", \"mqttPassword\": \"${data.mqttPassword}\", \"internalServerIp\": \"${data.internalServerIp}\" }",
294306
"showProcess": true,
295307
"command": "tasmotaSendTo",
296308
"disabled": "return data._tasmotaIP == null || !data.internalServerIp || data.internalServerIp.search(/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/g) === -1 || data.internalServerIp === '127.0.0.1'|| !data._tasmotaTopic || data._tasmotaTopic.search(/^[a-zA-Z][\\w/]+$/g) === -1 || !data.mqttServer || data._tasmotaIP.search(/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/g) === -1 || !data.mqttPort || !data.mqttUsername || !data.mqttPassword || data.mqttServer && !data.internalServerIp || !data.mqttServer && !data.mqttIp",

‎build/main.js

+45-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎build/main.js.map

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/main.ts

+46-64
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,38 @@ class NspanelLovelaceUi extends utils.Adapter {
5858
native: {},
5959
});
6060
this.library = new Library(this);
61-
await this.delay(2000);
61+
62+
if (this.config.mqttServer && this.config.mqttPort && this.config.mqttUsername) {
63+
this.config.mqttPassword = this.config.mqttPassword || '1234';
64+
65+
const port = await this.getPortAsync(this.config.mqttPort);
66+
if (port !== this.config.mqttPort) {
67+
this.log.error(`Port ${this.config.mqttPort} is already in use!`);
68+
this.log.error(`Please change the port in the admin settings!`);
69+
this.log.error('Stopping adapter!');
70+
if (this.stop) {
71+
await this.stop();
72+
}
73+
return;
74+
}
75+
this.mqttServer = new MQTT.MQTTServerClass(
76+
this,
77+
this.config.mqttPort,
78+
this.config.mqttUsername,
79+
this.config.mqttPassword,
80+
'./mqtt',
81+
);
82+
this.config.mqttIp = '127.0.0.1';
83+
await this.delay(100);
84+
let c = 0;
85+
while (!this.mqttServer.ready) {
86+
this.log.debug('Wait for mqttServer');
87+
await this.delay(1000);
88+
if (c++ > 6) {
89+
throw new Error('mqttServer not ready!');
90+
}
91+
}
92+
}
6293

6394
await generateAliasDocumentation();
6495
if (this.config.testCase) {
@@ -214,26 +245,6 @@ class NspanelLovelaceUi extends utils.Adapter {
214245
this.log.warn('No pin entered for the service page! Please set a pin in the admin settings!');
215246
}
216247

217-
if (this.config.mqttServer && this.config.mqttPort && this.config.mqttUsername) {
218-
this.config.mqttPassword = this.config.mqttPassword || '1234';
219-
this.mqttServer = new MQTT.MQTTServerClass(
220-
this,
221-
this.config.mqttPort,
222-
this.config.mqttUsername,
223-
this.config.mqttPassword,
224-
'./mqtt',
225-
);
226-
this.config.mqttIp = '127.0.0.1';
227-
let c = 0;
228-
while (!this.mqttServer.ready) {
229-
this.log.debug('Wait for mqttServer');
230-
await this.delay(1000);
231-
if (c++ > 6) {
232-
throw new Error('mqttServer not ready!');
233-
}
234-
}
235-
}
236-
237248
if (!(this.config.mqttIp && this.config.mqttPort && this.config.mqttUsername && this.config.mqttPassword)) {
238249
this.log.error('Invalid admin configuration for mqtt!');
239250
this.testSuccessful = false;
@@ -453,7 +464,7 @@ class NspanelLovelaceUi extends utils.Adapter {
453464

454465
// If you need to accept messages in your adapter, uncomment the following block and the corresponding line in the constructor.
455466
// /**
456-
// * Somee message was sent to this instance over message box. Used by email, pushover, text2speech, ........
467+
// * Somee message was sent to this instance over message box. Used by email, pushover, text2speech, .
457468
// * Using this method requires "common.messagebox" property to be set to true in io-package.json
458469
// */
459470
private async onMessage(obj: ioBroker.Message): Promise<void> {
@@ -500,30 +511,6 @@ class NspanelLovelaceUi extends utils.Adapter {
500511
break;
501512
}
502513
case 'RefreshDevices': {
503-
/*const view = await this.getObjectViewAsync('system', 'device', {
504-
startkey: `${this.namespace}.panels.`,
505-
endkey: `${this.namespace}.panels.\u9999`,
506-
});
507-
let devices: any = {};
508-
if (view && view.rows) {
509-
devices = { panels: [] };
510-
for (const panel of view.rows) {
511-
const result = { id: '', name: '', topic: '', removeIt: false };
512-
const p = await this.getForeignObjectAsync(panel.id);
513-
if (
514-
p &&
515-
p.native &&
516-
p.native.name &&
517-
p.native.configName === obj.message.name &&
518-
p.native.topic === obj.message.topic
519-
) {
520-
result.id = ''; //p.native.name;
521-
result.name = p.native.configName;
522-
result.topic = p.native.topic;
523-
devices.panels.push(result);
524-
}
525-
}
526-
}*/
527514
const device = { id: '', name: obj.message.name, topic: obj.message.topic };
528515

529516
const mqtt = new MQTT.MQTTClientClass(
@@ -537,40 +524,29 @@ class NspanelLovelaceUi extends utils.Adapter {
537524
},
538525
);
539526
this.timeoutAdmin = this.setTimeout(
540-
async mqtt => {
541-
let rCount = 0;
527+
async (mqtt, obj) => {
542528
if (mqtt) {
543529
if (!device.id) {
544-
rCount++;
545530
mqtt.subscript(
546531
`${device.topic}/stat/STATUS0`,
547532
(_topic: string, _message: string) => {
548533
const msg = JSON.parse(_message) as STATUS0;
549534
if (msg.StatusNET) {
550535
device.id = this.library.cleandp(msg.StatusNET.Mac, false, true);
536+
this.log.debug(`Device found: ${device.id}`);
537+
}
538+
if (obj.callback) {
539+
this.sendTo(obj.from, obj.command, { native: device }, obj.callback);
551540
}
552-
rCount--;
553541
},
554542
);
555543
void mqtt.publish(`${device.topic}/cmnd/STATUS0`, '');
556544
}
557-
558-
const _waitForFinish = (count: number): void => {
559-
if (count > 10 || rCount === 0) {
560-
if (obj.callback) {
561-
this.sendTo(obj.from, obj.command, { native: device }, obj.callback);
562-
}
563-
mqtt.destroy();
564-
return;
565-
}
566-
567-
this.timeoutAdmin = this.setTimeout(_waitForFinish, 500, ++count);
568-
};
569-
_waitForFinish(0);
570545
}
571546
},
572547
500,
573548
mqtt,
549+
obj,
574550
);
575551

576552
break;
@@ -597,7 +573,7 @@ class NspanelLovelaceUi extends utils.Adapter {
597573
` MqttHost ${obj.message.mqttServer ? obj.message.internalServerIp : obj.message.mqttIp};` +
598574
` MqttPort ${obj.message.mqttPort}; MqttUser ${obj.message.mqttUsername}; MqttPassword ${obj.message.mqttPassword};` +
599575
` FullTopic ${`${obj.message.tasmotaTopic}/%prefix%/`.replaceAll('//', '/')};` +
600-
` MqttRetry 10; WebLog 2; template {"NAME":"NSPanel","GPIO":[0,0,0,0,3872,0,0,0,0,0,32,0,0,0,0,225,0,480,224,1,0,0,0,33,0,0,0,0,0,0,0,0,0,0,4736,0],"FLAG":0,"BASE":1}; Module 0;` +
576+
` MqttRetry 10; FriendlyName ${obj.message.tasmotaName}; Hostname ${obj.message.tasmotaName}; WebLog 2; template {"NAME":"${obj.message.tasmotaName}","GPIO":[0,0,0,0,3872,0,0,0,0,0,32,0,0,0,0,225,0,480,224,1,0,0,0,33,0,0,0,0,0,0,0,0,0,0,4736,0],"FLAG":0,"BASE":1}; Module 0;` +
601577
` Restart 1`;
602578
const u = new URL(
603579
`http://${obj.message.tasmotaIP}/cm?&cmnd=Backlog${url
@@ -618,6 +594,9 @@ class NspanelLovelaceUi extends utils.Adapter {
618594
}
619595
}
620596
}
597+
if (obj.callback) {
598+
this.sendTo(obj.from, obj.command, [], obj.callback);
599+
}
621600
break;
622601
//Backlog UrlFetch https://raw.githubusercontent.com/joBr99/nspanel-lovelace-ui/main/tasmota/autoexec.be; Restart 1
623602
//Backlog UpdateDriverVersion https://raw.githubusercontent.com/joBr99/nspanel-lovelace-ui/main/tasmota/autoexec.be; Restart 1
@@ -640,6 +619,9 @@ class NspanelLovelaceUi extends utils.Adapter {
640619
}
641620
}
642621
}
622+
if (obj.callback) {
623+
this.sendTo(obj.from, obj.command, [], obj.callback);
624+
}
643625
break;
644626
}
645627
case 'tftInstallSendTo': {

0 commit comments

Comments
 (0)