13 changed files with 302 additions and 167 deletions
@ -0,0 +1,48 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
Copyright © 2016-2021 The Thingsboard Authors |
||||
|
|
||||
|
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
you may not use this file except in compliance with the License. |
||||
|
You may obtain a copy of the License at |
||||
|
|
||||
|
http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
|
||||
|
Unless required by applicable law or agreed to in writing, software |
||||
|
distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
See the License for the specific language governing permissions and |
||||
|
limitations under the License. |
||||
|
|
||||
|
--> |
||||
|
<section [formGroup]="deviceCredentialsMqttFormGroup"> |
||||
|
<mat-form-field class="mat-block"> |
||||
|
<mat-label translate>device.client-id</mat-label> |
||||
|
<input matInput formControlName="clientId"> |
||||
|
<mat-error *ngIf="deviceCredentialsMqttFormGroup.get('clientId').hasError('pattern')"> |
||||
|
{{ 'device.client-id-pattern' | translate }} |
||||
|
</mat-error> |
||||
|
</mat-form-field> |
||||
|
<mat-form-field class="mat-block"> |
||||
|
<mat-label translate>device.user-name</mat-label> |
||||
|
<input matInput formControlName="userName" [required]="!!deviceCredentialsMqttFormGroup.get('password').value"> |
||||
|
<mat-error *ngIf="deviceCredentialsMqttFormGroup.get('userName').hasError('required')"> |
||||
|
{{ 'device.user-name-required' | translate }} |
||||
|
</mat-error> |
||||
|
</mat-form-field> |
||||
|
<mat-form-field class="mat-block"> |
||||
|
<mat-label translate>device.password</mat-label> |
||||
|
<input matInput formControlName="password" |
||||
|
autocomplete="new-password" |
||||
|
(ngModelChange)="passwordChanged()" |
||||
|
[type]="hidePassword ? 'password' : 'text'"> |
||||
|
<button mat-icon-button matSuffix type="button" |
||||
|
(click)="hidePassword = !hidePassword" |
||||
|
[attr.aria-pressed]="hidePassword"> |
||||
|
<mat-icon>{{hidePassword ? 'visibility_off' : 'visibility'}}</mat-icon> |
||||
|
</button> |
||||
|
</mat-form-field> |
||||
|
<tb-error style="margin-top: -12px; display: block;" |
||||
|
[error]="deviceCredentialsMqttFormGroup.hasError('atLeastOne') ? |
||||
|
('device.client-id-or-user-name-necessary' | translate) : ''"></tb-error> |
||||
|
</section> |
||||
@ -0,0 +1,133 @@ |
|||||
|
///
|
||||
|
/// Copyright © 2016-2021 The Thingsboard Authors
|
||||
|
///
|
||||
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
/// you may not use this file except in compliance with the License.
|
||||
|
/// You may obtain a copy of the License at
|
||||
|
///
|
||||
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
///
|
||||
|
/// Unless required by applicable law or agreed to in writing, software
|
||||
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
/// See the License for the specific language governing permissions and
|
||||
|
/// limitations under the License.
|
||||
|
///
|
||||
|
|
||||
|
import { Component, forwardRef, Input, OnDestroy } from '@angular/core'; |
||||
|
import { |
||||
|
ControlValueAccessor, |
||||
|
FormBuilder, |
||||
|
FormGroup, |
||||
|
NG_VALIDATORS, |
||||
|
NG_VALUE_ACCESSOR, |
||||
|
ValidationErrors, |
||||
|
Validator, |
||||
|
ValidatorFn, |
||||
|
Validators |
||||
|
} from '@angular/forms'; |
||||
|
import { Subject } from 'rxjs'; |
||||
|
import { DeviceCredentialMQTTBasic } from '@shared/models/device.models'; |
||||
|
import { takeUntil } from 'rxjs/operators'; |
||||
|
import { isDefinedAndNotNull, isEmptyStr } from '@core/utils'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-device-credentials-mqtt-basic', |
||||
|
templateUrl: './device-credentials-mqtt-basic.component.html', |
||||
|
providers: [ |
||||
|
{ |
||||
|
provide: NG_VALUE_ACCESSOR, |
||||
|
useExisting: forwardRef(() => DeviceCredentialsMqttBasicComponent), |
||||
|
multi: true |
||||
|
}, |
||||
|
{ |
||||
|
provide: NG_VALIDATORS, |
||||
|
useExisting: forwardRef(() => DeviceCredentialsMqttBasicComponent), |
||||
|
multi: true, |
||||
|
}], |
||||
|
styleUrls: [] |
||||
|
}) |
||||
|
export class DeviceCredentialsMqttBasicComponent implements ControlValueAccessor, Validator, OnDestroy { |
||||
|
|
||||
|
@Input() |
||||
|
disabled: boolean; |
||||
|
|
||||
|
deviceCredentialsMqttFormGroup: FormGroup; |
||||
|
|
||||
|
hidePassword = true; |
||||
|
|
||||
|
private destroy$ = new Subject(); |
||||
|
private propagateChange = (v: any) => {}; |
||||
|
|
||||
|
constructor(public fb: FormBuilder) { |
||||
|
this.deviceCredentialsMqttFormGroup = this.fb.group({ |
||||
|
clientId: [null, [Validators.pattern(/^[A-Za-z0-9]+$/)]], |
||||
|
userName: [null], |
||||
|
password: [null] |
||||
|
}, {validators: this.atLeastOne(Validators.required, ['clientId', 'userName'])}); |
||||
|
this.deviceCredentialsMqttFormGroup.valueChanges.pipe( |
||||
|
takeUntil(this.destroy$) |
||||
|
).subscribe((value) => { |
||||
|
this.updateView(value); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
ngOnDestroy(): void { |
||||
|
this.destroy$.next(); |
||||
|
this.destroy$.complete(); |
||||
|
} |
||||
|
|
||||
|
registerOnChange(fn: any): void { |
||||
|
this.propagateChange = fn; |
||||
|
} |
||||
|
|
||||
|
registerOnTouched(fn: any): void {} |
||||
|
|
||||
|
setDisabledState(isDisabled: boolean) { |
||||
|
this.disabled = isDisabled; |
||||
|
if (this.disabled) { |
||||
|
this.deviceCredentialsMqttFormGroup.disable({emitEvent: false}); |
||||
|
} else { |
||||
|
this.deviceCredentialsMqttFormGroup.enable({emitEvent: false}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
validate(): ValidationErrors | null { |
||||
|
return this.deviceCredentialsMqttFormGroup.valid ? null : { |
||||
|
deviceCredentialsMqttBasic: false |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
writeValue(mqttBasic: string) { |
||||
|
if (isDefinedAndNotNull(mqttBasic) && !isEmptyStr(mqttBasic)) { |
||||
|
const value = JSON.parse(mqttBasic); |
||||
|
this.deviceCredentialsMqttFormGroup.patchValue(value, {emitEvent: false}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
updateView(value: DeviceCredentialMQTTBasic) { |
||||
|
const formValue = JSON.stringify(value); |
||||
|
this.propagateChange(formValue); |
||||
|
} |
||||
|
|
||||
|
passwordChanged() { |
||||
|
const value = this.deviceCredentialsMqttFormGroup.get('password').value; |
||||
|
if (value !== '') { |
||||
|
this.deviceCredentialsMqttFormGroup.get('userName').setValidators([Validators.required]); |
||||
|
} else { |
||||
|
this.deviceCredentialsMqttFormGroup.get('userName').setValidators([]); |
||||
|
} |
||||
|
this.deviceCredentialsMqttFormGroup.get('userName').updateValueAndValidity({emitEvent: false}); |
||||
|
} |
||||
|
|
||||
|
private atLeastOne(validator: ValidatorFn, controls: string[] = null) { |
||||
|
return (group: FormGroup): ValidationErrors | null => { |
||||
|
if (!controls) { |
||||
|
controls = Object.keys(group.controls); |
||||
|
} |
||||
|
const hasAtLeastOne = group?.controls && controls.some(k => !validator(group.controls[k])); |
||||
|
|
||||
|
return hasAtLeastOne ? null : {atLeastOne: true}; |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,46 @@ |
|||||
|
///
|
||||
|
/// Copyright © 2016-2021 The Thingsboard Authors
|
||||
|
///
|
||||
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
/// you may not use this file except in compliance with the License.
|
||||
|
/// You may obtain a copy of the License at
|
||||
|
///
|
||||
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
///
|
||||
|
/// Unless required by applicable law or agreed to in writing, software
|
||||
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
/// See the License for the specific language governing permissions and
|
||||
|
/// limitations under the License.
|
||||
|
///
|
||||
|
|
||||
|
import { NgModule } from '@angular/core'; |
||||
|
import { CommonModule } from '@angular/common'; |
||||
|
import { SharedModule } from '@shared/shared.module'; |
||||
|
import { CopyDeviceCredentialsComponent } from './copy-device-credentials.component'; |
||||
|
import { DeviceCredentialsComponent } from './device-credentials.component'; |
||||
|
import { SecurityConfigLwm2mComponent } from './security-config-lwm2m.component'; |
||||
|
import { SecurityConfigLwm2mServerComponent } from './security-config-lwm2m-server.component'; |
||||
|
import { DeviceCredentialsMqttBasicComponent } from './device-credentials-mqtt-basic.component'; |
||||
|
|
||||
|
@NgModule({ |
||||
|
declarations: [ |
||||
|
CopyDeviceCredentialsComponent, |
||||
|
DeviceCredentialsComponent, |
||||
|
SecurityConfigLwm2mComponent, |
||||
|
SecurityConfigLwm2mServerComponent, |
||||
|
DeviceCredentialsMqttBasicComponent |
||||
|
], |
||||
|
imports: [ |
||||
|
CommonModule, |
||||
|
SharedModule |
||||
|
], |
||||
|
exports: [ |
||||
|
CopyDeviceCredentialsComponent, |
||||
|
DeviceCredentialsComponent, |
||||
|
SecurityConfigLwm2mComponent, |
||||
|
SecurityConfigLwm2mServerComponent, |
||||
|
DeviceCredentialsMqttBasicComponent |
||||
|
] |
||||
|
}) |
||||
|
export class DeviceCredentialsModule { } |
||||
Loading…
Reference in new issue