130 lines
4.8 KiB
TypeScript
Raw Normal View History

2019-08-13 19:58:35 +03:00
///
2020-02-20 10:26:43 +02:00
/// Copyright © 2016-2020 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-04-06 11:04:03 +03:00
import { DeviceInfo } from '@shared/models/device.models';
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';
import { DeviceService } from '@core/http/device.service';
import { ClipboardService } from 'ngx-clipboard';
2020-04-07 17:06:04 +03:00
import { EntityTableConfig } from '@home/models/entity/entities-table-config.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;
deviceScope: 'tenant' | 'customer' | 'customer_user';
constructor(protected store: Store<AppState>,
2019-08-14 19:55:24 +03:00
protected translate: TranslateService,
private deviceService: DeviceService,
private clipboardService: ClipboardService,
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;
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]],
2019-08-13 19:58:35 +03:00
type: [entity ? entity.type : null, [Validators.required]],
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],
description: [entity && entity.additionalInfo ? entity.additionalInfo.description : ''],
}
)
}
);
}
updateForm(entity: DeviceInfo) {
this.entityForm.patchValue({name: entity.name});
2020-08-26 15:00:11 +03:00
this.entityForm.patchValue({deviceProfileId: entity.deviceProfileId});
2019-08-13 19:58:35 +03:00
this.entityForm.patchValue({type: entity.type});
this.entityForm.patchValue({label: entity.label});
2020-08-26 15:00:11 +03:00
this.entityForm.patchValue({deviceData: entity.deviceData});
2019-08-13 19:58:35 +03:00
this.entityForm.patchValue({additionalInfo:
{gateway: entity.additionalInfo ? entity.additionalInfo.gateway : false}});
this.entityForm.patchValue({additionalInfo: {description: entity.additionalInfo ? entity.additionalInfo.description : ''}});
}
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'
}));
}
copyAccessToken($event) {
if (this.entity.id) {
this.deviceService.getDeviceCredentials(this.entity.id.id, true).subscribe(
(deviceCredentials) => {
const credentialsId = deviceCredentials.credentialsId;
if (this.clipboardService.copyFromContent(credentialsId)) {
this.store.dispatch(new ActionNotificationShow(
{
message: this.translate.instant('device.accessTokenCopiedMessage'),
type: 'success',
duration: 750,
verticalPosition: 'bottom',
horizontalPosition: 'right'
}));
}
}
);
}
}
2019-08-13 19:58:35 +03:00
}