166 lines
5.9 KiB
TypeScript
Raw Normal View History

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';
import {
createDeviceConfiguration,
2020-11-24 17:11:19 +02:00
createDeviceTransportConfiguration, DeviceCredentials,
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';
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>;
deviceScope: 'tenant' | 'customer' | 'customer_user' | 'edge';
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 {
return this.fb.group(
{
name: [entity ? entity.name : '', [Validators.required]],
2020-08-26 15:00:11 +03:00
deviceProfileId: [entity ? entity.deviceProfileId : null, [Validators.required]],
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],
overwriteActivityTime: [entity && entity.additionalInfo ? entity.additionalInfo.overwriteActivityTime : false],
2019-08-13 19:58:35 +03:00
description: [entity && entity.additionalInfo ? entity.additionalInfo.description : ''],
}
)
}
);
}
updateForm(entity: DeviceInfo) {
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 : ''
}
});
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'
}));
}
onDeviceProfileUpdated() {
this.entitiesTableConfig.table.updateData(false);
}
onDeviceProfileChanged(deviceProfile: DeviceProfileInfo) {
2020-09-03 17:43:53 +03:00
if (deviceProfile && this.isEdit) {
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();
} 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();
}
}
2021-04-29 16:01:47 +03:00
this.entityForm.patchValue({
firmwareId: null,
softwareId: null
});
}
}
2019-08-13 19:58:35 +03:00
}