Skip to content

Commit

Permalink
refacto(deviceService): move decoders in the DeviceService
Browse files Browse the repository at this point in the history
  • Loading branch information
Aschen committed Apr 14, 2021
1 parent c1e7e68 commit 9941b30
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/DeviceManagerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ export class DeviceManagerPlugin extends Plugin {

this.engineService = new EngineService(this.config, context);
this.payloadService = new PayloadService(this.config, context);
this.deviceService = new DeviceService(this.config, context);
this.deviceService = new DeviceService(this.config, context, this.decoders);
this.assetController = new AssetController(this.config, context);
this.engineController = new EngineController(this.config, context, this.engineService);
this.deviceController = new DeviceController(this.config, context, this.decoders, this.deviceService);
this.deviceController = new DeviceController(this.config, context, this.deviceService);

this.api['device-manager/asset'] = this.assetController.definition;
this.api['device-manager/device'] = this.deviceController.definition;
Expand Down
80 changes: 65 additions & 15 deletions lib/controllers/DeviceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ import {
} from 'kuzzle';

import { CRUDController } from './CRUDController';
import { Decoder } from '../decoders';
import { Device } from '../models';
import { DeviceBulkContent } from '../types';
import { DeviceService } from '../services';

export class DeviceController extends CRUDController {
private decoders: Map<string, Decoder>;
private deviceService: DeviceService;

get sdk(): EmbeddedSDK {
return this.context.accessors.sdk;
}

constructor(config: JSONObject, context: PluginContext, decoders: Map<string, Decoder>, deviceService: DeviceService) {
constructor(
config: JSONObject,
context: PluginContext,
deviceService: DeviceService
) {
super(config, context, 'devices');

this.decoders = decoders;

this.deviceService = deviceService;

this.definition = {
Expand Down Expand Up @@ -91,7 +91,13 @@ export class DeviceController extends CRUDController {
const document = { tenantId: tenantId, deviceId: deviceId };
const devices = await this.mGetDevice([document]);

await this.deviceService.mAttach(devices, [document], { strict: true, options: { ...request.input.args } });
await this.deviceService.mAttach(
devices,
[document],
{
strict: true,
options: { ...request.input.args }
});
}

/**
Expand All @@ -102,7 +108,13 @@ export class DeviceController extends CRUDController {

const devices = await this.mGetDevice(bulkData);

return this.deviceService.mAttach(devices, bulkData, { strict, options: { ...request.input.args } });
return this.deviceService.mAttach(
devices,
bulkData,
{
strict,
options: { ...request.input.args }
});
}

/**
Expand All @@ -114,18 +126,30 @@ export class DeviceController extends CRUDController {
const document: DeviceBulkContent = { deviceId };
const devices = await this.mGetDevice([document]);

await this.deviceService.mDetach(devices, [document], { strict: true, options: { ...request.input.args } });
await this.deviceService.mDetach(
devices,
[document],
{
strict: true,
options: { ...request.input.args }
});
}

/**
* Unattach multiple devices from multiple tenants
* Detach multiple devices from multiple tenants
*/
async mDetach (request: KuzzleRequest) {
const { bulkData, strict } = await this.mParseRequest(request);

const devices = await this.mGetDevice(bulkData);

return this.deviceService.mDetach(devices, bulkData, { strict, options: { ...request.input.args } });
return this.deviceService.mDetach(
devices,
bulkData,
{
strict,
options: { ...request.input.args }
});
}

/**
Expand All @@ -138,7 +162,13 @@ export class DeviceController extends CRUDController {
const document: DeviceBulkContent = { deviceId, assetId };
const devices = await this.mGetDevice([document]);

await this.deviceService.mLink(devices, [document], this.decoders, { strict: true, options: { ...request.input.args } });
await this.deviceService.mLink(
devices,
[document],
{
strict: true,
options: { ...request.input.args }
});
}

/**
Expand All @@ -149,7 +179,13 @@ export class DeviceController extends CRUDController {

const devices = await this.mGetDevice(bulkData);

return this.deviceService.mLink(devices, bulkData, this.decoders, { strict, options: { ...request.input.args } });
return this.deviceService.mLink(
devices,
bulkData,
{
strict,
options: { ...request.input.args }
});
}

/**
Expand All @@ -161,7 +197,12 @@ export class DeviceController extends CRUDController {
const document: DeviceBulkContent = { deviceId };
const devices = await this.mGetDevice([document]);

await this.deviceService.mUnlink(devices, { strict: true, options: { ...request.input.args } });
await this.deviceService.mUnlink(
devices,
{
strict: true,
options: { ...request.input.args }
});
}

/**
Expand All @@ -172,7 +213,12 @@ export class DeviceController extends CRUDController {

const devices = await this.mGetDevice(bulkData);

return this.deviceService.mUnlink(devices, { strict, options: { ...request.input.args } });
return this.deviceService.mUnlink(
devices,
{
strict,
options: { ...request.input.args }
});
}

/**
Expand Down Expand Up @@ -224,7 +270,11 @@ export class DeviceController extends CRUDController {
const lines = await csv({ delimiter: 'auto' })
.fromString(body.csv);

bulkData = lines.map(line => ({ tenantId: line.tenantId, deviceId: line.deviceId, assetId: line.assetId }));
bulkData = lines.map(({ tenantId, deviceId, assetId}) => ({
tenantId,
deviceId,
assetId
}));
}
else if (body.records) {
bulkData = body.records;
Expand Down
9 changes: 6 additions & 3 deletions lib/services/DeviceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ export class DeviceService {
private config: JSONObject;
private context: PluginContext;

private decoders: Map<string, Decoder>;

get sdk(): EmbeddedSDK {
return this.context.accessors.sdk;
}

constructor(config: JSONObject, context: PluginContext) {
constructor(config: JSONObject, context: PluginContext, decoders: Map<string, Decoder>) {
this.config = config;
this.context = context;

this.decoders = decoders;
}

async mAttach (
Expand Down Expand Up @@ -164,7 +168,6 @@ export class DeviceService {
async mLink (
devices: Device[],
bulkData: DeviceBulkContent[],
decoders: Map<string, Decoder>,
{ strict, options }: { strict?: boolean, options?: JSONObject }
) {
const detachedDevices = devices.filter(device => {
Expand Down Expand Up @@ -205,7 +208,7 @@ export class DeviceService {
const assetDocuments = [];

for (const device of devicesContent) {
const decoder = decoders.get(device._source.model);
const decoder = this.decoders.get(device._source.model);
const measures = await decoder.copyToAsset(device);
const { assetId } = bulkData.find(data => data.deviceId === device._id)

Expand Down

0 comments on commit 9941b30

Please sign in to comment.