@uns-kit/cli 2.0.64 → 2.0.65
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 +21 -21
- package/README.md +168 -168
- package/dist/index.js +59 -59
- package/package.json +6 -6
- package/templates/api/src/examples/api-example.ts +155 -155
- package/templates/azure-pipelines.yml +21 -21
- package/templates/codegen/codegen.ts +15 -15
- package/templates/codegen/src/uns/uns-tags.ts +1 -1
- package/templates/codegen/src/uns/uns-topics.ts +1 -1
- package/templates/config-files/config-docker.json +32 -32
- package/templates/config-files/config-localhost.json +32 -32
- package/templates/cron/AGENTS.md +20 -20
- package/templates/cron/src/examples/cron-example.ts +71 -71
- package/templates/default/.prettierignore +1 -1
- package/templates/default/.prettierrc +7 -7
- package/templates/default/AGENTS.md +20 -20
- package/templates/default/README.md +94 -94
- package/templates/default/config.json +35 -35
- package/templates/default/eslint.config.js +30 -30
- package/templates/default/gitignore +51 -51
- package/templates/default/package.json +46 -46
- package/templates/default/src/config/project.config.extension.example +23 -23
- package/templates/default/src/config/project.config.extension.ts +6 -6
- package/templates/default/src/examples/data-example.ts +95 -95
- package/templates/default/src/examples/load-test-data.ts +110 -110
- package/templates/default/src/examples/schema-system-metadata-example.ts +125 -125
- package/templates/default/src/examples/table-example.ts +107 -107
- package/templates/default/src/examples/table-window-load-test.ts +446 -446
- package/templates/default/src/examples/uns-gateway-cli.ts +10 -10
- package/templates/default/src/index.ts +41 -41
- package/templates/default/src/uns/uns-assets.ts +12 -12
- package/templates/default/src/uns/uns-dictionary.generated.ts +758 -758
- package/templates/default/src/uns/uns-measurements.generated.ts +366 -366
- package/templates/default/src/uns/uns-tags.ts +2 -2
- package/templates/default/src/uns/uns-topics.ts +2 -2
- package/templates/default/tsconfig.json +29 -29
- package/templates/python/app/README.md +8 -8
- package/templates/python/examples/README.md +134 -134
- package/templates/python/examples/api_handler.py +28 -28
- package/templates/python/examples/data_publish.py +11 -11
- package/templates/python/examples/data_subscribe.py +8 -8
- package/templates/python/examples/data_transformer.py +17 -17
- package/templates/python/examples/table_transformer.py +15 -15
- package/templates/python/gateway/cli.py +75 -75
- package/templates/python/gateway/client.py +155 -155
- package/templates/python/gateway/manager.py +97 -97
- package/templates/python/gen/__init__.py +1 -0
- package/templates/python/gen/uns_gateway_pb2.py +70 -0
- package/templates/python/gen/uns_gateway_pb2_grpc.py +312 -0
- package/templates/python/gitignore +47 -47
- package/templates/python/proto/uns-gateway.proto +105 -105
- package/templates/python/pyproject.toml +4 -4
- package/templates/python/runtime.json +4 -4
- package/templates/python/scripts/setup.sh +87 -87
- package/templates/uns-dictionary/uns-dictionary.json +650 -650
- package/templates/uns-measurements/uns-measurements.json +360 -360
- package/templates/vscode/.vscode/launch.json +164 -164
- package/templates/vscode/.vscode/settings.json +9 -9
- package/templates/vscode/.vscode/tasks.json +27 -27
- package/templates/vscode/uns-kit.code-workspace +13 -13
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example: publish schema-system metadata for object relationships and lifecycle timing.
|
|
3
|
-
*
|
|
4
|
-
* This is producer metadata. The controller stores these fields in
|
|
5
|
-
* attribute_schema.schema_json and can later materialize relationship evidence
|
|
6
|
-
* into object_id_relationship edges.
|
|
7
|
-
*/
|
|
8
|
-
import { ConfigFile, UnsProxyProcess, getLogger } from "@uns-kit/core";
|
|
9
|
-
import { registerAttributeDescriptions, registerObjectTypeDescriptions } from "@uns-kit/core/uns/uns-dictionary-registry.js";
|
|
10
|
-
import type { ISO8601 } from "@uns-kit/core/uns/uns-interfaces.js";
|
|
11
|
-
import { UnsTopics } from "@uns-kit/core/uns/uns-topics.js";
|
|
12
|
-
import {
|
|
13
|
-
GeneratedAttributeDescriptions,
|
|
14
|
-
GeneratedObjectTypeDescriptions,
|
|
15
|
-
GeneratedObjectTypes,
|
|
16
|
-
} from "../uns/uns-dictionary.generated.js";
|
|
17
|
-
import { resolveGeneratedAsset } from "../uns/uns-assets.js";
|
|
18
|
-
|
|
19
|
-
const logger = getLogger(import.meta.url);
|
|
20
|
-
|
|
21
|
-
const config = await ConfigFile.loadConfig();
|
|
22
|
-
registerObjectTypeDescriptions(GeneratedObjectTypeDescriptions);
|
|
23
|
-
registerAttributeDescriptions(GeneratedAttributeDescriptions);
|
|
24
|
-
|
|
25
|
-
const unsProxyProcess = new UnsProxyProcess(config.infra.host!, {
|
|
26
|
-
processName: config.uns.processName!,
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
const mqttOutput = await unsProxyProcess.createUnsMqttProxy(
|
|
30
|
-
config.output?.host!,
|
|
31
|
-
"templateSchemaMetadataOutput",
|
|
32
|
-
config.uns.instanceMode!,
|
|
33
|
-
config.uns.handover!,
|
|
34
|
-
{ publishThrottlingDelay: 1000 },
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
const now = new Date();
|
|
38
|
-
const time = now.toISOString() as ISO8601;
|
|
39
|
-
const topic: UnsTopics = "enterprise/site/area/line/";
|
|
40
|
-
const asset = resolveGeneratedAsset("asset");
|
|
41
|
-
const materialObjectType = GeneratedObjectTypes["material"];
|
|
42
|
-
const currentMaterialId = "1124";
|
|
43
|
-
const previousMaterialId = "112";
|
|
44
|
-
const mergedParentMaterialIds = ["1122", "1123"];
|
|
45
|
-
|
|
46
|
-
try {
|
|
47
|
-
await mqttOutput.publishMqttMessage({
|
|
48
|
-
topic,
|
|
49
|
-
asset,
|
|
50
|
-
assetDescription: "Example production line",
|
|
51
|
-
objectType: materialObjectType,
|
|
52
|
-
objectId: currentMaterialId,
|
|
53
|
-
attributes: [
|
|
54
|
-
{
|
|
55
|
-
attribute: "previous-material",
|
|
56
|
-
description: "Previous material ObjectId before this process stage.",
|
|
57
|
-
valueType: "string",
|
|
58
|
-
systemRole: "relationship-evidence",
|
|
59
|
-
relationshipEvidence: {
|
|
60
|
-
relationshipKey: "material-renumbering",
|
|
61
|
-
ownerEndpoint: "target",
|
|
62
|
-
valueEndpoint: "source",
|
|
63
|
-
sourceObjectType: materialObjectType,
|
|
64
|
-
targetObjectType: materialObjectType,
|
|
65
|
-
sourceObjectIdFrom: "value",
|
|
66
|
-
targetObjectIdFrom: "ownerObjectId",
|
|
67
|
-
observedAtFrom: "packetTimestamp",
|
|
68
|
-
defaultStatus: "suggested",
|
|
69
|
-
},
|
|
70
|
-
data: {
|
|
71
|
-
dataGroup: "material-lineage",
|
|
72
|
-
time,
|
|
73
|
-
value: previousMaterialId,
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
{
|
|
77
|
-
attribute: "previous-materials",
|
|
78
|
-
description: "Previous material ObjectIds that were joined into this material.",
|
|
79
|
-
valueType: "array<string>",
|
|
80
|
-
systemRole: "relationship-evidence",
|
|
81
|
-
relationshipEvidence: {
|
|
82
|
-
relationshipKey: "material-merge",
|
|
83
|
-
ownerEndpoint: "target",
|
|
84
|
-
valueEndpoint: "source",
|
|
85
|
-
sourceObjectType: materialObjectType,
|
|
86
|
-
targetObjectType: materialObjectType,
|
|
87
|
-
sourceObjectIdFrom: "value[]",
|
|
88
|
-
targetObjectIdFrom: "ownerObjectId",
|
|
89
|
-
observedAtFrom: "packetTimestamp",
|
|
90
|
-
defaultStatus: "suggested",
|
|
91
|
-
},
|
|
92
|
-
data: {
|
|
93
|
-
dataGroup: "material-lineage",
|
|
94
|
-
time,
|
|
95
|
-
currentMaterialObjectId: currentMaterialId,
|
|
96
|
-
operationId: "weld-1122-1123",
|
|
97
|
-
value: mergedParentMaterialIds,
|
|
98
|
-
},
|
|
99
|
-
},
|
|
100
|
-
{
|
|
101
|
-
attribute: "process-state",
|
|
102
|
-
description: "Material lifecycle state in this process stage.",
|
|
103
|
-
valueType: "string",
|
|
104
|
-
systemRole: "lifecycle-time-source",
|
|
105
|
-
lifecycle: {
|
|
106
|
-
timestampFrom: "packetTimestamp",
|
|
107
|
-
startValues: ["entered", "processing"],
|
|
108
|
-
endValues: ["done", "exited"],
|
|
109
|
-
},
|
|
110
|
-
validityMode: "lifecycle",
|
|
111
|
-
lifecycleEndValue: "done",
|
|
112
|
-
data: {
|
|
113
|
-
dataGroup: "material-lifecycle",
|
|
114
|
-
time,
|
|
115
|
-
value: "processing",
|
|
116
|
-
},
|
|
117
|
-
},
|
|
118
|
-
],
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
logger.info(`Published schema-system metadata example for material ${currentMaterialId}.`);
|
|
122
|
-
} finally {
|
|
123
|
-
await mqttOutput.stop();
|
|
124
|
-
unsProxyProcess.shutdown();
|
|
125
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Example: publish schema-system metadata for object relationships and lifecycle timing.
|
|
3
|
+
*
|
|
4
|
+
* This is producer metadata. The controller stores these fields in
|
|
5
|
+
* attribute_schema.schema_json and can later materialize relationship evidence
|
|
6
|
+
* into object_id_relationship edges.
|
|
7
|
+
*/
|
|
8
|
+
import { ConfigFile, UnsProxyProcess, getLogger } from "@uns-kit/core";
|
|
9
|
+
import { registerAttributeDescriptions, registerObjectTypeDescriptions } from "@uns-kit/core/uns/uns-dictionary-registry.js";
|
|
10
|
+
import type { ISO8601 } from "@uns-kit/core/uns/uns-interfaces.js";
|
|
11
|
+
import { UnsTopics } from "@uns-kit/core/uns/uns-topics.js";
|
|
12
|
+
import {
|
|
13
|
+
GeneratedAttributeDescriptions,
|
|
14
|
+
GeneratedObjectTypeDescriptions,
|
|
15
|
+
GeneratedObjectTypes,
|
|
16
|
+
} from "../uns/uns-dictionary.generated.js";
|
|
17
|
+
import { resolveGeneratedAsset } from "../uns/uns-assets.js";
|
|
18
|
+
|
|
19
|
+
const logger = getLogger(import.meta.url);
|
|
20
|
+
|
|
21
|
+
const config = await ConfigFile.loadConfig();
|
|
22
|
+
registerObjectTypeDescriptions(GeneratedObjectTypeDescriptions);
|
|
23
|
+
registerAttributeDescriptions(GeneratedAttributeDescriptions);
|
|
24
|
+
|
|
25
|
+
const unsProxyProcess = new UnsProxyProcess(config.infra.host!, {
|
|
26
|
+
processName: config.uns.processName!,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const mqttOutput = await unsProxyProcess.createUnsMqttProxy(
|
|
30
|
+
config.output?.host!,
|
|
31
|
+
"templateSchemaMetadataOutput",
|
|
32
|
+
config.uns.instanceMode!,
|
|
33
|
+
config.uns.handover!,
|
|
34
|
+
{ publishThrottlingDelay: 1000 },
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const now = new Date();
|
|
38
|
+
const time = now.toISOString() as ISO8601;
|
|
39
|
+
const topic: UnsTopics = "enterprise/site/area/line/";
|
|
40
|
+
const asset = resolveGeneratedAsset("asset");
|
|
41
|
+
const materialObjectType = GeneratedObjectTypes["material"];
|
|
42
|
+
const currentMaterialId = "1124";
|
|
43
|
+
const previousMaterialId = "112";
|
|
44
|
+
const mergedParentMaterialIds = ["1122", "1123"];
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
await mqttOutput.publishMqttMessage({
|
|
48
|
+
topic,
|
|
49
|
+
asset,
|
|
50
|
+
assetDescription: "Example production line",
|
|
51
|
+
objectType: materialObjectType,
|
|
52
|
+
objectId: currentMaterialId,
|
|
53
|
+
attributes: [
|
|
54
|
+
{
|
|
55
|
+
attribute: "previous-material",
|
|
56
|
+
description: "Previous material ObjectId before this process stage.",
|
|
57
|
+
valueType: "string",
|
|
58
|
+
systemRole: "relationship-evidence",
|
|
59
|
+
relationshipEvidence: {
|
|
60
|
+
relationshipKey: "material-renumbering",
|
|
61
|
+
ownerEndpoint: "target",
|
|
62
|
+
valueEndpoint: "source",
|
|
63
|
+
sourceObjectType: materialObjectType,
|
|
64
|
+
targetObjectType: materialObjectType,
|
|
65
|
+
sourceObjectIdFrom: "value",
|
|
66
|
+
targetObjectIdFrom: "ownerObjectId",
|
|
67
|
+
observedAtFrom: "packetTimestamp",
|
|
68
|
+
defaultStatus: "suggested",
|
|
69
|
+
},
|
|
70
|
+
data: {
|
|
71
|
+
dataGroup: "material-lineage",
|
|
72
|
+
time,
|
|
73
|
+
value: previousMaterialId,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
attribute: "previous-materials",
|
|
78
|
+
description: "Previous material ObjectIds that were joined into this material.",
|
|
79
|
+
valueType: "array<string>",
|
|
80
|
+
systemRole: "relationship-evidence",
|
|
81
|
+
relationshipEvidence: {
|
|
82
|
+
relationshipKey: "material-merge",
|
|
83
|
+
ownerEndpoint: "target",
|
|
84
|
+
valueEndpoint: "source",
|
|
85
|
+
sourceObjectType: materialObjectType,
|
|
86
|
+
targetObjectType: materialObjectType,
|
|
87
|
+
sourceObjectIdFrom: "value[]",
|
|
88
|
+
targetObjectIdFrom: "ownerObjectId",
|
|
89
|
+
observedAtFrom: "packetTimestamp",
|
|
90
|
+
defaultStatus: "suggested",
|
|
91
|
+
},
|
|
92
|
+
data: {
|
|
93
|
+
dataGroup: "material-lineage",
|
|
94
|
+
time,
|
|
95
|
+
currentMaterialObjectId: currentMaterialId,
|
|
96
|
+
operationId: "weld-1122-1123",
|
|
97
|
+
value: mergedParentMaterialIds,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
attribute: "process-state",
|
|
102
|
+
description: "Material lifecycle state in this process stage.",
|
|
103
|
+
valueType: "string",
|
|
104
|
+
systemRole: "lifecycle-time-source",
|
|
105
|
+
lifecycle: {
|
|
106
|
+
timestampFrom: "packetTimestamp",
|
|
107
|
+
startValues: ["entered", "processing"],
|
|
108
|
+
endValues: ["done", "exited"],
|
|
109
|
+
},
|
|
110
|
+
validityMode: "lifecycle",
|
|
111
|
+
lifecycleEndValue: "done",
|
|
112
|
+
data: {
|
|
113
|
+
dataGroup: "material-lifecycle",
|
|
114
|
+
time,
|
|
115
|
+
value: "processing",
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
logger.info(`Published schema-system metadata example for material ${currentMaterialId}.`);
|
|
122
|
+
} finally {
|
|
123
|
+
await mqttOutput.stop();
|
|
124
|
+
unsProxyProcess.shutdown();
|
|
125
|
+
}
|
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Change this file according to your specifications and rename it to index.ts
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { UnsProxyProcess, ConfigFile, getLogger } from "@uns-kit/core";
|
|
6
|
-
import { registerAttributeDescriptions, registerObjectTypeDescriptions } from "@uns-kit/core/uns/uns-dictionary-registry.js";
|
|
7
|
-
import { UnsTopics } from "@uns-kit/core/uns/uns-topics.js";
|
|
8
|
-
import {
|
|
9
|
-
GeneratedObjectTypes,
|
|
10
|
-
GeneratedAttributeDescriptions,
|
|
11
|
-
GeneratedObjectTypeDescriptions,
|
|
12
|
-
} from "../uns/uns-dictionary.generated.js";
|
|
13
|
-
import { GeneratedAssets, resolveGeneratedAsset } from "../uns/uns-assets.js";
|
|
14
|
-
import type { IUnsTableColumn, IUnsTableColumnMetadata } from "@uns-kit/core/uns/uns-interfaces.js";
|
|
15
|
-
import type { ISO8601 } from "@uns-kit/core/uns/uns-interfaces.js";
|
|
16
|
-
import { GeneratedPhysicalMeasurements } from "../uns/uns-measurements.generated.js";
|
|
17
|
-
|
|
18
|
-
const logger = getLogger(import.meta.url);
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Load the configuration from a file.
|
|
22
|
-
* On the server, this file is provided by the `uns-datahub-controller`.
|
|
23
|
-
* In the development environment, you are responsible for creating and maintaining this file and its contents.
|
|
24
|
-
*/
|
|
25
|
-
const config = await ConfigFile.loadConfig();
|
|
26
|
-
registerObjectTypeDescriptions(GeneratedObjectTypeDescriptions);
|
|
27
|
-
registerAttributeDescriptions(GeneratedAttributeDescriptions);
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Load and configure input and output brokers from config.json
|
|
31
|
-
*/
|
|
32
|
-
const unsProxyProcess = new UnsProxyProcess(config.infra.host!, {processName: config.uns.processName!});
|
|
33
|
-
const mqttInput = await unsProxyProcess.createUnsMqttProxy((config.input?.host)!, "templateUnsRttInput", config.uns.instanceMode!, config.uns.handover!, {
|
|
34
|
-
mqttSubToTopics: ["raw/#"],
|
|
35
|
-
});
|
|
36
|
-
const mqttOutput = await unsProxyProcess.createUnsMqttProxy((config.output?.host)!, "templateUnsRttOutput", config.uns.instanceMode!, config.uns.handover!, {
|
|
37
|
-
publishThrottlingDelay: 1000,
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* The input worker connects to the upstream broker and listens for incoming messages.
|
|
42
|
-
* It processes the messages and transforms them into a table-type IUnsMessage.
|
|
43
|
-
* The resulting message is published to the output broker.
|
|
44
|
-
*/
|
|
45
|
-
mqttInput.event.on("input", async (event) => {
|
|
46
|
-
try {
|
|
47
|
-
if (event.topic === "raw/data") {
|
|
48
|
-
const values = event.message.split(",");
|
|
49
|
-
const [countRaw, timestampRaw, sensorRaw] = values;
|
|
50
|
-
if (!countRaw || !timestampRaw || !sensorRaw) {
|
|
51
|
-
logger.warn(`Skipping malformed raw/data payload: ${event.message}`);
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const currentValue = Number.parseFloat(countRaw);
|
|
56
|
-
const eventDate = new Date(Number.parseInt(timestampRaw, 10));
|
|
57
|
-
const sensorValue = Number.parseFloat(sensorRaw);
|
|
58
|
-
|
|
59
|
-
const time: ISO8601 = eventDate.toISOString() as ISO8601;
|
|
60
|
-
const intervalStart: ISO8601 = new Date(eventDate.getTime() - 1000).toISOString() as ISO8601;
|
|
61
|
-
const intervalEnd: ISO8601 = eventDate.toISOString() as ISO8601;
|
|
62
|
-
const dataGroup = "sensor_table";
|
|
63
|
-
const columns: IUnsTableColumn[] = [
|
|
64
|
-
{ name: "current", type: "double", value: currentValue, uom: GeneratedPhysicalMeasurements.Ampere },
|
|
65
|
-
{ name: "voltage", type: "double", value: sensorValue },
|
|
66
|
-
{ name: "active_energy_total", type: "double", value: currentValue, uom: GeneratedPhysicalMeasurements.KiloWattHour },
|
|
67
|
-
];
|
|
68
|
-
const tableColumns: IUnsTableColumnMetadata[] = [
|
|
69
|
-
{
|
|
70
|
-
name: "active_energy_total",
|
|
71
|
-
valueType: "number",
|
|
72
|
-
presentationKind: "counter",
|
|
73
|
-
defaultAggregation: "last",
|
|
74
|
-
counterResetPolicy: "new-value",
|
|
75
|
-
},
|
|
76
|
-
];
|
|
77
|
-
const topic: UnsTopics = "enterprise/site/area/line/";
|
|
78
|
-
const asset = resolveGeneratedAsset("asset");
|
|
79
|
-
const assetDescription = ""; // customize manually
|
|
80
|
-
mqttOutput.publishMqttMessage({
|
|
81
|
-
topic,
|
|
82
|
-
asset,
|
|
83
|
-
assetDescription,
|
|
84
|
-
objectType: GeneratedObjectTypes["energy-resource"],
|
|
85
|
-
objectId: "main",
|
|
86
|
-
attributes: [
|
|
87
|
-
{
|
|
88
|
-
attribute: "measurements",
|
|
89
|
-
description: GeneratedAttributeDescriptions["status"] ?? "Table measurements with a counter column",
|
|
90
|
-
tableColumns,
|
|
91
|
-
table: {
|
|
92
|
-
dataGroup,
|
|
93
|
-
time,
|
|
94
|
-
intervalStart,
|
|
95
|
-
intervalEnd,
|
|
96
|
-
columns,
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
],
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
} catch (error) {
|
|
103
|
-
const reason = error instanceof Error ? error : new Error(String(error));
|
|
104
|
-
logger.error(`Error publishing message to MQTT: ${reason.message}`);
|
|
105
|
-
throw reason;
|
|
106
|
-
}
|
|
107
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* Change this file according to your specifications and rename it to index.ts
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { UnsProxyProcess, ConfigFile, getLogger } from "@uns-kit/core";
|
|
6
|
+
import { registerAttributeDescriptions, registerObjectTypeDescriptions } from "@uns-kit/core/uns/uns-dictionary-registry.js";
|
|
7
|
+
import { UnsTopics } from "@uns-kit/core/uns/uns-topics.js";
|
|
8
|
+
import {
|
|
9
|
+
GeneratedObjectTypes,
|
|
10
|
+
GeneratedAttributeDescriptions,
|
|
11
|
+
GeneratedObjectTypeDescriptions,
|
|
12
|
+
} from "../uns/uns-dictionary.generated.js";
|
|
13
|
+
import { GeneratedAssets, resolveGeneratedAsset } from "../uns/uns-assets.js";
|
|
14
|
+
import type { IUnsTableColumn, IUnsTableColumnMetadata } from "@uns-kit/core/uns/uns-interfaces.js";
|
|
15
|
+
import type { ISO8601 } from "@uns-kit/core/uns/uns-interfaces.js";
|
|
16
|
+
import { GeneratedPhysicalMeasurements } from "../uns/uns-measurements.generated.js";
|
|
17
|
+
|
|
18
|
+
const logger = getLogger(import.meta.url);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Load the configuration from a file.
|
|
22
|
+
* On the server, this file is provided by the `uns-datahub-controller`.
|
|
23
|
+
* In the development environment, you are responsible for creating and maintaining this file and its contents.
|
|
24
|
+
*/
|
|
25
|
+
const config = await ConfigFile.loadConfig();
|
|
26
|
+
registerObjectTypeDescriptions(GeneratedObjectTypeDescriptions);
|
|
27
|
+
registerAttributeDescriptions(GeneratedAttributeDescriptions);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Load and configure input and output brokers from config.json
|
|
31
|
+
*/
|
|
32
|
+
const unsProxyProcess = new UnsProxyProcess(config.infra.host!, {processName: config.uns.processName!});
|
|
33
|
+
const mqttInput = await unsProxyProcess.createUnsMqttProxy((config.input?.host)!, "templateUnsRttInput", config.uns.instanceMode!, config.uns.handover!, {
|
|
34
|
+
mqttSubToTopics: ["raw/#"],
|
|
35
|
+
});
|
|
36
|
+
const mqttOutput = await unsProxyProcess.createUnsMqttProxy((config.output?.host)!, "templateUnsRttOutput", config.uns.instanceMode!, config.uns.handover!, {
|
|
37
|
+
publishThrottlingDelay: 1000,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The input worker connects to the upstream broker and listens for incoming messages.
|
|
42
|
+
* It processes the messages and transforms them into a table-type IUnsMessage.
|
|
43
|
+
* The resulting message is published to the output broker.
|
|
44
|
+
*/
|
|
45
|
+
mqttInput.event.on("input", async (event) => {
|
|
46
|
+
try {
|
|
47
|
+
if (event.topic === "raw/data") {
|
|
48
|
+
const values = event.message.split(",");
|
|
49
|
+
const [countRaw, timestampRaw, sensorRaw] = values;
|
|
50
|
+
if (!countRaw || !timestampRaw || !sensorRaw) {
|
|
51
|
+
logger.warn(`Skipping malformed raw/data payload: ${event.message}`);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const currentValue = Number.parseFloat(countRaw);
|
|
56
|
+
const eventDate = new Date(Number.parseInt(timestampRaw, 10));
|
|
57
|
+
const sensorValue = Number.parseFloat(sensorRaw);
|
|
58
|
+
|
|
59
|
+
const time: ISO8601 = eventDate.toISOString() as ISO8601;
|
|
60
|
+
const intervalStart: ISO8601 = new Date(eventDate.getTime() - 1000).toISOString() as ISO8601;
|
|
61
|
+
const intervalEnd: ISO8601 = eventDate.toISOString() as ISO8601;
|
|
62
|
+
const dataGroup = "sensor_table";
|
|
63
|
+
const columns: IUnsTableColumn[] = [
|
|
64
|
+
{ name: "current", type: "double", value: currentValue, uom: GeneratedPhysicalMeasurements.Ampere },
|
|
65
|
+
{ name: "voltage", type: "double", value: sensorValue },
|
|
66
|
+
{ name: "active_energy_total", type: "double", value: currentValue, uom: GeneratedPhysicalMeasurements.KiloWattHour },
|
|
67
|
+
];
|
|
68
|
+
const tableColumns: IUnsTableColumnMetadata[] = [
|
|
69
|
+
{
|
|
70
|
+
name: "active_energy_total",
|
|
71
|
+
valueType: "number",
|
|
72
|
+
presentationKind: "counter",
|
|
73
|
+
defaultAggregation: "last",
|
|
74
|
+
counterResetPolicy: "new-value",
|
|
75
|
+
},
|
|
76
|
+
];
|
|
77
|
+
const topic: UnsTopics = "enterprise/site/area/line/";
|
|
78
|
+
const asset = resolveGeneratedAsset("asset");
|
|
79
|
+
const assetDescription = ""; // customize manually
|
|
80
|
+
mqttOutput.publishMqttMessage({
|
|
81
|
+
topic,
|
|
82
|
+
asset,
|
|
83
|
+
assetDescription,
|
|
84
|
+
objectType: GeneratedObjectTypes["energy-resource"],
|
|
85
|
+
objectId: "main",
|
|
86
|
+
attributes: [
|
|
87
|
+
{
|
|
88
|
+
attribute: "measurements",
|
|
89
|
+
description: GeneratedAttributeDescriptions["status"] ?? "Table measurements with a counter column",
|
|
90
|
+
tableColumns,
|
|
91
|
+
table: {
|
|
92
|
+
dataGroup,
|
|
93
|
+
time,
|
|
94
|
+
intervalStart,
|
|
95
|
+
intervalEnd,
|
|
96
|
+
columns,
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
} catch (error) {
|
|
103
|
+
const reason = error instanceof Error ? error : new Error(String(error));
|
|
104
|
+
logger.error(`Error publishing message to MQTT: ${reason.message}`);
|
|
105
|
+
throw reason;
|
|
106
|
+
}
|
|
107
|
+
});
|