kuzzle-device-manager 0.4.8 → 0.4.12
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 +4 -0
- package/index.js +17 -0
- package/lib/DeviceManagerPlugin.d.ts +100 -0
- package/lib/DeviceManagerPlugin.js +237 -0
- package/lib/controllers/AssetController.d.ts +7 -0
- package/lib/controllers/AssetController.js +53 -0
- package/lib/controllers/CRUDController.d.ts +34 -0
- package/lib/controllers/CRUDController.js +65 -0
- package/lib/controllers/DeviceController.d.ts +56 -0
- package/lib/controllers/DeviceController.js +218 -0
- package/lib/controllers/index.d.ts +3 -0
- package/lib/controllers/index.js +16 -0
- package/lib/core-classes/BatchProcessing/BatchController.d.ts +34 -0
- package/lib/core-classes/BatchProcessing/BatchController.js +115 -0
- package/lib/core-classes/BatchProcessing/BatchWriter.d.ts +110 -0
- package/lib/core-classes/BatchProcessing/BatchWriter.js +264 -0
- package/lib/core-classes/BatchProcessing/InstrumentablePromise.d.ts +6 -0
- package/lib/core-classes/BatchProcessing/InstrumentablePromise.js +13 -0
- package/lib/core-classes/BatchProcessing/index.d.ts +3 -0
- package/lib/core-classes/BatchProcessing/index.js +16 -0
- package/lib/core-classes/CustomMappings/AssetMappingsManager.d.ts +31 -0
- package/lib/core-classes/CustomMappings/AssetMappingsManager.js +79 -0
- package/lib/core-classes/CustomMappings/DeviceMappingsManager.d.ts +43 -0
- package/lib/core-classes/CustomMappings/DeviceMappingsManager.js +71 -0
- package/lib/core-classes/Decoder.d.ts +133 -0
- package/lib/core-classes/Decoder.js +166 -0
- package/lib/core-classes/DeviceManagerEngine.d.ts +21 -0
- package/lib/core-classes/DeviceManagerEngine.js +64 -0
- package/lib/core-classes/DeviceService.d.ts +41 -0
- package/lib/core-classes/DeviceService.js +264 -0
- package/lib/core-classes/MigrationService.d.ts +13 -0
- package/lib/core-classes/MigrationService.js +49 -0
- package/lib/core-classes/PayloadService.d.ts +43 -0
- package/lib/core-classes/PayloadService.js +191 -0
- package/lib/core-classes/index.d.ts +8 -0
- package/lib/core-classes/index.js +21 -0
- package/lib/models/BaseAsset.d.ts +56 -0
- package/lib/models/BaseAsset.js +58 -0
- package/lib/models/Catalog.d.ts +55 -0
- package/lib/models/Catalog.js +38 -0
- package/lib/models/Config.d.ts +33 -0
- package/lib/models/Config.js +26 -0
- package/lib/models/Device.d.ts +102 -0
- package/lib/models/Device.js +91 -0
- package/lib/models/index.d.ts +3 -0
- package/lib/models/index.js +16 -0
- package/lib/types/AssetsHistoryContent.d.ts +9 -0
- package/lib/types/AssetsHistoryContent.js +3 -0
- package/lib/types/BaseAssetContent.d.ts +8 -0
- package/lib/types/BaseAssetContent.js +3 -0
- package/lib/types/DeviceContent.d.ts +62 -0
- package/lib/types/DeviceContent.js +3 -0
- package/lib/types/Measure.d.ts +54 -0
- package/lib/types/Measure.js +3 -0
- package/lib/types/index.d.ts +3 -0
- package/lib/types/index.js +16 -0
- package/lib/utils/events.d.ts +8 -0
- package/lib/utils/events.js +17 -0
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +14 -0
- package/package.json +1 -1
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
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("./lib/DeviceManagerPlugin"), exports);
|
|
14
|
+
__exportStar(require("./lib/core-classes"), exports);
|
|
15
|
+
__exportStar(require("./lib/models"), exports);
|
|
16
|
+
__exportStar(require("./lib/types"), exports);
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Plugin, PluginContext, JSONObject } from 'kuzzle';
|
|
2
|
+
import { Decoder, PayloadHandler, AssetMappingsManager, DeviceMappingsManager } from './core-classes';
|
|
3
|
+
export declare type DeviceManagerConfig = {
|
|
4
|
+
/**
|
|
5
|
+
* Administration index name
|
|
6
|
+
*/
|
|
7
|
+
adminIndex: string;
|
|
8
|
+
/**
|
|
9
|
+
* Config collection name (in admin index)
|
|
10
|
+
*/
|
|
11
|
+
configCollection: string;
|
|
12
|
+
/**
|
|
13
|
+
* Administration collections mappings (in admin index)
|
|
14
|
+
*/
|
|
15
|
+
adminCollections: {
|
|
16
|
+
config: JSONObject;
|
|
17
|
+
devices: JSONObject;
|
|
18
|
+
payloads: JSONObject;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Tenants collections
|
|
22
|
+
*/
|
|
23
|
+
collections: {
|
|
24
|
+
assets: JSONObject;
|
|
25
|
+
devices: JSONObject;
|
|
26
|
+
'asset-history': JSONObject;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Interval to write documents from the buffer
|
|
30
|
+
*/
|
|
31
|
+
writerInterval: number;
|
|
32
|
+
};
|
|
33
|
+
export declare class DeviceManagerPlugin extends Plugin {
|
|
34
|
+
config: DeviceManagerConfig;
|
|
35
|
+
private defaultConfig;
|
|
36
|
+
private assetController;
|
|
37
|
+
private deviceController;
|
|
38
|
+
private engineController;
|
|
39
|
+
private payloadService;
|
|
40
|
+
private deviceManagerEngine;
|
|
41
|
+
private deviceService;
|
|
42
|
+
private migrationService;
|
|
43
|
+
private batchWriter;
|
|
44
|
+
private get sdk();
|
|
45
|
+
/**
|
|
46
|
+
* List of registered decoders.
|
|
47
|
+
* Map<model, decoder>
|
|
48
|
+
*/
|
|
49
|
+
private decoders;
|
|
50
|
+
private deviceMappings;
|
|
51
|
+
private assetMappings;
|
|
52
|
+
get assets(): AssetMappingsManager;
|
|
53
|
+
get devices(): DeviceMappingsManager;
|
|
54
|
+
/**
|
|
55
|
+
* Constructor
|
|
56
|
+
*/
|
|
57
|
+
constructor();
|
|
58
|
+
/**
|
|
59
|
+
* Init the plugin
|
|
60
|
+
*/
|
|
61
|
+
init(config: JSONObject, context: PluginContext): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Registers a new decoder for a device model.
|
|
64
|
+
*
|
|
65
|
+
* This will register a new API action:
|
|
66
|
+
* - controller: `"device-manager/payload"`
|
|
67
|
+
* - action: `action` property of the decoder or the device model in kebab-case
|
|
68
|
+
*
|
|
69
|
+
* If a custom payload handler is given then it will be used to process payloads
|
|
70
|
+
* instead of the PayloadService.process method.
|
|
71
|
+
*
|
|
72
|
+
* @param decoder Instantiated decoder
|
|
73
|
+
* @param options.handler Custom payload handler
|
|
74
|
+
*
|
|
75
|
+
* @returns Corresponding API action requestPayload
|
|
76
|
+
*/
|
|
77
|
+
registerDecoder(decoder: Decoder, { handler }?: {
|
|
78
|
+
handler?: PayloadHandler;
|
|
79
|
+
}): {
|
|
80
|
+
controller: string;
|
|
81
|
+
action: string;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Initialize the administration index of the plugin
|
|
85
|
+
*/
|
|
86
|
+
private initDatabase;
|
|
87
|
+
/**
|
|
88
|
+
* Merge custom mappings defined in the Decoder into the "payloads" collection
|
|
89
|
+
* mappings.
|
|
90
|
+
*
|
|
91
|
+
* Those custom mappings allow to search raw payloads more efficiently.
|
|
92
|
+
*/
|
|
93
|
+
private getPayloadsMappings;
|
|
94
|
+
/**
|
|
95
|
+
* Initialize the config document if it does not exists
|
|
96
|
+
*/
|
|
97
|
+
private initializeConfig;
|
|
98
|
+
private pipeCheckEngine;
|
|
99
|
+
private generateConfigID;
|
|
100
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
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.DeviceManagerPlugin = void 0;
|
|
7
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
8
|
+
const kuzzle_1 = require("kuzzle");
|
|
9
|
+
const kuzzle_plugin_commons_1 = require("kuzzle-plugin-commons");
|
|
10
|
+
const controllers_1 = require("./controllers");
|
|
11
|
+
const core_classes_1 = require("./core-classes");
|
|
12
|
+
const models_1 = require("./models");
|
|
13
|
+
class DeviceManagerPlugin extends kuzzle_1.Plugin {
|
|
14
|
+
/**
|
|
15
|
+
* Constructor
|
|
16
|
+
*/
|
|
17
|
+
constructor() {
|
|
18
|
+
super({
|
|
19
|
+
kuzzleVersion: '>=2.14.0 <3'
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* List of registered decoders.
|
|
23
|
+
* Map<model, decoder>
|
|
24
|
+
*/
|
|
25
|
+
this.decoders = new Map();
|
|
26
|
+
this.deviceMappings = new core_classes_1.DeviceMappingsManager(models_1.devicesMappings);
|
|
27
|
+
this.assetMappings = new core_classes_1.AssetMappingsManager(models_1.assetsMappings, this.deviceMappings);
|
|
28
|
+
this.api = {
|
|
29
|
+
'device-manager/payload': {
|
|
30
|
+
actions: {}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
this.defaultConfig = {
|
|
34
|
+
adminIndex: 'device-manager',
|
|
35
|
+
configCollection: 'config',
|
|
36
|
+
adminCollections: {
|
|
37
|
+
config: {
|
|
38
|
+
dynamic: 'strict',
|
|
39
|
+
properties: {
|
|
40
|
+
type: { type: 'keyword' },
|
|
41
|
+
engine: {
|
|
42
|
+
properties: {
|
|
43
|
+
index: { type: 'keyword' },
|
|
44
|
+
group: { type: 'keyword' },
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
catalog: models_1.catalogMappings,
|
|
48
|
+
'device-manager': {
|
|
49
|
+
properties: {
|
|
50
|
+
autoProvisionning: { type: 'boolean' },
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
devices: models_1.devicesMappings,
|
|
56
|
+
payloads: {
|
|
57
|
+
dynamic: 'strict',
|
|
58
|
+
properties: {
|
|
59
|
+
uuid: { type: 'keyword' },
|
|
60
|
+
valid: { type: 'boolean' },
|
|
61
|
+
deviceModel: { type: 'keyword' },
|
|
62
|
+
payload: {
|
|
63
|
+
dynamic: 'false',
|
|
64
|
+
properties: {}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
collections: {
|
|
70
|
+
assets: models_1.assetsMappings,
|
|
71
|
+
devices: models_1.devicesMappings,
|
|
72
|
+
'asset-history': models_1.assetsHistoryMappings,
|
|
73
|
+
},
|
|
74
|
+
writerInterval: 50
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
get sdk() {
|
|
78
|
+
return this.context.accessors.sdk;
|
|
79
|
+
}
|
|
80
|
+
get assets() {
|
|
81
|
+
return this.assetMappings;
|
|
82
|
+
}
|
|
83
|
+
get devices() {
|
|
84
|
+
return this.deviceMappings;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Init the plugin
|
|
88
|
+
*/
|
|
89
|
+
async init(config, context) {
|
|
90
|
+
this.config = lodash_1.default.merge({}, this.defaultConfig, config);
|
|
91
|
+
this.context = context;
|
|
92
|
+
this.batchWriter = new core_classes_1.BatchWriter(this.sdk, { interval: this.config.writerInterval });
|
|
93
|
+
this.batchWriter.begin();
|
|
94
|
+
this.payloadService = new core_classes_1.PayloadService(this, this.batchWriter);
|
|
95
|
+
this.deviceService = new core_classes_1.DeviceService(this, this.decoders);
|
|
96
|
+
this.migrationService = new core_classes_1.MigrationService('device-manager', this);
|
|
97
|
+
this.deviceManagerEngine = new core_classes_1.DeviceManagerEngine(this, this.assetMappings, this.deviceMappings);
|
|
98
|
+
this.assetController = new controllers_1.AssetController(this);
|
|
99
|
+
this.deviceController = new controllers_1.DeviceController(this, this.deviceService);
|
|
100
|
+
this.engineController = new kuzzle_plugin_commons_1.EngineController('device-manager', this, this.deviceManagerEngine);
|
|
101
|
+
this.api['device-manager/asset'] = this.assetController.definition;
|
|
102
|
+
this.api['device-manager/device'] = this.deviceController.definition;
|
|
103
|
+
this.pipes = {
|
|
104
|
+
'device-manager/device:beforeUpdate': this.pipeCheckEngine.bind(this),
|
|
105
|
+
'device-manager/device:beforeSearch': this.pipeCheckEngine.bind(this),
|
|
106
|
+
'device-manager/device:beforeAttachTenant': this.pipeCheckEngine.bind(this),
|
|
107
|
+
'device-manager/asset:before*': this.pipeCheckEngine.bind(this),
|
|
108
|
+
'document:beforeCreate': this.generateConfigID.bind(this),
|
|
109
|
+
};
|
|
110
|
+
await this.initDatabase();
|
|
111
|
+
await this.migrationService.run();
|
|
112
|
+
for (const decoder of this.decoders.values()) {
|
|
113
|
+
this.context.log.info(`Register API action "device-manager/payload:${decoder.action}" with decoder "${decoder.constructor.name}" for device "${decoder.deviceModel}"`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Registers a new decoder for a device model.
|
|
118
|
+
*
|
|
119
|
+
* This will register a new API action:
|
|
120
|
+
* - controller: `"device-manager/payload"`
|
|
121
|
+
* - action: `action` property of the decoder or the device model in kebab-case
|
|
122
|
+
*
|
|
123
|
+
* If a custom payload handler is given then it will be used to process payloads
|
|
124
|
+
* instead of the PayloadService.process method.
|
|
125
|
+
*
|
|
126
|
+
* @param decoder Instantiated decoder
|
|
127
|
+
* @param options.handler Custom payload handler
|
|
128
|
+
*
|
|
129
|
+
* @returns Corresponding API action requestPayload
|
|
130
|
+
*/
|
|
131
|
+
registerDecoder(decoder, { handler } = {}) {
|
|
132
|
+
decoder.action = decoder.action || kuzzle_1.Inflector.kebabCase(decoder.deviceModel);
|
|
133
|
+
if (this.api['device-manager/payload'].actions[decoder.action]) {
|
|
134
|
+
throw new kuzzle_1.PluginImplementationError(`A decoder for "${decoder.deviceModel}" has already been registered.`);
|
|
135
|
+
}
|
|
136
|
+
this.api['device-manager/payload'].actions[decoder.action] = {
|
|
137
|
+
handler: request => handler ? handler(request, decoder) : this.payloadService.process(request, decoder),
|
|
138
|
+
http: decoder.http,
|
|
139
|
+
};
|
|
140
|
+
this.decoders.set(decoder.deviceModel, decoder);
|
|
141
|
+
return {
|
|
142
|
+
controller: 'device-manager/payload',
|
|
143
|
+
action: decoder.action,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Initialize the administration index of the plugin
|
|
148
|
+
*/
|
|
149
|
+
async initDatabase() {
|
|
150
|
+
const mutex = new kuzzle_1.Mutex('device-manager/initDatabase');
|
|
151
|
+
await mutex.lock();
|
|
152
|
+
try {
|
|
153
|
+
if (!await this.sdk.index.exists(this.config.adminIndex)) {
|
|
154
|
+
// Possible race condition because of index cache propagation.
|
|
155
|
+
// The index has been created but the node didn't receive the index
|
|
156
|
+
// cache update message yet, causing index:exists to returns false
|
|
157
|
+
try {
|
|
158
|
+
await this.sdk.index.create(this.config.adminIndex);
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
if (!error.message.includes('already exists')) {
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
await Promise.all([
|
|
167
|
+
this.sdk.collection.create(this.config.adminIndex, this.config.configCollection, this.config.adminCollections.config),
|
|
168
|
+
this.sdk.collection.create(this.config.adminIndex, 'devices', this.deviceMappings.get()),
|
|
169
|
+
this.sdk.collection.create(this.config.adminIndex, 'payloads', this.getPayloadsMappings()),
|
|
170
|
+
]);
|
|
171
|
+
await this.initializeConfig();
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
// When nodes are starting at the same time, the index cache synchronization
|
|
175
|
+
// message is received too late so index.exists returns false
|
|
176
|
+
if (error.id !== 'services.storage.index_already_exists') {
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
finally {
|
|
181
|
+
await mutex.unlock();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Merge custom mappings defined in the Decoder into the "payloads" collection
|
|
186
|
+
* mappings.
|
|
187
|
+
*
|
|
188
|
+
* Those custom mappings allow to search raw payloads more efficiently.
|
|
189
|
+
*/
|
|
190
|
+
getPayloadsMappings() {
|
|
191
|
+
const mappings = JSON.parse(JSON.stringify(this.config.adminCollections.payloads));
|
|
192
|
+
for (const decoder of this.decoders.values()) {
|
|
193
|
+
mappings.properties.payload.properties = {
|
|
194
|
+
...mappings.properties.payload.properties,
|
|
195
|
+
...decoder.payloadsMappings
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
return mappings;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Initialize the config document if it does not exists
|
|
202
|
+
*/
|
|
203
|
+
async initializeConfig() {
|
|
204
|
+
const exists = await this.sdk.document.exists(this.config.adminIndex, this.config.configCollection, 'plugin--device-manager');
|
|
205
|
+
if (!exists) {
|
|
206
|
+
await this.sdk.document.create(this.config.adminIndex, this.config.configCollection, {
|
|
207
|
+
type: 'device-manager',
|
|
208
|
+
'device-manager': { autoProvisionning: true }
|
|
209
|
+
}, 'plugin--device-manager');
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
async pipeCheckEngine(request) {
|
|
213
|
+
const index = request.getIndex();
|
|
214
|
+
const { result: { exists } } = await this.sdk.query({
|
|
215
|
+
controller: 'device-manager/engine',
|
|
216
|
+
action: 'exists',
|
|
217
|
+
index,
|
|
218
|
+
});
|
|
219
|
+
if (!exists) {
|
|
220
|
+
throw new kuzzle_1.BadRequestError(`Tenant "${index}" does not have a device-manager engine`);
|
|
221
|
+
}
|
|
222
|
+
return request;
|
|
223
|
+
}
|
|
224
|
+
async generateConfigID(request) {
|
|
225
|
+
if (request.getCollection() !== this.config.configCollection) {
|
|
226
|
+
return request;
|
|
227
|
+
}
|
|
228
|
+
const document = request.getBody();
|
|
229
|
+
if (document.type !== 'catalog' || request.getId(({ ifMissing: 'ignore' }))) {
|
|
230
|
+
return request;
|
|
231
|
+
}
|
|
232
|
+
request.input.args._id = `catalog--${document.catalog.deviceId}`;
|
|
233
|
+
return request;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
exports.DeviceManagerPlugin = DeviceManagerPlugin;
|
|
237
|
+
//# sourceMappingURL=DeviceManagerPlugin.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { KuzzleRequest, EmbeddedSDK, Plugin } from 'kuzzle';
|
|
2
|
+
import { CRUDController } from './CRUDController';
|
|
3
|
+
export declare class AssetController extends CRUDController {
|
|
4
|
+
get sdk(): EmbeddedSDK;
|
|
5
|
+
constructor(plugin: Plugin);
|
|
6
|
+
create(request: KuzzleRequest): Promise<import("kuzzle").Document>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AssetController = void 0;
|
|
4
|
+
const CRUDController_1 = require("./CRUDController");
|
|
5
|
+
const BaseAsset_1 = require("../models/BaseAsset");
|
|
6
|
+
class AssetController extends CRUDController_1.CRUDController {
|
|
7
|
+
get sdk() {
|
|
8
|
+
return this.context.accessors.sdk;
|
|
9
|
+
}
|
|
10
|
+
constructor(plugin) {
|
|
11
|
+
super(plugin, 'assets');
|
|
12
|
+
this.definition = {
|
|
13
|
+
actions: {
|
|
14
|
+
create: {
|
|
15
|
+
handler: this.create.bind(this),
|
|
16
|
+
http: [{ verb: 'post', path: 'device-manager/:index/assets' }],
|
|
17
|
+
},
|
|
18
|
+
update: {
|
|
19
|
+
handler: this.update.bind(this),
|
|
20
|
+
http: [{ verb: 'put', path: 'device-manager/:index/assets/:_id' }],
|
|
21
|
+
},
|
|
22
|
+
delete: {
|
|
23
|
+
handler: this.delete.bind(this),
|
|
24
|
+
http: [{ verb: 'delete', path: 'device-manager/:index/assets/:_id' }],
|
|
25
|
+
},
|
|
26
|
+
search: {
|
|
27
|
+
handler: this.search.bind(this),
|
|
28
|
+
http: [
|
|
29
|
+
{ verb: 'post', path: 'device-manager/:index/assets/_search' },
|
|
30
|
+
{ verb: 'get', path: 'device-manager/:index/assets/_search' },
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async create(request) {
|
|
37
|
+
const type = request.getBodyString('type');
|
|
38
|
+
const model = request.getBodyString('model');
|
|
39
|
+
const reference = request.getBodyString('reference');
|
|
40
|
+
if (!request.input.resource._id) {
|
|
41
|
+
const assetContent = {
|
|
42
|
+
type,
|
|
43
|
+
model,
|
|
44
|
+
reference,
|
|
45
|
+
};
|
|
46
|
+
const asset = new BaseAsset_1.BaseAsset(assetContent);
|
|
47
|
+
request.input.resource._id = asset._id;
|
|
48
|
+
}
|
|
49
|
+
return await super.create(request);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.AssetController = AssetController;
|
|
53
|
+
//# sourceMappingURL=AssetController.js.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { KuzzleRequest, PluginContext, ControllerDefinition, Plugin } from 'kuzzle';
|
|
2
|
+
import { DeviceManagerConfig } from '../DeviceManagerPlugin';
|
|
3
|
+
export declare class CRUDController {
|
|
4
|
+
protected context: PluginContext;
|
|
5
|
+
protected config: DeviceManagerConfig;
|
|
6
|
+
private collection;
|
|
7
|
+
definition: ControllerDefinition;
|
|
8
|
+
constructor(plugin: Plugin, collection: string);
|
|
9
|
+
get as(): (user: any) => import("kuzzle").EmbeddedSDK;
|
|
10
|
+
/**
|
|
11
|
+
* Create an asset or a device depending on the collection.
|
|
12
|
+
*
|
|
13
|
+
* @param request
|
|
14
|
+
*/
|
|
15
|
+
create(request: KuzzleRequest): Promise<import("kuzzle").Document>;
|
|
16
|
+
/**
|
|
17
|
+
* Delete an asset or a device depending on the collection.
|
|
18
|
+
*
|
|
19
|
+
* @param request
|
|
20
|
+
*/
|
|
21
|
+
delete(request: KuzzleRequest): Promise<number>;
|
|
22
|
+
/**
|
|
23
|
+
* search assets or devices depending on the collection.
|
|
24
|
+
*
|
|
25
|
+
* @param request
|
|
26
|
+
*/
|
|
27
|
+
search(request: KuzzleRequest): Promise<any>;
|
|
28
|
+
/**
|
|
29
|
+
* Create an asset or a device depending on the collection.
|
|
30
|
+
*
|
|
31
|
+
* @param request
|
|
32
|
+
*/
|
|
33
|
+
update(request: KuzzleRequest): Promise<import("kuzzle").Document>;
|
|
34
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CRUDController = void 0;
|
|
4
|
+
class CRUDController {
|
|
5
|
+
constructor(plugin, collection) {
|
|
6
|
+
this.config = plugin.config;
|
|
7
|
+
this.context = plugin.context;
|
|
8
|
+
this.collection = collection;
|
|
9
|
+
}
|
|
10
|
+
get as() {
|
|
11
|
+
return user => this.context.accessors.sdk.as(user, { checkRights: true });
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Create an asset or a device depending on the collection.
|
|
15
|
+
*
|
|
16
|
+
* @param request
|
|
17
|
+
*/
|
|
18
|
+
async create(request) {
|
|
19
|
+
const index = request.getIndex();
|
|
20
|
+
const asset = request.getBody();
|
|
21
|
+
const id = request.input.resource._id;
|
|
22
|
+
return this.as(request.context.user).document.create(index, this.collection, asset, id, { ...request.input.args });
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Delete an asset or a device depending on the collection.
|
|
26
|
+
*
|
|
27
|
+
* @param request
|
|
28
|
+
*/
|
|
29
|
+
async delete(request) {
|
|
30
|
+
const index = request.getIndex();
|
|
31
|
+
const id = request.getId();
|
|
32
|
+
return this.as(request.context.user).document.delete(index, this.collection, id, { ...request.input.args });
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* search assets or devices depending on the collection.
|
|
36
|
+
*
|
|
37
|
+
* @param request
|
|
38
|
+
*/
|
|
39
|
+
async search(request) {
|
|
40
|
+
const index = request.getIndex();
|
|
41
|
+
const { searchBody } = request.getSearchParams();
|
|
42
|
+
const res = await this.as(request.context.user).query({
|
|
43
|
+
controller: 'document',
|
|
44
|
+
action: 'search',
|
|
45
|
+
index,
|
|
46
|
+
collection: this.collection,
|
|
47
|
+
body: searchBody,
|
|
48
|
+
...request.input.args
|
|
49
|
+
});
|
|
50
|
+
return res.result;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Create an asset or a device depending on the collection.
|
|
54
|
+
*
|
|
55
|
+
* @param request
|
|
56
|
+
*/
|
|
57
|
+
async update(request) {
|
|
58
|
+
const index = request.getIndex();
|
|
59
|
+
const body = request.getBody();
|
|
60
|
+
const id = request.getId();
|
|
61
|
+
return this.as(request.context.user).document.update(index, this.collection, id, body, { ...request.input.args });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.CRUDController = CRUDController;
|
|
65
|
+
//# sourceMappingURL=CRUDController.js.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { KuzzleRequest, EmbeddedSDK, Plugin } from 'kuzzle';
|
|
2
|
+
import { CRUDController } from './CRUDController';
|
|
3
|
+
import { DeviceService } from '../core-classes';
|
|
4
|
+
export declare class DeviceController extends CRUDController {
|
|
5
|
+
private deviceService;
|
|
6
|
+
get sdk(): EmbeddedSDK;
|
|
7
|
+
constructor(plugin: Plugin, deviceService: DeviceService);
|
|
8
|
+
/**
|
|
9
|
+
* Attach a device to a tenant
|
|
10
|
+
*/
|
|
11
|
+
attachTenant(request: KuzzleRequest): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Attach multiple devices to multiple tenants
|
|
14
|
+
*/
|
|
15
|
+
mAttach(request: KuzzleRequest): Promise<import("../types").DeviceMAttachementContent>;
|
|
16
|
+
/**
|
|
17
|
+
* Unattach a device from it's tenant
|
|
18
|
+
*/
|
|
19
|
+
detach(request: KuzzleRequest): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Detach multiple devices from multiple tenants
|
|
22
|
+
*/
|
|
23
|
+
mDetach(request: KuzzleRequest): Promise<{
|
|
24
|
+
errors: any[];
|
|
25
|
+
successes: any[];
|
|
26
|
+
}>;
|
|
27
|
+
/**
|
|
28
|
+
* Link a device to an asset.
|
|
29
|
+
* @todo there is not restriction according to tenant index?
|
|
30
|
+
*/
|
|
31
|
+
linkAsset(request: KuzzleRequest): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Link multiple devices to multiple assets.
|
|
34
|
+
*/
|
|
35
|
+
mLink(request: KuzzleRequest): Promise<{
|
|
36
|
+
errors: any[];
|
|
37
|
+
successes: any[];
|
|
38
|
+
}>;
|
|
39
|
+
/**
|
|
40
|
+
* Unlink a device from an asset.
|
|
41
|
+
*/
|
|
42
|
+
unlink(request: KuzzleRequest): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Unlink multiple device from multiple assets.
|
|
45
|
+
*/
|
|
46
|
+
mUnlink(request: KuzzleRequest): Promise<{
|
|
47
|
+
errors: any[];
|
|
48
|
+
successes: any[];
|
|
49
|
+
}>;
|
|
50
|
+
/**
|
|
51
|
+
* Clean payload collection for a time period
|
|
52
|
+
*/
|
|
53
|
+
prunePayloads(request: KuzzleRequest): Promise<any>;
|
|
54
|
+
private mGetDevice;
|
|
55
|
+
private mParseRequest;
|
|
56
|
+
}
|