@uns-kit/cron 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Aljoša Vister
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # @uns-kit/cron
2
+
3
+ `@uns-kit/cron` adds cron-style scheduling to the UNS runtime. It registers a `createCrontabProxy` method on `UnsProxyProcess`, emitting `cronEvent` notifications that can be bridged to MQTT topics or any downstream integration.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @uns-kit/cron
9
+ # or
10
+ npm install @uns-kit/cron
11
+ ```
12
+
13
+ You will also need `@uns-kit/core` in your project because the plugin augments `UnsProxyProcess`.
14
+
15
+ ## Quick Start
16
+
17
+ ```ts
18
+ import UnsProxyProcess from "@uns-kit/core/uns/uns-proxy-process";
19
+ import unsCronPlugin, { type UnsProxyProcessWithCron } from "@uns-kit/cron";
20
+
21
+ const process = new UnsProxyProcess("mqtt-broker:1883", { processName: "cron-demo" }) as UnsProxyProcessWithCron;
22
+ unsCronPlugin;
23
+
24
+ const cronProxy = await process.createCrontabProxy("* * * * * *");
25
+ cronProxy.event.on("cronEvent", () => {
26
+ console.log("tick");
27
+ });
28
+ ```
29
+
30
+ ## Scripts
31
+
32
+ ```bash
33
+ pnpm run typecheck
34
+ pnpm run build
35
+ ```
36
+
37
+ `build` emits ESM JavaScript and type declarations into `dist/`.
38
+
39
+ ## License
40
+
41
+ MIT © Aljoša Vister
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Change this file according to your specifications and rename it to index.ts
3
+ */
4
+ import UnsProxyProcess from "@uns-kit/core/uns/uns-proxy-process";
5
+ import { ConfigFile } from "@uns-kit/core/config-file";
6
+ import logger from "@uns-kit/core/logger";
7
+ import { PhysicalMeasurements } from "@uns-kit/core/uns/uns-measurements";
8
+ import { UnsPacket } from "@uns-kit/core/uns/uns-packet";
9
+ import unsCronPlugin from "./uns-cron-plugin";
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
+ * Connect to the output broker and create a crontab proxy
18
+ */
19
+ const unsProxyProcess = new UnsProxyProcess(config.infra.host, { processName: config.uns.processName });
20
+ unsCronPlugin;
21
+ const mqttOutput = await unsProxyProcess.createUnsMqttProxy(config.output.host, "templateUnsRttOutput", config.uns.instanceMode, config.uns.handover, { publishThrottlingDelay: 1000 });
22
+ const cronInput = await unsProxyProcess.createCrontabProxy("* * * * * *");
23
+ /**
24
+ * Event listener for cron events.
25
+ * On each cron event, publish a message to the MQTT output broker.
26
+ */
27
+ cronInput.event.on("cronEvent", async (event) => {
28
+ try {
29
+ const time = UnsPacket.formatToISO8601(new Date());
30
+ const numberValue = 42;
31
+ const message = { data: { time, value: numberValue, uom: PhysicalMeasurements.MiliVolt } };
32
+ const topic = "sij/";
33
+ const tags = [];
34
+ const packet = await UnsPacket.unsPacketFromUnsMessage(message);
35
+ mqttOutput.publishMqttMessage({ topic, attribute: "data-number", packet, description: "Number value", tags });
36
+ }
37
+ catch (error) {
38
+ logger.error(`Error publishing message to MQTT: ${error.message}`);
39
+ throw error;
40
+ }
41
+ });
@@ -0,0 +1,3 @@
1
+ export { default } from "./uns-cron-plugin.js";
2
+ export { UnsCronProxy, type UnsProxyProcessWithCron } from "./uns-cron-plugin.js";
3
+ export { default as UnsCronProxyClass } from "./uns-cron-proxy.js";
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { default } from "./uns-cron-plugin.js";
2
+ export { UnsCronProxy } from "./uns-cron-plugin.js";
3
+ export { default as UnsCronProxyClass } from "./uns-cron-proxy.js";
@@ -0,0 +1,9 @@
1
+ import type { TaskOptions } from "node-cron";
2
+ import UnsProxyProcess, { type UnsProxyProcessPlugin } from "@uns-kit/core/uns/uns-proxy-process";
3
+ import UnsCronProxy from "./uns-cron-proxy.js";
4
+ declare const unsCronPlugin: UnsProxyProcessPlugin;
5
+ export default unsCronPlugin;
6
+ export { UnsCronProxy };
7
+ export type UnsProxyProcessWithCron = UnsProxyProcess & {
8
+ createCrontabProxy(cronExpression: string, options?: TaskOptions): Promise<UnsCronProxy>;
9
+ };
@@ -0,0 +1,24 @@
1
+ import UnsProxyProcess from "@uns-kit/core/uns/uns-proxy-process";
2
+ import UnsCronProxy from "./uns-cron-proxy.js";
3
+ const cronProxyRegistry = new WeakMap();
4
+ const getCronProxies = (instance) => {
5
+ let proxies = cronProxyRegistry.get(instance);
6
+ if (!proxies) {
7
+ proxies = [];
8
+ cronProxyRegistry.set(instance, proxies);
9
+ }
10
+ return proxies;
11
+ };
12
+ const unsCronPlugin = ({ define }) => {
13
+ define({
14
+ async createCrontabProxy(cronExpression, options) {
15
+ await this.waitForProcessConnection();
16
+ const unsCronProxy = new UnsCronProxy(cronExpression, options);
17
+ getCronProxies(this).push(unsCronProxy);
18
+ return unsCronProxy;
19
+ },
20
+ });
21
+ };
22
+ UnsProxyProcess.use(unsCronPlugin);
23
+ export default unsCronPlugin;
24
+ export { UnsCronProxy };
@@ -0,0 +1,8 @@
1
+ import { TaskOptions } from "node-cron";
2
+ import UnsProxy from "@uns-kit/core/uns/uns-proxy";
3
+ export default class UnsCronProxy extends UnsProxy {
4
+ private readonly cronExpression;
5
+ private readonly task;
6
+ constructor(cronExpression: string, options?: TaskOptions);
7
+ stop(): Promise<void>;
8
+ }
@@ -0,0 +1,17 @@
1
+ import cron from "node-cron";
2
+ import UnsProxy from "@uns-kit/core/uns/uns-proxy";
3
+ export default class UnsCronProxy extends UnsProxy {
4
+ cronExpression;
5
+ task;
6
+ constructor(cronExpression, options) {
7
+ super();
8
+ this.cronExpression = cronExpression;
9
+ this.task = cron.schedule(this.cronExpression, () => {
10
+ this.event.emit("cronEvent", {});
11
+ }, options);
12
+ }
13
+ async stop() {
14
+ await super.stop();
15
+ this.task.stop();
16
+ }
17
+ }
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@uns-kit/cron",
3
+ "version": "0.0.1",
4
+ "description": "Cron-driven plugin for UnsProxyProcess that emits UNS events on a schedule.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "Aljoša Vister <aljosa.vister@gmail.com>",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/uns-datahub/uns-kit.git",
11
+ "directory": "packages/uns-cron"
12
+ },
13
+ "keywords": [
14
+ "uns",
15
+ "cron",
16
+ "scheduler",
17
+ "mqtt",
18
+ "node-cron",
19
+ "typescript"
20
+ ],
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "exports": {
25
+ "./*": "./dist/*"
26
+ },
27
+ "main": "dist/index.js",
28
+ "types": "dist/index.d.ts",
29
+ "dependencies": {
30
+ "@uns-kit/core": "^0.0.1",
31
+ "node-cron": "^4.2.1"
32
+ },
33
+ "scripts": {
34
+ "build": "tsc -p tsconfig.build.json",
35
+ "typecheck": "tsc -p tsconfig.json --noEmit"
36
+ }
37
+ }