diff --git a/lib/modules/measure/MeasureService.ts b/lib/modules/measure/MeasureService.ts index eaf1c041..fe4a1949 100644 --- a/lib/modules/measure/MeasureService.ts +++ b/lib/modules/measure/MeasureService.ts @@ -513,7 +513,7 @@ export class MeasureService extends BaseService { device._source.measures = {}; } - let lastMeasuredAt = 0; + let lastMeasuredAt = device._source.lastMeasuredAt ?? 0; for (const measurement of measurements) { if (measurement.origin.type !== "device") { @@ -564,7 +564,7 @@ export class MeasureService extends BaseService { asset._source.measures = {}; } - let lastMeasuredAt = 0; + let lastMeasuredAt = asset._source.lastMeasuredAt ?? 0; for (const measurement of measurements) { if (measurement.origin.type === "computed") { diff --git a/tests/scenario/modules/ingestion-pipeline/pipeline-process-before.test.ts b/tests/scenario/modules/ingestion-pipeline/pipeline-process-before.test.ts index c9968aca..4ef58c0d 100644 --- a/tests/scenario/modules/ingestion-pipeline/pipeline-process-before.test.ts +++ b/tests/scenario/modules/ingestion-pipeline/pipeline-process-before.test.ts @@ -78,4 +78,69 @@ describe("Ingestion Pipeline: process before", () => { expect(asset._source.lastMeasuredAt).toBe(1680096420000); }); + + it("should not update lastMeasuredAt if measures are older than current lastMeasure", async () => { + const now = Date.now(); + await sendDummyTempPositionPayloads(sdk, [ + { + deviceEUI: "linked2", + temperature: { + value: 21, + measuredAt: now, + }, + location: { + value: { + lat: 21, + lon: 21, + }, + measuredAt: now, + }, + }, + ]); + + let device = await sdk.document.get( + "engine-ayse", + "devices", + "DummyTempPosition-linked2", + ); + expect(device._source.lastMeasuredAt).toBeGreaterThanOrEqual(now); + let asset = await sdk.document.get( + "engine-ayse", + "assets", + "Container-linked2", + ); + expect(asset._source.lastMeasuredAt).toBeGreaterThanOrEqual(now); + + await sendDummyTempPositionPayloads(sdk, [ + { + deviceEUI: "linked2", + temperature: { + value: 21, + measuredAt: now - 100000, + }, + location: { + value: { + lat: 21, + lon: 21, + }, + measuredAt: now - 100000, + }, + }, + ]); + device = await sdk.document.get( + "engine-ayse", + "devices", + "DummyTempPosition-linked2", + ); + + expect(device._source.lastMeasuredAt).toBeGreaterThanOrEqual(now); + + asset = await sdk.document.get( + "engine-ayse", + "assets", + "Container-linked2", + ); + + expect(asset._source.lastMeasuredAt).toBeGreaterThanOrEqual(now); + }); });