@machhub-dev/sdk-ts 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 +21 -0
- package/dist/cjs/classes/auth.d.ts +18 -0
- package/dist/cjs/classes/auth.js +90 -0
- package/dist/cjs/classes/collection.d.ts +18 -0
- package/dist/cjs/classes/collection.js +43 -0
- package/dist/cjs/classes/flow.d.ts +6 -0
- package/dist/cjs/classes/flow.js +12 -0
- package/dist/cjs/classes/function.d.ts +10 -0
- package/dist/cjs/classes/function.js +29 -0
- package/dist/cjs/classes/historian.d.ts +12 -0
- package/dist/cjs/classes/historian.js +40 -0
- package/dist/cjs/classes/tag.d.ts +10 -0
- package/dist/cjs/classes/tag.js +25 -0
- package/dist/cjs/client.d.ts +13 -0
- package/dist/cjs/client.js +35 -0
- package/dist/cjs/config.d.ts +21 -0
- package/dist/cjs/config.js +37 -0
- package/dist/cjs/example/functions-file-config.d.ts +0 -0
- package/dist/cjs/example/functions-file-config.js +1 -0
- package/dist/cjs/example/functions.d.ts +1 -0
- package/dist/cjs/example/functions.js +18 -0
- package/dist/cjs/http-client.d.ts +10 -0
- package/dist/cjs/http-client.js +62 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +7 -0
- package/dist/cjs/sdk-ts-clean.d.ts +108 -0
- package/dist/cjs/sdk-ts-clean.js +294 -0
- package/dist/cjs/sdk-ts.d.ts +71 -0
- package/dist/cjs/sdk-ts.js +227 -0
- package/dist/cjs/services/http.service.d.ts +41 -0
- package/dist/cjs/services/http.service.js +178 -0
- package/dist/cjs/services/mqtt.service.d.ts +15 -0
- package/dist/cjs/services/mqtt.service.js +103 -0
- package/dist/cjs/services/nats.service.d.ts +78 -0
- package/dist/cjs/services/nats.service.js +237 -0
- package/dist/cjs/types/auth.models.d.ts +52 -0
- package/dist/cjs/types/auth.models.js +100 -0
- package/dist/cjs/types/recordID.models.d.ts +7 -0
- package/dist/cjs/types/recordID.models.js +32 -0
- package/dist/cjs/types/response.models.d.ts +4 -0
- package/dist/cjs/types/response.models.js +2 -0
- package/dist/cjs/types/tag.models.d.ts +4 -0
- package/dist/cjs/types/tag.models.js +2 -0
- package/dist/cjs/utils/appConfig.d.ts +5 -0
- package/dist/cjs/utils/appConfig.js +62 -0
- package/dist/cjs/websocket-client.d.ts +15 -0
- package/dist/cjs/websocket-client.js +96 -0
- package/dist/classes/auth.d.ts +18 -0
- package/dist/classes/auth.js +86 -0
- package/dist/classes/collection.d.ts +18 -0
- package/dist/classes/collection.js +39 -0
- package/dist/classes/flow.d.ts +6 -0
- package/dist/classes/flow.js +8 -0
- package/dist/classes/function.d.ts +10 -0
- package/dist/classes/function.js +25 -0
- package/dist/classes/historian.d.ts +12 -0
- package/dist/classes/historian.js +36 -0
- package/dist/classes/tag.d.ts +10 -0
- package/dist/classes/tag.js +21 -0
- package/dist/client.d.ts +13 -0
- package/dist/client.js +31 -0
- package/dist/config.d.ts +21 -0
- package/dist/config.js +33 -0
- package/dist/example/functions-file-config.d.ts +0 -0
- package/dist/example/functions-file-config.js +1 -0
- package/dist/example/functions.d.ts +1 -0
- package/dist/example/functions.js +16 -0
- package/dist/http-client.d.ts +10 -0
- package/dist/http-client.js +58 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +2 -0
- package/dist/sdk-ts-clean.d.ts +108 -0
- package/dist/sdk-ts-clean.js +290 -0
- package/dist/sdk-ts.d.ts +71 -0
- package/dist/sdk-ts.js +223 -0
- package/dist/services/http.service.d.ts +41 -0
- package/dist/services/http.service.js +173 -0
- package/dist/services/mqtt.service.d.ts +15 -0
- package/dist/services/mqtt.service.js +96 -0
- package/dist/services/nats.service.d.ts +78 -0
- package/dist/services/nats.service.js +233 -0
- package/dist/types/auth.models.d.ts +52 -0
- package/dist/types/auth.models.js +97 -0
- package/dist/types/recordID.models.d.ts +7 -0
- package/dist/types/recordID.models.js +27 -0
- package/dist/types/response.models.d.ts +4 -0
- package/dist/types/response.models.js +1 -0
- package/dist/types/tag.models.d.ts +4 -0
- package/dist/types/tag.models.js +1 -0
- package/dist/utils/appConfig.d.ts +5 -0
- package/dist/utils/appConfig.js +26 -0
- package/dist/websocket-client.d.ts +15 -0
- package/dist/websocket-client.js +92 -0
- package/package.json +33 -0
- package/src/classes/auth.ts +103 -0
- package/src/classes/collection.ts +55 -0
- package/src/classes/flow.ts +13 -0
- package/src/classes/function.ts +34 -0
- package/src/classes/historian.ts +49 -0
- package/src/classes/tag.ts +30 -0
- package/src/example/functions.ts +21 -0
- package/src/index.ts +8 -0
- package/src/sdk-ts.ts +255 -0
- package/src/services/http.service.ts +239 -0
- package/src/services/mqtt.service.ts +114 -0
- package/src/services/nats.service.ts +262 -0
- package/src/types/auth.models.ts +157 -0
- package/src/types/recordID.models.ts +33 -0
- package/src/types/response.models.ts +4 -0
- package/src/types/tag.models.ts +4 -0
- package/src/utils/appConfig.ts +30 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Collection } from "./classes/collection";
|
|
2
|
+
import { Historian } from "./classes/historian";
|
|
3
|
+
import { Tag } from "./classes/tag";
|
|
4
|
+
import { Function } from "./classes/function";
|
|
5
|
+
import { Flow } from "./classes/flow";
|
|
6
|
+
import { Auth } from "./classes/auth";
|
|
7
|
+
export interface SDKConfig {
|
|
8
|
+
application_id: string;
|
|
9
|
+
httpUrl?: string;
|
|
10
|
+
mqttUrl?: string;
|
|
11
|
+
natsUrl?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare class SDK {
|
|
14
|
+
private http;
|
|
15
|
+
private mqtt;
|
|
16
|
+
private nats;
|
|
17
|
+
private _historian;
|
|
18
|
+
private _tag;
|
|
19
|
+
private _function;
|
|
20
|
+
private _flow;
|
|
21
|
+
private _auth;
|
|
22
|
+
/**
|
|
23
|
+
* Initializes the SDK with the required clients.
|
|
24
|
+
*
|
|
25
|
+
* You can initialize the SDK in the following ways:
|
|
26
|
+
*
|
|
27
|
+
* **1. Environment Variables (Recommended - works in both Node.js and Browser):**
|
|
28
|
+
* ```bash
|
|
29
|
+
* # For Vite/SPA projects (prefix with VITE_):
|
|
30
|
+
* VITE_MACHHUB_APPLICATION_ID=your-app-id
|
|
31
|
+
* VITE_MACHHUB_HTTP_URL=http://localhost:80
|
|
32
|
+
* VITE_MACHHUB_MQTT_URL=ws://localhost:180
|
|
33
|
+
* VITE_MACHHUB_NATS_URL=ws://localhost:7500
|
|
34
|
+
*
|
|
35
|
+
* # For Node.js projects:
|
|
36
|
+
* MACHHUB_APPLICATION_ID=your-app-id
|
|
37
|
+
* MACHHUB_HTTP_URL=http://localhost:80
|
|
38
|
+
* MACHHUB_MQTT_URL=ws://localhost:180
|
|
39
|
+
* MACHHUB_NATS_URL=ws://localhost:7500
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* ```typescript
|
|
43
|
+
* import { SDK } from '@machhub-dev/sdk-ts';
|
|
44
|
+
*
|
|
45
|
+
* const sdk = new SDK();
|
|
46
|
+
* await sdk.Initialize(); // Automatically reads from environment variables
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* **2. Programmatic Configuration:**
|
|
50
|
+
* ```typescript
|
|
51
|
+
* import { SDK, type SDKConfig } from '@machhub-dev/sdk-ts';
|
|
52
|
+
*
|
|
53
|
+
* const config: SDKConfig = {
|
|
54
|
+
* application_id: 'your-app-id',
|
|
55
|
+
* httpUrl: 'http://localhost:80', // optional
|
|
56
|
+
* mqttUrl: 'ws://localhost:180', // optional
|
|
57
|
+
* natsUrl: 'ws://localhost:7500', // optional
|
|
58
|
+
* };
|
|
59
|
+
*
|
|
60
|
+
* const sdk = new SDK();
|
|
61
|
+
* await sdk.Initialize(config);
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* **3. machhub.sdk.env File (Node.js only):**
|
|
65
|
+
* ```typescript
|
|
66
|
+
* // Create machhub.sdk.env in your project root:
|
|
67
|
+
* // MACHHUB_APPLICATION_ID=your-app-id
|
|
68
|
+
* // MACHHUB_HTTP_URL=http://localhost:80
|
|
69
|
+
* // MACHHUB_MQTT_URL=ws://localhost:180
|
|
70
|
+
* // MACHHUB_NATS_URL=ws://localhost:7500
|
|
71
|
+
*
|
|
72
|
+
* const sdk = new SDK();
|
|
73
|
+
* await sdk.Initialize(); // Reads from machhub.sdk.env if no env vars
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @param config {SDKConfig} Optional configuration object. If not provided, will try environment variables first, then machhub.sdk.env file.
|
|
77
|
+
* @param envFilePath {string} Optional path to env file. Only works in Node.js environments. Defaults to 'machhub.sdk.env' in project root.
|
|
78
|
+
* @returns {Promise<boolean>} Resolves to true if initialization is successful.
|
|
79
|
+
*/
|
|
80
|
+
Initialize(config?: SDKConfig | null, envFilePath?: string): Promise<boolean>;
|
|
81
|
+
/**
|
|
82
|
+
* Getter for `auth`. Ensures `auth` is accessed only after initialization.
|
|
83
|
+
*/
|
|
84
|
+
get auth(): Auth;
|
|
85
|
+
/**
|
|
86
|
+
* Getter for `historian`. Ensures `historian` is accessed only after initialization.
|
|
87
|
+
*/
|
|
88
|
+
get historian(): Historian;
|
|
89
|
+
/**
|
|
90
|
+
* Getter for `tag`. Ensures `tag` is accessed only after initialization.
|
|
91
|
+
*/
|
|
92
|
+
get tag(): Tag;
|
|
93
|
+
/**
|
|
94
|
+
* Getter for `function`. Ensures `function` is accessed only after initialization.
|
|
95
|
+
*/
|
|
96
|
+
get function(): Function;
|
|
97
|
+
/**
|
|
98
|
+
* Getter for `flow`. Ensures `flow` is accessed only after initialization.
|
|
99
|
+
*/
|
|
100
|
+
get flow(): Flow;
|
|
101
|
+
/**
|
|
102
|
+
* Creates a collection instance to interact with the specified table/collection.
|
|
103
|
+
* Throws an error if the SDK is not initialized.
|
|
104
|
+
* @param collectionName {string} The collection/table name.
|
|
105
|
+
* @returns {Collection} An instance of Collection.
|
|
106
|
+
*/
|
|
107
|
+
collection(collectionName: string): Collection;
|
|
108
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SDK = void 0;
|
|
4
|
+
const http_service_1 = require("./services/http.service");
|
|
5
|
+
const mqtt_service_1 = require("./services/mqtt.service");
|
|
6
|
+
const nats_service_1 = require("./services/nats.service");
|
|
7
|
+
const appConfig_1 = require("./utils/appConfig");
|
|
8
|
+
const collection_1 = require("./classes/collection");
|
|
9
|
+
const historian_1 = require("./classes/historian");
|
|
10
|
+
const tag_1 = require("./classes/tag");
|
|
11
|
+
const function_1 = require("./classes/function");
|
|
12
|
+
const flow_1 = require("./classes/flow");
|
|
13
|
+
const auth_1 = require("./classes/auth");
|
|
14
|
+
const MACHHUB_SDK_PATH = "machhub";
|
|
15
|
+
// Core HTTP client class
|
|
16
|
+
class HTTPClient {
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new HTTPClient instance
|
|
19
|
+
* @param applicationID The ID for your application (required)
|
|
20
|
+
* @param httpUrl The base URL for HTTP connection (default = http://localhost:80)
|
|
21
|
+
*/
|
|
22
|
+
constructor(applicationID, httpUrl = "http://localhost:80") {
|
|
23
|
+
if (!applicationID) {
|
|
24
|
+
throw new Error("Application ID is required.");
|
|
25
|
+
}
|
|
26
|
+
this.httpService = new http_service_1.HTTPService(httpUrl, MACHHUB_SDK_PATH, applicationID);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Gets server info
|
|
30
|
+
*/
|
|
31
|
+
async getInfo() {
|
|
32
|
+
return this.httpService.request.get("info");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// Core MQTT client class
|
|
36
|
+
class MQTTClient {
|
|
37
|
+
constructor(mqttService) {
|
|
38
|
+
this.mqttService = mqttService;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new MQTTClient instance
|
|
42
|
+
* @param applicationID The ID for your application
|
|
43
|
+
* @param mqttUrl The base URL for MQTT connection (default = ws://localhost:180)
|
|
44
|
+
*/
|
|
45
|
+
static async getInstance(applicationID, mqttUrl = "ws://localhost:180") {
|
|
46
|
+
if (!this.instance) {
|
|
47
|
+
const mqttService = await mqtt_service_1.MQTTService.getInstance(mqttUrl);
|
|
48
|
+
this.instance = new MQTTClient(mqttService);
|
|
49
|
+
}
|
|
50
|
+
return this.instance;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Subscribes to live tag data updates
|
|
54
|
+
* @param topic The tag topic
|
|
55
|
+
* @param callback The callback function for data updates
|
|
56
|
+
*/
|
|
57
|
+
async subscribeLiveData(topic, callback) {
|
|
58
|
+
return this.mqttService.addTopicHandler(topic, callback);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Publishes a message to a specific topic
|
|
62
|
+
* @param topic The topic to publish to
|
|
63
|
+
* @param data The data to publish
|
|
64
|
+
*/
|
|
65
|
+
async publish(topic, data) {
|
|
66
|
+
return this.mqttService.publish(topic, data);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// Core NATS client class
|
|
70
|
+
class NATSClient {
|
|
71
|
+
constructor(natsService) {
|
|
72
|
+
this.natsService = natsService;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Creates a new NATSClient instance
|
|
76
|
+
* @param applicationID The ID for your application
|
|
77
|
+
* @param natsUrl The base URL for NATS connection (default = ws://localhost:7500)
|
|
78
|
+
*/
|
|
79
|
+
static async getInstance(applicationID, natsUrl = "ws://localhost:7500") {
|
|
80
|
+
if (!this.instance) {
|
|
81
|
+
const natsService = await nats_service_1.NATSService.getInstance(natsUrl);
|
|
82
|
+
this.instance = new NATSClient(natsService);
|
|
83
|
+
}
|
|
84
|
+
return this.instance;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Subscribes to subject updates
|
|
88
|
+
* @param subject The subject to subscribe to
|
|
89
|
+
* @param callback The callback function for data updates
|
|
90
|
+
*/
|
|
91
|
+
async subscribe(subject, callback) {
|
|
92
|
+
return this.natsService.addSubjectHandler(subject, callback);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Publishes a message to a specific subject
|
|
96
|
+
* @param subject The subject to publish to
|
|
97
|
+
* @param data The data to publish
|
|
98
|
+
*/
|
|
99
|
+
async publish(subject, data) {
|
|
100
|
+
return this.natsService.publish(subject, data);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// SDK Class
|
|
104
|
+
class SDK {
|
|
105
|
+
constructor() {
|
|
106
|
+
this.http = null;
|
|
107
|
+
this.mqtt = null;
|
|
108
|
+
this.nats = null;
|
|
109
|
+
this._historian = null;
|
|
110
|
+
this._tag = null;
|
|
111
|
+
this._function = null;
|
|
112
|
+
this._flow = null;
|
|
113
|
+
this._auth = null;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Initializes the SDK with the required clients.
|
|
117
|
+
*
|
|
118
|
+
* You can initialize the SDK in the following ways:
|
|
119
|
+
*
|
|
120
|
+
* **1. Environment Variables (Recommended - works in both Node.js and Browser):**
|
|
121
|
+
* ```bash
|
|
122
|
+
* # For Vite/SPA projects (prefix with VITE_):
|
|
123
|
+
* VITE_MACHHUB_APPLICATION_ID=your-app-id
|
|
124
|
+
* VITE_MACHHUB_HTTP_URL=http://localhost:80
|
|
125
|
+
* VITE_MACHHUB_MQTT_URL=ws://localhost:180
|
|
126
|
+
* VITE_MACHHUB_NATS_URL=ws://localhost:7500
|
|
127
|
+
*
|
|
128
|
+
* # For Node.js projects:
|
|
129
|
+
* MACHHUB_APPLICATION_ID=your-app-id
|
|
130
|
+
* MACHHUB_HTTP_URL=http://localhost:80
|
|
131
|
+
* MACHHUB_MQTT_URL=ws://localhost:180
|
|
132
|
+
* MACHHUB_NATS_URL=ws://localhost:7500
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* ```typescript
|
|
136
|
+
* import { SDK } from '@machhub-dev/sdk-ts';
|
|
137
|
+
*
|
|
138
|
+
* const sdk = new SDK();
|
|
139
|
+
* await sdk.Initialize(); // Automatically reads from environment variables
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* **2. Programmatic Configuration:**
|
|
143
|
+
* ```typescript
|
|
144
|
+
* import { SDK, type SDKConfig } from '@machhub-dev/sdk-ts';
|
|
145
|
+
*
|
|
146
|
+
* const config: SDKConfig = {
|
|
147
|
+
* application_id: 'your-app-id',
|
|
148
|
+
* httpUrl: 'http://localhost:80', // optional
|
|
149
|
+
* mqttUrl: 'ws://localhost:180', // optional
|
|
150
|
+
* natsUrl: 'ws://localhost:7500', // optional
|
|
151
|
+
* };
|
|
152
|
+
*
|
|
153
|
+
* const sdk = new SDK();
|
|
154
|
+
* await sdk.Initialize(config);
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* **3. machhub.sdk.env File (Node.js only):**
|
|
158
|
+
* ```typescript
|
|
159
|
+
* // Create machhub.sdk.env in your project root:
|
|
160
|
+
* // MACHHUB_APPLICATION_ID=your-app-id
|
|
161
|
+
* // MACHHUB_HTTP_URL=http://localhost:80
|
|
162
|
+
* // MACHHUB_MQTT_URL=ws://localhost:180
|
|
163
|
+
* // MACHHUB_NATS_URL=ws://localhost:7500
|
|
164
|
+
*
|
|
165
|
+
* const sdk = new SDK();
|
|
166
|
+
* await sdk.Initialize(); // Reads from machhub.sdk.env if no env vars
|
|
167
|
+
* ```
|
|
168
|
+
*
|
|
169
|
+
* @param config {SDKConfig} Optional configuration object. If not provided, will try environment variables first, then machhub.sdk.env file.
|
|
170
|
+
* @param envFilePath {string} Optional path to env file. Only works in Node.js environments. Defaults to 'machhub.sdk.env' in project root.
|
|
171
|
+
* @returns {Promise<boolean>} Resolves to true if initialization is successful.
|
|
172
|
+
*/
|
|
173
|
+
async Initialize(config, envFilePath) {
|
|
174
|
+
try {
|
|
175
|
+
let finalConfig;
|
|
176
|
+
if (config) {
|
|
177
|
+
// Use provided config
|
|
178
|
+
finalConfig = config;
|
|
179
|
+
console.log("Using programmatic configuration");
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
// Try to load configuration automatically
|
|
183
|
+
const autoConfig = (0, appConfig_1.loadSDKConfig)(envFilePath);
|
|
184
|
+
if (!autoConfig) {
|
|
185
|
+
const isBrowser = typeof window !== 'undefined';
|
|
186
|
+
if (isBrowser) {
|
|
187
|
+
throw new Error(`No configuration found. For browser environments, either:
|
|
188
|
+
|
|
189
|
+
1. Set environment variables (recommended):
|
|
190
|
+
VITE_MACHHUB_APPLICATION_ID=your-app-id
|
|
191
|
+
VITE_MACHHUB_HTTP_URL=http://localhost:80
|
|
192
|
+
VITE_MACHHUB_MQTT_URL=ws://localhost:180
|
|
193
|
+
VITE_MACHHUB_NATS_URL=ws://localhost:7500
|
|
194
|
+
|
|
195
|
+
2. Pass a config object:
|
|
196
|
+
await sdk.Initialize({application_id: 'your-id', ...})`);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
const defaultPath = envFilePath || 'machhub.sdk.env (in project root)';
|
|
200
|
+
throw new Error(`No configuration found. Either:
|
|
201
|
+
|
|
202
|
+
1. Set environment variables:
|
|
203
|
+
MACHHUB_APPLICATION_ID=your-app-id
|
|
204
|
+
MACHHUB_HTTP_URL=http://localhost:80
|
|
205
|
+
MACHHUB_MQTT_URL=ws://localhost:180
|
|
206
|
+
MACHHUB_NATS_URL=ws://localhost:7500
|
|
207
|
+
|
|
208
|
+
2. Create a '${defaultPath}' file with your configuration
|
|
209
|
+
|
|
210
|
+
3. Pass a config object:
|
|
211
|
+
await sdk.Initialize({application_id: 'your-id', ...})`);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
finalConfig = autoConfig;
|
|
215
|
+
}
|
|
216
|
+
const { application_id, httpUrl, mqttUrl, natsUrl } = finalConfig;
|
|
217
|
+
if (!application_id) {
|
|
218
|
+
throw new Error("application_id is required in the configuration.");
|
|
219
|
+
}
|
|
220
|
+
this.http = new HTTPClient(application_id, httpUrl);
|
|
221
|
+
this.mqtt = await MQTTClient.getInstance(application_id, mqttUrl);
|
|
222
|
+
this.nats = await NATSClient.getInstance(application_id, natsUrl);
|
|
223
|
+
this._historian = new historian_1.Historian(this.http["httpService"], this.mqtt["mqttService"]);
|
|
224
|
+
this._tag = new tag_1.Tag(this.http["httpService"], this.mqtt["mqttService"]);
|
|
225
|
+
this._function = new function_1.Function(this.http["httpService"], this.nats["natsService"]);
|
|
226
|
+
this._flow = new flow_1.Flow(this.http["httpService"]);
|
|
227
|
+
this._auth = new auth_1.Auth(this.http["httpService"]);
|
|
228
|
+
console.log("SDK initialized successfully!");
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
console.error("Failed to initialize SDK:", error.message || error);
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Getter for `auth`. Ensures `auth` is accessed only after initialization.
|
|
238
|
+
*/
|
|
239
|
+
get auth() {
|
|
240
|
+
if (!this._auth) {
|
|
241
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `auth`.");
|
|
242
|
+
}
|
|
243
|
+
return this._auth;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Getter for `historian`. Ensures `historian` is accessed only after initialization.
|
|
247
|
+
*/
|
|
248
|
+
get historian() {
|
|
249
|
+
if (!this._historian) {
|
|
250
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `historian`.");
|
|
251
|
+
}
|
|
252
|
+
return this._historian;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Getter for `tag`. Ensures `tag` is accessed only after initialization.
|
|
256
|
+
*/
|
|
257
|
+
get tag() {
|
|
258
|
+
if (!this._tag) {
|
|
259
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `tag`.");
|
|
260
|
+
}
|
|
261
|
+
return this._tag;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Getter for `function`. Ensures `function` is accessed only after initialization.
|
|
265
|
+
*/
|
|
266
|
+
get function() {
|
|
267
|
+
if (!this._function) {
|
|
268
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `function`.");
|
|
269
|
+
}
|
|
270
|
+
return this._function;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Getter for `flow`. Ensures `flow` is accessed only after initialization.
|
|
274
|
+
*/
|
|
275
|
+
get flow() {
|
|
276
|
+
if (!this._flow) {
|
|
277
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `flow`.");
|
|
278
|
+
}
|
|
279
|
+
return this._flow;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Creates a collection instance to interact with the specified table/collection.
|
|
283
|
+
* Throws an error if the SDK is not initialized.
|
|
284
|
+
* @param collectionName {string} The collection/table name.
|
|
285
|
+
* @returns {Collection} An instance of Collection.
|
|
286
|
+
*/
|
|
287
|
+
collection(collectionName) {
|
|
288
|
+
if (!this.http) {
|
|
289
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing collection.");
|
|
290
|
+
}
|
|
291
|
+
return new collection_1.Collection(this.http["httpService"], this.mqtt ? this.mqtt["mqttService"] : null, collectionName);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
exports.SDK = SDK;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Collection } from "./classes/collection";
|
|
2
|
+
import { Historian } from "./classes/historian";
|
|
3
|
+
import { Tag } from "./classes/tag";
|
|
4
|
+
import { Function } from "./classes/function";
|
|
5
|
+
import { Flow } from "./classes/flow";
|
|
6
|
+
import { Auth } from "./classes/auth";
|
|
7
|
+
export interface SDKConfig {
|
|
8
|
+
application_id: string;
|
|
9
|
+
httpUrl?: string;
|
|
10
|
+
mqttUrl?: string;
|
|
11
|
+
natsUrl?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare class SDK {
|
|
14
|
+
private http;
|
|
15
|
+
private mqtt;
|
|
16
|
+
private nats;
|
|
17
|
+
private _historian;
|
|
18
|
+
private _tag;
|
|
19
|
+
private _function;
|
|
20
|
+
private _flow;
|
|
21
|
+
private _auth;
|
|
22
|
+
/**
|
|
23
|
+
* Initializes the SDK with the required clients.
|
|
24
|
+
*
|
|
25
|
+
* Example usage:
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { SDK, type SDKConfig } from '@machhub-dev/sdk-ts';
|
|
28
|
+
*
|
|
29
|
+
* const config: SDKConfig = {
|
|
30
|
+
* application_id: 'your-app-id',
|
|
31
|
+
* httpUrl: 'http://localhost:80', // optional (default = http://localhost:80)
|
|
32
|
+
* mqttUrl: 'ws://localhost:180', // optional (default = ws://localhost:180)
|
|
33
|
+
* natsUrl: 'ws://localhost:7500', // optional (default = ws://localhost:7500)
|
|
34
|
+
* };
|
|
35
|
+
*
|
|
36
|
+
* const sdk = new SDK();
|
|
37
|
+
* await sdk.Initialize(config);
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @param config {SDKConfig} The configuration object containing initialization parameters. See SDKConfig for details.
|
|
41
|
+
* @returns {Promise<boolean>} Resolves to true if initialization is successful.
|
|
42
|
+
*/
|
|
43
|
+
Initialize(config: SDKConfig): Promise<boolean>;
|
|
44
|
+
/**
|
|
45
|
+
* Getter for `auth`. Ensures `auth` is accessed only after initialization.
|
|
46
|
+
*/
|
|
47
|
+
get auth(): Auth;
|
|
48
|
+
/**
|
|
49
|
+
* Getter for `historian`. Ensures `historian` is accessed only after initialization.
|
|
50
|
+
*/
|
|
51
|
+
get historian(): Historian;
|
|
52
|
+
/**
|
|
53
|
+
* Getter for `tag`. Ensures `tag` is accessed only after initialization.
|
|
54
|
+
*/
|
|
55
|
+
get tag(): Tag;
|
|
56
|
+
/**
|
|
57
|
+
* Getter for `function`. Ensures `function` is accessed only after initialization.
|
|
58
|
+
*/
|
|
59
|
+
get function(): Function;
|
|
60
|
+
/**
|
|
61
|
+
* Getter for `flow`. Ensures `flow` is accessed only after initialization.
|
|
62
|
+
*/
|
|
63
|
+
get flow(): Flow;
|
|
64
|
+
/**
|
|
65
|
+
* Creates a collection instance to interact with the specified table/collection.
|
|
66
|
+
* Throws an error if the SDK is not initialized.
|
|
67
|
+
* @param collectionName {string} The collection/table name.
|
|
68
|
+
* @returns {Collection} An instance of Collection.
|
|
69
|
+
*/
|
|
70
|
+
collection(collectionName: string): Collection;
|
|
71
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SDK = void 0;
|
|
4
|
+
const http_service_1 = require("./services/http.service");
|
|
5
|
+
const mqtt_service_1 = require("./services/mqtt.service");
|
|
6
|
+
const nats_service_1 = require("./services/nats.service");
|
|
7
|
+
const appConfig_1 = require("./utils/appConfig");
|
|
8
|
+
const collection_1 = require("./classes/collection");
|
|
9
|
+
const historian_1 = require("./classes/historian");
|
|
10
|
+
const tag_1 = require("./classes/tag");
|
|
11
|
+
const function_1 = require("./classes/function");
|
|
12
|
+
const flow_1 = require("./classes/flow");
|
|
13
|
+
const auth_1 = require("./classes/auth");
|
|
14
|
+
const MACHHUB_SDK_PATH = "machhub";
|
|
15
|
+
// Core HTTP client class
|
|
16
|
+
class HTTPClient {
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new HTTPClient instance
|
|
19
|
+
* @param applicationID The ID for your application (required)
|
|
20
|
+
* @param httpUrl The base URL for HTTP connection (default = http://localhost:80)
|
|
21
|
+
*/
|
|
22
|
+
constructor(applicationID, httpUrl = "http://localhost:80") {
|
|
23
|
+
if (!applicationID) {
|
|
24
|
+
const config = (0, appConfig_1.getAppConfig)();
|
|
25
|
+
if (config != undefined) {
|
|
26
|
+
applicationID = config.application_id;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
throw new Error("Failed to get Configuration.");
|
|
30
|
+
}
|
|
31
|
+
if (!applicationID) {
|
|
32
|
+
throw new Error("Application ID is required. Set it via the APP_ID environment variable or pass it as a parameter.");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
this.httpService = new http_service_1.HTTPService(httpUrl, MACHHUB_SDK_PATH, applicationID);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Gets server info
|
|
39
|
+
*/
|
|
40
|
+
async getInfo() {
|
|
41
|
+
return this.httpService.request.get("info");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Core MQTT client class
|
|
45
|
+
class MQTTClient {
|
|
46
|
+
constructor(mqttService) {
|
|
47
|
+
this.mqttService = mqttService;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Creates a new MQTTClient instance
|
|
51
|
+
* @param applicationID The ID for your application
|
|
52
|
+
* @param mqttUrl The base URL for MQTT connection (default = ws://localhost:180)
|
|
53
|
+
*/
|
|
54
|
+
static async getInstance(applicationID, mqttUrl = "ws://localhost:180") {
|
|
55
|
+
// if (!applicationID) {
|
|
56
|
+
// applicationID = process.env.APP_ID;
|
|
57
|
+
// if (!applicationID) {
|
|
58
|
+
// throw new Error("Application ID is required. Set it via the APP_ID environment variable or pass it as a parameter.");
|
|
59
|
+
// }
|
|
60
|
+
// }
|
|
61
|
+
if (!this.instance) {
|
|
62
|
+
const mqttService = await mqtt_service_1.MQTTService.getInstance(mqttUrl);
|
|
63
|
+
this.instance = new MQTTClient(mqttService); // Use the constructor to initialize the instance
|
|
64
|
+
}
|
|
65
|
+
return this.instance;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Subscribes to live tag data updates
|
|
69
|
+
* @param topic The tag topic
|
|
70
|
+
* @param callback The callback function for data updates
|
|
71
|
+
*/
|
|
72
|
+
async subscribeLiveData(topic, callback) {
|
|
73
|
+
return this.mqttService.addTopicHandler(topic, callback);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Publishes a message to a specific topic
|
|
77
|
+
* @param topic The topic to publish to
|
|
78
|
+
* @param data The data to publish
|
|
79
|
+
*/
|
|
80
|
+
async publish(topic, data) {
|
|
81
|
+
return this.mqttService.publish(topic, data);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// Core NATS client class
|
|
85
|
+
class NATSClient {
|
|
86
|
+
constructor(natsService) {
|
|
87
|
+
this.natsService = natsService;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Creates a new NATSClient instance
|
|
91
|
+
* @param applicationID The ID for your application
|
|
92
|
+
* @param natsUrl The base URL for NATS connection (default = ws://localhost:7500)
|
|
93
|
+
*/
|
|
94
|
+
static async getInstance(applicationID, natsUrl = "ws://localhost:7500") {
|
|
95
|
+
if (!this.instance) {
|
|
96
|
+
const natsService = await nats_service_1.NATSService.getInstance(natsUrl);
|
|
97
|
+
this.instance = new NATSClient(natsService);
|
|
98
|
+
}
|
|
99
|
+
return this.instance;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Subscribes to subject updates
|
|
103
|
+
* @param subject The subject to subscribe to
|
|
104
|
+
* @param callback The callback function for data updates
|
|
105
|
+
*/
|
|
106
|
+
async subscribe(subject, callback) {
|
|
107
|
+
return this.natsService.addSubjectHandler(subject, callback);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Publishes a message to a specific subject
|
|
111
|
+
* @param subject The subject to publish to
|
|
112
|
+
* @param data The data to publish
|
|
113
|
+
*/
|
|
114
|
+
async publish(subject, data) {
|
|
115
|
+
return this.natsService.publish(subject, data);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// SDK Class
|
|
119
|
+
class SDK {
|
|
120
|
+
constructor() {
|
|
121
|
+
this.http = null;
|
|
122
|
+
this.mqtt = null;
|
|
123
|
+
this.nats = null;
|
|
124
|
+
this._historian = null;
|
|
125
|
+
this._tag = null;
|
|
126
|
+
this._function = null;
|
|
127
|
+
this._flow = null;
|
|
128
|
+
this._auth = null;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Initializes the SDK with the required clients.
|
|
132
|
+
*
|
|
133
|
+
* Example usage:
|
|
134
|
+
* ```typescript
|
|
135
|
+
* import { SDK, type SDKConfig } from '@machhub-dev/sdk-ts';
|
|
136
|
+
*
|
|
137
|
+
* const config: SDKConfig = {
|
|
138
|
+
* application_id: 'your-app-id',
|
|
139
|
+
* httpUrl: 'http://localhost:80', // optional (default = http://localhost:80)
|
|
140
|
+
* mqttUrl: 'ws://localhost:180', // optional (default = ws://localhost:180)
|
|
141
|
+
* natsUrl: 'ws://localhost:7500', // optional (default = ws://localhost:7500)
|
|
142
|
+
* };
|
|
143
|
+
*
|
|
144
|
+
* const sdk = new SDK();
|
|
145
|
+
* await sdk.Initialize(config);
|
|
146
|
+
* ```
|
|
147
|
+
*
|
|
148
|
+
* @param config {SDKConfig} The configuration object containing initialization parameters. See SDKConfig for details.
|
|
149
|
+
* @returns {Promise<boolean>} Resolves to true if initialization is successful.
|
|
150
|
+
*/
|
|
151
|
+
async Initialize(config) {
|
|
152
|
+
try {
|
|
153
|
+
const { application_id, httpUrl, mqttUrl, natsUrl } = config;
|
|
154
|
+
this.http = new HTTPClient(application_id, httpUrl);
|
|
155
|
+
this.mqtt = await MQTTClient.getInstance(application_id, mqttUrl);
|
|
156
|
+
this.nats = await NATSClient.getInstance(application_id, natsUrl);
|
|
157
|
+
this._historian = new historian_1.Historian(this.http["httpService"], this.mqtt["mqttService"]);
|
|
158
|
+
this._tag = new tag_1.Tag(this.http["httpService"], this.mqtt["mqttService"]);
|
|
159
|
+
this._function = new function_1.Function(this.http["httpService"], this.nats["natsService"]);
|
|
160
|
+
this._flow = new flow_1.Flow(this.http["httpService"]);
|
|
161
|
+
this._auth = new auth_1.Auth(this.http["httpService"]);
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
console.error("Failed to initialize:", error);
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Getter for `auth`. Ensures `auth` is accessed only after initialization.
|
|
171
|
+
*/
|
|
172
|
+
get auth() {
|
|
173
|
+
if (!this._auth) {
|
|
174
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `auth`.");
|
|
175
|
+
}
|
|
176
|
+
return this._auth;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Getter for `historian`. Ensures `historian` is accessed only after initialization.
|
|
180
|
+
*/
|
|
181
|
+
get historian() {
|
|
182
|
+
if (!this._historian) {
|
|
183
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `historian`.");
|
|
184
|
+
}
|
|
185
|
+
return this._historian;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Getter for `tag`. Ensures `tag` is accessed only after initialization.
|
|
189
|
+
*/
|
|
190
|
+
get tag() {
|
|
191
|
+
if (!this._tag) {
|
|
192
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `tag`.");
|
|
193
|
+
}
|
|
194
|
+
return this._tag;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Getter for `function`. Ensures `function` is accessed only after initialization.
|
|
198
|
+
*/
|
|
199
|
+
get function() {
|
|
200
|
+
if (!this._function) {
|
|
201
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `function`.");
|
|
202
|
+
}
|
|
203
|
+
return this._function;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Getter for `flow`. Ensures `flow` is accessed only after initialization.
|
|
207
|
+
*/
|
|
208
|
+
get flow() {
|
|
209
|
+
if (!this._flow) {
|
|
210
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `flow`.");
|
|
211
|
+
}
|
|
212
|
+
return this._flow;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Creates a collection instance to interact with the specified table/collection.
|
|
216
|
+
* Throws an error if the SDK is not initialized.
|
|
217
|
+
* @param collectionName {string} The collection/table name.
|
|
218
|
+
* @returns {Collection} An instance of Collection.
|
|
219
|
+
*/
|
|
220
|
+
collection(collectionName) {
|
|
221
|
+
if (!this.http) {
|
|
222
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing collection.");
|
|
223
|
+
}
|
|
224
|
+
return new collection_1.Collection(this.http["httpService"], this.mqtt ? this.mqtt["mqttService"] : null, collectionName);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
exports.SDK = SDK;
|