25 changed files with 1028 additions and 216 deletions
@ -0,0 +1,77 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2020 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]="deviceCredentialsFormGroup"> |
|||
<mat-form-field class="mat-block"> |
|||
<mat-label translate>device.credentials-type</mat-label> |
|||
<mat-select formControlName="credentialsType"> |
|||
<mat-option *ngFor="let credentialsType of credentialsTypes" [value]="credentialsType"> |
|||
{{ credentialTypeNamesMap.get(deviceCredentialsType[credentialsType]) }} |
|||
</mat-option> |
|||
</mat-select> |
|||
</mat-form-field> |
|||
<mat-form-field *ngIf="deviceCredentialsFormGroup.get('credentialsType').value === deviceCredentialsType.ACCESS_TOKEN" |
|||
class="mat-block"> |
|||
<mat-label translate>device.access-token</mat-label> |
|||
<input matInput formControlName="credentialsId" required> |
|||
<mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsId').hasError('required')"> |
|||
{{ 'device.access-token-required' | translate }} |
|||
</mat-error> |
|||
<mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsId').hasError('pattern')"> |
|||
{{ 'device.access-token-invalid' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
<mat-form-field *ngIf="deviceCredentialsFormGroup.get('credentialsType').value === deviceCredentialsType.X509_CERTIFICATE" |
|||
class="mat-block"> |
|||
<mat-label translate>device.rsa-key</mat-label> |
|||
<textarea matInput formControlName="credentialsValue" cols="15" rows="5" required></textarea> |
|||
<mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('required')"> |
|||
{{ 'device.rsa-key-required' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
<section *ngIf="deviceCredentialsFormGroup.get('credentialsType').value === deviceCredentialsType.MQTT_BASIC" formGroupName="credentialsBasic"> |
|||
<mat-form-field class="mat-block"> |
|||
<mat-label translate>device.client-id</mat-label> |
|||
<input matInput formControlName="clientId"> |
|||
<mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsBasic.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]="!!deviceCredentialsFormGroup.get('credentialsBasic.password').value"> |
|||
<mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsBasic.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> |
|||
<mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsBasic').hasError('atLeastOne')"> |
|||
{{ 'device.client-id-or-user-name-necessary' | translate }} |
|||
</mat-error> |
|||
</section> |
|||
</section> |
|||
@ -0,0 +1,229 @@ |
|||
///
|
|||
/// Copyright © 2016-2020 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, OnInit } from '@angular/core'; |
|||
import { |
|||
ControlValueAccessor, |
|||
FormBuilder, |
|||
FormControl, |
|||
FormGroup, |
|||
NG_VALIDATORS, |
|||
NG_VALUE_ACCESSOR, |
|||
ValidationErrors, |
|||
Validator, |
|||
ValidatorFn, |
|||
Validators |
|||
} from '@angular/forms'; |
|||
import { |
|||
credentialTypeNames, |
|||
DeviceCredentialMQTTBasic, |
|||
DeviceCredentials, |
|||
DeviceCredentialsType |
|||
} from '@shared/models/device.models'; |
|||
import { Subscription } from 'rxjs'; |
|||
import { isDefinedAndNotNull } from '@core/utils'; |
|||
import { distinctUntilChanged } from 'rxjs/operators'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-device-credentials', |
|||
templateUrl: './device-credentials.component.html', |
|||
providers: [ |
|||
{ |
|||
provide: NG_VALUE_ACCESSOR, |
|||
useExisting: forwardRef(() => DeviceCredentialsComponent), |
|||
multi: true |
|||
}, |
|||
{ |
|||
provide: NG_VALIDATORS, |
|||
useExisting: forwardRef(() => DeviceCredentialsComponent), |
|||
multi: true, |
|||
}], |
|||
styleUrls: [] |
|||
}) |
|||
export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit, Validator, OnDestroy { |
|||
|
|||
deviceCredentialsFormGroup: FormGroup; |
|||
|
|||
subscriptions: Subscription[] = []; |
|||
|
|||
@Input() |
|||
disabled: boolean; |
|||
|
|||
deviceCredentials: DeviceCredentials = null; |
|||
|
|||
submitted = false; |
|||
|
|||
deviceCredentialsType = DeviceCredentialsType; |
|||
|
|||
credentialsTypes = Object.keys(DeviceCredentialsType); |
|||
|
|||
credentialTypeNamesMap = credentialTypeNames; |
|||
|
|||
hidePassword = true; |
|||
|
|||
private propagateChange = (v: any) => {}; |
|||
|
|||
constructor(public fb: FormBuilder) { |
|||
this.deviceCredentialsFormGroup = this.fb.group({ |
|||
credentialsType: [DeviceCredentialsType.ACCESS_TOKEN], |
|||
credentialsId: [null], |
|||
credentialsValue: [null], |
|||
credentialsBasic: this.fb.group({ |
|||
clientId: [null, [Validators.pattern(/^[A-Za-z0-9]+$/)]], |
|||
userName: [null], |
|||
password: [null] |
|||
}, {validators: this.atLeastOne(Validators.required, ['clientId', 'userName'])}) |
|||
}); |
|||
this.deviceCredentialsFormGroup.get('credentialsBasic').disable(); |
|||
this.subscriptions.push( |
|||
this.deviceCredentialsFormGroup.valueChanges.pipe(distinctUntilChanged()).subscribe(() => { |
|||
this.updateView(); |
|||
}) |
|||
); |
|||
this.subscriptions.push( |
|||
this.deviceCredentialsFormGroup.get('credentialsType').valueChanges.subscribe(() => { |
|||
this.credentialsTypeChanged(); |
|||
}) |
|||
); |
|||
} |
|||
|
|||
ngOnInit(): void { |
|||
if (this.disabled) { |
|||
this.deviceCredentialsFormGroup.disable({emitEvent: false}); |
|||
} |
|||
} |
|||
|
|||
ngOnDestroy() { |
|||
this.subscriptions.forEach(s => s.unsubscribe()); |
|||
} |
|||
|
|||
writeValue(value: DeviceCredentials | null): void { |
|||
if (isDefinedAndNotNull(value)) { |
|||
this.deviceCredentials = value; |
|||
let credentialsBasic = {clientId: null, userName: null, password: null}; |
|||
let credentialsValue = null; |
|||
if (value.credentialsType === DeviceCredentialsType.MQTT_BASIC) { |
|||
credentialsBasic = JSON.parse(value.credentialsValue) as DeviceCredentialMQTTBasic; |
|||
} else { |
|||
credentialsValue = value.credentialsValue; |
|||
} |
|||
this.deviceCredentialsFormGroup.patchValue({ |
|||
credentialsType: value.credentialsType, |
|||
credentialsId: value.credentialsId, |
|||
credentialsValue, |
|||
credentialsBasic |
|||
}, {emitEvent: false}); |
|||
this.updateValidators(); |
|||
} |
|||
} |
|||
|
|||
updateView() { |
|||
const deviceCredentialsValue = this.deviceCredentialsFormGroup.value; |
|||
if (deviceCredentialsValue.credentialsType === DeviceCredentialsType.MQTT_BASIC) { |
|||
deviceCredentialsValue.credentialsValue = JSON.stringify(deviceCredentialsValue.credentialsBasic); |
|||
} |
|||
delete deviceCredentialsValue.credentialsBasic; |
|||
this.propagateChange(deviceCredentialsValue); |
|||
} |
|||
|
|||
registerOnChange(fn: any): void { |
|||
this.propagateChange = fn; |
|||
} |
|||
|
|||
registerOnTouched(fn: any): void {} |
|||
|
|||
setDisabledState(isDisabled: boolean): void { |
|||
this.disabled = isDisabled; |
|||
if (this.disabled) { |
|||
this.deviceCredentialsFormGroup.disable({emitEvent: false}); |
|||
} else { |
|||
this.deviceCredentialsFormGroup.enable({emitEvent: false}); |
|||
this.updateValidators(); |
|||
} |
|||
} |
|||
|
|||
public validate(c: FormControl) { |
|||
return this.deviceCredentialsFormGroup.valid ? null : { |
|||
deviceCredentials: { |
|||
valid: false, |
|||
}, |
|||
}; |
|||
} |
|||
|
|||
credentialsTypeChanged(): void { |
|||
this.deviceCredentialsFormGroup.patchValue({ |
|||
credentialsId: null, |
|||
credentialsValue: null, |
|||
credentialsBasic: {clientId: '', userName: '', password: ''} |
|||
}); |
|||
this.updateValidators(); |
|||
} |
|||
|
|||
updateValidators(): void { |
|||
this.hidePassword = true; |
|||
const crendetialsType = this.deviceCredentialsFormGroup.get('credentialsType').value as DeviceCredentialsType; |
|||
switch (crendetialsType) { |
|||
case DeviceCredentialsType.ACCESS_TOKEN: |
|||
this.deviceCredentialsFormGroup.get('credentialsId').setValidators([Validators.required, Validators.pattern(/^.{1,20}$/)]); |
|||
this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false}); |
|||
this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([]); |
|||
this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false}); |
|||
this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false}); |
|||
break; |
|||
case DeviceCredentialsType.X509_CERTIFICATE: |
|||
this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required]); |
|||
this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false}); |
|||
this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]); |
|||
this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false}); |
|||
this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false}); |
|||
break; |
|||
case DeviceCredentialsType.MQTT_BASIC: |
|||
this.deviceCredentialsFormGroup.get('credentialsBasic').enable({emitEvent: false}); |
|||
this.deviceCredentialsFormGroup.get('credentialsBasic').updateValueAndValidity({emitEvent: false}); |
|||
this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]); |
|||
this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false}); |
|||
this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([]); |
|||
this.deviceCredentialsFormGroup.get('credentialsValue').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}; |
|||
}; |
|||
} |
|||
|
|||
passwordChanged() { |
|||
const value = this.deviceCredentialsFormGroup.get('credentialsBasic.password').value; |
|||
if (value !== '') { |
|||
this.deviceCredentialsFormGroup.get('credentialsBasic.userName').setValidators([Validators.required]); |
|||
if (this.deviceCredentialsFormGroup.get('credentialsBasic.userName').untouched) { |
|||
this.deviceCredentialsFormGroup.get('credentialsBasic.userName').markAsTouched({onlySelf: true}); |
|||
} |
|||
} else { |
|||
this.deviceCredentialsFormGroup.get('credentialsBasic.userName').setValidators([]); |
|||
} |
|||
this.deviceCredentialsFormGroup.get('credentialsBasic.userName').updateValueAndValidity({ |
|||
emitEvent: false, |
|||
onlySelf: true |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,169 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2020 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. |
|||
|
|||
--> |
|||
<div> |
|||
<mat-toolbar color="primary"> |
|||
<h2 translate>device.add-device-text</h2> |
|||
<span fxFlex></span> |
|||
<button mat-icon-button |
|||
(click)="cancel()" |
|||
type="button"> |
|||
<mat-icon class="material-icons">close</mat-icon> |
|||
</button> |
|||
</mat-toolbar> |
|||
<mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async"> |
|||
</mat-progress-bar> |
|||
<div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div> |
|||
<div mat-dialog-content> |
|||
<mat-horizontal-stepper [linear]="true" [labelPosition]="labelPosition" #addDeviceWizardStepper (selectionChange)="changeStep($event)"> |
|||
<ng-template matStepperIcon="edit"> |
|||
<mat-icon>check</mat-icon> |
|||
</ng-template> |
|||
<mat-step [stepControl]="deviceWizardFormGroup"> |
|||
<form [formGroup]="deviceWizardFormGroup" style="padding-bottom: 16px;"> |
|||
<ng-template matStepLabel>{{ 'device.wizard.device-details' | translate}}</ng-template> |
|||
<fieldset [disabled]="isLoading$ | async"> |
|||
<mat-form-field class="mat-block"> |
|||
<mat-label translate>device.name</mat-label> |
|||
<input matInput formControlName="name" required> |
|||
<mat-error *ngIf="deviceWizardFormGroup.get('name').hasError('required')"> |
|||
{{ 'device.name-required' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
<mat-form-field class="mat-block"> |
|||
<mat-label translate>device.label</mat-label> |
|||
<input matInput formControlName="label"> |
|||
</mat-form-field> |
|||
<mat-form-field class="mat-block" style="padding-bottom: 14px;"> |
|||
<mat-label translate>device-profile.transport-type</mat-label> |
|||
<mat-select formControlName="transportType" required> |
|||
<mat-option *ngFor="let type of deviceTransportTypes" [value]="type"> |
|||
{{deviceTransportTypeTranslations.get(type) | translate}} |
|||
</mat-option> |
|||
</mat-select> |
|||
<mat-hint *ngIf="deviceWizardFormGroup.get('transportType').value"> |
|||
{{deviceTransportTypeHints.get(deviceWizardFormGroup.get('transportType').value) | translate}} |
|||
</mat-hint> |
|||
<mat-error *ngIf="deviceWizardFormGroup.get('transportType').hasError('required')"> |
|||
{{ 'device-profile.transport-type-required' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
<div fxLayout="row" fxLayoutGap="16px"> |
|||
<mat-radio-group fxLayout="column" formControlName="addProfileType" fxLayoutAlign="space-around"> |
|||
<mat-radio-button [value]="0" color="primary"> |
|||
<span translate>device.wizard.existing-device-profile</span> |
|||
</mat-radio-button> |
|||
<mat-radio-button [value]="1" color="primary"> |
|||
<span translate>device.wizard.new-device-profile</span> |
|||
</mat-radio-button> |
|||
</mat-radio-group> |
|||
<div fxLayout="column"> |
|||
<tb-device-profile-autocomplete |
|||
[required]="!createProfile" |
|||
[transportType]="deviceWizardFormGroup.get('transportType').value" |
|||
formControlName="deviceProfileId" |
|||
(deviceProfileChanged)="$event?.transportType ? deviceWizardFormGroup.get('transportType').patchValue($event?.transportType) : {}" |
|||
[addNewProfile]="false" |
|||
[selectDefaultProfile]="true" |
|||
[editProfileEnabled]="false"> |
|||
</tb-device-profile-autocomplete> |
|||
<mat-form-field fxFlex class="mat-block"> |
|||
<mat-label translate>device-profile.new-device-profile-name</mat-label> |
|||
<input matInput formControlName="newDeviceProfileTitle" |
|||
[required]="createProfile"> |
|||
<mat-error *ngIf="deviceWizardFormGroup.get('newDeviceProfileTitle').hasError('required')"> |
|||
{{ 'device-profile.new-device-profile-name-required' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
</div> |
|||
</div> |
|||
<mat-checkbox formControlName="gateway" style="padding-bottom: 16px;"> |
|||
{{ 'device.is-gateway' | translate }} |
|||
</mat-checkbox> |
|||
<mat-form-field class="mat-block"> |
|||
<mat-label translate>device.description</mat-label> |
|||
<textarea matInput formControlName="description" rows="2"></textarea> |
|||
</mat-form-field> |
|||
</fieldset> |
|||
</form> |
|||
</mat-step> |
|||
<mat-step [stepControl]="transportConfigFormGroup" *ngIf="createTransportConfiguration"> |
|||
<form [formGroup]="transportConfigFormGroup" style="padding-bottom: 16px;"> |
|||
<ng-template matStepLabel>{{ 'device-profile.transport-configuration' | translate }}</ng-template> |
|||
<tb-device-profile-transport-configuration |
|||
formControlName="transportConfiguration" |
|||
required> |
|||
</tb-device-profile-transport-configuration> |
|||
</form> |
|||
</mat-step> |
|||
<mat-step [stepControl]="alarmRulesFormGroup" [optional]="true" *ngIf="createProfile"> |
|||
<form [formGroup]="alarmRulesFormGroup" style="padding-bottom: 16px;"> |
|||
<ng-template matStepLabel>{{'device-profile.alarm-rules-with-count' | translate: |
|||
{count: alarmRulesFormGroup.get('alarms').value ? |
|||
alarmRulesFormGroup.get('alarms').value.length : 0} }}</ng-template> |
|||
<tb-device-profile-alarms |
|||
formControlName="alarms"> |
|||
</tb-device-profile-alarms> |
|||
</form> |
|||
</mat-step> |
|||
<mat-step [stepControl]="credentialsFormGroup" [optional]="true"> |
|||
<ng-template matStepLabel>{{ 'device.credentials' | translate }}</ng-template> |
|||
<form [formGroup]="credentialsFormGroup" style="padding-bottom: 16px;"> |
|||
<mat-checkbox style="padding-bottom: 16px;" formControlName="setCredential">{{ 'device.wizard.add-credential' | translate }}</mat-checkbox> |
|||
<tb-device-credentials |
|||
[fxShow]="credentialsFormGroup.get('setCredential').value" |
|||
formControlName="credential"> |
|||
</tb-device-credentials> |
|||
</form> |
|||
</mat-step> |
|||
<mat-step [stepControl]="customerFormGroup" [optional]="true"> |
|||
<ng-template matStepLabel>{{ 'customer.customer' | translate }}</ng-template> |
|||
<form [formGroup]="customerFormGroup" style="padding-bottom: 16px;"> |
|||
<tb-entity-autocomplete |
|||
formControlName="customerId" |
|||
labelText="device.wizard.customer-to-assign-device" |
|||
[entityType]="entityType.CUSTOMER"> |
|||
</tb-entity-autocomplete> |
|||
</form> |
|||
</mat-step> |
|||
</mat-horizontal-stepper> |
|||
</div> |
|||
<div mat-dialog-actions fxLayout="column" fxLayoutAlign="start wrap" fxLayoutGap="8px" style="height: 100px;"> |
|||
<div fxFlex fxLayout="row" fxLayoutAlign="end"> |
|||
<button mat-raised-button |
|||
*ngIf="showNext" |
|||
[disabled]="(isLoading$ | async)" |
|||
(click)="nextStep()">{{ 'action.next-with-label' | translate:{label: (getFormLabel(this.selectedIndex+1) | translate)} }}</button> |
|||
</div> |
|||
<div fxFlex fxLayout="row"> |
|||
<button mat-button |
|||
color="primary" |
|||
[disabled]="(isLoading$ | async)" |
|||
(click)="cancel()">{{ 'action.cancel' | translate }}</button> |
|||
<span fxFlex></span> |
|||
<div fxLayout="row wrap" fxLayoutGap="8px"> |
|||
<button mat-raised-button *ngIf="selectedIndex > 0" |
|||
[disabled]="(isLoading$ | async)" |
|||
(click)="previousStep()">{{ 'action.back' | translate }}</button> |
|||
<button mat-raised-button |
|||
[disabled]="(isLoading$ | async)" |
|||
color="primary" |
|||
(click)="add()">{{ 'action.add' | translate }}</button> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
@ -0,0 +1,64 @@ |
|||
/** |
|||
* Copyright © 2016-2020 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 "../../../../../scss/constants"; |
|||
|
|||
:host-context(.tb-fullscreen-dialog .mat-dialog-container) { |
|||
@media #{$mat-lt-sm} { |
|||
.mat-dialog-content { |
|||
max-height: 75vh; |
|||
} |
|||
} |
|||
} |
|||
|
|||
:host ::ng-deep { |
|||
.mat-dialog-content { |
|||
display: flex; |
|||
flex-direction: column; |
|||
height: 100%; |
|||
|
|||
.mat-stepper-horizontal { |
|||
display: flex; |
|||
flex-direction: column; |
|||
height: 100%; |
|||
overflow: hidden; |
|||
@media #{$mat-lt-sm} { |
|||
.mat-step-label { |
|||
white-space: normal; |
|||
overflow: visible; |
|||
.mat-step-text-label { |
|||
overflow: visible; |
|||
} |
|||
} |
|||
} |
|||
.mat-horizontal-content-container { |
|||
height: 450px; |
|||
max-height: 100%; |
|||
width: 100%;; |
|||
overflow-y: auto; |
|||
@media #{$mat-gt-sm} { |
|||
min-width: 800px; |
|||
} |
|||
} |
|||
.mat-horizontal-stepper-content[aria-expanded=true] { |
|||
height: 100%; |
|||
form { |
|||
height: 100%; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,331 @@ |
|||
///
|
|||
/// Copyright © 2016-2020 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, Inject, OnDestroy, SkipSelf, ViewChild } from '@angular/core'; |
|||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { FormBuilder, FormControl, FormGroup, FormGroupDirective, NgForm, Validators } from '@angular/forms'; |
|||
import { DialogComponent } from '@shared/components/dialog.component'; |
|||
import { Router } from '@angular/router'; |
|||
import { |
|||
createDeviceProfileConfiguration, |
|||
createDeviceProfileTransportConfiguration, |
|||
DeviceProfile, |
|||
DeviceProfileType, |
|||
DeviceTransportType, deviceTransportTypeHintMap, |
|||
deviceTransportTypeTranslationMap |
|||
} from '@shared/models/device.models'; |
|||
import { MatHorizontalStepper } from '@angular/material/stepper'; |
|||
import { AddEntityDialogData } from '@home/models/entity/entity-component.models'; |
|||
import { BaseData, HasId } from '@shared/models/base-data'; |
|||
import { EntityType } from '@shared/models/entity-type.models'; |
|||
import { DeviceProfileService } from '@core/http/device-profile.service'; |
|||
import { EntityId } from '@shared/models/id/entity-id'; |
|||
import { Observable, of, Subscription } from 'rxjs'; |
|||
import { map, mergeMap, tap } from 'rxjs/operators'; |
|||
import { DeviceService } from '@core/http/device.service'; |
|||
import { ErrorStateMatcher } from '@angular/material/core'; |
|||
import { StepperSelectionEvent } from '@angular/cdk/stepper'; |
|||
import { BreakpointObserver, BreakpointState } from '@angular/cdk/layout'; |
|||
import { MediaBreakpoints } from '@shared/models/constants'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-device-wizard', |
|||
templateUrl: './device-wizard-dialog.component.html', |
|||
providers: [], |
|||
styleUrls: ['./device-wizard-dialog.component.scss'] |
|||
}) |
|||
export class DeviceWizardDialogComponent extends |
|||
DialogComponent<DeviceWizardDialogComponent, boolean> implements OnDestroy, ErrorStateMatcher { |
|||
|
|||
@ViewChild('addDeviceWizardStepper', {static: true}) addDeviceWizardStepper: MatHorizontalStepper; |
|||
|
|||
selectedIndex = 0; |
|||
|
|||
showNext = true; |
|||
|
|||
createProfile = false; |
|||
createTransportConfiguration = false; |
|||
|
|||
entityType = EntityType; |
|||
|
|||
deviceTransportTypes = Object.keys(DeviceTransportType); |
|||
|
|||
deviceTransportTypeTranslations = deviceTransportTypeTranslationMap; |
|||
|
|||
deviceTransportTypeHints = deviceTransportTypeHintMap; |
|||
|
|||
deviceWizardFormGroup: FormGroup; |
|||
|
|||
transportConfigFormGroup: FormGroup; |
|||
|
|||
alarmRulesFormGroup: FormGroup; |
|||
|
|||
credentialsFormGroup: FormGroup; |
|||
|
|||
customerFormGroup: FormGroup; |
|||
|
|||
labelPosition = 'end'; |
|||
|
|||
private subscriptions: Subscription[] = []; |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
protected router: Router, |
|||
@Inject(MAT_DIALOG_DATA) public data: AddEntityDialogData<BaseData<EntityId>>, |
|||
@SkipSelf() private errorStateMatcher: ErrorStateMatcher, |
|||
public dialogRef: MatDialogRef<DeviceWizardDialogComponent, boolean>, |
|||
private deviceProfileService: DeviceProfileService, |
|||
private deviceService: DeviceService, |
|||
private breakpointObserver: BreakpointObserver, |
|||
private fb: FormBuilder) { |
|||
super(store, router, dialogRef); |
|||
this.deviceWizardFormGroup = this.fb.group({ |
|||
name: ['', Validators.required], |
|||
label: [''], |
|||
gateway: [false], |
|||
transportType: [DeviceTransportType.DEFAULT, Validators.required], |
|||
addProfileType: [0], |
|||
deviceProfileId: [null, Validators.required], |
|||
newDeviceProfileTitle: [{value: null, disabled: true}], |
|||
description: [''] |
|||
} |
|||
); |
|||
|
|||
this.subscriptions.push(this.deviceWizardFormGroup.get('addProfileType').valueChanges.subscribe( |
|||
(addProfileType: number) => { |
|||
if (addProfileType === 0) { |
|||
this.deviceWizardFormGroup.get('deviceProfileId').setValidators([Validators.required]); |
|||
this.deviceWizardFormGroup.get('deviceProfileId').enable(); |
|||
this.deviceWizardFormGroup.get('newDeviceProfileTitle').setValidators(null); |
|||
this.deviceWizardFormGroup.get('newDeviceProfileTitle').disable(); |
|||
this.deviceWizardFormGroup.updateValueAndValidity(); |
|||
this.createProfile = false; |
|||
this.createTransportConfiguration = false; |
|||
} else { |
|||
this.deviceWizardFormGroup.get('deviceProfileId').setValidators(null); |
|||
this.deviceWizardFormGroup.get('deviceProfileId').disable(); |
|||
this.deviceWizardFormGroup.get('newDeviceProfileTitle').setValidators([Validators.required]); |
|||
this.deviceWizardFormGroup.get('newDeviceProfileTitle').enable(); |
|||
this.deviceWizardFormGroup.updateValueAndValidity(); |
|||
this.createProfile = true; |
|||
this.createTransportConfiguration = this.deviceWizardFormGroup.get('transportType').value && |
|||
DeviceTransportType.DEFAULT !== this.deviceWizardFormGroup.get('transportType').value; |
|||
} |
|||
} |
|||
)); |
|||
|
|||
this.transportConfigFormGroup = this.fb.group( |
|||
{ |
|||
transportConfiguration: [createDeviceProfileTransportConfiguration(DeviceTransportType.DEFAULT), Validators.required] |
|||
} |
|||
); |
|||
this.subscriptions.push(this.deviceWizardFormGroup.get('transportType').valueChanges.subscribe((transportType) => { |
|||
this.deviceProfileTransportTypeChanged(transportType); |
|||
})); |
|||
|
|||
this.alarmRulesFormGroup = this.fb.group({ |
|||
alarms: [null] |
|||
} |
|||
); |
|||
|
|||
this.credentialsFormGroup = this.fb.group({ |
|||
setCredential: [false], |
|||
credential: [{value: null, disabled: true}] |
|||
} |
|||
); |
|||
|
|||
this.subscriptions.push(this.credentialsFormGroup.get('setCredential').valueChanges.subscribe((value) => { |
|||
if (value) { |
|||
this.credentialsFormGroup.get('credential').enable(); |
|||
} else { |
|||
this.credentialsFormGroup.get('credential').disable(); |
|||
} |
|||
})); |
|||
|
|||
this.customerFormGroup = this.fb.group({ |
|||
customerId: [null] |
|||
} |
|||
); |
|||
|
|||
this.labelPosition = this.breakpointObserver.isMatched(MediaBreakpoints['gt-sm']) ? 'end' : 'bottom'; |
|||
|
|||
this.subscriptions.push(this.breakpointObserver |
|||
.observe(MediaBreakpoints['gt-sm']) |
|||
.subscribe((state: BreakpointState) => { |
|||
if (state.matches) { |
|||
this.labelPosition = 'end'; |
|||
} else { |
|||
this.labelPosition = 'bottom'; |
|||
} |
|||
} |
|||
)); |
|||
} |
|||
|
|||
ngOnDestroy() { |
|||
super.ngOnDestroy(); |
|||
this.subscriptions.forEach(s => s.unsubscribe()); |
|||
} |
|||
|
|||
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean { |
|||
const originalErrorState = this.errorStateMatcher.isErrorState(control, form); |
|||
const customErrorState = !!(control && control.invalid); |
|||
return originalErrorState || customErrorState; |
|||
} |
|||
|
|||
cancel(): void { |
|||
this.dialogRef.close(null); |
|||
} |
|||
|
|||
previousStep(): void { |
|||
this.addDeviceWizardStepper.previous(); |
|||
} |
|||
|
|||
nextStep(): void { |
|||
this.addDeviceWizardStepper.next(); |
|||
} |
|||
|
|||
getFormLabel(index: number): string { |
|||
if (index > 0) { |
|||
if (!this.createProfile) { |
|||
index += 2; |
|||
} else if (!this.createTransportConfiguration) { |
|||
index += 1; |
|||
} |
|||
} |
|||
switch (index) { |
|||
case 0: |
|||
return 'device.wizard.device-details'; |
|||
case 1: |
|||
return 'device-profile.transport-configuration'; |
|||
case 2: |
|||
return 'device-profile.alarm-rules'; |
|||
case 3: |
|||
return 'device.credentials'; |
|||
case 4: |
|||
return 'customer.customer'; |
|||
} |
|||
} |
|||
|
|||
get maxStepperIndex(): number { |
|||
return this.addDeviceWizardStepper?._steps?.length - 1; |
|||
} |
|||
|
|||
private deviceProfileTransportTypeChanged(deviceTransportType: DeviceTransportType): void { |
|||
this.transportConfigFormGroup.patchValue( |
|||
{transportConfiguration: createDeviceProfileTransportConfiguration(deviceTransportType)}); |
|||
this.createTransportConfiguration = this.createProfile && deviceTransportType && |
|||
DeviceTransportType.DEFAULT !== deviceTransportType; |
|||
} |
|||
|
|||
add(): void { |
|||
if (this.allValid()) { |
|||
this.createDeviceProfile().pipe( |
|||
mergeMap(profileId => this.createDevice(profileId)), |
|||
mergeMap(device => this.saveCredentials(device)) |
|||
).subscribe( |
|||
(created) => { |
|||
this.dialogRef.close(created); |
|||
} |
|||
); |
|||
} |
|||
} |
|||
|
|||
private createDeviceProfile(): Observable<EntityId> { |
|||
if (this.deviceWizardFormGroup.get('addProfileType').value) { |
|||
const deviceProfile: DeviceProfile = { |
|||
name: this.deviceWizardFormGroup.get('newDeviceProfileTitle').value, |
|||
type: DeviceProfileType.DEFAULT, |
|||
transportType: this.deviceWizardFormGroup.get('transportType').value, |
|||
profileData: { |
|||
configuration: createDeviceProfileConfiguration(DeviceProfileType.DEFAULT), |
|||
transportConfiguration: this.transportConfigFormGroup.get('transportConfiguration').value, |
|||
alarms: this.alarmRulesFormGroup.get('alarms').value |
|||
} |
|||
}; |
|||
return this.deviceProfileService.saveDeviceProfile(deviceProfile).pipe( |
|||
map(profile => profile.id), |
|||
tap((profileId) => { |
|||
this.deviceWizardFormGroup.patchValue({ |
|||
deviceProfileId: profileId, |
|||
addProfileType: 0 |
|||
}); |
|||
}) |
|||
); |
|||
} else { |
|||
return of(null); |
|||
} |
|||
} |
|||
|
|||
private createDevice(profileId: EntityId = this.deviceWizardFormGroup.get('deviceProfileId').value): Observable<BaseData<HasId>> { |
|||
const device = { |
|||
name: this.deviceWizardFormGroup.get('name').value, |
|||
label: this.deviceWizardFormGroup.get('label').value, |
|||
deviceProfileId: profileId, |
|||
additionalInfo: { |
|||
gateway: this.deviceWizardFormGroup.get('gateway').value, |
|||
description: this.deviceWizardFormGroup.get('description').value |
|||
}, |
|||
customerId: null |
|||
}; |
|||
if (this.customerFormGroup.get('customerId').value) { |
|||
device.customerId = { |
|||
entityType: EntityType.CUSTOMER, |
|||
id: this.customerFormGroup.get('customerId').value |
|||
}; |
|||
} |
|||
return this.data.entitiesTableConfig.saveEntity(device); |
|||
} |
|||
|
|||
private saveCredentials(device: BaseData<HasId>): Observable<boolean> { |
|||
if (this.credentialsFormGroup.get('setCredential').value) { |
|||
return this.deviceService.getDeviceCredentials(device.id.id).pipe( |
|||
mergeMap( |
|||
(deviceCredentials) => { |
|||
const deviceCredentialsValue = {...deviceCredentials, ...this.credentialsFormGroup.value.credential}; |
|||
return this.deviceService.saveDeviceCredentials(deviceCredentialsValue); |
|||
} |
|||
), |
|||
map(() => true)); |
|||
} |
|||
return of(true); |
|||
} |
|||
|
|||
allValid(): boolean { |
|||
if (this.addDeviceWizardStepper.steps.find((item, index) => { |
|||
if (item.stepControl.invalid) { |
|||
item.interacted = true; |
|||
this.addDeviceWizardStepper.selectedIndex = index; |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} )) { |
|||
return false; |
|||
} else { |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
changeStep($event: StepperSelectionEvent): void { |
|||
this.selectedIndex = $event.selectedIndex; |
|||
if (this.selectedIndex === this.maxStepperIndex) { |
|||
this.showNext = false; |
|||
} else { |
|||
this.showNext = true; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue