Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to list all registered decoders by the plugin #114

Merged
merged 9 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/1/classes/decoder/constructor/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions doc/1/guides/decoders/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeviceContent> {
Expand Down Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion features/DecodersController.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"}] |
| decoders | [{"deviceModel":"DummyTemp","deviceMeasures":["temperature"]},{"deviceModel":"DummyTempPosition","deviceMeasures":["temperature","position"]}] |
2 changes: 1 addition & 1 deletion features/fixtures/application/decoders/DummyTempDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 10 additions & 3 deletions lib/core-classes/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
HttpRoute,
PreconditionError,
} from 'kuzzle';
import _ from 'lodash';

import has from 'lodash/has';

import { Device, BaseAsset } from '../models';

Expand All @@ -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
*/
Expand Down Expand Up @@ -63,8 +69,9 @@ export abstract class Decoder {
/**
* @param deviceModel Device model for this decoder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments should be updated as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this hasn't changed 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was talking about the constructor now takes 2 params instead of one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, i didn't understud it that way ! i'll change that with the last Nitpiking you added

*/
constructor (deviceModel: string) {
constructor (deviceModel: string, deviceMeasures: string[]) {
this.deviceModel = deviceModel;
this.deviceMeasures = deviceMeasures;
}

/**
Expand Down Expand Up @@ -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`);
}
}
Expand Down
9 changes: 8 additions & 1 deletion lib/core-classes/DecodersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Aschen marked this conversation as resolved.
Show resolved Hide resolved

return {
deviceModel,
deviceMeasures
}
})
};
}
}