@wiotp/sdk 0.4.2 → 0.6.2

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 (48) hide show
  1. package/LICENSE +203 -203
  2. package/README.md +68 -68
  3. package/dist/BaseClient.js +259 -0
  4. package/dist/BaseConfig.js +194 -0
  5. package/dist/api/ApiClient.js +508 -0
  6. package/dist/api/ApiErrors.js +118 -0
  7. package/dist/api/DscClient.js +332 -0
  8. package/dist/api/LecClient.js +48 -0
  9. package/dist/api/MgmtClient.js +77 -0
  10. package/dist/api/RegistryClient.js +234 -0
  11. package/dist/api/RulesClient.js +105 -0
  12. package/dist/api/StateClient.js +738 -0
  13. package/dist/application/ApplicationClient.js +436 -0
  14. package/dist/application/ApplicationConfig.js +233 -0
  15. package/dist/application/index.js +23 -0
  16. package/dist/bundled/wiotp-bundle.js +35592 -0
  17. package/dist/bundled/wiotp-bundle.min.js +47 -0
  18. package/dist/device/DeviceClient.js +125 -0
  19. package/dist/device/DeviceConfig.js +216 -0
  20. package/dist/device/index.js +23 -0
  21. package/dist/gateway/GatewayClient.js +159 -0
  22. package/dist/gateway/GatewayConfig.js +52 -0
  23. package/dist/gateway/index.js +23 -0
  24. package/dist/index.js +55 -0
  25. package/dist/util.js +50 -0
  26. package/package.json +92 -84
  27. package/src/BaseClient.js +215 -215
  28. package/src/BaseConfig.js +157 -157
  29. package/src/api/ApiClient.js +454 -454
  30. package/src/api/ApiErrors.js +33 -33
  31. package/src/api/DscClient.js +164 -145
  32. package/src/api/LecClient.js +32 -32
  33. package/src/api/MgmtClient.js +57 -57
  34. package/src/api/RegistryClient.js +194 -194
  35. package/src/api/RulesClient.js +84 -84
  36. package/src/api/StateClient.js +650 -650
  37. package/src/application/ApplicationClient.js +348 -348
  38. package/src/application/ApplicationConfig.js +191 -191
  39. package/src/application/index.js +12 -12
  40. package/src/device/DeviceClient.js +78 -78
  41. package/src/device/DeviceConfig.js +175 -175
  42. package/src/device/index.js +14 -14
  43. package/src/gateway/GatewayClient.js +114 -114
  44. package/src/gateway/GatewayConfig.js +21 -21
  45. package/src/gateway/index.js +13 -13
  46. package/{index.js → src/index.js} +19 -19
  47. package/src/util.js +38 -38
  48. package/src/util/IoTFoundation.pem +0 -82
