|
|
|
@ -21,16 +21,11 @@ import { Store } from '@ngrx/store'; |
|
|
|
import { AppState } from '@app/core/core.state'; |
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion'; |
|
|
|
import { |
|
|
|
ATTR, |
|
|
|
INSTANCES, RESOURCES, OBSERVE_ATTR_TELEMETRY, OBSERVE, ATTRIBUTE, TELEMETRY, KEY_NAME, |
|
|
|
getDefaultProfileConfig, |
|
|
|
Instance, |
|
|
|
KEY_NAME, |
|
|
|
ObjectLwM2M, |
|
|
|
OBSERVE, |
|
|
|
OBSERVE_ATTR, |
|
|
|
ProfileConfigModels, |
|
|
|
ResourceLwM2M, |
|
|
|
TELEMETRY |
|
|
|
ModelValue |
|
|
|
} from './profile-config.models'; |
|
|
|
import { DeviceProfileService } from '@core/http/device-profile.service'; |
|
|
|
import { deepClone, isDefinedAndNotNull, isUndefined } from '@core/utils'; |
|
|
|
@ -55,11 +50,6 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro |
|
|
|
|
|
|
|
lwm2mDeviceProfileFormGroup: FormGroup; |
|
|
|
lwm2mDeviceConfigFormGroup: FormGroup; |
|
|
|
observeAttr = OBSERVE_ATTR; |
|
|
|
observe = OBSERVE; |
|
|
|
attribute = ATTR; |
|
|
|
telemetry = TELEMETRY; |
|
|
|
keyName = KEY_NAME; |
|
|
|
bootstrapServers: string; |
|
|
|
bootstrapServer: string; |
|
|
|
lwm2mServer: string; |
|
|
|
@ -122,7 +112,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
writeValue(value: any | null): void { |
|
|
|
writeValue(value: ProfileConfigModels | null): void { |
|
|
|
this.configurationValue = (Object.keys(value).length === 0) ? getDefaultProfileConfig() : value; |
|
|
|
this.lwm2mDeviceConfigFormGroup.patchValue({ |
|
|
|
configurationJson: this.configurationValue |
|
|
|
@ -131,7 +121,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro |
|
|
|
} |
|
|
|
|
|
|
|
private initWriteValue = (): void => { |
|
|
|
const modelValue = {objectIds: null, objectsList: []}; |
|
|
|
const modelValue = {objectIds: null, objectsList: []} as ModelValue; |
|
|
|
modelValue.objectIds = this.getObjectsFromJsonAllConfig(); |
|
|
|
if (modelValue.objectIds !== null) { |
|
|
|
const sortOrder = { |
|
|
|
@ -149,11 +139,10 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private updateWriteValue = (value: any): void => { |
|
|
|
const objectsList = value.objectsList; |
|
|
|
private updateWriteValue = (value: ModelValue): void => { |
|
|
|
this.lwm2mDeviceProfileFormGroup.patchValue({ |
|
|
|
objectIds: value, |
|
|
|
observeAttrTelemetry: this.getObserveAttrTelemetryObjects(objectsList), |
|
|
|
observeAttrTelemetry: this.getObserveAttrTelemetryObjects(value['objectsList']), |
|
|
|
shortId: this.configurationValue.bootstrap.servers.shortId, |
|
|
|
lifetime: this.configurationValue.bootstrap.servers.lifetime, |
|
|
|
defaultMinPeriod: this.configurationValue.bootstrap.servers.defaultMinPeriod, |
|
|
|
@ -199,22 +188,22 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro |
|
|
|
|
|
|
|
private getObserveAttrTelemetryObjects = (listObject: ObjectLwM2M[]): object => { |
|
|
|
const clientObserveAttrTelemetry = listObject; |
|
|
|
if (this.configurationValue[this.observeAttr]) { |
|
|
|
const observeArray = this.configurationValue[this.observeAttr][this.observe]; |
|
|
|
const attributeArray = this.configurationValue[this.observeAttr][this.attribute]; |
|
|
|
const telemetryArray = this.configurationValue[this.observeAttr][this.telemetry]; |
|
|
|
const keyNameJson = this.configurationValue[this.observeAttr][this.keyName]; |
|
|
|
if (this.configurationValue.observeAttr) { |
|
|
|
const observeArray = this.configurationValue.observeAttr.observe; |
|
|
|
const attributeArray = this.configurationValue.observeAttr.attribute; |
|
|
|
const telemetryArray = this.configurationValue.observeAttr.telemetry; |
|
|
|
const keyNameJson = this.configurationValue.observeAttr.keyName; |
|
|
|
if (this.includesNotZeroInstance(attributeArray, telemetryArray)) { |
|
|
|
this.addInstances(attributeArray, telemetryArray, clientObserveAttrTelemetry); |
|
|
|
} |
|
|
|
if (isDefinedAndNotNull(observeArray)) { |
|
|
|
this.updateObserveAttrTelemetryObjects(observeArray, clientObserveAttrTelemetry, 'observe'); |
|
|
|
this.updateObserveAttrTelemetryObjects(observeArray, clientObserveAttrTelemetry, OBSERVE); |
|
|
|
} |
|
|
|
if (isDefinedAndNotNull(attributeArray)) { |
|
|
|
this.updateObserveAttrTelemetryObjects(attributeArray, clientObserveAttrTelemetry, 'attribute'); |
|
|
|
this.updateObserveAttrTelemetryObjects(attributeArray, clientObserveAttrTelemetry, ATTRIBUTE); |
|
|
|
} |
|
|
|
if (isDefinedAndNotNull(telemetryArray)) { |
|
|
|
this.updateObserveAttrTelemetryObjects(telemetryArray, clientObserveAttrTelemetry, 'telemetry'); |
|
|
|
this.updateObserveAttrTelemetryObjects(telemetryArray, clientObserveAttrTelemetry, TELEMETRY); |
|
|
|
} |
|
|
|
if (isDefinedAndNotNull(keyNameJson)) { |
|
|
|
this.updateKeyNameObjects(keyNameJson, clientObserveAttrTelemetry); |
|
|
|
@ -273,74 +262,51 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro |
|
|
|
const telemetryArray: Array<string> = []; |
|
|
|
const keyNameNew = {}; |
|
|
|
const observeJson: ObjectLwM2M[] = JSON.parse(JSON.stringify(val)); |
|
|
|
const paths = new Set<string>(); |
|
|
|
let pathObj; |
|
|
|
let pathInst; |
|
|
|
let pathRes; |
|
|
|
observeJson.forEach(obj => { |
|
|
|
for (const [key, value] of Object.entries(obj)) { |
|
|
|
if (key === 'id') { |
|
|
|
pathObj = value; |
|
|
|
} |
|
|
|
if (key === 'instances') { |
|
|
|
const instancesJson = value as Instance[]; |
|
|
|
if (instancesJson.length > 0) { |
|
|
|
instancesJson.forEach(instance => { |
|
|
|
for (const [instanceKey, instanceValue] of Object.entries(instance)) { |
|
|
|
if (instanceKey === 'id') { |
|
|
|
pathInst = instanceValue; |
|
|
|
if (obj.hasOwnProperty(INSTANCES) && Array.isArray(obj.instances)) { |
|
|
|
obj.instances.forEach(instance => { |
|
|
|
if (instance.hasOwnProperty(RESOURCES) && Array.isArray(instance.resources)) { |
|
|
|
instance.resources.forEach(resource => { |
|
|
|
if (resource.attribute || resource.telemetry) { |
|
|
|
let pathRes = `/${obj.id}/${instance.id}/${resource.id}`; |
|
|
|
if (resource.observe) { |
|
|
|
observeArray.push(pathRes); |
|
|
|
} |
|
|
|
if (resource.attribute) { |
|
|
|
attributeArray.push(pathRes); |
|
|
|
} |
|
|
|
if (instanceKey === 'resources') { |
|
|
|
const resourcesJson = instanceValue as ResourceLwM2M[]; |
|
|
|
if (resourcesJson.length > 0) { |
|
|
|
resourcesJson.forEach(res => { |
|
|
|
for (const [resourceKey, value] of Object.entries(res)) { |
|
|
|
if (resourceKey === 'id') { |
|
|
|
pathRes = `/${pathObj}/${pathInst}/${value}`; |
|
|
|
} else if (resourceKey === 'observe' && value) { |
|
|
|
observeArray.push(pathRes); |
|
|
|
} else if (resourceKey === 'attribute' && value) { |
|
|
|
attributeArray.push(pathRes); |
|
|
|
paths.add(pathRes); |
|
|
|
} else if (resourceKey === 'telemetry' && value) { |
|
|
|
telemetryArray.push(pathRes); |
|
|
|
paths.add(pathRes); |
|
|
|
} |
|
|
|
else if (resourceKey === this.keyName && paths.has(pathRes)) { |
|
|
|
console.warn(pathRes, value); |
|
|
|
keyNameNew[pathRes] = value; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
if (resource.telemetry) { |
|
|
|
telemetryArray.push(pathRes); |
|
|
|
} |
|
|
|
keyNameNew[pathRes] = resource.keyName; |
|
|
|
} |
|
|
|
}); |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
}); |
|
|
|
if (isUndefined(this.configurationValue[this.observeAttr])) { |
|
|
|
this.configurationValue[this.observeAttr] = { |
|
|
|
[this.observe]: observeArray, |
|
|
|
[this.attribute]: attributeArray, |
|
|
|
[this.telemetry]: telemetryArray |
|
|
|
if (isUndefined(this.configurationValue.observeAttr)) { |
|
|
|
this.configurationValue.observeAttr = { |
|
|
|
observe: observeArray, |
|
|
|
attribute: attributeArray, |
|
|
|
telemetry: telemetryArray, |
|
|
|
keyName: this.sortObjectKeyPathJson(KEY_NAME, keyNameNew) |
|
|
|
}; |
|
|
|
} else { |
|
|
|
this.configurationValue[this.observeAttr][this.observe] = observeArray; |
|
|
|
this.configurationValue[this.observeAttr][this.attribute] = attributeArray; |
|
|
|
this.configurationValue[this.observeAttr][this.telemetry] = telemetryArray; |
|
|
|
this.configurationValue.observeAttr.observe = observeArray; |
|
|
|
this.configurationValue.observeAttr.attribute = attributeArray; |
|
|
|
this.configurationValue.observeAttr.telemetry = telemetryArray; |
|
|
|
this.configurationValue.observeAttr.keyName = this.sortObjectKeyPathJson(KEY_NAME, keyNameNew); |
|
|
|
} |
|
|
|
this.configurationValue[this.observeAttr][this.keyName] = this.sortObjectKeyPathJson('keyName', keyNameNew); |
|
|
|
} |
|
|
|
|
|
|
|
sortObjectKeyPathJson = (key: string, value: object): object => { |
|
|
|
if (key === 'keyName') { |
|
|
|
if (key === KEY_NAME) { |
|
|
|
return Object.keys(value).sort(this.sortPath).reduce((obj, keySort) => { |
|
|
|
obj[keySort] = value[keySort]; |
|
|
|
return obj; |
|
|
|
}, {}); |
|
|
|
} else if (key === 'observe' || key === 'attribute' || key === 'telemetry') { |
|
|
|
} else if (key === OBSERVE || key === ATTRIBUTE || key === TELEMETRY) { |
|
|
|
return Object.values(value).sort(this.sortPath).reduce((arr, arrValue) => { |
|
|
|
arr.push(arrValue); |
|
|
|
return arr; |
|
|
|
@ -359,19 +325,19 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro |
|
|
|
|
|
|
|
private getObjectsFromJsonAllConfig = (): number[] => { |
|
|
|
const objectsIds = new Set<number>(); |
|
|
|
if (this.configurationValue[this.observeAttr]) { |
|
|
|
if (this.configurationValue[this.observeAttr][this.observe]) { |
|
|
|
this.configurationValue[this.observeAttr][this.observe].forEach(obj => { |
|
|
|
if (this.configurationValue.observeAttr) { |
|
|
|
if (this.configurationValue.observeAttr.observe) { |
|
|
|
this.configurationValue.observeAttr.observe.forEach(obj => { |
|
|
|
objectsIds.add(Array.from(obj.substring(1).split('/'), Number)[0]); |
|
|
|
}); |
|
|
|
} |
|
|
|
if (this.configurationValue[this.observeAttr][this.attribute]) { |
|
|
|
this.configurationValue[this.observeAttr][this.attribute].forEach(obj => { |
|
|
|
if (this.configurationValue.observeAttr.attribute) { |
|
|
|
this.configurationValue.observeAttr.attribute.forEach(obj => { |
|
|
|
objectsIds.add(Array.from(obj.substring(1).split('/'), Number)[0]); |
|
|
|
}); |
|
|
|
} |
|
|
|
if (this.configurationValue[this.observeAttr][this.telemetry]) { |
|
|
|
this.configurationValue[this.observeAttr][this.telemetry].forEach(obj => { |
|
|
|
if (this.configurationValue.observeAttr.telemetry) { |
|
|
|
this.configurationValue.observeAttr.telemetry.forEach(obj => { |
|
|
|
objectsIds.add(Array.from(obj.substring(1).split('/'), Number)[0]); |
|
|
|
}); |
|
|
|
} |
|
|
|
@ -390,15 +356,15 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro |
|
|
|
} |
|
|
|
|
|
|
|
removeObjectsList = (value: ObjectLwM2M): void => { |
|
|
|
const objectsOld = this.lwm2mDeviceProfileFormGroup.get('observeAttrTelemetry').value.clientLwM2M; |
|
|
|
const objectsOld = this.lwm2mDeviceProfileFormGroup.get(OBSERVE_ATTR_TELEMETRY).value.clientLwM2M; |
|
|
|
const isIdIndex = (element) => element.id === value.id; |
|
|
|
const index = objectsOld.findIndex(isIdIndex); |
|
|
|
if (index >= 0) { |
|
|
|
objectsOld.splice(index, 1); |
|
|
|
} |
|
|
|
this.removeObserveAttrTelemetryFromJson(this.observe, value.id); |
|
|
|
this.removeObserveAttrTelemetryFromJson(this.telemetry, value.id); |
|
|
|
this.removeObserveAttrTelemetryFromJson(this.attribute, value.id); |
|
|
|
this.removeObserveAttrTelemetryFromJson(OBSERVE, value.id); |
|
|
|
this.removeObserveAttrTelemetryFromJson(TELEMETRY, value.id); |
|
|
|
this.removeObserveAttrTelemetryFromJson(ATTRIBUTE, value.id); |
|
|
|
this.removeKeyNameFromJson(value.id); |
|
|
|
this.updateObserveAttrTelemetryObjectFormGroup(objectsOld); |
|
|
|
this.upDateJsonAllConfig(); |
|
|
|
@ -406,15 +372,15 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro |
|
|
|
|
|
|
|
private removeObserveAttrTelemetryFromJson = (observeAttrTel: string, id: number): void => { |
|
|
|
const isIdIndex = (element) => element.startsWith(`/${id}`); |
|
|
|
let index = this.configurationValue[this.observeAttr][observeAttrTel].findIndex(isIdIndex); |
|
|
|
let index = this.configurationValue.observeAttr[observeAttrTel].findIndex(isIdIndex); |
|
|
|
while (index >= 0) { |
|
|
|
this.configurationValue[this.observeAttr][observeAttrTel].splice(index, 1); |
|
|
|
index = this.configurationValue[this.observeAttr][observeAttrTel].findIndex(isIdIndex, index); |
|
|
|
this.configurationValue.observeAttr[observeAttrTel].splice(index, 1); |
|
|
|
index = this.configurationValue.observeAttr[observeAttrTel].findIndex(isIdIndex, index); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private removeKeyNameFromJson = (id: number): void => { |
|
|
|
const keyNameJson = this.configurationValue[this.observeAttr][this.keyName]; |
|
|
|
const keyNameJson = this.configurationValue.observeAttr.keyName; |
|
|
|
Object.keys(keyNameJson).forEach(key => { |
|
|
|
if (key.startsWith(`/${id}`)) { |
|
|
|
delete keyNameJson[key]; |
|
|
|
@ -423,17 +389,17 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro |
|
|
|
} |
|
|
|
|
|
|
|
isPathInJson(path: string): boolean { |
|
|
|
let isPath = this.findPathInJson(path, this.attribute); |
|
|
|
let isPath = this.findPathInJson(path, ATTRIBUTE); |
|
|
|
if (!isPath) { |
|
|
|
isPath = this.findPathInJson(path, this.telemetry); |
|
|
|
isPath = this.findPathInJson(path, TELEMETRY); |
|
|
|
} |
|
|
|
return !!isPath; |
|
|
|
} |
|
|
|
|
|
|
|
private findPathInJson = (path: string, side: string): string => { |
|
|
|
if (this.configurationValue[this.observeAttr]) { |
|
|
|
if (this.configurationValue[this.observeAttr][side]) { |
|
|
|
return this.configurationValue[this.observeAttr][side].find( |
|
|
|
if (this.configurationValue.observeAttr) { |
|
|
|
if (this.configurationValue.observeAttr[side]) { |
|
|
|
return this.configurationValue.bootstrap[side].find( |
|
|
|
pathJs => pathJs === path); |
|
|
|
} |
|
|
|
} |
|
|
|
|