dt-common-device 3.0.10 → 3.0.12

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 (145) hide show
  1. package/dist/audit/AuditProperties.d.ts +16 -0
  2. package/dist/audit/AuditUtils.d.ts +2 -0
  3. package/dist/audit/AuditUtils.js +36 -0
  4. package/dist/config/config.d.ts +1 -2
  5. package/dist/config/config.js +4 -5
  6. package/dist/constants/Service.d.ts +1 -0
  7. package/dist/device/local/events/EventHandler.js +6 -6
  8. package/dist/device/local/events/Events.d.ts +33 -12
  9. package/dist/device/local/events/Events.js +33 -12
  10. package/dist/device/local/repository/Schedule.repository.d.ts +0 -1
  11. package/dist/device/local/repository/Schedule.repository.js +6 -6
  12. package/dist/queue/entities/HybridHttpQueue.d.ts +4 -14
  13. package/dist/queue/entities/HybridHttpQueue.js +31 -119
  14. package/dist/queue/interfaces/IHybridHttpQueue.d.ts +2 -12
  15. package/dist/queue/interfaces/IJobResult.d.ts +1 -8
  16. package/dist/queue/interfaces/index.d.ts +0 -1
  17. package/dist/queue/interfaces/index.js +0 -1
  18. package/dist/queue/services/QueueService.d.ts +2 -12
  19. package/dist/queue/types/http.types.d.ts +6 -4
  20. package/dist/queue/types/queue.types.d.ts +12 -29
  21. package/dist/queue/utils/jobUtils.d.ts +2 -4
  22. package/dist/queue/utils/jobUtils.js +0 -48
  23. package/dist/queue/utils/queueUtils.d.ts +7 -0
  24. package/dist/queue/utils/queueUtils.js +114 -4
  25. package/dist/queue/utils/rateLimit.utils.d.ts +1 -1
  26. package/dist/queue/utils/rateLimit.utils.js +38 -2
  27. package/package.json +6 -1
  28. package/.eslintrc.js +0 -44
  29. package/dist/device/cloud/interface.d.ts +0 -101
  30. package/dist/device/cloud/interface.js +0 -3
  31. package/dist/device/cloud/interfaces/IDeviceConnectionService.d.ts +0 -7
  32. package/dist/device/cloud/interfaces/IDeviceConnectionService.js +0 -3
  33. package/dist/device/cloud/interfaces/IDevicesService.d.ts +0 -9
  34. package/dist/device/cloud/services/Device.service.d.ts +0 -39
  35. package/dist/device/cloud/services/Device.service.js +0 -9
  36. package/dist/device/cloud/services/DeviceCloudService.d.ts +0 -42
  37. package/dist/device/cloud/services/DeviceCloudService.js +0 -59
  38. package/dist/device/cloud/services/DeviceHub.service.d.ts +0 -3
  39. package/dist/device/cloud/services/DeviceHub.service.js +0 -6
  40. package/dist/device/cloud/services/Hub.service.d.ts +0 -25
  41. package/dist/device/cloud/services/Hub.service.js +0 -9
  42. package/dist/device/cloud/services/SmartThingsDeviceService.d.ts +0 -38
  43. package/dist/device/cloud/services/SmartThingsDeviceService.js +0 -52
  44. package/dist/device/index.d.ts +0 -4
  45. package/dist/device/index.js +0 -20
  46. package/dist/device/local/interface.d.ts +0 -0
  47. package/dist/device/local/interface.js +0 -1
  48. package/dist/device/local/services/DeviceHub.service.d.ts +0 -11
  49. package/dist/device/local/services/DeviceHub.service.js +0 -40
  50. package/src/alerts/Alert.model.ts +0 -289
  51. package/src/alerts/Alert.repository.ts +0 -487
  52. package/src/alerts/Alert.service.ts +0 -711
  53. package/src/alerts/AlertBuilder.example.ts +0 -126
  54. package/src/alerts/AlertBuilder.ts +0 -208
  55. package/src/alerts/AlertService.example.ts +0 -232
  56. package/src/alerts/alert.types.ts +0 -64
  57. package/src/alerts/index.ts +0 -3
  58. package/src/config/config.ts +0 -202
  59. package/src/config/config.types.ts +0 -21
  60. package/src/connection/Connection.repository.ts +0 -52
  61. package/src/connection/Connection.service.ts +0 -39
  62. package/src/connection/IConnection.ts +0 -27
  63. package/src/connection/index.ts +0 -3
  64. package/src/constants/ConnectionProviders.ts +0 -11
  65. package/src/constants/Event.ts +0 -89
  66. package/src/constants/Service.ts +0 -17
  67. package/src/constants/index.ts +0 -3
  68. package/src/db/db.ts +0 -24
  69. package/src/db/index.ts +0 -2
  70. package/src/db/redis.ts +0 -20
  71. package/src/device/cloud/entities/CloudDevice.ts +0 -40
  72. package/src/device/cloud/entities/CloudDeviceService.ts +0 -8
  73. package/src/device/cloud/entities/DeviceFactory.ts +0 -27
  74. package/src/device/cloud/entities/index.ts +0 -3
  75. package/src/device/cloud/interfaces/ICloudDevice.ts +0 -14
  76. package/src/device/cloud/interfaces/ICloudDeviceService.ts +0 -6
  77. package/src/device/cloud/interfaces/IDeviceFactory.ts +0 -5
  78. package/src/device/cloud/interfaces/IRawDataTransformer.ts +0 -5
  79. package/src/device/cloud/interfaces/IRawDevice.ts +0 -19
  80. package/src/device/cloud/interfaces/index.ts +0 -5
  81. package/src/device/local/interfaces/IDevice.ts +0 -62
  82. package/src/device/local/interfaces/IDtDevice.ts +0 -16
  83. package/src/device/local/interfaces/ISchedule.ts +0 -40
  84. package/src/device/local/interfaces/index.ts +0 -3
  85. package/src/device/local/repository/Device.repository.ts +0 -368
  86. package/src/device/local/repository/Hub.repository.ts +0 -107
  87. package/src/device/local/repository/Schedule.repository.ts +0 -72
  88. package/src/device/local/services/Device.service.ts +0 -436
  89. package/src/device/local/services/Hub.service.ts +0 -57
  90. package/src/device/local/services/Schedule.service.ts +0 -26
  91. package/src/device/local/services/index.ts +0 -3
  92. package/src/docs/Alert.model.md +0 -319
  93. package/src/docs/Alerts&IssuesModel.md +0 -312
  94. package/src/docs/Issue.model.md +0 -386
  95. package/src/docs/SECURITY.md +0 -67
  96. package/src/docs/TROUBLESHOOTING.md +0 -184
  97. package/src/events/BaseEventHandler.ts +0 -145
  98. package/src/events/BaseEventTransformer.ts +0 -97
  99. package/src/events/DeviceEventHandler.ts +0 -213
  100. package/src/events/DeviceEventTransformerFactory.ts +0 -77
  101. package/src/events/EventHandler.ts +0 -124
  102. package/src/events/EventHandlerOrchestrator.ts +0 -119
  103. package/src/events/EventProcessingService.ts +0 -248
  104. package/src/events/InternalEventSubscription.ts +0 -194
  105. package/src/events/index.ts +0 -9
  106. package/src/events/interfaces/DeviceEvent.ts +0 -56
  107. package/src/events/interfaces/IEventHandler.ts +0 -28
  108. package/src/events/interfaces/IEventTransformer.ts +0 -8
  109. package/src/events/interfaces/IInternalEvent.ts +0 -33
  110. package/src/events/interfaces/index.ts +0 -4
  111. package/src/index.ts +0 -43
  112. package/src/issues/Issue.model.ts +0 -350
  113. package/src/issues/Issue.repository.ts +0 -517
  114. package/src/issues/Issue.service.ts +0 -932
  115. package/src/issues/IssueBuilder.example.ts +0 -210
  116. package/src/issues/IssueBuilder.ts +0 -263
  117. package/src/issues/IssueService.example.ts +0 -310
  118. package/src/issues/index.ts +0 -2
  119. package/src/issues/issue.types.ts +0 -98
  120. package/src/property/IProperty.ts +0 -30
  121. package/src/property/Property.repository.ts +0 -53
  122. package/src/property/Property.service.ts +0 -38
  123. package/src/property/index.ts +0 -2
  124. package/src/queue/entities/HybridHttpQueue.ts +0 -274
  125. package/src/queue/entities/index.ts +0 -1
  126. package/src/queue/index.ts +0 -6
  127. package/src/queue/interfaces/IHttpRequestJob.ts +0 -10
  128. package/src/queue/interfaces/IHybridHttpQueue.ts +0 -25
  129. package/src/queue/interfaces/IJobResult.ts +0 -15
  130. package/src/queue/interfaces/IRateLimitConfig.ts +0 -5
  131. package/src/queue/interfaces/index.ts +0 -4
  132. package/src/queue/services/QueueService.ts +0 -40
  133. package/src/queue/services/index.ts +0 -1
  134. package/src/queue/types/http.types.ts +0 -23
  135. package/src/queue/types/index.ts +0 -2
  136. package/src/queue/types/queue.types.ts +0 -21
  137. package/src/queue/utils/index.ts +0 -3
  138. package/src/queue/utils/jobUtils.ts +0 -79
  139. package/src/queue/utils/queueUtils.ts +0 -84
  140. package/src/queue/utils/rateLimit.utils.ts +0 -131
  141. package/src/utils/http.utils.ts +0 -143
  142. package/src/utils/index.ts +0 -2
  143. package/src/utils/redis.utils.ts +0 -74
  144. package/tsconfig.json +0 -20
  145. /package/dist/{device/cloud/interfaces/IDevicesService.js → audit/AuditProperties.js} +0 -0
