Skip to content

Commit

Permalink
feat(digitalTwin): add support modify mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
sebtiz13 committed Oct 30, 2023
1 parent 2ff4d61 commit 8430b73
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 15 deletions.
4 changes: 3 additions & 1 deletion lib/modules/device/collections/deviceMappings.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { CollectionMappings } from "kuzzle";

/**
* Base mappings for the "devices" collection.
*
* Those mappings does not contains the `measures` and `metadata` mappings.
*/
export const devicesMappings = {
export const devicesMappings: CollectionMappings = {
dynamic: "strict",
properties: {
model: {
Expand Down
20 changes: 8 additions & 12 deletions lib/modules/plugin/DeviceManagerEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,20 @@ import { JSONObject } from "kuzzle-sdk";
import { AbstractEngine, ConfigManager } from "kuzzle-plugin-commons";
import { EngineContent } from "kuzzle-plugin-commons";

import {
assetsMappings,
assetsHistoryMappings,
assetGroupsMappings,
} from "../asset";
import { assetsHistoryMappings, assetGroupsMappings } from "../asset";
import {
AssetModelContent,
DeviceModelContent,
MeasureModelContent,
} from "../model";
import { getEmbeddedMeasureMappings, measuresMappings } from "../measure";
import { devicesMappings } from "../device";
import { onAsk } from "../shared";
import { NamedMeasures } from "../decoder";

import { DeviceManagerConfiguration } from "./types/DeviceManagerConfiguration";
import { DeviceManagerPlugin } from "./DeviceManagerPlugin";
import { InternalCollection } from "./types/InternalCollection";

const digitalTwinMappings = {
asset: assetsMappings,
device: devicesMappings,
} as const;

export type AskEngineList = {
name: "ask:device-manager:engine:list";

Expand Down Expand Up @@ -211,6 +201,12 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {
private async getDigitalTwinMappings<
TDigitalTwinModelContent extends AssetModelContent | DeviceModelContent
>(digitalTwinType: "asset" | "device", engineGroup?: string) {
if (
this.config.engineCollections[digitalTwinType] === undefined ||
this.config.engineCollections[digitalTwinType].mappings === undefined
) {
throw new InternalError(`Cannot find mapping for "${digitalTwinType}"`);
}
const models = await this.getModels<TDigitalTwinModelContent>(
this.config.adminIndex,
digitalTwinType,
Expand All @@ -223,7 +219,7 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {
);

const mappings = JSON.parse(
JSON.stringify(digitalTwinMappings[digitalTwinType])
JSON.stringify(this.config.engineCollections[digitalTwinType].mappings)
);

for (const model of models) {
Expand Down
10 changes: 9 additions & 1 deletion lib/modules/plugin/DeviceManagerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {

import { DeviceModule, devicesMappings } from "../device";
import { MeasureModule } from "../measure";
import { AssetModule } from "../asset";
import { AssetModule, assetsMappings } from "../asset";
import {
DecoderModule,
NamedMeasures,
Expand Down Expand Up @@ -217,6 +217,14 @@ export class DeviceManagerPlugin extends Plugin {
},
settings: {},
},
asset: {
name: InternalCollection.ASSETS,
mappings: assetsMappings,
},
device: {
name: InternalCollection.DEVICES,
mappings: devicesMappings,
},
},
};
/* eslint-enable sort-keys */
Expand Down
10 changes: 9 additions & 1 deletion lib/modules/plugin/types/DeviceManagerConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSONObject } from "kuzzle-sdk";
import { CollectionMappings, JSONObject } from "kuzzle-sdk";

export type DeviceManagerConfiguration = {
/**
Expand Down Expand Up @@ -52,5 +52,13 @@ export type DeviceManagerConfiguration = {
mappings: JSONObject;
settings: JSONObject;
};
asset: {
name: string;
mappings: CollectionMappings;
};
device: {
name: string;
mappings: CollectionMappings;
};
};
};
11 changes: 11 additions & 0 deletions tests/application/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ const app = new Backend("kuzzle");

const deviceManager = new DeviceManagerPlugin();

//? Add custom mapping properties
deviceManager.config.engineCollections.asset.mappings.properties["softTenant"] = {
type: "keyword",
fields: { text: { type: "text" } },
};
deviceManager.config.engineCollections.device.mappings.properties["softTenant"] = {
type: "keyword",
fields: { text: { type: "text" } },
};


deviceManager.models.registerDevice("DummyTempPosition", {
decoder: new DummyTempPositionDecoder(),
metadataMappings: {
Expand Down
43 changes: 43 additions & 0 deletions tests/scenario/custom-mapping.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { JSONObject } from "kuzzle-sdk";
import { useSdk } from "../helpers";
import { beforeAllCreateEngines } from "../hooks/engines";

describe("Assets mapping", () => {
const sdk = useSdk();

beforeAll(async () => {
await sdk.connect();

await beforeAllCreateEngines(sdk);
});

afterAll(async () => {
sdk.disconnect();
});

it("asset mapping can be customize by application", async () => {
const mapping: JSONObject = await sdk.collection.getMapping(
"engine-ayse",
"assets"
);

expect(mapping.properties?.softTenant).toBeDefined();
expect(mapping.properties.softTenant).toMatchObject({
type: "keyword",
fields: { text: { type: "text" } },
});
});

it("device mapping can be customize by application", async () => {
const mapping: JSONObject = await sdk.collection.getMapping(
"engine-ayse",
"devices"
);

expect(mapping.properties?.softTenant).toBeDefined();
expect(mapping.properties.softTenant).toMatchObject({
type: "keyword",
fields: { text: { type: "text" } },
});
});
});

0 comments on commit 8430b73

Please sign in to comment.