2019-08-13 19:58:35 +03:00
|
|
|
///
|
2021-01-11 13:42:16 +02:00
|
|
|
/// Copyright © 2016-2021 The Thingsboard Authors
|
2019-08-13 19:58:35 +03:00
|
|
|
///
|
|
|
|
|
/// 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.
|
|
|
|
|
///
|
|
|
|
|
|
2020-04-07 17:06:04 +03:00
|
|
|
import { Component, Inject } from '@angular/core';
|
2020-04-06 11:04:03 +03:00
|
|
|
import { Store } from '@ngrx/store';
|
2019-08-13 19:58:35 +03:00
|
|
|
import { AppState } from '@core/core.state';
|
2019-08-21 18:18:46 +03:00
|
|
|
import { EntityComponent } from '../../components/entity/entity.component';
|
2019-08-13 19:58:35 +03:00
|
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
2020-09-03 14:14:46 +03:00
|
|
|
import {
|
|
|
|
|
createDeviceConfiguration,
|
2020-11-24 17:11:19 +02:00
|
|
|
createDeviceTransportConfiguration, DeviceCredentials,
|
2020-09-03 14:14:46 +03:00
|
|
|
DeviceData,
|
|
|
|
|
DeviceInfo,
|
|
|
|
|
DeviceProfileInfo,
|
|
|
|
|
DeviceProfileType,
|
|
|
|
|
DeviceTransportType
|
|
|
|
|
} from '@shared/models/device.models';
|
2020-04-06 11:04:03 +03:00
|
|
|
import { EntityType } from '@shared/models/entity-type.models';
|
|
|
|
|
import { NULL_UUID } from '@shared/models/id/has-uuid';
|
|
|
|
|
import { ActionNotificationShow } from '@core/notification/notification.actions';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2020-04-07 17:06:04 +03:00
|
|
|
import { EntityTableConfig } from '@home/models/entity/entities-table-config.models';
|
2020-11-24 17:11:19 +02:00
|
|
|
import { Subject } from 'rxjs';
|
2021-05-31 18:15:31 +03:00
|
|
|
import { OtaUpdateType } from '@shared/models/ota-package.models';
|
2021-06-15 16:50:30 +03:00
|
|
|
import { distinctUntilChanged } from 'rxjs/operators';
|
2019-08-13 19:58:35 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-device',
|
|
|
|
|
templateUrl: './device.component.html',
|
|
|
|
|
styleUrls: ['./device.component.scss']
|
|
|
|
|
})
|
|
|
|
|
export class DeviceComponent extends EntityComponent<DeviceInfo> {
|
|
|
|
|
|
|
|
|
|
entityType = EntityType;
|
|
|
|
|
|
2020-11-24 17:11:19 +02:00
|
|
|
deviceCredentials$: Subject<DeviceCredentials>;
|
2020-11-23 12:13:05 +02:00
|
|
|
|
2021-06-23 11:58:04 +03:00
|
|
|
deviceScope: 'tenant' | 'customer' | 'customer_user' | 'edge' | 'edge_customer_user';
|
2019-08-13 19:58:35 +03:00
|
|
|
|
2021-05-31 18:15:31 +03:00
|
|
|
otaUpdateType = OtaUpdateType;
|
2021-04-29 16:01:47 +03:00
|
|
|
|
2019-08-13 19:58:35 +03:00
|
|
|
constructor(protected store: Store<AppState>,
|
2019-08-14 19:55:24 +03:00
|
|
|
protected translate: TranslateService,
|
2020-04-07 17:06:04 +03:00
|
|
|
@Inject('entity') protected entityValue: DeviceInfo,
|
2020-04-08 19:31:08 +03:00
|
|
|
@Inject('entitiesTableConfig') protected entitiesTableConfigValue: EntityTableConfig<DeviceInfo>,
|
2019-08-13 19:58:35 +03:00
|
|
|
public fb: FormBuilder) {
|
2020-04-08 19:31:08 +03:00
|
|
|
super(store, fb, entityValue, entitiesTableConfigValue);
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
this.deviceScope = this.entitiesTableConfig.componentsData.deviceScope;
|
2020-11-24 17:11:19 +02:00
|
|
|
this.deviceCredentials$ = this.entitiesTableConfigValue.componentsData.deviceCredentials$;
|
2019-08-13 19:58:35 +03:00
|
|
|
super.ngOnInit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hideDelete() {
|
|
|
|
|
if (this.entitiesTableConfig) {
|
|
|
|
|
return !this.entitiesTableConfig.deleteEnabled(this.entity);
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isAssignedToCustomer(entity: DeviceInfo): boolean {
|
|
|
|
|
return entity && entity.customerId && entity.customerId.id !== NULL_UUID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buildForm(entity: DeviceInfo): FormGroup {
|
2021-06-15 16:50:30 +03:00
|
|
|
const form = this.fb.group(
|
2019-08-13 19:58:35 +03:00
|
|
|
{
|
|
|
|
|
name: [entity ? entity.name : '', [Validators.required]],
|
2020-08-26 15:00:11 +03:00
|
|
|
deviceProfileId: [entity ? entity.deviceProfileId : null, [Validators.required]],
|
2021-04-13 15:10:13 +03:00
|
|
|
firmwareId: [entity ? entity.firmwareId : null],
|
2021-04-29 16:01:47 +03:00
|
|
|
softwareId: [entity ? entity.softwareId : null],
|
2019-08-13 19:58:35 +03:00
|
|
|
label: [entity ? entity.label : ''],
|
2020-08-26 15:00:11 +03:00
|
|
|
deviceData: [entity ? entity.deviceData : null, [Validators.required]],
|
2019-08-13 19:58:35 +03:00
|
|
|
additionalInfo: this.fb.group(
|
|
|
|
|
{
|
|
|
|
|
gateway: [entity && entity.additionalInfo ? entity.additionalInfo.gateway : false],
|
2021-01-22 18:33:51 +02:00
|
|
|
overwriteActivityTime: [entity && entity.additionalInfo ? entity.additionalInfo.overwriteActivityTime : false],
|
2019-08-13 19:58:35 +03:00
|
|
|
description: [entity && entity.additionalInfo ? entity.additionalInfo.description : ''],
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-06-15 16:50:30 +03:00
|
|
|
form.get('deviceProfileId').valueChanges.pipe(
|
|
|
|
|
distinctUntilChanged((prev, curr) => prev?.id === curr?.id)
|
|
|
|
|
).subscribe(profileId => {
|
|
|
|
|
if (profileId && this.isEdit) {
|
|
|
|
|
this.entityForm.patchValue({
|
|
|
|
|
firmwareId: null,
|
|
|
|
|
softwareId: null
|
|
|
|
|
}, {emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return form;
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateForm(entity: DeviceInfo) {
|
2021-01-15 18:55:35 +02:00
|
|
|
this.entityForm.patchValue({
|
2021-04-29 16:01:47 +03:00
|
|
|
name: entity.name,
|
|
|
|
|
deviceProfileId: entity.deviceProfileId,
|
|
|
|
|
firmwareId: entity.firmwareId,
|
|
|
|
|
softwareId: entity.softwareId,
|
|
|
|
|
label: entity.label,
|
|
|
|
|
deviceData: entity.deviceData,
|
|
|
|
|
additionalInfo: {
|
|
|
|
|
gateway: entity.additionalInfo ? entity.additionalInfo.gateway : false,
|
|
|
|
|
overwriteActivityTime: entity.additionalInfo ? entity.additionalInfo.overwriteActivityTime : false,
|
|
|
|
|
description: entity.additionalInfo ? entity.additionalInfo.description : ''
|
|
|
|
|
}
|
2021-01-15 18:55:35 +02:00
|
|
|
});
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
2019-08-14 19:55:24 +03:00
|
|
|
|
|
|
|
|
onDeviceIdCopied($event) {
|
|
|
|
|
this.store.dispatch(new ActionNotificationShow(
|
|
|
|
|
{
|
|
|
|
|
message: this.translate.instant('device.idCopiedMessage'),
|
|
|
|
|
type: 'success',
|
|
|
|
|
duration: 750,
|
|
|
|
|
verticalPosition: 'bottom',
|
|
|
|
|
horizontalPosition: 'right'
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 14:14:46 +03:00
|
|
|
onDeviceProfileUpdated() {
|
|
|
|
|
this.entitiesTableConfig.table.updateData(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onDeviceProfileChanged(deviceProfile: DeviceProfileInfo) {
|
2020-09-03 17:43:53 +03:00
|
|
|
if (deviceProfile && this.isEdit) {
|
2020-09-03 14:14:46 +03:00
|
|
|
const deviceProfileType: DeviceProfileType = deviceProfile.type;
|
|
|
|
|
const deviceTransportType: DeviceTransportType = deviceProfile.transportType;
|
|
|
|
|
let deviceData: DeviceData = this.entityForm.getRawValue().deviceData;
|
|
|
|
|
if (!deviceData) {
|
|
|
|
|
deviceData = {
|
|
|
|
|
configuration: createDeviceConfiguration(deviceProfileType),
|
|
|
|
|
transportConfiguration: createDeviceTransportConfiguration(deviceTransportType)
|
|
|
|
|
};
|
|
|
|
|
this.entityForm.patchValue({deviceData});
|
2020-09-03 17:43:53 +03:00
|
|
|
this.entityForm.markAsDirty();
|
2020-09-03 14:14:46 +03:00
|
|
|
} else {
|
|
|
|
|
let changed = false;
|
|
|
|
|
if (deviceData.configuration.type !== deviceProfileType) {
|
|
|
|
|
deviceData.configuration = createDeviceConfiguration(deviceProfileType);
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
if (deviceData.transportConfiguration.type !== deviceTransportType) {
|
|
|
|
|
deviceData.transportConfiguration = createDeviceTransportConfiguration(deviceTransportType);
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
if (changed) {
|
|
|
|
|
this.entityForm.patchValue({deviceData});
|
2020-09-03 17:43:53 +03:00
|
|
|
this.entityForm.markAsDirty();
|
2020-09-03 14:14:46 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|