From bfdf632dee697060bba7e60617e9d6b83248ab17 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 20 Oct 2021 16:55:17 +0200 Subject: [PATCH 1/7] add feature list-decoders --- .../controllers/device/list-decoders/index.md | 58 +++++++++++++++++++ features/DeviceController.feature | 6 ++ lib/controllers/DeviceController.ts | 11 ++++ lib/core-classes/DeviceService.ts | 7 +++ 4 files changed, 82 insertions(+) create mode 100644 doc/1/controllers/device/list-decoders/index.md diff --git a/doc/1/controllers/device/list-decoders/index.md b/doc/1/controllers/device/list-decoders/index.md new file mode 100644 index 00000000..3134ccd5 --- /dev/null +++ b/doc/1/controllers/device/list-decoders/index.md @@ -0,0 +1,58 @@ +--- +code: true +type: page +title: listDecoders +description: List available registered decoders +--- + +# linkAsset + +List available registered decoders. + +--- + +## Query Syntax + +### HTTP + +```http +URL: http://localhost:7512/_/device-manager/devices/_listDecoders +Method: GET +``` + +### Other protocols + +```js +{ + "controller": "device-manager/device", + "action": "listDecoders", +} +``` + +### Kourou + +```bash +kourou device-manager/device:listDecoders +``` +--- + +## Response + +```js +{ + "action": "listDecoders", + "controller": "device-manager/device", + "error": null, + "node": "knode-shaggy-salmon-94717", + "requestId": "e74e2d34-6699-4dbf-8f38-874c6d2b9e1a", + "result": { + "size": 2, + "decoders": [ + "DummyTemp", + "DummyTempPosition" + ] + }, + "status": 200, + "volatile": null +} +``` diff --git a/features/DeviceController.feature b/features/DeviceController.feature index db5cee34..b68e9f34 100644 --- a/features/DeviceController.feature +++ b/features/DeviceController.feature @@ -325,3 +325,9 @@ Feature: Device Manager device controller | collection | "payloads" | Then I should receive a result matching: | total | 1 | + + Scenario: List all registered decoders + When When I successfully execute the action "device-manager/device":"listDecoders" + Then I should receive a result matching: + | size | 2 | + | decoders | ["DummyTemp", "DummyTempPosition"] | \ No newline at end of file diff --git a/lib/controllers/DeviceController.ts b/lib/controllers/DeviceController.ts index c8658cfe..71bbd4dd 100644 --- a/lib/controllers/DeviceController.ts +++ b/lib/controllers/DeviceController.ts @@ -72,10 +72,21 @@ export class DeviceController extends CRUDController { handler: this.prunePayloads.bind(this), http: [{ verb: 'delete', path: 'device-manager/devices/_prunePayloads' }] }, + listDecoders: { + handler: this.listDecoders.bind(this), + http: [{ verb: 'get', path: 'device-manager/devices/_listDecoders' }] + } } }; } + /** + * List all available decoders + */ + async listDecoders () { + return this.deviceService.listDecoders(); + } + /** * Attach a device to a tenant */ diff --git a/lib/core-classes/DeviceService.ts b/lib/core-classes/DeviceService.ts index c8dfb362..c94d0dba 100644 --- a/lib/core-classes/DeviceService.ts +++ b/lib/core-classes/DeviceService.ts @@ -36,6 +36,13 @@ export class DeviceService { this.decoders = decoders; } + async listDecoders() { + return { + size: this.decoders.size, + decoders: Array.from(this.decoders.keys()) + }; + } + async mAttach ( devices: Device[], bulkData: DeviceBulkContent[], From 0ff485ed18f758067cec4f555e0e3073d576c1d1 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Thu, 21 Oct 2021 11:06:40 +0200 Subject: [PATCH 2/7] Move the logic to a dedicated controller DecodersController --- .../decoder/list-decoders/index.md | 67 +++++++++++++++++++ .../controllers/device/list-decoders/index.md | 58 ---------------- lib/DeviceManagerPlugin.ts | 7 ++ lib/controllers/DecodersController.ts | 40 +++++++++++ lib/controllers/DeviceController.ts | 11 --- lib/controllers/index.ts | 1 + lib/core-classes/DecodersService.ts | 28 ++++++++ lib/core-classes/DeviceService.ts | 7 -- lib/core-classes/index.ts | 2 + 9 files changed, 145 insertions(+), 76 deletions(-) create mode 100644 doc/1/controllers/decoder/list-decoders/index.md delete mode 100644 doc/1/controllers/device/list-decoders/index.md create mode 100644 lib/controllers/DecodersController.ts create mode 100644 lib/core-classes/DecodersService.ts diff --git a/doc/1/controllers/decoder/list-decoders/index.md b/doc/1/controllers/decoder/list-decoders/index.md new file mode 100644 index 00000000..4ce63f3b --- /dev/null +++ b/doc/1/controllers/decoder/list-decoders/index.md @@ -0,0 +1,67 @@ +--- +code: true +type: page +title: list +description: List available registered decoders +--- + +# linkAsset + +List available registered decoders. + +--- + +## Query Syntax + +### HTTP + +```http +URL: http://localhost:7512/_/device-manager/decoders/_list +Method: GET +``` + +### Other protocols + +```js +{ + "controller": "device-manager/decoders", + "action": "list", +} +``` + +### Kourou + +```bash +kourou device-manager/decoders:list +``` +--- + +## Response + +```js +{ + "action": "list", + "controller": "device-manager/decoders", + "error": null, + "node": "knode-abrasive-manatee-50960", + "requestId": "ea8fcab2-dfb2-44d5-880d-cbe1d69696b6", + "result": [ + { + "payloadsMappings": { + "deviceEUI": { + "type": "keyword" + } + }, + "deviceModel": "DummyTemp", + "action": "dummy-temp" + }, + { + "payloadsMappings": {}, + "deviceModel": "DummyTempPosition", + "action": "dummy-temp-position" + } + ], + "status": 200, + "volatile": null +} +``` diff --git a/doc/1/controllers/device/list-decoders/index.md b/doc/1/controllers/device/list-decoders/index.md deleted file mode 100644 index 3134ccd5..00000000 --- a/doc/1/controllers/device/list-decoders/index.md +++ /dev/null @@ -1,58 +0,0 @@ ---- -code: true -type: page -title: listDecoders -description: List available registered decoders ---- - -# linkAsset - -List available registered decoders. - ---- - -## Query Syntax - -### HTTP - -```http -URL: http://localhost:7512/_/device-manager/devices/_listDecoders -Method: GET -``` - -### Other protocols - -```js -{ - "controller": "device-manager/device", - "action": "listDecoders", -} -``` - -### Kourou - -```bash -kourou device-manager/device:listDecoders -``` ---- - -## Response - -```js -{ - "action": "listDecoders", - "controller": "device-manager/device", - "error": null, - "node": "knode-shaggy-salmon-94717", - "requestId": "e74e2d34-6699-4dbf-8f38-874c6d2b9e1a", - "result": { - "size": 2, - "decoders": [ - "DummyTemp", - "DummyTempPosition" - ] - }, - "status": 200, - "volatile": null -} -``` diff --git a/lib/DeviceManagerPlugin.ts b/lib/DeviceManagerPlugin.ts index 7d3d33b1..f1deb5c5 100644 --- a/lib/DeviceManagerPlugin.ts +++ b/lib/DeviceManagerPlugin.ts @@ -15,6 +15,7 @@ import { EngineController } from 'kuzzle-plugin-commons'; import { AssetController, DeviceController, + DecodersController } from './controllers'; import { DeviceManagerEngine, @@ -26,6 +27,7 @@ import { PayloadHandler, AssetMappingsManager, DeviceMappingsManager, + DecodersService } from './core-classes'; import { assetsMappings, @@ -81,10 +83,12 @@ export class DeviceManagerPlugin extends Plugin { private assetController: AssetController; private deviceController: DeviceController; private engineController: EngineController; + private decodersController: DecodersController; private payloadService: PayloadService; private deviceManagerEngine: DeviceManagerEngine; private deviceService: DeviceService; + private decodersService: DecodersService; private migrationService: MigrationService; private batchWriter: BatchWriter; @@ -186,15 +190,18 @@ export class DeviceManagerPlugin extends Plugin { this.payloadService = new PayloadService(this, this.batchWriter); this.deviceService = new DeviceService(this, this.decoders); + this.decodersService = new DecodersService(this, this.decoders); this.migrationService = new MigrationService('device-manager', this); this.deviceManagerEngine = new DeviceManagerEngine(this, this.assetMappings, this.deviceMappings); this.assetController = new AssetController(this); this.deviceController = new DeviceController(this, this.deviceService); + this.decodersController = new DecodersController(this, this.decodersService); this.engineController = new EngineController('device-manager', this, this.deviceManagerEngine); this.api['device-manager/asset'] = this.assetController.definition; this.api['device-manager/device'] = this.deviceController.definition; + this.api['device-manager/decoders'] = this.decodersController.definition; this.pipes = { 'device-manager/device:beforeUpdate': this.pipeCheckEngine.bind(this), diff --git a/lib/controllers/DecodersController.ts b/lib/controllers/DecodersController.ts new file mode 100644 index 00000000..8ffc99c4 --- /dev/null +++ b/lib/controllers/DecodersController.ts @@ -0,0 +1,40 @@ +import { + EmbeddedSDK, + Plugin, +} from 'kuzzle'; + +import { CRUDController } from './CRUDController'; +import { DecodersService } from '../core-classes'; + +export class DecodersController extends CRUDController { + private decodersService: DecodersService; + + get sdk(): EmbeddedSDK { + return this.context.accessors.sdk; + } + + constructor(plugin: Plugin, decodersService: DecodersService) { + super(plugin, 'decoders'); + + this.decodersService = decodersService; + + this.definition = { + actions: { + list: { + handler: this.list.bind(this), + http: [{ verb: 'get', path: 'device-manager/decoders/_list' }] + } + } + }; + } + + /** + * List all available decoders + */ + async list () { + const decoders = this.decodersService.list(); + + return decoders; + } + +} diff --git a/lib/controllers/DeviceController.ts b/lib/controllers/DeviceController.ts index f532bf31..eec7a79d 100644 --- a/lib/controllers/DeviceController.ts +++ b/lib/controllers/DeviceController.ts @@ -72,22 +72,11 @@ export class DeviceController extends CRUDController { prunePayloads: { handler: this.prunePayloads.bind(this), http: [{ verb: 'delete', path: 'device-manager/devices/_prunePayloads' }] - }, - listDecoders: { - handler: this.listDecoders.bind(this), - http: [{ verb: 'get', path: 'device-manager/devices/_listDecoders' }] } } }; } - /** - * List all available decoders - */ - async listDecoders () { - return this.deviceService.listDecoders(); - } - /** * Attach a device to a tenant */ diff --git a/lib/controllers/index.ts b/lib/controllers/index.ts index 771b3361..5ed9a330 100644 --- a/lib/controllers/index.ts +++ b/lib/controllers/index.ts @@ -1,3 +1,4 @@ export * from './AssetController'; export * from './CRUDController'; export * from './DeviceController'; +export * from './DecodersController'; diff --git a/lib/core-classes/DecodersService.ts b/lib/core-classes/DecodersService.ts new file mode 100644 index 00000000..7c2f226b --- /dev/null +++ b/lib/core-classes/DecodersService.ts @@ -0,0 +1,28 @@ +import { PluginContext, EmbeddedSDK, Plugin } from "kuzzle"; + +import { Decoder } from "./Decoder"; +import { DeviceManagerConfig } from "../DeviceManagerPlugin"; + +export class DecodersService { + private config: DeviceManagerConfig; + private context: PluginContext; + + private decoders: Map; + + get sdk(): EmbeddedSDK { + return this.context.accessors.sdk; + } + + constructor(plugin: Plugin, decoders: Map) { + this.config = plugin.config as any; + this.context = plugin.context; + + this.decoders = decoders; + } + + async list() { + return Array + .from(this.decoders.keys()) + .map(decoder => this.decoders.get(decoder)); + } +} diff --git a/lib/core-classes/DeviceService.ts b/lib/core-classes/DeviceService.ts index c94d0dba..c8dfb362 100644 --- a/lib/core-classes/DeviceService.ts +++ b/lib/core-classes/DeviceService.ts @@ -36,13 +36,6 @@ export class DeviceService { this.decoders = decoders; } - async listDecoders() { - return { - size: this.decoders.size, - decoders: Array.from(this.decoders.keys()) - }; - } - async mAttach ( devices: Device[], bulkData: DeviceBulkContent[], diff --git a/lib/core-classes/index.ts b/lib/core-classes/index.ts index b3b125ab..2b431cf8 100644 --- a/lib/core-classes/index.ts +++ b/lib/core-classes/index.ts @@ -10,6 +10,8 @@ export * from './BatchProcessing'; export * from './Decoder'; +export * from './DecodersService'; + export * from './CustomMappings/AssetMappingsManager'; export * from './CustomMappings/DeviceMappingsManager'; From 98dec5c888980ef7228017c523b768db9b13beba Mon Sep 17 00:00:00 2001 From: Nicolas Date: Thu, 21 Oct 2021 11:18:55 +0200 Subject: [PATCH 3/7] Add DecodersController.features --- .../decoder/list-decoders/index.md | 36 ++++++++++--------- features/DecodersController.feature | 6 ++++ features/DeviceController.feature | 8 +---- lib/core-classes/DecodersService.ts | 8 +++-- 4 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 features/DecodersController.feature diff --git a/doc/1/controllers/decoder/list-decoders/index.md b/doc/1/controllers/decoder/list-decoders/index.md index 4ce63f3b..f25248fe 100644 --- a/doc/1/controllers/decoder/list-decoders/index.md +++ b/doc/1/controllers/decoder/list-decoders/index.md @@ -43,24 +43,26 @@ kourou device-manager/decoders:list "action": "list", "controller": "device-manager/decoders", "error": null, - "node": "knode-abrasive-manatee-50960", - "requestId": "ea8fcab2-dfb2-44d5-880d-cbe1d69696b6", - "result": [ - { - "payloadsMappings": { - "deviceEUI": { - "type": "keyword" - } + "node": "knode-common-carpenter-91992", + "requestId": "94ea97c3-18af-4a7e-a848-3760b9761c98", + "result": { + "decoders": [ + { + "payloadsMappings": { + "deviceEUI": { + "type": "keyword" + } + }, + "deviceModel": "DummyTemp", + "action": "dummy-temp" }, - "deviceModel": "DummyTemp", - "action": "dummy-temp" - }, - { - "payloadsMappings": {}, - "deviceModel": "DummyTempPosition", - "action": "dummy-temp-position" - } - ], + { + "payloadsMappings": {}, + "deviceModel": "DummyTempPosition", + "action": "dummy-temp-position" + } + ] + }, "status": 200, "volatile": null } diff --git a/features/DecodersController.feature b/features/DecodersController.feature new file mode 100644 index 00000000..f7491c64 --- /dev/null +++ b/features/DecodersController.feature @@ -0,0 +1,6 @@ +Feature: Device Manager decoders controller + + Scenario: List all registered decoders + When When I successfully execute the action "device-manager/decoders":"list" + Then I should receive a result matching: + | decoders | [{"payloadsMappings":{"deviceEUI":{"type":"keyword"}},"deviceModel":"DummyTemp","action":"dummy-temp"},{"payloadsMappings":{},"deviceModel":"DummyTempPosition","action":"dummy-temp-position"}] | \ No newline at end of file diff --git a/features/DeviceController.feature b/features/DeviceController.feature index a0b397bb..b449148e 100644 --- a/features/DeviceController.feature +++ b/features/DeviceController.feature @@ -8,7 +8,7 @@ Feature: Device Manager device controller | tenantId | "tenant-kuzzle" | And The document "tenant-kuzzle":"devices":"DummyTemp-detached" exists - Scenario: Attech a non-existing device to a tenant should throw an error + Scenario: Attach a non-existing device to a tenant should throw an error When I execute the action "device-manager/device":"attachTenant" with args: | _id | "Not-existing-device" | | index | "tenant-kuzzle" | @@ -332,9 +332,3 @@ Feature: Device Manager device controller | collection | "payloads" | Then I should receive a result matching: | total | 1 | - - Scenario: List all registered decoders - When When I successfully execute the action "device-manager/device":"listDecoders" - Then I should receive a result matching: - | size | 2 | - | decoders | ["DummyTemp", "DummyTempPosition"] | \ No newline at end of file diff --git a/lib/core-classes/DecodersService.ts b/lib/core-classes/DecodersService.ts index 7c2f226b..62843cf8 100644 --- a/lib/core-classes/DecodersService.ts +++ b/lib/core-classes/DecodersService.ts @@ -21,8 +21,10 @@ export class DecodersService { } async list() { - return Array - .from(this.decoders.keys()) - .map(decoder => this.decoders.get(decoder)); + return { + decoders: Array + .from(this.decoders.keys()) + .map(decoder => this.decoders.get(decoder)) + }; } } From 3b6c6bcb080816b10564622830b22506b56e0e82 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Thu, 21 Oct 2021 16:49:37 +0200 Subject: [PATCH 4/7] Add deviceMeasures to Decoder constructor --- doc/1/classes/decoder/constructor/index.md | 2 +- doc/1/guides/decoders/index.md | 4 ++-- features/DecodersController.feature | 2 +- .../application/decoders/DummyTempDecoder.ts | 2 +- .../decoders/DummyTempPositionDecoder.ts | 2 +- lib/core-classes/Decoder.ts | 13 ++++++++++--- lib/core-classes/DecodersService.ts | 9 ++++++++- 7 files changed, 24 insertions(+), 10 deletions(-) diff --git a/doc/1/classes/decoder/constructor/index.md b/doc/1/classes/decoder/constructor/index.md index 2d964389..14672952 100644 --- a/doc/1/classes/decoder/constructor/index.md +++ b/doc/1/classes/decoder/constructor/index.md @@ -26,7 +26,7 @@ import { Decoder } from 'kuzzle-plugin-device-manager'; class KarakoyDecoder extends Decoder { constructor () { - super('Karakoy'); + super("Karakoy", ["temperature"]); } // This method must be implemented diff --git a/doc/1/guides/decoders/index.md b/doc/1/guides/decoders/index.md index f14e4ec1..6fea9dfd 100644 --- a/doc/1/guides/decoders/index.md +++ b/doc/1/guides/decoders/index.md @@ -37,7 +37,7 @@ const deviceManager = new DeviceManagerPlugin(); class KarakoyDecoder extends Decoder { constructor () { - super("Karakoy"); + super("Karakoy", ["temperature"]); } async decode (payload: JSONObject, request: KuzzleRequest): Promise { @@ -74,7 +74,7 @@ You can specify a custom API action and custom HTTP routes by defining the `acti ```js class KarakoyDecoder extends Decoder { constructor () { - super("Karakoy"); + super("Karakoy", ["temperature"]); // Generated API action: "device-manager/payload:karakoy-v1" this.action = 'karakoy-v1'; diff --git a/features/DecodersController.feature b/features/DecodersController.feature index f7491c64..32a9f4a6 100644 --- a/features/DecodersController.feature +++ b/features/DecodersController.feature @@ -3,4 +3,4 @@ Feature: Device Manager decoders controller Scenario: List all registered decoders When When I successfully execute the action "device-manager/decoders":"list" Then I should receive a result matching: - | decoders | [{"payloadsMappings":{"deviceEUI":{"type":"keyword"}},"deviceModel":"DummyTemp","action":"dummy-temp"},{"payloadsMappings":{},"deviceModel":"DummyTempPosition","action":"dummy-temp-position"}] | \ No newline at end of file + | decoders | [{"deviceModel":"DummyTemp","deviceMeasures":["temperature"]},{"deviceModel":"DummyTempPosition","deviceMeasures":["temperature","position"]}] | \ No newline at end of file diff --git a/features/fixtures/application/decoders/DummyTempDecoder.ts b/features/fixtures/application/decoders/DummyTempDecoder.ts index d1840f5f..0c1632e1 100644 --- a/features/fixtures/application/decoders/DummyTempDecoder.ts +++ b/features/fixtures/application/decoders/DummyTempDecoder.ts @@ -3,7 +3,7 @@ import { JSONObject, KuzzleRequest, PreconditionError } from 'kuzzle'; export class DummyTempDecoder extends Decoder { constructor () { - super('DummyTemp'); + super('DummyTemp', ["temperature"]); this.payloadsMappings = { deviceEUI: { type: 'keyword' } diff --git a/features/fixtures/application/decoders/DummyTempPositionDecoder.ts b/features/fixtures/application/decoders/DummyTempPositionDecoder.ts index aec6a6d8..37ff3f7a 100644 --- a/features/fixtures/application/decoders/DummyTempPositionDecoder.ts +++ b/features/fixtures/application/decoders/DummyTempPositionDecoder.ts @@ -3,7 +3,7 @@ import { JSONObject, KuzzleRequest, PreconditionError } from 'kuzzle'; export class DummyTempPositionDecoder extends Decoder { constructor () { - super('DummyTempPosition'); + super('DummyTempPosition', ["temperature", "position"]); } async validate (payload: JSONObject, request: KuzzleRequest) { diff --git a/lib/core-classes/Decoder.ts b/lib/core-classes/Decoder.ts index 7bcb61dc..d28a7646 100644 --- a/lib/core-classes/Decoder.ts +++ b/lib/core-classes/Decoder.ts @@ -4,7 +4,8 @@ import { HttpRoute, PreconditionError, } from 'kuzzle'; -import _ from 'lodash'; + +import has from 'lodash/has'; import { Device, BaseAsset } from '../models'; @@ -26,6 +27,11 @@ export abstract class Decoder { */ deviceModel: string; + /** + * Device measure type + */ + deviceMeasures: string[]; + /** * Custom name for the associated API action in the "payload" controller */ @@ -63,8 +69,9 @@ export abstract class Decoder { /** * @param deviceModel Device model for this decoder */ - constructor (deviceModel: string) { + constructor (deviceModel: string, deviceMeasures: string[]) { this.deviceModel = deviceModel; + this.deviceMeasures = deviceMeasures; } /** @@ -197,7 +204,7 @@ export abstract class Decoder { */ ensureProperties (payload: JSONObject, paths: string[]): void | never { for (const path of paths) { - if (! _.has(payload, path)) { + if (! has(payload, path)) { throw new PreconditionError(`Missing property "${path}" in payload`); } } diff --git a/lib/core-classes/DecodersService.ts b/lib/core-classes/DecodersService.ts index 62843cf8..0bf715d2 100644 --- a/lib/core-classes/DecodersService.ts +++ b/lib/core-classes/DecodersService.ts @@ -24,7 +24,14 @@ export class DecodersService { return { decoders: Array .from(this.decoders.keys()) - .map(decoder => this.decoders.get(decoder)) + .map(decoder => { + const { deviceModel, deviceMeasures } = this.decoders.get(decoder) + + return { + deviceModel, + deviceMeasures + } + }) }; } } From 252768ea828eeebe4e8422457f0ff04c4a6dd412 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 22 Oct 2021 11:33:17 +0200 Subject: [PATCH 5/7] requested changes & docs & tests --- .../decoder/list-decoders/index.md | 21 +++++++++---------- lib/controllers/DecodersController.ts | 4 ++-- lib/core-classes/Decoder.ts | 19 +++++++++++++++-- lib/core-classes/DecodersService.ts | 18 +++++----------- lib/types/DecodersContent.ts | 4 ++++ lib/types/index.ts | 2 ++ 6 files changed, 40 insertions(+), 28 deletions(-) create mode 100644 lib/types/DecodersContent.ts diff --git a/doc/1/controllers/decoder/list-decoders/index.md b/doc/1/controllers/decoder/list-decoders/index.md index f25248fe..4ed15332 100644 --- a/doc/1/controllers/decoder/list-decoders/index.md +++ b/doc/1/controllers/decoder/list-decoders/index.md @@ -5,7 +5,7 @@ title: list description: List available registered decoders --- -# linkAsset +# List List available registered decoders. @@ -43,23 +43,22 @@ kourou device-manager/decoders:list "action": "list", "controller": "device-manager/decoders", "error": null, - "node": "knode-common-carpenter-91992", - "requestId": "94ea97c3-18af-4a7e-a848-3760b9761c98", + "node": "knode-nine-hydra-22631", + "requestId": "d888a8e1-2f80-4849-99d0-86ea70fe91e4", "result": { "decoders": [ { - "payloadsMappings": { - "deviceEUI": { - "type": "keyword" - } - }, "deviceModel": "DummyTemp", - "action": "dummy-temp" + "deviceMeasures": [ + "temperature" + ] }, { - "payloadsMappings": {}, "deviceModel": "DummyTempPosition", - "action": "dummy-temp-position" + "deviceMeasures": [ + "temperature", + "position" + ] } ] }, diff --git a/lib/controllers/DecodersController.ts b/lib/controllers/DecodersController.ts index 8ffc99c4..e70aadc3 100644 --- a/lib/controllers/DecodersController.ts +++ b/lib/controllers/DecodersController.ts @@ -32,9 +32,9 @@ export class DecodersController extends CRUDController { * List all available decoders */ async list () { - const decoders = this.decodersService.list(); + const decoders = await this.decodersService.list(); - return decoders; + return { decoders }; } } diff --git a/lib/core-classes/Decoder.ts b/lib/core-classes/Decoder.ts index d28a7646..9b4e6ad6 100644 --- a/lib/core-classes/Decoder.ts +++ b/lib/core-classes/Decoder.ts @@ -9,7 +9,7 @@ import has from 'lodash/has'; import { Device, BaseAsset } from '../models'; -import { AssetMeasures, DeviceContent } from '../types'; +import { AssetMeasures, DeviceContent, DecoderConstructor } from '../types'; /* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/no-empty-function */ @@ -28,7 +28,7 @@ export abstract class Decoder { deviceModel: string; /** - * Device measure type + * Array of device measure type */ deviceMeasures: string[]; @@ -209,6 +209,21 @@ export abstract class Decoder { } } } + + static serialize(decoders: Map): DecoderConstructor[] { + const decoderList = Array + .from(decoders.keys()) + .map(decoder => { + const { deviceModel, deviceMeasures } = decoders.get(decoder) + + return { + deviceModel, + deviceMeasures + } + }); + + return decoderList; + } } /* eslint-enable @typescript-eslint/no-unused-vars */ diff --git a/lib/core-classes/DecodersService.ts b/lib/core-classes/DecodersService.ts index 0bf715d2..74d6ef05 100644 --- a/lib/core-classes/DecodersService.ts +++ b/lib/core-classes/DecodersService.ts @@ -2,6 +2,7 @@ import { PluginContext, EmbeddedSDK, Plugin } from "kuzzle"; import { Decoder } from "./Decoder"; import { DeviceManagerConfig } from "../DeviceManagerPlugin"; +import { DecoderConstructor } from '../types/DecodersContent'; export class DecodersService { private config: DeviceManagerConfig; @@ -20,18 +21,9 @@ export class DecodersService { this.decoders = decoders; } - async list() { - return { - decoders: Array - .from(this.decoders.keys()) - .map(decoder => { - const { deviceModel, deviceMeasures } = this.decoders.get(decoder) - - return { - deviceModel, - deviceMeasures - } - }) - }; + async list(): Promise { + const decoders = Decoder.serialize(this.decoders); + + return decoders; } } diff --git a/lib/types/DecodersContent.ts b/lib/types/DecodersContent.ts new file mode 100644 index 00000000..52f9cfa1 --- /dev/null +++ b/lib/types/DecodersContent.ts @@ -0,0 +1,4 @@ +export interface DecoderConstructor { + deviceModel: string + deviceMeasures: string[] +} \ No newline at end of file diff --git a/lib/types/index.ts b/lib/types/index.ts index ea6f5524..74bcdb38 100644 --- a/lib/types/index.ts +++ b/lib/types/index.ts @@ -3,3 +3,5 @@ export * from './Measure'; export * from './DeviceContent'; export * from './BaseAssetContent'; + +export * from './DecodersContent'; \ No newline at end of file From a593e9b07e8faa4605cfd32fc182b0695fd034c8 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 26 Oct 2021 17:16:30 +0200 Subject: [PATCH 6/7] add .vscode to list of ignored files --- .gitignore | 2 ++ .vscode/settings.json | 25 ------------------------- 2 files changed, 2 insertions(+), 25 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 20129325..9d94d6f4 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,5 @@ index.js.map # Doc doc/framework frontmatter-errors.json + +.vscode \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 428e3861..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "workbench.colorCustomizations": { - "activityBar.activeBackground": "#2f7c47", - "activityBar.activeBorder": "#422c74", - "activityBar.background": "#2f7c47", - "activityBar.foreground": "#e7e7e7", - "activityBar.inactiveForeground": "#e7e7e799", - "activityBarBadge.background": "#422c74", - "activityBarBadge.foreground": "#e7e7e7", - "editorGroup.border": "#2f7c47", - "panel.border": "#2f7c47", - "sash.hoverBorder": "#2f7c47", - "sideBar.border": "#2f7c47", - "statusBar.background": "#215732", - "statusBar.foreground": "#e7e7e7", - "statusBarItem.hoverBackground": "#2f7c47", - "statusBarItem.remoteBackground": "#215732", - "statusBarItem.remoteForeground": "#e7e7e7", - "titleBar.activeBackground": "#215732", - "titleBar.activeForeground": "#e7e7e7", - "titleBar.inactiveBackground": "#21573299", - "titleBar.inactiveForeground": "#e7e7e799" - }, - "peacock.color": "#215732" -} \ No newline at end of file From ba7485533b8de7d3411597346c74765d02616036 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 27 Oct 2021 14:40:21 +0200 Subject: [PATCH 7/7] docs and smoll improv --- lib/core-classes/Decoder.ts | 1 + lib/core-classes/DecodersService.ts | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/core-classes/Decoder.ts b/lib/core-classes/Decoder.ts index 48766789..808fddcf 100644 --- a/lib/core-classes/Decoder.ts +++ b/lib/core-classes/Decoder.ts @@ -68,6 +68,7 @@ export abstract class Decoder { /** * @param deviceModel Device model for this decoder + * @param deviceMeasures Devices measure types for this decoder */ constructor (deviceModel: string, deviceMeasures: string[]) { this.deviceModel = deviceModel; diff --git a/lib/core-classes/DecodersService.ts b/lib/core-classes/DecodersService.ts index c8c287f7..b4199a9d 100644 --- a/lib/core-classes/DecodersService.ts +++ b/lib/core-classes/DecodersService.ts @@ -23,8 +23,7 @@ export class DecodersService { async list(): Promise { const decoders = Array - .from(this.decoders.keys()) - .map(key => this.decoders.get(key)) + .from(this.decoders.values()) .map(decoder => decoder.serialize()) return decoders;