@hahnpro/flow-sdk 5.0.0-0 → 5.0.0
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/LICENSE +1 -1
- package/README.md +1 -1
- package/dist/FlowApplication.d.ts +53 -14
- package/dist/FlowApplication.js +266 -85
- package/dist/FlowElement.d.ts +12 -4
- package/dist/FlowElement.js +47 -48
- package/dist/FlowEvent.d.ts +7 -1
- package/dist/FlowEvent.js +5 -5
- package/dist/FlowLogger.d.ts +9 -6
- package/dist/FlowLogger.js +17 -18
- package/dist/FlowModule.d.ts +1 -1
- package/dist/FlowModule.js +3 -3
- package/dist/RpcClient.d.ts +5 -3
- package/dist/RpcClient.js +23 -9
- package/dist/TestModule.js +6 -6
- package/dist/amqp.d.ts +14 -0
- package/dist/amqp.js +13 -0
- package/dist/extra-validators.js +15 -8
- package/dist/flow.interface.d.ts +7 -1
- package/dist/flow.interface.js +7 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/rpc_server.py +89 -77
- package/dist/unit-decorators.d.ts +39 -0
- package/dist/unit-decorators.js +157 -0
- package/dist/unit-utils.d.ts +8 -0
- package/dist/unit-utils.js +144 -0
- package/dist/units.d.ts +31 -0
- package/dist/units.js +570 -0
- package/dist/utils.d.ts +5 -1
- package/dist/utils.js +40 -7
- package/package.json +35 -33
- package/dist/api/Queue.d.ts +0 -15
- package/dist/api/Queue.js +0 -25
- package/dist/api/api.d.ts +0 -22
- package/dist/api/api.interface.d.ts +0 -14
- package/dist/api/api.interface.js +0 -2
- package/dist/api/api.js +0 -38
- package/dist/api/asset.interface.d.ts +0 -45
- package/dist/api/asset.interface.js +0 -2
- package/dist/api/asset.service.d.ts +0 -8
- package/dist/api/asset.service.js +0 -18
- package/dist/api/content.interface.d.ts +0 -33
- package/dist/api/content.interface.js +0 -2
- package/dist/api/content.service.d.ts +0 -9
- package/dist/api/content.service.js +0 -22
- package/dist/api/data.interface.d.ts +0 -29
- package/dist/api/data.interface.js +0 -2
- package/dist/api/data.service.d.ts +0 -15
- package/dist/api/data.service.js +0 -50
- package/dist/api/endpoint.interface.d.ts +0 -18
- package/dist/api/endpoint.interface.js +0 -2
- package/dist/api/endpoint.service.d.ts +0 -7
- package/dist/api/endpoint.service.js +0 -14
- package/dist/api/http.service.d.ts +0 -29
- package/dist/api/http.service.js +0 -96
- package/dist/api/index.d.ts +0 -11
- package/dist/api/index.js +0 -15
- package/dist/api/mock/api.mock.d.ts +0 -102
- package/dist/api/mock/api.mock.js +0 -81
- package/dist/api/mock/asset.mock.service.d.ts +0 -9
- package/dist/api/mock/asset.mock.service.js +0 -18
- package/dist/api/mock/content.mock.service.d.ts +0 -9
- package/dist/api/mock/content.mock.service.js +0 -21
- package/dist/api/mock/data.mock.service.d.ts +0 -11
- package/dist/api/mock/data.mock.service.js +0 -53
- package/dist/api/mock/endpoint.mock.service.d.ts +0 -10
- package/dist/api/mock/endpoint.mock.service.js +0 -15
- package/dist/api/mock/index.d.ts +0 -7
- package/dist/api/mock/index.js +0 -10
- package/dist/api/mock/secret.mock.service.d.ts +0 -5
- package/dist/api/mock/secret.mock.service.js +0 -11
- package/dist/api/mock/task.mock.service.d.ts +0 -8
- package/dist/api/mock/task.mock.service.js +0 -16
- package/dist/api/mock/timeseries.mock.service.d.ts +0 -18
- package/dist/api/mock/timeseries.mock.service.js +0 -70
- package/dist/api/mock/user.mock.service.d.ts +0 -7
- package/dist/api/mock/user.mock.service.js +0 -12
- package/dist/api/secret.interface.d.ts +0 -9
- package/dist/api/secret.interface.js +0 -2
- package/dist/api/secret.service.d.ts +0 -6
- package/dist/api/secret.service.js +0 -10
- package/dist/api/sidriveiq.interface.d.ts +0 -104
- package/dist/api/sidriveiq.interface.js +0 -2
- package/dist/api/sidriveiq.service.d.ts +0 -31
- package/dist/api/sidriveiq.service.js +0 -97
- package/dist/api/task.interface.d.ts +0 -26
- package/dist/api/task.interface.js +0 -2
- package/dist/api/task.service.d.ts +0 -7
- package/dist/api/task.service.js +0 -13
- package/dist/api/timeseries.interface.d.ts +0 -51
- package/dist/api/timeseries.interface.js +0 -2
- package/dist/api/timeseries.service.d.ts +0 -17
- package/dist/api/timeseries.service.js +0 -38
- package/dist/api/user.interface.d.ts +0 -3
- package/dist/api/user.interface.js +0 -2
- package/dist/api/user.service.d.ts +0 -7
- package/dist/api/user.service.js +0 -21
package/dist/rpc_server.py
CHANGED
|
@@ -1,104 +1,116 @@
|
|
|
1
|
+
import asyncio
|
|
1
2
|
import json
|
|
3
|
+
from asyncio import Future
|
|
4
|
+
from functools import partial, wraps
|
|
5
|
+
from aio_pika import IncomingMessage, Exchange, Message, connect_robust, ExchangeType
|
|
6
|
+
from aio_pika.abc import AbstractRobustExchange
|
|
2
7
|
import os
|
|
3
|
-
from functools import wraps
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
user = os.getenv("RABBIT_USER", "guest")
|
|
10
|
+
password = os.getenv("RABBIT_PASSWORD", "guest")
|
|
11
|
+
host = os.getenv("RABBIT_HOST", "localhost")
|
|
12
|
+
port = os.getenv("RABBIT_PORT", "5672")
|
|
13
|
+
vhost = os.getenv("RABBIT_VHOST", "")
|
|
14
|
+
routingKey = os.getenv("RPC_ROUTING_KEY", "rpc")
|
|
15
|
+
max_msg_size = int(os.getenv("MAX_RPC_MSG_SIZE_BYTES", "0"))
|
|
6
16
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
host = os.environ["RABBIT_HOST"]
|
|
10
|
-
except:
|
|
11
|
-
host = "localhost"
|
|
12
|
-
|
|
13
|
-
port = ""
|
|
14
|
-
try:
|
|
15
|
-
port = os.environ["RABBIT_PORT"]
|
|
16
|
-
except:
|
|
17
|
-
port = "5672"
|
|
18
|
-
|
|
19
|
-
routingKey = ""
|
|
20
|
-
try:
|
|
21
|
-
routingKey = os.environ["RPC_ROUTING_KEY"]
|
|
22
|
-
except:
|
|
23
|
-
routingKey = "rpc"
|
|
24
|
-
|
|
25
|
-
remoteProcedures = {}
|
|
17
|
+
remote_procedures = {}
|
|
18
|
+
flow_logs_exchange: AbstractRobustExchange
|
|
26
19
|
|
|
27
20
|
|
|
28
21
|
def RemoteProcedure(func):
|
|
29
|
-
global
|
|
22
|
+
global remote_procedures
|
|
30
23
|
|
|
31
24
|
@wraps(func)
|
|
32
25
|
def function_wrapper(*args, **kwargs):
|
|
33
26
|
return func(*args, **kwargs)
|
|
34
27
|
|
|
35
|
-
|
|
28
|
+
remote_procedures[func.__name__] = func
|
|
36
29
|
|
|
37
30
|
return function_wrapper
|
|
38
31
|
|
|
39
32
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
33
|
+
loop = asyncio.new_event_loop()
|
|
34
|
+
asyncio.set_event_loop(loop)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
async def on_message(exchange: Exchange, message: IncomingMessage):
|
|
38
|
+
def callback(future: Future):
|
|
39
|
+
try:
|
|
40
|
+
res = future.result()
|
|
41
|
+
if type(res) is tuple and type(res[1]) is type and issubclass(res[1], json.JSONEncoder):
|
|
42
|
+
res = json.dumps(res[0], cls=res[1])
|
|
43
|
+
reply1 = {"type": "reply", "value": res}
|
|
44
|
+
except Exception as err:
|
|
45
|
+
# print(traceback.format_list(traceback.extract_stack(err)))
|
|
46
|
+
reply1 = {"type": "error", "message": str(err), "stack": "failed"}
|
|
47
|
+
|
|
48
|
+
asyncio.ensure_future(send_reply(exchange, reply1, message), loop=loop)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
request = json.loads(message.body.decode())
|
|
47
52
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
)
|
|
53
|
+
# call function
|
|
54
|
+
if remote_procedures.keys().__contains__(request["functionName"]):
|
|
55
|
+
func = remote_procedures.get(request["functionName"])
|
|
56
|
+
future = loop.run_in_executor(None, func, *request["arguments"])
|
|
57
|
+
future.add_done_callback(callback)
|
|
52
58
|
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
else:
|
|
60
|
+
reply = {
|
|
61
|
+
"type": "error",
|
|
62
|
+
"message": request["functionName"] + " is not a function",
|
|
63
|
+
}
|
|
64
|
+
await send_reply(exchange, reply, original_message=message)
|
|
55
65
|
|
|
56
|
-
self.channel.queue_bind(
|
|
57
|
-
exchange="rpc_direct_exchange",
|
|
58
|
-
queue=queue_name,
|
|
59
|
-
routing_key=self.routingKey,
|
|
60
|
-
)
|
|
61
66
|
|
|
62
|
-
|
|
67
|
+
async def send_reply(exchange: Exchange, reply, original_message: Message):
|
|
68
|
+
body = json.dumps(reply).encode("utf-8")
|
|
63
69
|
|
|
64
|
-
|
|
70
|
+
if max_msg_size > 0 and len(body) > max_msg_size:
|
|
71
|
+
body = json.dumps(
|
|
72
|
+
{
|
|
73
|
+
"type": "error",
|
|
74
|
+
"message": "Max RPC message size exceeded: " + str(len(body)) + " bytes / " + max_msg_size + " bytes",
|
|
75
|
+
}
|
|
76
|
+
).encode("utf-8")
|
|
65
77
|
|
|
66
|
-
|
|
67
|
-
|
|
78
|
+
await exchange.publish(
|
|
79
|
+
Message(body=body, correlation_id=original_message.correlation_id),
|
|
80
|
+
routing_key=original_message.reply_to,
|
|
81
|
+
)
|
|
68
82
|
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
|
|
84
|
+
async def main(loop, routing_key):
|
|
85
|
+
global flow_logs_exchange
|
|
86
|
+
|
|
87
|
+
url = "amqp://%s:%s@%s:%s/%s" % (user, password, host, port, vhost)
|
|
88
|
+
connection = await connect_robust(
|
|
89
|
+
url, loop=loop
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
channel = await connection.channel()
|
|
93
|
+
|
|
94
|
+
dest_exchange = await channel.declare_exchange(name="rpc_direct_exchange", type=ExchangeType.DIRECT)
|
|
95
|
+
flow_logs_exchange = await channel.declare_exchange(name='flowlogs', type=ExchangeType.FANOUT, durable=True)
|
|
96
|
+
|
|
97
|
+
queue = await channel.declare_queue("", exclusive=True)
|
|
98
|
+
|
|
99
|
+
await queue.bind(dest_exchange, routing_key)
|
|
100
|
+
|
|
101
|
+
await queue.consume(partial(
|
|
102
|
+
on_message, channel.default_exchange)
|
|
103
|
+
)
|
|
71
104
|
|
|
72
105
|
|
|
73
106
|
def start_consumer(routing_key=routingKey):
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
try:
|
|
77
|
-
request = json.loads(body)
|
|
78
|
-
|
|
79
|
-
# call function
|
|
80
|
-
if remoteProcedures.keys().__contains__(request["functionName"]):
|
|
81
|
-
func = remoteProcedures.get(request["functionName"])
|
|
82
|
-
res = func(*request["arguments"])
|
|
83
|
-
reply = {"type": "reply", "value": res}
|
|
84
|
-
else:
|
|
85
|
-
reply = {
|
|
86
|
-
"type": "error",
|
|
87
|
-
"message": request["functionName"] + " is not a function",
|
|
88
|
-
}
|
|
107
|
+
loop.create_task(main(loop, routing_key))
|
|
108
|
+
loop.run_forever()
|
|
89
109
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
routing_key=props.reply_to,
|
|
98
|
-
properties=pika.BasicProperties(correlation_id=props.correlation_id),
|
|
99
|
-
body=json.dumps(reply),
|
|
100
|
-
)
|
|
101
|
-
ch.basic_ack(delivery_tag=method.delivery_tag)
|
|
102
|
-
|
|
103
|
-
with PikaMassenger(routing_key) as consumer:
|
|
104
|
-
consumer.consume(callback=callback)
|
|
110
|
+
def log(message):
|
|
111
|
+
global flow_logs_exchange
|
|
112
|
+
|
|
113
|
+
if flow_logs_exchange is not None:
|
|
114
|
+
flow_logs_exchange.publish(Message(body=json.dumps(message).encode("utf-8")), "")
|
|
115
|
+
else:
|
|
116
|
+
print("Connection not established. Call start_consumer first.")
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ValidationOptions } from 'class-validator';
|
|
2
|
+
export declare function IsTime(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
3
|
+
export declare function IsLength(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
4
|
+
export declare function IsMass(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
5
|
+
export declare function IsElectricCurrent(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
6
|
+
export declare function IsThermodynamicTemperature(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
7
|
+
export declare function IsAmountOfSubstance(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
8
|
+
export declare function IsLuminousIntensity(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
9
|
+
export declare function IsVoltage(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
10
|
+
export declare function IsForce(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
11
|
+
export declare function IsEnergy(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
12
|
+
export declare function IsPower(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
13
|
+
export declare function IsPressure(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
14
|
+
export declare function IsFrequency(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
15
|
+
export declare function IsArea(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
16
|
+
export declare function IsVolume(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
17
|
+
export declare function IsAngle(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
18
|
+
export declare function IsTranslationalAcceleration(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
19
|
+
export declare function IsTranslationalVelocity(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
20
|
+
export declare function IsTranslationalDisplacement(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
21
|
+
export declare function IsSpringConstant(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
22
|
+
export declare function IsRotationalAcceleration(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
23
|
+
export declare function IsRotationalVelocity(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
24
|
+
export declare function IsRotationalDisplacement(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
25
|
+
export declare function IsTorque(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
26
|
+
export declare function IsMomentOfInertia(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
27
|
+
export declare function IsRotatingUnbalance(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
28
|
+
export declare function IsMechanicalPower(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
29
|
+
export declare function IsMechanicalEnergy(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
30
|
+
export declare function IsDynamicViscosity(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
31
|
+
export declare function IsVolumeFlow(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
32
|
+
export declare function IsMassFlow(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
33
|
+
export declare function IsHeatFlux(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
34
|
+
export declare function IsThermalEnergy(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
35
|
+
export declare function IsSpecificHeatCapacity(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
36
|
+
export declare function IsThermalTransmittance(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
37
|
+
export declare function IsElectricalPower(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
38
|
+
export declare function IsElectricalEnergy(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
39
|
+
export declare function IsElectricalFrequency(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IsElectricalFrequency = exports.IsElectricalEnergy = exports.IsElectricalPower = exports.IsThermalTransmittance = exports.IsSpecificHeatCapacity = exports.IsThermalEnergy = exports.IsHeatFlux = exports.IsMassFlow = exports.IsVolumeFlow = exports.IsDynamicViscosity = exports.IsMechanicalEnergy = exports.IsMechanicalPower = exports.IsRotatingUnbalance = exports.IsMomentOfInertia = exports.IsTorque = exports.IsRotationalDisplacement = exports.IsRotationalVelocity = exports.IsRotationalAcceleration = exports.IsSpringConstant = exports.IsTranslationalDisplacement = exports.IsTranslationalVelocity = exports.IsTranslationalAcceleration = exports.IsAngle = exports.IsVolume = exports.IsArea = exports.IsFrequency = exports.IsPressure = exports.IsPower = exports.IsEnergy = exports.IsForce = exports.IsVoltage = exports.IsLuminousIntensity = exports.IsAmountOfSubstance = exports.IsThermodynamicTemperature = exports.IsElectricCurrent = exports.IsMass = exports.IsLength = exports.IsTime = void 0;
|
|
4
|
+
const unit_utils_1 = require("./unit-utils");
|
|
5
|
+
const units_1 = require("./units");
|
|
6
|
+
function IsTime(unit = units_1.units.time.baseUnit, validationOptions) {
|
|
7
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'time', validationOptions);
|
|
8
|
+
}
|
|
9
|
+
exports.IsTime = IsTime;
|
|
10
|
+
function IsLength(unit = units_1.units.length.baseUnit, validationOptions) {
|
|
11
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'length', validationOptions);
|
|
12
|
+
}
|
|
13
|
+
exports.IsLength = IsLength;
|
|
14
|
+
function IsMass(unit = units_1.units.mass.baseUnit, validationOptions) {
|
|
15
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'mass', validationOptions);
|
|
16
|
+
}
|
|
17
|
+
exports.IsMass = IsMass;
|
|
18
|
+
function IsElectricCurrent(unit = units_1.units.electricCurrent.baseUnit, validationOptions) {
|
|
19
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'electricCurrent', validationOptions);
|
|
20
|
+
}
|
|
21
|
+
exports.IsElectricCurrent = IsElectricCurrent;
|
|
22
|
+
function IsThermodynamicTemperature(unit = units_1.units.thermodynamicTemperature.baseUnit, validationOptions) {
|
|
23
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'thermodynamicTemperature', validationOptions);
|
|
24
|
+
}
|
|
25
|
+
exports.IsThermodynamicTemperature = IsThermodynamicTemperature;
|
|
26
|
+
function IsAmountOfSubstance(unit = units_1.units.amountOfSubstance.baseUnit, validationOptions) {
|
|
27
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'amountOfSubstance', validationOptions);
|
|
28
|
+
}
|
|
29
|
+
exports.IsAmountOfSubstance = IsAmountOfSubstance;
|
|
30
|
+
function IsLuminousIntensity(unit = units_1.units.luminousIntensity.baseUnit, validationOptions) {
|
|
31
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'luminousIntensity', validationOptions);
|
|
32
|
+
}
|
|
33
|
+
exports.IsLuminousIntensity = IsLuminousIntensity;
|
|
34
|
+
function IsVoltage(unit = units_1.units.voltage.baseUnit, validationOptions) {
|
|
35
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'voltage', validationOptions);
|
|
36
|
+
}
|
|
37
|
+
exports.IsVoltage = IsVoltage;
|
|
38
|
+
function IsForce(unit = units_1.units.force.baseUnit, validationOptions) {
|
|
39
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'force', validationOptions);
|
|
40
|
+
}
|
|
41
|
+
exports.IsForce = IsForce;
|
|
42
|
+
function IsEnergy(unit = units_1.units.energy.baseUnit, validationOptions) {
|
|
43
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'energy', validationOptions);
|
|
44
|
+
}
|
|
45
|
+
exports.IsEnergy = IsEnergy;
|
|
46
|
+
function IsPower(unit = units_1.units.power.baseUnit, validationOptions) {
|
|
47
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'power', validationOptions);
|
|
48
|
+
}
|
|
49
|
+
exports.IsPower = IsPower;
|
|
50
|
+
function IsPressure(unit = units_1.units.pressure.baseUnit, validationOptions) {
|
|
51
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'pressure', validationOptions);
|
|
52
|
+
}
|
|
53
|
+
exports.IsPressure = IsPressure;
|
|
54
|
+
function IsFrequency(unit = units_1.units.frequency.baseUnit, validationOptions) {
|
|
55
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'frequency', validationOptions);
|
|
56
|
+
}
|
|
57
|
+
exports.IsFrequency = IsFrequency;
|
|
58
|
+
function IsArea(unit = units_1.units.area.baseUnit, validationOptions) {
|
|
59
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'area', validationOptions);
|
|
60
|
+
}
|
|
61
|
+
exports.IsArea = IsArea;
|
|
62
|
+
function IsVolume(unit = units_1.units.volume.baseUnit, validationOptions) {
|
|
63
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'volume', validationOptions);
|
|
64
|
+
}
|
|
65
|
+
exports.IsVolume = IsVolume;
|
|
66
|
+
function IsAngle(unit = units_1.units.angle.baseUnit, validationOptions) {
|
|
67
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'angle', validationOptions);
|
|
68
|
+
}
|
|
69
|
+
exports.IsAngle = IsAngle;
|
|
70
|
+
function IsTranslationalAcceleration(unit = units_1.units.translationalAcceleration.baseUnit, validationOptions) {
|
|
71
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'translationalAcceleration', validationOptions);
|
|
72
|
+
}
|
|
73
|
+
exports.IsTranslationalAcceleration = IsTranslationalAcceleration;
|
|
74
|
+
function IsTranslationalVelocity(unit = units_1.units.translationalVelocity.baseUnit, validationOptions) {
|
|
75
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'translationalVelocity', validationOptions);
|
|
76
|
+
}
|
|
77
|
+
exports.IsTranslationalVelocity = IsTranslationalVelocity;
|
|
78
|
+
function IsTranslationalDisplacement(unit = units_1.units.translationalDisplacement.baseUnit, validationOptions) {
|
|
79
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'translationalDisplacement', validationOptions);
|
|
80
|
+
}
|
|
81
|
+
exports.IsTranslationalDisplacement = IsTranslationalDisplacement;
|
|
82
|
+
function IsSpringConstant(unit = units_1.units.springConstant.baseUnit, validationOptions) {
|
|
83
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'springConstant', validationOptions);
|
|
84
|
+
}
|
|
85
|
+
exports.IsSpringConstant = IsSpringConstant;
|
|
86
|
+
function IsRotationalAcceleration(unit = units_1.units.rotationalAcceleration.baseUnit, validationOptions) {
|
|
87
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'rotationalAcceleration', validationOptions);
|
|
88
|
+
}
|
|
89
|
+
exports.IsRotationalAcceleration = IsRotationalAcceleration;
|
|
90
|
+
function IsRotationalVelocity(unit = units_1.units.rotationalVelocity.baseUnit, validationOptions) {
|
|
91
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'rotationalVelocity', validationOptions);
|
|
92
|
+
}
|
|
93
|
+
exports.IsRotationalVelocity = IsRotationalVelocity;
|
|
94
|
+
function IsRotationalDisplacement(unit = units_1.units.rotationalDisplacement.baseUnit, validationOptions) {
|
|
95
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'rotationalDisplacement', validationOptions);
|
|
96
|
+
}
|
|
97
|
+
exports.IsRotationalDisplacement = IsRotationalDisplacement;
|
|
98
|
+
function IsTorque(unit = units_1.units.torque.baseUnit, validationOptions) {
|
|
99
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'torque', validationOptions);
|
|
100
|
+
}
|
|
101
|
+
exports.IsTorque = IsTorque;
|
|
102
|
+
function IsMomentOfInertia(unit = units_1.units.momentOfInertia.baseUnit, validationOptions) {
|
|
103
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'momentOfInertia', validationOptions);
|
|
104
|
+
}
|
|
105
|
+
exports.IsMomentOfInertia = IsMomentOfInertia;
|
|
106
|
+
function IsRotatingUnbalance(unit = units_1.units.rotatingUnbalance.baseUnit, validationOptions) {
|
|
107
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'rotatingUnbalance', validationOptions);
|
|
108
|
+
}
|
|
109
|
+
exports.IsRotatingUnbalance = IsRotatingUnbalance;
|
|
110
|
+
function IsMechanicalPower(unit = units_1.units.mechanicalPower.baseUnit, validationOptions) {
|
|
111
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'mechanicalPower', validationOptions);
|
|
112
|
+
}
|
|
113
|
+
exports.IsMechanicalPower = IsMechanicalPower;
|
|
114
|
+
function IsMechanicalEnergy(unit = units_1.units.mechanicalEnergy.baseUnit, validationOptions) {
|
|
115
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'mechanicalEnergy', validationOptions);
|
|
116
|
+
}
|
|
117
|
+
exports.IsMechanicalEnergy = IsMechanicalEnergy;
|
|
118
|
+
function IsDynamicViscosity(unit = units_1.units.dynamicViscosity.baseUnit, validationOptions) {
|
|
119
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'dynamicViscosity', validationOptions);
|
|
120
|
+
}
|
|
121
|
+
exports.IsDynamicViscosity = IsDynamicViscosity;
|
|
122
|
+
function IsVolumeFlow(unit = units_1.units.volumeFlow.baseUnit, validationOptions) {
|
|
123
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'volumeFlow', validationOptions);
|
|
124
|
+
}
|
|
125
|
+
exports.IsVolumeFlow = IsVolumeFlow;
|
|
126
|
+
function IsMassFlow(unit = units_1.units.massFlow.baseUnit, validationOptions) {
|
|
127
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'massFlow', validationOptions);
|
|
128
|
+
}
|
|
129
|
+
exports.IsMassFlow = IsMassFlow;
|
|
130
|
+
function IsHeatFlux(unit = units_1.units.heatFlux.baseUnit, validationOptions) {
|
|
131
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'heatFlux', validationOptions);
|
|
132
|
+
}
|
|
133
|
+
exports.IsHeatFlux = IsHeatFlux;
|
|
134
|
+
function IsThermalEnergy(unit = units_1.units.thermalEnergy.baseUnit, validationOptions) {
|
|
135
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'thermalEnergy', validationOptions);
|
|
136
|
+
}
|
|
137
|
+
exports.IsThermalEnergy = IsThermalEnergy;
|
|
138
|
+
function IsSpecificHeatCapacity(unit = units_1.units.specificHeatCapacity.baseUnit, validationOptions) {
|
|
139
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'specificHeatCapacity', validationOptions);
|
|
140
|
+
}
|
|
141
|
+
exports.IsSpecificHeatCapacity = IsSpecificHeatCapacity;
|
|
142
|
+
function IsThermalTransmittance(unit = units_1.units.thermalTransmittance.baseUnit, validationOptions) {
|
|
143
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'thermalTransmittance', validationOptions);
|
|
144
|
+
}
|
|
145
|
+
exports.IsThermalTransmittance = IsThermalTransmittance;
|
|
146
|
+
function IsElectricalPower(unit = units_1.units.electricalPower.baseUnit, validationOptions) {
|
|
147
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'electricalPower', validationOptions);
|
|
148
|
+
}
|
|
149
|
+
exports.IsElectricalPower = IsElectricalPower;
|
|
150
|
+
function IsElectricalEnergy(unit = units_1.units.electricalEnergy.baseUnit, validationOptions) {
|
|
151
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'electricalEnergy', validationOptions);
|
|
152
|
+
}
|
|
153
|
+
exports.IsElectricalEnergy = IsElectricalEnergy;
|
|
154
|
+
function IsElectricalFrequency(unit = units_1.units.electricalFrequency.baseUnit, validationOptions) {
|
|
155
|
+
return (0, unit_utils_1.makeUnitDecorator)(unit, 'electricalFrequency', validationOptions);
|
|
156
|
+
}
|
|
157
|
+
exports.IsElectricalFrequency = IsElectricalFrequency;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ValidationOptions } from 'class-validator';
|
|
2
|
+
export declare function makeUnitDecorator(unit: string, metric: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
3
|
+
export declare function verifyUnit(unit: string, metric: string): number;
|
|
4
|
+
export declare function computeUnitOptions(dimension: string): {
|
|
5
|
+
prefUnit: string;
|
|
6
|
+
convfactor: number;
|
|
7
|
+
offset: number;
|
|
8
|
+
}[];
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeUnitOptions = exports.verifyUnit = exports.makeUnitDecorator = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const class_validator_1 = require("class-validator");
|
|
6
|
+
const units_1 = require("./units");
|
|
7
|
+
function makeUnitDecorator(unit, metric, validationOptions) {
|
|
8
|
+
const conversionFactor = verifyUnit(unit, metric);
|
|
9
|
+
if (conversionFactor < 0) {
|
|
10
|
+
throw new Error(`${unit} is not a valid ${metric}.`);
|
|
11
|
+
}
|
|
12
|
+
return (object, propertyName) => {
|
|
13
|
+
Reflect.defineMetadata(`conversionFactor:${propertyName}`, conversionFactor, object.constructor);
|
|
14
|
+
(0, class_validator_1.registerDecorator)({
|
|
15
|
+
target: object.constructor,
|
|
16
|
+
propertyName,
|
|
17
|
+
options: validationOptions,
|
|
18
|
+
constraints: [metric, unit],
|
|
19
|
+
validator: UnitArgsValidator,
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
exports.makeUnitDecorator = makeUnitDecorator;
|
|
24
|
+
let UnitArgsValidator = class UnitArgsValidator {
|
|
25
|
+
validate(value, validationArguments) {
|
|
26
|
+
return (0, class_validator_1.isNumber)(value);
|
|
27
|
+
}
|
|
28
|
+
defaultMessage(args) {
|
|
29
|
+
return `${args.property} must be a number conforming to the specified constraints`;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
UnitArgsValidator = tslib_1.__decorate([
|
|
33
|
+
(0, class_validator_1.ValidatorConstraint)({ async: false })
|
|
34
|
+
], UnitArgsValidator);
|
|
35
|
+
const prefixes = [
|
|
36
|
+
{ prefix: 'Y', convfactor: 1e24 },
|
|
37
|
+
{ prefix: 'Z', convfactor: 1e21 },
|
|
38
|
+
{ prefix: 'E', convfactor: 1e18 },
|
|
39
|
+
{ prefix: 'P', convfactor: 1e15 },
|
|
40
|
+
{ prefix: 'T', convfactor: 1e12 },
|
|
41
|
+
{ prefix: 'G', convfactor: 1e9 },
|
|
42
|
+
{ prefix: 'M', convfactor: 1e6 },
|
|
43
|
+
{ prefix: 'k', convfactor: 1e3 },
|
|
44
|
+
{ prefix: 'h', convfactor: 1e2 },
|
|
45
|
+
{ prefix: 'da', convfactor: 1e1 },
|
|
46
|
+
{ prefix: 'd', convfactor: 1e-1 },
|
|
47
|
+
{ prefix: 'c', convfactor: 1e-2 },
|
|
48
|
+
{ prefix: 'm', convfactor: 1e-3 },
|
|
49
|
+
{ prefix: 'μ', convfactor: 1e-6 },
|
|
50
|
+
{ prefix: 'n', convfactor: 1e-9 },
|
|
51
|
+
{ prefix: 'p', convfactor: 1e-12 },
|
|
52
|
+
{ prefix: 'f', convfactor: 1e-15 },
|
|
53
|
+
{ prefix: 'a', convfactor: 1e-18 },
|
|
54
|
+
{ prefix: 'z', convfactor: 1e-21 },
|
|
55
|
+
{ prefix: 'y', convfactor: 1e-24 },
|
|
56
|
+
];
|
|
57
|
+
function verifyUnit(unit, metric) {
|
|
58
|
+
if (unit === '' || metric === '')
|
|
59
|
+
return -1;
|
|
60
|
+
const definition = units_1.units[metric];
|
|
61
|
+
for (const dimension of definition.dimensions) {
|
|
62
|
+
const components = dimension.split('*');
|
|
63
|
+
const nominator = components.filter((v) => !v.includes('-'));
|
|
64
|
+
const denominator = components.filter((v) => v.includes('-'));
|
|
65
|
+
const nomIndices = computeIndices(unit.split('/')[0], nominator);
|
|
66
|
+
const denomIndices = computeIndices(unit.split('/')[1] || '', denominator);
|
|
67
|
+
const nomFactor = verifyIndices(nomIndices, unit.split('/')[0]);
|
|
68
|
+
const denomFactor = verifyIndices(denomIndices, unit.split('/')[1]);
|
|
69
|
+
if (nomFactor > 0 && denomFactor > 0) {
|
|
70
|
+
return nomFactor / denomFactor;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return -1;
|
|
74
|
+
}
|
|
75
|
+
exports.verifyUnit = verifyUnit;
|
|
76
|
+
function verifyIndices(indices, unit) {
|
|
77
|
+
if (indices.length === 0 || !unit)
|
|
78
|
+
return 1;
|
|
79
|
+
const filtered = indices
|
|
80
|
+
.map((arr) => arr.filter((v) => v.index !== -1))
|
|
81
|
+
.reduce((previousValue, currentValue) => {
|
|
82
|
+
if (currentValue.length > 1) {
|
|
83
|
+
for (let j = 0; j < currentValue.length - 1; j++) {
|
|
84
|
+
previousValue.forEach((value) => previousValue.push([...value]));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
let index = 0;
|
|
88
|
+
while (index < previousValue.length) {
|
|
89
|
+
previousValue[index].push(currentValue[index % currentValue.length]);
|
|
90
|
+
index++;
|
|
91
|
+
}
|
|
92
|
+
return previousValue;
|
|
93
|
+
}, [[]]);
|
|
94
|
+
outer: for (const filteredElement of filtered) {
|
|
95
|
+
let index = 0;
|
|
96
|
+
let convfactor = 1;
|
|
97
|
+
while (index != unit.length) {
|
|
98
|
+
const find = filteredElement.find((obj) => obj?.index === index);
|
|
99
|
+
if (!find)
|
|
100
|
+
continue outer;
|
|
101
|
+
index += find.length;
|
|
102
|
+
convfactor *= find.convfactor;
|
|
103
|
+
}
|
|
104
|
+
return convfactor;
|
|
105
|
+
}
|
|
106
|
+
return -1;
|
|
107
|
+
}
|
|
108
|
+
function computeIndices(unit, dimensions) {
|
|
109
|
+
return dimensions.map((dimension) => {
|
|
110
|
+
const options = computeUnitOptions(dimension);
|
|
111
|
+
const indices = options.map((preUnit) => unit.indexOf(preUnit.prefUnit));
|
|
112
|
+
return indices.map((ind, index) => ({
|
|
113
|
+
index: ind,
|
|
114
|
+
length: options[index].prefUnit.length,
|
|
115
|
+
convfactor: options[index].convfactor,
|
|
116
|
+
offset: options[index].offset,
|
|
117
|
+
}));
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
function computeUnitOptions(dimension) {
|
|
121
|
+
const definition = units_1.units[units_1.dimensionToUnitMap[dimension.substring(0, 1)]];
|
|
122
|
+
const exponent = dimension.length === 1 ? 1 : Number.parseInt(dimension.substring(dimension.length - 1), 10);
|
|
123
|
+
const withPrefixes = definition.units
|
|
124
|
+
.filter((unit) => unit.SIPrefixes)
|
|
125
|
+
.map((unit) => prefixes.map((prefix) => ({
|
|
126
|
+
prefUnit: prefix.prefix.concat(unit.unit),
|
|
127
|
+
convfactor: Math.pow(prefix.convfactor * unit.conversionFactor, exponent),
|
|
128
|
+
offset: unit.offset || 0,
|
|
129
|
+
})))
|
|
130
|
+
.reduce((prev, curr) => prev.concat(curr), []);
|
|
131
|
+
const allUnits = withPrefixes.concat(definition.units.map((unit) => ({
|
|
132
|
+
prefUnit: unit.unit,
|
|
133
|
+
convfactor: Math.pow(unit.conversionFactor, exponent),
|
|
134
|
+
offset: unit.offset || 0,
|
|
135
|
+
})));
|
|
136
|
+
return dimension.length > 1 && !dimension.endsWith('-1')
|
|
137
|
+
? allUnits.map((unit) => ({
|
|
138
|
+
prefUnit: `${unit.prefUnit}^${dimension.substring(dimension.length - 1)}`,
|
|
139
|
+
convfactor: unit.convfactor,
|
|
140
|
+
offset: unit.offset,
|
|
141
|
+
}))
|
|
142
|
+
: allUnits;
|
|
143
|
+
}
|
|
144
|
+
exports.computeUnitOptions = computeUnitOptions;
|
package/dist/units.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare const dimensionToUnitMap: {
|
|
2
|
+
T: string;
|
|
3
|
+
L: string;
|
|
4
|
+
M: string;
|
|
5
|
+
I: string;
|
|
6
|
+
Θ: string;
|
|
7
|
+
N: string;
|
|
8
|
+
J: string;
|
|
9
|
+
U: string;
|
|
10
|
+
F: string;
|
|
11
|
+
E: string;
|
|
12
|
+
W: string;
|
|
13
|
+
P: string;
|
|
14
|
+
A: string;
|
|
15
|
+
V: string;
|
|
16
|
+
f: string;
|
|
17
|
+
};
|
|
18
|
+
export declare const units: UnitDefinitions;
|
|
19
|
+
export type UnitDefinitions = {
|
|
20
|
+
[key: string]: UnitDefinition;
|
|
21
|
+
};
|
|
22
|
+
export type UnitDefinition = {
|
|
23
|
+
units: {
|
|
24
|
+
unit: string;
|
|
25
|
+
conversionFactor: number;
|
|
26
|
+
SIPrefixes: boolean;
|
|
27
|
+
offset?: number;
|
|
28
|
+
}[];
|
|
29
|
+
baseUnit: string;
|
|
30
|
+
dimensions: string[];
|
|
31
|
+
};
|