thingsboard/ui-ngx/src/app/modules/home/components/wizard/device-wizard-dialog.component.ts

332 lines
12 KiB
TypeScript
Raw Normal View History

2020-09-29 15:47:17 +03:00
///
/// 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, deviceTransportTypeConfigurationInfoMap, deviceTransportTypeHintMap,
2020-09-29 15:47:17 +03:00
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';
2020-10-09 17:19:51 +03:00
import { Observable, of, Subscription } from 'rxjs';
2020-09-29 15:47:17 +03:00
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';
2020-10-09 17:19:51 +03:00
import { BreakpointObserver, BreakpointState } from '@angular/cdk/layout';
import { MediaBreakpoints } from '@shared/models/constants';
2020-09-29 15:47:17 +03:00
@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;
2020-10-09 17:19:51 +03:00
showNext = true;
2020-09-29 15:47:17 +03:00
2020-10-09 17:19:51 +03:00
createProfile = false;
createTransportConfiguration = false;
2020-09-29 15:47:17 +03:00
entityType = EntityType;
deviceTransportTypes = Object.keys(DeviceTransportType);
deviceTransportTypeTranslations = deviceTransportTypeTranslationMap;
2020-10-09 17:19:51 +03:00
deviceTransportTypeHints = deviceTransportTypeHintMap;
2020-09-29 15:47:17 +03:00
2020-10-09 17:19:51 +03:00
deviceWizardFormGroup: FormGroup;
2020-09-29 15:47:17 +03:00
transportConfigFormGroup: FormGroup;
alarmRulesFormGroup: FormGroup;
2020-10-09 17:19:51 +03:00
credentialsFormGroup: FormGroup;
customerFormGroup: FormGroup;
labelPosition = 'end';
2020-09-29 15:47:17 +03:00
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,
2020-10-09 17:19:51 +03:00
private breakpointObserver: BreakpointObserver,
2020-09-29 15:47:17 +03:00
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],
2020-10-09 17:19:51 +03:00
newDeviceProfileTitle: [{value: null, disabled: true}],
description: ['']
2020-09-29 15:47:17 +03:00
}
);
2020-10-09 17:19:51 +03:00
this.subscriptions.push(this.deviceWizardFormGroup.get('addProfileType').valueChanges.subscribe(
2020-09-29 15:47:17 +03:00
(addProfileType: number) => {
if (addProfileType === 0) {
2020-10-09 17:19:51 +03:00
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;
2020-09-29 15:47:17 +03:00
} else {
2020-10-09 17:19:51 +03:00
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 &&
deviceTransportTypeConfigurationInfoMap.get(this.deviceWizardFormGroup.get('transportType').value).hasProfileConfiguration;
2020-09-29 15:47:17 +03:00
}
}
));
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]
}
);
2020-10-09 17:19:51 +03:00
this.credentialsFormGroup = this.fb.group({
2020-09-29 15:47:17 +03:00
setCredential: [false],
credential: [{value: null, disabled: true}]
}
);
2020-10-09 17:19:51 +03:00
this.subscriptions.push(this.credentialsFormGroup.get('setCredential').valueChanges.subscribe((value) => {
2020-09-29 15:47:17 +03:00
if (value) {
2020-10-09 17:19:51 +03:00
this.credentialsFormGroup.get('credential').enable();
2020-09-29 15:47:17 +03:00
} else {
2020-10-09 17:19:51 +03:00
this.credentialsFormGroup.get('credential').disable();
2020-09-29 15:47:17 +03:00
}
}));
2020-10-09 17:19:51 +03:00
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';
}
}
));
2020-09-29 15:47:17 +03:00
}
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 {
2020-10-09 17:19:51 +03:00
this.addDeviceWizardStepper.next();
2020-09-29 15:47:17 +03:00
}
2020-10-09 17:19:51 +03:00
getFormLabel(index: number): string {
if (index > 0) {
if (!this.createProfile) {
index += 2;
} else if (!this.createTransportConfiguration) {
index += 1;
}
}
2020-09-29 15:47:17 +03:00
switch (index) {
case 0:
2020-10-09 17:19:51 +03:00
return 'device.wizard.device-details';
2020-09-29 15:47:17 +03:00
case 1:
2020-10-09 17:19:51 +03:00
return 'device-profile.transport-configuration';
2020-09-29 15:47:17 +03:00
case 2:
2020-10-09 17:19:51 +03:00
return 'device-profile.alarm-rules';
2020-09-29 15:47:17 +03:00
case 3:
2020-10-09 17:19:51 +03:00
return 'device.credentials';
2020-09-29 15:47:17 +03:00
case 4:
2020-10-09 17:19:51 +03:00
return 'customer.customer';
2020-09-29 15:47:17 +03:00
}
}
get maxStepperIndex(): number {
return this.addDeviceWizardStepper?._steps?.length - 1;
}
private deviceProfileTransportTypeChanged(deviceTransportType: DeviceTransportType): void {
this.transportConfigFormGroup.patchValue(
{transportConfiguration: createDeviceProfileTransportConfiguration(deviceTransportType)});
2020-10-09 17:19:51 +03:00
this.createTransportConfiguration = this.createProfile && deviceTransportType &&
deviceTransportTypeConfigurationInfoMap.get(deviceTransportType).hasProfileConfiguration;
2020-09-29 15:47:17 +03:00
}
2020-10-09 17:19:51 +03:00
add(): void {
if (this.allValid()) {
this.createDeviceProfile().pipe(
mergeMap(profileId => this.createDevice(profileId)),
mergeMap(device => this.saveCredentials(device))
).subscribe(
(created) => {
this.dialogRef.close(created);
}
);
}
2020-09-29 15:47:17 +03:00
}
2020-10-09 17:19:51 +03:00
private createDeviceProfile(): Observable<EntityId> {
if (this.deviceWizardFormGroup.get('addProfileType').value) {
2020-09-29 15:47:17 +03:00
const deviceProfile: DeviceProfile = {
2020-10-09 17:19:51 +03:00
name: this.deviceWizardFormGroup.get('newDeviceProfileTitle').value,
2020-09-29 15:47:17 +03:00
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) => {
2020-10-09 17:19:51 +03:00
this.deviceWizardFormGroup.patchValue({
2020-09-29 15:47:17 +03:00
deviceProfileId: profileId,
addProfileType: 0
});
})
);
} else {
return of(this.deviceWizardFormGroup.get('deviceProfileId').value);
2020-09-29 15:47:17 +03:00
}
}
private createDevice(profileId): Observable<BaseData<HasId>> {
2020-09-29 15:47:17 +03:00
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
};
2020-10-09 17:19:51 +03:00
if (this.customerFormGroup.get('customerId').value) {
2020-09-29 15:47:17 +03:00
device.customerId = {
entityType: EntityType.CUSTOMER,
2020-10-09 17:19:51 +03:00
id: this.customerFormGroup.get('customerId').value
2020-09-29 15:47:17 +03:00
};
}
return this.data.entitiesTableConfig.saveEntity(device);
}
2020-10-09 17:19:51 +03:00
private saveCredentials(device: BaseData<HasId>): Observable<boolean> {
if (this.credentialsFormGroup.get('setCredential').value) {
2020-09-29 15:47:17 +03:00
return this.deviceService.getDeviceCredentials(device.id.id).pipe(
mergeMap(
(deviceCredentials) => {
2020-10-09 17:19:51 +03:00
const deviceCredentialsValue = {...deviceCredentials, ...this.credentialsFormGroup.value.credential};
2020-09-29 15:47:17 +03:00
return this.deviceService.saveDeviceCredentials(deviceCredentialsValue);
}
),
map(() => true));
}
return of(true);
}
2020-10-09 17:19:51 +03:00
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;
}
}
2020-09-29 15:47:17 +03:00
changeStep($event: StepperSelectionEvent): void {
this.selectedIndex = $event.selectedIndex;
if (this.selectedIndex === this.maxStepperIndex) {
2020-10-09 17:19:51 +03:00
this.showNext = false;
2020-09-29 15:47:17 +03:00
} else {
2020-10-09 17:19:51 +03:00
this.showNext = true;
2020-09-29 15:47:17 +03:00
}
}
}