Skip to content

Commit

Permalink
Merge pull request #1389 from telefonicaid/task/entityNameExp_with_me…
Browse files Browse the repository at this point in the history
…asures

Evaluate group entityNameExp with a context including measures
  • Loading branch information
fgalan authored Jul 3, 2023
2 parents 31c8154 + 7c839d4 commit 2e9ad0d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Add: Evaluate group entityNameExp with a context including measures (#1334)
- Remove: expressionLanguage field (as it it not longer needed, due to only one expression language is supported, after the removal of legacy expressions in 3.2.0) (#1384)
- Add: allow update timestamp and other config fields of device
- Fix: use 'options=upsert' when update ngsiv2 CB entities and appendMode is enabled (#956)
Expand Down
12 changes: 7 additions & 5 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,10 @@ Some transformation plugins also allow the use of the following optional fields:
an expression based on a combination of the reported values. See the
[Expression Language definition](#expression-language-support) for details
- **skipValue**: indicates that if the result of applying `expression` to a measure is equal to the value of
`skipValue` then the attribute corresponding to the measure is not sent to CB. In other words, this field **is
not an expression**, it is a value that is compared with the result of applying `expression` to a measure. By
default if `skipValue` is not defined then is considered as `null` (i.e. if the result of apply `expression` results
in `null` then corresponding attribute is not sent to CB). It is only used if `expression` is provided (otherwise is
ignored).
`skipValue` then the attribute corresponding to the measure is not sent to CB. In other words, this field **is not
an expression**, it is a value that is compared with the result of applying `expression` to a measure. By default if
`skipValue` is not defined then is considered as `null` (i.e. if the result of apply `expression` results in `null`
then corresponding attribute is not sent to CB). It is only used if `expression` is provided (otherwise is ignored).
- **entity_name**: the presence of this attribute indicates that the value will not be stored in the original device
entity but in a new entity with an ID given by this attribute. The type of this additional entity can be configured
with the `entity_type` attribute. If no type is configured, the device entity type is used instead. Entity names can
Expand Down Expand Up @@ -521,6 +520,9 @@ expression. In all cases the following data is available to all expressions:
- `subservice`: device subservice
- `staticAttributes`: static attributes defined in the device or config group

Additionally, for attribute expressions (`expression`, `entity_name`) and `entityNameExp` measures are avaiable in the
**context** used to evaluate them.

### Examples of JEXL expressions

The following table shows expressions and their expected outcomes taking into account the following measures at
Expand Down
3 changes: 3 additions & 0 deletions lib/services/devices/deviceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ function mergeDeviceWithConfiguration(fields, defaults, deviceData, configuratio
if (configuration && configuration.explicitAttrs !== undefined && deviceData.explicitAttrs === undefined) {
deviceData.explicitAttrs = configuration.explicitAttrs;
}
if (configuration && configuration.entityNameExp !== undefined) {
deviceData.entityNameExp = configuration.entityNameExp;
}

logger.debug(context, 'deviceData after merge with conf: %j', deviceData);
callback(null, deviceData);
Expand Down
17 changes: 17 additions & 0 deletions lib/services/ngsi/entities-NGSI-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,23 @@ function sendUpdateValueNgsi2(entityName, attributes, typeInformation, token, ca
}
}

// Evaluate entityNameExp with a context including measures
if (typeInformation.entityNameExp !== undefined && typeInformation.entityNameExp !== '') {
try {
logger.debug(context, 'sendUpdateValueNgsi2 entityNameExp %j ', typeInformation.entityNameExp);
entityName = expressionPlugin.applyExpression(typeInformation.entityNameExp, ctxt, typeInformation);
payload.entities[0].id = entityName;
ctxt['entity_name'] = entityName;
} catch (e) {
logger.debug(
context,
'Error evaluating expression for entityName: %s with context: %s',
typeInformation.entityNameExp,
ctxt
);
}
}

logger.debug(context, 'sendUpdateValueNgsi2 currentEntity sorted %j ', currentEntity);
let timestampValue = undefined;
// Loop for each final attribute to apply alias, multientity and expressions
Expand Down

0 comments on commit 2e9ad0d

Please sign in to comment.