@uns-kit/cli 0.0.36 → 0.0.37

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 (46) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +128 -128
  3. package/dist/index.js +11 -5
  4. package/package.json +8 -2
  5. package/templates/api/src/examples/api-example.ts +62 -62
  6. package/templates/azure-pipelines.yml +21 -21
  7. package/templates/codegen/codegen.ts +15 -15
  8. package/templates/codegen/src/uns/uns-tags.ts +1 -1
  9. package/templates/codegen/src/uns/uns-topics.ts +1 -1
  10. package/templates/config-files/config-docker.json +26 -26
  11. package/templates/config-files/config-localhost.json +26 -26
  12. package/templates/cron/src/examples/cron-example.ts +46 -46
  13. package/templates/default/README.md +32 -30
  14. package/templates/default/config.json +27 -27
  15. package/templates/default/gitignore +51 -51
  16. package/templates/default/package.json +38 -19
  17. package/templates/default/src/config/project.config.extension.example +23 -23
  18. package/templates/default/src/config/project.config.extension.ts +6 -6
  19. package/templates/default/src/examples/data-example.ts +68 -68
  20. package/templates/default/src/examples/load-test-data.ts +108 -108
  21. package/templates/default/src/examples/table-example.ts +66 -66
  22. package/templates/default/src/examples/uns-gateway-cli.ts +7 -7
  23. package/templates/default/src/index.ts +15 -15
  24. package/templates/default/src/uns/uns-tags.ts +2 -2
  25. package/templates/default/src/uns/uns-topics.ts +2 -2
  26. package/templates/default/tsconfig.json +29 -16
  27. package/templates/python/app/README.md +8 -8
  28. package/templates/python/examples/README.md +134 -134
  29. package/templates/python/examples/api_handler.py +28 -28
  30. package/templates/python/examples/data_publish.py +11 -11
  31. package/templates/python/examples/data_subscribe.py +8 -8
  32. package/templates/python/examples/data_transformer.py +17 -17
  33. package/templates/python/examples/table_transformer.py +15 -15
  34. package/templates/python/gateway/cli.py +75 -75
  35. package/templates/python/gateway/client.py +155 -155
  36. package/templates/python/gateway/manager.py +97 -97
  37. package/templates/python/proto/uns-gateway.proto +102 -102
  38. package/templates/python/pyproject.toml +4 -4
  39. package/templates/python/scripts/setup.sh +87 -87
  40. package/templates/temporal/src/examples/temporal-example.ts +35 -35
  41. package/templates/vscode/.vscode/launch.json +164 -164
  42. package/templates/vscode/.vscode/settings.json +9 -9
  43. package/templates/vscode/uns-kit.code-workspace +13 -13
  44. package/templates/python/gen/__init__.py +0 -1
  45. package/templates/python/gen/uns_gateway_pb2.py +0 -70
  46. package/templates/python/gen/uns_gateway_pb2_grpc.py +0 -312
