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.
Files changed (102) hide show
  1. package/index.d.ts +1 -0
  2. package/index.js +6 -1
  3. package/lib/DeviceManagerPlugin.d.ts +33 -65
  4. package/lib/DeviceManagerPlugin.js +142 -131
  5. package/lib/controllers/AssetController.d.ts +20 -5
  6. package/lib/controllers/AssetController.js +78 -23
  7. package/lib/controllers/DecodersController.d.ts +16 -0
  8. package/lib/controllers/DecodersController.js +27 -0
  9. package/lib/controllers/DeviceController.d.ts +25 -22
  10. package/lib/controllers/DeviceController.js +150 -109
  11. package/lib/controllers/index.d.ts +1 -1
  12. package/lib/controllers/index.js +6 -2
  13. package/lib/core-classes/AssetService.d.ts +31 -0
  14. package/lib/core-classes/AssetService.js +82 -0
  15. package/lib/core-classes/Decoder.d.ts +14 -59
  16. package/lib/core-classes/Decoder.js +16 -86
  17. package/lib/core-classes/DeviceManagerEngine.d.ts +11 -9
  18. package/lib/core-classes/DeviceManagerEngine.js +20 -32
  19. package/lib/core-classes/DeviceService.d.ts +81 -24
  20. package/lib/core-classes/DeviceService.js +178 -220
  21. package/lib/core-classes/PayloadService.d.ts +39 -26
  22. package/lib/core-classes/PayloadService.js +132 -124
  23. package/lib/core-classes/index.d.ts +5 -4
  24. package/lib/core-classes/index.js +10 -5
  25. package/lib/core-classes/{CustomMappings/AssetMappingsManager.d.ts → registers/AssetsRegister.d.ts} +8 -9
  26. package/lib/core-classes/{CustomMappings/AssetMappingsManager.js → registers/AssetsRegister.js} +17 -28
  27. package/lib/core-classes/registers/DecodersRegister.d.ts +46 -0
  28. package/lib/core-classes/registers/DecodersRegister.js +167 -0
  29. package/lib/core-classes/registers/DevicesRegister.d.ts +20 -0
  30. package/lib/core-classes/registers/DevicesRegister.js +37 -0
  31. package/lib/core-classes/registers/MeasuresRegister.d.ts +29 -0
  32. package/lib/core-classes/registers/MeasuresRegister.js +53 -0
  33. package/lib/mappings/assetsMappings.d.ts +36 -0
  34. package/lib/mappings/assetsMappings.js +36 -0
  35. package/lib/mappings/deviceMappings.d.ts +44 -0
  36. package/lib/mappings/deviceMappings.js +42 -0
  37. package/lib/mappings/index.d.ts +4 -0
  38. package/lib/mappings/index.js +21 -0
  39. package/lib/mappings/measuresMappings.d.ts +53 -0
  40. package/lib/mappings/measuresMappings.js +48 -0
  41. package/lib/mappings/payloadsMappings.d.ts +18 -0
  42. package/lib/mappings/payloadsMappings.js +16 -0
  43. package/lib/measures/BatteryMeasure.d.ts +7 -0
  44. package/lib/measures/BatteryMeasure.js +12 -0
  45. package/lib/measures/HumidityMeasure.d.ts +7 -0
  46. package/lib/measures/HumidityMeasure.js +12 -0
  47. package/lib/measures/MovementMeasure.d.ts +7 -0
  48. package/lib/measures/MovementMeasure.js +12 -0
  49. package/lib/measures/PositionMeasure.d.ts +12 -0
  50. package/lib/measures/PositionMeasure.js +16 -0
  51. package/lib/measures/TemperatureMeasure.d.ts +7 -0
  52. package/lib/measures/TemperatureMeasure.js +12 -0
  53. package/lib/measures/index.d.ts +5 -0
  54. package/lib/measures/index.js +22 -0
  55. package/lib/models/BaseAsset.d.ts +1 -48
  56. package/lib/models/BaseAsset.js +5 -43
  57. package/lib/models/Config.d.ts +1 -13
  58. package/lib/models/Device.d.ts +5 -96
  59. package/lib/models/Device.js +5 -73
  60. package/lib/models/index.d.ts +0 -1
  61. package/lib/models/index.js +5 -2
  62. package/lib/types/BaseAssetContent.d.ts +8 -3
  63. package/lib/types/DecodedPayload.d.ts +42 -0
  64. package/lib/types/{AssetsHistoryContent.js → DecodedPayload.js} +1 -1
  65. package/lib/types/DecoderContent.d.ts +4 -0
  66. package/lib/types/DecoderContent.js +3 -0
  67. package/lib/types/DeviceContent.d.ts +12 -44
  68. package/lib/types/DeviceManagerConfiguration.d.ts +40 -0
  69. package/lib/types/DeviceManagerConfiguration.js +3 -0
  70. package/lib/types/index.d.ts +7 -2
  71. package/lib/types/index.js +12 -3
  72. package/lib/types/measures/MeasureContent.d.ts +56 -0
  73. package/lib/types/measures/MeasureContent.js +3 -0
  74. package/lib/types/measures/MeasureDefinition.d.ts +40 -0
  75. package/lib/types/measures/MeasureDefinition.js +3 -0
  76. package/lib/types/measures/Measurement.d.ts +19 -0
  77. package/lib/types/{Measure.js → measures/Measurement.js} +1 -1
  78. package/lib/utils/index.d.ts +1 -1
  79. package/lib/utils/index.js +6 -2
  80. package/lib/utils/writeMany.d.ts +13 -0
  81. package/lib/utils/writeMany.js +37 -0
  82. package/package.json +14 -18
  83. package/lib/controllers/CRUDController.d.ts +0 -34
  84. package/lib/controllers/CRUDController.js +0 -65
  85. package/lib/core-classes/BatchProcessing/BatchController.d.ts +0 -34
  86. package/lib/core-classes/BatchProcessing/BatchController.js +0 -115
  87. package/lib/core-classes/BatchProcessing/BatchWriter.d.ts +0 -110
  88. package/lib/core-classes/BatchProcessing/BatchWriter.js +0 -264
  89. package/lib/core-classes/BatchProcessing/InstrumentablePromise.d.ts +0 -6
  90. package/lib/core-classes/BatchProcessing/InstrumentablePromise.js +0 -13
  91. package/lib/core-classes/BatchProcessing/index.d.ts +0 -3
  92. package/lib/core-classes/BatchProcessing/index.js +0 -16
  93. package/lib/core-classes/CustomMappings/DeviceMappingsManager.d.ts +0 -43
  94. package/lib/core-classes/CustomMappings/DeviceMappingsManager.js +0 -71
  95. package/lib/core-classes/MigrationService.d.ts +0 -13
  96. package/lib/core-classes/MigrationService.js +0 -49
  97. package/lib/models/Catalog.d.ts +0 -55
  98. package/lib/models/Catalog.js +0 -38
  99. package/lib/types/AssetsHistoryContent.d.ts +0 -9
  100. package/lib/types/Measure.d.ts +0 -54
  101. package/lib/utils/events.d.ts +0 -8
  102. package/lib/utils/events.js +0 -17
