@@ -25,12 +25,12 @@ import {
25
25
ConnectivityProtocol ,
26
26
ConnectivitySettings ,
27
27
connectivitySettingsKey ,
28
+ generalSettingsKey ,
29
+ GeneralSettings ,
28
30
WebSocketSettings ,
29
- webSocketSettingsKey
30
31
} from '@shared/models/settings.models' ;
31
32
import { SettingsService } from '@core/http/settings.service' ;
32
33
import { takeUntil } from 'rxjs/operators' ;
33
- import { isUndefined } from '@core/utils' ;
34
34
import { MqttJsClientService } from '@core/http/mqtt-js-client.service' ;
35
35
import { MatCard , MatCardHeader , MatCardTitle , MatCardContent } from '@angular/material/card' ;
36
36
import { TranslateModule } from '@ngx-translate/core' ;
@@ -54,21 +54,24 @@ import { HasConfirmForm } from '@core/guards/confirm-on-exit.guard';
54
54
export class GeneralSettingsComponent extends PageComponent implements OnDestroy , HasConfirmForm {
55
55
56
56
generalSettingsForm : UntypedFormGroup ;
57
+ webSocketSettingsForm : UntypedFormGroup ;
57
58
connectivitySettingsForm : UntypedFormGroup ;
59
+
58
60
protocol = 'mqtt' ;
59
61
listenerPortMap = new Map < ConnectivityProtocol , number > ( ) ;
60
62
63
+ private generalSettings : AdminSettings < GeneralSettings > ;
64
+ private webSocketSettings : AdminSettings < WebSocketSettings > ;
61
65
private connectivitySettings : AdminSettings < ConnectivitySettings > ;
62
- private generalSettings : AdminSettings < WebSocketSettings > ;
66
+
63
67
private destroy$ = new Subject < void > ( ) ;
64
68
65
69
constructor ( protected store : Store < AppState > ,
66
70
private settingsService : SettingsService ,
67
71
private mqttJsClientService : MqttJsClientService ,
68
72
public fb : UntypedFormBuilder ) {
69
73
super ( store ) ;
70
- this . buildConnectivitySettingsForm ( ) ;
71
- this . buildWebSocketSettingsForm ( ) ;
74
+ this . buildForms ( ) ;
72
75
this . getSettings ( ) ;
73
76
}
74
77
@@ -77,57 +80,22 @@ export class GeneralSettingsComponent extends PageComponent implements OnDestroy
77
80
super . ngOnDestroy ( ) ;
78
81
}
79
82
80
- private buildConnectivitySettingsForm ( ) {
81
- this . connectivitySettingsForm = this . fb . group ( {
82
- ws : this . buildConnectivityInfoForm ( 'ws' ) ,
83
- wss : this . buildConnectivityInfoForm ( 'wss' ) ,
84
- mqtt : this . buildConnectivityInfoForm ( 'mqtt' ) ,
85
- mqtts : this . buildConnectivityInfoForm ( 'mqtts' ) ,
86
- } ) ;
87
- }
88
-
89
- private buildConnectivityInfoForm ( protocol : ConnectivityProtocol ) : UntypedFormGroup {
90
- const formGroup = this . fb . group ( {
91
- enabled : [ false , [ ] ] ,
92
- host : [ { value : '' , disabled : true } , [ Validators . required ] ] ,
93
- port : [ { value : null , disabled : true } , [ Validators . min ( 1 ) , Validators . max ( 65535 ) , Validators . pattern ( '[0-9]*' ) , Validators . required ] ]
94
- } ) ;
95
- formGroup . get ( 'enabled' ) . valueChanges . pipe (
96
- takeUntil ( this . destroy$ )
97
- ) . subscribe ( value => {
98
- if ( value ) {
99
- formGroup . get ( 'host' ) . enable ( { emitEvent : false } ) ;
100
- formGroup . get ( 'port' ) . enable ( { emitEvent : false } ) ;
101
- this . settingsService . getListenerPort ( protocol ) . subscribe ( value => this . listenerPortMap . set ( protocol , value ) ) ;
102
- } else {
103
- formGroup . get ( 'host' ) . disable ( { emitEvent : false } ) ;
104
- formGroup . get ( 'port' ) . disable ( { emitEvent : false } ) ;
105
- this . listenerPortMap . delete ( protocol ) ;
106
- }
107
- } ) ;
108
- return formGroup ;
109
- }
110
-
111
- private buildWebSocketSettingsForm ( ) {
112
- this . generalSettingsForm = this . fb . group ( {
113
- isLoggingEnabled : [ null , [ ] ] ,
114
- maxMessages : [ null , [ Validators . required ] ]
115
- } ) ;
116
- }
117
-
118
83
saveGeneralSettings ( ) {
119
- let generalSettings : AdminSettings < WebSocketSettings > = JSON . parse ( JSON . stringify ( this . generalSettings ) ) ;
120
- const maxMessagesChanged = this . generalSettings . jsonValue . maxMessages !== this . generalSettingsForm . value . maxMessages ;
121
- if ( isUndefined ( this . generalSettings ) ) {
122
- generalSettings = {
123
- key : webSocketSettingsKey ,
124
- jsonValue : this . generalSettingsForm . value
125
- } ;
126
- }
84
+ const generalSettings : AdminSettings < GeneralSettings > = JSON . parse ( JSON . stringify ( this . generalSettings ) ) ;
127
85
generalSettings . jsonValue = { ...generalSettings . jsonValue , ...this . generalSettingsForm . value } ;
128
86
this . settingsService . saveAdminSettings ( generalSettings )
129
87
. subscribe ( settings => {
130
88
this . processGeneralSettings ( settings ) ;
89
+ } ) ;
90
+ }
91
+
92
+ saveWebSocketSettings ( ) {
93
+ const webSocketSettings : AdminSettings < WebSocketSettings > = JSON . parse ( JSON . stringify ( this . webSocketSettings ) ) ;
94
+ const maxMessagesChanged = this . webSocketSettings . jsonValue . maxMessages !== this . webSocketSettingsForm . value . maxMessages ;
95
+ webSocketSettings . jsonValue = { ...webSocketSettings . jsonValue , ...this . webSocketSettingsForm . value } ;
96
+ this . settingsService . saveAdminSettings ( webSocketSettings )
97
+ . subscribe ( settings => {
98
+ this . processWebSocketSettings ( settings ) ;
131
99
if ( maxMessagesChanged ) {
132
100
this . mqttJsClientService . clearAllMessages ( ) ;
133
101
}
@@ -156,6 +124,11 @@ export class GeneralSettingsComponent extends PageComponent implements OnDestroy
156
124
this . generalSettingsForm . reset ( generalSettings ) ;
157
125
}
158
126
127
+ discardWebSocketSettings ( ) : void {
128
+ const webSocketSettings = this . webSocketSettings . jsonValue ;
129
+ this . webSocketSettingsForm . reset ( webSocketSettings ) ;
130
+ }
131
+
159
132
discardConnectivitySettings ( ) : void {
160
133
this . connectivitySettingsForm . reset ( this . connectivitySettings . jsonValue ) ;
161
134
}
@@ -179,33 +152,96 @@ export class GeneralSettingsComponent extends PageComponent implements OnDestroy
179
152
}
180
153
}
181
154
182
- private processConnectivitySettings ( settings : AdminSettings < ConnectivitySettings > ) : void {
183
- this . connectivitySettings = settings ;
184
- this . connectivitySettingsForm . reset ( this . connectivitySettings . jsonValue ) ;
155
+ confirmForm ( ) : UntypedFormGroup {
156
+ if ( this . generalSettingsForm . dirty ) {
157
+ return this . generalSettingsForm ;
158
+ } else if ( this . webSocketSettingsForm . dirty ) {
159
+ return this . webSocketSettingsForm ;
160
+ }
161
+ return this . connectivitySettingsForm ;
185
162
}
186
163
187
- private processGeneralSettings ( settings : AdminSettings < WebSocketSettings > ) : void {
164
+ private buildForms ( ) {
165
+ this . buildGeneralSettingsForm ( ) ;
166
+ this . buildWebSocketSettingsForm ( ) ;
167
+ this . buildConnectivitySettingsForm ( ) ;
168
+ }
169
+
170
+ private buildGeneralSettingsForm ( ) {
171
+ this . generalSettingsForm = this . fb . group ( {
172
+ baseUrl : [ null , [ Validators . required ] ] ,
173
+ prohibitDifferentUrl : [ null , [ ] ] ,
174
+ } ) ;
175
+ }
176
+
177
+ private buildWebSocketSettingsForm ( ) {
178
+ this . webSocketSettingsForm = this . fb . group ( {
179
+ isLoggingEnabled : [ null , [ ] ] ,
180
+ maxMessages : [ null , [ Validators . required ] ] ,
181
+ } ) ;
182
+ }
183
+
184
+ private buildConnectivitySettingsForm ( ) {
185
+ this . connectivitySettingsForm = this . fb . group ( {
186
+ ws : this . buildConnectivityInfoForm ( 'ws' ) ,
187
+ wss : this . buildConnectivityInfoForm ( 'wss' ) ,
188
+ mqtt : this . buildConnectivityInfoForm ( 'mqtt' ) ,
189
+ mqtts : this . buildConnectivityInfoForm ( 'mqtts' ) ,
190
+ } ) ;
191
+ }
192
+
193
+ private buildConnectivityInfoForm ( protocol : ConnectivityProtocol ) : UntypedFormGroup {
194
+ const formGroup = this . fb . group ( {
195
+ enabled : [ false , [ ] ] ,
196
+ host : [ { value : '' , disabled : true } , [ Validators . required ] ] ,
197
+ port : [ { value : null , disabled : true } , [ Validators . min ( 1 ) , Validators . max ( 65535 ) , Validators . pattern ( '[0-9]*' ) , Validators . required ] ]
198
+ } ) ;
199
+ formGroup . get ( 'enabled' ) . valueChanges . pipe (
200
+ takeUntil ( this . destroy$ )
201
+ ) . subscribe ( value => {
202
+ if ( value ) {
203
+ formGroup . get ( 'host' ) . enable ( { emitEvent : false } ) ;
204
+ formGroup . get ( 'port' ) . enable ( { emitEvent : false } ) ;
205
+ this . settingsService . getListenerPort ( protocol ) . subscribe ( value => this . listenerPortMap . set ( protocol , value ) ) ;
206
+ } else {
207
+ formGroup . get ( 'host' ) . disable ( { emitEvent : false } ) ;
208
+ formGroup . get ( 'port' ) . disable ( { emitEvent : false } ) ;
209
+ this . listenerPortMap . delete ( protocol ) ;
210
+ }
211
+ } ) ;
212
+ return formGroup ;
213
+ }
214
+
215
+ private processGeneralSettings ( settings : AdminSettings < GeneralSettings > ) : void {
188
216
this . generalSettings = settings ;
189
217
this . generalSettingsForm . reset ( this . generalSettings . jsonValue ) ;
190
218
}
191
219
220
+ private processWebSocketSettings ( settings : AdminSettings < WebSocketSettings > ) : void {
221
+ this . webSocketSettings = settings ;
222
+ this . webSocketSettingsForm . reset ( this . webSocketSettings . jsonValue ) ;
223
+ }
224
+
225
+ private processConnectivitySettings ( settings : AdminSettings < ConnectivitySettings > ) : void {
226
+ this . connectivitySettings = settings ;
227
+ this . connectivitySettingsForm . reset ( this . connectivitySettings . jsonValue ) ;
228
+ }
229
+
192
230
private getSettings ( ) {
193
231
this . getConnectivitySettings ( ) ;
194
232
this . getWebSocketGeneralSettings ( ) ;
233
+ this . getGeneralSettings ( ) ;
195
234
}
196
235
197
236
private getConnectivitySettings ( ) {
198
237
this . settingsService . getAdminSettings < ConnectivitySettings > ( connectivitySettingsKey ) . subscribe ( settings => this . processConnectivitySettings ( settings ) ) ;
199
238
}
200
239
201
240
private getWebSocketGeneralSettings ( ) {
202
- this . settingsService . getWebSocketSettings ( ) . subscribe ( settings => this . processGeneralSettings ( settings ) ) ;
241
+ this . settingsService . getWebSocketSettings ( ) . subscribe ( settings => this . processWebSocketSettings ( settings ) ) ;
203
242
}
204
243
205
- confirmForm ( ) : UntypedFormGroup {
206
- if ( this . generalSettingsForm . dirty ) {
207
- return this . generalSettingsForm ;
208
- }
209
- return this . connectivitySettingsForm ;
244
+ private getGeneralSettings ( ) {
245
+ this . settingsService . getAdminSettings < GeneralSettings > ( generalSettingsKey ) . subscribe ( settings => this . processGeneralSettings ( settings ) ) ;
210
246
}
211
247
}
0 commit comments