kuzzle-device-manager 0.4.8 → 0.4.9

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.
Files changed (61) hide show
  1. package/index.d.ts +4 -0
  2. package/index.js +17 -0
  3. package/lib/DeviceManagerPlugin.d.ts +100 -0
  4. package/lib/DeviceManagerPlugin.js +237 -0
  5. package/lib/controllers/AssetController.d.ts +7 -0
  6. package/lib/controllers/AssetController.js +53 -0
  7. package/lib/controllers/CRUDController.d.ts +34 -0
  8. package/lib/controllers/CRUDController.js +65 -0
  9. package/lib/controllers/DeviceController.d.ts +56 -0
  10. package/lib/controllers/DeviceController.js +218 -0
  11. package/lib/controllers/index.d.ts +3 -0
  12. package/lib/controllers/index.js +16 -0
  13. package/lib/core-classes/BatchProcessing/BatchController.d.ts +32 -0
  14. package/lib/core-classes/BatchProcessing/BatchController.js +86 -0
  15. package/lib/core-classes/BatchProcessing/BatchWriter.d.ts +110 -0
  16. package/lib/core-classes/BatchProcessing/BatchWriter.js +264 -0
  17. package/lib/core-classes/BatchProcessing/InstrumentablePromise.d.ts +6 -0
  18. package/lib/core-classes/BatchProcessing/InstrumentablePromise.js +13 -0
  19. package/lib/core-classes/BatchProcessing/index.d.ts +3 -0
  20. package/lib/core-classes/BatchProcessing/index.js +16 -0
  21. package/lib/core-classes/CustomMappings/AssetMappingsManager.d.ts +31 -0
  22. package/lib/core-classes/CustomMappings/AssetMappingsManager.js +79 -0
  23. package/lib/core-classes/CustomMappings/DeviceMappingsManager.d.ts +43 -0
  24. package/lib/core-classes/CustomMappings/DeviceMappingsManager.js +71 -0
  25. package/lib/core-classes/Decoder.d.ts +133 -0
  26. package/lib/core-classes/Decoder.js +166 -0
  27. package/lib/core-classes/DeviceManagerEngine.d.ts +21 -0
  28. package/lib/core-classes/DeviceManagerEngine.js +64 -0
  29. package/lib/core-classes/DeviceService.d.ts +41 -0
  30. package/lib/core-classes/DeviceService.js +264 -0
  31. package/lib/core-classes/MigrationService.d.ts +13 -0
  32. package/lib/core-classes/MigrationService.js +49 -0
  33. package/lib/core-classes/PayloadService.d.ts +43 -0
  34. package/lib/core-classes/PayloadService.js +191 -0
  35. package/lib/core-classes/index.d.ts +8 -0
  36. package/lib/core-classes/index.js +21 -0
  37. package/lib/models/BaseAsset.d.ts +56 -0
  38. package/lib/models/BaseAsset.js +58 -0
  39. package/lib/models/Catalog.d.ts +55 -0
  40. package/lib/models/Catalog.js +38 -0
  41. package/lib/models/Config.d.ts +33 -0
  42. package/lib/models/Config.js +26 -0
  43. package/lib/models/Device.d.ts +102 -0
  44. package/lib/models/Device.js +91 -0
  45. package/lib/models/index.d.ts +3 -0
  46. package/lib/models/index.js +16 -0
  47. package/lib/types/AssetsHistoryContent.d.ts +9 -0
  48. package/lib/types/AssetsHistoryContent.js +3 -0
  49. package/lib/types/BaseAssetContent.d.ts +8 -0
  50. package/lib/types/BaseAssetContent.js +3 -0
  51. package/lib/types/DeviceContent.d.ts +62 -0
  52. package/lib/types/DeviceContent.js +3 -0
  53. package/lib/types/Measure.d.ts +54 -0
  54. package/lib/types/Measure.js +3 -0
  55. package/lib/types/index.d.ts +3 -0
  56. package/lib/types/index.js +16 -0
  57. package/lib/utils/events.d.ts +8 -0
  58. package/lib/utils/events.js +17 -0
  59. package/lib/utils/index.d.ts +1 -0
  60. package/lib/utils/index.js +14 -0
  61. package/package.json +1 -1