@@ -7,23 +7,25 @@ exports.PayloadService = void 0;
7
7
  const lodash_1 = __importDefault(require("lodash"));
8
8
  const kuzzle_1 = require("kuzzle");
9
9
  const uuid_1 = require("uuid");
10
+ const kuzzle_sdk_1 = require("kuzzle-sdk");
10
11
  const models_1 = require("../models");
11
- const utils_1 = require("../utils");
12
12
  class PayloadService {
13
- constructor(plugin, batchWriter) {
13
+ constructor(plugin, measuresRegister) {
14
14
  this.config = plugin.config;
15
15
  this.context = plugin.context;
16
- this.batchController = batchWriter.document;
16
+ this.measuresRegister = measuresRegister;
17
+ this.batch = new kuzzle_sdk_1.BatchController(this.sdk, {
18
+ interval: plugin.config.batchInterval
19
+ });
17
20
  }
18
21
  get sdk() {
19
22
  return this.context.accessors.sdk;
20
23
  }
24
+ get app() {
25
+ return global.app;
26
+ }
21
27
  async process(request, decoder, { refresh = undefined } = {}) {
22
- const payload = request.input.body;
23
- if (!payload
24
- || (typeof payload === 'object' && Object.keys(payload).length === 0)) {
25
- throw new kuzzle_1.BadRequestError('The body must contain the payload.');
26
- }
28
+ const payload = request.getBody();
27
29
  const uuid = request.input.args.uuid || (0, uuid_1.v4)();
28
30
  let valid = true;
29
31
  try {
@@ -31,160 +33,166 @@ class PayloadService {
31
33
  if (!valid) {
32
34
  return { valid };
33
35
  }
34
- await decoder.beforeProcessing(payload, request);
35
36
  }
36
37
  catch (error) {
37
38
  valid = false;
38
39
  throw error;
39
40
  }
40
41
  finally {
41
- await this.batchController.create(this.config.adminIndex, 'payloads', {
42
+ await this.batch.create(this.config.adminIndex, 'payloads', {
42
43
  deviceModel: decoder.deviceModel,
44
+ payload,
43
45
  uuid,
44
46
  valid,
45
- payload,
46
47
  }, uuid);
47
48
  }
48
- const deviceContent = await decoder.decode(payload, request);
49
- // Inject payload uuid
50
- for (const measure of Object.values(deviceContent.measures)) {
51
- if (!measure.payloadUuid) {
52
- measure.payloadUuid = uuid;
53
- }
49
+ const decodedPayload = await decoder.decode(payload, request);
50
+ const newMeasures = [];
51
+ const deviceId = models_1.Device.id(decoder.deviceModel, decodedPayload.reference);
52
+ for (const [type, measure] of Object.entries(decodedPayload.measures)) {
53
+ newMeasures.push({
54
+ measuredAt: measure.measuredAt,
55
+ origin: {
56
+ assetId: null,
57
+ id: deviceId,
58
+ model: decoder.deviceModel,
59
+ payloadUuids: [uuid],
60
+ type: 'device',
61
+ },
62
+ type,
63
+ unit: this.measuresRegister.get(type).unit,
64
+ values: measure.values
65
+ });
54
66
  }
55
- if (!deviceContent.model) {
56
- deviceContent.model = decoder.deviceModel;
67
+ try {
68
+ const deviceDoc = await this.batch.get(this.config.adminIndex, 'devices', deviceId);
69
+ const device = new models_1.Device(deviceDoc._source);
70
+ if (device._source.assetId) {
71
+ for (const measure of newMeasures) {
72
+ measure.origin.assetId = device._source.assetId;
73
+ }
74
+ }
75
+ return await this.update(device, newMeasures, { refresh });
57
76
  }
58
- const device = new models_1.Device(deviceContent);
59
- const exists = await this.batchController.exists(this.config.adminIndex, 'devices', device._id);
60
- if (exists) {
61
- return await this.update(device, decoder, request, { refresh });
77
+ catch (error) {
78
+ if (error.id === 'services.storage.not_found') {
79
+ return this.provisionning(decoder.deviceModel, decodedPayload.reference, newMeasures, { refresh });
80
+ }
81
+ throw error;
62
82
  }
63
- return await this.deviceProvisionning(device, decoder, request, { refresh });
64
83
  }
65
- async register(device, decoder, request, { refresh }) {
66
- const enrichedDevice = await decoder.beforeRegister(device, request);
67
- await this.batchController.create(this.config.adminIndex, 'devices', enrichedDevice._source, enrichedDevice._id, { refresh });
68
- return await decoder.afterRegister(enrichedDevice, request);
84
+ async provisionning(model, reference, measures, { refresh }) {
85
+ const pluginConfig = await this.batch.get(this.config.adminIndex, this.config.adminCollections.config.name, 'plugin--device-manager');
86
+ const autoProvisioning = pluginConfig._source['device-manager'].provisioningStrategy === 'auto';
87
+ if (!autoProvisioning) {
88
+ throw new kuzzle_1.UnauthorizedError(`The device model "${model}" with reference "${reference}" is not registered on the platform.`);
89
+ }
90
+ const deviceId = models_1.Device.id(model, reference);
91
+ const deviceContent = {
92
+ measures,
93
+ model,
94
+ reference,
95
+ };
96
+ return this.register(deviceId, deviceContent, { refresh });
69
97
  }
70
98
  /**
71
- * Device provisioning strategy.
72
- *
73
- * If autoProvisionning is on, device is automatically registered, otherwise
74
- * we request the admin provisioning catalog to ensure this device is allowed
75
- * to register.
76
- *
77
- * After registration, we look at the admin provisioning catalog entry to:
78
- * - attach the device to a tenant
79
- * - link the device to an asset of this tenant
80
- *
81
- * Then we look into the tenant provisioning catalog entry to:
82
- * - link the device to an asset of this tenant
99
+ * Register a new device by creating the document in admin index
100
+ * @todo add before/afterRegister events
83
101
  */
84
- async deviceProvisionning(device, decoder, request, { refresh }) {
85
- const pluginConfigDocument = await this.batchController.get(this.config.adminIndex, this.config.configCollection, 'plugin--device-manager');
86
- const autoProvisionning = pluginConfigDocument._source['device-manager'].autoProvisionning;
87
- const catalogEntry = await this.getCatalogEntry(this.config.adminIndex, device._id);
88
- if (!autoProvisionning && !catalogEntry) {
89
- throw new kuzzle_1.BadRequestError(`Device ${device._id} is not provisionned.`);
90
- }
91
- if (!autoProvisionning && catalogEntry.content.authorized === false) {
92
- throw new kuzzle_1.BadRequestError(`Device ${device._id} is not allowed for registration.`);
93
- }
94
- const ret = await this.register(device, decoder, request, { refresh });
95
- // If there is not auto attachment to a tenant then we cannot link asset as well
96
- if (!catalogEntry || !catalogEntry.content.tenantId) {
97
- return ret;
98
- }
99
- await this.sdk.query({
100
- controller: 'device-manager/device',
101
- action: 'attachTenant',
102
- _id: device._id,
103
- index: catalogEntry.content.tenantId,
104
- });
105
- if (catalogEntry.content.assetId) {
106
- await this.sdk.query({
107
- controller: 'device-manager/device',
108
- action: 'linkAsset',
109
- _id: device._id,
110
- assetId: catalogEntry.content.assetId,
111
- });
112
- }
113
- const tenantCatalogEntry = await this.getCatalogEntry(catalogEntry.content.tenantId, device._id);
114
- if (tenantCatalogEntry
115
- && tenantCatalogEntry.content.authorized !== false
116
- && tenantCatalogEntry.content.assetId) {
117
- await this.sdk.query({
118
- controller: 'device-manager/device',
119
- action: 'linkAsset',
120
- _id: device._id,
121
- assetId: tenantCatalogEntry.content.assetId,
122
- });
123
- }
124
- return ret;
102
+ async register(deviceId, deviceContent, { refresh }) {
103
+ const deviceDoc = await this.batch.create(this.config.adminIndex, 'devices', deviceContent, deviceId, { refresh });
104
+ const device = new models_1.Device(deviceDoc._source);
105
+ return {
106
+ asset: null,
107
+ device: device.serialize(),
108
+ engineId: device._source.engineId,
109
+ };
125
110
  }
126
111
  /**
127
- * Get the device entry from the provisioning catalog in corresponding index
112
+ * Updates the device with the new measures:
113
+ * - in admin index
114
+ * - in engine index
115
+ * - in linked asset
116
+ * - historize measures in engine index
117
+ *
118
+ * @todo add before/afterUpdate events
128
119
  */
129
- async getCatalogEntry(index, deviceId) {
130
- try {
131
- const document = await this.batchController.get(index, this.config.configCollection, `catalog--${deviceId}`);
132
- return new models_1.Catalog(document);
133
- }
134
- catch (error) {
135
- if (error.id !== 'services.storage.not_found') {
136
- throw error;
137
- }
138
- return null;
139
- }
140
- }
141
- async update(device, decoder, request, { refresh }) {
120
+ async update(device, newMeasures, { refresh }) {
142
121
  const refreshableCollections = [];
143
- const previousDevice = await this.batchController.get(this.config.adminIndex, 'devices', device._id);
144
- const enrichedDevice = await decoder.beforeUpdate(device, request);
145
- const deviceDocument = await this.batchController.update(this.config.adminIndex, 'devices', enrichedDevice._id, enrichedDevice._source, { source: true, retryOnConflict: 10 });
146
- const updatedDevice = new models_1.Device(deviceDocument._source, deviceDocument._id);
122
+ const updatedDevice = await this.updateDevice(device, newMeasures);
147
123
  refreshableCollections.push([this.config.adminIndex, 'devices']);
148
- const tenantId = previousDevice._source.tenantId;
124
+ const engineId = updatedDevice._source.engineId;
149
125
  let updatedAsset = null;
150
126
  // Propagate device into tenant index
151
- if (tenantId) {
152
- await this.batchController.update(tenantId, 'devices', enrichedDevice._id, enrichedDevice._source, { retryOnConflict: 10 });
153
- refreshableCollections.push([tenantId, 'devices']);
127
+ if (engineId) {
128
+ await this.historizeMeasures(engineId, newMeasures);
129
+ await this.batch.update(engineId, 'devices', updatedDevice._id, updatedDevice._source, { retryOnConflict: 10 });
130
+ refreshableCollections.push([engineId, 'devices']);
154
131
  // Propagate measures into linked asset
155
- const assetId = previousDevice._source.assetId;
132
+ const assetId = updatedDevice._source.assetId;
156
133
  if (assetId) {
157
- updatedAsset = await this.propagateToAsset(tenantId, decoder, updatedDevice, assetId);
158
- await this.historizeAsset(tenantId, updatedAsset);
159
- refreshableCollections.push([tenantId, 'assets']);
134
+ updatedAsset = await this.propagateToAsset(engineId, newMeasures, assetId);
135
+ refreshableCollections.push([engineId, 'assets']);
160
136
  }
161
137
  }
162
138
  if (refresh === 'wait_for') {
163
139
  await Promise.all(refreshableCollections.map(([index, collection]) => (this.sdk.collection.refresh(index, collection))));
164
140
  }
165
- return decoder.afterUpdate(updatedDevice, updatedAsset, request);
141
+ return {
142
+ asset: updatedAsset ? updatedAsset.serialize() : null,
143
+ device: device.serialize(),
144
+ engineId,
145
+ };
146
+ }
147
+ /**
148
+ * Updates a device with the new measures
149
+ *
150
+ * @returns Updated device
151
+ */
152
+ async updateDevice(device, newMeasures) {
153
+ // dup array reference
154
+ const measures = newMeasures.map(m => m);
155
+ // Keep previous measures that were not updated
156
+ for (const previousMeasure of device._source.measures) {
157
+ if (!measures.find(m => m.type === previousMeasure.type)) {
158
+ measures.push(previousMeasure);
159
+ }
160
+ }
161
+ device._source.measures = measures;
162
+ const result = await global.app.trigger(`engine:${device._source.engineId}:device:measures:new`, { device, measures: newMeasures });
163
+ const deviceDocument = await this.batch.update(this.config.adminIndex, 'devices', result.device._id, result.device._source, { retryOnConflict: 10, source: true });
164
+ return new models_1.Device(deviceDocument._source);
166
165
  }
167
166
  /**
168
- * Propagate the measures inside the asset document.
167
+ * Save measures in engine "measures" collection
169
168
  */
170
- async propagateToAsset(tenantId, decoder, updatedDevice, assetId) {
171
- const measures = await decoder.copyToAsset(updatedDevice);
172
- const measureTypes = Object.keys(measures);
173
- const asset = await this.batchController.get(tenantId, 'assets', assetId);
174
- asset._source.measures = lodash_1.default.merge(asset._source.measures, measures);
175
- const { result } = await global.app.trigger(`engine:${tenantId}:asset:measures:new`, (0, utils_1.eventPayload)({ asset, measureTypes }));
176
- const assetDocument = await this.batchController.update(tenantId, 'assets', assetId, result.asset._source, { source: true, retryOnConflict: 10 });
177
- return new models_1.BaseAsset(assetDocument._source, assetDocument._id);
169
+ async historizeMeasures(engineId, measures) {
170
+ await Promise.all(measures.map(measure => {
171
+ return this.batch.create(engineId, 'measures', measure);
172
+ }));
178
173
  }
179
174
  /**
180
- * Creates an history entry for an asset
175
+ * Propagate the measures inside the linked asset document.
181
176
  */
182
- async historizeAsset(tenantId, asset) {
183
- await this.batchController.create(tenantId, 'asset-history', {
184
- assetId: asset._id,
185
- measureTypes: Object.keys(asset._source.measures),
186
- asset: lodash_1.default.omit(asset._source, ['_kuzzle_info']),
187
- });
177
+ async propagateToAsset(engineId, newMeasures, assetId) {
178
+ // dup array reference
179
+ const measures = newMeasures.map(m => m);
180
+ const asset = await this.batch.get(engineId, 'assets', assetId);
181
+ if (!lodash_1.default.isArray(asset._source.measures)) {
182
+ throw new kuzzle_1.BadRequestError(`Asset "${assetId}" measures property is not an array.`);
183
+ }
184
+ // Keep previous measures that were not updated
185
+ // array are updated in place so we need to keep previous elements
186
+ for (const previousMeasure of asset._source.measures) {
187
+ if (!measures.find(m => m.type === previousMeasure.type)) {
188
+ measures.push(previousMeasure);
189
+ }
190
+ }
191
+ asset._source.measures = measures;
192
+ // Give the list of new measures types in event payload
193
+ const result = await global.app.trigger(`engine:${engineId}:asset:measures:new`, { asset, measures: newMeasures });
194
+ const assetDocument = await this.batch.update(engineId, 'assets', assetId, result.asset._source, { retryOnConflict: 10, source: true });
195
+ return new models_1.BaseAsset(assetDocument._source, assetDocument._id);
188
196
  }
189
197
  }
190
198
  exports.PayloadService = PayloadService;
@@ -1,8 +1,9 @@
1
1
  export * from './PayloadService';
2
2
  export * from './DeviceManagerEngine';
3
3
  export * from './DeviceService';
4
- export * from './MigrationService';
5
- export * from './BatchProcessing';
6
4
  export * from './Decoder';
7
- export * from './CustomMappings/AssetMappingsManager';
8
- export * from './CustomMappings/DeviceMappingsManager';
5
+ export * from './AssetService';
6
+ export * from './registers/AssetsRegister';
7
+ export * from './registers/DevicesRegister';
8
+ export * from './registers/MeasuresRegister';
9
+ export * from './registers/DecodersRegister';
@@ -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.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
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];
@@ -13,9 +17,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
13
17
  __exportStar(require("./PayloadService"), exports);
14
18
  __exportStar(require("./DeviceManagerEngine"), exports);
15
19
  __exportStar(require("./DeviceService"), exports);
16
- __exportStar(require("./MigrationService"), exports);
17
- __exportStar(require("./BatchProcessing"), exports);
18
20
  __exportStar(require("./Decoder"), exports);
19
- __exportStar(require("./CustomMappings/AssetMappingsManager"), exports);
20
- __exportStar(require("./CustomMappings/DeviceMappingsManager"), exports);
21
+ __exportStar(require("./AssetService"), exports);
22
+ __exportStar(require("./registers/AssetsRegister"), exports);
23
+ __exportStar(require("./registers/DevicesRegister"), exports);
24
+ __exportStar(require("./registers/MeasuresRegister"), exports);
25
+ __exportStar(require("./registers/DecodersRegister"), exports);
21
26
  //# sourceMappingURL=index.js.map
@@ -1,11 +1,11 @@
1
1
  import { JSONObject } from 'kuzzle';
2
- import { DeviceMappingsManager } from './DeviceMappingsManager';
3
- export declare class AssetMappingsManager {
2
+ import { MeasuresRegister } from './MeasuresRegister';
3
+ export declare class AssetsRegister {
4
4
  private baseMappings;
5
- private deviceMappings;
5
+ private measuresRegister;
6
6
  private assetByName;
7
7
  private assetByGroup;
8
- constructor(baseMappings: JSONObject, deviceMappings: DeviceMappingsManager);
8
+ constructor(measuresRegister: MeasuresRegister);
9
9
  /**
10
10
  * Register an asset type with custom metadata.
11
11
  *
@@ -16,16 +16,15 @@ export declare class AssetMappingsManager {
16
16
  * @param metadata Asset type custom metadata
17
17
  * @param options.group Only inject this asset type to engine of this group
18
18
  *
19
- * @example
20
- *
19
+ * ```js
21
20
  * plugin.assets.register('container', {
22
21
  * size: { type: 'integer' },
23
22
  * serial: { type: 'keyword' },
24
23
  * });
25
- *
24
+ * ```
26
25
  */
27
- register(name: string, metadata: JSONObject, { group }?: {
26
+ register(type: string, metadata: JSONObject, { group }?: {
28
27
  group?: string;
29
28
  }): void;
30
- get(group?: string): JSONObject;
29
+ getMappings(group?: string): JSONObject;
31
30
  }
@@ -3,15 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.AssetMappingsManager = void 0;
6
+ exports.AssetsRegister = void 0;
7
7
  const lodash_1 = __importDefault(require("lodash"));
8
8
  const kuzzle_1 = require("kuzzle");
9
- class AssetMappingsManager {
10
- constructor(baseMappings, deviceMappings) {
9
+ const mappings_1 = require("../../mappings");
10
+ class AssetsRegister {
11
+ constructor(measuresRegister) {
11
12
  this.assetByName = new Map();
12
13
  this.assetByGroup = new Map();
13
- this.baseMappings = baseMappings;
14
- this.deviceMappings = deviceMappings;
14
+ this.baseMappings = JSON.parse(JSON.stringify(mappings_1.assetsMappings));
15
+ this.measuresRegister = measuresRegister;
15
16
  this.assetByGroup.set('commons', []);
16
17
  }
17
18
  /**
@@ -24,44 +25,32 @@ class AssetMappingsManager {
24
25
  * @param metadata Asset type custom metadata
25
26
  * @param options.group Only inject this asset type to engine of this group
26
27
  *
27
- * @example
28
- *
28
+ * ```js
29
29
  * plugin.assets.register('container', {
30
30
  * size: { type: 'integer' },
31
31
  * serial: { type: 'keyword' },
32
32
  * });
33
- *
33
+ * ```
34
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.`);
35
+ register(type, metadata, { group = 'commons' } = {}) {
36
+ if (this.assetByName.has(type)) {
37
+ throw new kuzzle_1.PluginImplementationError(`Asset of type "${type}" has already been registered.`);
38
38
  }
39
39
  const definition = {
40
- name,
41
40
  group,
42
41
  metadata,
42
+ type,
43
43
  };
44
- this.assetByName.set(name, definition);
44
+ this.assetByName.set(type, definition);
45
45
  if (!this.assetByGroup.has(group)) {
46
46
  this.assetByGroup.set(group, []);
47
47
  }
48
48
  this.assetByGroup.get(group).push(definition);
49
49
  }
50
- get(group = 'commons') {
50
+ getMappings(group = 'commons') {
51
51
  const mappings = JSON.parse(JSON.stringify(this.baseMappings));
52
- const deviceMappings = this.deviceMappings.get();
53
52
  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
- }
53
+ mappings.properties.measures = this.measuresRegister.getMappings();
65
54
  if (this.assetByGroup.has('commons')) {
66
55
  for (const definition of this.assetByGroup.get('commons')) {
67
56
  mappings.properties.metadata.properties = lodash_1.default.merge({}, mappings.properties.metadata.properties, definition.metadata);
@@ -75,5 +64,5 @@ class AssetMappingsManager {
75
64
  return mappings;
76
65
  }
77
66
  }
78
- exports.AssetMappingsManager = AssetMappingsManager;
79
- //# sourceMappingURL=AssetMappingsManager.js.map
67
+ exports.AssetsRegister = AssetsRegister;
68
+ //# sourceMappingURL=AssetsRegister.js.map
@@ -0,0 +1,46 @@
1
+ import { ControllerDefinition, PluginContext } from 'kuzzle';
2
+ import { Decoder } from '../Decoder';
3
+ import { DecoderContent } from '../../types/DecoderContent';
4
+ import { PayloadService } from '../PayloadService';
5
+ export declare class DecodersRegister {
6
+ private context;
7
+ private _decoders;
8
+ private get sdk();
9
+ get decoders(): Decoder[];
10
+ init(context: PluginContext): void;
11
+ list(): Promise<DecoderContent[]>;
12
+ /**
13
+ * Registers a new decoder for a device model.
14
+ *
15
+ * This will register a new API action:
16
+ * - controller: `device-manager/payload`
17
+ * - action: `action` property of the decoder or the device model in kebab-case
18
+ *
19
+ * @param decoder Instantiated decoder
20
+ *
21
+ * @returns Corresponding API action requestPayload
22
+ */
23
+ register(decoder: Decoder): {
24
+ action: string;
25
+ controller: string;
26
+ };
27
+ /**
28
+ * Build the PayloadController with registered decoders
29
+ *
30
+ * @todo generate OpenAPI specification
31
+ *
32
+ * @internal
33
+ */
34
+ getPayloadController(payloadService: PayloadService): ControllerDefinition;
35
+ printDecoders(): void;
36
+ /**
37
+ * Creates default roles, profiles and users associated to the generated actions
38
+ * in the Payload controller.
39
+ *
40
+ * This method never returns a rejected promise.
41
+ */
42
+ createDefaultRights(): Promise<void>;
43
+ private createDefaultUsers;
44
+ private createDefaultProfiles;
45
+ private createDefaultRoles;
46
+ }