11 changed files with 562 additions and 29 deletions
@ -0,0 +1,155 @@ |
|||
<!-- |
|||
|
|||
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 style="min-width: 1000px;"> |
|||
<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" #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"> |
|||
<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-error *ngIf="deviceWizardFormGroup.get('transportType').hasError('required')"> |
|||
{{ 'device-profile.transport-type-required' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
<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]="profileConfigFormGroup"> |
|||
<form [formGroup]="profileConfigFormGroup" style="padding-bottom: 16px;"> |
|||
<ng-template matStepLabel>{{ 'device.wizard.profile-configuration' | translate}}</ng-template> |
|||
<mat-radio-group fxLayout="column" fxFlex formControlName="addProfileType"> |
|||
<mat-radio-button [value]="0" color="primary"> |
|||
<section> |
|||
<span translate>device.wizard.existing-device-profile</span> |
|||
<tb-device-profile-autocomplete |
|||
[required]="profileConfigFormGroup.get('addProfileType').value === 0" |
|||
[transportType]="deviceWizardFormGroup.get('transportType').value" |
|||
formControlName="deviceProfileId" |
|||
[addNewProfile]="false" |
|||
[editProfileEnabled]="false"> |
|||
</tb-device-profile-autocomplete> |
|||
</section> |
|||
</mat-radio-button> |
|||
<mat-radio-button [value]="1" color="primary"> |
|||
<section fxLayout="column"> |
|||
<span translate>device.wizard.new-device-profile</span> |
|||
<mat-form-field fxFlex class="mat-block"> |
|||
<mat-label translate>device-profile.device-profile</mat-label> |
|||
<input matInput formControlName="newDeviceProfileTitle" |
|||
[required]="profileConfigFormGroup.get('addProfileType').value === 1"> |
|||
<mat-error *ngIf="profileConfigFormGroup.get('newDeviceProfileTitle').hasError('required')"> |
|||
{{ 'device-profile.device-profile-required' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
</section> |
|||
</mat-radio-button> |
|||
</mat-radio-group> |
|||
</form> |
|||
</mat-step> |
|||
<mat-step [stepControl]="transportConfigFormGroup" *ngIf="createdProfile"> |
|||
<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" *ngIf="createdProfile"> |
|||
<form [formGroup]="alarmRulesFormGroup" style="padding-bottom: 16px;"> |
|||
<ng-template matStepLabel>{{'device-profile.alarm-rules' | 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]="specificConfigFormGroup"> |
|||
<ng-template matStepLabel>{{ 'device.wizard.specific-configuration' | translate }}</ng-template> |
|||
<form [formGroup]="specificConfigFormGroup" style="padding-bottom: 16px;"> |
|||
<tb-entity-autocomplete |
|||
formControlName="customerId" |
|||
labelText="device.wizard.customer-to-assign-device" |
|||
[entityType]="entityType.CUSTOMER"> |
|||
</tb-entity-autocomplete> |
|||
<mat-checkbox formControlName="setCredential">{{ 'device.wizard.add-credential' | translate }}</mat-checkbox> |
|||
<tb-device-credentials |
|||
[fxShow]="specificConfigFormGroup.get('setCredential').value" |
|||
formControlName="credential"> |
|||
</tb-device-credentials> |
|||
</form> |
|||
</mat-step> |
|||
</mat-horizontal-stepper> |
|||
</div> |
|||
<div mat-dialog-actions fxLayout="row wrap" fxLayoutAlign="space-between center"> |
|||
<button mat-button *ngIf="selectedIndex > 0" |
|||
[disabled]="(isLoading$ | async)" |
|||
(click)="previousStep()">{{ 'action.back' | translate }}</button> |
|||
<span *ngIf="selectedIndex == 0"></span> |
|||
<div fxLayout="row wrap" fxLayoutGap="20px"> |
|||
<button mat-button |
|||
[disabled]="(isLoading$ | async)" |
|||
(click)="cancel()">{{ 'action.cancel' | translate }}</button> |
|||
<button mat-raised-button |
|||
[disabled]="(isLoading$ | async) || selectedForm.invalid" |
|||
color="primary" |
|||
(click)="nextStep()">{{ nextStepButtonLabel$ | async | translate }}</button> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
@ -0,0 +1,38 @@ |
|||
/** |
|||
* 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. |
|||
*/ |
|||
:host { |
|||
.mat-dialog-content { |
|||
display: flex; |
|||
flex-direction: column; |
|||
overflow: hidden; |
|||
|
|||
.mat-stepper-horizontal { |
|||
display: flex; |
|||
flex-direction: column; |
|||
overflow: hidden; |
|||
} |
|||
} |
|||
} |
|||
|
|||
:host ::ng-deep { |
|||
.mat-dialog-content { |
|||
.mat-stepper-horizontal { |
|||
.mat-horizontal-content-container { |
|||
overflow: auto; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,286 @@ |
|||
///
|
|||
/// 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, |
|||
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 { BehaviorSubject, 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'; |
|||
|
|||
@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; |
|||
|
|||
nextStepButtonLabel$ = new BehaviorSubject<string>('action.continue'); |
|||
|
|||
createdProfile = false; |
|||
|
|||
entityType = EntityType; |
|||
|
|||
deviceTransportTypes = Object.keys(DeviceTransportType); |
|||
|
|||
deviceTransportTypeTranslations = deviceTransportTypeTranslationMap; |
|||
|
|||
deviceWizardFormGroup: FormGroup; |
|||
|
|||
profileConfigFormGroup: FormGroup; |
|||
|
|||
transportConfigFormGroup: FormGroup; |
|||
|
|||
alarmRulesFormGroup: FormGroup; |
|||
|
|||
specificConfigFormGroup: FormGroup; |
|||
|
|||
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 fb: FormBuilder) { |
|||
super(store, router, dialogRef); |
|||
this.deviceWizardFormGroup = this.fb.group({ |
|||
name: ['', Validators.required], |
|||
label: [''], |
|||
gateway: [false], |
|||
transportType: [DeviceTransportType.DEFAULT, Validators.required], |
|||
description: [''] |
|||
} |
|||
); |
|||
|
|||
this.profileConfigFormGroup = this.fb.group({ |
|||
addProfileType: [0], |
|||
deviceProfileId: [null, Validators.required], |
|||
newDeviceProfileTitle: [{value: null, disabled: true}] |
|||
} |
|||
); |
|||
|
|||
this.subscriptions.push(this.profileConfigFormGroup.get('addProfileType').valueChanges.subscribe( |
|||
(addProfileType: number) => { |
|||
if (addProfileType === 0) { |
|||
this.profileConfigFormGroup.get('deviceProfileId').setValidators([Validators.required]); |
|||
this.profileConfigFormGroup.get('deviceProfileId').enable(); |
|||
this.profileConfigFormGroup.get('newDeviceProfileTitle').setValidators(null); |
|||
this.profileConfigFormGroup.get('newDeviceProfileTitle').disable(); |
|||
this.profileConfigFormGroup.updateValueAndValidity(); |
|||
this.createdProfile = false; |
|||
} else { |
|||
this.profileConfigFormGroup.get('deviceProfileId').setValidators(null); |
|||
this.profileConfigFormGroup.get('deviceProfileId').disable(); |
|||
this.profileConfigFormGroup.get('newDeviceProfileTitle').setValidators([Validators.required]); |
|||
this.profileConfigFormGroup.get('newDeviceProfileTitle').enable(); |
|||
this.profileConfigFormGroup.updateValueAndValidity(); |
|||
this.createdProfile = true; |
|||
} |
|||
} |
|||
)); |
|||
|
|||
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.specificConfigFormGroup = this.fb.group({ |
|||
customerId: [null], |
|||
setCredential: [false], |
|||
credential: [{value: null, disabled: true}] |
|||
} |
|||
); |
|||
|
|||
this.subscriptions.push(this.specificConfigFormGroup.get('setCredential').valueChanges.subscribe((value) => { |
|||
if (value) { |
|||
this.specificConfigFormGroup.get('credential').enable(); |
|||
} else { |
|||
this.specificConfigFormGroup.get('credential').disable(); |
|||
} |
|||
})); |
|||
} |
|||
|
|||
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 { |
|||
if (this.selectedIndex < this.maxStepperIndex) { |
|||
this.addDeviceWizardStepper.next(); |
|||
} else { |
|||
this.add(); |
|||
} |
|||
} |
|||
|
|||
get selectedForm(): FormGroup { |
|||
const index = !this.createdProfile && this.selectedIndex === this.maxStepperIndex ? 4 : this.selectedIndex; |
|||
switch (index) { |
|||
case 0: |
|||
return this.deviceWizardFormGroup; |
|||
case 1: |
|||
return this.profileConfigFormGroup; |
|||
case 2: |
|||
return this.transportConfigFormGroup; |
|||
case 3: |
|||
return this.alarmRulesFormGroup; |
|||
case 4: |
|||
return this.specificConfigFormGroup; |
|||
} |
|||
} |
|||
|
|||
get maxStepperIndex(): number { |
|||
return this.addDeviceWizardStepper?._steps?.length - 1; |
|||
} |
|||
|
|||
private deviceProfileTransportTypeChanged(deviceTransportType: DeviceTransportType): void { |
|||
this.transportConfigFormGroup.patchValue( |
|||
{transportConfiguration: createDeviceProfileTransportConfiguration(deviceTransportType)}); |
|||
} |
|||
|
|||
private add(): void { |
|||
this.creatProfile().pipe( |
|||
mergeMap(profileId => this.createdDevice(profileId)), |
|||
mergeMap(device => this.saveCredential(device)) |
|||
).subscribe( |
|||
(created) => { |
|||
this.dialogRef.close(created); |
|||
} |
|||
); |
|||
} |
|||
|
|||
private creatProfile(): Observable<EntityId> { |
|||
if (this.profileConfigFormGroup.get('addProfileType').value) { |
|||
const deviceProfile: DeviceProfile = { |
|||
name: this.profileConfigFormGroup.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.profileConfigFormGroup.patchValue({ |
|||
deviceProfileId: profileId, |
|||
addProfileType: 0 |
|||
}); |
|||
this.addDeviceWizardStepper.selectedIndex = 2; |
|||
}) |
|||
); |
|||
} else { |
|||
return of(null); |
|||
} |
|||
} |
|||
|
|||
private createdDevice(profileId: EntityId = this.profileConfigFormGroup.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.specificConfigFormGroup.get('customerId').value) { |
|||
device.customerId = { |
|||
entityType: EntityType.CUSTOMER, |
|||
id: this.specificConfigFormGroup.get('customerId').value |
|||
}; |
|||
} |
|||
return this.data.entitiesTableConfig.saveEntity(device); |
|||
} |
|||
|
|||
private saveCredential(device: BaseData<HasId>): Observable<boolean> { |
|||
if (this.specificConfigFormGroup.get('setCredential').value) { |
|||
return this.deviceService.getDeviceCredentials(device.id.id).pipe( |
|||
mergeMap( |
|||
(deviceCredentials) => { |
|||
const deviceCredentialsValue = {...deviceCredentials, ...this.specificConfigFormGroup.value.credential}; |
|||
return this.deviceService.saveDeviceCredentials(deviceCredentialsValue); |
|||
} |
|||
), |
|||
map(() => true)); |
|||
} |
|||
return of(true); |
|||
} |
|||
|
|||
changeStep($event: StepperSelectionEvent): void { |
|||
this.selectedIndex = $event.selectedIndex; |
|||
if (this.selectedIndex === this.maxStepperIndex) { |
|||
this.nextStepButtonLabel$.next('action.add'); |
|||
} else { |
|||
this.nextStepButtonLabel$.next('action.continue'); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue