Browse Source

json formating connector

pull/8337/head
Maksym Dudnik 3 years ago
parent
commit
880e1b32da
  1. 23
      ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-connectors.component.ts

23
ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-connectors.component.ts

@ -165,7 +165,6 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie
} else if (sortHeaderId === 'enabled') { } else if (sortHeaderId === 'enabled') {
return this.activeConnectors.includes(data.key) ? 1 : 0; return this.activeConnectors.includes(data.key) ? 1 : 0;
} }
console.log(data, sortHeaderId, data[sortHeaderId] || data.value[sortHeaderId]);
return data[sortHeaderId] || data.value[sortHeaderId]; return data[sortHeaderId] || data.value[sortHeaderId];
}; };
@ -193,7 +192,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie
return (c: UntypedFormControl) => { return (c: UntypedFormControl) => {
const newName = c.value.trim().toLowerCase(); const newName = c.value.trim().toLowerCase();
const found = this.dataSource.data.find((connectorAttr) => { const found = this.dataSource.data.find((connectorAttr) => {
const connectorData = typeof connectorAttr.value === 'string' ? JSON.parse(connectorAttr.value) : connectorAttr.value; const connectorData = connectorAttr.value;
return connectorData.name.toLowerCase() === newName; return connectorData.name.toLowerCase() === newName;
}); });
if (found) { if (found) {
@ -284,15 +283,15 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie
} }
isConnectorSynced(attribute: AttributeData) { isConnectorSynced(attribute: AttributeData) {
const connectorData = typeof attribute.value === 'string' ? JSON.parse(attribute.value) : attribute.value; const connectorData = attribute.value;
if (!connectorData.ts) return false; if (!connectorData.ts) return false;
const clientIndex = this.activeData.findIndex(data => { const clientIndex = this.activeData.findIndex(data => {
const sharedData = typeof data.value === 'string' ? JSON.parse(data.value) : data.value; const sharedData = data.value;
return sharedData.name === connectorData.name; return sharedData.name === connectorData.name;
}) })
if (clientIndex == -1) return false; if (clientIndex == -1) return false;
const sharedIndex = this.sharedAttributeData.findIndex(data => { const sharedIndex = this.sharedAttributeData.findIndex(data => {
const sharedData = typeof data.value === 'string' ? JSON.parse(data.value) : data.value; const sharedData = data.value;
return sharedData.name === connectorData.name && sharedData.ts && sharedData.ts <= connectorData.ts; return sharedData.name === connectorData.name && sharedData.ts && sharedData.ts <= connectorData.ts;
}) })
return sharedIndex !== -1; return sharedIndex !== -1;
@ -301,7 +300,10 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie
combineData() { combineData() {
this.dataSource.data = [...this.activeData, ...this.inactiveData, ...this.sharedAttributeData].filter((item, index, self) => this.dataSource.data = [...this.activeData, ...this.inactiveData, ...this.sharedAttributeData].filter((item, index, self) =>
index === self.findIndex((t) => t.key === item.key) index === self.findIndex((t) => t.key === item.key)
); ).map(attribute=>{
attribute.value = typeof attribute.value === 'string' ? JSON.parse(attribute.value) : attribute.value;
return attribute
});
} }
addAttribute(): void { addAttribute(): void {
@ -331,7 +333,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie
if (this.connectorForm.disabled) { if (this.connectorForm.disabled) {
this.connectorForm.enable(); this.connectorForm.enable();
} }
const connector = typeof attribute.value === 'string' ? JSON.parse(attribute.value) : attribute.value; const connector = attribute.value;
if (!connector.configuration) { if (!connector.configuration) {
connector.configuration = ''; connector.configuration = '';
} }
@ -345,7 +347,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie
isSameConnector(attribute): boolean { isSameConnector(attribute): boolean {
if (!this.initialConnector) return false; if (!this.initialConnector) return false;
const connector = typeof attribute.value === 'string' ? JSON.parse(attribute.value) : attribute.value; const connector = attribute.value;
return this.initialConnector.name === connector.name; return this.initialConnector.name === connector.name;
} }
@ -364,7 +366,7 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie
} }
returnType(attribute) { returnType(attribute) {
const value = typeof attribute.value === 'string' ? JSON.parse(attribute.value) : attribute.value; const value = attribute.value;
return this.gatewayConnectorDefaultTypes.get(value.type); return this.gatewayConnectorDefaultTypes.get(value.type);
} }
@ -411,7 +413,6 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie
if ($event) { if ($event) {
$event.stopPropagation(); $event.stopPropagation();
} }
attribute.value = typeof attribute.value === 'string' ? JSON.parse(attribute.value) : attribute.value;
const params = deepClone(this.ctx.stateController.getStateParams()); const params = deepClone(this.ctx.stateController.getStateParams());
params.connector_logs = attribute; params.connector_logs = attribute;
params.targetEntityParamName = 'connector_logs'; params.targetEntityParamName = 'connector_logs';
@ -422,7 +423,6 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie
if ($event) { if ($event) {
$event.stopPropagation(); $event.stopPropagation();
} }
attribute.value = typeof attribute.value === 'string' ? JSON.parse(attribute.value) : attribute.value;
const params = deepClone(this.ctx.stateController.getStateParams()); const params = deepClone(this.ctx.stateController.getStateParams());
params.connector_rpc = attribute; params.connector_rpc = attribute;
params.targetEntityParamName = 'connector_rpc'; params.targetEntityParamName = 'connector_rpc';
@ -434,7 +434,6 @@ export class GatewayConnectorComponent extends PageComponent implements AfterVie
const wasEnabled = this.activeConnectors.includes(attribute.key); const wasEnabled = this.activeConnectors.includes(attribute.key);
const scopeOld = wasEnabled ? AttributeScope.SHARED_SCOPE : AttributeScope.SERVER_SCOPE; const scopeOld = wasEnabled ? AttributeScope.SHARED_SCOPE : AttributeScope.SERVER_SCOPE;
const scopeNew = !wasEnabled ? AttributeScope.SHARED_SCOPE : AttributeScope.SERVER_SCOPE; const scopeNew = !wasEnabled ? AttributeScope.SHARED_SCOPE : AttributeScope.SERVER_SCOPE;
attribute.value = typeof attribute.value === 'string' ? JSON.parse(attribute.value) : attribute.value;
attribute.value.ts = new Date().getTime(); attribute.value.ts = new Date().getTime();
const tasks = [this.attributeService.saveEntityAttributes(this.device, AttributeScope.SHARED_SCOPE, [{ const tasks = [this.attributeService.saveEntityAttributes(this.device, AttributeScope.SHARED_SCOPE, [{
key: 'active_connectors', key: 'active_connectors',

Loading…
Cancel
Save