@@ -0,0 +1,264 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BatchWriter = exports.BatchBuffer = void 0;
4
+ const BatchController_1 = require("./BatchController");
5
+ const InstrumentablePromise_1 = require("./InstrumentablePromise");
6
+ class BatchBuffer {
7
+ constructor() {
8
+ this.indexes = {};
9
+ }
10
+ /**
11
+ * Add a document to the buffer of a specific collection
12
+ *
13
+ * @param index Index name
14
+ * @param collection Collection name
15
+ * @param body Document content
16
+ * @param _id Document ID
17
+ * @param options Option object passed to the m* action
18
+ *
19
+ * @returns An object containing the index of the array of results and a promise resolving to the array of results
20
+ */
21
+ add(index, collection, body, _id = undefined, options) {
22
+ if (!this.indexes[index]) {
23
+ this.indexes[index] = {};
24
+ }
25
+ if (!this.indexes[index][collection]) {
26
+ this.indexes[index][collection] = {
27
+ documents: [],
28
+ promise: new InstrumentablePromise_1.InstrumentablePromise(),
29
+ options,
30
+ };
31
+ }
32
+ const idx = this.indexes[index][collection].documents.length;
33
+ this.indexes[index][collection].options = { ...this.indexes[index][collection].options, ...options };
34
+ this.indexes[index][collection].documents.push({
35
+ _id,
36
+ body
37
+ });
38
+ return {
39
+ idx,
40
+ promise: this.indexes[index][collection].promise,
41
+ };
42
+ }
43
+ }
44
+ exports.BatchBuffer = BatchBuffer;
45
+ /**
46
+ * This class handle buffers for every supported API action of the document controller:
47
+ * - create, update, createOrReplace, replace, get, exists, delete
48
+ *
49
+ * Each buffer is filled with documents to be write/get/delete into a collection.
50
+ *
51
+ * A timer will regularly execute the m* actions with the documents inside the buffers.
52
+ *
53
+ * If the interval is too big, buffers may contain too much documents for Kuzzle limits.
54
+ * (e.g. "limits.documentsWriteCount" is 200 by default)
55
+ */
56
+ class BatchWriter {
57
+ /**
58
+ * @param sdk Connected SDK
59
+ * @param options.interval Timer interval in ms (50). Actions will be executed every {interval} ms
60
+ * @param options.maxWriteBufferSize Max write buffer size (200). (Should match "limits.documentsWriteCount")
61
+ * @param options.maxReadBufferSize Max read buffer size. (Should match "limits.documentsReadCount")
62
+ */
63
+ constructor(sdk, { interval = 50, maxWriteBufferSize = 200, maxReadBufferSize = 200 } = {}) {
64
+ // Buffers
65
+ this.buffers = {
66
+ create: new BatchBuffer(),
67
+ update: new BatchBuffer(),
68
+ get: new BatchBuffer(),
69
+ exists: new BatchBuffer(),
70
+ delete: new BatchBuffer(),
71
+ replace: new BatchBuffer(),
72
+ createOrReplace: new BatchBuffer(),
73
+ };
74
+ this.sdk = sdk;
75
+ this.interval = interval;
76
+ this.maxWriteBufferSize = maxWriteBufferSize;
77
+ this.maxReadBufferSize = maxReadBufferSize;
78
+ this.document = new BatchController_1.BatchController(this.sdk, this);
79
+ }
80
+ get addCreate() {
81
+ // @todo implements the send of buffer if approaching the limit
82
+ return this.buffers.create.add.bind(this.buffers.create);
83
+ }
84
+ get addUpdate() {
85
+ return this.buffers.update.add.bind(this.buffers.update);
86
+ }
87
+ get addGet() {
88
+ return this.buffers.get.add.bind(this.buffers.get);
89
+ }
90
+ get addExists() {
91
+ return this.buffers.exists.add.bind(this.buffers.exists);
92
+ }
93
+ get addDelete() {
94
+ return this.buffers.delete.add.bind(this.buffers.delete);
95
+ }
96
+ get addReplace() {
97
+ return this.buffers.replace.add.bind(this.buffers.replace);
98
+ }
99
+ get addCreateOrReplace() {
100
+ return this.buffers.createOrReplace.add.bind(this.buffers.createOrReplace);
101
+ }
102
+ /**
103
+ * Execute API actions with documents stored in the buffers.
104
+ */
105
+ async execute() {
106
+ const buffers = {};
107
+ // make a copy of the buffers
108
+ for (const [name, buffer] of Object.entries(this.buffers)) {
109
+ buffers[name] = buffer;
110
+ }
111
+ // prepare to enqueue documents in buffers
112
+ this.prepareRound();
113
+ await Promise.all([
114
+ this.sendCreateBuffer(buffers.create),
115
+ this.sendUpdateBuffer(buffers.update),
116
+ this.sendCreateOrReplaceBuffer(buffers.createOrReplace),
117
+ this.sendDeleteBuffer(buffers.delete),
118
+ this.sendExistsBuffer(buffers.exists),
119
+ this.sendGetBuffer(buffers.get),
120
+ this.sendReplaceBuffer(buffers.replace),
121
+ ]);
122
+ }
123
+ /**
124
+ * Initialize the buffers and start the timer
125
+ */
126
+ begin() {
127
+ this.prepareRound();
128
+ this.timer = setInterval(async () => {
129
+ await this.execute();
130
+ }, this.interval);
131
+ }
132
+ /**
133
+ * Execute pending actions from the buffers and stop the timer.
134
+ */
135
+ async end() {
136
+ await this.execute();
137
+ clearInterval(this.timer);
138
+ }
139
+ /**
140
+ * Reset all the buffers
141
+ */
142
+ prepareRound() {
143
+ for (const name of Object.keys(this.buffers)) {
144
+ this.buffers[name] = new BatchBuffer();
145
+ }
146
+ }
147
+ async sendCreateBuffer(buffer, options) {
148
+ const promises = [];
149
+ for (const [index, collectionBuffer] of Object.entries(buffer.indexes)) {
150
+ for (const [collection, { promise, documents }] of Object.entries(collectionBuffer)) {
151
+ if (documents.length === 0) {
152
+ promise.resolve();
153
+ continue;
154
+ }
155
+ promises.push(this.sdk.document.mCreate(index, collection, documents, { ...options, strict: true })
156
+ .then(promise.resolve)
157
+ .catch(promise.reject));
158
+ }
159
+ }
160
+ await Promise.all(promises);
161
+ }
162
+ async sendUpdateBuffer(buffer, options) {
163
+ const promises = [];
164
+ for (const [index, collectionBuffer] of Object.entries(buffer.indexes)) {
165
+ for (const [collection, { promise, documents }] of Object.entries(collectionBuffer)) {
166
+ if (documents.length === 0) {
167
+ promise.resolve();
168
+ continue;
169
+ }
170
+ promises.push(this.sdk.document.mUpdate(index, collection, documents, { ...options })
171
+ .then(promise.resolve)
172
+ .catch(promise.reject));
173
+ }
174
+ }
175
+ await Promise.all(promises);
176
+ }
177
+ async sendReplaceBuffer(buffer, options) {
178
+ const promises = [];
179
+ for (const [index, collectionBuffer] of Object.entries(buffer.indexes)) {
180
+ for (const [collection, { promise, documents }] of Object.entries(collectionBuffer)) {
181
+ if (documents.length === 0) {
182
+ promise.resolve();
183
+ continue;
184
+ }
185
+ promises.push(this.sdk.document.mReplace(index, collection, documents, { ...options })
186
+ .then(promise.resolve)
187
+ .catch(promise.reject));
188
+ }
189
+ }
190
+ await Promise.all(promises);
191
+ }
192
+ async sendCreateOrReplaceBuffer(buffer, options) {
193
+ const promises = [];
194
+ for (const [index, collectionBuffer] of Object.entries(buffer.indexes)) {
195
+ for (const [collection, { promise, documents }] of Object.entries(collectionBuffer)) {
196
+ if (documents.length === 0) {
197
+ promise.resolve();
198
+ continue;
199
+ }
200
+ promises.push(this.sdk.document.mCreateOrReplace(index, collection, documents, { ...options })
201
+ .then(promise.resolve)
202
+ .catch(promise.reject));
203
+ }
204
+ }
205
+ await Promise.all(promises);
206
+ }
207
+ async sendGetBuffer(buffer) {
208
+ const promises = [];
209
+ for (const [index, collectionBuffer] of Object.entries(buffer.indexes)) {
210
+ for (const [collection, { promise, documents }] of Object.entries(collectionBuffer)) {
211
+ if (documents.length === 0) {
212
+ promise.resolve();
213
+ continue;
214
+ }
215
+ const ids = documents.map(({ _id }) => _id);
216
+ promises.push(this.sdk.document.mGet(index, collection, ids)
217
+ .then(promise.resolve)
218
+ .catch(promise.reject));
219
+ }
220
+ }
221
+ await Promise.all(promises);
222
+ }
223
+ async sendExistsBuffer(buffer) {
224
+ const promises = [];
225
+ for (const [index, collectionBuffer] of Object.entries(buffer.indexes)) {
226
+ for (const [collection, { promise, documents }] of Object.entries(collectionBuffer)) {
227
+ if (documents.length === 0) {
228
+ promise.resolve();
229
+ continue;
230
+ }
231
+ const ids = documents.map(({ _id }) => _id);
232
+ promises.push(this.sdk.document.mGet(index, collection, ids)
233
+ .then(({ successes }) => {
234
+ const successesId = successes.map(({ _id }) => _id);
235
+ const exists = new Array(ids.length);
236
+ for (let i = 0; i < exists.length; i++) {
237
+ exists[i] = successesId.includes(ids[i]);
238
+ }
239
+ promise.resolve({ successes: exists });
240
+ })
241
+ .catch(promise.reject));
242
+ }
243
+ }
244
+ await Promise.all(promises);
245
+ }
246
+ async sendDeleteBuffer(buffer) {
247
+ const promises = [];
248
+ for (const [index, collectionBuffer] of Object.entries(buffer.indexes)) {
249
+ for (const [collection, { promise, documents }] of Object.entries(collectionBuffer)) {
250
+ if (documents.length === 0) {
251
+ promise.resolve();
252
+ continue;
253
+ }
254
+ const ids = documents.map(({ _id }) => _id);
255
+ promises.push(this.sdk.document.mDelete(index, collection, ids)
256
+ .then(promise.resolve)
257
+ .catch(promise.reject));
258
+ }
259
+ }
260
+ await Promise.all(promises);
261
+ }
262
+ }
263
+ exports.BatchWriter = BatchWriter;
264
+ //# sourceMappingURL=BatchWriter.js.map
@@ -0,0 +1,6 @@
1
+ export declare class InstrumentablePromise {
2
+ promise: Promise<any>;
3
+ resolve: (...any: any[]) => any;
4
+ reject: (...any: any[]) => any;
5
+ constructor();
6
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InstrumentablePromise = void 0;
4
+ class InstrumentablePromise {
5
+ constructor() {
6
+ this.promise = new Promise((resolve, reject) => {
7
+ this.resolve = resolve;
8
+ this.reject = reject;
9
+ });
10
+ }
11
+ }
12
+ exports.InstrumentablePromise = InstrumentablePromise;
13
+ //# sourceMappingURL=InstrumentablePromise.js.map
@@ -0,0 +1,3 @@
1
+ export * from './BatchController';
2
+ export * from './BatchWriter';
3
+ export * from './InstrumentablePromise';
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./BatchController"), exports);
14
+ __exportStar(require("./BatchWriter"), exports);
15
+ __exportStar(require("./InstrumentablePromise"), exports);
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,31 @@
1
+ import { JSONObject } from 'kuzzle';
2
+ import { DeviceMappingsManager } from './DeviceMappingsManager';
3
+ export declare class AssetMappingsManager {
4
+ private baseMappings;
5
+ private deviceMappings;
6
+ private assetByName;
7
+ private assetByGroup;
8
+ constructor(baseMappings: JSONObject, deviceMappings: DeviceMappingsManager);
9
+ /**
10
+ * Register an asset type with custom metadata.
11
+ *
12
+ * If a "group" is provided, this asset type metadata will only be injected when
13
+ * creating an engine of the same group.
14
+ *
15
+ * @param name Asset type name
16
+ * @param metadata Asset type custom metadata
17
+ * @param options.group Only inject this asset type to engine of this group
18
+ *
19
+ * @example
20
+ *
21
+ * plugin.assets.register('container', {
22
+ * size: { type: 'integer' },
23
+ * serial: { type: 'keyword' },
24
+ * });
25
+ *
26
+ */
27
+ register(name: string, metadata: JSONObject, { group }?: {
28
+ group?: string;
29
+ }): void;
30
+ get(group?: string): JSONObject;
31
+ }
@@ -0,0 +1,79 @@
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.AssetMappingsManager = void 0;
7
+ const lodash_1 = __importDefault(require("lodash"));
8
+ const kuzzle_1 = require("kuzzle");
9
+ class AssetMappingsManager {
10
+ constructor(baseMappings, deviceMappings) {
11
+ this.assetByName = new Map();
12
+ this.assetByGroup = new Map();
13
+ this.baseMappings = baseMappings;
14
+ this.deviceMappings = deviceMappings;
15
+ this.assetByGroup.set('commons', []);
16
+ }
17
+ /**
18
+ * Register an asset type with custom metadata.
19
+ *
20
+ * If a "group" is provided, this asset type metadata will only be injected when
21
+ * creating an engine of the same group.
22
+ *
23
+ * @param name Asset type name
24
+ * @param metadata Asset type custom metadata
25
+ * @param options.group Only inject this asset type to engine of this group
26
+ *
27
+ * @example
28
+ *
29
+ * plugin.assets.register('container', {
30
+ * size: { type: 'integer' },
31
+ * serial: { type: 'keyword' },
32
+ * });
33
+ *
34
+ */
35
+ register(name, metadata, { group = 'commons' } = {}) {
36
+ if (this.assetByName.has(name)) {
37
+ throw new kuzzle_1.PluginImplementationError(`Asset of type "${name}" has already been registered.`);
38
+ }
39
+ const definition = {
40
+ name,
41
+ group,
42
+ metadata,
43
+ };
44
+ this.assetByName.set(name, definition);
45
+ if (!this.assetByGroup.has(group)) {
46
+ this.assetByGroup.set(group, []);
47
+ }
48
+ this.assetByGroup.get(group).push(definition);
49
+ }
50
+ get(group = 'commons') {
51
+ const mappings = JSON.parse(JSON.stringify(this.baseMappings));
52
+ const deviceMappings = this.deviceMappings.get();
53
+ mappings.properties.measures.dynamic = 'false';
54
+ for (const [name, measure] of Object.entries(deviceMappings.properties.measures.properties)) {
55
+ mappings.properties.measures.properties[name] = {
56
+ properties: {
57
+ id: { type: 'keyword' },
58
+ model: { type: 'keyword' },
59
+ reference: { type: 'keyword' },
60
+ qos: deviceMappings.properties.qos,
61
+ ...measure.properties,
62
+ }
63
+ };
64
+ }
65
+ if (this.assetByGroup.has('commons')) {
66
+ for (const definition of this.assetByGroup.get('commons')) {
67
+ mappings.properties.metadata.properties = lodash_1.default.merge({}, mappings.properties.metadata.properties, definition.metadata);
68
+ }
69
+ }
70
+ if (group !== 'commons' && this.assetByGroup.has(group)) {
71
+ for (const definition of this.assetByGroup.get(group)) {
72
+ mappings.properties.metadata.properties = lodash_1.default.merge({}, mappings.properties.metadata.properties, definition.metadata);
73
+ }
74
+ }
75
+ return mappings;
76
+ }
77
+ }
78
+ exports.AssetMappingsManager = AssetMappingsManager;
79
+ //# sourceMappingURL=AssetMappingsManager.js.map
@@ -0,0 +1,43 @@
1
+ import { JSONObject } from 'kuzzle';
2
+ export declare class DeviceMappingsManager {
3
+ private mappings;
4
+ constructor(baseMappings: JSONObject);
5
+ /**
6
+ * Register custom metadata for devices
7
+ *
8
+ * @param metadata Device custom metadata mappings
9
+ *
10
+ * @example
11
+ *
12
+ * plugin.devices.registerMetadata({
13
+ * serial: { type: 'keyword' },
14
+ * });
15
+ *
16
+ */
17
+ registerMetadata(metadata: any): void;
18
+ /**
19
+ * Register custom QoS for devices
20
+ *
21
+ * @param qos Device custom QoS mappings
22
+ *
23
+ * @example
24
+ *
25
+ * plugin.devices.registerQoS({
26
+ * battery: { type: 'integer' },
27
+ * });
28
+ */
29
+ registerQoS(qos: JSONObject): void;
30
+ /**
31
+ * Register custom measure for devices mappings
32
+ *
33
+ * @param qos Device custom measure
34
+ *
35
+ * @example
36
+ *
37
+ * plugin.devices.registerMeasure('humidity', {
38
+ * value: { type: 'float' },
39
+ * });
40
+ */
41
+ registerMeasure(name: string, measure: JSONObject): void;
42
+ get(): JSONObject;
43
+ }
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeviceMappingsManager = void 0;
4
+ const kuzzle_1 = require("kuzzle");
5
+ class DeviceMappingsManager {
6
+ constructor(baseMappings) {
7
+ this.mappings = JSON.parse(JSON.stringify(baseMappings));
8
+ }
9
+ /**
10
+ * Register custom metadata for devices
11
+ *
12
+ * @param metadata Device custom metadata mappings
13
+ *
14
+ * @example
15
+ *
16
+ * plugin.devices.registerMetadata({
17
+ * serial: { type: 'keyword' },
18
+ * });
19
+ *
20
+ */
21
+ registerMetadata(metadata) {
22
+ for (const [name, value] of Object.entries(metadata)) {
23
+ this.mappings.properties.metadata.properties[name] = value;
24
+ }
25
+ }
26
+ /**
27
+ * Register custom QoS for devices
28
+ *
29
+ * @param qos Device custom QoS mappings
30
+ *
31
+ * @example
32
+ *
33
+ * plugin.devices.registerQoS({
34
+ * battery: { type: 'integer' },
35
+ * });
36
+ */
37
+ registerQoS(qos) {
38
+ for (const [name, value] of Object.entries(qos)) {
39
+ this.mappings.properties.qos.properties[name] = value;
40
+ }
41
+ }
42
+ /**
43
+ * Register custom measure for devices mappings
44
+ *
45
+ * @param qos Device custom measure
46
+ *
47
+ * @example
48
+ *
49
+ * plugin.devices.registerMeasure('humidity', {
50
+ * value: { type: 'float' },
51
+ * });
52
+ */
53
+ registerMeasure(name, measure) {
54
+ if (this.mappings.properties.measures.properties[name]) {
55
+ throw new kuzzle_1.PluginImplementationError(`Measure "${name}" already exists.`);
56
+ }
57
+ const newMeasure = {
58
+ ...measure,
59
+ payloadUuid: { type: 'keyword' },
60
+ updatedAt: { type: 'date' },
61
+ };
62
+ this.mappings.properties.measures.properties[name] = {
63
+ properties: newMeasure
64
+ };
65
+ }
66
+ get() {
67
+ return this.mappings;
68
+ }
69
+ }
70
+ exports.DeviceMappingsManager = DeviceMappingsManager;
71
+ //# sourceMappingURL=DeviceMappingsManager.js.map
@@ -0,0 +1,133 @@
1
+ import { JSONObject, KuzzleRequest, HttpRoute } from 'kuzzle';
2
+ import { Device, BaseAsset } from '../models';
3
+ import { AssetMeasures, DeviceContent } from '../types';
4
+ /**
5
+ * Base class to implement a decoder for a device model.
6
+ * The device model must be passed to the parent constructor.
7
+ * The abstract "decode" method must be implemented.
8
+ */
9
+ export declare abstract class Decoder {
10
+ private _http?;
11
+ /**
12
+ * Device model name
13
+ */
14
+ deviceModel: string;
15
+ /**
16
+ * Custom name for the associated API action in the "payload" controller
17
+ */
18
+ action?: string;
19
+ /**
20
+ * Custom mappings for the payload collection.
21
+ * It will be injected in the "payload" property and it should allows to index
22
+ * the device model unique identifier field.
23
+ *
24
+ * @example
25
+ * this.payloadsMappings = {
26
+ * device_properties: {
27
+ * properties: {
28
+ * deveui: { type: 'keyword' }
29
+ * }
30
+ * }
31
+ * }
32
+ */
33
+ payloadsMappings?: JSONObject;
34
+ /**
35
+ * Define custom HTTP routes
36
+ *
37
+ * @param http HttpRoute array
38
+ */
39
+ set http(http: HttpRoute[]);
40
+ get http(): HttpRoute[];
41
+ /**
42
+ * @param deviceModel Device model for this decoder
43
+ */
44
+ constructor(deviceModel: string);
45
+ /**
46
+ * Validate the payload format before processing.
47
+ *
48
+ * If the method:
49
+ * - return true: the payload will be processed (status 200)
50
+ * - return false: the payload will be skipped (status 200)
51
+ * - throw an error: the payload will be skipped (status 4** or 5**)
52
+ *
53
+ * @param payload Raw payload received in the API action body
54
+ * @param request Original request
55
+ *
56
+ * @return A boolean indicating if the payload is valid
57
+ */
58
+ validate(payload: JSONObject, request: KuzzleRequest): Promise<boolean> | never;
59
+ /**
60
+ * Decode the payload:
61
+ * - set "reference"
62
+ * - fetch measures
63
+ * - fetch qos
64
+ *
65
+ * @param payload Raw payload received in the API action body
66
+ * @param request Original request
67
+ *
68
+ * @returns Device content to save
69
+ */
70
+ abstract decode(payload: JSONObject, request: KuzzleRequest): Promise<DeviceContent>;
71
+ /**
72
+ * Hook executed before processing the payload but after validation
73
+ *
74
+ * @param payload Raw payload received in the API action body
75
+ * @param request Original request
76
+ */
77
+ beforeProcessing(payload: JSONObject, request: KuzzleRequest): Promise<void>;
78
+ /**
79
+ * Enrichment hook executed before registering a device
80
+ *
81
+ * @param device Densor before being persisted
82
+ * @param request Original request
83
+ *
84
+ * @returns Enriched device
85
+ */
86
+ beforeRegister(device: Device, request: KuzzleRequest): Promise<Device>;
87
+ /**
88
+ * Hook executed after registering a device.
89
+ * Return value of this method will be returned in the API action result.
90
+ *
91
+ * @param device Device after being persisted
92
+ * @param request Original request
93
+ *
94
+ * @returns Result of the corresponding API action
95
+ */
96
+ afterRegister(device: Device, request: KuzzleRequest): Promise<any>;
97
+ /**
98
+ * Enrichment hook executed before updating a device
99
+ *
100
+ * @param device Device before being updated
101
+ * @param request Original request
102
+ *
103
+ * @returns Enriched device
104
+ */
105
+ beforeUpdate(device: Device, request: KuzzleRequest): Promise<Device>;
106
+ /**
107
+ * Hook executed after updating a device.
108
+ * Return value of this method will be returned in the API action result.
109
+ *
110
+ * @param device Device after being updated
111
+ * @param request Original request
112
+ *
113
+ * @returns Result of the corresponding API action
114
+ */
115
+ afterUpdate(device: Device, asset: BaseAsset, request: KuzzleRequest): Promise<any>;
116
+ /**
117
+ * Build the "measures" property that will be persisted in the asset document
118
+ *
119
+ * @param device Device after being updated
120
+ *
121
+ * @returns Content of the "measures" property
122
+ */
123
+ copyToAsset(device: Device): Promise<AssetMeasures>;
124
+ /**
125
+ * Checks if the provided properties are present in the payload
126
+ *
127
+ * @param payload Raw payload received in the API action body
128
+ * @param paths Paths of properties (lodash style)
129
+ *
130
+ * @throws
131
+ */
132
+ ensureProperties(payload: JSONObject, paths: string[]): void | never;
133
+ }