import { HttpClient } from '@angular/common/http'; import { Component, OnInit, ViewContainerRef } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { NzMessageService } from 'ng-zorro-antd/message'; import { NzModalService } from 'ng-zorro-antd/modal'; import { TreeService } from 'src/app/service/tree.service'; import { ScriptComponent } from './script/script.component'; @Component({ selector: 'app-status-monitoring', templateUrl: './status-monitoring.component.html', styleUrls: ['./status-monitoring.component.scss'] }) export class StatusMonitoringComponent implements OnInit { constructor(private http: HttpClient, private fb: FormBuilder, private toTree: TreeService, private message: NzMessageService, private modal: NzModalService, private viewContainerRef: ViewContainerRef) { } validateForm!: FormGroup; ngOnInit(): void { this.validateForm = this.fb.group({ organization: [null], state: [null] }); this.getAllOrganization() } //获取所有组织机构 nodes: any = [] defaultOrId: string defaultExpandedKeys = []; getAllOrganization() { let params = { ContainsChildren: true, pageSize: 9999 } this.http.get('/api/Organizations', { params: params }).subscribe((data: any) => { console.log('组织机构列表', data) data.items.forEach(element => { element.key = element.id element.title = element.name // element.selectable = false }); this.nodes = [...this.toTree.toTree(data.items)] this.defaultOrId = this.nodes[0].id this.validateForm.value.organization = this.defaultOrId this.getConditionMonitoring() }) } submitForm(): void { for (const i in this.validateForm.controls) { this.validateForm.controls[i].markAsDirty(); this.validateForm.controls[i].updateValueAndValidity(); } this.getConditionMonitoring() } resetForm(e: MouseEvent): void { e.preventDefault(); this.validateForm.reset(); for (const key in this.validateForm.controls) { this.validateForm.controls[key].markAsPristine(); this.validateForm.controls[key].updateValueAndValidity(); } this.validateForm.patchValue({ organization: this.nodes[0].id, }); this.PageNumber = 1 this.getConditionMonitoring() } listOfData: any num: string PageNumber: number = 1 isLoading = false //获取盒子状态 getConditionMonitoring() { let params = { ContainsChildren: true, OrganizationId: this.defaultOrId, PageNumber: this.PageNumber, PageSize: 10, HubConnectionState: this.validateForm.value.state } this.isLoading = true this.http.get('/api/EdgeDevices/Statuses', { params: params }).subscribe( (data: any) => { console.log(data) this.isLoading = false this.listOfData = data.items this.num = data.totalCount }, err => { } ) } pageChange($event) { this.PageNumber = $event this.getConditionMonitoring() } checked = false; loading indeterminate = false; listOfCurrentPageData: readonly any[] = []; setOfCheckedId = new Set(); updateCheckedSet(id: number, checked: boolean): void { if (checked) { this.setOfCheckedId.add(id); } else { this.setOfCheckedId.delete(id); } } onCurrentPageDataChange(listOfCurrentPageData: readonly any[]): void { this.listOfCurrentPageData = listOfCurrentPageData; this.refreshCheckedStatus(); } refreshCheckedStatus(): void { const listOfEnabledData = this.listOfCurrentPageData.filter(({ disabled }) => !disabled); this.checked = listOfEnabledData.every(({ id }) => this.setOfCheckedId.has(id)); this.indeterminate = listOfEnabledData.some(({ id }) => this.setOfCheckedId.has(id)) && !this.checked; } onItemChecked(id: number, checked: boolean): void { this.updateCheckedSet(id, checked); this.refreshCheckedStatus(); } onAllChecked(checked: boolean): void { this.listOfCurrentPageData .filter(({ disabled }) => !disabled) .forEach(({ id }) => this.updateCheckedSet(id, checked)); this.refreshCheckedStatus(); } sendRequest(type): void { const requestData = this.listOfData.filter(data => this.setOfCheckedId.has(data.id)); let strArr = [] requestData.forEach(element => { strArr.push(element.id) }); let body this.loading = type; body = { edgeDeviceIds: strArr } this.http.patch('/api/EdgeDevices/Commands', body, { params: { command: type } }).subscribe({ next: (data: any) => { this.message.create('success', '通知边缘盒子成功,请过一段时间手动刷新尝试!'); console.log(data) if (data.failedItems.length != 0) { data.failedItems.forEach(element => { this.message.create('info', element.detail); }); } this.setOfCheckedId.clear(); this.refreshCheckedStatus(); this.loading = null; this.getConditionMonitoring() }, error: (err) => { this.loading = null; } }) } executeTheScript() { const requestData = this.listOfData.filter(data => this.setOfCheckedId.has(data.id)); let strArr = [] requestData.forEach(element => { strArr.push(element.id) }); const modal = this.modal.create({ nzTitle: '选择执行脚本', nzContent: ScriptComponent, nzViewContainerRef: this.viewContainerRef, nzWidth: 288, nzComponentParams: {}, nzOnOk: async () => { if (instance.validateForm.valid) { let body = { edgeDeviceIds: strArr } await new Promise(resolve => { this.http.patch('/api/EdgeDevices/Commands/ExecuteScript', body, { params: { name: instance.validateForm.value.script } }).subscribe({ next: (data: any) => { resolve(data) this.message.create('success', '通知边缘盒子成功,请过一段时间手动刷新尝试!'); // if (data.failedItems && data.failedItems.length != 0) { // data.failedItems.forEach(element => { // this.message.create('info', element.detail); // }); // } this.setOfCheckedId.clear(); this.refreshCheckedStatus(); this.loading = null; this.getConditionMonitoring() return true }, error: (err) => { resolve(err) this.loading = null; return false } }) }) } else { this.message.create('warning', '请填写完整!'); return false } } }); const instance = modal.getContentComponent(); } }