thingsboard/ui-ngx/src/app/modules/home/components/profile/alarm/create-alarm-rules.component.ts

198 lines
6.0 KiB
TypeScript
Raw Normal View History

2020-09-09 18:27:28 +03:00
///
2023-01-31 10:43:56 +02:00
/// Copyright © 2016-2023 The Thingsboard Authors
2020-09-09 18:27:28 +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, forwardRef, Input, OnInit } from '@angular/core';
import {
AbstractControl,
ControlValueAccessor,
2023-02-02 15:55:06 +02:00
UntypedFormArray,
UntypedFormBuilder,
UntypedFormControl,
UntypedFormGroup,
2020-09-09 18:27:28 +03:00
NG_VALIDATORS,
2020-10-14 12:32:07 +03:00
NG_VALUE_ACCESSOR, ValidationErrors,
2020-09-09 18:27:28 +03:00
Validator,
Validators
} from '@angular/forms';
2020-10-14 12:32:07 +03:00
import { AlarmRule, alarmRuleValidator } from '@shared/models/device.models';
2020-09-09 18:27:28 +03:00
import { MatDialog } from '@angular/material/dialog';
import { Subscription } from 'rxjs';
import { AlarmSeverity, alarmSeverityTranslations } from '@shared/models/alarm.models';
import { EntityId } from '@shared/models/id/entity-id';
2020-09-09 18:27:28 +03:00
@Component({
selector: 'tb-create-alarm-rules',
templateUrl: './create-alarm-rules.component.html',
styleUrls: ['./create-alarm-rules.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CreateAlarmRulesComponent),
multi: true
},
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => CreateAlarmRulesComponent),
multi: true,
}
]
})
export class CreateAlarmRulesComponent implements ControlValueAccessor, OnInit, Validator {
alarmSeverities = Object.keys(AlarmSeverity);
alarmSeverityEnum = AlarmSeverity;
alarmSeverityTranslationMap = alarmSeverityTranslations;
@Input()
disabled: boolean;
@Input()
deviceProfileId: EntityId;
2023-02-02 15:55:06 +02:00
createAlarmRulesFormGroup: UntypedFormGroup;
2020-09-09 18:27:28 +03:00
private usedSeverities: AlarmSeverity[] = [];
2020-09-09 18:27:28 +03:00
private valueChangeSubscription: Subscription = null;
private propagateChange = (v: any) => { };
constructor(private dialog: MatDialog,
2023-02-02 15:55:06 +02:00
private fb: UntypedFormBuilder) {
2020-09-09 18:27:28 +03:00
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}
registerOnTouched(fn: any): void {
}
ngOnInit() {
this.createAlarmRulesFormGroup = this.fb.group({
createAlarmRules: this.fb.array([])
});
}
2023-02-02 15:55:06 +02:00
createAlarmRulesFormArray(): UntypedFormArray {
return this.createAlarmRulesFormGroup.get('createAlarmRules') as UntypedFormArray;
2020-09-09 18:27:28 +03:00
}
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
if (this.disabled) {
this.createAlarmRulesFormGroup.disable({emitEvent: false});
} else {
this.createAlarmRulesFormGroup.enable({emitEvent: false});
}
}
writeValue(createAlarmRules: {[severity: string]: AlarmRule}): void {
if (this.valueChangeSubscription) {
this.valueChangeSubscription.unsubscribe();
}
const createAlarmRulesControls: Array<AbstractControl> = [];
if (createAlarmRules) {
Object.keys(createAlarmRules).forEach((severity) => {
const createAlarmRule = createAlarmRules[severity];
if (severity === 'empty') {
severity = null;
}
createAlarmRulesControls.push(this.fb.group({
severity: [severity, Validators.required],
alarmRule: [createAlarmRule, Validators.required]
}));
});
}
this.createAlarmRulesFormGroup.setControl('createAlarmRules', this.fb.array(createAlarmRulesControls));
if (this.disabled) {
this.createAlarmRulesFormGroup.disable({emitEvent: false});
} else {
this.createAlarmRulesFormGroup.enable({emitEvent: false});
}
this.valueChangeSubscription = this.createAlarmRulesFormGroup.valueChanges.subscribe(() => {
this.updateModel();
});
this.updateUsedSeverities();
2020-09-14 19:23:18 +03:00
if (!this.disabled && !this.createAlarmRulesFormGroup.valid) {
this.updateModel();
}
2020-09-09 18:27:28 +03:00
}
public removeCreateAlarmRule(index: number) {
2023-02-02 15:55:06 +02:00
(this.createAlarmRulesFormGroup.get('createAlarmRules') as UntypedFormArray).removeAt(index);
2020-09-09 18:27:28 +03:00
}
public addCreateAlarmRule() {
const createAlarmRule: AlarmRule = {
condition: {
condition: []
}
};
2023-02-02 15:55:06 +02:00
const createAlarmRulesArray = this.createAlarmRulesFormGroup.get('createAlarmRules') as UntypedFormArray;
2020-09-09 18:27:28 +03:00
createAlarmRulesArray.push(this.fb.group({
2020-10-14 12:32:07 +03:00
severity: [this.getFirstUnusedSeverity(), Validators.required],
alarmRule: [createAlarmRule, alarmRuleValidator]
2020-09-09 18:27:28 +03:00
}));
this.createAlarmRulesFormGroup.updateValueAndValidity();
2020-10-14 12:32:07 +03:00
if (!this.createAlarmRulesFormGroup.valid) {
this.updateModel();
}
}
private getFirstUnusedSeverity(): AlarmSeverity {
for (const severityKey of Object.keys(AlarmSeverity)) {
const severity = AlarmSeverity[severityKey];
if (this.usedSeverities.indexOf(severity) === -1) {
return severity;
}
}
return null;
2020-09-09 18:27:28 +03:00
}
2023-02-02 15:55:06 +02:00
public validate(c: UntypedFormControl) {
2020-09-11 19:27:29 +03:00
return (this.createAlarmRulesFormGroup.valid) ? null : {
2020-09-09 18:27:28 +03:00
createAlarmRules: {
valid: false,
},
};
}
public isDisabledSeverity(severity: AlarmSeverity, index: number): boolean {
const usedIndex = this.usedSeverities.indexOf(severity);
return usedIndex > -1 && usedIndex !== index;
}
private updateUsedSeverities() {
this.usedSeverities = [];
const value: {severity: string, alarmRule: AlarmRule}[] = this.createAlarmRulesFormGroup.get('createAlarmRules').value;
value.forEach((rule, index) => {
this.usedSeverities[index] = AlarmSeverity[rule.severity];
});
}
2020-09-09 18:27:28 +03:00
private updateModel() {
2020-09-10 19:37:14 +03:00
const value: {severity: string, alarmRule: AlarmRule}[] = this.createAlarmRulesFormGroup.get('createAlarmRules').value;
const createAlarmRules: {[severity: string]: AlarmRule} = {};
value.forEach(v => {
createAlarmRules[v.severity] = v.alarmRule;
});
this.updateUsedSeverities();
2020-09-10 19:37:14 +03:00
this.propagateChange(createAlarmRules);
2020-09-09 18:27:28 +03:00
}
}