@@ -1,131 +0,0 @@
1
- import { IRateLimitConfig } from "../interfaces";
2
- import { getRedisClient } from "../../db/redis";
3
- import { getConfig } from "../../config/config";
4
-
5
- export class RateLimitUtils {
6
- private static redisClient = getRedisClient();
7
-
8
- static async checkRateLimit(
9
- connectionId: string,
10
- provider: string,
11
- rateLimitConfigs: Map<string, IRateLimitConfig>
12
- ): Promise<boolean> {
13
- const config = rateLimitConfigs.get(provider);
14
- if (!config) {
15
- getConfig().LOGGER.warn(
16
- `No rate limit config found for provider: ${provider}`
17
- );
18
- return true;
19
- }
20
-
21
- const key = `rate_limit:${provider}:${connectionId}`;
22
- const now = Date.now();
23
- const windowStart = now - config.windowMs;
24
-
25
- try {
26
- const data = await this.redisClient.get(key);
27
- const requests = data
28
- ? JSON.parse(data).filter((t: number) => t > windowStart)
29
- : [];
30
-
31
- if (requests.length >= config.maxRequests) return false;
32
-
33
- requests.push(now);
34
- await this.redisClient.setex(
35
- key,
36
- Math.ceil(config.windowMs / 1000),
37
- JSON.stringify(requests)
38
- );
39
- return true;
40
- } catch (error) {
41
- getConfig().LOGGER.error(`Rate limit check error: ${error}`);
42
- return true;
43
- }
44
- }
45
-
46
- static initializeRateLimitConfigs(): Map<string, IRateLimitConfig> {
47
- const configs = new Map<string, IRateLimitConfig>();
48
-
49
- // Configure rate limits for different providers
50
- configs.set("Sensibo", {
51
- maxRequests: 5,
52
- windowMs: 60000,
53
- provider: "Sensibo",
54
- });
55
-
56
- return configs;
57
- }
58
-
59
- static async isRateLimitAllowed(
60
- connectionId: string,
61
- provider: string,
62
- rateLimitConfigs: Map<string, IRateLimitConfig>
63
- ): Promise<boolean> {
64
- const config = rateLimitConfigs.get(provider);
65
- if (!config) {
66
- getConfig().LOGGER.warn(
67
- `No rate limit config found for provider: ${provider}`
68
- );
69
- return true;
70
- }
71
-
72
- const key = `rate_limit:${provider}:${connectionId}`;
73
- const now = Date.now();
74
- const windowStart = now - config.windowMs;
75
-
76
- try {
77
- const data = await this.redisClient.get(key);
78
- const requests = data
79
- ? JSON.parse(data).filter((t: number) => t > windowStart)
80
- : [];
81
-
82
- return requests.length < config.maxRequests;
83
- } catch (error) {
84
- getConfig().LOGGER.error(`Rate limit check error: ${error}`);
85
- return true;
86
- }
87
- }
88
- static async recordRequest(
89
- connectionId: string,
90
- provider: string
91
- ): Promise<void> {
92
- const config = this.getRateLimitConfig(provider);
93
- if (!config) return;
94
-
95
- const key = `rate_limit:${provider}:${connectionId}`;
96
- const now = Date.now();
97
- const windowStart = now - config.windowMs;
98
-
99
- try {
100
- const data = await this.redisClient.get(key);
101
- const requests = data
102
- ? JSON.parse(data).filter((t: number) => t > windowStart)
103
- : [];
104
-
105
- requests.push(now);
106
- await this.redisClient.setex(
107
- key,
108
- Math.ceil(config.windowMs / 1000),
109
- JSON.stringify(requests)
110
- );
111
- } catch (error) {
112
- getConfig().LOGGER.error(`Rate limit record error: ${error}`);
113
- }
114
- }
115
-
116
- static async getRawRequestTimestamps(key: string): Promise<number[]> {
117
- try {
118
- const data = await this.redisClient.get(key);
119
- return data ? JSON.parse(data) : [];
120
- } catch (error) {
121
- getConfig().LOGGER.error(
122
- `Error fetching raw request timestamps: ${error}`
123
- );
124
- return [];
125
- }
126
- }
127
-
128
- static getRateLimitConfig(provider: string): IRateLimitConfig | undefined {
129
- return this.initializeRateLimitConfigs().get(provider);
130
- }
131
- }
@@ -1,143 +0,0 @@
1
- import { getConfig } from "../config/config";
2
- import axios from "axios";
3
-
4
- /**
5
- * Validates if a URL is properly formatted and accessible
6
- */
7
- export function validateServiceUrl(url: string): boolean {
8
- try {
9
- const parsedUrl = new URL(url);
10
- return parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:";
11
- } catch (error) {
12
- getConfig().LOGGER.error(`Invalid service URL: ${url}`, error);
13
- return false;
14
- }
15
- }
16
-
17
- /**
18
- * Creates a properly configured axios instance with error handling
19
- */
20
- export function createAxiosInstance(baseURL?: string) {
21
- const instance = axios.create({
22
- baseURL,
23
- timeout: 30000, // 30 seconds timeout
24
- maxRedirects: 5,
25
- validateStatus: (status: number) => status < 500, // Don't throw on 4xx errors
26
- headers: {
27
- "Content-Type": "application/json",
28
- "User-Agent": "dt-common-device/1.3.0",
29
- },
30
- });
31
-
32
- // Add request interceptor for logging
33
- instance.interceptors.request.use(
34
- (config: any) => {
35
- const logger = getConfig().LOGGER;
36
- logger.info(
37
- `Making request to: ${config.method?.toUpperCase()} ${config.url}`,
38
- {
39
- baseURL: config.baseURL,
40
- timeout: config.timeout,
41
- }
42
- );
43
- return config;
44
- },
45
- (error: any) => {
46
- getConfig().LOGGER.error("Request interceptor error:", error);
47
- return Promise.reject(error);
48
- }
49
- );
50
-
51
- // Add response interceptor for error handling
52
- instance.interceptors.response.use(
53
- (response: any) => {
54
- return response;
55
- },
56
- (error: any) => {
57
- const logger = getConfig().LOGGER;
58
- const errorInfo = {
59
- url: error.config?.url,
60
- method: error.config?.method,
61
- status: error.response?.status,
62
- statusText: error.response?.statusText,
63
- message: error.message,
64
- code: error.code,
65
- baseURL: error.config?.baseURL,
66
- };
67
-
68
- logger.error("HTTP request failed:", errorInfo);
69
-
70
- // Log additional details for network errors
71
- if (error.code === "ECONNREFUSED" || error.code === "ENOTFOUND") {
72
- logger.error("Network connectivity issue detected. Please check:", {
73
- serviceUrl: error.config?.baseURL,
74
- errorCode: error.code,
75
- errorMessage: error.message,
76
- });
77
- }
78
-
79
- return Promise.reject(error);
80
- }
81
- );
82
-
83
- return instance;
84
- }
85
-
86
- /**
87
- * Centralized axios instance for device service
88
- */
89
- let deviceServiceAxiosInstance: any = null;
90
-
91
- export function getDeviceServiceAxiosInstance(): any {
92
- if (!deviceServiceAxiosInstance) {
93
- const { DEVICE_SERVICE } = getConfig();
94
- if (!DEVICE_SERVICE) {
95
- throw new Error(
96
- "DEVICE_SERVICE is not configured. Call initialize() first with DEVICE_SERVICE."
97
- );
98
- }
99
- deviceServiceAxiosInstance = createAxiosInstance(DEVICE_SERVICE);
100
- }
101
- return deviceServiceAxiosInstance;
102
- }
103
-
104
- /**
105
- * Retry function for failed HTTP requests
106
- */
107
- export async function retryRequest<T>(
108
- requestFn: () => Promise<T>,
109
- maxRetries: number = 3,
110
- delay: number = 1000
111
- ): Promise<T> {
112
- let lastError: any;
113
-
114
- for (let attempt = 1; attempt <= maxRetries; attempt++) {
115
- try {
116
- return await requestFn();
117
- } catch (error: any) {
118
- lastError = error;
119
-
120
- if (attempt === maxRetries) {
121
- getConfig().LOGGER.error(
122
- `Request failed after ${maxRetries} attempts:`,
123
- error
124
- );
125
- throw error;
126
- }
127
-
128
- getConfig().LOGGER.warn(
129
- `Request attempt ${attempt} failed, retrying in ${delay}ms:`,
130
- {
131
- error: error.message,
132
- attempt,
133
- maxRetries,
134
- }
135
- );
136
-
137
- await new Promise((resolve) => setTimeout(resolve, delay));
138
- delay *= 2; // Exponential backoff
139
- }
140
- }
141
-
142
- throw lastError;
143
- }
@@ -1,2 +0,0 @@
1
- export * from "./redis.utils";
2
- export * from "./http.utils";
@@ -1,74 +0,0 @@
1
- import { Service } from "typedi";
2
- import { getRedisClient } from "../db/redis";
3
-
4
- @Service()
5
- export class RedisUtils {
6
- private readonly client = getRedisClient(); // singleton Redis client instance
7
-
8
- async hget(key: string, field: string): Promise<any> {
9
- try {
10
- const value: string | null = await this.client.hget(key, field);
11
- if (!value) {
12
- return null;
13
- }
14
- return JSON.parse(value);
15
- } catch (error) {
16
- console.error(`Error getting value for key ${key}:`, error);
17
- throw error;
18
- }
19
- }
20
-
21
- async hset(key: string, field: string, value: string): Promise<number> {
22
- try {
23
- return await this.client.hset(key, field, value);
24
- } catch (error) {
25
- console.error(`Error setting value for key ${key}:`, error);
26
- throw error;
27
- }
28
- }
29
-
30
- // async del(key: string): Promise<number> {
31
- // return this.client.del(key);
32
- // }
33
-
34
- async hdel(key: string, ...fields: string[]): Promise<number> {
35
- return await this.client.hdel(key, ...fields);
36
- }
37
-
38
- // async get(key: string): Promise<string | null> {
39
- // try {
40
- // return await this.client.get(key);
41
- // } catch (error) {
42
- // console.error(`Error getting value for key ${key}:`, error);
43
- // throw error;
44
- // }
45
- // }
46
-
47
- /* async set(key: string, value: string): Promise<boolean> {
48
- try {
49
- await this.client.set(key, value);
50
- return true;
51
- } catch (error) {
52
- console.error(`Error setting value for key ${key}:`, error);
53
- throw error;
54
- }
55
- } */
56
-
57
- async exists(key: string): Promise<number> {
58
- try {
59
- return await this.client.exists(key);
60
- } catch (error) {
61
- console.error(`Error checking existence for key ${key}:`, error);
62
- throw error;
63
- }
64
- }
65
-
66
- async expire(key: string, seconds: number): Promise<any> {
67
- try {
68
- return await this.client.expire(key, seconds);
69
- } catch (error) {
70
- console.error(`Error setting expiration for key ${key}:`, error);
71
- throw error;
72
- }
73
- }
74
- }
package/tsconfig.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "module": "commonjs",
5
- "lib": ["ES2020"],
6
- "declaration": true,
7
- "outDir": "./dist",
8
- "rootDir": "./src",
9
- "strict": true,
10
- "esModuleInterop": true,
11
- "skipLibCheck": true,
12
- "forceConsistentCasingInFileNames": true,
13
- "moduleResolution": "node",
14
- "resolveJsonModule": true,
15
- "isolatedModules": true,
16
- "noEmit": false
17
- },
18
- "include": ["src/**/*"],
19
- "exclude": ["node_modules", "dist"]
20
- }