@@ -58,7 +58,38 @@ class NspanelLovelaceUi extends utils.Adapter {
58
58
native : { } ,
59
59
} ) ;
60
60
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
+ }
62
93
63
94
await generateAliasDocumentation ( ) ;
64
95
if ( this . config . testCase ) {
@@ -214,26 +245,6 @@ class NspanelLovelaceUi extends utils.Adapter {
214
245
this . log . warn ( 'No pin entered for the service page! Please set a pin in the admin settings!' ) ;
215
246
}
216
247
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
-
237
248
if ( ! ( this . config . mqttIp && this . config . mqttPort && this . config . mqttUsername && this . config . mqttPassword ) ) {
238
249
this . log . error ( 'Invalid admin configuration for mqtt!' ) ;
239
250
this . testSuccessful = false ;
@@ -453,7 +464,7 @@ class NspanelLovelaceUi extends utils.Adapter {
453
464
454
465
// If you need to accept messages in your adapter, uncomment the following block and the corresponding line in the constructor.
455
466
// /**
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, .
457
468
// * Using this method requires "common.messagebox" property to be set to true in io-package.json
458
469
// */
459
470
private async onMessage ( obj : ioBroker . Message ) : Promise < void > {
@@ -500,30 +511,6 @@ class NspanelLovelaceUi extends utils.Adapter {
500
511
break ;
501
512
}
502
513
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
- }*/
527
514
const device = { id : '' , name : obj . message . name , topic : obj . message . topic } ;
528
515
529
516
const mqtt = new MQTT . MQTTClientClass (
@@ -537,40 +524,29 @@ class NspanelLovelaceUi extends utils.Adapter {
537
524
} ,
538
525
) ;
539
526
this . timeoutAdmin = this . setTimeout (
540
- async mqtt => {
541
- let rCount = 0 ;
527
+ async ( mqtt , obj ) => {
542
528
if ( mqtt ) {
543
529
if ( ! device . id ) {
544
- rCount ++ ;
545
530
mqtt . subscript (
546
531
`${ device . topic } /stat/STATUS0` ,
547
532
( _topic : string , _message : string ) => {
548
533
const msg = JSON . parse ( _message ) as STATUS0 ;
549
534
if ( msg . StatusNET ) {
550
535
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 ) ;
551
540
}
552
- rCount -- ;
553
541
} ,
554
542
) ;
555
543
void mqtt . publish ( `${ device . topic } /cmnd/STATUS0` , '' ) ;
556
544
}
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 ) ;
570
545
}
571
546
} ,
572
547
500 ,
573
548
mqtt ,
549
+ obj ,
574
550
) ;
575
551
576
552
break ;
@@ -597,7 +573,7 @@ class NspanelLovelaceUi extends utils.Adapter {
597
573
` MqttHost ${ obj . message . mqttServer ? obj . message . internalServerIp : obj . message . mqttIp } ;` +
598
574
` MqttPort ${ obj . message . mqttPort } ; MqttUser ${ obj . message . mqttUsername } ; MqttPassword ${ obj . message . mqttPassword } ;` +
599
575
` 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;` +
601
577
` Restart 1` ;
602
578
const u = new URL (
603
579
`http://${ obj . message . tasmotaIP } /cm?&cmnd=Backlog${ url
@@ -618,6 +594,9 @@ class NspanelLovelaceUi extends utils.Adapter {
618
594
}
619
595
}
620
596
}
597
+ if ( obj . callback ) {
598
+ this . sendTo ( obj . from , obj . command , [ ] , obj . callback ) ;
599
+ }
621
600
break ;
622
601
//Backlog UrlFetch https://raw.githubusercontent.com/joBr99/nspanel-lovelace-ui/main/tasmota/autoexec.be; Restart 1
623
602
//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 {
640
619
}
641
620
}
642
621
}
622
+ if ( obj . callback ) {
623
+ this . sendTo ( obj . from , obj . command , [ ] , obj . callback ) ;
624
+ }
643
625
break ;
644
626
}
645
627
case 'tftInstallSendTo' : {
0 commit comments