@machhub-dev/sdk-ts 0.0.2 → 0.0.4
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/dist/cjs/classes/auth.d.ts +2 -2
- package/dist/cjs/classes/collection.d.ts +2 -2
- package/dist/cjs/classes/collection.js +9 -0
- package/dist/cjs/classes/flow.d.ts +1 -1
- package/dist/cjs/classes/function.d.ts +2 -2
- package/dist/cjs/classes/historian.d.ts +3 -3
- package/dist/cjs/classes/tag.d.ts +2 -2
- package/dist/cjs/example/functions.js +2 -2
- package/dist/cjs/sdk-ts.d.ts +9 -8
- package/dist/cjs/sdk-ts.js +79 -37
- package/dist/cjs/services/http.service.d.ts +7 -2
- package/dist/cjs/services/http.service.js +32 -3
- package/dist/cjs/services/mqtt.service.d.ts +1 -1
- package/dist/cjs/services/mqtt.service.js +9 -2
- package/dist/cjs/types/auth.models.d.ts +2 -2
- package/dist/classes/auth.d.ts +2 -2
- package/dist/classes/collection.d.ts +2 -2
- package/dist/classes/collection.js +9 -0
- package/dist/classes/flow.d.ts +1 -1
- package/dist/classes/function.d.ts +2 -2
- package/dist/classes/historian.d.ts +3 -3
- package/dist/classes/tag.d.ts +2 -2
- package/dist/example/functions.js +1 -1
- package/dist/sdk-ts.d.ts +9 -8
- package/dist/sdk-ts.js +72 -30
- package/dist/services/http.service.d.ts +7 -2
- package/dist/services/http.service.js +32 -3
- package/dist/services/mqtt.service.d.ts +1 -1
- package/dist/services/mqtt.service.js +9 -2
- package/dist/types/auth.models.d.ts +2 -2
- package/package.json +1 -1
- package/src/classes/auth.ts +2 -2
- package/src/classes/collection.ts +11 -2
- package/src/classes/flow.ts +1 -1
- package/src/classes/function.ts +2 -2
- package/src/classes/historian.ts +3 -3
- package/src/classes/tag.ts +2 -2
- package/src/example/functions.ts +1 -1
- package/src/sdk-ts.ts +87 -31
- package/src/services/http.service.ts +50 -15
- package/src/services/mqtt.service.ts +9 -2
- package/src/types/auth.models.ts +2 -2
- package/src/utils/appConfig.ts +0 -30
package/dist/sdk-ts.js
CHANGED
|
@@ -1,35 +1,22 @@
|
|
|
1
|
-
import { HTTPService } from "./services/http.service";
|
|
2
|
-
import { MQTTService } from "./services/mqtt.service";
|
|
3
|
-
import { NATSService } from "./services/nats.service";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { Auth } from "./classes/auth";
|
|
1
|
+
import { HTTPService } from "./services/http.service.js";
|
|
2
|
+
import { MQTTService } from "./services/mqtt.service.js";
|
|
3
|
+
import { NATSService } from "./services/nats.service.js";
|
|
4
|
+
import { Collection } from "./classes/collection.js";
|
|
5
|
+
import { Historian } from "./classes/historian.js";
|
|
6
|
+
import { Tag } from "./classes/tag.js";
|
|
7
|
+
import { Function } from "./classes/function.js";
|
|
8
|
+
import { Flow } from "./classes/flow.js";
|
|
9
|
+
import { Auth } from "./classes/auth.js";
|
|
11
10
|
const MACHHUB_SDK_PATH = "machhub";
|
|
12
11
|
// Core HTTP client class
|
|
13
12
|
class HTTPClient {
|
|
14
13
|
/**
|
|
15
14
|
* Creates a new HTTPClient instance
|
|
16
15
|
* @param applicationID The ID for your application (required)
|
|
17
|
-
* @param httpUrl The base URL for HTTP connection (default = http://localhost:
|
|
16
|
+
* @param httpUrl The base URL for HTTP connection (default = http://localhost:6188)
|
|
18
17
|
*/
|
|
19
|
-
constructor(applicationID, httpUrl
|
|
20
|
-
|
|
21
|
-
const config = getAppConfig();
|
|
22
|
-
if (config != undefined) {
|
|
23
|
-
applicationID = config.application_id;
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
throw new Error("Failed to get Configuration.");
|
|
27
|
-
}
|
|
28
|
-
if (!applicationID) {
|
|
29
|
-
throw new Error("Application ID is required. Set it via the APP_ID environment variable or pass it as a parameter.");
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
this.httpService = new HTTPService(httpUrl, MACHHUB_SDK_PATH, applicationID);
|
|
18
|
+
constructor(applicationID, httpUrl, developerKey) {
|
|
19
|
+
this.httpService = new HTTPService(httpUrl, MACHHUB_SDK_PATH, applicationID, developerKey);
|
|
33
20
|
}
|
|
34
21
|
/**
|
|
35
22
|
* Gets server info
|
|
@@ -48,7 +35,7 @@ class MQTTClient {
|
|
|
48
35
|
* @param applicationID The ID for your application
|
|
49
36
|
* @param mqttUrl The base URL for MQTT connection (default = ws://localhost:180)
|
|
50
37
|
*/
|
|
51
|
-
static async getInstance(applicationID, mqttUrl = "ws://localhost:180") {
|
|
38
|
+
static async getInstance(applicationID, mqttUrl = "ws://localhost:180", developerKey) {
|
|
52
39
|
// if (!applicationID) {
|
|
53
40
|
// applicationID = process.env.APP_ID;
|
|
54
41
|
// if (!applicationID) {
|
|
@@ -56,7 +43,7 @@ class MQTTClient {
|
|
|
56
43
|
// }
|
|
57
44
|
// }
|
|
58
45
|
if (!this.instance) {
|
|
59
|
-
const mqttService = await MQTTService.getInstance(mqttUrl);
|
|
46
|
+
const mqttService = await MQTTService.getInstance(mqttUrl, developerKey);
|
|
60
47
|
this.instance = new MQTTClient(mqttService); // Use the constructor to initialize the instance
|
|
61
48
|
}
|
|
62
49
|
return this.instance;
|
|
@@ -133,7 +120,7 @@ export class SDK {
|
|
|
133
120
|
*
|
|
134
121
|
* const config: SDKConfig = {
|
|
135
122
|
* application_id: 'your-app-id',
|
|
136
|
-
* httpUrl: 'http://localhost:
|
|
123
|
+
* httpUrl: 'http://localhost:6188', // optional (default = http://localhost:6188)
|
|
137
124
|
* mqttUrl: 'ws://localhost:180', // optional (default = ws://localhost:180)
|
|
138
125
|
* natsUrl: 'ws://localhost:7500', // optional (default = ws://localhost:7500)
|
|
139
126
|
* };
|
|
@@ -147,9 +134,31 @@ export class SDK {
|
|
|
147
134
|
*/
|
|
148
135
|
async Initialize(config) {
|
|
149
136
|
try {
|
|
137
|
+
console.log("Initializing SDK with config:", config);
|
|
138
|
+
// Methods to initialize config
|
|
139
|
+
// 1. Via application_id + URLs + developer key passed in config parameter
|
|
140
|
+
// 2. Via development server - Set via Extension/
|
|
141
|
+
// API to get Config (All SDK URLs default to localhost with port from querying current window or 61888)
|
|
142
|
+
if (config === undefined)
|
|
143
|
+
config = { application_id: "" };
|
|
144
|
+
if (!config.application_id)
|
|
145
|
+
config = { application_id: "" };
|
|
146
|
+
console.log("Using application_id:", config.application_id);
|
|
147
|
+
const PORT = await getEnvPort();
|
|
148
|
+
console.log("Using port:", PORT);
|
|
149
|
+
if (!config.httpUrl) {
|
|
150
|
+
config.httpUrl = "http://localhost:" + PORT;
|
|
151
|
+
}
|
|
152
|
+
if (!config.mqttUrl) {
|
|
153
|
+
config.mqttUrl = "ws://localhost:" + PORT + "/mqtt";
|
|
154
|
+
}
|
|
155
|
+
if (!config.natsUrl) {
|
|
156
|
+
config.natsUrl = "ws://localhost:" + PORT + "/nats";
|
|
157
|
+
}
|
|
150
158
|
const { application_id, httpUrl, mqttUrl, natsUrl } = config;
|
|
151
|
-
|
|
152
|
-
this.
|
|
159
|
+
console.log("Final config:", { application_id, httpUrl, mqttUrl, natsUrl });
|
|
160
|
+
this.http = new HTTPClient(application_id, httpUrl, config.developer_key);
|
|
161
|
+
this.mqtt = await MQTTClient.getInstance(application_id, mqttUrl, config.developer_key);
|
|
153
162
|
this.nats = await NATSClient.getInstance(application_id, natsUrl);
|
|
154
163
|
this._historian = new Historian(this.http["httpService"], this.mqtt["mqttService"]);
|
|
155
164
|
this._tag = new Tag(this.http["httpService"], this.mqtt["mqttService"]);
|
|
@@ -221,3 +230,36 @@ export class SDK {
|
|
|
221
230
|
return new Collection(this.http["httpService"], this.mqtt ? this.mqtt["mqttService"] : null, collectionName);
|
|
222
231
|
}
|
|
223
232
|
}
|
|
233
|
+
async function getEnvPort() {
|
|
234
|
+
console.log(window.location.origin);
|
|
235
|
+
try {
|
|
236
|
+
const response = await fetchData(window.location.origin + "/_cfg");
|
|
237
|
+
// console.log('Response:', response);
|
|
238
|
+
// console.log('runtimeID: ', response.runtimeID);
|
|
239
|
+
// console.log('applicationID: ', response.runtimeID.split('XmchX')[0]);
|
|
240
|
+
return response.port;
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
// console.log('No configured runtime ID:', error);
|
|
244
|
+
// TODO: Use DevPort from SDK Config or default to 61888
|
|
245
|
+
return "61888";
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async function fetchData(url) {
|
|
249
|
+
try {
|
|
250
|
+
const response = await fetch(url, {
|
|
251
|
+
method: 'GET',
|
|
252
|
+
headers: {
|
|
253
|
+
'Accept': 'application/json',
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
if (!response.ok) {
|
|
257
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
258
|
+
}
|
|
259
|
+
const data = await response.json();
|
|
260
|
+
return data;
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
@@ -8,16 +8,20 @@ export declare class HTTPException extends Error {
|
|
|
8
8
|
export declare class HTTPService {
|
|
9
9
|
private url;
|
|
10
10
|
private applicationID;
|
|
11
|
-
|
|
11
|
+
private developerKey?;
|
|
12
|
+
constructor(url: string, prefix: string, applicationID: string, developerKey?: string);
|
|
12
13
|
get request(): RequestParameters;
|
|
14
|
+
private addRuntimeHeaders;
|
|
15
|
+
private getCookie;
|
|
13
16
|
}
|
|
14
17
|
declare class RequestParameters {
|
|
15
18
|
private base;
|
|
16
19
|
private applicationID;
|
|
20
|
+
private developerKey?;
|
|
17
21
|
query?: Record<string, string>;
|
|
18
22
|
init?: RequestInit;
|
|
19
23
|
headers?: Record<string, string>;
|
|
20
|
-
constructor(base: URL, applicationID: string, query?: Record<string, string>);
|
|
24
|
+
constructor(base: URL, applicationID: string, developerKey?: string, query?: Record<string, string>);
|
|
21
25
|
private withQuery;
|
|
22
26
|
private parseInit;
|
|
23
27
|
withBody(body: BodyInit): RequestParameters;
|
|
@@ -30,6 +34,7 @@ declare class RequestParameters {
|
|
|
30
34
|
setBearerToken(token: string): RequestParameters;
|
|
31
35
|
withAccessToken(): RequestParameters;
|
|
32
36
|
withDomain(): RequestParameters;
|
|
37
|
+
withDeveloperKey(): RequestParameters;
|
|
33
38
|
withContentType(mime: string): RequestParameters;
|
|
34
39
|
withJSON(body: Record<string, unknown>): RequestParameters;
|
|
35
40
|
get<ReturnType>(path: string, query?: Record<string, string>): Promise<ReturnType>;
|
|
@@ -10,23 +10,46 @@ export class HTTPException extends Error {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
export class HTTPService {
|
|
13
|
-
constructor(url, prefix, applicationID) {
|
|
13
|
+
constructor(url, prefix, applicationID, developerKey) {
|
|
14
14
|
if (prefix == null)
|
|
15
15
|
prefix = "";
|
|
16
16
|
this.url = new URL(prefix, url);
|
|
17
17
|
this.applicationID = "domains:" + applicationID;
|
|
18
|
+
this.developerKey = developerKey;
|
|
18
19
|
}
|
|
19
20
|
get request() {
|
|
20
|
-
return new RequestParameters(this.url, this.applicationID);
|
|
21
|
+
return new RequestParameters(this.url, this.applicationID, this.developerKey);
|
|
22
|
+
}
|
|
23
|
+
addRuntimeHeaders(headers = {}) {
|
|
24
|
+
// Add runtime ID from cookie if available (for hosted applications)
|
|
25
|
+
if (typeof document !== 'undefined') {
|
|
26
|
+
const runtimeID = this.getCookie('machhub_runtime_id');
|
|
27
|
+
if (runtimeID) {
|
|
28
|
+
headers['X-MachHub-Runtime-ID'] = runtimeID;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return headers;
|
|
32
|
+
}
|
|
33
|
+
getCookie(name) {
|
|
34
|
+
if (typeof document === 'undefined')
|
|
35
|
+
return null;
|
|
36
|
+
const value = `; ${document.cookie}`;
|
|
37
|
+
const parts = value.split(`; ${name}=`);
|
|
38
|
+
if (parts.length === 2) {
|
|
39
|
+
return parts.pop()?.split(';').shift() || null;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
21
42
|
}
|
|
22
43
|
}
|
|
23
44
|
class RequestParameters {
|
|
24
|
-
constructor(base, applicationID, query) {
|
|
45
|
+
constructor(base, applicationID, developerKey, query) {
|
|
25
46
|
this.base = base;
|
|
26
47
|
this.applicationID = applicationID;
|
|
48
|
+
this.developerKey = developerKey;
|
|
27
49
|
this.query = query;
|
|
28
50
|
this.withDomain(); // Ensure withDomain() is called by default
|
|
29
51
|
this.withAccessToken(); // Ensure withAccessToken() is called by default
|
|
52
|
+
this.withDeveloperKey(); // Ensure withDeveloperKey() is called by default
|
|
30
53
|
}
|
|
31
54
|
withQuery(path, query) {
|
|
32
55
|
const newPath = [this.base.pathname, path].map(pathPart => pathPart.replace(/(^\/|\/$)/g, "")).join("/");
|
|
@@ -114,6 +137,12 @@ class RequestParameters {
|
|
|
114
137
|
this.setHeader("Domain", this.applicationID);
|
|
115
138
|
return this;
|
|
116
139
|
}
|
|
140
|
+
withDeveloperKey() {
|
|
141
|
+
if (!this.developerKey)
|
|
142
|
+
return this;
|
|
143
|
+
this.setHeader("X-Machhub-Api-Key", this.developerKey);
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
117
146
|
withContentType(mime) {
|
|
118
147
|
this.setHeader("Content-Type", mime);
|
|
119
148
|
return this;
|
|
@@ -5,7 +5,7 @@ export declare class MQTTService {
|
|
|
5
5
|
private static instance;
|
|
6
6
|
private subscribedTopics;
|
|
7
7
|
constructor(url: string);
|
|
8
|
-
static getInstance(url?: string): Promise<MQTTService>;
|
|
8
|
+
static getInstance(url?: string, developerKey?: string): Promise<MQTTService>;
|
|
9
9
|
static resetInstance(): void;
|
|
10
10
|
addTopicHandler(topic: string, handler: (message: unknown) => void): void;
|
|
11
11
|
clearTopics(): void;
|
|
@@ -4,13 +4,20 @@ export class MQTTService {
|
|
|
4
4
|
this.subscribedTopics = [];
|
|
5
5
|
this.url = url;
|
|
6
6
|
}
|
|
7
|
-
static async getInstance(url = "ws://localhost:180") {
|
|
7
|
+
static async getInstance(url = "ws://localhost:180", developerKey) {
|
|
8
8
|
if (!this.instance || !this.instance.client) {
|
|
9
9
|
this.instance = new MQTTService(url);
|
|
10
10
|
// Ensure the URL is set correctly
|
|
11
11
|
this.instance.url = url;
|
|
12
12
|
// Initialize the MQTT client
|
|
13
|
-
|
|
13
|
+
const connectOptions = {
|
|
14
|
+
protocolVersion: 5,
|
|
15
|
+
...(developerKey && {
|
|
16
|
+
username: "mch_developer_key",
|
|
17
|
+
password: developerKey,
|
|
18
|
+
})
|
|
19
|
+
};
|
|
20
|
+
this.instance.client = mqtt.connect(this.instance.url, connectOptions);
|
|
14
21
|
this.instance.attachMessageListener();
|
|
15
22
|
// console.log("MQTT Client initialized with URL:", this.instance.url);
|
|
16
23
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { RecordID } from "./recordID.models";
|
|
2
|
-
import { BaseResponse } from "./response.models";
|
|
1
|
+
import { RecordID } from "./recordID.models.js";
|
|
2
|
+
import { BaseResponse } from "./response.models.js";
|
|
3
3
|
export interface LoginResponse extends BaseResponse {
|
|
4
4
|
tkn: string;
|
|
5
5
|
}
|
package/package.json
CHANGED
package/src/classes/auth.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { HTTPService } from "../services/http.service";
|
|
2
|
-
import { Action, ActionResponse, Feature, Group, LoginResponse, User, ValidateJWTResponse } from "../types/auth.models";
|
|
1
|
+
import { HTTPService } from "../services/http.service.js";
|
|
2
|
+
import { Action, ActionResponse, Feature, Group, LoginResponse, User, ValidateJWTResponse } from "../types/auth.models.js";
|
|
3
3
|
|
|
4
4
|
export class Auth {
|
|
5
5
|
private httpService: HTTPService;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { HTTPService } from "../services/http.service";
|
|
2
|
-
import { MQTTService } from "../services/mqtt.service";
|
|
1
|
+
import { HTTPService } from "../services/http.service.js";
|
|
2
|
+
import { MQTTService } from "../services/mqtt.service.js";
|
|
3
3
|
|
|
4
4
|
export class Collection {
|
|
5
5
|
protected httpService: HTTPService;
|
|
@@ -38,6 +38,9 @@ export class Collection {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
async getOne(id: string): Promise<any> {
|
|
41
|
+
if (!id) {
|
|
42
|
+
throw new Error("ID must be provided");
|
|
43
|
+
}
|
|
41
44
|
return this.httpService.request.get(id);
|
|
42
45
|
}
|
|
43
46
|
|
|
@@ -46,10 +49,16 @@ export class Collection {
|
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
async update(id: string, data: Record<string, any>): Promise<any> {
|
|
52
|
+
if (!id) {
|
|
53
|
+
throw new Error("ID must be provided");
|
|
54
|
+
}
|
|
49
55
|
return this.httpService.request.withJSON(data).put(id);
|
|
50
56
|
}
|
|
51
57
|
|
|
52
58
|
async delete(id: string): Promise<any> {
|
|
59
|
+
if (!id) {
|
|
60
|
+
throw new Error("ID must be provided");
|
|
61
|
+
}
|
|
53
62
|
return this.httpService.request.delete(id);
|
|
54
63
|
}
|
|
55
64
|
}
|
package/src/classes/flow.ts
CHANGED
package/src/classes/function.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { HTTPService } from "../services/http.service";
|
|
2
|
-
import { NATSService } from "../services/nats.service";
|
|
1
|
+
import { HTTPService } from "../services/http.service.js";
|
|
2
|
+
import { NATSService } from "../services/nats.service.js";
|
|
3
3
|
|
|
4
4
|
export class Function {
|
|
5
5
|
private httpService: HTTPService;
|
package/src/classes/historian.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { HTTPService } from "../services/http.service";
|
|
2
|
-
import { MQTTService } from "../services/mqtt.service";
|
|
3
|
-
import { HistorizedData } from "../types/tag.models";
|
|
1
|
+
import { HTTPService } from "../services/http.service.js";
|
|
2
|
+
import { MQTTService } from "../services/mqtt.service.js";
|
|
3
|
+
import { HistorizedData } from "../types/tag.models.js";
|
|
4
4
|
|
|
5
5
|
export class Historian {
|
|
6
6
|
private httpService: HTTPService;
|
package/src/classes/tag.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { HTTPService } from "../services/http.service";
|
|
2
|
-
import { MQTTService } from "../services/mqtt.service";
|
|
1
|
+
import { HTTPService } from "../services/http.service.js";
|
|
2
|
+
import { MQTTService } from "../services/mqtt.service.js";
|
|
3
3
|
|
|
4
4
|
export class Tag {
|
|
5
5
|
private httpService: HTTPService;
|
package/src/example/functions.ts
CHANGED
package/src/sdk-ts.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { HTTPService } from "./services/http.service";
|
|
2
|
-
import { MQTTService } from "./services/mqtt.service";
|
|
3
|
-
import { NATSService } from "./services/nats.service";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { Auth } from "./classes/auth";
|
|
1
|
+
import { HTTPService } from "./services/http.service.js";
|
|
2
|
+
import { MQTTService } from "./services/mqtt.service.js";
|
|
3
|
+
import { NATSService } from "./services/nats.service.js";
|
|
4
|
+
import { Collection } from "./classes/collection.js";
|
|
5
|
+
import { Historian } from "./classes/historian.js";
|
|
6
|
+
import { Tag } from "./classes/tag.js";
|
|
7
|
+
import { Function } from "./classes/function.js";
|
|
8
|
+
import { Flow } from "./classes/flow.js";
|
|
9
|
+
import { Auth } from "./classes/auth.js";
|
|
11
10
|
|
|
12
11
|
const MACHHUB_SDK_PATH = "machhub";
|
|
13
12
|
|
|
@@ -18,21 +17,10 @@ class HTTPClient {
|
|
|
18
17
|
/**
|
|
19
18
|
* Creates a new HTTPClient instance
|
|
20
19
|
* @param applicationID The ID for your application (required)
|
|
21
|
-
* @param httpUrl The base URL for HTTP connection (default = http://localhost:
|
|
20
|
+
* @param httpUrl The base URL for HTTP connection (default = http://localhost:6188)
|
|
22
21
|
*/
|
|
23
|
-
constructor(applicationID: string, httpUrl:
|
|
24
|
-
|
|
25
|
-
const config = getAppConfig()
|
|
26
|
-
if (config != undefined) {
|
|
27
|
-
applicationID = config.application_id;
|
|
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 HTTPService(httpUrl, MACHHUB_SDK_PATH, applicationID);
|
|
22
|
+
constructor(applicationID: string, httpUrl:string, developerKey?: string) {
|
|
23
|
+
this.httpService = new HTTPService(httpUrl, MACHHUB_SDK_PATH, applicationID, developerKey);
|
|
36
24
|
}
|
|
37
25
|
|
|
38
26
|
/**
|
|
@@ -57,7 +45,7 @@ class MQTTClient {
|
|
|
57
45
|
* @param applicationID The ID for your application
|
|
58
46
|
* @param mqttUrl The base URL for MQTT connection (default = ws://localhost:180)
|
|
59
47
|
*/
|
|
60
|
-
static async getInstance(applicationID?: string, mqttUrl: string = "ws://localhost:180"): Promise<MQTTClient> {
|
|
48
|
+
static async getInstance(applicationID?: string, mqttUrl: string = "ws://localhost:180", developerKey? :string): Promise<MQTTClient> {
|
|
61
49
|
// if (!applicationID) {
|
|
62
50
|
// applicationID = process.env.APP_ID;
|
|
63
51
|
// if (!applicationID) {
|
|
@@ -65,7 +53,7 @@ class MQTTClient {
|
|
|
65
53
|
// }
|
|
66
54
|
// }
|
|
67
55
|
if (!this.instance) {
|
|
68
|
-
const mqttService = await MQTTService.getInstance(mqttUrl);
|
|
56
|
+
const mqttService = await MQTTService.getInstance(mqttUrl, developerKey);
|
|
69
57
|
this.instance = new MQTTClient(mqttService); // Use the constructor to initialize the instance
|
|
70
58
|
}
|
|
71
59
|
return this.instance;
|
|
@@ -133,6 +121,7 @@ class NATSClient {
|
|
|
133
121
|
|
|
134
122
|
export interface SDKConfig {
|
|
135
123
|
application_id: string;
|
|
124
|
+
developer_key?: string;
|
|
136
125
|
httpUrl?: string;
|
|
137
126
|
mqttUrl?: string;
|
|
138
127
|
natsUrl?: string;
|
|
@@ -158,7 +147,7 @@ export class SDK {
|
|
|
158
147
|
*
|
|
159
148
|
* const config: SDKConfig = {
|
|
160
149
|
* application_id: 'your-app-id',
|
|
161
|
-
* httpUrl: 'http://localhost:
|
|
150
|
+
* httpUrl: 'http://localhost:6188', // optional (default = http://localhost:6188)
|
|
162
151
|
* mqttUrl: 'ws://localhost:180', // optional (default = ws://localhost:180)
|
|
163
152
|
* natsUrl: 'ws://localhost:7500', // optional (default = ws://localhost:7500)
|
|
164
153
|
* };
|
|
@@ -170,13 +159,44 @@ export class SDK {
|
|
|
170
159
|
* @param config {SDKConfig} The configuration object containing initialization parameters. See SDKConfig for details.
|
|
171
160
|
* @returns {Promise<boolean>} Resolves to true if initialization is successful.
|
|
172
161
|
*/
|
|
173
|
-
public async Initialize(config
|
|
162
|
+
public async Initialize(config?: SDKConfig): Promise<boolean> {
|
|
174
163
|
try {
|
|
164
|
+
console.log("Initializing SDK with config:", config)
|
|
165
|
+
|
|
166
|
+
// Methods to initialize config
|
|
167
|
+
// 1. Via application_id + URLs + developer key passed in config parameter
|
|
168
|
+
// 2. Via development server - Set via Extension/
|
|
169
|
+
// API to get Config (All SDK URLs default to localhost with port from querying current window or 61888)
|
|
170
|
+
|
|
171
|
+
if (config === undefined) config = { application_id: "" }
|
|
172
|
+
if (!config.application_id) config = { application_id: "" }
|
|
173
|
+
console.log("Using application_id:", config.application_id);
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
const PORT = await getEnvPort();
|
|
177
|
+
console.log("Using port:", PORT);
|
|
178
|
+
|
|
179
|
+
if (!config.httpUrl) {
|
|
180
|
+
config.httpUrl = "http://localhost:" + PORT;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (!config.mqttUrl) {
|
|
184
|
+
config.mqttUrl = "ws://localhost:" + PORT + "/mqtt";
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!config.natsUrl) {
|
|
188
|
+
config.natsUrl = "ws://localhost:" + PORT + "/nats";
|
|
189
|
+
}
|
|
190
|
+
|
|
175
191
|
const { application_id, httpUrl, mqttUrl, natsUrl } = config;
|
|
176
192
|
|
|
177
|
-
|
|
178
|
-
|
|
193
|
+
console.log("Final config:", { application_id, httpUrl, mqttUrl, natsUrl });
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
this.http = new HTTPClient(application_id, httpUrl, config.developer_key);
|
|
197
|
+
this.mqtt = await MQTTClient.getInstance(application_id, mqttUrl, config.developer_key);
|
|
179
198
|
this.nats = await NATSClient.getInstance(application_id, natsUrl);
|
|
199
|
+
|
|
180
200
|
this._historian = new Historian(this.http["httpService"], this.mqtt["mqttService"]);
|
|
181
201
|
this._tag = new Tag(this.http["httpService"], this.mqtt["mqttService"]);
|
|
182
202
|
this._function = new Function(this.http["httpService"], this.nats["natsService"]);
|
|
@@ -252,4 +272,40 @@ export class SDK {
|
|
|
252
272
|
}
|
|
253
273
|
return new Collection(this.http["httpService"], this.mqtt ? this.mqtt["mqttService"] : null, collectionName);
|
|
254
274
|
}
|
|
255
|
-
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async function getEnvPort(): Promise<string> {
|
|
278
|
+
console.log(window.location.origin)
|
|
279
|
+
try {
|
|
280
|
+
const response = await fetchData<{runtimeID:string, port:string}>(window.location.origin + "/_cfg");
|
|
281
|
+
// console.log('Response:', response);
|
|
282
|
+
// console.log('runtimeID: ', response.runtimeID);
|
|
283
|
+
// console.log('applicationID: ', response.runtimeID.split('XmchX')[0]);
|
|
284
|
+
return response.port;
|
|
285
|
+
} catch (error) {
|
|
286
|
+
// console.log('No configured runtime ID:', error);
|
|
287
|
+
// TODO: Use DevPort from SDK Config or default to 61888
|
|
288
|
+
return "61888";
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
async function fetchData<T>(url: string): Promise<T> {
|
|
294
|
+
try {
|
|
295
|
+
const response = await fetch(url, {
|
|
296
|
+
method: 'GET',
|
|
297
|
+
headers: {
|
|
298
|
+
'Accept': 'application/json',
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
if (!response.ok) {
|
|
303
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const data: T = await response.json();
|
|
307
|
+
return data;
|
|
308
|
+
} catch (error) {
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
}
|