@@ -1,651 +1,651 @@
1
- /**
2
- *****************************************************************************
3
- Copyright (c) 2014, 2019 IBM Corporation and other Contributors.
4
- All rights reserved. This program and the accompanying materials
5
- are made available under the terms of the Eclipse Public License v1.0
6
- which accompanies this distribution, and is available at
7
- http://www.eclipse.org/legal/epl-v10.html
8
- *****************************************************************************
9
- *
10
- */
11
- import log from 'loglevel';
12
-
13
- export default class StateClient {
14
- constructor(apiClient) {
15
- this.log = log;
16
-
17
- this.apiClient = apiClient;
18
- this.draftMode = true;
19
-
20
- // Create an alias to the apiClient's methods that we use
21
- this.callApi = this.apiClient.callApi.bind(this.apiClient);
22
- this.parseSortSpec = this.apiClient.parseSortSpec.bind(this.apiClient);
23
- this.callFormDataApi = this.apiClient.callFormDataApi.bind(this.apiClient);
24
- this.invalidOperation = this.apiClient.invalidOperation.bind(this.apiClient);
25
- }
26
-
27
- workWithActive() {
28
- this.draftMode = false;
29
- }
30
-
31
- workWithDraft() {
32
- this.draftMode = true;
33
- }
34
-
35
- // IM Device state API
36
- createSchema(schemaContents, name, description) {
37
- var body = {
38
- 'schemaFile': schemaContents,
39
- 'schemaType': 'json-schema',
40
- 'name': name,
41
- }
42
-
43
- if (description) {
44
- body.description = description
45
- }
46
-
47
- var base = this.draftMode ? ["draft", "schemas"] : ["schemas"]
48
- return this.callFormDataApi('POST', 201, true, base, body, null);
49
- }
50
-
51
- getSchema(schemaId) {
52
- var base = this.draftMode ? ["draft", "schemas", schemaId] : ["schemas", schemaId]
53
- return this.callApi('GET', 200, true, base);
54
- }
55
-
56
- getActiveSchema(schemaId) {
57
- return this.callApi('GET', 200, true, ["schemas", schemaId]);
58
- }
59
-
60
- getSchemas() {
61
- var base = this.draftMode ? ["draft", "schemas"] : ["schemas"]
62
- return this.callApi('GET', 200, true, base);
63
- }
64
-
65
- getActiveSchemas() {
66
- return this.callApi('GET', 200, true, ["schemas"]);
67
- }
68
-
69
- updateSchema(schemaId, name, description) {
70
- var body = {
71
- "id": schemaId,
72
- "name": name,
73
- "description": description
74
- }
75
-
76
- var base = this.draftMode ? ["draft", "schemas", schemaId] : ["schemas", schemaId]
77
- return this.callApi('PUT', 200, true, base, body);
78
- }
79
-
80
- updateSchemaContent(schemaId, schemaContents, filename) {
81
- var body = {
82
- 'schemaFile': schemaContents,
83
- 'name': filename
84
- }
85
-
86
- var base = this.draftMode ? ["draft", "schemas", schemaId, "content"] : ["schemas", schemaId, "content"]
87
- return this.callFormDataApi('PUT', 204, false, base, body, null);
88
- }
89
-
90
- getSchemaContent(schemaId) {
91
- var base = this.draftMode ? ["draft", "schemas", schemaId, "content"] : ["schemas", schemaId, "content"]
92
- return this.callApi('GET', 200, true, base);
93
- }
94
-
95
- getActiveSchemaContent(schemaId) {
96
- return this.callApi('GET', 200, true, ["schemas", schemaId, "content"]);
97
- }
98
-
99
- deleteSchema(schemaId) {
100
- var base = this.draftMode ? ["draft", "schemas", schemaId] : ["schemas", schemaId]
101
- return this.callApi('DELETE', 204, false, base, null);
102
- }
103
-
104
-
105
- createEventType(name, description, schemaId) {
106
- var body = {
107
- 'name': name,
108
- 'description': description,
109
- 'schemaId': schemaId,
110
- }
111
- var base = this.draftMode ? ["draft", "event", "types"] : ["event", "types"]
112
- return this.callApi('POST', 201, true, base, JSON.stringify(body));
113
- }
114
-
115
- getEventType(eventTypeId) {
116
- var base = this.draftMode ? ["draft", "event", "types", eventTypeId] : ["event", "types", eventTypeId]
117
- return this.callApi('GET', 200, true, base);
118
- }
119
-
120
- getActiveEventType(eventTypeId) {
121
- return this.callApi('GET', 200, true, ["event", "types", eventTypeId]);
122
- }
123
-
124
- deleteEventType(eventTypeId) {
125
- var base = this.draftMode ? ["draft", "event", "types", eventTypeId] : ["event", "types", eventTypeId]
126
- return this.callApi('DELETE', 204, false, base);
127
- }
128
-
129
- updateEventType(eventTypeId, name, description, schemaId) {
130
- var body = {
131
- "id": eventTypeId,
132
- "name": name,
133
- "description": description,
134
- "schemaId": schemaId
135
- }
136
-
137
- var base = this.draftMode ? ["draft", "event", "types", eventTypeId] : ["event", "types", eventTypeId]
138
- return this.callApi('PUT', 200, true, base, body);
139
- }
140
-
141
- getEventTypes() {
142
- var base = this.draftMode ? ["draft", "event", "types"] : ["event", "types"]
143
- return this.callApi('GET', 200, true, base);
144
- }
145
-
146
- getActiveEventTypes() {
147
- return this.callApi('GET', 200, true, ["event", "types"]);
148
- }
149
-
150
- createPhysicalInterface(name, description) {
151
- var body = {
152
- 'name': name,
153
- 'description': description
154
- }
155
-
156
- var base = this.draftMode ? ["draft", "physicalinterfaces"] : ["physicalinterfaces"]
157
- return this.callApi('POST', 201, true, base, body);
158
- }
159
-
160
- getPhysicalInterface(physicalInterfaceId) {
161
- var base = this.draftMode ? ["draft", "physicalinterfaces", physicalInterfaceId] : ["physicalinterfaces", physicalInterfaceId]
162
- return this.callApi('GET', 200, true, base);
163
- }
164
-
165
- getActivePhysicalInterface(physicalInterfaceId) {
166
- return this.callApi('GET', 200, true, ["physicalinterfaces", physicalInterfaceId]);
167
- }
168
-
169
- deletePhysicalInterface(physicalInterfaceId) {
170
- var base = this.draftMode ? ["draft", "physicalinterfaces", physicalInterfaceId] : ["physicalinterfaces", physicalInterfaceId]
171
- return this.callApi('DELETE', 204, false, base);
172
- }
173
-
174
- updatePhysicalInterface(physicalInterfaceId, name, description) {
175
- var body = {
176
- 'id': physicalInterfaceId,
177
- 'name': name,
178
- 'description': description
179
- }
180
-
181
- var base = this.draftMode ? ["draft", "physicalinterfaces", physicalInterfaceId] : ["physicalinterfaces", physicalInterfaceId]
182
- return this.callApi('PUT', 200, true, base, body);
183
- }
184
-
185
- getPhysicalInterfaces() {
186
- var base = this.draftMode ? ["draft", "physicalinterfaces"] : ["physicalinterfaces"]
187
- return this.callApi('GET', 200, true, base);
188
- }
189
-
190
- getActivePhysicalInterfaces() {
191
- return this.callApi('GET', 200, true, ["physicalinterfaces"]);
192
- }
193
-
194
- createPhysicalInterfaceEventMapping(physicalInterfaceId, eventId, eventTypeId) {
195
- var body = {
196
- "eventId": eventId,
197
- "eventTypeId": eventTypeId
198
- }
199
-
200
- var base = this.draftMode ? ["draft", "physicalinterfaces", physicalInterfaceId, "events"] : ["physicalinterfaces", physicalInterfaceId, "events"]
201
- return this.callApi('POST', 201, true, base, body);
202
- }
203
-
204
- getPhysicalInterfaceEventMappings(physicalInterfaceId) {
205
- var base = this.draftMode ? ["draft", "physicalinterfaces", physicalInterfaceId, "events"] : ["physicalinterfaces", physicalInterfaceId, "events"]
206
- return this.callApi('GET', 200, true, base);
207
- }
208
-
209
- getActivePhysicalInterfaceEventMappings(physicalInterfaceId) {
210
- return this.callApi('GET', 200, true, ["physicalinterfaces", physicalInterfaceId, "events"]);
211
- }
212
-
213
- deletePhysicalInterfaceEventMapping(physicalInterfaceId, eventId) {
214
- var base = this.draftMode ? ["draft", "physicalinterfaces", physicalInterfaceId, "events", eventId] : ["physicalinterfaces", physicalInterfaceId, "events", eventId]
215
- return this.callApi('DELETE', 204, false, base);
216
- }
217
-
218
- createLogicalInterface(name, description, schemaId, alias) {
219
- var body = {
220
- 'name': name,
221
- 'description': description,
222
- 'schemaId': schemaId,
223
- }
224
- if (alias !== undefined) {
225
- body.alias = alias;
226
- }
227
-
228
- var base = this.draftMode ? ["draft", "logicalinterfaces"] : ["applicationinterfaces"]
229
- return this.callApi('POST', 201, true, base, body);
230
- }
231
-
232
- getLogicalInterface(logicalInterfaceId) {
233
- var base = this.draftMode ? ["draft", "logicalinterfaces", logicalInterfaceId] : ["applicationinterfaces", logicalInterfaceId]
234
- return this.callApi('GET', 200, true, base);
235
- }
236
-
237
- getActiveLogicalInterface(logicalInterfaceId) {
238
- return this.callApi('GET', 200, true, ["logicalinterfaces", logicalInterfaceId]);
239
- }
240
-
241
- deleteLogicalInterface(logicalInterfaceId) {
242
- var base = this.draftMode ? ["draft", "logicalinterfaces", logicalInterfaceId] : ["applicationinterfaces", logicalInterfaceId]
243
- return this.callApi('DELETE', 204, false, base);
244
- }
245
-
246
- updateLogicalInterface(logicalInterfaceId, name, description, schemaId, alias) {
247
- var body = {
248
- "id": logicalInterfaceId,
249
- "name": name,
250
- "description": description,
251
- "schemaId": schemaId
252
- }
253
- if (alias !== undefined) {
254
- body.alias = alias;
255
- }
256
-
257
- var base = this.draftMode ? ["draft", "logicalinterfaces", logicalInterfaceId] : ["applicationinterfaces", logicalInterfaceId]
258
- return this.callApi('PUT', 200, true, base, body);
259
- }
260
-
261
-
262
- getLogicalInterfaces(bookmark, limit, sort, name) {
263
- var base = this.draftMode ? ["draft", "logicalinterfaces"] : ["logicalinterfaces"]
264
-
265
- const _sort = this.parseSortSpec(sort);
266
-
267
- return this.callApi('GET', 200, true, base, undefined, {_bookmark: bookmark,_limit: limit, _sort, name : name ? name : undefined});
268
- }
269
-
270
- getActiveLogicalInterfaces() {
271
- return this.callApi('GET', 200, true, ["logicalinterfaces"]);
272
- }
273
-
274
- // Application interface patch operation on draft version
275
- // Acceptable operation id - validate-configuration, activate-configuration, list-differences
276
- patchOperationLogicalInterface(logicalInterfaceId, operationId) {
277
- var body = {
278
- "operation": operationId
279
- }
280
-
281
- if(this.draftMode) {
282
- switch(operationId) {
283
- case 'validate-configuration':
284
- return this.callApi('PATCH', 200, true, ["draft", "logicalinterfaces", logicalInterfaceId], body);
285
- break
286
- case 'activate-configuration':
287
- return this.callApi('PATCH', 202, true, ["draft", "logicalinterfaces", logicalInterfaceId], body);
288
- case 'deactivate-configuration':
289
- return this.callApi('PATCH', 202, true, ["draft", "logicalinterfaces", logicalInterfaceId], body);
290
- case 'list-differences':
291
- return this.callApi('PATCH', 200, true, ["draft", "logicalinterfaces", logicalInterfaceId], body);
292
- default:
293
- return this.callApi('PATCH', 200, true, ["draft", "logicalinterfaces", logicalInterfaceId], body);
294
- }
295
- } else {
296
- return this.invalidOperation("PATCH operation not allowed on logical interface");
297
- }
298
- }
299
-
300
- // Application interface patch operation on active version
301
- // Acceptable operation id - deactivate-configuration
302
- patchOperationActiveLogicalInterface(logicalInterfaceId, operationId) {
303
- var body = {
304
- "operation": operationId
305
- }
306
-
307
- if(this.draftMode) {
308
- return this.callApi('PATCH', 202, true, ["logicalinterfaces", logicalInterfaceId], body)
309
- }
310
- else {
311
- return this.invalidOperation("PATCH operation 'deactivate-configuration' not allowed on logical interface");
312
- }
313
- }
314
-
315
- // Create device type with physical Interface Id
316
- createDeviceType(typeId, description, deviceInfo, metadata, classId, physicalInterfaceId) {
317
- this.log.debug("[ApiClient] registerDeviceType(" + typeId + ", " + description + ", " + deviceInfo + ", " + metadata + ", " + classId + ", " + physicalInterfaceId + ")");
318
- classId = classId || "Device";
319
- let body = {
320
- id: typeId,
321
- classId: classId,
322
- deviceInfo: deviceInfo,
323
- description: description,
324
- metadata: metadata,
325
- physicalInterfaceId: physicalInterfaceId
326
- };
327
-
328
- return this.callApi('POST', 201, true, ['device', 'types'], JSON.stringify(body));
329
- }
330
-
331
- createDeviceTypePhysicalInterfaceAssociation(typeId, physicalInterfaceId) {
332
- let body = {
333
- id: physicalInterfaceId
334
- };
335
-
336
- if(this.draftMode) {
337
- return this.callApi('POST', 201, true, ['draft', 'device', 'types', typeId, 'physicalinterface'], JSON.stringify(body));
338
- } else {
339
- return this.callApi('PUT', 200, true, ['device', 'types', typeId], JSON.stringify({physicalInterfaceId : physicalInterfaceId}));
340
- }
341
-
342
- }
343
-
344
- getDeviceTypePhysicalInterfaces(typeId) {
345
- if(this.draftMode) {
346
- return this.callApi('GET', 200, true, ['draft', 'device', 'types', typeId, 'physicalinterface']);
347
- } else {
348
- return this.invalidOperation("GET Device type's physical interface is not allowed");
349
- }
350
- }
351
-
352
- getActiveDeviceTypePhysicalInterfaces(typeId) {
353
- return this.callApi('GET', 200, true, ['device', 'types', typeId, 'physicalinterface']);
354
- }
355
-
356
-
357
- deleteDeviceTypePhysicalInterfaceAssociation(typeId) {
358
- if(this.draftMode) {
359
- return this.callApi('DELETE', 204, false, ['draft', 'device', 'types', typeId, 'physicalinterface']);
360
- } else {
361
- return this.invalidOperation("DELETE Device type's physical interface is not allowed");
362
- }
363
- }
364
-
365
- createDeviceTypeLogicalInterfaceAssociation(typeId, logicalInterfaceId) {
366
- var body = {
367
- 'id': logicalInterfaceId
368
- }
369
-
370
- var base = this.draftMode ? ['draft', 'device', 'types', typeId, 'logicalinterfaces'] : ['device', 'types', typeId, 'applicationinterfaces']
371
- return this.callApi('POST', 201, true, base, body);
372
- }
373
-
374
- getDeviceTypeLogicalInterfaces(typeId) {
375
- var base = this.draftMode ? ['draft', 'device', 'types', typeId, 'logicalinterfaces'] : ['device', 'types', typeId, 'applicationinterfaces']
376
- return this.callApi('GET', 200, true, base);
377
- }
378
-
379
- getActiveDeviceTypeLogicalInterfaces(typeId) {
380
- return this.callApi('GET', 200, true, ['device', 'types', typeId, 'logicalinterfaces']);
381
- }
382
-
383
- createDeviceTypeLogicalInterfacePropertyMappings(typeId, logicalInterfaceId, mappings, notificationStrategy) {
384
- var body = null, base = null
385
- if(this.draftMode) {
386
- body = {
387
- "logicalInterfaceId": logicalInterfaceId,
388
- "propertyMappings": mappings,
389
- "notificationStrategy": "never"
390
- }
391
-
392
- if(notificationStrategy) {
393
- body.notificationStrategy = notificationStrategy
394
- }
395
-
396
- base = ['draft', 'device', 'types', typeId, 'mappings']
397
- } else {
398
- body = {
399
- "applicationInterfaceId": logicalInterfaceId,
400
- "propertyMappings": mappings
401
- }
402
- base = ['device', 'types', typeId, 'mappings']
403
- }
404
-
405
- return this.callApi('POST', 201, true, base, body);
406
- }
407
-
408
- getDeviceTypePropertyMappings(typeId) {
409
- var base = this.draftMode ? ['draft', 'device', 'types', typeId, 'mappings'] : ['device', 'types', typeId, 'mappings']
410
- return this.callApi('GET', 200, true, base);
411
- }
412
-
413
- getActiveDeviceTypePropertyMappings(typeId) {
414
- return this.callApi('GET', 200, true, ['device', 'types', typeId, 'mappings']);
415
- }
416
-
417
- getDeviceTypeLogicalInterfacePropertyMappings(typeId, logicalInterfaceId) {
418
- var base = this.draftMode ? ['draft', 'device', 'types', typeId, 'mappings', logicalInterfaceId] : ['device', 'types', typeId, 'mappings', logicalInterfaceId]
419
- return this.callApi('GET', 200, true, base);
420
- }
421
-
422
- getActiveDeviceTypeLogicalInterfacePropertyMappings(typeId, logicalInterfaceId) {
423
- return this.callApi('GET', 200, true, ['device', 'types', typeId, 'mappings', logicalInterfaceId]);
424
- }
425
-
426
- updateDeviceTypeLogicalInterfacePropertyMappings(typeId, logicalInterfaceId, mappings, notificationStrategy) {
427
- var body = null, base = null
428
- if(this.draftMode) {
429
- body = {
430
- "logicalInterfaceId": logicalInterfaceId,
431
- "propertyMappings": mappings,
432
- "notificationStrategy": "never"
433
- }
434
-
435
- if(notificationStrategy) {
436
- body.notificationStrategy = notificationStrategy
437
- }
438
-
439
- base = ['draft', 'device', 'types', typeId, 'mappings', logicalInterfaceId]
440
- } else {
441
- body = {
442
- "applicationInterfaceId": logicalInterfaceId,
443
- "propertyMappings": mappings
444
- }
445
- base = ['device', 'types', typeId, 'mappings', logicalInterfaceId]
446
- }
447
- return this.callApi('PUT', 200, false, base, body);
448
- }
449
-
450
- deleteDeviceTypeLogicalInterfacePropertyMappings(typeId, logicalInterfaceId) {
451
- var base = this.draftMode ? ['draft', 'device', 'types', typeId, 'mappings', logicalInterfaceId] : ['device', 'types', typeId, 'mappings', logicalInterfaceId]
452
- return this.callApi('DELETE', 204, false, base);
453
- }
454
-
455
- deleteDeviceTypeLogicalInterfaceAssociation(typeId, logicalInterfaceId) {
456
- var base = this.draftMode ? ['draft', 'device', 'types', typeId, 'logicalinterfaces', logicalInterfaceId] : ['device', 'types', typeId, 'applicationinterfaces', logicalInterfaceId]
457
- return this.callApi('DELETE', 204, false, base);
458
- }
459
-
460
- // Device Type patch operation on draft version
461
- // Acceptable operation id - validate-configuration, activate-configuration, list-differences
462
- patchOperationDeviceType(typeId, operationId) {
463
- if(!operationId) {
464
- return invalidOperation("PATCH operation is not allowed. Operation id is expected")
465
- }
466
-
467
- var body = {
468
- "operation": operationId
469
- }
470
-
471
- var base = this.draftMode ? ['draft', 'device', 'types', typeId]: ['device', 'types', typeId]
472
-
473
- if(this.draftMode) {
474
- switch(operationId) {
475
- case 'validate-configuration':
476
- return this.callApi('PATCH', 200, true, base, body);
477
- break
478
- case 'activate-configuration':
479
- return this.callApi('PATCH', 202, true, base, body);
480
- break
481
- case 'deactivate-configuration':
482
- return this.callApi('PATCH', 202, true, base, body);
483
- break
484
- case 'list-differences':
485
- return this.callApi('PATCH', 200, true, base, body);
486
- break
487
- default:
488
- return this.invalidOperation("PATCH operation is not allowed. Invalid operation id")
489
- }
490
- } else {
491
- switch(operationId) {
492
- case 'validate-configuration':
493
- return this.callApi('PATCH', 200, true, base, body);
494
- break
495
- case 'deploy-configuration':
496
- return this.callApi('PATCH', 202, true, base, body);
497
- break
498
- case 'remove-deployed-configuration':
499
- return this.callApi('PATCH', 202, true, base, body);
500
- break
501
- case 'list-differences':
502
- return this.invalidOperation("PATCH operation 'list-differences' is not allowed")
503
- break
504
- default:
505
- return this.invalidOperation("PATCH operation is not allowed. Invalid operation id")
506
- }
507
- }
508
- }
509
-
510
-
511
- // Device Type patch operation on active version
512
- // Acceptable operation id - deactivate-configuration
513
- patchOperationActiveDeviceType(typeId, operationId) {
514
- var body = {
515
- "operation": operationId
516
- }
517
-
518
- if(this.draftMode) {
519
- return this.callApi('PATCH', 202, true, ['device', 'types', typeId], body);
520
- }
521
- else {
522
- return this.invalidOperation("PATCH operation 'deactivate-configuration' is not allowed");
523
- }
524
- }
525
-
526
- getDeviceTypeDeployedConfiguration(typeId) {
527
- if(this.draftMode) {
528
- return this.invalidOperation("GET deployed configuration is not allowed");
529
- } else {
530
- return this.callApi('GET', 200, true, ['device', 'types', typeId, 'deployedconfiguration']);
531
- }
532
- }
533
-
534
- getDeviceState(typeId, deviceId, logicalInterfaceId) {
535
- return this.callApi('GET', 200, true, ['device', 'types', typeId, 'devices', deviceId, 'state', logicalInterfaceId]);
536
- }
537
-
538
- createSchemaAndEventType(schemaContents, schemaFileName, eventTypeName, eventDescription) {
539
- var body = {
540
- 'schemaFile': schemaContents,
541
- 'schemaType': 'json-schema',
542
- 'name': schemaFileName
543
- }
544
-
545
- var createSchema = new Promise((resolve, reject) => {
546
- var base = this.draftMode ? ["draft", "schemas"] : ["schemas"]
547
- this.callFormDataApi('POST', 201, true, base, body, null).then(result => {
548
- resolve(result)
549
- }, error => {
550
- reject(error)
551
- })
552
- })
553
-
554
- return createSchema.then(value => {
555
- var schemaId = value["id"]
556
- return this.createEventType(eventTypeName, eventDescription, schemaId)
557
- })
558
- }
559
-
560
- createSchemaAndLogicalInterface(schemaContents, schemaFileName, appInterfaceName, appInterfaceDescription, appInterfaceAlias) {
561
- var body = {
562
- 'schemaFile': schemaContents,
563
- 'schemaType': 'json-schema',
564
- 'name': schemaFileName
565
- }
566
-
567
- var createSchema = new Promise((resolve, reject) => {
568
- var base = this.draftMode ? ["draft", "schemas"] : ["schemas"]
569
- this.callFormDataApi('POST', 201, true, base, body, null).then(result => {
570
- resolve(result)
571
- }, error => {
572
- reject(error)
573
- })
574
- })
575
-
576
- return createSchema.then(value => {
577
- var schemaId = value.id
578
- return this.createLogicalInterface(appInterfaceName, appInterfaceDescription, schemaId, appInterfaceAlias)
579
- })
580
- }
581
-
582
- createPhysicalInterfaceWithEventMapping(physicalInterfaceName, description, eventId, eventTypeId) {
583
- var createPhysicalInterface = new Promise((resolve, reject) => {
584
- this.createPhysicalInterface(physicalInterfaceName, description).then(result => {
585
- resolve(result)
586
- }, error => {
587
- reject(error)
588
- })
589
- })
590
-
591
- return createPhysicalInterface.then(value => {
592
- var physicalInterface = value
593
-
594
- var PhysicalInterfaceEventMapping = new Promise((resolve, reject) => {
595
- this.createPhysicalInterfaceEventMapping(physicalInterface.id, eventId, eventTypeId).then(result => {
596
- resolve([physicalInterface, result])
597
- }, error => {
598
- reject(error)
599
- })
600
- })
601
-
602
- return PhysicalInterfaceEventMapping.then(result => {
603
- return result
604
- })
605
- })
606
- }
607
-
608
- createDeviceTypeLogicalInterfaceEventMapping(deviceTypeName, description, logicalInterfaceId, eventMapping, notificationStrategy) {
609
- var createDeviceType = new Promise((resolve, reject) => {
610
- this.createDeviceType(deviceTypeName, description).then(result => {
611
- resolve(result)
612
- }, error => {
613
- reject(error)
614
- })
615
- })
616
-
617
- return createDeviceType.then(result => {
618
- var deviceObject = result
619
- var deviceTypeLogicalInterface = null
620
- var deviceTypeLogicalInterface = new Promise((resolve, reject) => {
621
- this.createDeviceTypeLogicalInterfaceAssociation(deviceObject.id, logicalInterfaceId).then(result => {
622
- resolve(result)
623
- }, error => {
624
- reject(error)
625
- })
626
- })
627
-
628
- return deviceTypeLogicalInterface.then(result => {
629
- deviceTypeLogicalInterface = result
630
- var deviceTypeLogicalInterfacePropertyMappings = new Promise((resolve, reject) => {
631
- var notificationstrategy = "never"
632
- if(notificationStrategy) {
633
- notificationstrategy = notificationStrategy
634
- }
635
-
636
- this.createDeviceTypeLogicalInterfacePropertyMappings(deviceObject.id, logicalInterfaceId, eventMapping, notificationstrategy).then(result => {
637
- var arr = [deviceObject, deviceTypeLogicalInterface, result]
638
- resolve(arr)
639
- }, error => {
640
- reject(error)
641
- })
642
- })
643
-
644
- return deviceTypeLogicalInterfacePropertyMappings.then(result => {
645
- return result
646
- })
647
- })
648
- })
649
- }
650
-
1
+ /**
2
+ *****************************************************************************
3
+ Copyright (c) 2014, 2019 IBM Corporation and other Contributors.
4
+ All rights reserved. This program and the accompanying materials
5
+ are made available under the terms of the Eclipse Public License v1.0
6
+ which accompanies this distribution, and is available at
7
+ http://www.eclipse.org/legal/epl-v10.html
8
+ *****************************************************************************
9
+ *
10
+ */
11
+ import log from 'loglevel';
12
+
13
+ export default class StateClient {
14
+ constructor(apiClient) {
15
+ this.log = log;
16
+
17
+ this.apiClient = apiClient;
18
+ this.draftMode = true;
19
+
20
+ // Create an alias to the apiClient's methods that we use
21
+ this.callApi = this.apiClient.callApi.bind(this.apiClient);
22
+ this.parseSortSpec = this.apiClient.parseSortSpec.bind(this.apiClient);
23
+ this.callFormDataApi = this.apiClient.callFormDataApi.bind(this.apiClient);
24
+ this.invalidOperation = this.apiClient.invalidOperation.bind(this.apiClient);
25
+ }
26
+
27
+ workWithActive() {
28
+ this.draftMode = false;
29
+ }
30
+
31
+ workWithDraft() {
32
+ this.draftMode = true;
33
+ }
34
+
35
+ // IM Device state API
36
+ createSchema(schemaContents, name, description) {
37
+ var body = {
38
+ 'schemaFile': schemaContents,
39
+ 'schemaType': 'json-schema',
40
+ 'name': name,
41
+ }
42
+
43
+ if (description) {
44
+ body.description = description
45
+ }
46
+
47
+ var base = this.draftMode ? ["draft", "schemas"] : ["schemas"]
48
+ return this.callFormDataApi('POST', 201, true, base, body, null);
49
+ }
50
+
51
+ getSchema(schemaId) {
52
+ var base = this.draftMode ? ["draft", "schemas", schemaId] : ["schemas", schemaId]
53
+ return this.callApi('GET', 200, true, base);
54
+ }
55
+
56
+ getActiveSchema(schemaId) {
57
+ return this.callApi('GET', 200, true, ["schemas", schemaId]);
58
+ }
59
+
60
+ getSchemas() {
61
+ var base = this.draftMode ? ["draft", "schemas"] : ["schemas"]
62
+ return this.callApi('GET', 200, true, base);
63
+ }
64
+
65
+ getActiveSchemas() {
66
+ return this.callApi('GET', 200, true, ["schemas"]);
67
+ }
68
+
69
+ updateSchema(schemaId, name, description) {
70
+ var body = {
71
+ "id": schemaId,
72
+ "name": name,
73
+ "description": description
74
+ }
75
+
76
+ var base = this.draftMode ? ["draft", "schemas", schemaId] : ["schemas", schemaId]
77
+ return this.callApi('PUT', 200, true, base, body);
78
+ }
79
+
80
+ updateSchemaContent(schemaId, schemaContents, filename) {
81
+ var body = {
82
+ 'schemaFile': schemaContents,
83
+ 'name': filename
84
+ }
85
+
86
+ var base = this.draftMode ? ["draft", "schemas", schemaId, "content"] : ["schemas", schemaId, "content"]
87
+ return this.callFormDataApi('PUT', 204, false, base, body, null);
88
+ }
89
+
90
+ getSchemaContent(schemaId) {
91
+ var base = this.draftMode ? ["draft", "schemas", schemaId, "content"] : ["schemas", schemaId, "content"]
92
+ return this.callApi('GET', 200, true, base);
93
+ }
94
+
95
+ getActiveSchemaContent(schemaId) {
96
+ return this.callApi('GET', 200, true, ["schemas", schemaId, "content"]);
97
+ }
98
+
99
+ deleteSchema(schemaId) {
100
+ var base = this.draftMode ? ["draft", "schemas", schemaId] : ["schemas", schemaId]
101
+ return this.callApi('DELETE', 204, false, base, null);
102
+ }
103
+
104
+
105
+ createEventType(name, description, schemaId) {
106
+ var body = {
107
+ 'name': name,
108
+ 'description': description,
109
+ 'schemaId': schemaId,
110
+ }
111
+ var base = this.draftMode ? ["draft", "event", "types"] : ["event", "types"]
112
+ return this.callApi('POST', 201, true, base, JSON.stringify(body));
113
+ }
114
+
115
+ getEventType(eventTypeId) {
116
+ var base = this.draftMode ? ["draft", "event", "types", eventTypeId] : ["event", "types", eventTypeId]
117
+ return this.callApi('GET', 200, true, base);
118
+ }
119
+
120
+ getActiveEventType(eventTypeId) {
121
+ return this.callApi('GET', 200, true, ["event", "types", eventTypeId]);
122
+ }
123
+
124
+ deleteEventType(eventTypeId) {
125
+ var base = this.draftMode ? ["draft", "event", "types", eventTypeId] : ["event", "types", eventTypeId]
126
+ return this.callApi('DELETE', 204, false, base);
127
+ }
128
+
129
+ updateEventType(eventTypeId, name, description, schemaId) {
130
+ var body = {
131
+ "id": eventTypeId,
132
+ "name": name,
133
+ "description": description,
134
+ "schemaId": schemaId
135
+ }
136
+
137
+ var base = this.draftMode ? ["draft", "event", "types", eventTypeId] : ["event", "types", eventTypeId]
138
+ return this.callApi('PUT', 200, true, base, body);
139
+ }
140
+
141
+ getEventTypes() {
142
+ var base = this.draftMode ? ["draft", "event", "types"] : ["event", "types"]
143
+ return this.callApi('GET', 200, true, base);
144
+ }
145
+
146
+ getActiveEventTypes() {
147
+ return this.callApi('GET', 200, true, ["event", "types"]);
148
+ }
149
+
150
+ createPhysicalInterface(name, description) {
151
+ var body = {
152
+ 'name': name,
153
+ 'description': description
154
+ }
155
+
156
+ var base = this.draftMode ? ["draft", "physicalinterfaces"] : ["physicalinterfaces"]
157
+ return this.callApi('POST', 201, true, base, body);
158
+ }
159
+
160
+ getPhysicalInterface(physicalInterfaceId) {
161
+ var base = this.draftMode ? ["draft", "physicalinterfaces", physicalInterfaceId] : ["physicalinterfaces", physicalInterfaceId]
162
+ return this.callApi('GET', 200, true, base);
163
+ }
164
+
165
+ getActivePhysicalInterface(physicalInterfaceId) {
166
+ return this.callApi('GET', 200, true, ["physicalinterfaces", physicalInterfaceId]);
167
+ }
168
+
169
+ deletePhysicalInterface(physicalInterfaceId) {
170
+ var base = this.draftMode ? ["draft", "physicalinterfaces", physicalInterfaceId] : ["physicalinterfaces", physicalInterfaceId]
171
+ return this.callApi('DELETE', 204, false, base);
172
+ }
173
+
174
+ updatePhysicalInterface(physicalInterfaceId, name, description) {
175
+ var body = {
176
+ 'id': physicalInterfaceId,
177
+ 'name': name,
178
+ 'description': description
179
+ }
180
+
181
+ var base = this.draftMode ? ["draft", "physicalinterfaces", physicalInterfaceId] : ["physicalinterfaces", physicalInterfaceId]
182
+ return this.callApi('PUT', 200, true, base, body);
183
+ }
184
+
185
+ getPhysicalInterfaces() {
186
+ var base = this.draftMode ? ["draft", "physicalinterfaces"] : ["physicalinterfaces"]
187
+ return this.callApi('GET', 200, true, base);
188
+ }
189
+
190
+ getActivePhysicalInterfaces() {
191
+ return this.callApi('GET', 200, true, ["physicalinterfaces"]);
192
+ }
193
+
194
+ createPhysicalInterfaceEventMapping(physicalInterfaceId, eventId, eventTypeId) {
195
+ var body = {
196
+ "eventId": eventId,
197
+ "eventTypeId": eventTypeId
198
+ }
199
+
200
+ var base = this.draftMode ? ["draft", "physicalinterfaces", physicalInterfaceId, "events"] : ["physicalinterfaces", physicalInterfaceId, "events"]
201
+ return this.callApi('POST', 201, true, base, body);
202
+ }
203
+
204
+ getPhysicalInterfaceEventMappings(physicalInterfaceId) {
205
+ var base = this.draftMode ? ["draft", "physicalinterfaces", physicalInterfaceId, "events"] : ["physicalinterfaces", physicalInterfaceId, "events"]
206
+ return this.callApi('GET', 200, true, base);
207
+ }
208
+
209
+ getActivePhysicalInterfaceEventMappings(physicalInterfaceId) {
210
+ return this.callApi('GET', 200, true, ["physicalinterfaces", physicalInterfaceId, "events"]);
211
+ }
212
+
213
+ deletePhysicalInterfaceEventMapping(physicalInterfaceId, eventId) {
214
+ var base = this.draftMode ? ["draft", "physicalinterfaces", physicalInterfaceId, "events", eventId] : ["physicalinterfaces", physicalInterfaceId, "events", eventId]
215
+ return this.callApi('DELETE', 204, false, base);
216
+ }
217
+
218
+ createLogicalInterface(name, description, schemaId, alias) {
219
+ var body = {
220
+ 'name': name,
221
+ 'description': description,
222
+ 'schemaId': schemaId,
223
+ }
224
+ if (alias !== undefined) {
225
+ body.alias = alias;
226
+ }
227
+
228
+ var base = this.draftMode ? ["draft", "logicalinterfaces"] : ["applicationinterfaces"]
229
+ return this.callApi('POST', 201, true, base, body);
230
+ }
231
+
232
+ getLogicalInterface(logicalInterfaceId) {
233
+ var base = this.draftMode ? ["draft", "logicalinterfaces", logicalInterfaceId] : ["applicationinterfaces", logicalInterfaceId]
234
+ return this.callApi('GET', 200, true, base);
235
+ }
236
+
237
+ getActiveLogicalInterface(logicalInterfaceId) {
238
+ return this.callApi('GET', 200, true, ["logicalinterfaces", logicalInterfaceId]);
239
+ }
240
+
241
+ deleteLogicalInterface(logicalInterfaceId) {
242
+ var base = this.draftMode ? ["draft", "logicalinterfaces", logicalInterfaceId] : ["applicationinterfaces", logicalInterfaceId]
243
+ return this.callApi('DELETE', 204, false, base);
244
+ }
245
+
246
+ updateLogicalInterface(logicalInterfaceId, name, description, schemaId, alias) {
247
+ var body = {
248
+ "id": logicalInterfaceId,
249
+ "name": name,
250
+ "description": description,
251
+ "schemaId": schemaId
252
+ }
253
+ if (alias !== undefined) {
254
+ body.alias = alias;
255
+ }
256
+
257
+ var base = this.draftMode ? ["draft", "logicalinterfaces", logicalInterfaceId] : ["applicationinterfaces", logicalInterfaceId]
258
+ return this.callApi('PUT', 200, true, base, body);
259
+ }
260
+
261
+
262
+ getLogicalInterfaces(bookmark, limit, sort, name) {
263
+ var base = this.draftMode ? ["draft", "logicalinterfaces"] : ["logicalinterfaces"]
264
+
265
+ const _sort = this.parseSortSpec(sort);
266
+
267
+ return this.callApi('GET', 200, true, base, undefined, {_bookmark: bookmark,_limit: limit, _sort, name : name ? name : undefined});
268
+ }
269
+
270
+ getActiveLogicalInterfaces() {
271
+ return this.callApi('GET', 200, true, ["logicalinterfaces"]);
272
+ }
273
+
274
+ // Application interface patch operation on draft version
275
+ // Acceptable operation id - validate-configuration, activate-configuration, list-differences
276
+ patchOperationLogicalInterface(logicalInterfaceId, operationId) {
277
+ var body = {
278
+ "operation": operationId
279
+ }
280
+
281
+ if(this.draftMode) {
282
+ switch(operationId) {
283
+ case 'validate-configuration':
284
+ return this.callApi('PATCH', 200, true, ["draft", "logicalinterfaces", logicalInterfaceId], body);
285
+ break
286
+ case 'activate-configuration':
287
+ return this.callApi('PATCH', 202, true, ["draft", "logicalinterfaces", logicalInterfaceId], body);
288
+ case 'deactivate-configuration':
289
+ return this.callApi('PATCH', 202, true, ["draft", "logicalinterfaces", logicalInterfaceId], body);
290
+ case 'list-differences':
291
+ return this.callApi('PATCH', 200, true, ["draft", "logicalinterfaces", logicalInterfaceId], body);
292
+ default:
293
+ return this.callApi('PATCH', 200, true, ["draft", "logicalinterfaces", logicalInterfaceId], body);
294
+ }
295
+ } else {
296
+ return this.invalidOperation("PATCH operation not allowed on logical interface");
297
+ }
298
+ }
299
+
300
+ // Application interface patch operation on active version
301
+ // Acceptable operation id - deactivate-configuration
302
+ patchOperationActiveLogicalInterface(logicalInterfaceId, operationId) {
303
+ var body = {
304
+ "operation": operationId
305
+ }
306
+
307
+ if(this.draftMode) {
308
+ return this.callApi('PATCH', 202, true, ["logicalinterfaces", logicalInterfaceId], body)
309
+ }
310
+ else {
311
+ return this.invalidOperation("PATCH operation 'deactivate-configuration' not allowed on logical interface");
312
+ }
313
+ }
314
+
315
+ // Create device type with physical Interface Id
316
+ createDeviceType(typeId, description, deviceInfo, metadata, classId, physicalInterfaceId) {
317
+ this.log.debug("[ApiClient] registerDeviceType(" + typeId + ", " + description + ", " + deviceInfo + ", " + metadata + ", " + classId + ", " + physicalInterfaceId + ")");
318
+ classId = classId || "Device";
319
+ let body = {
320
+ id: typeId,
321
+ classId: classId,
322
+ deviceInfo: deviceInfo,
323
+ description: description,
324
+ metadata: metadata,
325
+ physicalInterfaceId: physicalInterfaceId
326
+ };
327
+
328
+ return this.callApi('POST', 201, true, ['device', 'types'], JSON.stringify(body));
329
+ }
330
+
331
+ createDeviceTypePhysicalInterfaceAssociation(typeId, physicalInterfaceId) {
332
+ let body = {
333
+ id: physicalInterfaceId
334
+ };
335
+
336
+ if(this.draftMode) {
337
+ return this.callApi('POST', 201, true, ['draft', 'device', 'types', typeId, 'physicalinterface'], JSON.stringify(body));
338
+ } else {
339
+ return this.callApi('PUT', 200, true, ['device', 'types', typeId], JSON.stringify({physicalInterfaceId : physicalInterfaceId}));
340
+ }
341
+
342
+ }
343
+
344
+ getDeviceTypePhysicalInterfaces(typeId) {
345
+ if(this.draftMode) {
346
+ return this.callApi('GET', 200, true, ['draft', 'device', 'types', typeId, 'physicalinterface']);
347
+ } else {
348
+ return this.invalidOperation("GET Device type's physical interface is not allowed");
349
+ }
350
+ }
351
+
352
+ getActiveDeviceTypePhysicalInterfaces(typeId) {
353
+ return this.callApi('GET', 200, true, ['device', 'types', typeId, 'physicalinterface']);
354
+ }
355
+
356
+
357
+ deleteDeviceTypePhysicalInterfaceAssociation(typeId) {
358
+ if(this.draftMode) {
359
+ return this.callApi('DELETE', 204, false, ['draft', 'device', 'types', typeId, 'physicalinterface']);
360
+ } else {
361
+ return this.invalidOperation("DELETE Device type's physical interface is not allowed");
362
+ }
363
+ }
364
+
365
+ createDeviceTypeLogicalInterfaceAssociation(typeId, logicalInterfaceId) {
366
+ var body = {
367
+ 'id': logicalInterfaceId
368
+ }
369
+
370
+ var base = this.draftMode ? ['draft', 'device', 'types', typeId, 'logicalinterfaces'] : ['device', 'types', typeId, 'applicationinterfaces']
371
+ return this.callApi('POST', 201, true, base, body);
372
+ }
373
+
374
+ getDeviceTypeLogicalInterfaces(typeId) {
375
+ var base = this.draftMode ? ['draft', 'device', 'types', typeId, 'logicalinterfaces'] : ['device', 'types', typeId, 'applicationinterfaces']
376
+ return this.callApi('GET', 200, true, base);
377
+ }
378
+
379
+ getActiveDeviceTypeLogicalInterfaces(typeId) {
380
+ return this.callApi('GET', 200, true, ['device', 'types', typeId, 'logicalinterfaces']);
381
+ }
382
+
383
+ createDeviceTypeLogicalInterfacePropertyMappings(typeId, logicalInterfaceId, mappings, notificationStrategy) {
384
+ var body = null, base = null
385
+ if(this.draftMode) {
386
+ body = {
387
+ "logicalInterfaceId": logicalInterfaceId,
388
+ "propertyMappings": mappings,
389
+ "notificationStrategy": "never"
390
+ }
391
+
392
+ if(notificationStrategy) {
393
+ body.notificationStrategy = notificationStrategy
394
+ }
395
+
396
+ base = ['draft', 'device', 'types', typeId, 'mappings']
397
+ } else {
398
+ body = {
399
+ "applicationInterfaceId": logicalInterfaceId,
400
+ "propertyMappings": mappings
401
+ }
402
+ base = ['device', 'types', typeId, 'mappings']
403
+ }
404
+
405
+ return this.callApi('POST', 201, true, base, body);
406
+ }
407
+
408
+ getDeviceTypePropertyMappings(typeId) {
409
+ var base = this.draftMode ? ['draft', 'device', 'types', typeId, 'mappings'] : ['device', 'types', typeId, 'mappings']
410
+ return this.callApi('GET', 200, true, base);
411
+ }
412
+
413
+ getActiveDeviceTypePropertyMappings(typeId) {
414
+ return this.callApi('GET', 200, true, ['device', 'types', typeId, 'mappings']);
415
+ }
416
+
417
+ getDeviceTypeLogicalInterfacePropertyMappings(typeId, logicalInterfaceId) {
418
+ var base = this.draftMode ? ['draft', 'device', 'types', typeId, 'mappings', logicalInterfaceId] : ['device', 'types', typeId, 'mappings', logicalInterfaceId]
419
+ return this.callApi('GET', 200, true, base);
420
+ }
421
+
422
+ getActiveDeviceTypeLogicalInterfacePropertyMappings(typeId, logicalInterfaceId) {
423
+ return this.callApi('GET', 200, true, ['device', 'types', typeId, 'mappings', logicalInterfaceId]);
424
+ }
425
+
426
+ updateDeviceTypeLogicalInterfacePropertyMappings(typeId, logicalInterfaceId, mappings, notificationStrategy) {
427
+ var body = null, base = null
428
+ if(this.draftMode) {
429
+ body = {
430
+ "logicalInterfaceId": logicalInterfaceId,
431
+ "propertyMappings": mappings,
432
+ "notificationStrategy": "never"
433
+ }
434
+
435
+ if(notificationStrategy) {
436
+ body.notificationStrategy = notificationStrategy
437
+ }
438
+
439
+ base = ['draft', 'device', 'types', typeId, 'mappings', logicalInterfaceId]
440
+ } else {
441
+ body = {
442
+ "applicationInterfaceId": logicalInterfaceId,
443
+ "propertyMappings": mappings
444
+ }
445
+ base = ['device', 'types', typeId, 'mappings', logicalInterfaceId]
446
+ }
447
+ return this.callApi('PUT', 200, false, base, body);
448
+ }
449
+
450
+ deleteDeviceTypeLogicalInterfacePropertyMappings(typeId, logicalInterfaceId) {
451
+ var base = this.draftMode ? ['draft', 'device', 'types', typeId, 'mappings', logicalInterfaceId] : ['device', 'types', typeId, 'mappings', logicalInterfaceId]
452
+ return this.callApi('DELETE', 204, false, base);
453
+ }
454
+
455
+ deleteDeviceTypeLogicalInterfaceAssociation(typeId, logicalInterfaceId) {
456
+ var base = this.draftMode ? ['draft', 'device', 'types', typeId, 'logicalinterfaces', logicalInterfaceId] : ['device', 'types', typeId, 'applicationinterfaces', logicalInterfaceId]
457
+ return this.callApi('DELETE', 204, false, base);
458
+ }
459
+
460
+ // Device Type patch operation on draft version
461
+ // Acceptable operation id - validate-configuration, activate-configuration, list-differences
462
+ patchOperationDeviceType(typeId, operationId) {
463
+ if(!operationId) {
464
+ return invalidOperation("PATCH operation is not allowed. Operation id is expected")
465
+ }
466
+
467
+ var body = {
468
+ "operation": operationId
469
+ }
470
+
471
+ var base = this.draftMode ? ['draft', 'device', 'types', typeId]: ['device', 'types', typeId]
472
+
473
+ if(this.draftMode) {
474
+ switch(operationId) {
475
+ case 'validate-configuration':
476
+ return this.callApi('PATCH', 200, true, base, body);
477
+ break
478
+ case 'activate-configuration':
479
+ return this.callApi('PATCH', 202, true, base, body);
480
+ break
481
+ case 'deactivate-configuration':
482
+ return this.callApi('PATCH', 202, true, base, body);
483
+ break
484
+ case 'list-differences':
485
+ return this.callApi('PATCH', 200, true, base, body);
486
+ break
487
+ default:
488
+ return this.invalidOperation("PATCH operation is not allowed. Invalid operation id")
489
+ }
490
+ } else {
491
+ switch(operationId) {
492
+ case 'validate-configuration':
493
+ return this.callApi('PATCH', 200, true, base, body);
494
+ break
495
+ case 'deploy-configuration':
496
+ return this.callApi('PATCH', 202, true, base, body);
497
+ break
498
+ case 'remove-deployed-configuration':
499
+ return this.callApi('PATCH', 202, true, base, body);
500
+ break
501
+ case 'list-differences':
502
+ return this.invalidOperation("PATCH operation 'list-differences' is not allowed")
503
+ break
504
+ default:
505
+ return this.invalidOperation("PATCH operation is not allowed. Invalid operation id")
506
+ }
507
+ }
508
+ }
509
+
510
+
511
+ // Device Type patch operation on active version
512
+ // Acceptable operation id - deactivate-configuration
513
+ patchOperationActiveDeviceType(typeId, operationId) {
514
+ var body = {
515
+ "operation": operationId
516
+ }
517
+
518
+ if(this.draftMode) {
519
+ return this.callApi('PATCH', 202, true, ['device', 'types', typeId], body);
520
+ }
521
+ else {
522
+ return this.invalidOperation("PATCH operation 'deactivate-configuration' is not allowed");
523
+ }
524
+ }
525
+
526
+ getDeviceTypeDeployedConfiguration(typeId) {
527
+ if(this.draftMode) {
528
+ return this.invalidOperation("GET deployed configuration is not allowed");
529
+ } else {
530
+ return this.callApi('GET', 200, true, ['device', 'types', typeId, 'deployedconfiguration']);
531
+ }
532
+ }
533
+
534
+ getDeviceState(typeId, deviceId, logicalInterfaceId) {
535
+ return this.callApi('GET', 200, true, ['device', 'types', typeId, 'devices', deviceId, 'state', logicalInterfaceId]);
536
+ }
537
+
538
+ createSchemaAndEventType(schemaContents, schemaFileName, eventTypeName, eventDescription) {
539
+ var body = {
540
+ 'schemaFile': schemaContents,
541
+ 'schemaType': 'json-schema',
542
+ 'name': schemaFileName
543
+ }
544
+
545
+ var createSchema = new Promise((resolve, reject) => {
546
+ var base = this.draftMode ? ["draft", "schemas"] : ["schemas"]
547
+ this.callFormDataApi('POST', 201, true, base, body, null).then(result => {
548
+ resolve(result)
549
+ }, error => {
550
+ reject(error)
551
+ })
552
+ })
553
+
554
+ return createSchema.then(value => {
555
+ var schemaId = value["id"]
556
+ return this.createEventType(eventTypeName, eventDescription, schemaId)
557
+ })
558
+ }
559
+
560
+ createSchemaAndLogicalInterface(schemaContents, schemaFileName, appInterfaceName, appInterfaceDescription, appInterfaceAlias) {
561
+ var body = {
562
+ 'schemaFile': schemaContents,
563
+ 'schemaType': 'json-schema',
564
+ 'name': schemaFileName
565
+ }
566
+
567
+ var createSchema = new Promise((resolve, reject) => {
568
+ var base = this.draftMode ? ["draft", "schemas"] : ["schemas"]
569
+ this.callFormDataApi('POST', 201, true, base, body, null).then(result => {
570
+ resolve(result)
571
+ }, error => {
572
+ reject(error)
573
+ })
574
+ })
575
+
576
+ return createSchema.then(value => {
577
+ var schemaId = value.id
578
+ return this.createLogicalInterface(appInterfaceName, appInterfaceDescription, schemaId, appInterfaceAlias)
579
+ })
580
+ }
581
+
582
+ createPhysicalInterfaceWithEventMapping(physicalInterfaceName, description, eventId, eventTypeId) {
583
+ var createPhysicalInterface = new Promise((resolve, reject) => {
584
+ this.createPhysicalInterface(physicalInterfaceName, description).then(result => {
585
+ resolve(result)
586
+ }, error => {
587
+ reject(error)
588
+ })
589
+ })
590
+
591
+ return createPhysicalInterface.then(value => {
592
+ var physicalInterface = value
593
+
594
+ var PhysicalInterfaceEventMapping = new Promise((resolve, reject) => {
595
+ this.createPhysicalInterfaceEventMapping(physicalInterface.id, eventId, eventTypeId).then(result => {
596
+ resolve([physicalInterface, result])
597
+ }, error => {
598
+ reject(error)
599
+ })
600
+ })
601
+
602
+ return PhysicalInterfaceEventMapping.then(result => {
603
+ return result
604
+ })
605
+ })
606
+ }
607
+
608
+ createDeviceTypeLogicalInterfaceEventMapping(deviceTypeName, description, logicalInterfaceId, eventMapping, notificationStrategy) {
609
+ var createDeviceType = new Promise((resolve, reject) => {
610
+ this.createDeviceType(deviceTypeName, description).then(result => {
611
+ resolve(result)
612
+ }, error => {
613
+ reject(error)
614
+ })
615
+ })
616
+
617
+ return createDeviceType.then(result => {
618
+ var deviceObject = result
619
+ var deviceTypeLogicalInterface = null
620
+ var deviceTypeLogicalInterface = new Promise((resolve, reject) => {
621
+ this.createDeviceTypeLogicalInterfaceAssociation(deviceObject.id, logicalInterfaceId).then(result => {
622
+ resolve(result)
623
+ }, error => {
624
+ reject(error)
625
+ })
626
+ })
627
+
628
+ return deviceTypeLogicalInterface.then(result => {
629
+ deviceTypeLogicalInterface = result
630
+ var deviceTypeLogicalInterfacePropertyMappings = new Promise((resolve, reject) => {
631
+ var notificationstrategy = "never"
632
+ if(notificationStrategy) {
633
+ notificationstrategy = notificationStrategy
634
+ }
635
+
636
+ this.createDeviceTypeLogicalInterfacePropertyMappings(deviceObject.id, logicalInterfaceId, eventMapping, notificationstrategy).then(result => {
637
+ var arr = [deviceObject, deviceTypeLogicalInterface, result]
638
+ resolve(arr)
639
+ }, error => {
640
+ reject(error)
641
+ })
642
+ })
643
+
644
+ return deviceTypeLogicalInterfacePropertyMappings.then(result => {
645
+ return result
646
+ })
647
+ })
648
+ })
649
+ }
650
+
651
651
  };