@@ -1,27 +1,27 @@
1
- {
2
- "uns": {
3
- "graphql": "http://localhost:3200/graphql",
4
- "rest": "http://localhost:3200/api",
5
- "processName": "__APP_NAME__",
6
- "instanceMode": "wait",
7
- "handover": true,
8
- "jwksWellKnownUrl": "http://localhost:3200/api/.well-known/jwks.json",
9
- "kidWellKnownUrl": "http://localhost:3200/api/.well-known/kid",
10
- "email": "user@example-org.com",
11
- "password": "secret"
12
- },
13
- "infra": {
14
- "host": "localhost:1883"
15
- },
16
- "output": {
17
- "host": "localhost:1883"
18
- },
19
- "input": {
20
- "host": "localhost:1883"
21
- },
22
- "devops": {
23
- "provider": "azure-devops",
24
- "organization": "example-org",
25
- "project": "example-project"
26
- }
1
+ {
2
+ "uns": {
3
+ "graphql": "http://localhost:3200/graphql",
4
+ "rest": "http://localhost:3200/api",
5
+ "processName": "__APP_NAME__",
6
+ "instanceMode": "wait",
7
+ "handover": true,
8
+ "jwksWellKnownUrl": "http://localhost:3200/api/.well-known/jwks.json",
9
+ "kidWellKnownUrl": "http://localhost:3200/api/.well-known/kid",
10
+ "email": "user@example-org.com",
11
+ "password": "secret"
12
+ },
13
+ "infra": {
14
+ "host": "localhost:1883"
15
+ },
16
+ "output": {
17
+ "host": "localhost:1883"
18
+ },
19
+ "input": {
20
+ "host": "localhost:1883"
21
+ },
22
+ "devops": {
23
+ "provider": "azure-devops",
24
+ "organization": "example-org",
25
+ "project": "example-project"
26
+ }
27
27
  }
@@ -1,46 +1,46 @@
1
- /**
2
- * Change this file according to your specifications and rename it to index.ts
3
- */
4
- import { UnsProxyProcess, ConfigFile, logger } from "@uns-kit/core.js";
5
- import type { IUnsMessage, UnsEvents } from "@uns-kit/core.js";
6
- import "@uns-kit/cron";
7
- import { type UnsProxyProcessWithCron } from "@uns-kit/cron.js";
8
- import { UnsTags } from "../uns/uns-tags.js";
9
- import { UnsTopics } from "../uns/uns-topics.js";
10
- import { PhysicalMeasurements } from "@uns-kit/core/uns/uns-measurements.js";
11
- import { UnsPacket } from "@uns-kit/core/uns/uns-packet.js";
12
-
13
-
14
- /**
15
- * Load the configuration from a file.
16
- * On the server, this file is provided by the `uns-datahub-controller`.
17
- * In the development environment, you are responsible for creating and maintaining this file and its contents.
18
- */
19
- const config = await ConfigFile.loadConfig();
20
-
21
- /**
22
- * Connect to the output broker and create a crontab proxy
23
- */
24
- const unsProxyProcess = new UnsProxyProcess(config.infra.host!, {processName:config.uns.processName}) as UnsProxyProcessWithCron;;
25
- const mqttOutput = await unsProxyProcess.createUnsMqttProxy((config.output?.host)!, "templateUnsRttOutput", config.uns.instanceMode, config.uns.handover, { publishThrottlingDelay: 1000});
26
- const cronInput = await unsProxyProcess.createCrontabProxy("* * * * * *");
27
-
28
- /**
29
- * Event listener for cron events.
30
- * On each cron event, publish a message to the MQTT output broker.
31
- */
32
- cronInput.event.on("cronEvent", async (event: UnsEvents["cronEvent"]) => {
33
- try {
34
- const time = UnsPacket.formatToISO8601(new Date());
35
- const numberValue: number = 42;
36
- const message: IUnsMessage = { data: { time, value: numberValue, uom: PhysicalMeasurements.MiliVolt } };
37
- const topic: UnsTopics = "example/";
38
- const tags: UnsTags[] = [];
39
- const packet = await UnsPacket.unsPacketFromUnsMessage(message);
40
- mqttOutput.publishMqttMessage({ topic, attribute: "data-number", packet, description: "Number value", tags });
41
- } catch (error) {
42
- const reason = error instanceof Error ? error : new Error(String(error));
43
- logger.error(`Error publishing message to MQTT: ${reason.message}`);
44
- throw error;
45
- }
46
- });
1
+ /**
2
+ * Change this file according to your specifications and rename it to index.ts
3
+ */
4
+ import { UnsProxyProcess, ConfigFile, logger } from "@uns-kit/core";
5
+ import type { IUnsMessage, UnsEvents } from "@uns-kit/core";
6
+ import "@uns-kit/cron";
7
+ import { type UnsProxyProcessWithCron } from "@uns-kit/cron.js";
8
+ import { UnsTags } from "../uns/uns-tags.js";
9
+ import { UnsTopics } from "../uns/uns-topics.js";
10
+ import { PhysicalMeasurements } from "@uns-kit/core/uns/uns-measurements.js";
11
+ import { UnsPacket } from "@uns-kit/core/uns/uns-packet.js";
12
+
13
+
14
+ /**
15
+ * Load the configuration from a file.
16
+ * On the server, this file is provided by the `uns-datahub-controller`.
17
+ * In the development environment, you are responsible for creating and maintaining this file and its contents.
18
+ */
19
+ const config = await ConfigFile.loadConfig();
20
+
21
+ /**
22
+ * Connect to the output broker and create a crontab proxy
23
+ */
24
+ const unsProxyProcess = new UnsProxyProcess(config.infra.host!, {processName:config.uns.processName}) as UnsProxyProcessWithCron;;
25
+ const mqttOutput = await unsProxyProcess.createUnsMqttProxy((config.output?.host)!, "templateUnsRttOutput", config.uns.instanceMode, config.uns.handover, { publishThrottlingDelay: 1000});
26
+ const cronInput = await unsProxyProcess.createCrontabProxy("* * * * * *");
27
+
28
+ /**
29
+ * Event listener for cron events.
30
+ * On each cron event, publish a message to the MQTT output broker.
31
+ */
32
+ cronInput.event.on("cronEvent", async (event: UnsEvents["cronEvent"]) => {
33
+ try {
34
+ const time = UnsPacket.formatToISO8601(new Date());
35
+ const numberValue: number = 42;
36
+ const message: IUnsMessage = { data: { time, value: numberValue, uom: PhysicalMeasurements.MiliVolt } };
37
+ const topic: UnsTopics = "example/";
38
+ const tags: UnsTags[] = [];
39
+ const packet = await UnsPacket.unsPacketFromUnsMessage(message);
40
+ mqttOutput.publishMqttMessage({ topic, attribute: "data-number", packet, description: "Number value", tags });
41
+ } catch (error) {
42
+ const reason = error instanceof Error ? error : new Error(String(error));
43
+ logger.error(`Error publishing message to MQTT: ${reason.message}`);
44
+ throw error;
45
+ }
46
+ });
@@ -1,30 +1,32 @@
1
- # __APP_NAME__
2
-
3
- Generated with `@uns-kit/cli`.
4
-
5
- ## Scripts
6
-
7
- ```bash
8
- pnpm run dev # start the local development loop
9
- pnpm run build # emit dist/ output
10
- pnpm run start # run the compiled entrypoint
11
- pnpm run generate-config-schema # regenerate config.schema.json and AppConfig augmentations
12
- pnpm run codegen # regenerate typed GraphQL operations (after configure-codegen)
13
- pnpm run refresh-uns # rebuild UNS topics/tags from the live schema
14
- ```
15
-
16
- ## Configuration
17
-
18
- Update `config.json` with your broker, UNS URLs, and credentials. The generated file contains sensible defaults for local development.
19
-
20
- ## Next Steps
21
-
22
- - Install additional plugins: `pnpm add @uns-kit/api` etc.
23
- - Create MQTT proxies or Temporal workflows inside `src/index.ts`.
24
- - Extend `src/config/project.config.extension.ts` with project-specific sections and run `pnpm run generate-config-schema` (reload your editor's TS server afterward if completions lag).
25
- - Run `uns-kit configure-devops` to add the Azure DevOps pull-request tooling.
26
- - Run `uns-kit configure-vscode` to copy workspace/launch configuration for VS Code.
27
- - Run `uns-kit configure-codegen` to scaffold GraphQL code generation and UNS refresh scripts.
28
- - Run `uns-kit configure-api` / `configure-cron` / `configure-temporal` to pull in example stubs and install the matching UNS plugins.
29
- - Run `uns-kit configure-python` to copy the Python gateway client template (examples, scripts, proto).
30
- - Commit your new project and start building!
1
+ # __APP_NAME__
2
+
3
+ Generated with `@uns-kit/cli`.
4
+
5
+ Requires Node.js 22+ and pnpm (see `packageManager` in package.json).
6
+
7
+ ## Scripts
8
+
9
+ ```bash
10
+ pnpm run dev # start the local development loop
11
+ pnpm run build # emit dist/ output
12
+ pnpm run start # run the compiled entrypoint
13
+ pnpm run generate-config-schema # regenerate config.schema.json and AppConfig augmentations
14
+ pnpm run codegen # regenerate typed GraphQL operations (after configure-codegen)
15
+ pnpm run refresh-uns # rebuild UNS topics/tags from the live schema
16
+ ```
17
+
18
+ ## Configuration
19
+
20
+ Update `config.json` with your broker, UNS URLs, and credentials. The generated file contains sensible defaults for local development.
21
+
22
+ ## Next Steps
23
+
24
+ - Install additional plugins: `pnpm add @uns-kit/api` etc.
25
+ - Create MQTT proxies or Temporal workflows inside `src/index.ts`.
26
+ - Extend `src/config/project.config.extension.ts` with project-specific sections and run `pnpm run generate-config-schema` (reload your editor's TS server afterward if completions lag).
27
+ - Run `uns-kit configure-devops` to add the Azure DevOps pull-request tooling.
28
+ - Run `uns-kit configure-vscode` to copy workspace/launch configuration for VS Code.
29
+ - Run `uns-kit configure-codegen` to scaffold GraphQL code generation and UNS refresh scripts.
30
+ - Run `uns-kit configure-api` / `configure-cron` / `configure-temporal` to pull in example stubs and install the matching UNS plugins.
31
+ - Run `uns-kit configure-python` to copy the Python gateway client template (examples, scripts, proto).
32
+ - Commit your new project and start building!
@@ -1,27 +1,27 @@
1
- {
2
- "uns": {
3
- "graphql": "http://localhost:3200/graphql",
4
- "rest": "http://localhost:3200/api",
5
- "processName": "__APP_NAME__",
6
- "instanceMode": "wait",
7
- "handover": true,
8
- "jwksWellKnownUrl": "http://localhost:3200/api/.well-known/jwks.json",
9
- "kidWellKnownUrl": "http://localhost:3200/api/.well-known/kid",
10
- "email": "user@example-org.com",
11
- "password": "secret"
12
- },
13
- "infra": {
14
- "host": "localhost:1883"
15
- },
16
- "output": {
17
- "host": "localhost:1883"
18
- },
19
- "input": {
20
- "host": "localhost:1883"
21
- },
22
- "devops": {
23
- "provider": "azure-devops",
24
- "organization": "example-org",
25
- "project": "example-project"
26
- }
27
- }
1
+ {
2
+ "uns": {
3
+ "graphql": "http://localhost:3200/graphql",
4
+ "rest": "http://localhost:3200/api",
5
+ "processName": "__APP_NAME__",
6
+ "instanceMode": "wait",
7
+ "handover": true,
8
+ "jwksWellKnownUrl": "http://localhost:3200/api/.well-known/jwks.json",
9
+ "kidWellKnownUrl": "http://localhost:3200/api/.well-known/kid",
10
+ "email": "user@example-org.com",
11
+ "password": "secret"
12
+ },
13
+ "infra": {
14
+ "host": "localhost:1883"
15
+ },
16
+ "output": {
17
+ "host": "localhost:1883"
18
+ },
19
+ "input": {
20
+ "host": "localhost:1883"
21
+ },
22
+ "devops": {
23
+ "provider": "azure-devops",
24
+ "organization": "example-org",
25
+ "project": "example-project"
26
+ }
27
+ }
@@ -1,51 +1,51 @@
1
- # Dependencies
2
- node_modules/
3
- .pnpm-store/
4
- .pnpm/
5
- .pnp.cjs
6
- .pnp.loader.mjs
7
-
8
- # Build outputs
9
- build/
10
- dist/
11
- tmp/
12
- temp/
13
- *.tsbuildinfo
14
-
15
- # Logs & diagnostics
16
- logs/
17
- *.log
18
- npm-debug.log*
19
- yarn-debug.log*
20
- pnpm-debug.log*
21
- lerna-debug.log*
22
-
23
- # Test & coverage artifacts
24
- coverage/
25
- .nyc_output/
26
- .vitest/
27
-
28
- # Runtime queues & sandboxes
29
- publisherQueue/
30
- sandbox-*
31
- tmp-rtt
32
- .tmp-rtt
33
-
34
- # Environment & secrets
35
- .env
36
- .env.*
37
- !.env.example
38
- !.env.sample
39
-
40
- # UNS configuration overrides
41
- config*.json
42
-
43
- # Editor files & OS cruft
44
- .idea/
45
- .DS_Store
46
- Thumbs.db
47
- *.swp
48
- *.swo
49
-
50
- # Package managers
51
- package-lock.json
1
+ # Dependencies
2
+ node_modules/
3
+ .pnpm-store/
4
+ .pnpm/
5
+ .pnp.cjs
6
+ .pnp.loader.mjs
7
+
8
+ # Build outputs
9
+ build/
10
+ dist/
11
+ tmp/
12
+ temp/
13
+ *.tsbuildinfo
14
+
15
+ # Logs & diagnostics
16
+ logs/
17
+ *.log
18
+ npm-debug.log*
19
+ yarn-debug.log*
20
+ pnpm-debug.log*
21
+ lerna-debug.log*
22
+
23
+ # Test & coverage artifacts
24
+ coverage/
25
+ .nyc_output/
26
+ .vitest/
27
+
28
+ # Runtime queues & sandboxes
29
+ publisherQueue/
30
+ sandbox-*
31
+ tmp-rtt
32
+ .tmp-rtt
33
+
34
+ # Environment & secrets
35
+ .env
36
+ .env.*
37
+ !.env.example
38
+ !.env.sample
39
+
40
+ # UNS configuration overrides
41
+ config*.json
42
+
43
+ # Editor files & OS cruft
44
+ .idea/
45
+ .DS_Store
46
+ Thumbs.db
47
+ *.swp
48
+ *.swo
49
+
50
+ # Package managers
51
+ package-lock.json
@@ -1,19 +1,38 @@
1
- {
2
- "name": "__APP_NAME__",
3
- "version": "0.0.0",
4
- "type": "module",
5
- "scripts": {
6
- "typecheck": "tsc --noEmit",
7
- "build": "tsc -p tsconfig.json",
8
- "dev": "tsx watch src/index.ts",
9
- "generate-config-schema": "node ./node_modules/@uns-kit/core/dist/tools/generate-config-schema.js"
10
- },
11
- "dependencies": {
12
- "@uns-kit/core": "__UNS_KIT_CORE_VERSION__",
13
- "zod": "^3.23.8"
14
- },
15
- "devDependencies": {
16
- "tsx": "^4.20.5",
17
- "typescript": "^5.9.2"
18
- }
19
- }
1
+ {
2
+ "name": "__APP_NAME__",
3
+ "version": "0.0.0",
4
+ "private": true,
5
+ "description": "UNS application generated by @uns-kit/cli",
6
+ "type": "module",
7
+ "packageManager": "pnpm@9.12.1",
8
+ "engines": {
9
+ "node": ">=22"
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ }
19
+ },
20
+ "scripts": {
21
+ "clean": "rimraf dist",
22
+ "typecheck": "tsc --noEmit",
23
+ "build": "pnpm run clean && tsc -p tsconfig.json",
24
+ "dev": "tsx watch src/index.ts",
25
+ "start": "node dist/index.js",
26
+ "generate-config-schema": "node ./node_modules/@uns-kit/core/dist/tools/generate-config-schema.js"
27
+ },
28
+ "dependencies": {
29
+ "@uns-kit/core": "__UNS_KIT_CORE_VERSION__",
30
+ "zod": "^3.23.8"
31
+ },
32
+ "devDependencies": {
33
+ "@types/node": "^22.19.1",
34
+ "rimraf": "^6.1.2",
35
+ "tsx": "^4.20.6",
36
+ "typescript": "^5.9.3"
37
+ }
38
+ }
@@ -1,23 +1,23 @@
1
- import { z } from "zod";
2
- import { secretValueSchema } from "@uns-kit/core/uns-config/secret-placeholders.js";
3
- import { hostValueSchema } from "@uns-kit/core/uns-config/host-placeholders.js";
4
-
5
- // Extend this schema with project-specific configuration sections.
6
- export const projectExtrasSchema = z.object({
7
- pg: z.object({
8
- user: z.string().min(1, "pg.user is required"),
9
- host: hostValueSchema,
10
- port: z.number().int().positive().default(5432),
11
- ssl: z.boolean().default(false),
12
- database: z.string().min(1, "pg.database is required"),
13
- isPoolConnection: z.boolean().default(false),
14
- password: secretValueSchema.optional(),
15
- }),
16
-
17
- caddy: z.object({
18
- adminUrl: z.string().url(),
19
- proxyHost: z.string().url(),
20
- }),
21
- });
22
-
23
- export type ProjectExtras = z.infer<typeof projectExtrasSchema>;
1
+ import { z } from "zod";
2
+ import { secretValueSchema } from "@uns-kit/core/uns-config/secret-placeholders.js";
3
+ import { hostValueSchema } from "@uns-kit/core/uns-config/host-placeholders.js";
4
+
5
+ // Extend this schema with project-specific configuration sections.
6
+ export const projectExtrasSchema = z.object({
7
+ pg: z.object({
8
+ user: z.string().min(1, "pg.user is required"),
9
+ host: hostValueSchema,
10
+ port: z.number().int().positive().default(5432),
11
+ ssl: z.boolean().default(false),
12
+ database: z.string().min(1, "pg.database is required"),
13
+ isPoolConnection: z.boolean().default(false),
14
+ password: secretValueSchema.optional(),
15
+ }),
16
+
17
+ caddy: z.object({
18
+ adminUrl: z.string().url(),
19
+ proxyHost: z.string().url(),
20
+ }),
21
+ });
22
+
23
+ export type ProjectExtras = z.infer<typeof projectExtrasSchema>;
@@ -1,6 +1,6 @@
1
- import { z } from "zod";
2
-
3
- // Extend this schema with project-specific configuration sections.
4
- export const projectExtrasSchema = z.object({});
5
-
6
- export type ProjectExtras = z.infer<typeof projectExtrasSchema>;
1
+ import { z } from "zod";
2
+
3
+ // Extend this schema with project-specific configuration sections.
4
+ export const projectExtrasSchema = z.object({});
5
+
6
+ export type ProjectExtras = z.infer<typeof projectExtrasSchema>;
@@ -1,68 +1,68 @@
1
- /**
2
- * Change this file according to your specifications and rename it to index.ts
3
- */
4
- import { UnsProxyProcess, ConfigFile, logger, type IUnsMessage } from "@uns-kit/core.js";
5
- import { DataSizeMeasurements, PhysicalMeasurements } from "@uns-kit/core/uns/uns-measurements.js";
6
- import { UnsPacket } from "@uns-kit/core/uns/uns-packet.js";
7
- import { UnsTags } from "@uns-kit/core/uns/uns-tags.js";
8
- import { UnsTopics } from "@uns-kit/core/uns/uns-topics.js";
9
-
10
- /**
11
- * Load the configuration from a file.
12
- * On the server, this file is provided by the `uns-datahub-controller`.
13
- * In the development environment, you are responsible for creating and maintaining this file and its contents.
14
- */
15
- const config = await ConfigFile.loadConfig();
16
-
17
- /**
18
- * Connect to input and output brokers
19
- */
20
- const unsProxyProcess = new UnsProxyProcess(config.infra.host!, {processName: config.uns.processName!});
21
- const mqttInput = await unsProxyProcess.createUnsMqttProxy((config.input?.host)!, "templateUnsRttInput", config.uns.instanceMode!, config.uns.handover!, {
22
- mqttSubToTopics: ["raw/#"],
23
- });
24
- const mqttOutput = await unsProxyProcess.createUnsMqttProxy((config.output?.host)!, "templateUnsRttOutput", config.uns.instanceMode!, config.uns.handover!, { publishThrottlingDelay: 1000});
25
-
26
-
27
- /**
28
- * Event listener for input events.
29
- * Transform an input message and publish it with publishMqttMessage function.
30
- */
31
- mqttInput.event.on("input", async (event) => {
32
- try {
33
- if (event.topic === "raw/data") {
34
- const values = event.message.split(",");
35
- const [countRaw, timestampRaw, sensorRaw] = values;
36
- const numberValue = Number.parseFloat(countRaw);
37
- const eventDate = new Date(Number.parseInt(timestampRaw, 10));
38
- const sensorValue = Number.parseFloat(sensorRaw);
39
- const time = UnsPacket.formatToISO8601(eventDate);
40
-
41
- const dataGroup = "sensor";
42
-
43
- const message: IUnsMessage = {
44
- data: { dataGroup, time, value: numberValue, uom: PhysicalMeasurements.None },
45
- };
46
- const topic: UnsTopics = "example/";
47
- const tags: UnsTags[] = [];
48
- const packet = await UnsPacket.unsPacketFromUnsMessage(message);
49
- mqttOutput.publishMqttMessage({ topic, attribute: "data-count", packet, description: "Counter", tags });
50
-
51
- const sensorMessage: IUnsMessage = {
52
- data: { dataGroup, time, value: sensorValue, uom: PhysicalMeasurements.Celsius },
53
- };
54
- const sensorPacket = await UnsPacket.unsPacketFromUnsMessage(sensorMessage);
55
- mqttOutput.publishMqttMessage({
56
- topic,
57
- attribute: "data-sensor",
58
- packet: sensorPacket,
59
- description: "Simulated sensor value",
60
- tags,
61
- });
62
- }
63
- } catch (error) {
64
- const reason = error instanceof Error ? error : new Error(String(error));
65
- logger.error(`Error publishing message to MQTT: ${reason.message}`);
66
- throw reason;
67
- }
68
- });
1
+ /**
2
+ * Change this file according to your specifications and rename it to index.ts
3
+ */
4
+ import { UnsProxyProcess, ConfigFile, logger, type IUnsMessage } from "@uns-kit/core";
5
+ import { DataSizeMeasurements, PhysicalMeasurements } from "@uns-kit/core/uns/uns-measurements.js";
6
+ import { UnsPacket } from "@uns-kit/core/uns/uns-packet.js";
7
+ import { UnsTags } from "@uns-kit/core/uns/uns-tags.js";
8
+ import { UnsTopics } from "@uns-kit/core/uns/uns-topics.js";
9
+
10
+ /**
11
+ * Load the configuration from a file.
12
+ * On the server, this file is provided by the `uns-datahub-controller`.
13
+ * In the development environment, you are responsible for creating and maintaining this file and its contents.
14
+ */
15
+ const config = await ConfigFile.loadConfig();
16
+
17
+ /**
18
+ * Connect to input and output brokers
19
+ */
20
+ const unsProxyProcess = new UnsProxyProcess(config.infra.host!, {processName: config.uns.processName!});
21
+ const mqttInput = await unsProxyProcess.createUnsMqttProxy((config.input?.host)!, "templateUnsRttInput", config.uns.instanceMode!, config.uns.handover!, {
22
+ mqttSubToTopics: ["raw/#"],
23
+ });
24
+ const mqttOutput = await unsProxyProcess.createUnsMqttProxy((config.output?.host)!, "templateUnsRttOutput", config.uns.instanceMode!, config.uns.handover!, { publishThrottlingDelay: 1000});
25
+
26
+
27
+ /**
28
+ * Event listener for input events.
29
+ * Transform an input message and publish it with publishMqttMessage function.
30
+ */
31
+ mqttInput.event.on("input", async (event) => {
32
+ try {
33
+ if (event.topic === "raw/data") {
34
+ const values = event.message.split(",");
35
+ const [countRaw, timestampRaw, sensorRaw] = values;
36
+ const numberValue = Number.parseFloat(countRaw);
37
+ const eventDate = new Date(Number.parseInt(timestampRaw, 10));
38
+ const sensorValue = Number.parseFloat(sensorRaw);
39
+ const time = UnsPacket.formatToISO8601(eventDate);
40
+
41
+ const dataGroup = "sensor";
42
+
43
+ const message: IUnsMessage = {
44
+ data: { dataGroup, time, value: numberValue, uom: PhysicalMeasurements.None },
45
+ };
46
+ const topic: UnsTopics = "example/";
47
+ const tags: UnsTags[] = [];
48
+ const packet = await UnsPacket.unsPacketFromUnsMessage(message);
49
+ mqttOutput.publishMqttMessage({ topic, attribute: "data-count", packet, description: "Counter", tags });
50
+
51
+ const sensorMessage: IUnsMessage = {
52
+ data: { dataGroup, time, value: sensorValue, uom: PhysicalMeasurements.Celsius },
53
+ };
54
+ const sensorPacket = await UnsPacket.unsPacketFromUnsMessage(sensorMessage);
55
+ mqttOutput.publishMqttMessage({
56
+ topic,
57
+ attribute: "data-sensor",
58
+ packet: sensorPacket,
59
+ description: "Simulated sensor value",
60
+ tags,
61
+ });
62
+ }
63
+ } catch (error) {
64
+ const reason = error instanceof Error ? error : new Error(String(error));
65
+ logger.error(`Error publishing message to MQTT: ${reason.message}`);
66
+ throw reason;
67
+ }
68
+ });