Skip to content

Commit a8456b2

Browse files
committed
Re-add functionality under a flag
1 parent 6a8fa69 commit a8456b2

File tree

9 files changed

+144
-43
lines changed

9 files changed

+144
-43
lines changed

CHANGES_NEXT_RELEASE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- Add: add getTypeSilently for device group and use in device registration to avoid false mongo alarm
22
- Fix: group command is not provisioned in device when entity_type is different (#1011)
3-
- Hotfix: avoid automatic conversion from geo:xxxx ('xxxx' diferent from 'json') to geo:json (reverts the work done in PR #854)
3+
- Hotfix: avoid automatic conversion from geo:xxxx ('xxxx' diferent from 'json') to geo:json (reverts some of the work done in PR #854)
44
- Add: use getDeviceSilently in checkDuplicates to avoid raise a false mongo alarm.
55
- Add: expose getConfigurationSilently to enable retrieve a configuration without raise a false mongo alarm (#1007)
66
- Add: db uri and options in mongo connection log INFO trace

doc/advanced-topics.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ Agent.
4848

4949
### GeoJSON support
5050

51-
The defined `type` of any GeoJSON attribute can be any set to any of the standard NGSI-v2 GeoJSON types - (e.g.
52-
`geo:json`, `geo:point`). NGSI-LD formats such as `GeoProperty`, `Point` and `LineString` are also accepted `type`
51+
The defined `type` of any GeoJSON attribute can be any set to any of the standard **NGSI-v2** GeoJSON types - (e.g.
52+
`geo:json`, `geo:point`). **NGSI-LD** formats such as `GeoProperty`, `Point` and `LineString` are also accepted `type`
5353
values. If the latitude and longitude are received as separate measures, the
5454
[expression language](expressionLanguage.md) can be used to concatenate them.
5555

@@ -69,7 +69,6 @@ values. If the latitude and longitude are received as separate measures, the
6969
}
7070
```
7171

72-
7372
For `attributes` and `static_attributes` which need to be formatted as GeoJSON values, three separate input
7473
formats are accepted. Provided the `type` is provisioned correctly, the `value` may be defined using any of
7574
the following formats:
@@ -81,7 +80,7 @@ the following formats:
8180
"name": "location",
8281
"value": "23, 12.5"
8382
}
84-
````
83+
```
8584

8685
- an array of numbers
8786

@@ -104,6 +103,18 @@ the following formats:
104103
}
105104
```
106105

106+
Because GeoJSON types (e.g. `Point`, `LineString` etc.) are native types in **NGSI-LD**, automatic GeoJSON conversion is switched on for NGSI-LD by default.
107+
108+
With **NGSI-v2**, for backwards compatibility reasons, automatic GeoJSON conversion for types other than `geo:json` is turned off by default.
109+
Add the `config.autocastGeoJSON` configuration to enable GeoJSON conversion
110+
and supply a comma delimited list of additional types to convert.
111+
112+
```json
113+
{
114+
"autocastGeoJSON" : "Point,LineString,Polygon"
115+
}
116+
```
117+
107118
### Metadata support
108119

109120
Both `attributes` and `static_attributes` may be supplied with metadata when provisioning an IoT Agent, so that the
@@ -151,7 +162,7 @@ following:
151162
- Temporal Properties (e.g. `Datetime`, `Date` , `Time`)
152163
- GeoJSON types (e.g `Point`, `LineString`, `Polygon`, `MultiPoint`, `MultiLineString`, `MultiPolygon`)
153164

154-
Most NGSI-LD attributes are sent to the Context Broker as _properties_. If a GeoJSON type or native JSON type is
165+
Most **NGSI-LD** attributes are sent to the Context Broker as _properties_. If a GeoJSON type or native JSON type is
155166
defined, the data will be converted to the appropriate type. Temporal properties should always be expressed in UTC,
156167
using ISO 8601. This ISO 8601 conversion is applied automatically for the `observedAt` _property-of-a-property_ metadata
157168
where present.

doc/installationguide.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ used for the same purpose. For instance:
278278
standard, but the final decision has yet been confirmed), take into account it could change
279279
- **explicitAttrs**: if this flag is activated, only provisioned attributes will be processed to Context Broker. This
280280
flag is overwritten by `explicitAttrs` flag in group or device provision.
281-
- **defaultEntityNameConjunction**: the default conjunction string used to compose a default `entity_name` when is not
282-
provided at device provisioning time; in that case `entity_name` is composed by `type` + `:` + `device_id`.
281+
- **defaultEntityNameConjunction**: the default conjunction string used to compose a default `entity_name` when is not
282+
provided at device provisioning time; in that case `entity_name` is composed by `type` + `:` + `device_id`.
283283
Default value is `:`. This value is overwritten by `defaultEntityNameConjunction` in group provision.
284284
- **relaxTemplateValidation**: if this flag is activated, `objectId` attributes for incoming devices are not
285285
validated, and may exceptionally include characters (such as semi-colons) which are
@@ -342,6 +342,7 @@ overrides.
342342
| IOTA_POLLING_EXPIRATION | `pollingExpiration` |
343343
| IOTA_POLLING_DAEMON_FREQ | `pollingDaemonFrequency` |
344344
| IOTA_AUTOCAST | `autocast` |
345+
| IOTA_AUTOCAST_GEOJSON | `autocastGeoJSON` |
345346
| IOTA_MULTI_CORE | `multiCore` |
346347
| IOTA_JSON_LD_CONTEXT | `jsonLdContext` |
347348
| IOTA_FALLBACK_TENANT | `fallbackTenant` |

lib/commonConfig.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ function processEnvironmentVariables() {
143143
'IOTA_MONGO_DB',
144144
'IOTA_MONGO_REPLICASET',
145145
'IOTA_AUTOCAST',
146+
'IOTA_AUTOCAST_GEOJSON',
146147
'IOTA_MONGO_PASSWORD',
147148
'IOTA_MONGO_AUTH_SOURCE',
148149
'IOTA_MONGO_RETRIES',
@@ -453,6 +454,12 @@ function processEnvironmentVariables() {
453454
config.autocast = process.env.IOTA_AUTOCAST === 'true';
454455
}
455456

457+
458+
if (process.env.IOTA_AUTOCAST_GEOJSON) {
459+
config.autocastGeoJSON = process.env.IOTA_AUTOCAST_GEOJSON.split(',').map((ctx) => ctx.trim());
460+
}
461+
config.autocastGeoJSON = config.autocastGeoJSON || [];
462+
456463
if (process.env.IOTA_MULTI_CORE) {
457464
config.multiCore = process.env.IOTA_MULTI_CORE === 'true';
458465
} else {

lib/services/ngsi/entities-NGSI-v2.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@ const context = {
5151
*/
5252
function formatGeoAttrs(attr) {
5353
const obj = attr;
54+
5455
if (attr.type) {
55-
switch (attr.type.toLowerCase()) {
56+
if ( attr.type.toLowerCase() === 'geo:json'){
57+
// cast GeoJSON by default
58+
obj.type = 'geo:json';
59+
obj.value = NGSIUtils.getLngLats('Point', attr.value);
60+
} else if (config.getConfig().autocastGeoJSON.includes(attr.type)){
61+
switch (attr.type.toLowerCase()) {
5662
// GeoProperties
57-
case 'geo:json':
5863
case 'geoproperty':
5964
case 'point':
6065
case 'geo:point':
@@ -86,6 +91,7 @@ function formatGeoAttrs(attr) {
8691
obj.type = 'geo:json';
8792
obj.value = NGSIUtils.getLngLats('MultiPolygon', attr.value);
8893
break;
94+
}
8995
}
9096
}
9197
return obj;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"lint:text": "textlint '*.md' 'doc/*.md'",
3737
"prettier": "prettier --config .prettierrc.json --write '**/**/**/**/*.js' '**/**/**/*.js' '**/**/*.js' '**/*.js' '*.js'",
3838
"prettier:text": "prettier 'README.md' 'doc/*.md' 'doc/**/*.md' --no-config --tab-width 4 --print-width 120 --write --prose-wrap always",
39-
"test": "nyc --reporter=text mocha --recursive 'test/**/*.js' --reporter spec --timeout 5000 --ui bdd --exit --color true",
39+
"test": "nyc --reporter=text mocha --recursive 'test/**/*.js' --reporter spec --timeout 5000 --ui bdd --exit --color true -g 'NGSI-v2 - Bidirectional data plugin'",
4040
"test:debug": "mocha --recursive 'test/**/*.js' --reporter spec --inspect-brk --timeout 30000 --ui bdd --exit",
4141
"test:coverage": "nyc --reporter=lcov mocha -- --recursive 'test/**/*.js' --reporter spec --timeout 5000 --exit",
4242
"test:coveralls": "npm run test:coverage && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"location": {
3+
"value": "23,12.5",
4+
"type": "Point"
5+
}
6+
}
7+

test/unit/ngsiv2/ngsiService/geoproperties-test.js

Lines changed: 99 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,66 @@
2424
*/
2525

2626
const iotAgentLib = require('../../../../lib/fiware-iotagent-lib');
27-
// FIXME: #1012
28-
//const utils = require('../../../tools/utils');
29-
//const should = require('should');
27+
const utils = require('../../../tools/utils');
28+
const should = require('should');
3029
const logger = require('logops');
31-
//const nock = require('nock');
32-
//let contextBrokerMock;
33-
//const iotAgentConfig = {
34-
// autocast: true,
35-
// contextBroker: {
36-
// host: '192.168.1.1',
37-
// port: '1026',
38-
// ngsiVersion: 'v2'
39-
// },
40-
// server: {
41-
// port: 4041
42-
// },
43-
// types: {
44-
// Light: {
45-
// commands: [],
46-
// type: 'Light',
47-
// active: [
48-
// {
49-
// name: 'location',
50-
// type: 'geo:json'
51-
// }
52-
// ]
53-
// }
54-
// },
55-
// service: 'smartGondor',
56-
// subservice: 'gardens',
57-
// providerUrl: 'http://smartGondor.com'
58-
//};
30+
const nock = require('nock');
31+
let contextBrokerMock;
32+
const iotAgentConfig = {
33+
autocast: true,
34+
autocastGeoJSON: ['Point', 'Polygon', 'LineString'],
35+
contextBroker: {
36+
host: '192.168.1.1',
37+
port: '1026',
38+
ngsiVersion: 'v2'
39+
},
40+
server: {
41+
port: 4041
42+
},
43+
types: {
44+
Light: {
45+
commands: [],
46+
type: 'Light',
47+
active: [
48+
{
49+
name: 'location',
50+
type: 'geo:json'
51+
}
52+
]
53+
}
54+
},
55+
service: 'smartGondor',
56+
subservice: 'gardens',
57+
providerUrl: 'http://smartGondor.com'
58+
};
59+
60+
// No autoCast
61+
const iotAgentConfig2 = {
62+
autocast: true,
63+
contextBroker: {
64+
host: '192.168.1.1',
65+
port: '1026',
66+
ngsiVersion: 'v2'
67+
},
68+
server: {
69+
port: 4041
70+
},
71+
types: {
72+
Light: {
73+
commands: [],
74+
type: 'Light',
75+
active: [
76+
{
77+
name: 'location',
78+
type: 'geo:json'
79+
}
80+
]
81+
}
82+
},
83+
service: 'smartGondor',
84+
subservice: 'gardens',
85+
providerUrl: 'http://smartGondor.com'
86+
};
5987

6088
describe('NGSI-v2 - Geo-JSON types autocast test', function () {
6189
beforeEach(function () {
@@ -105,6 +133,46 @@ describe('NGSI-v2 - Geo-JSON types autocast test', function () {
105133
}
106134
);
107135

136+
137+
describe(
138+
'When the IoT Agent receives new geo-information from a device.' +
139+
'Location with Point type and String value and Point is not autocast',
140+
function () {
141+
const values = [
142+
{
143+
name: 'location',
144+
type: 'Point',
145+
value: '23,12.5'
146+
}
147+
];
148+
149+
beforeEach(function (done) {
150+
nock.cleanAll();
151+
152+
contextBrokerMock = nock('http://192.168.1.1:1026')
153+
.matchHeader('fiware-service', 'smartGondor')
154+
.post(
155+
'/v2/entities/light1/attrs',
156+
utils.readExampleFile(
157+
'./test/unit/ngsiv2/examples/contextRequests/updateContextGeopropertiesAsString.json'
158+
)
159+
)
160+
.query({ type: 'Light' })
161+
.reply(204);
162+
163+
iotAgentLib.activate(iotAgentConfig2, done);
164+
});
165+
166+
it('should not change the value of the corresponding attribute in the context broker', function (done) {
167+
iotAgentLib.update('light1', 'Light', '', values, function (error) {
168+
should.not.exist(error);
169+
contextBrokerMock.done();
170+
done();
171+
});
172+
});
173+
}
174+
);
175+
108176
describe(
109177
'When the IoT Agent receives new geo-information from a device.' +
110178
'Location with geo:json type and GeoJSON object value',

test/unit/ngsiv2/plugins/bidirectional-plugin_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const nock = require('nock');
3333
const request = require('request');
3434
let contextBrokerMock;
3535
const iotAgentConfig = {
36+
autocastGeoJSON: ['geo:point'],
3637
contextBroker: {
3738
host: '192.168.1.1',
3839
port: '1026',
@@ -61,7 +62,7 @@ describe('NGSI-v2 - Bidirectional data plugin', function () {
6162
};
6263

6364
beforeEach(function (done) {
64-
logger.setLevel('FATAL');
65+
logger.setLevel('DEBUG');
6566

6667
iotAgentLib.activate(iotAgentConfig, function () {
6768
iotAgentLib.clearAll(function () {

0 commit comments

Comments
 (0)