|
|
|
@ -31,7 +31,7 @@ import { EntityId } from '@shared/models/id/entity-id'; |
|
|
|
import { AttributeService } from '@core/http/attribute.service'; |
|
|
|
import { TranslateService } from '@ngx-translate/core'; |
|
|
|
import { forkJoin, Observable, of, Subject, Subscription } from 'rxjs'; |
|
|
|
import { AttributeScope } from '@shared/models/telemetry/telemetry.models'; |
|
|
|
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'; |
|
|
|
@ -110,8 +110,10 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
activeConnectors: Array<string>; |
|
|
|
mode: ConfigurationModes = this.ConnectorConfigurationModes.BASIC; |
|
|
|
initialConnector: GatewayConnector; |
|
|
|
basicConfigInitSubject = new Subject<void>(); |
|
|
|
|
|
|
|
private gatewayVersion: string; |
|
|
|
private isGatewayActive: boolean; |
|
|
|
private inactiveConnectors: Array<string>; |
|
|
|
private attributeDataSource: AttributeDatasource; |
|
|
|
private inactiveConnectorsDataSource: AttributeDatasource; |
|
|
|
@ -124,7 +126,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
private subscriptionOptions: WidgetSubscriptionOptions = { |
|
|
|
callbacks: { |
|
|
|
onDataUpdated: () => this.ctx.ngZone.run(() => { |
|
|
|
this.onDataUpdated(); |
|
|
|
this.onErrorsUpdated(); |
|
|
|
}), |
|
|
|
onDataUpdateError: (_, e) => this.ctx.ngZone.run(() => { |
|
|
|
this.onDataUpdateError(e); |
|
|
|
@ -155,8 +157,10 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
ngAfterViewInit(): void { |
|
|
|
this.dataSource.sort = this.sort; |
|
|
|
this.dataSource.sortingDataAccessor = this.getSortingDataAccessor(); |
|
|
|
this.ctx.$scope.gatewayConnectors = this; |
|
|
|
|
|
|
|
this.loadConnectors(); |
|
|
|
this.loadGatewayState(); |
|
|
|
this.observeModeChange(); |
|
|
|
} |
|
|
|
|
|
|
|
@ -166,9 +170,9 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
super.ngOnDestroy(); |
|
|
|
} |
|
|
|
|
|
|
|
saveConnector(): void { |
|
|
|
saveConnector(isNew = true): void { |
|
|
|
const value = this.getConnectorData(); |
|
|
|
const scope = (!this.initialConnector || this.activeConnectors.includes(this.initialConnector.name)) |
|
|
|
const scope = (isNew || this.activeConnectors.includes(this.initialConnector.name)) |
|
|
|
? AttributeScope.SHARED_SCOPE |
|
|
|
: AttributeScope.SERVER_SCOPE; |
|
|
|
|
|
|
|
@ -275,7 +279,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
|
|
|
|
isConnectorSynced(attribute: GatewayAttributeData): boolean { |
|
|
|
const connectorData = attribute.value; |
|
|
|
if (!connectorData.ts || attribute.skipSync) { |
|
|
|
if (!connectorData.ts || attribute.skipSync || !this.isGatewayActive) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
const clientIndex = this.activeData.findIndex(data => { |
|
|
|
@ -479,7 +483,6 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
filter(Boolean), |
|
|
|
) |
|
|
|
.subscribe(value => { |
|
|
|
this.initialConnector = null; |
|
|
|
if (this.connectorForm.disabled) { |
|
|
|
this.connectorForm.enable(); |
|
|
|
} |
|
|
|
@ -487,9 +490,16 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
value.configurationJson = {} as ConnectorBaseConfig; |
|
|
|
} |
|
|
|
value.basicConfig = value.configurationJson; |
|
|
|
this.updateConnector(value); |
|
|
|
this.initialConnector = value; |
|
|
|
this.connectorForm.patchValue(value, {emitEvent: false}); |
|
|
|
this.generate('basicConfig.broker.clientId'); |
|
|
|
setTimeout(() => this.saveConnector()); |
|
|
|
if (this.connectorForm.get('type').value === value.type || !this.allowBasicConfig.has(value.type)) { |
|
|
|
this.saveConnector(); |
|
|
|
} else { |
|
|
|
this.basicConfigInitSubject.pipe(take(1)).subscribe(() => { |
|
|
|
this.saveConnector(); |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@ -590,6 +600,19 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private loadGatewayState(): void { |
|
|
|
this.attributeService.getEntityAttributes(this.device, AttributeScope.SERVER_SCOPE) |
|
|
|
.pipe(takeUntil(this.destroy$)) |
|
|
|
.subscribe((attributes: AttributeData[]) => { |
|
|
|
|
|
|
|
const active = attributes.find(data => data.key === 'active').value; |
|
|
|
const lastDisconnectedTime = attributes.find(data => data.key === 'lastDisconnectTime')?.value; |
|
|
|
const lastConnectedTime = attributes.find(data => data.key === 'lastConnectTime').value; |
|
|
|
|
|
|
|
this.isGatewayActive = this.getGatewayStatus(active, lastConnectedTime, lastDisconnectedTime); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private parseConnectors(attribute: GatewayAttributeData[]): string[] { |
|
|
|
const connectors = attribute?.[0]?.value || []; |
|
|
|
return isString(connectors) ? JSON.parse(connectors) : connectors; |
|
|
|
@ -598,7 +621,14 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
private observeModeChange(): void { |
|
|
|
this.connectorForm.get('mode').valueChanges |
|
|
|
.pipe(takeUntil(this.destroy$)) |
|
|
|
.subscribe(() => this.connectorForm.get('mode').markAsPristine()); |
|
|
|
.subscribe((mode) => { |
|
|
|
this.connectorForm.get('mode').markAsPristine(); |
|
|
|
if (mode === ConfigurationModes.BASIC) { |
|
|
|
this.basicConfigInitSubject.pipe(take(1)).subscribe(() => { |
|
|
|
this.patchBasicConfigConnector({...this.initialConnector, mode: ConfigurationModes.BASIC}); |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private observeAttributeChange(): void { |
|
|
|
@ -664,10 +694,29 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
console.error(errorText); |
|
|
|
} |
|
|
|
|
|
|
|
private onErrorsUpdated(): void { |
|
|
|
this.cd.detectChanges(); |
|
|
|
} |
|
|
|
|
|
|
|
private onDataUpdated(): void { |
|
|
|
const dataSources = this.ctx.defaultSubscription.data; |
|
|
|
|
|
|
|
const active = dataSources.find(data => data.dataKey.name === 'active').data[0][1]; |
|
|
|
const lastDisconnectedTime = dataSources.find(data => data.dataKey.name === 'lastDisconnectTime').data[0][1]; |
|
|
|
const lastConnectedTime = dataSources.find(data => data.dataKey.name === 'lastConnectTime').data[0][1]; |
|
|
|
|
|
|
|
this.isGatewayActive = this.getGatewayStatus(active, lastConnectedTime, lastDisconnectedTime); |
|
|
|
|
|
|
|
this.cd.detectChanges(); |
|
|
|
} |
|
|
|
|
|
|
|
private getGatewayStatus(active: boolean, lastConnectedTime: number, lastDisconnectedTime: number): boolean { |
|
|
|
if (!active) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
return !lastDisconnectedTime || lastConnectedTime > lastDisconnectedTime; |
|
|
|
} |
|
|
|
|
|
|
|
private generateSubscription(): void { |
|
|
|
if (this.subscription) { |
|
|
|
this.subscription.unsubscribe(); |
|
|
|
@ -760,13 +809,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
case ConnectorType.MQTT: |
|
|
|
case ConnectorType.OPCUA: |
|
|
|
case ConnectorType.MODBUS: |
|
|
|
this.connectorForm.get('mode').setValue(connector.mode || ConfigurationModes.BASIC, {emitEvent: false}); |
|
|
|
this.connectorForm.get('configVersion').setValue(connector.configVersion, {emitEvent: false}); |
|
|
|
setTimeout(() => { |
|
|
|
this.connectorForm.patchValue(connector, {emitEvent: false}); |
|
|
|
this.connectorForm.markAsPristine(); |
|
|
|
this.createBasicConfigWatcher(); |
|
|
|
}); |
|
|
|
this.updateBasicConfigConnector(connector); |
|
|
|
break; |
|
|
|
default: |
|
|
|
this.connectorForm.patchValue({...connector, mode: null}); |
|
|
|
@ -775,6 +818,24 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie |
|
|
|
this.createJsonConfigWatcher(); |
|
|
|
} |
|
|
|
|
|
|
|
private updateBasicConfigConnector(connector: GatewayConnector): void { |
|
|
|
this.connectorForm.get('mode').setValue(connector.mode || ConfigurationModes.BASIC, {emitEvent: false}); |
|
|
|
this.connectorForm.get('configVersion').setValue(connector.configVersion, {emitEvent: false}); |
|
|
|
if ((!connector.mode || connector.mode === ConfigurationModes.BASIC) && this.connectorForm.get('type').value !== connector.type) { |
|
|
|
this.basicConfigInitSubject.asObservable().pipe(take(1)).subscribe(() => { |
|
|
|
this.patchBasicConfigConnector(connector); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
this.patchBasicConfigConnector(connector); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private patchBasicConfigConnector(connector: GatewayConnector): void { |
|
|
|
this.connectorForm.patchValue(connector, {emitEvent: false}); |
|
|
|
this.connectorForm.markAsPristine(); |
|
|
|
this.createBasicConfigWatcher(); |
|
|
|
} |
|
|
|
|
|
|
|
private toggleReportStrategy(type: ConnectorType): void { |
|
|
|
const reportStrategyControl = this.connectorForm.get('reportStrategy'); |
|
|
|
if (type === ConnectorType.MODBUS) { |
|
|
|
|