@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.
Files changed (112) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/classes/auth.d.ts +18 -0
  3. package/dist/cjs/classes/auth.js +90 -0
  4. package/dist/cjs/classes/collection.d.ts +18 -0
  5. package/dist/cjs/classes/collection.js +43 -0
  6. package/dist/cjs/classes/flow.d.ts +6 -0
  7. package/dist/cjs/classes/flow.js +12 -0
  8. package/dist/cjs/classes/function.d.ts +10 -0
  9. package/dist/cjs/classes/function.js +29 -0
  10. package/dist/cjs/classes/historian.d.ts +12 -0
  11. package/dist/cjs/classes/historian.js +40 -0
  12. package/dist/cjs/classes/tag.d.ts +10 -0
  13. package/dist/cjs/classes/tag.js +25 -0
  14. package/dist/cjs/client.d.ts +13 -0
  15. package/dist/cjs/client.js +35 -0
  16. package/dist/cjs/config.d.ts +21 -0
  17. package/dist/cjs/config.js +37 -0
  18. package/dist/cjs/example/functions-file-config.d.ts +0 -0
  19. package/dist/cjs/example/functions-file-config.js +1 -0
  20. package/dist/cjs/example/functions.d.ts +1 -0
  21. package/dist/cjs/example/functions.js +18 -0
  22. package/dist/cjs/http-client.d.ts +10 -0
  23. package/dist/cjs/http-client.js +62 -0
  24. package/dist/cjs/index.d.ts +6 -0
  25. package/dist/cjs/index.js +7 -0
  26. package/dist/cjs/sdk-ts-clean.d.ts +108 -0
  27. package/dist/cjs/sdk-ts-clean.js +294 -0
  28. package/dist/cjs/sdk-ts.d.ts +71 -0
  29. package/dist/cjs/sdk-ts.js +227 -0
  30. package/dist/cjs/services/http.service.d.ts +41 -0
  31. package/dist/cjs/services/http.service.js +178 -0
  32. package/dist/cjs/services/mqtt.service.d.ts +15 -0
  33. package/dist/cjs/services/mqtt.service.js +103 -0
  34. package/dist/cjs/services/nats.service.d.ts +78 -0
  35. package/dist/cjs/services/nats.service.js +237 -0
  36. package/dist/cjs/types/auth.models.d.ts +52 -0
  37. package/dist/cjs/types/auth.models.js +100 -0
  38. package/dist/cjs/types/recordID.models.d.ts +7 -0
  39. package/dist/cjs/types/recordID.models.js +32 -0
  40. package/dist/cjs/types/response.models.d.ts +4 -0
  41. package/dist/cjs/types/response.models.js +2 -0
  42. package/dist/cjs/types/tag.models.d.ts +4 -0
  43. package/dist/cjs/types/tag.models.js +2 -0
  44. package/dist/cjs/utils/appConfig.d.ts +5 -0
  45. package/dist/cjs/utils/appConfig.js +62 -0
  46. package/dist/cjs/websocket-client.d.ts +15 -0
  47. package/dist/cjs/websocket-client.js +96 -0
  48. package/dist/classes/auth.d.ts +18 -0
  49. package/dist/classes/auth.js +86 -0
  50. package/dist/classes/collection.d.ts +18 -0
  51. package/dist/classes/collection.js +39 -0
  52. package/dist/classes/flow.d.ts +6 -0
  53. package/dist/classes/flow.js +8 -0
  54. package/dist/classes/function.d.ts +10 -0
  55. package/dist/classes/function.js +25 -0
  56. package/dist/classes/historian.d.ts +12 -0
  57. package/dist/classes/historian.js +36 -0
  58. package/dist/classes/tag.d.ts +10 -0
  59. package/dist/classes/tag.js +21 -0
  60. package/dist/client.d.ts +13 -0
  61. package/dist/client.js +31 -0
  62. package/dist/config.d.ts +21 -0
  63. package/dist/config.js +33 -0
  64. package/dist/example/functions-file-config.d.ts +0 -0
  65. package/dist/example/functions-file-config.js +1 -0
  66. package/dist/example/functions.d.ts +1 -0
  67. package/dist/example/functions.js +16 -0
  68. package/dist/http-client.d.ts +10 -0
  69. package/dist/http-client.js +58 -0
  70. package/dist/index.d.ts +6 -0
  71. package/dist/index.js +2 -0
  72. package/dist/sdk-ts-clean.d.ts +108 -0
  73. package/dist/sdk-ts-clean.js +290 -0
  74. package/dist/sdk-ts.d.ts +71 -0
  75. package/dist/sdk-ts.js +223 -0
  76. package/dist/services/http.service.d.ts +41 -0
  77. package/dist/services/http.service.js +173 -0
  78. package/dist/services/mqtt.service.d.ts +15 -0
  79. package/dist/services/mqtt.service.js +96 -0
  80. package/dist/services/nats.service.d.ts +78 -0
  81. package/dist/services/nats.service.js +233 -0
  82. package/dist/types/auth.models.d.ts +52 -0
  83. package/dist/types/auth.models.js +97 -0
  84. package/dist/types/recordID.models.d.ts +7 -0
  85. package/dist/types/recordID.models.js +27 -0
  86. package/dist/types/response.models.d.ts +4 -0
  87. package/dist/types/response.models.js +1 -0
  88. package/dist/types/tag.models.d.ts +4 -0
  89. package/dist/types/tag.models.js +1 -0
  90. package/dist/utils/appConfig.d.ts +5 -0
  91. package/dist/utils/appConfig.js +26 -0
  92. package/dist/websocket-client.d.ts +15 -0
  93. package/dist/websocket-client.js +92 -0
  94. package/package.json +33 -0
  95. package/src/classes/auth.ts +103 -0
  96. package/src/classes/collection.ts +55 -0
  97. package/src/classes/flow.ts +13 -0
  98. package/src/classes/function.ts +34 -0
  99. package/src/classes/historian.ts +49 -0
  100. package/src/classes/tag.ts +30 -0
  101. package/src/example/functions.ts +21 -0
  102. package/src/index.ts +8 -0
  103. package/src/sdk-ts.ts +255 -0
  104. package/src/services/http.service.ts +239 -0
  105. package/src/services/mqtt.service.ts +114 -0
  106. package/src/services/nats.service.ts +262 -0
  107. package/src/types/auth.models.ts +157 -0
  108. package/src/types/recordID.models.ts +33 -0
  109. package/src/types/response.models.ts +4 -0
  110. package/src/types/tag.models.ts +4 -0
  111. package/src/utils/appConfig.ts +30 -0
  112. package/tsconfig.json +14 -0
