thingsboard/ui-ngx/src/app/modules/home/components/alarm/alarm-details-dialog.component.ts

184 lines
6.1 KiB
TypeScript
Raw Normal View History

2019-08-29 12:08:49 +03:00
///
2024-01-09 10:46:16 +02:00
/// Copyright © 2016-2024 The Thingsboard Authors
2019-08-29 12:08:49 +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.
///
import { Component, Inject, OnInit, ViewChild } from '@angular/core';
2020-02-10 13:15:29 +02:00
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
2019-08-29 12:08:49 +03:00
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
2023-02-02 15:55:06 +02:00
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
2020-04-23 12:01:58 +03:00
import { Observable, ReplaySubject } from 'rxjs';
2019-08-29 12:08:49 +03:00
import { Router } from '@angular/router';
import { DialogComponent } from '@app/shared/components/dialog.component';
import {
AlarmInfo,
alarmSeverityColors,
alarmSeverityTranslations,
AlarmStatus,
alarmStatusTranslations
} from '@app/shared/models/alarm.models';
import { AlarmService } from '@core/http/alarm.service';
2020-04-23 12:01:58 +03:00
import { tap } from 'rxjs/operators';
2019-08-29 12:08:49 +03:00
import { DatePipe } from '@angular/common';
import { TranslateService } from '@ngx-translate/core';
import { AlarmCommentComponent } from '@home/components/alarm/alarm-comment.component';
2023-03-29 12:15:46 +03:00
import { MillisecondsToTimeStringPipe } from '@shared/pipe/milliseconds-to-time-string.pipe';
2023-05-19 10:55:19 +03:00
import { UtilsService } from '@core/services/utils.service';
2019-08-29 12:08:49 +03:00
export interface AlarmDetailsDialogData {
2022-06-21 17:14:03 +03:00
alarmId?: string;
alarm?: AlarmInfo;
2019-08-29 12:08:49 +03:00
allowAcknowledgment: boolean;
allowClear: boolean;
displayDetails: boolean;
2023-03-29 12:15:46 +03:00
allowAssign: boolean;
2019-08-29 12:08:49 +03:00
}
@Component({
selector: 'tb-alarm-details-dialog',
templateUrl: './alarm-details-dialog.component.html',
2023-02-16 14:51:24 +02:00
styleUrls: ['./alarm-details-dialog.component.scss']
2019-08-29 12:08:49 +03:00
})
export class AlarmDetailsDialogComponent extends DialogComponent<AlarmDetailsDialogComponent, boolean> implements OnInit {
2022-06-21 17:14:03 +03:00
alarmId: string;
2023-02-02 15:55:06 +02:00
alarmFormGroup: UntypedFormGroup;
2019-08-29 12:08:49 +03:00
allowAcknowledgment: boolean;
allowClear: boolean;
displayDetails: boolean;
2023-03-29 12:15:46 +03:00
allowAssign: boolean;
2019-08-29 12:08:49 +03:00
2020-04-23 12:01:58 +03:00
loadAlarmSubject = new ReplaySubject<AlarmInfo>();
2019-08-29 12:08:49 +03:00
alarm$: Observable<AlarmInfo> = this.loadAlarmSubject.asObservable().pipe(
2020-04-23 12:01:58 +03:00
tap(alarm => this.loadAlarmFields(alarm))
2019-08-29 12:08:49 +03:00
);
alarmSeverityColorsMap = alarmSeverityColors;
alarmStatuses = AlarmStatus;
alarmUpdated = false;
@ViewChild('alarmCommentComponent', { static: true }) alarmCommentComponent: AlarmCommentComponent;
2019-08-29 12:08:49 +03:00
constructor(protected store: Store<AppState>,
protected router: Router,
private datePipe: DatePipe,
2023-03-29 12:15:46 +03:00
private millisecondsToTimeStringPipe: MillisecondsToTimeStringPipe,
2019-08-29 12:08:49 +03:00
private translate: TranslateService,
@Inject(MAT_DIALOG_DATA) public data: AlarmDetailsDialogData,
private alarmService: AlarmService,
2023-05-19 10:55:19 +03:00
private utils: UtilsService,
2019-08-29 12:08:49 +03:00
public dialogRef: MatDialogRef<AlarmDetailsDialogComponent, boolean>,
public fb: UntypedFormBuilder) {
2019-08-29 12:08:49 +03:00
super(store, router, dialogRef);
this.allowAcknowledgment = data.allowAcknowledgment;
this.allowClear = data.allowClear;
this.displayDetails = data.displayDetails;
2023-03-29 12:15:46 +03:00
this.allowAssign = data.allowAssign;
2019-08-29 12:08:49 +03:00
this.alarmFormGroup = this.fb.group(
{
originatorName: [''],
2023-03-29 12:15:46 +03:00
alarmSeverity: [''],
2019-08-29 12:08:49 +03:00
startTime: [''],
2023-03-29 12:15:46 +03:00
duration: [''],
2019-08-29 12:08:49 +03:00
type: [''],
alarmStatus: [''],
alarmDetails: [null]
}
);
2022-06-21 17:14:03 +03:00
if (!this.data.alarm) {
this.alarmId = this.data.alarmId;
this.loadAlarm();
} else {
this.alarmId = this.data.alarm?.id?.id;
setTimeout(() => {
this.loadAlarmSubject.next(this.data.alarm);
}, 0);
2022-06-21 17:14:03 +03:00
}
2019-08-29 12:08:49 +03:00
}
loadAlarm() {
this.alarmService.getAlarmInfo(this.alarmId, {ignoreLoading: true}).subscribe(
2019-08-29 12:08:49 +03:00
alarm => this.loadAlarmSubject.next(alarm)
);
}
loadAlarmFields(alarm: AlarmInfo) {
this.alarmFormGroup.get('originatorName')
2023-03-29 12:15:46 +03:00
.patchValue(alarm.originatorLabel ? alarm.originatorLabel : alarm.originatorName);
this.alarmFormGroup.get('alarmSeverity')
.patchValue(this.translate.instant(alarmSeverityTranslations.get(alarm.severity)));
2019-08-29 12:08:49 +03:00
if (alarm.startTs) {
this.alarmFormGroup.get('startTime')
.patchValue(this.datePipe.transform(alarm.startTs, 'yyyy-MM-dd HH:mm:ss'));
}
2023-03-29 12:15:46 +03:00
if (alarm.startTs || alarm.endTs) {
let duration = '';
if (alarm.startTs && (alarm.status === AlarmStatus.ACTIVE_ACK || alarm.status === AlarmStatus.ACTIVE_UNACK)) {
duration = this.millisecondsToTimeStringPipe.transform(Date.now() - alarm.startTs);
}
if (alarm.endTs && (alarm.status === AlarmStatus.CLEARED_ACK || alarm.status === AlarmStatus.CLEARED_UNACK)) {
duration = this.millisecondsToTimeStringPipe.transform(alarm.endTs - alarm.startTs);
}
this.alarmFormGroup.get('duration').patchValue(duration);
2019-08-29 12:08:49 +03:00
}
2023-05-19 10:55:19 +03:00
this.alarmFormGroup.get('type').patchValue(this.utils.customTranslation(alarm.type, alarm.type));
2019-08-29 12:08:49 +03:00
this.alarmFormGroup.get('alarmStatus')
.patchValue(this.translate.instant(alarmStatusTranslations.get(alarm.status)));
this.alarmFormGroup.get('alarmDetails').patchValue(alarm.details);
}
ngOnInit(): void {
}
close(): void {
this.dialogRef.close(this.alarmUpdated);
}
acknowledge(): void {
2022-06-21 17:14:03 +03:00
if (this.alarmId) {
this.alarmService.ackAlarm(this.alarmId).subscribe(
() => {
this.alarmUpdated = true;
this.loadAlarm();
this.alarmCommentComponent.loadAlarmComments();
2022-06-21 17:14:03 +03:00
}
);
}
2019-08-29 12:08:49 +03:00
}
clear(): void {
2022-06-21 17:14:03 +03:00
if (this.alarmId) {
this.alarmService.clearAlarm(this.alarmId).subscribe(
() => {
this.alarmUpdated = true;
this.loadAlarm();
this.alarmCommentComponent.loadAlarmComments();
2022-06-21 17:14:03 +03:00
}
);
}
2019-08-29 12:08:49 +03:00
}
onReassign(): void {
this.alarmUpdated = true;
2023-05-23 09:37:58 +03:00
this.loadAlarm();
this.alarmCommentComponent.loadAlarmComments();
2023-02-16 14:51:24 +02:00
}
2019-08-29 12:08:49 +03:00
}