| | |
| | | import { filter } from 'rxjs/operators'; |
| | | import { HttpClient } from '@angular/common/http'; |
| | | import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms'; |
| | | import { SensorsService } from '@business/services/http/sensors.service'; |
| | | import { Grid, PageBean } from '@business/entity/grid'; |
| | | import { Organization } from '@business/entity/data'; |
| | | import { Component, OnInit } from '@angular/core'; |
| | | import { Component, OnInit, OnDestroy } from '@angular/core'; |
| | | import { OrganizationService } from '@business/services/http/organization.service'; |
| | | import { _HttpClient } from '@delon/theme'; |
| | | import { AlarmConfigService } from '@business/services/http/alarm-config.service'; |
| | | import { AlarmStyle } from '@business/enum/types.enum'; |
| | | |
| | | @Component({ |
| | | selector: 'app-organization-config', |
| | | templateUrl: './organization-config.component.html', |
| | | styles: [] |
| | | styles: [], |
| | | providers: [AlarmConfigService] |
| | | }) |
| | | export class OrganizationConfigComponent implements OnInit { |
| | | organization: Organization; |
| | | export class OrganizationConfigComponent implements OnInit, OnDestroy { |
| | | ngOnDestroy(): void { |
| | | this.backToList(); |
| | | } |
| | | private organization:Organization; |
| | | grid: Grid<object> = new Grid<object>(null); |
| | | validateForm: FormGroup; |
| | | constructor( |
| | | private organizationService: OrganizationService |
| | | ) { |
| | | console.log(this.organizationService.data); |
| | | private organizationService: OrganizationService, |
| | | private sensorsService: SensorsService, |
| | | private alarmConfigService:AlarmConfigService, |
| | | private formBuilder: FormBuilder, |
| | | private http: _HttpClient |
| | | ) { |
| | | this.organization = this.organizationService.data; |
| | | } |
| | | |
| | | ngOnInit() { |
| | | this.load(); |
| | | } |
| | | load(reload: boolean = false) { |
| | | if (reload) { |
| | | this.grid.pageIndex = 1 ; |
| | | } |
| | | // TODO |
| | | // 延时加载避免ExpressionChangedAfterItHasBeenCheckedError |
| | | // setTimeout(() => { |
| | | // this.grid.loading = true; |
| | | // }, 1); |
| | | let pageBean = this.organizationService.config.pageBean;// |
| | | let resultBean = this.organizationService.config.resultBean; |
| | | resultBean = resultBean == null?{} : resultBean; |
| | | if (pageBean != null && pageBean.data != null) { |
| | | this.grid.initData(pageBean); |
| | | this.grid.refreshStatus(); |
| | | setTimeout(() => { |
| | | this.grid.loading = false; |
| | | }, 1); |
| | | let validates = {}; |
| | | let data = resultBean.data == null ?{}:resultBean.data; |
| | | const value = data['value'] == null ? {} : data['value']; |
| | | let alarmLevels = value['alarmLevels']; |
| | | alarmLevels = alarmLevels==null ? {} : alarmLevels; |
| | | // 三级警报 |
| | | const level_num = 3; |
| | | this.grid.data.forEach( |
| | | (sensor: object) => { |
| | | const sensorLevel = alarmLevels[sensor['key']] == null ?{}:alarmLevels[sensor['key']]; |
| | | const enable_key = sensor['key'] + '_enable'; |
| | | const enable_value = sensorLevel['enable'] == null ? false : sensorLevel['enable'] == 1; |
| | | validates[enable_key] = enable_value; |
| | | const increments:number[] = sensorLevel['increment']; |
| | | const degression:number[] = sensorLevel['degression']; |
| | | for(let i=0;i<level_num;i++){ |
| | | const increment_key = sensor['key']+'_level_increment_'+(i+1); |
| | | if( increments == null || increments.length == 0 ){ |
| | | validates[increment_key] = [null,[Validators.pattern('^\\d+(\\.\\d+)?$')]]; |
| | | }else{ |
| | | const increment_value = increments.length>i ? increments[i]:null ; |
| | | validates[increment_key] = [increment_value,[Validators.pattern('^\\d+(\\.\\d+)?$')]]; |
| | | } |
| | | const degression_key = sensor['key']+'_level_degression_'+(i+1); |
| | | if( degression == null || degression.length == 0 ){ |
| | | validates[degression_key] = [null,[Validators.pattern('^\\d+(\\.\\d+)?$')]]; |
| | | }else{ |
| | | const degression_value = degression.length>i ? degression[i]:null ; |
| | | validates[degression_key] = [degression_value,[Validators.pattern('^\\d+(\\.\\d+)?$')]]; |
| | | } |
| | | } |
| | | |
| | | } |
| | | ); |
| | | validates['_allChecked'] = [this.grid.allChecked]; |
| | | //报警方式加载 |
| | | let alarmMode = value['alarmMode']; |
| | | alarmMode = alarmMode==null ? {}: alarmMode; |
| | | this.alarmModes.push( |
| | | {label:'邮件',value:AlarmStyle.email, disabled: false}, |
| | | {label:'短信',value:AlarmStyle.sms, disabled: false}, |
| | | {label:'语音',value:AlarmStyle.voice, disabled: false}, |
| | | {label:'微信',value:AlarmStyle.weixin, disabled: false} |
| | | ); |
| | | validates['mode_enable'] = [alarmMode['enable']]; |
| | | for(let i = 0; i<level_num; i++){ |
| | | const n = i+1; |
| | | validates['mode_level'+n] = [AlarmStyle[alarmMode['level'+n]]]; |
| | | } |
| | | this.validateForm = this.formBuilder.group(validates); |
| | | this.grid.loading = false; |
| | | this.enableKeys = Object.keys(this.validateForm.value).filter( |
| | | (key: string) => { |
| | | return key.endsWith('_enable'); |
| | | } |
| | | ); |
| | | this.enableKeys.forEach( |
| | | (key: string) => { |
| | | this.validateForm.controls[key].valueChanges.subscribe( |
| | | (value: any) => { |
| | | if(this._allCheckTriggers <= 0){ |
| | | this.refreshIndeterminate(); |
| | | } else { |
| | | this._allCheckTriggers--; |
| | | } |
| | | } |
| | | ); |
| | | } |
| | | ); |
| | | this.refreshIndeterminate(); |
| | | } |
| | | |
| | | } |
| | | alarmModes: {label:string,value:AlarmStyle,disabled: boolean} [] = []; |
| | | |
| | | backToList() { |
| | | this.organizationService.handle = 'list'; |
| | | this.organizationService.title = '组织列表'; |
| | | } |
| | | enableKeys: string []; |
| | | indeterminate: boolean; |
| | | checkAll(param){ |
| | | this._allCheckTriggers = 0; |
| | | this.enableKeys.forEach( |
| | | (key: string) => { |
| | | this._allCheckTriggers++; |
| | | this.validateForm.controls[key].setValue(param); |
| | | } |
| | | ); |
| | | this.refreshIndeterminate(); |
| | | } |
| | | private _allCheckTriggers: number = 0; |
| | | refreshIndeterminate(){ |
| | | const allChecked = this.enableKeys.every(key => this.validateForm.controls[key].value); |
| | | const allUnChecked = this.enableKeys.every(key => !this.validateForm.controls[key].value); |
| | | this.indeterminate = (!allChecked) && (!allUnChecked); |
| | | } |
| | | save($event, value, valid) { |
| | | $event.preventDefault(); |
| | | if (valid) { |
| | | |
| | | |
| | | } |
| | | } |
| | | } |