@@ -0,0 +1,52 @@
1
+ import { RecordID } from "./recordID.models";
2
+ import { BaseResponse } from "./response.models";
3
+ export interface LoginResponse extends BaseResponse {
4
+ tkn: string;
5
+ }
6
+ export interface PermissionResponse extends BaseResponse {
7
+ permission: boolean;
8
+ }
9
+ export interface ValidateJWTResponse extends BaseResponse {
10
+ valid: boolean;
11
+ }
12
+ export interface User {
13
+ id?: RecordID;
14
+ firstName: string;
15
+ lastName: string;
16
+ username: string;
17
+ email: string;
18
+ password: string;
19
+ number: string;
20
+ createdDt?: string;
21
+ userImage: string | ArrayBuffer | null;
22
+ group_ids?: string[];
23
+ }
24
+ export interface Group {
25
+ id?: RecordID;
26
+ features?: {
27
+ name: string;
28
+ action: string;
29
+ domain: string;
30
+ scope: string;
31
+ }[];
32
+ name: string;
33
+ user_ids: RecordID[];
34
+ }
35
+ export interface Feature {
36
+ name: string;
37
+ action: string;
38
+ scope: Scope;
39
+ }
40
+ export interface Permission {
41
+ displayName: string;
42
+ description: string;
43
+ name: string;
44
+ action: string;
45
+ scope: string;
46
+ }
47
+ export declare function machhubPermissions(): Permission[];
48
+ export interface ActionResponse extends BaseResponse {
49
+ action: "read" | "read-write" | "";
50
+ }
51
+ export type Action = "read" | "read-write";
52
+ export type Scope = "self" | "domain" | "all" | "nil";
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.machhubPermissions = machhubPermissions;
4
+ ;
5
+ ;
6
+ function machhubPermissions() {
7
+ return [
8
+ {
9
+ displayName: 'Applications',
10
+ description: 'View or manage applications all applications in MACHHUB',
11
+ name: 'applications',
12
+ action: 'nil',
13
+ scope: 'all'
14
+ },
15
+ {
16
+ displayName: 'Users',
17
+ description: 'View or manage user accounts in this domain',
18
+ name: 'users',
19
+ action: 'nil',
20
+ scope: 'domain'
21
+ },
22
+ {
23
+ displayName: 'Groups',
24
+ description: 'View or manage user groups in this domain',
25
+ name: 'groups',
26
+ action: 'nil',
27
+ scope: 'domain'
28
+ },
29
+ {
30
+ displayName: 'Manage Own API Keys',
31
+ description: 'View or manage API keys for own account',
32
+ name: 'api_keys',
33
+ action: 'nil',
34
+ scope: 'self'
35
+ },
36
+ {
37
+ displayName: 'Manage All API Keys',
38
+ description: 'View or manage all API keys in MACHHUB',
39
+ name: 'api_keys',
40
+ action: 'nil',
41
+ scope: 'all'
42
+ },
43
+ {
44
+ displayName: 'Upstreams',
45
+ description: 'View or manage all upstream configurations in MACHHUB',
46
+ name: 'upstreams',
47
+ action: 'nil',
48
+ scope: 'domain'
49
+ },
50
+ {
51
+ displayName: 'Manage Namespace',
52
+ description: 'View or manage namespaces in this domain',
53
+ name: 'namespace',
54
+ action: 'nil',
55
+ scope: 'domain'
56
+ },
57
+ {
58
+ displayName: 'Historian',
59
+ description: 'View or manage historian data in this domain',
60
+ name: 'historian',
61
+ action: 'nil',
62
+ scope: 'domain'
63
+ },
64
+ {
65
+ displayName: 'Collections',
66
+ description: 'View or manage collections in this domain',
67
+ name: 'collections',
68
+ action: 'nil',
69
+ scope: 'domain'
70
+ },
71
+ {
72
+ displayName: 'Logs',
73
+ description: 'View or manage system logs',
74
+ name: 'logs',
75
+ action: 'nil',
76
+ scope: 'all'
77
+ },
78
+ {
79
+ displayName: 'General Settings',
80
+ description: 'View or manage general system settings',
81
+ name: 'general_settings',
82
+ action: 'nil',
83
+ scope: 'all'
84
+ },
85
+ {
86
+ displayName: 'Gateway Settings',
87
+ description: 'View or manage gateway settings',
88
+ name: 'gateway',
89
+ action: 'nil',
90
+ scope: 'all'
91
+ },
92
+ {
93
+ displayName: 'License',
94
+ description: 'View or manage MACHHUB licensing details',
95
+ name: 'license',
96
+ action: 'nil',
97
+ scope: 'all'
98
+ }
99
+ ];
100
+ }
@@ -0,0 +1,7 @@
1
+ export interface RecordID {
2
+ Table: string;
3
+ ID: string;
4
+ }
5
+ export declare function StringToRecordID(id: string): RecordID;
6
+ export declare function RecordIDToString(recordID: RecordID | undefined): string;
7
+ export declare function emptyRecordID(): RecordID;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StringToRecordID = StringToRecordID;
4
+ exports.RecordIDToString = RecordIDToString;
5
+ exports.emptyRecordID = emptyRecordID;
6
+ function StringToRecordID(id) {
7
+ const splitID = id.split(":");
8
+ if (splitID.length != 2)
9
+ return emptyRecordID();
10
+ return {
11
+ Table: containsSpace(splitID[0]) ? `(${splitID[0]})` : splitID[0],
12
+ ID: splitID[1]
13
+ };
14
+ }
15
+ function RecordIDToString(recordID) {
16
+ if (recordID != undefined) {
17
+ return recordID.Table + ":" + recordID.ID;
18
+ }
19
+ else {
20
+ return "";
21
+ }
22
+ }
23
+ function emptyRecordID() {
24
+ return {
25
+ Table: "",
26
+ ID: "",
27
+ };
28
+ }
29
+ // Helper function to check if a string contains spaces
30
+ function containsSpace(s) {
31
+ return s.includes(" ");
32
+ }
@@ -0,0 +1,4 @@
1
+ export interface BaseResponse {
2
+ success: boolean;
3
+ message: boolean;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ export interface HistorizedData {
2
+ timestamp: string;
3
+ value: unknown;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ interface AppConfig {
2
+ application_id: string;
3
+ }
4
+ export declare function getAppConfig(): AppConfig;
5
+ export {};
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.getAppConfig = getAppConfig;
37
+ const fs = __importStar(require("fs"));
38
+ const path = __importStar(require("path"));
39
+ function getAppConfig() {
40
+ console.log("getAppConfig");
41
+ if (typeof process !== 'undefined' && process.env && process.env.APP_CONFIG_PATH) {
42
+ // Node.js environment
43
+ console.log(process.env);
44
+ console.log(process.env.APP_CONFIG_PATH);
45
+ const configPath = process.env.APP_CONFIG_PATH || path.resolve(__dirname, '../../../sdk.config.json');
46
+ const rawData = fs.readFileSync(configPath, 'utf-8');
47
+ return JSON.parse(rawData);
48
+ }
49
+ else {
50
+ try {
51
+ const configPath = path.resolve('../../../sdk.config.json');
52
+ console.log(configPath);
53
+ const rawData = fs.readFileSync(configPath, 'utf-8');
54
+ console.log("TEST");
55
+ console.log(rawData);
56
+ return JSON.parse(rawData);
57
+ }
58
+ catch (e) {
59
+ throw new Error("Configuration not found. Set it via the APP_CONFIG_PATH environment variable : " + e);
60
+ }
61
+ }
62
+ }
@@ -0,0 +1,15 @@
1
+ import { ConfigManager } from './config';
2
+ export declare class WebSocketClient {
3
+ private configManager;
4
+ private ws;
5
+ private reconnectAttempts;
6
+ private maxReconnectAttempts;
7
+ private reconnectDelay;
8
+ constructor(configManager: ConfigManager);
9
+ updateConfig(): void;
10
+ connect(): Promise<void>;
11
+ disconnect(): void;
12
+ send(data: any): void;
13
+ private handleMessage;
14
+ private handleReconnect;
15
+ }
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebSocketClient = void 0;
4
+ class WebSocketClient {
5
+ constructor(configManager) {
6
+ this.ws = null;
7
+ this.reconnectAttempts = 0;
8
+ this.maxReconnectAttempts = 5;
9
+ this.reconnectDelay = 1000;
10
+ this.configManager = configManager;
11
+ }
12
+ updateConfig() {
13
+ // If connected, reconnect with new config
14
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {
15
+ this.disconnect();
16
+ this.connect();
17
+ }
18
+ }
19
+ connect() {
20
+ return new Promise((resolve, reject) => {
21
+ const config = this.configManager.getConfig();
22
+ const wsUrl = config.wsUrl;
23
+ if (!wsUrl) {
24
+ reject(new Error('WebSocket URL not configured'));
25
+ return;
26
+ }
27
+ try {
28
+ this.ws = new WebSocket(wsUrl);
29
+ this.ws.onopen = () => {
30
+ console.log('WebSocket connected');
31
+ this.reconnectAttempts = 0;
32
+ // Send runtime ID if available
33
+ if (config.runtimeId) {
34
+ this.send({ type: 'auth', runtimeId: config.runtimeId });
35
+ }
36
+ resolve();
37
+ };
38
+ this.ws.onmessage = (event) => {
39
+ try {
40
+ const data = JSON.parse(event.data);
41
+ this.handleMessage(data);
42
+ }
43
+ catch (error) {
44
+ console.error('Failed to parse WebSocket message:', error);
45
+ }
46
+ };
47
+ this.ws.onclose = (event) => {
48
+ console.log('WebSocket disconnected:', event.code, event.reason);
49
+ this.handleReconnect();
50
+ };
51
+ this.ws.onerror = (error) => {
52
+ console.error('WebSocket error:', error);
53
+ reject(error);
54
+ };
55
+ }
56
+ catch (error) {
57
+ reject(error);
58
+ }
59
+ });
60
+ }
61
+ disconnect() {
62
+ if (this.ws) {
63
+ this.ws.close();
64
+ this.ws = null;
65
+ }
66
+ this.reconnectAttempts = 0;
67
+ }
68
+ send(data) {
69
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {
70
+ this.ws.send(JSON.stringify(data));
71
+ }
72
+ else {
73
+ console.warn('WebSocket not connected, cannot send message');
74
+ }
75
+ }
76
+ handleMessage(data) {
77
+ // Handle incoming messages
78
+ console.log('WebSocket message received:', data);
79
+ }
80
+ handleReconnect() {
81
+ if (this.reconnectAttempts < this.maxReconnectAttempts) {
82
+ this.reconnectAttempts++;
83
+ const delay = this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1);
84
+ console.log(`Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`);
85
+ setTimeout(() => {
86
+ this.connect().catch(error => {
87
+ console.error('Reconnection failed:', error);
88
+ });
89
+ }, delay);
90
+ }
91
+ else {
92
+ console.error('Max reconnection attempts reached');
93
+ }
94
+ }
95
+ }
96
+ exports.WebSocketClient = WebSocketClient;
@@ -0,0 +1,18 @@
1
+ import { HTTPService } from "../services/http.service";
2
+ import { Action, ActionResponse, Feature, Group, LoginResponse, User, ValidateJWTResponse } from "../types/auth.models";
3
+ export declare class Auth {
4
+ private httpService;
5
+ constructor(httpService: HTTPService);
6
+ login(username: string, password: string): Promise<LoginResponse | undefined>;
7
+ validateJWT(token: string): Promise<ValidateJWTResponse>;
8
+ logout(): Promise<void>;
9
+ checkAction(feature: string, scope: string): Promise<ActionResponse>;
10
+ checkPermission(feature: string, scope: string, action: Action): Promise<ActionResponse>;
11
+ getUsers(): Promise<User[]>;
12
+ getUserById(userId: string): Promise<User>;
13
+ createUser(firstName: string, lastName: string, username: string, email: string, password: string, number: string, userImage: string): Promise<User>;
14
+ getGroups(): Promise<Group[]>;
15
+ createGroup(name: string, features: Feature[]): Promise<Group>;
16
+ addUserToGroup(userId: string, groupId: string): Promise<ActionResponse>;
17
+ addPermissionsToGroup(group_id: string, permissions: Feature[]): Promise<ActionResponse>;
18
+ }
@@ -0,0 +1,86 @@
1
+ export class Auth {
2
+ constructor(httpService) {
3
+ this.httpService = httpService;
4
+ }
5
+ async login(username, password) {
6
+ let res;
7
+ try {
8
+ res = await this.httpService.request.withJSON({
9
+ username: username,
10
+ password: password,
11
+ }).post("/auth/login");
12
+ if (localStorage) {
13
+ localStorage.setItem("x-machhub-auth-tkn", res.tkn); // Set User JWT
14
+ }
15
+ else {
16
+ console.error("localStorage is not available. The program needs to be in a browser environment.");
17
+ }
18
+ return res;
19
+ }
20
+ catch (e) {
21
+ if (e.message == "localStorage is not defined") {
22
+ throw new Error("Login failed: localStorage is not available. The program needs to be in a browser environment.");
23
+ }
24
+ throw new Error("Login failed: " + e.message);
25
+ }
26
+ }
27
+ async validateJWT(token) {
28
+ let res = await this.httpService.request.withJSON({ token }).post("/auth/jwt/validate");
29
+ return res;
30
+ }
31
+ async logout() {
32
+ localStorage.removeItem("x-machhub-auth-tkn"); // Remove User JWT
33
+ }
34
+ async checkAction(feature, scope) {
35
+ try {
36
+ const res = await this.httpService.request.get(`/auth/permission/action/feature/${feature}/scope/${scope}`);
37
+ return res;
38
+ }
39
+ catch (e) {
40
+ throw new Error("failed to checkAction : " + e.message);
41
+ }
42
+ }
43
+ async checkPermission(feature, scope, action) {
44
+ try {
45
+ const res = await this.httpService.request.get(`/auth/permission/check/feature/${feature}/scope/${scope}/action/${action}`);
46
+ return res;
47
+ }
48
+ catch (e) {
49
+ throw new Error("failed to checkPermission : " + e.message);
50
+ }
51
+ }
52
+ async getUsers() {
53
+ return await this.httpService.request.get("/auth/user");
54
+ }
55
+ async getUserById(userId) {
56
+ return await this.httpService.request.get(`/auth/user/${userId}`);
57
+ }
58
+ async createUser(firstName, lastName, username, email, password, number, userImage) {
59
+ return await this.httpService.request.withJSON({
60
+ firstName: firstName,
61
+ lastName: lastName,
62
+ username: username,
63
+ email: email,
64
+ password: password,
65
+ number: number,
66
+ userImage: userImage
67
+ }).post("/auth/user");
68
+ }
69
+ async getGroups() {
70
+ return await this.httpService.request.get("/auth/group");
71
+ }
72
+ async createGroup(name, features) {
73
+ return await this.httpService.request.withJSON({
74
+ name: name,
75
+ features: features
76
+ }).post("/auth/group");
77
+ }
78
+ async addUserToGroup(userId, groupId) {
79
+ return await this.httpService.request.post(`/auth/group/${groupId}/user/${userId}`);
80
+ }
81
+ async addPermissionsToGroup(group_id, permissions) {
82
+ return await this.httpService.request.withJSON({
83
+ group_id, permissions
84
+ }).post("/auth/permission");
85
+ }
86
+ }
@@ -0,0 +1,18 @@
1
+ import { HTTPService } from "../services/http.service";
2
+ import { MQTTService } from "../services/mqtt.service";
3
+ export declare class Collection {
4
+ protected httpService: HTTPService;
5
+ protected mqttService: MQTTService | null;
6
+ protected collectionName: string;
7
+ protected queryParams: Record<string, any>;
8
+ constructor(httpService: HTTPService, mqttService: MQTTService | null, collectionName: string);
9
+ filter(fieldName: string, operator: "=" | ">" | "<" | "<=" | ">=" | "!=", value: any): Collection;
10
+ sort(field: string, direction?: "asc" | "desc"): Collection;
11
+ limit(limit: number): Collection;
12
+ offset(offset: number): Collection;
13
+ getAll(): Promise<any[]>;
14
+ getOne(id: string): Promise<any>;
15
+ create(data: Record<string, any>): Promise<any>;
16
+ update(id: string, data: Record<string, any>): Promise<any>;
17
+ delete(id: string): Promise<any>;
18
+ }
@@ -0,0 +1,39 @@
1
+ export class Collection {
2
+ constructor(httpService, mqttService, collectionName) {
3
+ this.queryParams = {};
4
+ this.httpService = httpService;
5
+ this.mqttService = mqttService;
6
+ this.collectionName = collectionName;
7
+ }
8
+ filter(fieldName, operator, value) {
9
+ this.queryParams[`filter[${fieldName}][${operator}][${typeof value}]`] = value;
10
+ return this;
11
+ }
12
+ sort(field, direction = "asc") {
13
+ this.queryParams.sort = `[${field}][${direction}]`;
14
+ return this;
15
+ }
16
+ limit(limit) {
17
+ this.queryParams.limit = limit;
18
+ return this;
19
+ }
20
+ offset(offset) {
21
+ this.queryParams.offset = offset;
22
+ return this;
23
+ }
24
+ async getAll() {
25
+ return this.httpService.request.get(this.collectionName + "/all", this.queryParams);
26
+ }
27
+ async getOne(id) {
28
+ return this.httpService.request.get(id);
29
+ }
30
+ async create(data) {
31
+ return this.httpService.request.withJSON(data).post(this.collectionName);
32
+ }
33
+ async update(id, data) {
34
+ return this.httpService.request.withJSON(data).put(id);
35
+ }
36
+ async delete(id) {
37
+ return this.httpService.request.delete(id);
38
+ }
39
+ }
@@ -0,0 +1,6 @@
1
+ import { HTTPService } from "../services/http.service";
2
+ export declare class Flow {
3
+ private httpService;
4
+ constructor(httpService: HTTPService);
5
+ executeFlow(name: string, payload: any): Promise<any>;
6
+ }
@@ -0,0 +1,8 @@
1
+ export class Flow {
2
+ constructor(httpService) {
3
+ this.httpService = httpService;
4
+ }
5
+ async executeFlow(name, payload) {
6
+ return await this.httpService.request.withJSON(payload).post("flow/execute/name/" + name);
7
+ }
8
+ }
@@ -0,0 +1,10 @@
1
+ import { HTTPService } from "../services/http.service";
2
+ import { NATSService } from "../services/nats.service";
3
+ export declare class Function {
4
+ private httpService;
5
+ private natsService;
6
+ constructor(httpService: HTTPService, natsService: NATSService);
7
+ executeFunction(function_type: string, function_name: string, payload: any): Promise<any>;
8
+ addFunction(name: string, func: (data: Record<string, any>) => Record<string, any>): Promise<any>;
9
+ initializeFunctions(): Promise<any>;
10
+ }
@@ -0,0 +1,25 @@
1
+ export class Function {
2
+ constructor(httpService, natsService) {
3
+ this.httpService = httpService;
4
+ this.natsService = natsService;
5
+ }
6
+ async executeFunction(function_type, function_name, payload) {
7
+ return await this.httpService.request.withJSON({
8
+ function_type: function_type,
9
+ function_name: function_name,
10
+ payload: payload,
11
+ }).post("function/execute");
12
+ }
13
+ async addFunction(name, func) {
14
+ if (!this.natsService) {
15
+ throw new Error("NATS service not connected");
16
+ }
17
+ return this.natsService.addFunction(name, func);
18
+ }
19
+ async initializeFunctions() {
20
+ if (!this.natsService) {
21
+ throw new Error("NATS service not connected");
22
+ }
23
+ return this.natsService.initializeFunctions();
24
+ }
25
+ }
@@ -0,0 +1,12 @@
1
+ import { HTTPService } from "../services/http.service";
2
+ import { MQTTService } from "../services/mqtt.service";
3
+ import { HistorizedData } from "../types/tag.models";
4
+ export declare class Historian {
5
+ private httpService;
6
+ private mqttService;
7
+ constructor(httpService: HTTPService, mqttService: MQTTService | null);
8
+ getAllHistorizedTags(): Promise<string[]>;
9
+ getHistoricalData(topic: string, start_time: Date, range?: string): Promise<HistorizedData[]>;
10
+ subscribeLiveData(topic: string, callback: (data: any) => void): Promise<any>;
11
+ getLastNValues(topic: string, n: number): Promise<HistorizedData[]>;
12
+ }
@@ -0,0 +1,36 @@
1
+ export class Historian {
2
+ constructor(httpService, mqttService) {
3
+ this.httpService = httpService;
4
+ this.mqttService = mqttService;
5
+ }
6
+ async getAllHistorizedTags() {
7
+ return this.httpService.request.get("historian/list");
8
+ }
9
+ async getHistoricalData(topic, start_time, range) {
10
+ let isoStartTime = start_time.toISOString();
11
+ return this.httpService.request.withJSON({
12
+ topic: topic,
13
+ start_time: isoStartTime,
14
+ range: range,
15
+ }).patch("historian");
16
+ }
17
+ async subscribeLiveData(topic, callback) {
18
+ if (!this.mqttService) {
19
+ throw new Error("MQTT service not connected");
20
+ }
21
+ this.mqttService.addTopicHandler(topic, callback);
22
+ }
23
+ async getLastNValues(topic, n) {
24
+ if (n <= 0) {
25
+ throw new Error("The number of values to fetch must be greater than 0.");
26
+ }
27
+ if (n > 100) {
28
+ throw new Error("The number of values to fetch must be less than 100.");
29
+ }
30
+ return this.httpService.request.withJSON({
31
+ topic: topic,
32
+ limit: n,
33
+ sort: "desc", // Fetch the latest values
34
+ }).patch("historian/last");
35
+ }
36
+ }