kuzzle-device-manager 0.4.13 → 2.0.0-rc3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +1 -0
- package/index.js +6 -1
- package/lib/DeviceManagerPlugin.d.ts +33 -65
- package/lib/DeviceManagerPlugin.js +142 -131
- package/lib/controllers/AssetController.d.ts +20 -5
- package/lib/controllers/AssetController.js +78 -23
- package/lib/controllers/DecodersController.d.ts +16 -0
- package/lib/controllers/DecodersController.js +27 -0
- package/lib/controllers/DeviceController.d.ts +25 -22
- package/lib/controllers/DeviceController.js +150 -109
- package/lib/controllers/index.d.ts +1 -1
- package/lib/controllers/index.js +6 -2
- package/lib/core-classes/AssetService.d.ts +31 -0
- package/lib/core-classes/AssetService.js +82 -0
- package/lib/core-classes/Decoder.d.ts +14 -59
- package/lib/core-classes/Decoder.js +16 -86
- package/lib/core-classes/DeviceManagerEngine.d.ts +11 -9
- package/lib/core-classes/DeviceManagerEngine.js +20 -32
- package/lib/core-classes/DeviceService.d.ts +81 -24
- package/lib/core-classes/DeviceService.js +178 -220
- package/lib/core-classes/PayloadService.d.ts +39 -26
- package/lib/core-classes/PayloadService.js +132 -124
- package/lib/core-classes/index.d.ts +5 -4
- package/lib/core-classes/index.js +10 -5
- package/lib/core-classes/{CustomMappings/AssetMappingsManager.d.ts → registers/AssetsRegister.d.ts} +8 -9
- package/lib/core-classes/{CustomMappings/AssetMappingsManager.js → registers/AssetsRegister.js} +17 -28
- package/lib/core-classes/registers/DecodersRegister.d.ts +46 -0
- package/lib/core-classes/registers/DecodersRegister.js +167 -0
- package/lib/core-classes/registers/DevicesRegister.d.ts +20 -0
- package/lib/core-classes/registers/DevicesRegister.js +37 -0
- package/lib/core-classes/registers/MeasuresRegister.d.ts +29 -0
- package/lib/core-classes/registers/MeasuresRegister.js +53 -0
- package/lib/mappings/assetsMappings.d.ts +36 -0
- package/lib/mappings/assetsMappings.js +36 -0
- package/lib/mappings/deviceMappings.d.ts +44 -0
- package/lib/mappings/deviceMappings.js +42 -0
- package/lib/mappings/index.d.ts +4 -0
- package/lib/mappings/index.js +21 -0
- package/lib/mappings/measuresMappings.d.ts +53 -0
- package/lib/mappings/measuresMappings.js +48 -0
- package/lib/mappings/payloadsMappings.d.ts +18 -0
- package/lib/mappings/payloadsMappings.js +16 -0
- package/lib/measures/BatteryMeasure.d.ts +7 -0
- package/lib/measures/BatteryMeasure.js +12 -0
- package/lib/measures/HumidityMeasure.d.ts +7 -0
- package/lib/measures/HumidityMeasure.js +12 -0
- package/lib/measures/MovementMeasure.d.ts +7 -0
- package/lib/measures/MovementMeasure.js +12 -0
- package/lib/measures/PositionMeasure.d.ts +12 -0
- package/lib/measures/PositionMeasure.js +16 -0
- package/lib/measures/TemperatureMeasure.d.ts +7 -0
- package/lib/measures/TemperatureMeasure.js +12 -0
- package/lib/measures/index.d.ts +5 -0
- package/lib/measures/index.js +22 -0
- package/lib/models/BaseAsset.d.ts +1 -48
- package/lib/models/BaseAsset.js +5 -43
- package/lib/models/Config.d.ts +1 -13
- package/lib/models/Device.d.ts +5 -96
- package/lib/models/Device.js +5 -73
- package/lib/models/index.d.ts +0 -1
- package/lib/models/index.js +5 -2
- package/lib/types/BaseAssetContent.d.ts +8 -3
- package/lib/types/DecodedPayload.d.ts +42 -0
- package/lib/types/{AssetsHistoryContent.js → DecodedPayload.js} +1 -1
- package/lib/types/DecoderContent.d.ts +4 -0
- package/lib/types/DecoderContent.js +3 -0
- package/lib/types/DeviceContent.d.ts +12 -44
- package/lib/types/DeviceManagerConfiguration.d.ts +40 -0
- package/lib/types/DeviceManagerConfiguration.js +3 -0
- package/lib/types/index.d.ts +7 -2
- package/lib/types/index.js +12 -3
- package/lib/types/measures/MeasureContent.d.ts +56 -0
- package/lib/types/measures/MeasureContent.js +3 -0
- package/lib/types/measures/MeasureDefinition.d.ts +40 -0
- package/lib/types/measures/MeasureDefinition.js +3 -0
- package/lib/types/measures/Measurement.d.ts +19 -0
- package/lib/types/{Measure.js → measures/Measurement.js} +1 -1
- package/lib/utils/index.d.ts +1 -1
- package/lib/utils/index.js +6 -2
- package/lib/utils/writeMany.d.ts +13 -0
- package/lib/utils/writeMany.js +37 -0
- package/package.json +14 -18
- package/lib/controllers/CRUDController.d.ts +0 -34
- package/lib/controllers/CRUDController.js +0 -65
- package/lib/core-classes/BatchProcessing/BatchController.d.ts +0 -34
- package/lib/core-classes/BatchProcessing/BatchController.js +0 -115
- package/lib/core-classes/BatchProcessing/BatchWriter.d.ts +0 -110
- package/lib/core-classes/BatchProcessing/BatchWriter.js +0 -264
- package/lib/core-classes/BatchProcessing/InstrumentablePromise.d.ts +0 -6
- package/lib/core-classes/BatchProcessing/InstrumentablePromise.js +0 -13
- package/lib/core-classes/BatchProcessing/index.d.ts +0 -3
- package/lib/core-classes/BatchProcessing/index.js +0 -16
- package/lib/core-classes/CustomMappings/DeviceMappingsManager.d.ts +0 -43
- package/lib/core-classes/CustomMappings/DeviceMappingsManager.js +0 -71
- package/lib/core-classes/MigrationService.d.ts +0 -13
- package/lib/core-classes/MigrationService.js +0 -49
- package/lib/models/Catalog.d.ts +0 -55
- package/lib/models/Catalog.js +0 -38
- package/lib/types/AssetsHistoryContent.d.ts +0 -9
- package/lib/types/Measure.d.ts +0 -54
- package/lib/utils/events.d.ts +0 -8
- package/lib/utils/events.js +0 -17
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ControllerDefinition } from 'kuzzle';
|
|
2
|
+
import { DeviceManagerPlugin } from '../DeviceManagerPlugin';
|
|
3
|
+
import { DecodersRegister } from '../core-classes';
|
|
4
|
+
export declare class DecodersController {
|
|
5
|
+
private context;
|
|
6
|
+
private config;
|
|
7
|
+
private decodersRegister;
|
|
8
|
+
definition: ControllerDefinition;
|
|
9
|
+
constructor(plugin: DeviceManagerPlugin, decodersRegister: DecodersRegister);
|
|
10
|
+
/**
|
|
11
|
+
* List all available decoders
|
|
12
|
+
*/
|
|
13
|
+
list(): Promise<{
|
|
14
|
+
decoders: import("../types").DecoderContent[];
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DecodersController = void 0;
|
|
4
|
+
class DecodersController {
|
|
5
|
+
constructor(plugin, decodersRegister) {
|
|
6
|
+
this.context = plugin.context;
|
|
7
|
+
this.config = plugin.config;
|
|
8
|
+
this.decodersRegister = decodersRegister;
|
|
9
|
+
this.definition = {
|
|
10
|
+
actions: {
|
|
11
|
+
list: {
|
|
12
|
+
handler: this.list.bind(this),
|
|
13
|
+
http: [{ path: 'device-manager/decoders', verb: 'get' }],
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* List all available decoders
|
|
20
|
+
*/
|
|
21
|
+
async list() {
|
|
22
|
+
const decoders = await this.decodersRegister.list();
|
|
23
|
+
return { decoders };
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.DecodersController = DecodersController;
|
|
27
|
+
//# sourceMappingURL=DecodersController.js.map
|
|
@@ -1,56 +1,59 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { CRUDController } from 'kuzzle-plugin-commons';
|
|
2
|
+
import { KuzzleRequest } from 'kuzzle';
|
|
3
3
|
import { DeviceService } from '../core-classes';
|
|
4
|
+
import { DeviceManagerPlugin } from '../DeviceManagerPlugin';
|
|
5
|
+
import { DeviceContent, DeviceManagerConfiguration } from '../types';
|
|
4
6
|
export declare class DeviceController extends CRUDController {
|
|
7
|
+
protected config: DeviceManagerConfiguration;
|
|
5
8
|
private deviceService;
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
constructor(plugin: DeviceManagerPlugin, deviceService: DeviceService);
|
|
10
|
+
/**
|
|
11
|
+
* Create and provision a new device
|
|
12
|
+
*/
|
|
13
|
+
create(request: KuzzleRequest): Promise<import("kuzzle").KDocument<DeviceContent>>;
|
|
14
|
+
update(): Promise<any>;
|
|
15
|
+
search(): Promise<void>;
|
|
16
|
+
delete(): Promise<any>;
|
|
8
17
|
/**
|
|
9
18
|
* Attach a device to a tenant
|
|
10
19
|
*/
|
|
11
|
-
|
|
20
|
+
attachEngine(request: KuzzleRequest): Promise<void>;
|
|
12
21
|
/**
|
|
13
22
|
* Attach multiple devices to multiple tenants
|
|
14
23
|
*/
|
|
15
|
-
|
|
24
|
+
mAttachEngines(request: KuzzleRequest): Promise<any[]>;
|
|
16
25
|
/**
|
|
17
26
|
* Unattach a device from it's tenant
|
|
18
27
|
*/
|
|
19
|
-
|
|
28
|
+
detachEngine(request: KuzzleRequest): Promise<void>;
|
|
20
29
|
/**
|
|
21
30
|
* Detach multiple devices from multiple tenants
|
|
22
31
|
*/
|
|
23
|
-
|
|
24
|
-
errors: any[];
|
|
25
|
-
successes: any[];
|
|
26
|
-
}>;
|
|
32
|
+
mDetachEngines(request: KuzzleRequest): Promise<any[]>;
|
|
27
33
|
/**
|
|
28
34
|
* Link a device to an asset.
|
|
29
|
-
* @todo there is
|
|
35
|
+
* @todo there is no restriction according to tenant index?
|
|
30
36
|
*/
|
|
31
37
|
linkAsset(request: KuzzleRequest): Promise<void>;
|
|
32
38
|
/**
|
|
33
39
|
* Link multiple devices to multiple assets.
|
|
34
40
|
*/
|
|
35
|
-
|
|
36
|
-
errors: any[];
|
|
37
|
-
successes: any[];
|
|
38
|
-
}>;
|
|
41
|
+
mLinkAssets(request: KuzzleRequest): Promise<void>;
|
|
39
42
|
/**
|
|
40
43
|
* Unlink a device from an asset.
|
|
41
44
|
*/
|
|
42
|
-
|
|
45
|
+
unlinkAsset(request: KuzzleRequest): Promise<void>;
|
|
43
46
|
/**
|
|
44
47
|
* Unlink multiple device from multiple assets.
|
|
45
48
|
*/
|
|
46
|
-
|
|
47
|
-
errors: any[];
|
|
48
|
-
successes: any[];
|
|
49
|
-
}>;
|
|
49
|
+
mUnlinkAssets(request: KuzzleRequest): Promise<void>;
|
|
50
50
|
/**
|
|
51
51
|
* Clean payload collection for a time period
|
|
52
52
|
*/
|
|
53
53
|
prunePayloads(request: KuzzleRequest): Promise<any>;
|
|
54
|
-
|
|
54
|
+
importDevices(request: KuzzleRequest): Promise<{
|
|
55
|
+
errors: any[];
|
|
56
|
+
successes: any[];
|
|
57
|
+
}>;
|
|
55
58
|
private mParseRequest;
|
|
56
59
|
}
|
|
@@ -5,162 +5,200 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.DeviceController = void 0;
|
|
7
7
|
const csvtojson_1 = __importDefault(require("csvtojson"));
|
|
8
|
+
const kuzzle_plugin_commons_1 = require("kuzzle-plugin-commons");
|
|
8
9
|
const kuzzle_1 = require("kuzzle");
|
|
9
|
-
|
|
10
|
-
const models_1 = require("../models");
|
|
11
|
-
class DeviceController extends CRUDController_1.CRUDController {
|
|
10
|
+
class DeviceController extends kuzzle_plugin_commons_1.CRUDController {
|
|
12
11
|
constructor(plugin, deviceService) {
|
|
13
12
|
super(plugin, 'devices');
|
|
14
13
|
this.deviceService = deviceService;
|
|
14
|
+
/* eslint-disable sort-keys */
|
|
15
15
|
this.definition = {
|
|
16
16
|
actions: {
|
|
17
|
-
|
|
18
|
-
handler: this.
|
|
19
|
-
http: [{
|
|
17
|
+
attachEngine: {
|
|
18
|
+
handler: this.attachEngine.bind(this),
|
|
19
|
+
http: [{ path: 'device-manager/:index/devices/:_id/_attach', verb: 'put' }]
|
|
20
20
|
},
|
|
21
|
-
|
|
22
|
-
handler: this.
|
|
23
|
-
http: [
|
|
24
|
-
{ verb: 'post', path: 'device-manager/:index/devices/_search' },
|
|
25
|
-
{ verb: 'get', path: 'device-manager/:index/devices/_search' }
|
|
26
|
-
]
|
|
21
|
+
detachEngine: {
|
|
22
|
+
handler: this.detachEngine.bind(this),
|
|
23
|
+
http: [{ path: 'device-manager/devices/:_id/_detach', verb: 'delete' }]
|
|
27
24
|
},
|
|
28
|
-
|
|
29
|
-
handler: this.
|
|
30
|
-
http: [{
|
|
31
|
-
},
|
|
32
|
-
mAttach: {
|
|
33
|
-
handler: this.mAttach.bind(this),
|
|
34
|
-
http: [{ verb: 'put', path: 'device-manager/devices/_mAttach' }]
|
|
35
|
-
},
|
|
36
|
-
detach: {
|
|
37
|
-
handler: this.detach.bind(this),
|
|
38
|
-
http: [{ verb: 'delete', path: 'device-manager/devices/:_id/_detach' }]
|
|
39
|
-
},
|
|
40
|
-
mDetach: {
|
|
41
|
-
handler: this.mDetach.bind(this),
|
|
42
|
-
http: [{ verb: 'put', path: 'device-manager/devices/_mDetach' }]
|
|
25
|
+
importDevices: {
|
|
26
|
+
handler: this.importDevices.bind(this),
|
|
27
|
+
http: [{ path: 'device-manager/devices/_import', verb: 'post' }]
|
|
43
28
|
},
|
|
44
29
|
linkAsset: {
|
|
45
30
|
handler: this.linkAsset.bind(this),
|
|
46
|
-
http: [{
|
|
31
|
+
http: [{ path: 'device-manager/:index/devices/:_id/_link/:assetId', verb: 'put' }]
|
|
47
32
|
},
|
|
48
|
-
|
|
49
|
-
handler: this.
|
|
50
|
-
http: [{
|
|
33
|
+
mAttachEngines: {
|
|
34
|
+
handler: this.mAttachEngines.bind(this),
|
|
35
|
+
http: [{ path: 'device-manager/devices/_mAttach', verb: 'put' }]
|
|
51
36
|
},
|
|
52
|
-
|
|
53
|
-
handler: this.
|
|
54
|
-
http: [{
|
|
37
|
+
mDetachEngines: {
|
|
38
|
+
handler: this.mDetachEngines.bind(this),
|
|
39
|
+
http: [{ path: 'device-manager/devices/_mDetach', verb: 'put' }]
|
|
55
40
|
},
|
|
56
|
-
|
|
57
|
-
handler: this.
|
|
58
|
-
http: [{
|
|
41
|
+
mLinkAssets: {
|
|
42
|
+
handler: this.mLinkAssets.bind(this),
|
|
43
|
+
http: [{ path: 'device-manager/devices/_mLink', verb: 'put' }]
|
|
44
|
+
},
|
|
45
|
+
mUnlinkAssets: {
|
|
46
|
+
handler: this.mUnlinkAssets.bind(this),
|
|
47
|
+
http: [{ path: 'device-manager/devices/_mUnlink', verb: 'put' }]
|
|
59
48
|
},
|
|
60
49
|
prunePayloads: {
|
|
61
50
|
handler: this.prunePayloads.bind(this),
|
|
62
|
-
http: [{
|
|
51
|
+
http: [{ path: 'device-manager/devices/_prunePayloads', verb: 'delete' }]
|
|
52
|
+
},
|
|
53
|
+
unlinkAsset: {
|
|
54
|
+
handler: this.unlinkAsset.bind(this),
|
|
55
|
+
http: [{ path: 'device-manager/:index/devices/:_id/_unlink', verb: 'delete' }]
|
|
56
|
+
},
|
|
57
|
+
// CRUD Controller
|
|
58
|
+
create: {
|
|
59
|
+
handler: this.create.bind(this),
|
|
60
|
+
http: [{ path: 'device-manager/:index/devices', verb: 'post' }]
|
|
63
61
|
},
|
|
64
62
|
}
|
|
65
63
|
};
|
|
64
|
+
/* eslint-enable sort-keys */
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Create and provision a new device
|
|
68
|
+
*/
|
|
69
|
+
async create(request) {
|
|
70
|
+
const engineId = request.getString('engineId');
|
|
71
|
+
const model = request.getBodyString('model');
|
|
72
|
+
const reference = request.getBodyString('reference');
|
|
73
|
+
const assetId = request.input.args.assetId || null;
|
|
74
|
+
const metadata = request.getBodyObject('metadata', {});
|
|
75
|
+
const refresh = request.getRefresh();
|
|
76
|
+
const deviceContent = {
|
|
77
|
+
measures: [],
|
|
78
|
+
metadata,
|
|
79
|
+
model,
|
|
80
|
+
reference,
|
|
81
|
+
};
|
|
82
|
+
const device = await this.deviceService.create(engineId, deviceContent, assetId, {
|
|
83
|
+
refresh
|
|
84
|
+
});
|
|
85
|
+
return device;
|
|
86
|
+
}
|
|
87
|
+
// @todo to be implemented
|
|
88
|
+
async update() {
|
|
89
|
+
throw new kuzzle_1.PluginImplementationError('Not available');
|
|
66
90
|
}
|
|
67
|
-
|
|
68
|
-
|
|
91
|
+
// @todo to be implemented
|
|
92
|
+
async search() {
|
|
93
|
+
throw new kuzzle_1.PluginImplementationError('Not available');
|
|
94
|
+
}
|
|
95
|
+
// @todo to be implemented
|
|
96
|
+
async delete() {
|
|
97
|
+
throw new kuzzle_1.PluginImplementationError('Not available');
|
|
69
98
|
}
|
|
70
99
|
/**
|
|
71
100
|
* Attach a device to a tenant
|
|
72
101
|
*/
|
|
73
|
-
async
|
|
74
|
-
const
|
|
102
|
+
async attachEngine(request) {
|
|
103
|
+
const engineId = request.getString('engineId');
|
|
75
104
|
const deviceId = request.getId();
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
105
|
+
const refresh = request.getRefresh();
|
|
106
|
+
const strict = request.getBoolean('strict');
|
|
107
|
+
const attacheRequest = {
|
|
108
|
+
deviceId,
|
|
109
|
+
engineId,
|
|
110
|
+
};
|
|
111
|
+
await this.deviceService.attachEngine(attacheRequest, { refresh, strict });
|
|
82
112
|
}
|
|
83
113
|
/**
|
|
84
114
|
* Attach multiple devices to multiple tenants
|
|
85
115
|
*/
|
|
86
|
-
async
|
|
87
|
-
const { bulkData
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
116
|
+
async mAttachEngines(request) {
|
|
117
|
+
const { bulkData } = await this.mParseRequest(request);
|
|
118
|
+
const refresh = request.getRefresh();
|
|
119
|
+
const strict = request.getBoolean('strict');
|
|
120
|
+
const promises = [];
|
|
121
|
+
for (const { engineId, deviceId } of bulkData) {
|
|
122
|
+
const attacheRequest = {
|
|
123
|
+
deviceId,
|
|
124
|
+
engineId,
|
|
125
|
+
};
|
|
126
|
+
promises.push(this.deviceService.attachEngine(attacheRequest, { refresh, strict }));
|
|
127
|
+
}
|
|
128
|
+
return await Promise.all(promises);
|
|
93
129
|
}
|
|
94
130
|
/**
|
|
95
131
|
* Unattach a device from it's tenant
|
|
96
132
|
*/
|
|
97
|
-
async
|
|
133
|
+
async detachEngine(request) {
|
|
98
134
|
const deviceId = request.getId();
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
await this.deviceService.
|
|
102
|
-
strict: true,
|
|
103
|
-
options: { ...request.input.args }
|
|
104
|
-
});
|
|
135
|
+
const refresh = request.getRefresh();
|
|
136
|
+
const strict = request.getBoolean('strict');
|
|
137
|
+
await this.deviceService.detachEngine(deviceId, { refresh, strict });
|
|
105
138
|
}
|
|
106
139
|
/**
|
|
107
140
|
* Detach multiple devices from multiple tenants
|
|
108
141
|
*/
|
|
109
|
-
async
|
|
110
|
-
const { bulkData
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
142
|
+
async mDetachEngines(request) {
|
|
143
|
+
const { bulkData } = await this.mParseRequest(request);
|
|
144
|
+
const promises = [];
|
|
145
|
+
for (const { deviceId } of bulkData) {
|
|
146
|
+
promises.push(this.deviceService.detachEngine(deviceId, request.input.args));
|
|
147
|
+
}
|
|
148
|
+
return await Promise.all(promises);
|
|
116
149
|
}
|
|
117
150
|
/**
|
|
118
151
|
* Link a device to an asset.
|
|
119
|
-
* @todo there is
|
|
152
|
+
* @todo there is no restriction according to tenant index?
|
|
120
153
|
*/
|
|
121
154
|
async linkAsset(request) {
|
|
122
155
|
const assetId = request.getString('assetId');
|
|
123
156
|
const deviceId = request.getId();
|
|
124
|
-
const
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
157
|
+
const measuresNames = request.getBodyObject('measuresNames', {});
|
|
158
|
+
const refresh = request.getRefresh();
|
|
159
|
+
const linkRequest = {
|
|
160
|
+
assetId,
|
|
161
|
+
deviceId,
|
|
162
|
+
measuresNames,
|
|
163
|
+
};
|
|
164
|
+
await this.deviceService.linkAsset(linkRequest, { refresh });
|
|
130
165
|
}
|
|
131
166
|
/**
|
|
132
167
|
* Link multiple devices to multiple assets.
|
|
133
168
|
*/
|
|
134
|
-
async
|
|
135
|
-
const { bulkData
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
169
|
+
async mLinkAssets(request) {
|
|
170
|
+
const { bulkData } = await this.mParseRequest(request);
|
|
171
|
+
const refresh = request.getRefresh();
|
|
172
|
+
for (const { deviceId, assetId } of bulkData) {
|
|
173
|
+
const linkRequest = {
|
|
174
|
+
assetId,
|
|
175
|
+
deviceId,
|
|
176
|
+
// @todo handle measure names
|
|
177
|
+
};
|
|
178
|
+
// Cannot be done in parallel because we need to copy previous measures
|
|
179
|
+
await this.deviceService.linkAsset(linkRequest, { refresh });
|
|
180
|
+
}
|
|
141
181
|
}
|
|
142
182
|
/**
|
|
143
183
|
* Unlink a device from an asset.
|
|
144
184
|
*/
|
|
145
|
-
async
|
|
185
|
+
async unlinkAsset(request) {
|
|
146
186
|
const deviceId = request.getId();
|
|
147
|
-
const
|
|
148
|
-
const
|
|
149
|
-
await this.deviceService.
|
|
150
|
-
strict: true,
|
|
151
|
-
options: { ...request.input.args }
|
|
152
|
-
});
|
|
187
|
+
const refresh = request.getRefresh();
|
|
188
|
+
const strict = request.getBoolean('strict');
|
|
189
|
+
await this.deviceService.unlinkAsset(deviceId, { refresh, strict });
|
|
153
190
|
}
|
|
154
191
|
/**
|
|
155
192
|
* Unlink multiple device from multiple assets.
|
|
156
193
|
*/
|
|
157
|
-
async
|
|
158
|
-
const { bulkData
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
194
|
+
async mUnlinkAssets(request) {
|
|
195
|
+
const { bulkData } = await this.mParseRequest(request);
|
|
196
|
+
const refresh = request.getRefresh();
|
|
197
|
+
const strict = request.getBoolean('strict');
|
|
198
|
+
for (const { deviceId } of bulkData) {
|
|
199
|
+
// Cannot be done in parallel because we need to keep previous measures
|
|
200
|
+
await this.deviceService.unlinkAsset(deviceId, { refresh, strict });
|
|
201
|
+
}
|
|
164
202
|
}
|
|
165
203
|
/**
|
|
166
204
|
* Clean payload collection for a time period
|
|
@@ -182,23 +220,26 @@ class DeviceController extends CRUDController_1.CRUDController {
|
|
|
182
220
|
if (body.keepInvalid) {
|
|
183
221
|
filter.push({ term: { valid: true } });
|
|
184
222
|
}
|
|
185
|
-
return
|
|
223
|
+
return this.as(request.context.user).bulk.deleteByQuery(this.config.adminIndex, 'payloads', { query: { bool: { filter } } });
|
|
186
224
|
}
|
|
187
|
-
async
|
|
188
|
-
const
|
|
189
|
-
const
|
|
190
|
-
|
|
225
|
+
async importDevices(request) {
|
|
226
|
+
const content = request.getBodyString('csv');
|
|
227
|
+
const refresh = request.getRefresh();
|
|
228
|
+
const devices = await (0, csvtojson_1.default)({ delimiter: 'auto' }).fromString(content);
|
|
229
|
+
return this.deviceService.importDevices(devices, {
|
|
230
|
+
refresh,
|
|
231
|
+
strict: true
|
|
232
|
+
});
|
|
191
233
|
}
|
|
192
234
|
async mParseRequest(request) {
|
|
193
235
|
const body = request.input.body;
|
|
194
236
|
let bulkData;
|
|
195
237
|
if (body.csv) {
|
|
196
|
-
const lines = await (0, csvtojson_1.default)({ delimiter: 'auto' })
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
tenantId,
|
|
238
|
+
const lines = await (0, csvtojson_1.default)({ delimiter: 'auto' }).fromString(body.csv);
|
|
239
|
+
bulkData = lines.map(({ engineId, deviceId, assetId }) => ({
|
|
240
|
+
assetId,
|
|
200
241
|
deviceId,
|
|
201
|
-
|
|
242
|
+
engineId
|
|
202
243
|
}));
|
|
203
244
|
}
|
|
204
245
|
else if (body.records) {
|
|
@@ -208,10 +249,10 @@ class DeviceController extends CRUDController_1.CRUDController {
|
|
|
208
249
|
bulkData = body.deviceIds.map((deviceId) => ({ deviceId }));
|
|
209
250
|
}
|
|
210
251
|
else {
|
|
211
|
-
throw new kuzzle_1.BadRequestError(
|
|
252
|
+
throw new kuzzle_1.BadRequestError('Malformed request missing property csv, records, deviceIds');
|
|
212
253
|
}
|
|
213
254
|
const strict = request.getBoolean('strict');
|
|
214
|
-
return {
|
|
255
|
+
return { bulkData, strict };
|
|
215
256
|
}
|
|
216
257
|
}
|
|
217
258
|
exports.DeviceController = DeviceController;
|
package/lib/controllers/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -11,6 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
11
15
|
};
|
|
12
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
17
|
__exportStar(require("./AssetController"), exports);
|
|
14
|
-
__exportStar(require("./CRUDController"), exports);
|
|
15
18
|
__exportStar(require("./DeviceController"), exports);
|
|
19
|
+
__exportStar(require("./DecodersController"), exports);
|
|
16
20
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Plugin, JSONObject, KDocument } from 'kuzzle';
|
|
2
|
+
import { MeasureContent } from '../types';
|
|
3
|
+
export declare class AssetService {
|
|
4
|
+
private config;
|
|
5
|
+
private context;
|
|
6
|
+
private get sdk();
|
|
7
|
+
constructor(plugin: Plugin);
|
|
8
|
+
/**
|
|
9
|
+
* Returns measures history for an asset
|
|
10
|
+
*
|
|
11
|
+
* @param engineId Engine ID
|
|
12
|
+
* @param assetId Asset ID
|
|
13
|
+
* @param options.size Number of measures to return
|
|
14
|
+
* @param options.startAt Returns measures starting from this date (ISO8601)
|
|
15
|
+
* @param options.endAt Returns measures until this date (ISO8601)
|
|
16
|
+
*/
|
|
17
|
+
measureHistory(engineId: string, assetId: string, { size, startAt, endAt }: {
|
|
18
|
+
size?: number;
|
|
19
|
+
startAt?: string;
|
|
20
|
+
endAt?: string;
|
|
21
|
+
}): Promise<KDocument<MeasureContent>[]>;
|
|
22
|
+
importAssets(index: string, assets: JSONObject, { strict, options }: {
|
|
23
|
+
strict?: boolean;
|
|
24
|
+
options?: JSONObject;
|
|
25
|
+
}): Promise<{
|
|
26
|
+
errors: any[];
|
|
27
|
+
successes: any[];
|
|
28
|
+
}>;
|
|
29
|
+
private getAsset;
|
|
30
|
+
private iso8601;
|
|
31
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.AssetService = void 0;
|
|
7
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
8
|
+
const kuzzle_1 = require("kuzzle");
|
|
9
|
+
const writeMany_1 = require("../utils/writeMany");
|
|
10
|
+
const BaseAsset_1 = require("../models/BaseAsset");
|
|
11
|
+
class AssetService {
|
|
12
|
+
constructor(plugin) {
|
|
13
|
+
this.config = plugin.config;
|
|
14
|
+
this.context = plugin.context;
|
|
15
|
+
}
|
|
16
|
+
get sdk() {
|
|
17
|
+
return this.context.accessors.sdk;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns measures history for an asset
|
|
21
|
+
*
|
|
22
|
+
* @param engineId Engine ID
|
|
23
|
+
* @param assetId Asset ID
|
|
24
|
+
* @param options.size Number of measures to return
|
|
25
|
+
* @param options.startAt Returns measures starting from this date (ISO8601)
|
|
26
|
+
* @param options.endAt Returns measures until this date (ISO8601)
|
|
27
|
+
*/
|
|
28
|
+
async measureHistory(engineId, assetId, { size = 25, startAt, endAt }) {
|
|
29
|
+
await this.getAsset(engineId, assetId);
|
|
30
|
+
const query = {
|
|
31
|
+
range: {
|
|
32
|
+
measuredAt: {
|
|
33
|
+
gte: 0,
|
|
34
|
+
lte: Number.MAX_SAFE_INTEGER,
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
if (startAt) {
|
|
39
|
+
query.range.measuredAt.gte = this.iso8601(startAt, 'startAt').getTime();
|
|
40
|
+
}
|
|
41
|
+
if (endAt) {
|
|
42
|
+
query.range.measuredAt.lte = this.iso8601(startAt, 'endAt').getTime();
|
|
43
|
+
}
|
|
44
|
+
const sort = { 'measuredAt': 'desc' };
|
|
45
|
+
const measures = await this.sdk.document.search(engineId, 'measures', { query, sort }, { size: size || 25 });
|
|
46
|
+
return measures.hits;
|
|
47
|
+
}
|
|
48
|
+
async importAssets(index, assets, { strict, options }) {
|
|
49
|
+
const results = {
|
|
50
|
+
errors: [],
|
|
51
|
+
successes: [],
|
|
52
|
+
};
|
|
53
|
+
const assetDocuments = assets.map((asset) => {
|
|
54
|
+
const _asset = new BaseAsset_1.BaseAsset(asset);
|
|
55
|
+
return {
|
|
56
|
+
_id: _asset._id,
|
|
57
|
+
body: lodash_1.default.omit(asset, ['_id']),
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
await (0, writeMany_1.writeToDatabase)(assetDocuments, async (result) => {
|
|
61
|
+
const created = await this.sdk.document.mCreate(index, 'assets', result, { strict, ...options });
|
|
62
|
+
return {
|
|
63
|
+
errors: results.errors.concat(created.errors),
|
|
64
|
+
successes: results.successes.concat(created.successes),
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
return results;
|
|
68
|
+
}
|
|
69
|
+
getAsset(engineId, assetId) {
|
|
70
|
+
return this.sdk.document.get(engineId, 'assets', assetId);
|
|
71
|
+
}
|
|
72
|
+
// @todo remove when we have the date extractor in the core
|
|
73
|
+
iso8601(value, name) {
|
|
74
|
+
const parsed = new Date(value);
|
|
75
|
+
if (isNaN(parsed)) {
|
|
76
|
+
throw new kuzzle_1.BadRequestError(`"${name}" is not a valid ISO8601 date`);
|
|
77
|
+
}
|
|
78
|
+
return parsed;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.AssetService = AssetService;
|
|
82
|
+
//# sourceMappingURL=AssetService.js.map
|