|
|
|
@ -24,14 +24,13 @@ import { MatDialog } from '@angular/material/dialog'; |
|
|
|
import { AttributeService } from '@core/http/attribute.service'; |
|
|
|
import { DeviceService } from '@core/http/device.service'; |
|
|
|
import { TranslateService } from '@ngx-translate/core'; |
|
|
|
import { forkJoin, merge } from 'rxjs'; |
|
|
|
import { forkJoin } from 'rxjs'; |
|
|
|
import { AttributeData, AttributeScope } from '@shared/models/telemetry/telemetry.models'; |
|
|
|
import { PageComponent } from '@shared/components/page.component'; |
|
|
|
import { PageLink } from '@shared/models/page/page-link'; |
|
|
|
import { AttributeDatasource } from '@home/models/datasource/attribute-datasource'; |
|
|
|
import { Direction, SortOrder } from '@shared/models/page/sort-order'; |
|
|
|
import { MatSort } from '@angular/material/sort'; |
|
|
|
import { tap } from 'rxjs/operators'; |
|
|
|
import { TelemetryWebsocketService } from '@core/ws/telemetry-websocket.service'; |
|
|
|
import { MatTableDataSource } from '@angular/material/table'; |
|
|
|
import { GatewayLogLevel } from '@home/components/widget/lib/gateway/gateway-configuration.component'; |
|
|
|
@ -40,6 +39,7 @@ import { DialogService } from '@core/services/dialog.service'; |
|
|
|
import { WidgetContext } from '@home/models/widget-component.models'; |
|
|
|
import { deepClone } from '@core/utils'; |
|
|
|
import { NULL_UUID } from '@shared/models/id/has-uuid'; |
|
|
|
import { tap } from 'rxjs/operators'; |
|
|
|
|
|
|
|
|
|
|
|
export interface gatewayConnector { |
|
|
|
@ -83,10 +83,10 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
|
|
|
|
attributeDataSource: AttributeDatasource; |
|
|
|
|
|
|
|
sharedAttributeData: Array<AttributeData> = []; |
|
|
|
|
|
|
|
inactiveConnectorsDataSource: AttributeDatasource; |
|
|
|
|
|
|
|
serverDataSource: AttributeDatasource; |
|
|
|
|
|
|
|
dataSource: MatTableDataSource<AttributeData>; |
|
|
|
|
|
|
|
displayedColumns = ['enabled', 'key', 'type', 'syncStatus', 'actions']; |
|
|
|
@ -100,7 +100,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
device: EntityId; |
|
|
|
|
|
|
|
@ViewChild('nameInput') nameInput: ElementRef; |
|
|
|
@ViewChild(MatSort, { static: false }) sort: MatSort; |
|
|
|
@ViewChild(MatSort, {static: false}) sort: MatSort; |
|
|
|
|
|
|
|
connectorForm: FormGroup; |
|
|
|
|
|
|
|
@ -120,6 +120,8 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
|
|
|
|
inactiveData: Array<any> = []; |
|
|
|
|
|
|
|
sharedAttributeData: Array<AttributeData> = []; |
|
|
|
|
|
|
|
initialConnector: gatewayConnector; |
|
|
|
|
|
|
|
constructor(protected router: Router, |
|
|
|
@ -138,6 +140,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
this.pageLink = new PageLink(1000, 0, null, sortOrder); |
|
|
|
this.attributeDataSource = new AttributeDatasource(this.attributeService, this.telemetryWsService, this.zone, this.translate); |
|
|
|
this.inactiveConnectorsDataSource = new AttributeDatasource(this.attributeService, this.telemetryWsService, this.zone, this.translate); |
|
|
|
this.serverDataSource = new AttributeDatasource(this.attributeService, this.telemetryWsService, this.zone, this.translate); |
|
|
|
this.dataSource = new MatTableDataSource<AttributeData>([]); |
|
|
|
this.connectorForm = this.fb.group({ |
|
|
|
name: ['', [Validators.required, this.uniqNameRequired()]], |
|
|
|
@ -155,11 +158,16 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
this.connectorForm.valueChanges.subscribe(() => { |
|
|
|
this.cd.detectChanges(); |
|
|
|
}); |
|
|
|
merge(this.sort.sortChange) |
|
|
|
.pipe( |
|
|
|
tap(() => this.updateData()) |
|
|
|
) |
|
|
|
.subscribe(); |
|
|
|
|
|
|
|
this.dataSource.sort = this.sort; |
|
|
|
this.dataSource.sortingDataAccessor = (data: AttributeData, sortHeaderId: string) => { |
|
|
|
if (sortHeaderId === 'syncStatus') { |
|
|
|
return this.isConnectorSynced(data) ? 1 : 0; |
|
|
|
} else if (sortHeaderId === 'enabled') { |
|
|
|
return this.activeConnectors.includes(data.key) ? 1 : 0; |
|
|
|
} |
|
|
|
return data[sortHeaderId]; |
|
|
|
}; |
|
|
|
|
|
|
|
this.viewsInited = true; |
|
|
|
if (this.device) { |
|
|
|
@ -239,7 +247,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
this.inactiveConnectors.push(value.name); |
|
|
|
updateActiveConnectors = true; |
|
|
|
} |
|
|
|
const tasks = [this.attributeService.saveEntityAttributes(this.device, AttributeScope.SHARED_SCOPE, attributesToSave)]; |
|
|
|
const tasks = [this.attributeService.saveEntityAttributes(this.device, scope, attributesToSave)]; |
|
|
|
if (updateActiveConnectors) { |
|
|
|
tasks.push(this.attributeService.saveEntityAttributes(this.device, scope, [{ |
|
|
|
key: scope == AttributeScope.SHARED_SCOPE ? 'active_connectors' : 'inactive_connectors', |
|
|
|
@ -248,7 +256,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
} |
|
|
|
|
|
|
|
if (attributesToDelete.length) { |
|
|
|
tasks.push(this.attributeService.deleteEntityAttributes(this.device, AttributeScope.SHARED_SCOPE, attributesToDelete)); |
|
|
|
tasks.push(this.attributeService.deleteEntityAttributes(this.device, scope, attributesToDelete)); |
|
|
|
} |
|
|
|
forkJoin(tasks).subscribe(_ => { |
|
|
|
this.initialConnector = value; |
|
|
|
@ -257,19 +265,6 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
resetSortAndFilter(update: boolean = true) { |
|
|
|
this.textSearchMode = false; |
|
|
|
this.pageLink.textSearch = null; |
|
|
|
if (this.viewsInited) { |
|
|
|
const sortable = this.sort.sortables.get('key'); |
|
|
|
this.sort.active = sortable.id; |
|
|
|
this.sort.direction = 'asc'; |
|
|
|
if (update) { |
|
|
|
this.updateData(true); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
updateData(reload: boolean = false) { |
|
|
|
this.pageLink.sortOrder.property = this.sort.active; |
|
|
|
this.pageLink.sortOrder.direction = Direction[this.sort.direction.toUpperCase()]; |
|
|
|
@ -278,10 +273,14 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
this.combineData(); |
|
|
|
}); |
|
|
|
this.inactiveConnectorsDataSource.loadAttributes(this.device, AttributeScope.SHARED_SCOPE, this.pageLink, reload).subscribe(data => { |
|
|
|
this.inactiveData = data.data.filter(value => this.inactiveConnectors.includes(value.key)); |
|
|
|
this.sharedAttributeData = data.data.filter(value => this.activeConnectors.includes(value.key)); |
|
|
|
this.combineData(); |
|
|
|
}); |
|
|
|
this.serverDataSource.loadAttributes(this.device, AttributeScope.SERVER_SCOPE, this.pageLink, reload).subscribe(data => { |
|
|
|
this.inactiveData = data.data.filter(value => this.inactiveConnectors.includes(value.key)); |
|
|
|
this.combineData(); |
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
isConnectorSynced(attribute: AttributeData) { |
|
|
|
@ -300,7 +299,9 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
} |
|
|
|
|
|
|
|
combineData() { |
|
|
|
this.dataSource.data = [...this.activeData, ...this.inactiveData]; |
|
|
|
this.dataSource.data = [...this.activeData, ...this.inactiveData, ...this.sharedAttributeData].filter((item, index, self) => |
|
|
|
index === self.findIndex((t) => t.key === item.key) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
addAttribute(): void { |
|
|
|
@ -377,7 +378,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
if (result) { |
|
|
|
const tasks = []; |
|
|
|
const scope = (this.initialConnector && this.activeConnectors.includes(this.initialConnector.name)) ? AttributeScope.SHARED_SCOPE : AttributeScope.SERVER_SCOPE; |
|
|
|
tasks.push(this.attributeService.deleteEntityAttributes(this.device, AttributeScope.SHARED_SCOPE, [attribute])); |
|
|
|
tasks.push(this.attributeService.deleteEntityAttributes(this.device, scope, [attribute])); |
|
|
|
const activeIndex = this.activeConnectors.indexOf(attribute.key); |
|
|
|
const inactiveIndex = this.inactiveConnectors.indexOf(attribute.key); |
|
|
|
if (activeIndex !== -1) { |
|
|
|
@ -431,6 +432,16 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
|
|
|
|
enableConnector(attribute): void { |
|
|
|
const wasEnabled = this.activeConnectors.includes(attribute.key); |
|
|
|
const scopeOld = wasEnabled ? AttributeScope.SHARED_SCOPE : AttributeScope.SERVER_SCOPE; |
|
|
|
const scopeNew = !wasEnabled ? AttributeScope.SHARED_SCOPE : AttributeScope.SERVER_SCOPE; |
|
|
|
const tasks = [this.attributeService.saveEntityAttributes(this.device, AttributeScope.SHARED_SCOPE, [{ |
|
|
|
key: 'active_connectors', |
|
|
|
value: this.activeConnectors |
|
|
|
}]), this.attributeService.saveEntityAttributes(this.device, AttributeScope.SERVER_SCOPE, [{ |
|
|
|
key: 'inactive_connectors', |
|
|
|
value: this.inactiveConnectors |
|
|
|
}]), this.attributeService.deleteEntityAttributes(this.device, scopeOld, [attribute]), |
|
|
|
this.attributeService.saveEntityAttributes(this.device, scopeNew, [attribute])]; |
|
|
|
if (wasEnabled) { |
|
|
|
const index = this.activeConnectors.indexOf(attribute.key); |
|
|
|
this.activeConnectors.splice(index, 1); |
|
|
|
@ -440,14 +451,8 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
this.inactiveConnectors.splice(index, 1); |
|
|
|
this.activeConnectors.push(attribute.key); |
|
|
|
} |
|
|
|
forkJoin([this.attributeService.saveEntityAttributes(this.device, AttributeScope.SHARED_SCOPE, [{ |
|
|
|
key: 'active_connectors', |
|
|
|
value: this.activeConnectors |
|
|
|
}]), this.attributeService.saveEntityAttributes(this.device, AttributeScope.SERVER_SCOPE, [{ |
|
|
|
key: 'inactive_connectors', |
|
|
|
value: this.inactiveConnectors |
|
|
|
}]),]).subscribe(_ => { |
|
|
|
this.updateData(); |
|
|
|
forkJoin(tasks).subscribe(_ => { |
|
|
|
this.updateData(true); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|