vrack2-core 0.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.
Files changed (114) hide show
  1. package/.eslintrc.js +6 -0
  2. package/LICENSE +177 -0
  3. package/README.md +6 -0
  4. package/docs/RU-README.md +6 -0
  5. package/lib/Container.d.ts +30 -0
  6. package/lib/Container.js +236 -0
  7. package/lib/DeviceManager.d.ts +32 -0
  8. package/lib/DeviceManager.js +143 -0
  9. package/lib/IServiceStructure.d.ts +5 -0
  10. package/lib/IServiceStructure.js +2 -0
  11. package/lib/IStructureDevice.d.ts +7 -0
  12. package/lib/IStructureDevice.js +2 -0
  13. package/lib/ImportManager.d.ts +15 -0
  14. package/lib/ImportManager.js +145 -0
  15. package/lib/MainProcess.d.ts +9 -0
  16. package/lib/MainProcess.js +28 -0
  17. package/lib/Utility.d.ts +21 -0
  18. package/lib/Utility.js +48 -0
  19. package/lib/actions/Action.d.ts +4 -0
  20. package/lib/actions/Action.js +16 -0
  21. package/lib/actions/BasicAction.d.ts +15 -0
  22. package/lib/actions/BasicAction.js +44 -0
  23. package/lib/actions/GlobalAction.d.ts +4 -0
  24. package/lib/actions/GlobalAction.js +17 -0
  25. package/lib/actions/IAction.d.ts +11 -0
  26. package/lib/actions/IAction.js +2 -0
  27. package/lib/actions/ILocalAction.d.ts +11 -0
  28. package/lib/actions/ILocalAction.js +2 -0
  29. package/lib/errors/CoreError.d.ts +53 -0
  30. package/lib/errors/CoreError.js +81 -0
  31. package/lib/errors/ErrorManager.d.ts +47 -0
  32. package/lib/errors/ErrorManager.js +84 -0
  33. package/lib/index.d.ts +19 -0
  34. package/lib/index.js +55 -0
  35. package/lib/ports/BasicPort.d.ts +11 -0
  36. package/lib/ports/BasicPort.js +49 -0
  37. package/lib/ports/ILocalPort.d.ts +10 -0
  38. package/lib/ports/ILocalPort.js +2 -0
  39. package/lib/ports/IPort.d.ts +10 -0
  40. package/lib/ports/IPort.js +2 -0
  41. package/lib/ports/Port.d.ts +6 -0
  42. package/lib/ports/Port.js +20 -0
  43. package/lib/ports/ReturnPort.d.ts +6 -0
  44. package/lib/ports/ReturnPort.js +21 -0
  45. package/lib/ports/StandartPort.d.ts +4 -0
  46. package/lib/ports/StandartPort.js +17 -0
  47. package/lib/service/Device.d.ts +175 -0
  48. package/lib/service/Device.js +191 -0
  49. package/lib/service/DeviceConnect.d.ts +21 -0
  50. package/lib/service/DeviceConnect.js +32 -0
  51. package/lib/service/DevicePort.d.ts +24 -0
  52. package/lib/service/DevicePort.js +41 -0
  53. package/lib/service/IDeviceEvent.d.ts +5 -0
  54. package/lib/service/IDeviceEvent.js +2 -0
  55. package/lib/test.d.ts +1 -0
  56. package/lib/test.js +58 -0
  57. package/lib/validator/IValidationProblem.d.ts +8 -0
  58. package/lib/validator/IValidationProblem.js +2 -0
  59. package/lib/validator/IValidationRule.d.ts +9 -0
  60. package/lib/validator/IValidationRule.js +2 -0
  61. package/lib/validator/IValidationSubrule.d.ts +4 -0
  62. package/lib/validator/IValidationSubrule.js +2 -0
  63. package/lib/validator/Rule.d.ts +12 -0
  64. package/lib/validator/Rule.js +30 -0
  65. package/lib/validator/Validator.d.ts +14 -0
  66. package/lib/validator/Validator.js +57 -0
  67. package/lib/validator/types/ArrayType.d.ts +14 -0
  68. package/lib/validator/types/ArrayType.js +58 -0
  69. package/lib/validator/types/BasicType.d.ts +18 -0
  70. package/lib/validator/types/BasicType.js +54 -0
  71. package/lib/validator/types/NumberType.d.ts +19 -0
  72. package/lib/validator/types/NumberType.js +66 -0
  73. package/lib/validator/types/ObjectType.d.ts +18 -0
  74. package/lib/validator/types/ObjectType.js +51 -0
  75. package/lib/validator/types/StringType.d.ts +19 -0
  76. package/lib/validator/types/StringType.js +63 -0
  77. package/package.json +26 -0
  78. package/src/Container.ts +237 -0
  79. package/src/DeviceManager.ts +124 -0
  80. package/src/IServiceStructure.ts +6 -0
  81. package/src/IStructureDevice.ts +5 -0
  82. package/src/ImportManager.ts +112 -0
  83. package/src/MainProcess.ts +18 -0
  84. package/src/Utility.ts +44 -0
  85. package/src/actions/Action.ts +12 -0
  86. package/src/actions/BasicAction.ts +54 -0
  87. package/src/actions/GlobalAction.ts +14 -0
  88. package/src/actions/IAction.ts +8 -0
  89. package/src/actions/ILocalAction.ts +8 -0
  90. package/src/errors/CoreError.ts +87 -0
  91. package/src/errors/ErrorManager.ts +93 -0
  92. package/src/index.ts +30 -0
  93. package/src/ports/BasicPort.ts +53 -0
  94. package/src/ports/ILocalPort.ts +12 -0
  95. package/src/ports/IPort.ts +12 -0
  96. package/src/ports/Port.ts +17 -0
  97. package/src/ports/ReturnPort.ts +19 -0
  98. package/src/ports/StandartPort.ts +13 -0
  99. package/src/service/Device.ts +225 -0
  100. package/src/service/DeviceConnect.ts +36 -0
  101. package/src/service/DevicePort.ts +46 -0
  102. package/src/service/IDeviceEvent.ts +5 -0
  103. package/src/test.ts +82 -0
  104. package/src/validator/IValidationProblem.ts +9 -0
  105. package/src/validator/IValidationRule.ts +10 -0
  106. package/src/validator/IValidationSubrule.ts +5 -0
  107. package/src/validator/Rule.ts +30 -0
  108. package/src/validator/Validator.ts +67 -0
  109. package/src/validator/types/ArrayType.ts +67 -0
  110. package/src/validator/types/BasicType.ts +61 -0
  111. package/src/validator/types/NumberType.ts +86 -0
  112. package/src/validator/types/ObjectType.ts +58 -0
  113. package/src/validator/types/StringType.ts +79 -0
  114. package/tsconfig.json +103 -0
