vrack2-core 0.0.1 → 1.0.1
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/README.md +33 -4
- package/docs/Bootstrap.md +77 -0
- package/docs/Container.md +124 -0
- package/docs/Device.md +272 -0
- package/docs/FastStart.md +111 -0
- package/docs/Structure.md +148 -0
- package/lib/Bootstrap.d.ts +79 -0
- package/lib/Bootstrap.js +103 -0
- package/lib/Container.d.ts +202 -6
- package/lib/Container.js +295 -27
- package/lib/IServiceStructure.d.ts +8 -0
- package/lib/IStructureDevice.d.ts +5 -0
- package/lib/ImportManager.d.ts +85 -3
- package/lib/ImportManager.js +122 -16
- package/lib/MainProcess.d.ts +30 -3
- package/lib/MainProcess.js +28 -6
- package/lib/Utility.d.ts +15 -21
- package/lib/Utility.js +40 -40
- package/lib/actions/Action.d.ts +10 -0
- package/lib/actions/Action.js +10 -0
- package/lib/actions/BasicAction.d.ts +60 -0
- package/lib/actions/BasicAction.js +62 -0
- package/lib/actions/GlobalAction.d.ts +5 -0
- package/lib/actions/GlobalAction.js +5 -0
- package/lib/actions/IAction.d.ts +7 -0
- package/lib/actions/ILocalAction.d.ts +7 -0
- package/lib/boot/BootClass.d.ts +93 -0
- package/lib/boot/BootClass.js +101 -0
- package/lib/boot/DeviceFileStorage.d.ts +38 -0
- package/lib/boot/DeviceFileStorage.js +112 -0
- package/lib/boot/DeviceManager.d.ts +190 -0
- package/lib/boot/DeviceManager.js +311 -0
- package/lib/boot/DeviceMetrics.d.ts +82 -0
- package/lib/boot/DeviceMetrics.js +128 -0
- package/lib/boot/StructureStorage.d.ts +59 -0
- package/lib/boot/StructureStorage.js +125 -0
- package/lib/errors/CoreError.d.ts +42 -25
- package/lib/errors/CoreError.js +44 -24
- package/lib/errors/ErrorManager.d.ts +18 -20
- package/lib/errors/ErrorManager.js +23 -22
- package/lib/index.d.ts +20 -4
- package/lib/index.js +28 -4
- package/lib/metrics/BasicMetric.d.ts +49 -0
- package/lib/metrics/BasicMetric.js +79 -0
- package/lib/metrics/IMetricSettings.d.ts +32 -0
- package/lib/metrics/IMetricSettings.js +2 -0
- package/lib/metrics/IvMs.d.ts +9 -0
- package/lib/metrics/IvMs.js +22 -0
- package/lib/metrics/IvS.d.ts +9 -0
- package/lib/metrics/IvS.js +22 -0
- package/lib/metrics/IvUs.d.ts +9 -0
- package/lib/metrics/IvUs.js +22 -0
- package/lib/metrics/Metric.d.ts +17 -0
- package/lib/metrics/Metric.js +27 -0
- package/lib/ports/BasicPort.d.ts +39 -0
- package/lib/ports/BasicPort.js +39 -0
- package/lib/ports/ILocalPort.d.ts +7 -0
- package/lib/ports/IPort.d.ts +7 -0
- package/lib/ports/Port.d.ts +10 -0
- package/lib/ports/Port.js +10 -0
- package/lib/ports/ReturnPort.d.ts +5 -0
- package/lib/ports/ReturnPort.js +5 -0
- package/lib/service/Device.d.ts +213 -78
- package/lib/service/Device.js +185 -83
- package/lib/service/DeviceConnect.d.ts +4 -8
- package/lib/service/DeviceConnect.js +4 -8
- package/lib/service/DevicePort.d.ts +15 -6
- package/lib/service/DevicePort.js +29 -12
- package/lib/service/IDeviceEvent.d.ts +4 -1
- package/lib/validator/IValidationProblem.d.ts +7 -0
- package/lib/validator/IValidationRule.d.ts +12 -0
- package/lib/validator/IValidationSubrule.d.ts +2 -0
- package/lib/validator/Rule.d.ts +6 -2
- package/lib/validator/Rule.js +12 -2
- package/lib/validator/Validator.d.ts +48 -3
- package/lib/validator/Validator.js +70 -18
- package/lib/validator/types/AnyType.d.ts +17 -0
- package/lib/validator/types/AnyType.js +34 -0
- package/lib/validator/types/ArrayType.d.ts +37 -4
- package/lib/validator/types/ArrayType.js +42 -6
- package/lib/validator/types/BasicType.d.ts +67 -7
- package/lib/validator/types/BasicType.js +74 -8
- package/lib/validator/types/BooleanType.d.ts +23 -0
- package/lib/validator/types/BooleanType.js +47 -0
- package/lib/validator/types/FunctionType.d.ts +17 -0
- package/lib/validator/types/FunctionType.js +38 -0
- package/lib/validator/types/NumberType.d.ts +40 -5
- package/lib/validator/types/NumberType.js +53 -14
- package/lib/validator/types/ObjectType.d.ts +32 -5
- package/lib/validator/types/ObjectType.js +42 -8
- package/lib/validator/types/StringType.d.ts +30 -5
- package/lib/validator/types/StringType.js +33 -7
- package/package.json +15 -14
- package/src/Bootstrap.ts +122 -0
- package/src/Container.ts +411 -43
- package/src/IServiceStructure.ts +9 -0
- package/src/IStructureDevice.ts +5 -0
- package/src/ImportManager.ts +119 -11
- package/src/MainProcess.ts +53 -8
- package/src/Utility.ts +35 -36
- package/src/actions/Action.ts +12 -0
- package/src/actions/BasicAction.ts +63 -0
- package/src/actions/GlobalAction.ts +5 -0
- package/src/actions/IAction.ts +7 -0
- package/src/actions/ILocalAction.ts +7 -0
- package/src/boot/BootClass.ts +117 -0
- package/src/boot/DeviceFileStorage.ts +96 -0
- package/src/boot/DeviceManager.ts +346 -0
- package/src/boot/DeviceMetrics.ts +129 -0
- package/src/boot/StructureStorage.ts +108 -0
- package/src/errors/CoreError.ts +52 -26
- package/src/errors/ErrorManager.ts +46 -33
- package/src/index.ts +30 -6
- package/src/metrics/BasicMetric.ts +84 -0
- package/src/metrics/IMetricSettings.ts +38 -0
- package/src/metrics/IvMs.ts +18 -0
- package/src/metrics/IvS.ts +18 -0
- package/src/metrics/IvUs.ts +17 -0
- package/src/metrics/Metric.ts +25 -0
- package/src/ports/BasicPort.ts +39 -0
- package/src/ports/ILocalPort.ts +7 -0
- package/src/ports/IPort.ts +7 -0
- package/src/ports/Port.ts +11 -1
- package/src/ports/ReturnPort.ts +5 -0
- package/src/service/Device.ts +234 -103
- package/src/service/DeviceConnect.ts +4 -8
- package/src/service/DevicePort.ts +30 -11
- package/src/service/IDeviceEvent.ts +4 -1
- package/src/validator/IValidationProblem.ts +7 -0
- package/src/validator/IValidationRule.ts +12 -0
- package/src/validator/IValidationSubrule.ts +3 -0
- package/src/validator/Rule.ts +16 -2
- package/src/validator/Validator.ts +74 -23
- package/src/validator/types/AnyType.ts +32 -0
- package/src/validator/types/ArrayType.ts +43 -7
- package/src/validator/types/BasicType.ts +78 -9
- package/src/validator/types/BooleanType.ts +49 -0
- package/src/validator/types/FunctionType.ts +39 -0
- package/src/validator/types/NumberType.ts +53 -15
- package/src/validator/types/ObjectType.ts +52 -14
- package/src/validator/types/StringType.ts +34 -10
- package/docs/RU-README.md +0 -6
- package/lib/DeviceManager.d.ts +0 -32
- package/lib/DeviceManager.js +0 -143
- package/lib/test.d.ts +0 -1
- package/lib/test.js +0 -58
- package/src/DeviceManager.ts +0 -124
- package/src/test.ts +0 -82
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import Container from '../Container';
|
|
2
|
+
import BasicType from '../validator/types/BasicType';
|
|
3
|
+
/**
|
|
4
|
+
* Boot classes are designed to override behavior outside of the container.
|
|
5
|
+
* They can also extend the container's capabilities.
|
|
6
|
+
*
|
|
7
|
+
* Boot classes are similar to devices inside the container,
|
|
8
|
+
* but they do not fall into it and do not have connections.
|
|
9
|
+
*
|
|
10
|
+
* Boot classes are created inside a Bootstrap class.
|
|
11
|
+
* To do this, you need to create a list of classes and specify them for bootstrapping,
|
|
12
|
+
*
|
|
13
|
+
* here is an example of such a list
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* DeviceManager: { path: 'vrack2-core.DeviceManager', options: { storageDir: './storage' }},
|
|
17
|
+
* DeviceStorage: { path: 'vrack2-core.DeviceFileStorage', options: {} },
|
|
18
|
+
* StructureStorage: { path: 'vrack2-core.StructureStorage', options: {} },
|
|
19
|
+
* DeviceMetrics: { path: 'vrack2-core.DeviceMetrics', options: {} }
|
|
20
|
+
* ```
|
|
21
|
+
* Where:
|
|
22
|
+
* ```
|
|
23
|
+
* [key: string - Unique bootclass identifier] : {
|
|
24
|
+
* path: string - Path to bootclass,
|
|
25
|
+
* options: {any} - Bootclass settings
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @see DeviceManager For boot class example
|
|
30
|
+
*/
|
|
31
|
+
export default class BootClass {
|
|
32
|
+
/**
|
|
33
|
+
* A unique identifier that will be filled in when the class is created within Bootstrap.
|
|
34
|
+
* Can be used to refer to the class directly
|
|
35
|
+
*
|
|
36
|
+
* @example 'DeviceName'
|
|
37
|
+
* */
|
|
38
|
+
id: string;
|
|
39
|
+
/**
|
|
40
|
+
* The path to a VRack2-style class that will be populated when the class is created inside Bootstrap.
|
|
41
|
+
* Can be used to determine class membership
|
|
42
|
+
*
|
|
43
|
+
* @example 'vrack.KeyManager'
|
|
44
|
+
*/
|
|
45
|
+
type: string;
|
|
46
|
+
/**
|
|
47
|
+
* Container for which Boot classes are loaded
|
|
48
|
+
* */
|
|
49
|
+
Container: Container;
|
|
50
|
+
/**
|
|
51
|
+
* Boot class parameters that will be passed from the settings of the list of boot classes
|
|
52
|
+
*
|
|
53
|
+
* @see checkOptions()
|
|
54
|
+
*/
|
|
55
|
+
options: {
|
|
56
|
+
[key: string]: any;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Method returns rules for validation of boot class options
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* return {
|
|
64
|
+
* keysPath: Rule.string().require().default('./keys.json')
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
checkOptions(): {
|
|
69
|
+
[key: string]: BasicType;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* @param id Unique ID
|
|
73
|
+
* @param type Device type string
|
|
74
|
+
* @param Container Active loader container
|
|
75
|
+
*/
|
|
76
|
+
constructor(id: string, type: string, Container: Container, options: {
|
|
77
|
+
[key: string]: any;
|
|
78
|
+
});
|
|
79
|
+
/**
|
|
80
|
+
* The method is the entry point to start the boot class
|
|
81
|
+
*/
|
|
82
|
+
process(): void;
|
|
83
|
+
/**
|
|
84
|
+
* Similar to `process` but asynchronous,
|
|
85
|
+
* the bootstrap loader will wait for the execution of all
|
|
86
|
+
* the `processPromise` methods of all bootstrap classes.
|
|
87
|
+
*/
|
|
88
|
+
processPromise(): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Method for calling container system errors
|
|
91
|
+
*/
|
|
92
|
+
error(error: Error): void;
|
|
93
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright © 2024 Boris Bobylev. All rights reserved.
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
*/
|
|
6
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
7
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
16
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
const Validator_1 = __importDefault(require("../validator/Validator"));
|
|
20
|
+
/**
|
|
21
|
+
* Boot classes are designed to override behavior outside of the container.
|
|
22
|
+
* They can also extend the container's capabilities.
|
|
23
|
+
*
|
|
24
|
+
* Boot classes are similar to devices inside the container,
|
|
25
|
+
* but they do not fall into it and do not have connections.
|
|
26
|
+
*
|
|
27
|
+
* Boot classes are created inside a Bootstrap class.
|
|
28
|
+
* To do this, you need to create a list of classes and specify them for bootstrapping,
|
|
29
|
+
*
|
|
30
|
+
* here is an example of such a list
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* DeviceManager: { path: 'vrack2-core.DeviceManager', options: { storageDir: './storage' }},
|
|
34
|
+
* DeviceStorage: { path: 'vrack2-core.DeviceFileStorage', options: {} },
|
|
35
|
+
* StructureStorage: { path: 'vrack2-core.StructureStorage', options: {} },
|
|
36
|
+
* DeviceMetrics: { path: 'vrack2-core.DeviceMetrics', options: {} }
|
|
37
|
+
* ```
|
|
38
|
+
* Where:
|
|
39
|
+
* ```
|
|
40
|
+
* [key: string - Unique bootclass identifier] : {
|
|
41
|
+
* path: string - Path to bootclass,
|
|
42
|
+
* options: {any} - Bootclass settings
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @see DeviceManager For boot class example
|
|
47
|
+
*/
|
|
48
|
+
class BootClass {
|
|
49
|
+
/**
|
|
50
|
+
* Method returns rules for validation of boot class options
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* return {
|
|
55
|
+
* keysPath: Rule.string().require().default('./keys.json')
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
checkOptions() {
|
|
60
|
+
return {};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* @param id Unique ID
|
|
64
|
+
* @param type Device type string
|
|
65
|
+
* @param Container Active loader container
|
|
66
|
+
*/
|
|
67
|
+
constructor(id, type, Container, options) {
|
|
68
|
+
/**
|
|
69
|
+
* Boot class parameters that will be passed from the settings of the list of boot classes
|
|
70
|
+
*
|
|
71
|
+
* @see checkOptions()
|
|
72
|
+
*/
|
|
73
|
+
this.options = {};
|
|
74
|
+
this.id = id;
|
|
75
|
+
this.type = type;
|
|
76
|
+
this.Container = Container;
|
|
77
|
+
this.options = options;
|
|
78
|
+
// Validating
|
|
79
|
+
const rules = this.checkOptions();
|
|
80
|
+
Validator_1.default.validate(rules, this.options);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The method is the entry point to start the boot class
|
|
84
|
+
*/
|
|
85
|
+
process() { return; }
|
|
86
|
+
/**
|
|
87
|
+
* Similar to `process` but asynchronous,
|
|
88
|
+
* the bootstrap loader will wait for the execution of all
|
|
89
|
+
* the `processPromise` methods of all bootstrap classes.
|
|
90
|
+
*/
|
|
91
|
+
processPromise() {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () { return; });
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Method for calling container system errors
|
|
96
|
+
*/
|
|
97
|
+
error(error) {
|
|
98
|
+
this.Container.emit('system.error', error);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.default = BootClass;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import BootClass from "./BootClass";
|
|
2
|
+
import BasicType from "../validator/types/BasicType";
|
|
3
|
+
/**
|
|
4
|
+
* File storage for devices that are used by default
|
|
5
|
+
* Uses container events to load and save storage state
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
export default class DeviceFileStorage extends BootClass {
|
|
9
|
+
checkOptions(): {
|
|
10
|
+
[key: string]: BasicType;
|
|
11
|
+
};
|
|
12
|
+
process(): void;
|
|
13
|
+
/**
|
|
14
|
+
* loading a device storage file
|
|
15
|
+
* If the primary storage file does not exist, an attempt will be made
|
|
16
|
+
* to locate the backup temporary storage file.
|
|
17
|
+
*
|
|
18
|
+
* @param deviceId Device ID
|
|
19
|
+
* @returns {any} Imported JSON
|
|
20
|
+
*/
|
|
21
|
+
loadDeviceStorage(deviceId: string): Promise<any>;
|
|
22
|
+
/**
|
|
23
|
+
* Save data device to storage
|
|
24
|
+
* First, a temporary file is created,
|
|
25
|
+
* and then the temporary file is copied to the main file
|
|
26
|
+
*
|
|
27
|
+
* @param deviceId Device ID
|
|
28
|
+
* @param trace Data object for storage
|
|
29
|
+
*/
|
|
30
|
+
saveDeviceStorage(deviceId: string, trace: any): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Forms the path to the storage file
|
|
33
|
+
*
|
|
34
|
+
* @param deviceId Device ID
|
|
35
|
+
* @param prefix Prefix between the device name and its extension
|
|
36
|
+
*/
|
|
37
|
+
protected makeDeviceStoragePath(deviceId: string, prefix?: string): string;
|
|
38
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright © 2024 Boris Bobylev. All rights reserved.
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
*/
|
|
6
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
7
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
16
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
const path_1 = __importDefault(require("path"));
|
|
20
|
+
const fs_1 = __importDefault(require("fs"));
|
|
21
|
+
const ImportManager_1 = __importDefault(require("../ImportManager"));
|
|
22
|
+
const BootClass_1 = __importDefault(require("./BootClass"));
|
|
23
|
+
const Rule_1 = __importDefault(require("../validator/Rule"));
|
|
24
|
+
/**
|
|
25
|
+
* File storage for devices that are used by default
|
|
26
|
+
* Uses container events to load and save storage state
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
class DeviceFileStorage extends BootClass_1.default {
|
|
30
|
+
checkOptions() {
|
|
31
|
+
return {
|
|
32
|
+
storageDir: Rule_1.default.string().require().default('./storage')
|
|
33
|
+
.description('Dir for storage files')
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
process() {
|
|
37
|
+
if (!ImportManager_1.default.isDir(this.options.storageDir))
|
|
38
|
+
fs_1.default.mkdirSync(this.options.storageDir, { recursive: true });
|
|
39
|
+
// Before process load storage data
|
|
40
|
+
this.Container.on('beforeProcess', () => {
|
|
41
|
+
for (const id in this.Container.devices) {
|
|
42
|
+
try {
|
|
43
|
+
this.Container.devices[id].storage = this.loadDeviceStorage(id);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
this.Container.emit('system.error', error);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
// On device save - save storage data
|
|
51
|
+
this.Container.on('device.save', (data) => {
|
|
52
|
+
try {
|
|
53
|
+
this.saveDeviceStorage(data.device, data.trace);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
this.Container.emit('system.error', error);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* loading a device storage file
|
|
62
|
+
* If the primary storage file does not exist, an attempt will be made
|
|
63
|
+
* to locate the backup temporary storage file.
|
|
64
|
+
*
|
|
65
|
+
* @param deviceId Device ID
|
|
66
|
+
* @returns {any} Imported JSON
|
|
67
|
+
*/
|
|
68
|
+
loadDeviceStorage(deviceId) {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
const fp = this.makeDeviceStoragePath(deviceId);
|
|
71
|
+
const fptmp = this.makeDeviceStoragePath(deviceId, '-tmp');
|
|
72
|
+
if (!fs_1.default.existsSync(fp)) {
|
|
73
|
+
if (!fs_1.default.existsSync(fptmp))
|
|
74
|
+
return {};
|
|
75
|
+
fs_1.default.renameSync(fptmp, fp);
|
|
76
|
+
}
|
|
77
|
+
return ImportManager_1.default.importJSON(fp);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Save data device to storage
|
|
82
|
+
* First, a temporary file is created,
|
|
83
|
+
* and then the temporary file is copied to the main file
|
|
84
|
+
*
|
|
85
|
+
* @param deviceId Device ID
|
|
86
|
+
* @param trace Data object for storage
|
|
87
|
+
*/
|
|
88
|
+
saveDeviceStorage(deviceId, trace) {
|
|
89
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
+
const fp = this.makeDeviceStoragePath(deviceId);
|
|
91
|
+
const dp = path_1.default.join(this.options.storageDir, this.Container.id);
|
|
92
|
+
const fptmp = this.makeDeviceStoragePath(deviceId, '-tmp');
|
|
93
|
+
if (!fs_1.default.existsSync(dp)) {
|
|
94
|
+
fs_1.default.mkdirSync(dp, { recursive: true });
|
|
95
|
+
}
|
|
96
|
+
fs_1.default.writeFileSync(fptmp, JSON.stringify(trace));
|
|
97
|
+
if (fs_1.default.existsSync(fp))
|
|
98
|
+
fs_1.default.rmSync(fp);
|
|
99
|
+
fs_1.default.renameSync(fptmp, fp);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Forms the path to the storage file
|
|
104
|
+
*
|
|
105
|
+
* @param deviceId Device ID
|
|
106
|
+
* @param prefix Prefix between the device name and its extension
|
|
107
|
+
*/
|
|
108
|
+
makeDeviceStoragePath(deviceId, prefix = '') {
|
|
109
|
+
return path_1.default.join(this.options.storageDir, this.Container.id, deviceId + prefix + '.json');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.default = DeviceFileStorage;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import CoreError from '../errors/CoreError';
|
|
2
|
+
import BootClass from './BootClass';
|
|
3
|
+
import BasicType from '../validator/types/BasicType';
|
|
4
|
+
import IAction from '../actions/IAction';
|
|
5
|
+
import IMetricSettings from '../metrics/IMetricSettings';
|
|
6
|
+
import IPort from '../ports/IPort';
|
|
7
|
+
import IValidationRule from '../validator/IValidationRule';
|
|
8
|
+
export interface IDeviceVendor {
|
|
9
|
+
/** Group name = Vendor name */
|
|
10
|
+
name: string;
|
|
11
|
+
/** Global devices dir */
|
|
12
|
+
dir: string;
|
|
13
|
+
/**
|
|
14
|
+
* List of all devices in group
|
|
15
|
+
* Like a
|
|
16
|
+
*
|
|
17
|
+
* ```
|
|
18
|
+
* ['Device1', 'Device2', 'Device3', 'Device4']
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
deviceList: Array<string>;
|
|
22
|
+
/**
|
|
23
|
+
* Errors in group
|
|
24
|
+
* Contains all errors collected in the group during initialization
|
|
25
|
+
*
|
|
26
|
+
* Errors like a DM_LIST_NOT_FOUND & DM_LIST_INCORRECT
|
|
27
|
+
*/
|
|
28
|
+
errors: Array<CoreError>;
|
|
29
|
+
/**
|
|
30
|
+
* Not used now
|
|
31
|
+
* Vendor description
|
|
32
|
+
*/
|
|
33
|
+
description: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* An object based on this interface contains all
|
|
37
|
+
* the basic exported information about the device
|
|
38
|
+
*
|
|
39
|
+
* @see getDeviceInfo
|
|
40
|
+
*/
|
|
41
|
+
export interface IDeivceInfo {
|
|
42
|
+
/** List of device actions */
|
|
43
|
+
actions: {
|
|
44
|
+
[key: string]: IAction;
|
|
45
|
+
};
|
|
46
|
+
/** List of device metrics */
|
|
47
|
+
metrics: {
|
|
48
|
+
[key: string]: IMetricSettings;
|
|
49
|
+
};
|
|
50
|
+
/** List of device inputs */
|
|
51
|
+
inputs: {
|
|
52
|
+
[key: string]: IPort;
|
|
53
|
+
};
|
|
54
|
+
/** List of device outputs */
|
|
55
|
+
outputs: {
|
|
56
|
+
[key: string]: IPort;
|
|
57
|
+
};
|
|
58
|
+
/** List of device options rules */
|
|
59
|
+
options: {
|
|
60
|
+
[key: string]: IValidationRule;
|
|
61
|
+
};
|
|
62
|
+
/** Device descriotion */
|
|
63
|
+
description: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* BootClass which generates a list of available devices for the container.
|
|
67
|
+
* Provides the ability to initialize devices and provide information about them
|
|
68
|
+
*
|
|
69
|
+
*
|
|
70
|
+
* @see get
|
|
71
|
+
*/
|
|
72
|
+
export default class DeviceManager extends BootClass {
|
|
73
|
+
/**
|
|
74
|
+
* Contains a list of devices and their full path to the class
|
|
75
|
+
* Like a:
|
|
76
|
+
*
|
|
77
|
+
* ```js
|
|
78
|
+
* {
|
|
79
|
+
* "vendorname.Devicename": '/path/to/js/file.js'
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
* */
|
|
83
|
+
protected devices: Map<string, string>;
|
|
84
|
+
/**
|
|
85
|
+
* Vendor array.
|
|
86
|
+
*
|
|
87
|
+
* @see IDeviceVendor
|
|
88
|
+
*/
|
|
89
|
+
protected vendorList: Array<IDeviceVendor>;
|
|
90
|
+
checkOptions(): {
|
|
91
|
+
[key: string]: BasicType;
|
|
92
|
+
};
|
|
93
|
+
process(): void;
|
|
94
|
+
/**
|
|
95
|
+
* Return vendor list
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* ['vrack', 'basic', 'net']
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
getVendorList(): string[];
|
|
103
|
+
/**
|
|
104
|
+
* Returns a list of devices from a specific vendor
|
|
105
|
+
*
|
|
106
|
+
* @param vendor Vendor name
|
|
107
|
+
* @see getVendorList()
|
|
108
|
+
*/
|
|
109
|
+
getVendorDeviceList(vendor: string): Array<string>;
|
|
110
|
+
/**
|
|
111
|
+
* Exports all basic device parameters and collects them in one object
|
|
112
|
+
* Used to generate documentation for the device
|
|
113
|
+
*
|
|
114
|
+
* @see IDeivceInfo
|
|
115
|
+
*/
|
|
116
|
+
getDeviceInfo(vendor: string, device: string): Promise<IDeivceInfo>;
|
|
117
|
+
/**
|
|
118
|
+
* Method for updating the device list
|
|
119
|
+
* Updates the available device lists.
|
|
120
|
+
*
|
|
121
|
+
* Searches the directory specified in `options.dir` and adds all directories in it as vendors.
|
|
122
|
+
* Each folder attempts to read the list.json file to get a list of vendor devices
|
|
123
|
+
*
|
|
124
|
+
* Two types of list.json file definition are supported
|
|
125
|
+
*
|
|
126
|
+
* First type - array:
|
|
127
|
+
*
|
|
128
|
+
* ```json
|
|
129
|
+
* ['Device1', 'Device2', 'Device3']
|
|
130
|
+
* ```
|
|
131
|
+
* Second type - Object
|
|
132
|
+
* This approach allows you to point to devices that are deeper than the vendor's root folder
|
|
133
|
+
*
|
|
134
|
+
* ```json
|
|
135
|
+
* {
|
|
136
|
+
* "Master": "devices/Master",
|
|
137
|
+
* "Interval": "devices/Interval",
|
|
138
|
+
* "Guard": "devices/Guard",
|
|
139
|
+
* }
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @see arrayDeviceListPrepare()
|
|
143
|
+
* @see objectDeviceListPrepare()
|
|
144
|
+
*/
|
|
145
|
+
protected updateDeviceList(): void;
|
|
146
|
+
/**
|
|
147
|
+
* Return class of device
|
|
148
|
+
*
|
|
149
|
+
* Requires special device string of path
|
|
150
|
+
* example "vrack.System" where "vrack" is vendor and "System" is device class
|
|
151
|
+
* @param {string} device Device path string
|
|
152
|
+
*/
|
|
153
|
+
get(device: string, updateList?: boolean): Promise<any>;
|
|
154
|
+
/**
|
|
155
|
+
* Find vendor by name
|
|
156
|
+
*
|
|
157
|
+
* Not found if return boolean
|
|
158
|
+
*/
|
|
159
|
+
protected findVendor(vendor: string): boolean | IDeviceVendor;
|
|
160
|
+
/**
|
|
161
|
+
* Processes a list of devices as an Array
|
|
162
|
+
*
|
|
163
|
+
* Takes an Array of type
|
|
164
|
+
* ```
|
|
165
|
+
* ['DeviceName', 'DeviceName2']
|
|
166
|
+
* ```
|
|
167
|
+
* @return {Array<string>} Sorted list
|
|
168
|
+
*/
|
|
169
|
+
protected arrayDeviceListPrepare(list: Array<string>, group: IDeviceVendor): string[];
|
|
170
|
+
/**
|
|
171
|
+
* Processes a list of devices as an object
|
|
172
|
+
*
|
|
173
|
+
* Takes an object of type
|
|
174
|
+
* ```
|
|
175
|
+
* {
|
|
176
|
+
* DeviceName: 'path/to/Device'
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* return keys of list
|
|
181
|
+
* ```
|
|
182
|
+
* ['DeviceName']
|
|
183
|
+
* ```
|
|
184
|
+
*
|
|
185
|
+
* Set in this.devices for device name requiere path
|
|
186
|
+
*/
|
|
187
|
+
protected objectDeviceListPrepare(list: {
|
|
188
|
+
[key: string]: string;
|
|
189
|
+
}, group: IDeviceVendor): string[];
|
|
190
|
+
}
|