...
|
...
|
@@ -19,9 +19,23 @@ import { ErrorStateMatcher } from '@angular/material/core'; |
19
|
19
|
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
20
|
20
|
import { Store } from '@ngrx/store';
|
21
|
21
|
import { AppState } from '@core/core.state';
|
22
|
|
-import { FormBuilder, FormControl, FormGroup, FormGroupDirective, NgForm, Validators } from '@angular/forms';
|
|
22
|
+import {
|
|
23
|
+ FormBuilder,
|
|
24
|
+ FormControl,
|
|
25
|
+ FormGroup,
|
|
26
|
+ FormGroupDirective,
|
|
27
|
+ NgForm,
|
|
28
|
+ ValidationErrors,
|
|
29
|
+ ValidatorFn,
|
|
30
|
+ Validators
|
|
31
|
+} from '@angular/forms';
|
23
|
32
|
import { DeviceService } from '@core/http/device.service';
|
24
|
|
-import { credentialTypeNames, DeviceCredentials, DeviceCredentialsType } from '@shared/models/device.models';
|
|
33
|
+import {
|
|
34
|
+ credentialTypeNames,
|
|
35
|
+ DeviceCredentialMQTTBasic,
|
|
36
|
+ DeviceCredentials,
|
|
37
|
+ DeviceCredentialsType
|
|
38
|
+} from '@shared/models/device.models';
|
25
|
39
|
import { DialogComponent } from '@shared/components/dialog.component';
|
26
|
40
|
import { Router } from '@angular/router';
|
27
|
41
|
|
...
|
...
|
@@ -53,6 +67,8 @@ export class DeviceCredentialsDialogComponent extends |
53
|
67
|
|
54
|
68
|
credentialTypeNamesMap = credentialTypeNames;
|
55
|
69
|
|
|
70
|
+ hidePassword = true;
|
|
71
|
+
|
56
|
72
|
constructor(protected store: Store<AppState>,
|
57
|
73
|
protected router: Router,
|
58
|
74
|
@Inject(MAT_DIALOG_DATA) public data: DeviceCredentialsDialogData,
|
...
|
...
|
@@ -69,7 +85,12 @@ export class DeviceCredentialsDialogComponent extends |
69
|
85
|
this.deviceCredentialsFormGroup = this.fb.group({
|
70
|
86
|
credentialsType: [DeviceCredentialsType.ACCESS_TOKEN],
|
71
|
87
|
credentialsId: [''],
|
72
|
|
- credentialsValue: ['']
|
|
88
|
+ credentialsValue: [''],
|
|
89
|
+ credentialsBasic: this.fb.group({
|
|
90
|
+ clientId: ['', [Validators.pattern(/^[A-Za-z0-9]+$/)]],
|
|
91
|
+ userName: [''],
|
|
92
|
+ password: ['']
|
|
93
|
+ }, {validators: this.atLeastOne(Validators.required, ['clientId', 'userName'])})
|
73
|
94
|
});
|
74
|
95
|
if (this.isReadOnly) {
|
75
|
96
|
this.deviceCredentialsFormGroup.disable({emitEvent: false});
|
...
|
...
|
@@ -89,10 +110,17 @@ export class DeviceCredentialsDialogComponent extends |
89
|
110
|
this.deviceService.getDeviceCredentials(this.data.deviceId).subscribe(
|
90
|
111
|
(deviceCredentials) => {
|
91
|
112
|
this.deviceCredentials = deviceCredentials;
|
|
113
|
+ let credentialsValue = deviceCredentials.credentialsValue;
|
|
114
|
+ let credentialsBasic = {clientId: null, userName: null, password: null};
|
|
115
|
+ if (deviceCredentials.credentialsType === DeviceCredentialsType.MQTT_BASIC) {
|
|
116
|
+ credentialsValue = null;
|
|
117
|
+ credentialsBasic = deviceCredentials.credentialsValue as DeviceCredentialMQTTBasic;
|
|
118
|
+ }
|
92
|
119
|
this.deviceCredentialsFormGroup.patchValue({
|
93
|
120
|
credentialsType: deviceCredentials.credentialsType,
|
94
|
121
|
credentialsId: deviceCredentials.credentialsId,
|
95
|
|
- credentialsValue: deviceCredentials.credentialsValue
|
|
122
|
+ credentialsValue,
|
|
123
|
+ credentialsBasic
|
96
|
124
|
});
|
97
|
125
|
this.updateValidators();
|
98
|
126
|
}
|
...
|
...
|
@@ -100,12 +128,16 @@ export class DeviceCredentialsDialogComponent extends |
100
|
128
|
}
|
101
|
129
|
|
102
|
130
|
credentialsTypeChanged(): void {
|
103
|
|
- this.deviceCredentialsFormGroup.patchValue(
|
104
|
|
- {credentialsId: null, credentialsValue: null}, {emitEvent: true});
|
|
131
|
+ this.deviceCredentialsFormGroup.patchValue({
|
|
132
|
+ credentialsId: null,
|
|
133
|
+ credentialsValue: null,
|
|
134
|
+ credentialsBasic: {clientId: '', userName: '', password: ''}
|
|
135
|
+ }, {emitEvent: true});
|
105
|
136
|
this.updateValidators();
|
106
|
137
|
}
|
107
|
138
|
|
108
|
139
|
updateValidators(): void {
|
|
140
|
+ this.hidePassword = true;
|
109
|
141
|
const crendetialsType = this.deviceCredentialsFormGroup.get('credentialsType').value as DeviceCredentialsType;
|
110
|
142
|
switch (crendetialsType) {
|
111
|
143
|
case DeviceCredentialsType.ACCESS_TOKEN:
|
...
|
...
|
@@ -113,27 +145,66 @@ export class DeviceCredentialsDialogComponent extends |
113
|
145
|
this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity();
|
114
|
146
|
this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([]);
|
115
|
147
|
this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity();
|
|
148
|
+ this.deviceCredentialsFormGroup.get('credentialsBasic').disable();
|
116
|
149
|
break;
|
117
|
150
|
case DeviceCredentialsType.X509_CERTIFICATE:
|
118
|
151
|
this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required]);
|
119
|
152
|
this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity();
|
120
|
153
|
this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]);
|
121
|
154
|
this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity();
|
|
155
|
+ this.deviceCredentialsFormGroup.get('credentialsBasic').disable();
|
122
|
156
|
break;
|
|
157
|
+ case DeviceCredentialsType.MQTT_BASIC:
|
|
158
|
+ this.deviceCredentialsFormGroup.get('credentialsBasic').enable();
|
|
159
|
+ this.deviceCredentialsFormGroup.get('credentialsBasic').updateValueAndValidity();
|
|
160
|
+ this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]);
|
|
161
|
+ this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity();
|
|
162
|
+ this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([]);
|
|
163
|
+ this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity();
|
123
|
164
|
}
|
124
|
165
|
}
|
125
|
166
|
|
|
167
|
+ private atLeastOne(validator: ValidatorFn, controls: string[] = null) {
|
|
168
|
+ return (group: FormGroup): ValidationErrors | null => {
|
|
169
|
+ if (!controls) {
|
|
170
|
+ controls = Object.keys(group.controls);
|
|
171
|
+ }
|
|
172
|
+ const hasAtLeastOne = group?.controls && controls.some(k => !validator(group.controls[k]));
|
|
173
|
+
|
|
174
|
+ return hasAtLeastOne ? null : {atLeastOne: true};
|
|
175
|
+ };
|
|
176
|
+ }
|
|
177
|
+
|
126
|
178
|
cancel(): void {
|
127
|
179
|
this.dialogRef.close(null);
|
128
|
180
|
}
|
129
|
181
|
|
130
|
182
|
save(): void {
|
131
|
183
|
this.submitted = true;
|
132
|
|
- this.deviceCredentials = {...this.deviceCredentials, ...this.deviceCredentialsFormGroup.value};
|
|
184
|
+ const deviceCredentialsValue = this.deviceCredentialsFormGroup.value;
|
|
185
|
+ if (deviceCredentialsValue.credentialsType === DeviceCredentialsType.MQTT_BASIC) {
|
|
186
|
+ deviceCredentialsValue.credentialsValue = deviceCredentialsValue.credentialsBasic;
|
|
187
|
+ }
|
|
188
|
+ delete deviceCredentialsValue.credentialsBasic;
|
|
189
|
+ this.deviceCredentials = {...this.deviceCredentials, ...deviceCredentialsValue};
|
133
|
190
|
this.deviceService.saveDeviceCredentials(this.deviceCredentials).subscribe(
|
134
|
191
|
(deviceCredentials) => {
|
135
|
192
|
this.dialogRef.close(deviceCredentials);
|
136
|
193
|
}
|
137
|
194
|
);
|
138
|
195
|
}
|
|
196
|
+
|
|
197
|
+ passwordChanged() {
|
|
198
|
+ const value = this.deviceCredentialsFormGroup.get('credentialsBasic.password').value;
|
|
199
|
+ if (value !== '') {
|
|
200
|
+ this.deviceCredentialsFormGroup.get('credentialsBasic.userName').setValidators([Validators.required]);
|
|
201
|
+ if (this.deviceCredentialsFormGroup.get('credentialsBasic.userName').untouched) {
|
|
202
|
+ this.deviceCredentialsFormGroup.get('credentialsBasic.userName').markAsTouched({onlySelf: true});
|
|
203
|
+ }
|
|
204
|
+ this.deviceCredentialsFormGroup.get('credentialsBasic.userName').updateValueAndValidity();
|
|
205
|
+ } else {
|
|
206
|
+ this.deviceCredentialsFormGroup.get('credentialsBasic.userName').setValidators([]);
|
|
207
|
+ this.deviceCredentialsFormGroup.get('credentialsBasic.userName').updateValueAndValidity();
|
|
208
|
+ }
|
|
209
|
+ }
|
139
|
210
|
} |
...
|
...
|
|