package/.eslintrc.js ADDED
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
3
+ parser: '@typescript-eslint/parser',
4
+ plugins: ['@typescript-eslint'],
5
+ root: true,
6
+ };
package/LICENSE ADDED
@@ -0,0 +1,177 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ VRack2 core
2
+ -----------
3
+
4
+ The core set of components for running vrack2. Currently in an initial state and refining as needed.
5
+
6
+ Currently unsuitable for public use.
@@ -0,0 +1,6 @@
1
+ VRack2 core
2
+ -----------
3
+
4
+ Основной набор компонентов для работы vrack2. На данный момент находится в начальном состоянии и дорабатывает по мере необходимости.
5
+
6
+ На данный момент непригодно для публичного использования.
@@ -0,0 +1,30 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from "events";
3
+ import DeviceManager from "./DeviceManager";
4
+ import IServiceStructure from "./IServiceStructure";
5
+ import IStructureDevice from "./IStructureDevice";
6
+ import Device from "./service/Device";
7
+ import BasicAction from "./actions/BasicAction";
8
+ import IPort from "./ports/IPort";
9
+ export default class Container extends EventEmitter {
10
+ protected config: IServiceStructure;
11
+ protected DeviceManager: DeviceManager;
12
+ protected devices: {
13
+ [key: string]: Device;
14
+ };
15
+ protected deviceActions: {
16
+ [key: string]: {
17
+ [key: string]: BasicAction;
18
+ };
19
+ };
20
+ constructor(config: IServiceStructure, dm: DeviceManager);
21
+ run(): Promise<void>;
22
+ protected initDevice(dconf: IStructureDevice): Promise<void>;
23
+ protected checkInputHandler(port: string, handler: string, device: Device): void;
24
+ protected initConnection(conn: string): Promise<void>;
25
+ private toConnection;
26
+ protected checkPortName(port: string): void;
27
+ protected getPortList(name: string, iPort: IPort): {
28
+ [key: string]: IPort;
29
+ };
30
+ }
@@ -0,0 +1,236 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const events_1 = __importDefault(require("events"));
16
+ const ErrorManager_1 = __importDefault(require("./errors/ErrorManager"));
17
+ const Rule_1 = __importDefault(require("./validator/Rule"));
18
+ const CoreError_1 = __importDefault(require("./errors/CoreError"));
19
+ const Validator_1 = __importDefault(require("./validator/Validator"));
20
+ const ImportManager_1 = __importDefault(require("./ImportManager"));
21
+ const DevicePort_1 = __importDefault(require("./service/DevicePort"));
22
+ const DeviceConnect_1 = __importDefault(require("./service/DeviceConnect"));
23
+ ErrorManager_1.default.register('Container', 'FBDRkSAWnlcc', 'CTR_ERROR_INIT_DEVICE', 'Device initialization error', {
24
+ deviceConfig: Rule_1.default.object().description('Device configuration')
25
+ });
26
+ ErrorManager_1.default.register('Container', '96UX24PTyFU7', 'CTR_ERROR_INIT_CONNECTION', 'Connection initialization error', {
27
+ connection: Rule_1.default.string().description('Connection string')
28
+ });
29
+ ErrorManager_1.default.register('Container', '0HVa3cO1E2vB', 'CTR_INCORRECT_DEVICE_ID', 'Incorrect device id', {});
30
+ ErrorManager_1.default.register('Container', 'uF62e07wloS9', 'CTR_DEVICE_DUBLICATE', 'Device id is dublicated', {});
31
+ ErrorManager_1.default.register('Container', '2RZznI3JDNUW', 'CTR_ERROR_PREPARE_OPTIONS', 'An error occurred while preparing options', {
32
+ message: Rule_1.default.string().description('Exception error string')
33
+ });
34
+ ErrorManager_1.default.register('Container', 'NDW2oD7mFxqB', 'CTR_DEVICE_ACTION_NF', 'Action on device not found', {
35
+ action: Rule_1.default.string().description('Action name'),
36
+ method: Rule_1.default.string().description('Method name')
37
+ });
38
+ ErrorManager_1.default.register('Container', 'jkIpU1p4z5uz', 'CTR_INCORRECT_DYNAMIC_PN', 'Incorrect dynamic port name', {
39
+ port: Rule_1.default.string().description('Incorrect port name')
40
+ });
41
+ ErrorManager_1.default.register('Container', 'e8m8dUVVOEU7', 'CTR_INCORRECT_PN', 'Incorrect port name', {
42
+ port: Rule_1.default.string().description('Incorrect port name')
43
+ });
44
+ ErrorManager_1.default.register('Container', 'qPevPU6SRJ18', 'CTR_INPUT_HANDLER_NF', 'Port input handler not found', {
45
+ port: Rule_1.default.string().description('Port name for handler'),
46
+ handler: Rule_1.default.string().description('Handler name')
47
+ });
48
+ ErrorManager_1.default.register('Container', 'Kp74OuVGNU0u', 'CTR_CONNECTION_INCORRECT', 'Incorrect connection format', {
49
+ connection: Rule_1.default.string().description('Connection string'),
50
+ error: Rule_1.default.string().description('String of error problem'),
51
+ });
52
+ ErrorManager_1.default.register('Container', 'eMrJEISxvali', 'CTR_CONNECTION_DEVICE_NF', 'Connection device not found', {
53
+ connection: Rule_1.default.string().description('Connection string'),
54
+ device: Rule_1.default.string().description('Device name not found')
55
+ });
56
+ ErrorManager_1.default.register('Container', 'CwFj1G47H45E', 'CTR_CONNECTION_PORT_NF', 'Connection port not found', {
57
+ connection: Rule_1.default.string().description('Connection string'),
58
+ port: Rule_1.default.string().description('Port name not found')
59
+ });
60
+ class Container extends events_1.default {
61
+ constructor(config, dm) {
62
+ super();
63
+ this.devices = {};
64
+ this.config = config;
65
+ this.DeviceManager = dm;
66
+ this.deviceActions = {};
67
+ }
68
+ run() {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ for (const device of this.config.devices) {
71
+ try {
72
+ yield this.initDevice(device);
73
+ }
74
+ catch (error) {
75
+ const ner = ErrorManager_1.default.make('CTR_ERROR_INIT_DEVICE', { deviceConfig: device });
76
+ if (error instanceof CoreError_1.default)
77
+ ner.add(error);
78
+ throw ner;
79
+ }
80
+ }
81
+ for (const conn of this.config.connections) {
82
+ try {
83
+ yield this.initConnection(conn);
84
+ }
85
+ catch (error) {
86
+ const ner = ErrorManager_1.default.make('CTR_ERROR_INIT_CONNECTION', { connection: conn });
87
+ if (error instanceof CoreError_1.default)
88
+ ner.add(error);
89
+ throw ner;
90
+ }
91
+ }
92
+ for (const key in this.devices)
93
+ this.devices[key].process();
94
+ for (const key in this.devices)
95
+ yield this.devices[key].processPromise();
96
+ // const cs = await this.DeviceManager.get('vrack.Master')
97
+ // const cse = new cs()
98
+ // console.log(cse)
99
+ // cse.run()
100
+ });
101
+ }
102
+ initDevice(dconf) {
103
+ return __awaiter(this, void 0, void 0, function* () {
104
+ const cs = yield this.DeviceManager.get(dconf.type);
105
+ //
106
+ if (dconf.id === undefined || !dconf.id || typeof dconf.id !== 'string') {
107
+ throw ErrorManager_1.default.make('CTR_INCORRECT_DEVICE_ID');
108
+ }
109
+ // Device id is dublicated
110
+ if (dconf.id in this.devices)
111
+ throw ErrorManager_1.default.make('CTR_DEVICE_DUBLICATE');
112
+ const dev = new cs(dconf.id, dconf.type, this);
113
+ this.devices[dconf.id] = dev;
114
+ // Fill options
115
+ for (const key in dconf.options)
116
+ dev.options[key] = dconf.options[key];
117
+ // try prepare options
118
+ try {
119
+ dev.prepareOptions();
120
+ }
121
+ catch (error) {
122
+ let message = '';
123
+ if (error instanceof Error)
124
+ message = error.toString();
125
+ const ner = ErrorManager_1.default.make('CTR_ERROR_PREPARE_OPTIONS', { message });
126
+ if (error instanceof CoreError_1.default)
127
+ ner.add(error);
128
+ throw ner;
129
+ }
130
+ const rules = dev.checkOptions();
131
+ // Validating
132
+ Validator_1.default.validate(rules, dev.options);
133
+ dev.preProcess();
134
+ // Check actions
135
+ this.deviceActions[dev.id] = dev.actions();
136
+ for (const action in this.deviceActions[dev.id]) {
137
+ const method = ImportManager_1.default.camelize('action.' + action);
138
+ if (!(method in dev))
139
+ throw ErrorManager_1.default.make('CTR_DEVICE_ACTION_NF', { action, method });
140
+ }
141
+ // make ports
142
+ // prepare ports
143
+ // make inputPorts
144
+ const iPorts = dev.inputs();
145
+ for (const key in iPorts) {
146
+ const exp = iPorts[key].export();
147
+ const pList = this.getPortList(key, exp);
148
+ for (const subkey in pList) {
149
+ this.checkPortName(subkey);
150
+ const handler = ImportManager_1.default.camelize('input.' + subkey);
151
+ this.checkInputHandler(subkey, handler, dev);
152
+ const ndp = new DevicePort_1.default(subkey, pList[subkey]);
153
+ dev.ports.input[subkey] = ndp;
154
+ if (handler in dev)
155
+ ndp.push = dev[handler].bind(dev);
156
+ }
157
+ }
158
+ const oPorts = dev.outputs();
159
+ for (const key in oPorts) {
160
+ const exp = oPorts[key].export();
161
+ const pList = this.getPortList(key, exp);
162
+ for (const subkey in pList) {
163
+ this.checkPortName(subkey);
164
+ const ndp = new DevicePort_1.default(subkey, pList[subkey]);
165
+ dev.ports.output[subkey] = ndp;
166
+ }
167
+ }
168
+ });
169
+ }
170
+ checkInputHandler(port, handler, device) {
171
+ if (!(handler in device))
172
+ throw ErrorManager_1.default.make('CTR_INPUT_HANDLER_NF', { port, handler });
173
+ }
174
+ initConnection(conn) {
175
+ return __awaiter(this, void 0, void 0, function* () {
176
+ const cc = this.toConnection(conn);
177
+ if (!(cc.outputDevice in this.devices))
178
+ throw ErrorManager_1.default.make('CTR_CONNECTION_DEVICE_NF', { connection: conn, device: cc.outputDevice });
179
+ if (!(cc.outputPort in this.devices[cc.outputDevice].ports.output))
180
+ throw ErrorManager_1.default.make('CTR_CONNECTION_PORT_NF', { connection: conn, port: cc.outputPort });
181
+ if (!(cc.inputDevice in this.devices))
182
+ throw ErrorManager_1.default.make('CTR_CONNECTION_DEVICE_NF', { connection: conn, device: cc.inputDevice });
183
+ if (!(cc.inputPort in this.devices[cc.inputDevice].ports.input))
184
+ throw ErrorManager_1.default.make('CTR_CONNECTION_PORT_NF', { connection: conn, port: cc.inputPort });
185
+ const outPort = this.devices[cc.outputDevice].ports.output[cc.outputPort];
186
+ const inPort = this.devices[cc.inputDevice].ports.input[cc.inputPort];
187
+ new DeviceConnect_1.default(outPort, inPort);
188
+ });
189
+ }
190
+ toConnection(con) {
191
+ var _a, _b;
192
+ const act = con.split('->');
193
+ if (act.length !== 2)
194
+ throw ErrorManager_1.default.make('CTR_CONNECTION_INCORRECT', { connection: con, error: "Syntax connection error, syntax have -> beetwen device" });
195
+ const outputDeviceActs = act[0].split('.');
196
+ const inputDeviceActs = act[1].split('.');
197
+ if (outputDeviceActs.length > 3 || inputDeviceActs.length > 3)
198
+ throw ErrorManager_1.default.make('CTR_CONNECTION_INCORRECT', { connection: con, error: "Syntax connection error, syntax have more 3 actets on side" });
199
+ if (outputDeviceActs.length < 2 || inputDeviceActs.length < 2)
200
+ throw ErrorManager_1.default.make('CTR_CONNECTION_INCORRECT', { connection: con, error: "Syntax connection error, syntax have less 2 actets on side" });
201
+ let outputDevice = (_a = outputDeviceActs.shift()) === null || _a === void 0 ? void 0 : _a.trim();
202
+ const outputPort = outputDeviceActs.join('.').trim();
203
+ let inputDevice = (_b = inputDeviceActs.shift()) === null || _b === void 0 ? void 0 : _b.trim();
204
+ const inputPort = inputDeviceActs.join('.').trim();
205
+ if (outputDevice === undefined)
206
+ outputDevice = '';
207
+ if (inputDevice === undefined)
208
+ inputDevice = '';
209
+ const result = {
210
+ outputDevice, outputPort, inputDevice, inputPort
211
+ };
212
+ return result;
213
+ }
214
+ checkPortName(port) {
215
+ if (!port.match(/[a-zA-Z0-9.]/))
216
+ throw ErrorManager_1.default.make('CTR_INCORRECT_PN', { port });
217
+ }
218
+ getPortList(name, iPort) {
219
+ const result = {};
220
+ if (!iPort.dynamic) {
221
+ result[name] = iPort;
222
+ return result;
223
+ }
224
+ if (!name.match(/%d/))
225
+ throw ErrorManager_1.default.make('CTR_INCORRECT_DYNAMIC_PN', { port: name });
226
+ for (let i = 1; i <= iPort.count; i++) {
227
+ const nIPort = Object.assign({}, iPort);
228
+ nIPort.count = 0;
229
+ nIPort.dynamic = false;
230
+ const nname = name.replace(/%d/, i + '');
231
+ result[nname] = nIPort;
232
+ }
233
+ return result;
234
+ }
235
+ }
236
+ exports.default = Container;
@@ -0,0 +1,32 @@
1
+ interface IDeviceGroup {
2
+ name: string;
3
+ dir: string;
4
+ devices: Array<any>;
5
+ deviceList: Array<string>;
6
+ errors: Array<string>;
7
+ description: string;
8
+ }
9
+ export default class DeviceManager {
10
+ dir: string;
11
+ systemDir: string;
12
+ devices: Map<string, string>;
13
+ deviceList: Array<IDeviceGroup>;
14
+ constructor(systemDir: string, dir: string);
15
+ updateList(): IDeviceGroup[];
16
+ devicesList(): IDeviceGroup[];
17
+ /**
18
+ * Return class of device
19
+ *
20
+ * Requires special device string of path
21
+ * example "vrack.System" where "vrack" is vendor and "System" is device class
22
+ * @param {string} device Device path string
23
+ */
24
+ get(device: string): Promise<any>;
25
+ /**
26
+ *
27
+ *
28
+ *
29
+ */
30
+ private getOnlyDirs;
31
+ }
32
+ export {};
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ const path_1 = __importDefault(require("path"));
39
+ const fs_1 = __importDefault(require("fs"));
40
+ const ErrorManager_1 = __importDefault(require("./errors/ErrorManager"));
41
+ const Rule_1 = __importDefault(require("./validator/Rule"));
42
+ ErrorManager_1.default.register('DeviceManager', 'S5dBTBKTnVbF', 'DM_DEVICE_NOT_FOUND', 'Device not found', {
43
+ device: Rule_1.default.string().require().description('Device request name')
44
+ });
45
+ class DeviceManager {
46
+ constructor(systemDir, dir) {
47
+ this.dir = './devices';
48
+ this.devices = new Map();
49
+ this.deviceList = [];
50
+ this.systemDir = systemDir;
51
+ this.dir = dir;
52
+ }
53
+ updateList() {
54
+ this.deviceList = this.devicesList();
55
+ for (const dgroup of this.deviceList) {
56
+ dgroup.devices = [];
57
+ for (const device of dgroup.deviceList) {
58
+ const ndevice = { name: device, errors: [] };
59
+ dgroup.devices.push(ndevice);
60
+ const devicePath = path_1.default.join(this.systemDir, this.dir, dgroup.name, device + '.js');
61
+ if (!fs_1.default.existsSync(devicePath)) {
62
+ ndevice.errors.push('File of device not found');
63
+ continue;
64
+ }
65
+ }
66
+ }
67
+ return this.deviceList;
68
+ }
69
+ devicesList() {
70
+ const result = [];
71
+ const files = fs_1.default.readdirSync(path_1.default.join(this.systemDir, this.dir));
72
+ const dirs = this.getOnlyDirs(files);
73
+ dirs.sort();
74
+ for (const ddir of dirs) {
75
+ const group = {
76
+ name: ddir,
77
+ dir: this.dir,
78
+ devices: [],
79
+ deviceList: [],
80
+ errors: [],
81
+ description: ''
82
+ };
83
+ result.push(group);
84
+ const listPath = path_1.default.join(this.systemDir, this.dir, ddir, 'list.json');
85
+ if (!fs_1.default.existsSync(listPath)) {
86
+ group.errors.push(listPath + ' ignored - list.json not found');
87
+ continue;
88
+ }
89
+ // @todo Исправить на SAFE
90
+ const list = JSON.parse(fs_1.default.readFileSync(listPath).toString('utf-8'));
91
+ if (!list || !Array.isArray(list)) {
92
+ group.errors.push(listPath + ' ignored - list.json incorrect');
93
+ continue;
94
+ }
95
+ for (const listKey in list) {
96
+ const reqPath = path_1.default.join(this.systemDir, this.dir, ddir, list[listKey]);
97
+ const devicePath = reqPath + '.js';
98
+ if (fs_1.default.existsSync(devicePath)) {
99
+ if (!this.devices.has(ddir + '.' + list[listKey]))
100
+ this.devices.set(ddir + '.' + list[listKey], reqPath);
101
+ else
102
+ console.log(devicePath + ' ignored - device not found');
103
+ }
104
+ }
105
+ list.sort();
106
+ group.deviceList = list;
107
+ }
108
+ return result;
109
+ }
110
+ /**
111
+ * Return class of device
112
+ *
113
+ * Requires special device string of path
114
+ * example "vrack.System" where "vrack" is vendor and "System" is device class
115
+ * @param {string} device Device path string
116
+ */
117
+ get(device) {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ const p = this.devices.get(device);
120
+ if (typeof p === 'string') {
121
+ const deviceClass = yield Promise.resolve(`${p}`).then(s => __importStar(require(s)));
122
+ if (deviceClass.default)
123
+ return deviceClass.default;
124
+ }
125
+ throw ErrorManager_1.default.make('DM_DEVICE_NOT_FOUND', { device });
126
+ });
127
+ }
128
+ /**
129
+ *
130
+ *
131
+ *
132
+ */
133
+ getOnlyDirs(files) {
134
+ const result = [];
135
+ for (const i in files) {
136
+ if (fs_1.default.statSync(path_1.default.join(this.systemDir, this.dir, files[i])).isDirectory())
137
+ result.push(files[i]);
138
+ }
139
+ result.sort();
140
+ return result;
141
+ }
142
+ }
143
+ exports.default = DeviceManager;
@@ -0,0 +1,5 @@
1
+ import IStructureDevice from "./IStructureDevice";
2
+ export default interface IServiceStructure {
3
+ devices: Array<IStructureDevice>;
4
+ connections: Array<string>;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ export default interface IStructureDevice {
2
+ id: string;
3
+ type: string;
4
+ options: {
5
+ [key: string]: any;
6
+ };
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });