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.
- package/.eslintrc.js +6 -0
- package/LICENSE +177 -0
- package/README.md +6 -0
- package/docs/RU-README.md +6 -0
- package/lib/Container.d.ts +30 -0
- package/lib/Container.js +236 -0
- package/lib/DeviceManager.d.ts +32 -0
- package/lib/DeviceManager.js +143 -0
- package/lib/IServiceStructure.d.ts +5 -0
- package/lib/IServiceStructure.js +2 -0
- package/lib/IStructureDevice.d.ts +7 -0
- package/lib/IStructureDevice.js +2 -0
- package/lib/ImportManager.d.ts +15 -0
- package/lib/ImportManager.js +145 -0
- package/lib/MainProcess.d.ts +9 -0
- package/lib/MainProcess.js +28 -0
- package/lib/Utility.d.ts +21 -0
- package/lib/Utility.js +48 -0
- package/lib/actions/Action.d.ts +4 -0
- package/lib/actions/Action.js +16 -0
- package/lib/actions/BasicAction.d.ts +15 -0
- package/lib/actions/BasicAction.js +44 -0
- package/lib/actions/GlobalAction.d.ts +4 -0
- package/lib/actions/GlobalAction.js +17 -0
- package/lib/actions/IAction.d.ts +11 -0
- package/lib/actions/IAction.js +2 -0
- package/lib/actions/ILocalAction.d.ts +11 -0
- package/lib/actions/ILocalAction.js +2 -0
- package/lib/errors/CoreError.d.ts +53 -0
- package/lib/errors/CoreError.js +81 -0
- package/lib/errors/ErrorManager.d.ts +47 -0
- package/lib/errors/ErrorManager.js +84 -0
- package/lib/index.d.ts +19 -0
- package/lib/index.js +55 -0
- package/lib/ports/BasicPort.d.ts +11 -0
- package/lib/ports/BasicPort.js +49 -0
- package/lib/ports/ILocalPort.d.ts +10 -0
- package/lib/ports/ILocalPort.js +2 -0
- package/lib/ports/IPort.d.ts +10 -0
- package/lib/ports/IPort.js +2 -0
- package/lib/ports/Port.d.ts +6 -0
- package/lib/ports/Port.js +20 -0
- package/lib/ports/ReturnPort.d.ts +6 -0
- package/lib/ports/ReturnPort.js +21 -0
- package/lib/ports/StandartPort.d.ts +4 -0
- package/lib/ports/StandartPort.js +17 -0
- package/lib/service/Device.d.ts +175 -0
- package/lib/service/Device.js +191 -0
- package/lib/service/DeviceConnect.d.ts +21 -0
- package/lib/service/DeviceConnect.js +32 -0
- package/lib/service/DevicePort.d.ts +24 -0
- package/lib/service/DevicePort.js +41 -0
- package/lib/service/IDeviceEvent.d.ts +5 -0
- package/lib/service/IDeviceEvent.js +2 -0
- package/lib/test.d.ts +1 -0
- package/lib/test.js +58 -0
- package/lib/validator/IValidationProblem.d.ts +8 -0
- package/lib/validator/IValidationProblem.js +2 -0
- package/lib/validator/IValidationRule.d.ts +9 -0
- package/lib/validator/IValidationRule.js +2 -0
- package/lib/validator/IValidationSubrule.d.ts +4 -0
- package/lib/validator/IValidationSubrule.js +2 -0
- package/lib/validator/Rule.d.ts +12 -0
- package/lib/validator/Rule.js +30 -0
- package/lib/validator/Validator.d.ts +14 -0
- package/lib/validator/Validator.js +57 -0
- package/lib/validator/types/ArrayType.d.ts +14 -0
- package/lib/validator/types/ArrayType.js +58 -0
- package/lib/validator/types/BasicType.d.ts +18 -0
- package/lib/validator/types/BasicType.js +54 -0
- package/lib/validator/types/NumberType.d.ts +19 -0
- package/lib/validator/types/NumberType.js +66 -0
- package/lib/validator/types/ObjectType.d.ts +18 -0
- package/lib/validator/types/ObjectType.js +51 -0
- package/lib/validator/types/StringType.d.ts +19 -0
- package/lib/validator/types/StringType.js +63 -0
- package/package.json +26 -0
- package/src/Container.ts +237 -0
- package/src/DeviceManager.ts +124 -0
- package/src/IServiceStructure.ts +6 -0
- package/src/IStructureDevice.ts +5 -0
- package/src/ImportManager.ts +112 -0
- package/src/MainProcess.ts +18 -0
- package/src/Utility.ts +44 -0
- package/src/actions/Action.ts +12 -0
- package/src/actions/BasicAction.ts +54 -0
- package/src/actions/GlobalAction.ts +14 -0
- package/src/actions/IAction.ts +8 -0
- package/src/actions/ILocalAction.ts +8 -0
- package/src/errors/CoreError.ts +87 -0
- package/src/errors/ErrorManager.ts +93 -0
- package/src/index.ts +30 -0
- package/src/ports/BasicPort.ts +53 -0
- package/src/ports/ILocalPort.ts +12 -0
- package/src/ports/IPort.ts +12 -0
- package/src/ports/Port.ts +17 -0
- package/src/ports/ReturnPort.ts +19 -0
- package/src/ports/StandartPort.ts +13 -0
- package/src/service/Device.ts +225 -0
- package/src/service/DeviceConnect.ts +36 -0
- package/src/service/DevicePort.ts +46 -0
- package/src/service/IDeviceEvent.ts +5 -0
- package/src/test.ts +82 -0
- package/src/validator/IValidationProblem.ts +9 -0
- package/src/validator/IValidationRule.ts +10 -0
- package/src/validator/IValidationSubrule.ts +5 -0
- package/src/validator/Rule.ts +30 -0
- package/src/validator/Validator.ts +67 -0
- package/src/validator/types/ArrayType.ts +67 -0
- package/src/validator/types/BasicType.ts +61 -0
- package/src/validator/types/NumberType.ts +86 -0
- package/src/validator/types/ObjectType.ts +58 -0
- package/src/validator/types/StringType.ts +79 -0
- package/tsconfig.json +103 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import ErrorManager from "./errors/ErrorManager";
|
|
4
|
+
import Rule from "./validator/Rule";
|
|
5
|
+
|
|
6
|
+
ErrorManager.register('ImportManager', 'kwRobwFPzc5g', 'IM_FILE_NOT_FOUND',
|
|
7
|
+
'Import file not found', {
|
|
8
|
+
filePath: Rule.string().description('Path to file')
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
ErrorManager.register('ImportManager', 'dMJfR6rmC7o6', 'IM_JSON_INCORRECT',
|
|
12
|
+
'Import file json incorrect', {
|
|
13
|
+
jsonRaw: Rule.string().description('Raw json data'),
|
|
14
|
+
parsingError: Rule.string().description('Json parse error string')
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
ErrorManager.register('ImportManager', 'u3dsX3vZpKrz', 'IM_CLASS_PATH_ERROR',
|
|
18
|
+
'Error import class - No acts', {
|
|
19
|
+
path: Rule.string().description('Class path string')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
ErrorManager.register('ImportManager', 'PscFAcmXr11U', 'IM_CLASS_VENDOR_ERROR',
|
|
23
|
+
'Error import class - vendor not found', {
|
|
24
|
+
path: Rule.string().description('Class path string')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
ErrorManager.register('ImportManager', 'm9MJNeKv3xYW', 'IM_CLASS_ACT_ERROR',
|
|
28
|
+
'Error import class - class act = undefined', {
|
|
29
|
+
path: Rule.string().description('Class path string')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
export default class ImportManager {
|
|
34
|
+
/**
|
|
35
|
+
* Dynamic import method
|
|
36
|
+
*
|
|
37
|
+
* @param {string} path Full or relative path to file
|
|
38
|
+
*/
|
|
39
|
+
static async importPath(raPath: string) {
|
|
40
|
+
|
|
41
|
+
// IF we have absolute path
|
|
42
|
+
if (path.isAbsolute(raPath)){
|
|
43
|
+
const ti = await ImportManager.tryImport(raPath)
|
|
44
|
+
if (ti !== false) return ti
|
|
45
|
+
}
|
|
46
|
+
const mbfp = path.join(ImportManager.systemPath(), raPath)
|
|
47
|
+
if (existsSync(mbfp)){
|
|
48
|
+
const ti = await ImportManager.tryImport(raPath)
|
|
49
|
+
if (ti !== false) return ti
|
|
50
|
+
}
|
|
51
|
+
const ti = await import(raPath)
|
|
52
|
+
return ti
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static async importClass(cs:string) {
|
|
56
|
+
const acts = cs.split('.')
|
|
57
|
+
const vendor = acts.shift()
|
|
58
|
+
if (typeof vendor !== 'string') throw ErrorManager.make('IM_CLASS_PATH_ERROR', { path: cs })
|
|
59
|
+
let ret = await ImportManager.tryImport(vendor)
|
|
60
|
+
if (ret === false) throw ErrorManager.make('IM_CLASS_VENDOR_ERROR', { path: cs })
|
|
61
|
+
for (const act of acts) {
|
|
62
|
+
ret = ret[act]
|
|
63
|
+
if (ret === undefined) throw ErrorManager.make('IM_CLASS_ACT_ERROR', { path: cs })
|
|
64
|
+
}
|
|
65
|
+
return ret
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static async importJSON(filePath: string){
|
|
69
|
+
if (!path.isAbsolute(filePath)){
|
|
70
|
+
filePath = path.join(ImportManager.systemPath(), filePath)
|
|
71
|
+
}
|
|
72
|
+
if (existsSync(filePath)){
|
|
73
|
+
const jdata = readFileSync(filePath).toString('utf-8')
|
|
74
|
+
return ImportManager.tryJsonParse(jdata)
|
|
75
|
+
}
|
|
76
|
+
throw ErrorManager.make('IM_FILE_NOT_FOUND', { filePath })
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static tryJsonParse(jsonRaw: string) : any{
|
|
80
|
+
try {
|
|
81
|
+
return JSON.parse(jsonRaw)
|
|
82
|
+
} catch (e) {
|
|
83
|
+
throw ErrorManager.make('IM_JSON_INCORRECT', {
|
|
84
|
+
jsonRaw, parsingError: (e instanceof Error)?e.toString() : 'Unknown base json parse error'
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
static camelize(text: string) {
|
|
90
|
+
return text.replace(/^([A-Z])|[.]+(\w)/g, function (match, p1, p2, offset) {
|
|
91
|
+
if (p2) return p2.toUpperCase()
|
|
92
|
+
return p1.toLowerCase()
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
protected static getSub(imp: any, get: string){
|
|
98
|
+
return imp[get]
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static systemPath(){
|
|
102
|
+
return process.cwd()
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
protected static async tryImport(p: string){
|
|
106
|
+
try {
|
|
107
|
+
return await import(p)
|
|
108
|
+
} catch (error){
|
|
109
|
+
return false
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import Container from "./Container";
|
|
2
|
+
import DeviceManager from "./DeviceManager";
|
|
3
|
+
import IServiceStructure from "./IServiceStructure";
|
|
4
|
+
import ImportManager from "./ImportManager";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export default class MainProcess {
|
|
8
|
+
Container: Container
|
|
9
|
+
DeviceManager : DeviceManager
|
|
10
|
+
constructor(ContainerClass: typeof Container, DeviceManagerClass: typeof DeviceManager, service: IServiceStructure){
|
|
11
|
+
this.DeviceManager = new DeviceManagerClass(ImportManager.systemPath(), './devices')
|
|
12
|
+
this.DeviceManager.updateList()
|
|
13
|
+
this.Container = new ContainerClass(service, this.DeviceManager)
|
|
14
|
+
}
|
|
15
|
+
async run (){
|
|
16
|
+
await this.Container.run()
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/Utility.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2022 Boris Bobylev. All rights reserved.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Преобразует строку типа `aaa.bbb.ccc` в строку типа `aaaBbbCcc`
|
|
8
|
+
* Обратите внимание на то, что первый символ первого актета не преобразуется,
|
|
9
|
+
* если есть такая необходимость, можно добавить точку в самое начало строки `.aaa.bbb.ccc`
|
|
10
|
+
* результат выполнения будет `AaaBbbCcc`
|
|
11
|
+
*/
|
|
12
|
+
function camelize(text: string) {
|
|
13
|
+
return text.replace(/^([A-Z])|[.]+(\w)/g, function (match, p1, p2, offset) {
|
|
14
|
+
if (p2) return p2.toUpperCase()
|
|
15
|
+
return p1.toLowerCase()
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Проверяет, является ли JSON строка валидным JSON
|
|
21
|
+
* Обычно используется, когда неважно в чем конретно проблема в JSON
|
|
22
|
+
* и нужно просто узнать валидный он или нет
|
|
23
|
+
*
|
|
24
|
+
*/
|
|
25
|
+
function validJSON(text: string) {
|
|
26
|
+
try {
|
|
27
|
+
const cfg = JSON.parse(text)
|
|
28
|
+
if (!(cfg && typeof cfg === 'object')) return false
|
|
29
|
+
return cfg
|
|
30
|
+
} catch (e) {
|
|
31
|
+
return false
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Преобразует строку в локальный путь VRack, на данный момент
|
|
37
|
+
* это просто приведение строки в lowerCase
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
function toPath(text: string) {
|
|
41
|
+
return text.toLowerCase()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { camelize, validJSON, toPath }
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2022 Boris Bobylev. All rights reserved.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import BasicType from "../validator/types/BasicType";
|
|
7
|
+
import IAction from "./IAction";
|
|
8
|
+
import ILocalAction from "./ILocalAction"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
export default class BasicAction {
|
|
12
|
+
protected action: ILocalAction
|
|
13
|
+
|
|
14
|
+
constructor() {
|
|
15
|
+
this.action = {
|
|
16
|
+
type: 'unknown',
|
|
17
|
+
requirements: {},
|
|
18
|
+
returns: {},
|
|
19
|
+
description: ''
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
requirements(req: { [key: string]: BasicType; }){
|
|
24
|
+
this.action.requirements = req
|
|
25
|
+
return this
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
returns (ret: { [key: string]: BasicType; }){
|
|
29
|
+
this.action.returns = ret
|
|
30
|
+
return this
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
description(des: string){
|
|
34
|
+
this.action.description = des
|
|
35
|
+
return this
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export(): IAction{
|
|
39
|
+
const nAction: IAction = {
|
|
40
|
+
type: this.action.type,
|
|
41
|
+
requirements: {},
|
|
42
|
+
returns: {},
|
|
43
|
+
description: this.action.description,
|
|
44
|
+
}
|
|
45
|
+
for (const key in this.action.requirements){
|
|
46
|
+
nAction.requirements[key] = this.action.requirements[key].export()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for (const key in this.action.returns){
|
|
50
|
+
nAction.returns[key] = this.action.returns[key].export()
|
|
51
|
+
}
|
|
52
|
+
return nAction
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2022 Boris Bobylev. All rights reserved.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import BasicAction from "./BasicAction";
|
|
7
|
+
|
|
8
|
+
export default class GlobalAction extends BasicAction {
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
super()
|
|
12
|
+
this.action.type = 'global'
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2022 Boris Bobylev. All rights reserved.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Базовый класс для реализаиции ошибок с возможностью
|
|
8
|
+
* импортирования и экспортирования классов ошибок для
|
|
9
|
+
* клиент-серверных операций.
|
|
10
|
+
*
|
|
11
|
+
* В VRack очень много передается по сети в виде JSON. Поскольку
|
|
12
|
+
* в чистом виде ошибки не преобрзауются в JSON, были сделаны методы
|
|
13
|
+
* для экспортирования и импортирования с сохранения основных важных свойств.
|
|
14
|
+
*
|
|
15
|
+
* Если импортируемая ошибка имеет свойства которых нет в базовом классе, буду добавлены.
|
|
16
|
+
* Для получения неизвестных свойств можно воспользоватся функцией (для typescript) getUnknownProperty
|
|
17
|
+
*
|
|
18
|
+
* Для работы с ошибками VRack рекомендуется использовать ErrorManager
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
class CoreError extends Error{
|
|
22
|
+
/** Флаг того что ошибка пренадлежит VRack */
|
|
23
|
+
vError = true
|
|
24
|
+
/** Код ошибки VRack */
|
|
25
|
+
vCode = ""
|
|
26
|
+
/** Кородкий код VRack */
|
|
27
|
+
vShort = ""
|
|
28
|
+
/** Список дополнительных параметров */
|
|
29
|
+
vAdd: Array<string> = []
|
|
30
|
+
/** */
|
|
31
|
+
vAddErrors: Array<CoreError> = []
|
|
32
|
+
|
|
33
|
+
constructor(name: string, message: string, code: string, short: string) {
|
|
34
|
+
super(message)
|
|
35
|
+
this.name = name
|
|
36
|
+
this.vCode = code
|
|
37
|
+
this.vShort = short
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Импортирует ошибку пришедшую по сети в виде JSON объекта
|
|
42
|
+
* На всякий случай использует objectify для приходящего объекта
|
|
43
|
+
*
|
|
44
|
+
* @returns {CoreError} this после модификации
|
|
45
|
+
*/
|
|
46
|
+
import(error: any) : this {
|
|
47
|
+
const objectifyError = CoreError.objectify(error)
|
|
48
|
+
Object.assign(this, objectifyError)
|
|
49
|
+
return this
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Возвращает объект для передачи его по сети с предварительным
|
|
54
|
+
* преобразованием в JSON
|
|
55
|
+
*/
|
|
56
|
+
export() : any {
|
|
57
|
+
return CoreError.objectify(this)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
add(err: CoreError) {
|
|
62
|
+
this.vAddErrors.push(CoreError.objectify(err))
|
|
63
|
+
return this
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Для Typescript используется для получения неизвестного
|
|
68
|
+
* свойства экземпляра после импорта пришедшей ошибки
|
|
69
|
+
*/
|
|
70
|
+
getUnknownProperty(field:string): any | undefined {
|
|
71
|
+
const dynamicKey = field as keyof CoreError;
|
|
72
|
+
return this[dynamicKey]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Возвращает объект для передачи его по сети
|
|
77
|
+
*
|
|
78
|
+
* @param {any} error Ошибка для преобразования в объект
|
|
79
|
+
* */
|
|
80
|
+
static objectify(error: any) : any {
|
|
81
|
+
const result: any = {}
|
|
82
|
+
for (const key of Object.getOwnPropertyNames(error)) result[key] = error[key]
|
|
83
|
+
return result
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export default CoreError
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2022 Boris Bobylev. All rights reserved.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import BasicType from "../validator/types/BasicType"
|
|
7
|
+
import CoreError from "./CoreError"
|
|
8
|
+
|
|
9
|
+
interface RegisteredError {
|
|
10
|
+
name: string,
|
|
11
|
+
code: string,
|
|
12
|
+
short: string,
|
|
13
|
+
description: string,
|
|
14
|
+
rules: {[key:string]: BasicType}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Класс является точкой входа для инициализации исключений
|
|
20
|
+
*
|
|
21
|
+
* Было принято решение вместо кучи мелких однотипных файлов сделать один класс
|
|
22
|
+
* со стандартным функционалом
|
|
23
|
+
*/
|
|
24
|
+
class ErrorManager {
|
|
25
|
+
|
|
26
|
+
private registeredList: Array<RegisteredError> = []
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Регистрирует тип ошибки компонента
|
|
30
|
+
*
|
|
31
|
+
* Перед тем как менеджер ошибок может создать ошибки, необходимо зарегистрировать ее тип.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} name Название компонента
|
|
34
|
+
* @param {string} code Код ошибки
|
|
35
|
+
* @param {string} short Короткий код ошибки
|
|
36
|
+
* @param {string} description Описание ошибки
|
|
37
|
+
*
|
|
38
|
+
*/
|
|
39
|
+
register(name: string, code: string, short: string, description: string, rules: {[key:string]: BasicType} = {}) {
|
|
40
|
+
const reg1 = this.getRegistered(code)
|
|
41
|
+
const reg2 = this.getRegistered(short)
|
|
42
|
+
if (reg1 !== null || reg2 !== null) throw this.make('EM_CODE_EXISTS', {code, short})
|
|
43
|
+
const nr = { name, code, short, description, rules}
|
|
44
|
+
this.registeredList.push(nr)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Создание ошибки
|
|
49
|
+
*
|
|
50
|
+
*
|
|
51
|
+
*
|
|
52
|
+
* @param {string} short
|
|
53
|
+
*/
|
|
54
|
+
make(short: string, additional = {}) {
|
|
55
|
+
const reg = this.getRegistered(short)
|
|
56
|
+
if (reg === null) throw this.make('EM_CODE_NOT_FOUND')
|
|
57
|
+
const ne = new CoreError(reg.name, reg.description,reg.code, reg.short)
|
|
58
|
+
ne.vAdd = Object.keys(additional)
|
|
59
|
+
return Object.assign(ne, additional)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Преобразует обычную ошибку в ошибку VRack
|
|
64
|
+
*
|
|
65
|
+
* @param {Error} error Ошибка для преобразования
|
|
66
|
+
*/
|
|
67
|
+
convert(error: any){
|
|
68
|
+
if (error.vError) return error
|
|
69
|
+
const ne = this.make('EM_ERROR_CONVERT')
|
|
70
|
+
ne.import(error)
|
|
71
|
+
return ne
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Возвращает тип ошибки или null
|
|
76
|
+
*
|
|
77
|
+
* @param {string} short Короткий код ошибки или код ошибки поиска
|
|
78
|
+
*/
|
|
79
|
+
private getRegistered(short: string) : RegisteredError | null {
|
|
80
|
+
for (const registered of this.registeredList){
|
|
81
|
+
if (registered.code === short || registered.short === short) return registered
|
|
82
|
+
}
|
|
83
|
+
return null
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
const GlobalErrorManager = new ErrorManager()
|
|
90
|
+
GlobalErrorManager.register('ErrorManager','NcZIb9QvQRcq', 'EM_CODE_EXISTS', 'This code already exists')
|
|
91
|
+
GlobalErrorManager.register('ErrorManager','uLYv4mE1Yo50', 'EM_CODE_NOT_FOUND', 'No such error found')
|
|
92
|
+
GlobalErrorManager.register('ErrorManager','RIl3BUrxWOzP', 'EM_ERROR_CONVERT', 'Converted error')
|
|
93
|
+
export default GlobalErrorManager
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2022 Boris Bobylev. All rights reserved.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/* ---------------- VALIDATOR EXPORT ------------------- */
|
|
8
|
+
export { default as ErrorManager } from './errors/ErrorManager'
|
|
9
|
+
export { default as CoreError } from './errors/CoreError'
|
|
10
|
+
export { default as Rule } from './validator/Rule'
|
|
11
|
+
export { default as Validator } from './validator/Validator'
|
|
12
|
+
export { default as IValidationProblem} from './validator/IValidationProblem'
|
|
13
|
+
export { default as IValidationRule} from './validator/IValidationRule'
|
|
14
|
+
export { default as IValidationSubrule} from './validator/IValidationSubrule'
|
|
15
|
+
|
|
16
|
+
/* ---------------- BASE OF VRACK ------------------- */
|
|
17
|
+
export { default as ImportManager } from './ImportManager';
|
|
18
|
+
export { default as DeviceManager } from './DeviceManager';
|
|
19
|
+
export { default as Container } from './Container';
|
|
20
|
+
export { default as MainProcess } from './MainProcess';
|
|
21
|
+
|
|
22
|
+
export { default as Action } from './actions/Action'
|
|
23
|
+
export { default as Device } from './service/Device'
|
|
24
|
+
export { default as DeviceConnect } from './service/DeviceConnect'
|
|
25
|
+
export { default as DevicePort } from './service/DevicePort'
|
|
26
|
+
export { default as Port } from './ports/Port'
|
|
27
|
+
|
|
28
|
+
export * from './service/Device'
|
|
29
|
+
export * from './service/DeviceConnect'
|
|
30
|
+
export * from './service/DevicePort'
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2022 Boris Bobylev. All rights reserved.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import BasicType from "../validator/types/BasicType";
|
|
7
|
+
import ILocalPort from "./ILocalPort";
|
|
8
|
+
import IPort from "./IPort";
|
|
9
|
+
|
|
10
|
+
export default class BasicPort {
|
|
11
|
+
protected port: ILocalPort
|
|
12
|
+
|
|
13
|
+
constructor() {
|
|
14
|
+
this.port = {
|
|
15
|
+
type: 'unknown',
|
|
16
|
+
description: '',
|
|
17
|
+
required: false,
|
|
18
|
+
dynamic: false,
|
|
19
|
+
count: 0
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
dynamic(count: number){
|
|
24
|
+
this.port.count = count
|
|
25
|
+
this.port.dynamic = true
|
|
26
|
+
return this
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
requirement(req: BasicType ){
|
|
30
|
+
this.port.requirement = req
|
|
31
|
+
return this
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
description(des: string){
|
|
35
|
+
this.port.description = des
|
|
36
|
+
return this
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export(): IPort{
|
|
40
|
+
const nPort: IPort = {
|
|
41
|
+
type: this.port.type,
|
|
42
|
+
description: this.port.description,
|
|
43
|
+
required: this.port.required,
|
|
44
|
+
dynamic: this.port.dynamic,
|
|
45
|
+
count: this.port.count
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (this.port.requirement instanceof BasicType) nPort.requirement = this.port.requirement.export()
|
|
49
|
+
if (this.port.return instanceof BasicType) nPort.return = this.port.return.export()
|
|
50
|
+
|
|
51
|
+
return nPort
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import IValidationRule from "../validator/IValidationRule";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export default interface IPort {
|
|
5
|
+
type: string;
|
|
6
|
+
requirement?: IValidationRule;
|
|
7
|
+
return?: IValidationRule;
|
|
8
|
+
description: string;
|
|
9
|
+
required: boolean;
|
|
10
|
+
dynamic: boolean;
|
|
11
|
+
count: number;
|
|
12
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2022 Boris Bobylev. All rights reserved.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import ReturnPort from "./ReturnPort";
|
|
7
|
+
import StandartPort from "./StandartPort";
|
|
8
|
+
|
|
9
|
+
export default class Port {
|
|
10
|
+
static standart() {
|
|
11
|
+
return new StandartPort()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static return() {
|
|
15
|
+
return new ReturnPort()
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2022 Boris Bobylev. All rights reserved.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import BasicType from "../validator/types/BasicType"
|
|
7
|
+
import BasicPort from "./BasicPort"
|
|
8
|
+
|
|
9
|
+
export default class ReturnPort extends BasicPort {
|
|
10
|
+
constructor() {
|
|
11
|
+
super()
|
|
12
|
+
this.port.type = 'return'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return(req: BasicType ){
|
|
16
|
+
this.port.return = req
|
|
17
|
+
return this
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2022 Boris Bobylev. All rights reserved.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import BasicPort from "./BasicPort"
|
|
7
|
+
|
|
8
|
+
export default class StandartPort extends BasicPort {
|
|
9
|
+
constructor() {
|
|
10
|
+
super()
|
|
11
|
+
this.port.type = 'standart'
|
|
12
|
+
}
|
|
13
|
+
}
|