From fda20f1b468bb52369fbf82f31cd23dad9015fc4 Mon Sep 17 00:00:00 2001 From: Adrien Maret Date: Mon, 19 Dec 2022 14:37:17 +0100 Subject: [PATCH] [breaking] Pass decodedPayload to Decoder.decode method (#243) Instantiate the `DecoderPayload` and pass it directly to the `Decoder.decode` method. Before ``` async decode( payload: JSONObject, ): Promise> { const decodedPayload = new DecodedPayload(this); // decode measurements.. return decodedPayload; } ``` After ``` async decode( decodedPayload: DecodedPayload, payload: JSONObject, ): Promise> { // decode measurements.. return decodedPayload; } ``` --- features/fixtures/application/decoders/DummyTempDecoder.ts | 7 ++++--- .../application/decoders/DummyTempPositionDecoder.ts | 7 ++++--- lib/modules/decoder/Decoder.ts | 3 ++- lib/modules/decoder/PayloadService.ts | 5 ++++- package-lock.json | 4 ++-- package.json | 2 +- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/features/fixtures/application/decoders/DummyTempDecoder.ts b/features/fixtures/application/decoders/DummyTempDecoder.ts index 4515b886..426b8205 100644 --- a/features/fixtures/application/decoders/DummyTempDecoder.ts +++ b/features/fixtures/application/decoders/DummyTempDecoder.ts @@ -29,9 +29,10 @@ export class DummyTempDecoder extends Decoder { return true; } - async decode(payload: JSONObject): Promise> { - const decodedPayload = new DecodedPayload(this); - + async decode( + decodedPayload: DecodedPayload, + payload: JSONObject, + ): Promise> { if (payload?.metadata?.color) { decodedPayload.addMetadata(payload.deviceEUI, { color: payload.metadata.color, diff --git a/features/fixtures/application/decoders/DummyTempPositionDecoder.ts b/features/fixtures/application/decoders/DummyTempPositionDecoder.ts index 7c4e94e1..a7dd1927 100644 --- a/features/fixtures/application/decoders/DummyTempPositionDecoder.ts +++ b/features/fixtures/application/decoders/DummyTempPositionDecoder.ts @@ -23,9 +23,10 @@ export class DummyTempPositionDecoder extends Decoder { return true; } - async decode(payload: JSONObject): Promise> { - const decodedPayload = new DecodedPayload(this); - + async decode( + decodedPayload: DecodedPayload, + payload: JSONObject, + ): Promise> { decodedPayload.addMeasurement( payload.deviceEUI, "temperature", diff --git a/lib/modules/decoder/Decoder.ts b/lib/modules/decoder/Decoder.ts index daed5d64..cf50aeb8 100644 --- a/lib/modules/decoder/Decoder.ts +++ b/lib/modules/decoder/Decoder.ts @@ -147,9 +147,10 @@ export abstract class Decoder { */ // eslint-disable-next-line no-unused-vars abstract decode( + decodedPayload: DecodedPayload, payload: JSONObject, request: KuzzleRequest - ): Promise>; + ): Promise>; /** * Checks if the provided properties are present in the payload diff --git a/lib/modules/decoder/PayloadService.ts b/lib/modules/decoder/PayloadService.ts index 1dc6e81b..e6e75725 100644 --- a/lib/modules/decoder/PayloadService.ts +++ b/lib/modules/decoder/PayloadService.ts @@ -14,6 +14,7 @@ import { } from "../../core"; import { DeviceContent, DeviceSerializer } from "../device"; import { EventMeasureIngest } from "../measure"; +import { DecodedPayload } from "./DecodedPayload"; import { Decoder } from "./Decoder"; @@ -63,7 +64,9 @@ export class PayloadService { await this.savePayload(decoder.deviceModel, uuid, valid, payload); } - const decodedPayload = await decoder.decode(payload, request); + let decodedPayload = new DecodedPayload(decoder); + + decodedPayload = await decoder.decode(decodedPayload, payload, request); const devices = await this.retrieveDevices( decoder.deviceModel, diff --git a/package-lock.json b/package-lock.json index e0559c31..d37e4f94 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "kuzzle-device-manager", - "version": "2.0.0-rc33", + "version": "2.0.0-rc34", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "kuzzle-device-manager", - "version": "2.0.0-rc33", + "version": "2.0.0-rc34", "license": "Apache-2.0", "dependencies": { "kuzzle-plugin-commons": "https://github.com/kuzzleio/kuzzle-plugin-commons/archive/refs/tags/1.0.5.tar.gz", diff --git a/package.json b/package.json index 9d8b0cdd..fb8fbc92 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kuzzle-device-manager", - "version": "2.0.0-rc33", + "version": "2.0.0-rc34", "description": "Manage your IoT devices and assets. Choose a provisioning strategy, receive and decode payload, handle your IoT business logic.", "author": "The Kuzzle Team (support@kuzzle.io)", "repository": {