Skip to content

Commit 31c4142

Browse files
authored
Merge pull request #102 from kuzzleio/add-full-asset-in-measure-event
## Description Fetch the entire asset before triggering the `tenant:{tenant-id}:asset:measure:new` event
2 parents d85fc48 + 0cd999a commit 31c4142

File tree

7 files changed

+36
-26
lines changed

7 files changed

+36
-26
lines changed

doc/1/guides/assets/index.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,22 @@ It is possible to override the [Decoder.copyToAsset](/official-plugins/device-ma
9090

9191
Assets are historized in the `assets-history` collection when a new measure is received.
9292

93-
Before historization, the `tenant:<tenant-id>:asset:measure:new` event is emitted. This event allows to enrich the asset before it is historized.
93+
Before historization, the `tenant:<tenant-id>:asset:measure:new` event is emitted.
9494

95-
The payload contains the new `measures` and the `assetId`.
95+
The payload contain the asset updated content and the types of the new added measures.
9696

97-
The content of `request.result.asset` will be used to update the asset measures and then create the history document.
97+
At the end of the processing, the asset will be updated and historized with the content of the `request.result.asset._source`.
9898

9999
```js
100100
app.pipe.register(`tenant:<tenant-id>:asset:measures:new`, async (request: KuzzleRequest) => {
101-
const measures: AssetMeasures = request.result.asset.measures;
102-
const assetId: string = request.result.assetId;
103-
104-
request.result.asset.metadata = {
105-
enriched: true,
106-
assetId: request.result.assetId
107-
};
101+
const asset = request.result.asset;
102+
const measureTypes = request.result.measureTypes;
103+
104+
if (measureTypes.includes('position')) {
105+
request.result.asset._source.metadata = {
106+
city: 'Adrasan',
107+
};
108+
}
108109

109110
return request;
110111
});

features/PayloadController.feature

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Feature: Payloads Controller
147147
| measures.temperature.qos.battery | 40 |
148148
# Enriched with the event
149149
| metadata.enriched | true |
150-
| metadata.assetId | "MART-linked" |
150+
| metadata.measureTypes | ["temperature"] |
151151
And I should receive a result matching:
152152
| device._id | "DummyTemp-attached_ayse_linked" |
153153
| asset._id | "MART-linked" |
@@ -160,10 +160,10 @@ Feature: Payloads Controller
160160
| batteryLevel | 0.42 |
161161
And I refresh the collection "tenant-ayse":"assets-history"
162162
And The last document from "tenant-ayse":"assets-history" content match:
163-
| assetId | "MART-linked" |
164-
| measureTypes | ["temperature"] |
165-
| assetId | "MART-linked" |
166-
| asset.model | "MART" |
167-
| asset.reference | "linked" |
168-
| asset.metadata.enriched | true |
169-
| asset.metadata.assetId | "MART-linked" |
163+
| assetId | "MART-linked" |
164+
| measureTypes | ["temperature"] |
165+
| assetId | "MART-linked" |
166+
| asset.model | "MART" |
167+
| asset.reference | "linked" |
168+
| asset.metadata.enriched | true |
169+
| asset.metadata.measureTypes | ["temperature"] |

features/fixtures/application/app.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ deviceManager.assets.register('hevSuit', {
6262

6363
// Register a pipe to enrich a tenant asset
6464
app.pipe.register(`tenant:tenant-ayse:asset:measures:new`, async (request: KuzzleRequest) => {
65-
if (request.result.assetId !== 'MART-linked') {
65+
if (request.result.asset._id !== 'MART-linked') {
6666
return request;
6767
}
6868

69-
request.result.asset.metadata = {
69+
request.result.asset._source.metadata = {
7070
enriched: true,
71-
assetId: request.result.assetId
71+
measureTypes: request.result.measureTypes
7272
};
7373

7474
return request;

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from './lib/DeviceManagerPlugin';
22

3-
export * from './lib/core-classes/Decoder';
3+
export * from './lib/core-classes';
44

55
export * from './lib/models';
66

lib/core-classes/PayloadService.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,24 @@ export class PayloadService {
290290
): Promise<BaseAsset> {
291291
const measures = await decoder.copyToAsset(updatedDevice);
292292

293+
const measureTypes = Object.keys(measures);
294+
295+
const asset = await this.batchController.get(
296+
tenantId,
297+
'assets',
298+
assetId);
299+
300+
asset._source.measures = _.merge(asset._source.measures, measures);
301+
293302
const { result } = await global.app.trigger(
294303
`tenant:${tenantId}:asset:measures:new`,
295-
eventPayload({ asset: { measures }, assetId }));
304+
eventPayload({ asset, measureTypes }));
296305

297306
const assetDocument = await this.batchController.update(
298307
tenantId,
299308
'assets',
300309
assetId,
301-
result.asset,
310+
result.asset._source,
302311
{ source: true, retryOnConflict: 10 });
303312

304313
return new BaseAsset(assetDocument._source as any, assetDocument._id);

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kuzzleio/plugin-device-manager",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "Manage your IoT devices and assets. Choose a provisionning strategy, receive and decode payload, handle your IoT business logic.",
55
"author": "The Kuzzle Team (support@kuzzle.io)",
66
"repository": {

0 commit comments

Comments
 (0)