Skip to content

Commit

Permalink
feat: add log in decoders (#339)
Browse files Browse the repository at this point in the history
* feat: add logger in Decoders

* chore: lint

* refactor: do not use the constructor directly pass the logger in registerDevice

* refacto setup logger in decoders

* untrack custom docker compose file

* fix: this.context is undefined before the init method called in app.start()

* fixed implementation

* made lint happier (hopfully)

---------

Co-authored-by: Eric Trousset <[email protected]>
  • Loading branch information
thomas-mauran and etrousset authored May 23, 2024
1 parent 5cd6aca commit fe08b7c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
11 changes: 11 additions & 0 deletions lib/modules/decoder/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ export type NamedMeasures = Array<{
export abstract class Decoder {
private _http?: HttpRoute[];

/**
* Internal logger.
*/
public log: {
debug: (message: any) => void;
error: (message: any) => void;
info: (message: any) => void;
silly: (message: any) => void;
verbose: (message: any) => void;
warn: (message: any) => void;
};
/**
* Device model name.
*
Expand Down
6 changes: 5 additions & 1 deletion lib/modules/plugin/DeviceManagerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export class DeviceManagerPlugin extends Plugin {
*/
registerDevice: (model: string, definition: DeviceModelDefinition) => {
this.decodersRegister.register(definition.decoder);
definition.decoder.log = this.context?.log;

this.modelsRegister.registerDevice(
model,
Expand Down Expand Up @@ -234,7 +235,6 @@ export class DeviceManagerPlugin extends Plugin {
});

/* eslint-disable sort-keys */

this.api = {};
this.pipes = {
"generic:document:beforeWrite": [],
Expand Down Expand Up @@ -314,6 +314,10 @@ export class DeviceManagerPlugin extends Plugin {
this.config = _.merge({}, this.config, config);
this.context = context;

for (const decoder of this.decodersRegister.decoders) {
decoder.log = this.context.log;
}

// Modules creation
this.assetModule = new AssetModule(this);
this.deviceModule = new DeviceModule(this);
Expand Down
28 changes: 16 additions & 12 deletions tests/application/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@ deviceManager.config.engineCollections.device.mappings.properties["softTenant"]
};


deviceManager.models.registerDevice("DummyTempPosition", {
decoder: new DummyTempPositionDecoder(),
metadataMappings: {
serial: { type: "keyword" },
},
});
deviceManager.models.registerDevice("DummyTempPosition",
{
decoder: new DummyTempPositionDecoder(),
metadataMappings: {
serial: { type: "keyword" },
},
}
);

deviceManager.models.registerDevice("DummyTemp", {
decoder: new DummyTempDecoder(),
metadataMappings: {
color: { type: "keyword" },
},
});
deviceManager.models.registerDevice("DummyTemp",
{
decoder: new DummyTempDecoder(),
metadataMappings: {
color: { type: "keyword" },
},
}
);

// Register an asset for the "commons" group

Expand Down
1 change: 1 addition & 0 deletions tests/application/decoders/DummyTempDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class DummyTempDecoder extends Decoder {
decodedPayload: DecodedPayload<DummyTempDecoder>,
rawPayload: JSONObject,
): Promise<DecodedPayload<DummyTempDecoder>> {
this.log.info("Decoding payload");
const payloads: any[] = rawPayload.measurements ?? [rawPayload];

for (const payload of payloads) {
Expand Down
4 changes: 4 additions & 0 deletions tests/application/decoders/DummyTempPositionDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export class DummyTempPositionDecoder extends Decoder {
{ name: "position", type: "position" },
] as const;

constructor() {
super();
}

async validate(payload: JSONObject) {
if (payload.deviceEUI === undefined) {
throw new PreconditionError('Invalid payload: missing "deviceEUI"');
Expand Down

0 comments on commit fe08b7c

Please sign in to comment.