dt-common-device 1.3.0 → 2.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/TROUBLESHOOTING.md +184 -0
- package/dist/config/config.d.ts +9 -2
- package/dist/config/config.js +97 -14
- package/dist/constants/Event.d.ts +75 -0
- package/dist/constants/Event.js +78 -0
- package/dist/db/db.d.ts +1 -0
- package/dist/db/db.js +18 -2
- package/dist/device/cloud/entities/CloudDevice.d.ts +1 -0
- package/dist/device/cloud/entities/CloudDevice.js +3 -0
- package/dist/device/local/entities/AlertBuilder.d.ts +87 -0
- package/dist/device/local/entities/AlertBuilder.example.d.ts +11 -0
- package/dist/device/local/entities/AlertBuilder.example.js +117 -0
- package/dist/device/local/entities/AlertBuilder.js +179 -0
- package/dist/device/local/entities/IssueBuilder.d.ts +109 -0
- package/dist/device/local/entities/IssueBuilder.example.d.ts +16 -0
- package/dist/device/local/entities/IssueBuilder.example.js +196 -0
- package/dist/device/local/entities/IssueBuilder.js +237 -0
- package/dist/device/local/entities/index.d.ts +2 -0
- package/dist/device/local/entities/index.js +7 -0
- package/dist/device/local/events/EventHandler.js +6 -6
- package/dist/device/local/events/Events.d.ts +33 -12
- package/dist/device/local/events/Events.js +33 -12
- package/dist/device/local/interfaces/IDevice.d.ts +10 -9
- package/dist/device/local/interfaces/IDevice.js +7 -0
- package/dist/device/local/models/Alert.model.d.ts +28 -0
- package/dist/device/local/models/Alert.model.js +222 -0
- package/dist/device/local/models/Issue.model.d.ts +28 -0
- package/dist/device/local/models/Issue.model.js +260 -0
- package/dist/device/local/repository/Alert.repository.d.ts +106 -0
- package/dist/device/local/repository/Alert.repository.js +374 -0
- package/dist/device/local/repository/Device.repository.d.ts +11 -2
- package/dist/device/local/repository/Device.repository.js +163 -30
- package/dist/device/local/repository/Hub.repository.d.ts +1 -1
- package/dist/device/local/repository/Hub.repository.js +60 -18
- package/dist/device/local/repository/Issue.repository.d.ts +113 -0
- package/dist/device/local/repository/Issue.repository.js +401 -0
- package/dist/device/local/repository/Schedule.repository.d.ts +1 -1
- package/dist/device/local/repository/Schedule.repository.js +14 -18
- package/dist/device/local/services/Alert.service.d.ts +135 -5
- package/dist/device/local/services/Alert.service.js +471 -7
- package/dist/device/local/services/AlertService.example.d.ts +55 -0
- package/dist/device/local/services/AlertService.example.js +148 -0
- package/dist/device/local/services/Device.service.d.ts +9 -6
- package/dist/device/local/services/Device.service.js +61 -41
- package/dist/device/local/services/Issue.service.d.ts +168 -0
- package/dist/device/local/services/Issue.service.js +642 -0
- package/dist/device/local/services/IssueService.example.d.ts +68 -0
- package/dist/device/local/services/IssueService.example.js +177 -0
- package/dist/device/local/services/index.d.ts +7 -5
- package/dist/device/local/services/index.js +21 -11
- package/dist/events/BaseEventHandler.d.ts +43 -0
- package/dist/events/BaseEventHandler.js +111 -0
- package/dist/events/BaseEventTransformer.d.ts +26 -0
- package/dist/events/BaseEventTransformer.js +72 -0
- package/dist/events/DeviceEventHandler.d.ts +15 -0
- package/dist/events/DeviceEventHandler.js +152 -0
- package/dist/events/DeviceEventTransformerFactory.d.ts +27 -0
- package/dist/events/DeviceEventTransformerFactory.js +116 -0
- package/dist/events/EventHandler.d.ts +11 -0
- package/dist/events/EventHandler.js +111 -0
- package/dist/events/EventHandlerOrchestrator.d.ts +35 -0
- package/dist/events/EventHandlerOrchestrator.js +141 -0
- package/dist/events/EventProcessingService.d.ts +43 -0
- package/dist/events/EventProcessingService.js +243 -0
- package/dist/events/InternalEventSubscription.d.ts +44 -0
- package/dist/events/InternalEventSubscription.js +152 -0
- package/dist/events/index.d.ts +9 -0
- package/dist/events/index.js +21 -0
- package/dist/events/interfaces/DeviceEvent.d.ts +48 -0
- package/dist/events/interfaces/IEventHandler.d.ts +23 -0
- package/dist/{device/cloud/interface.js → events/interfaces/IEventHandler.js} +0 -1
- package/dist/events/interfaces/IEventTransformer.d.ts +7 -0
- package/dist/{device/cloud/interfaces/IDeviceConnectionService.js → events/interfaces/IEventTransformer.js} +0 -1
- package/dist/events/interfaces/IInternalEvent.d.ts +42 -0
- package/dist/events/interfaces/IInternalEvent.js +2 -0
- package/dist/events/interfaces/index.d.ts +4 -0
- package/dist/{device → events/interfaces}/index.js +4 -4
- package/dist/index.d.ts +6 -2
- package/dist/index.js +9 -2
- package/dist/types/alert.types.d.ts +57 -0
- package/dist/types/alert.types.js +22 -0
- package/dist/types/config.types.d.ts +15 -4
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/issue.types.d.ts +90 -0
- package/dist/types/issue.types.js +40 -0
- package/dist/utils/http-utils.d.ts +13 -0
- package/dist/utils/http-utils.js +117 -0
- package/package.json +2 -1
- package/src/config/config.ts +117 -14
- package/src/{device/local/events/Events.ts → constants/Event.ts} +34 -13
- package/src/db/db.ts +14 -5
- package/src/device/cloud/entities/CloudDevice.ts +4 -0
- package/src/device/local/entities/AlertBuilder.example.ts +126 -0
- package/src/device/local/entities/AlertBuilder.ts +202 -0
- package/src/device/local/entities/IssueBuilder.example.ts +210 -0
- package/src/device/local/entities/IssueBuilder.ts +263 -0
- package/src/device/local/entities/README.md +173 -0
- package/src/device/local/entities/index.ts +2 -0
- package/src/device/local/interfaces/IDevice.ts +11 -9
- package/src/device/local/models/Alert.model.md +319 -0
- package/src/device/local/models/Alert.model.ts +283 -0
- package/src/device/local/models/Issue.model.md +386 -0
- package/src/device/local/models/Issue.model.ts +350 -0
- package/src/device/local/models/README.md +312 -0
- package/src/device/local/repository/Alert.repository.ts +465 -0
- package/src/device/local/repository/Device.repository.ts +251 -30
- package/src/device/local/repository/Hub.repository.ts +74 -18
- package/src/device/local/repository/Issue.repository.ts +517 -0
- package/src/device/local/repository/Schedule.repository.ts +28 -22
- package/src/device/local/services/Alert.service.ts +617 -5
- package/src/device/local/services/AlertService.example.ts +229 -0
- package/src/device/local/services/Device.service.ts +74 -52
- package/src/device/local/services/Issue.service.ts +872 -0
- package/src/device/local/services/IssueService.example.ts +307 -0
- package/src/device/local/services/index.ts +7 -5
- package/src/events/BaseEventHandler.ts +145 -0
- package/src/events/BaseEventTransformer.ts +97 -0
- package/src/events/DeviceEventHandler.ts +211 -0
- package/src/events/DeviceEventTransformerFactory.ts +77 -0
- package/src/{device/local/events → events}/EventHandler.ts +24 -19
- package/src/events/EventHandlerOrchestrator.ts +119 -0
- package/src/events/EventProcessingService.ts +248 -0
- package/src/events/InternalEventSubscription.ts +219 -0
- package/src/events/index.ts +9 -0
- package/src/events/interfaces/DeviceEvent.ts +56 -0
- package/src/events/interfaces/IEventHandler.ts +28 -0
- package/src/events/interfaces/IEventTransformer.ts +8 -0
- package/src/events/interfaces/IInternalEvent.ts +47 -0
- package/src/events/interfaces/index.ts +4 -0
- package/src/index.ts +9 -2
- package/src/types/alert.types.ts +64 -0
- package/src/types/config.types.ts +17 -4
- package/src/types/index.ts +2 -0
- package/src/types/issue.types.ts +98 -0
- package/src/utils/http-utils.ts +143 -0
- package/dist/device/cloud/interface.d.ts +0 -101
- package/dist/device/cloud/interfaces/IDeviceConnectionService.d.ts +0 -7
- package/dist/device/cloud/interfaces/IDevicesService.d.ts +0 -9
- package/dist/device/cloud/services/Device.service.d.ts +0 -39
- package/dist/device/cloud/services/Device.service.js +0 -9
- package/dist/device/cloud/services/DeviceCloudService.d.ts +0 -42
- package/dist/device/cloud/services/DeviceCloudService.js +0 -59
- package/dist/device/cloud/services/DeviceHub.service.d.ts +0 -3
- package/dist/device/cloud/services/DeviceHub.service.js +0 -6
- package/dist/device/cloud/services/Hub.service.d.ts +0 -25
- package/dist/device/cloud/services/Hub.service.js +0 -9
- package/dist/device/cloud/services/SmartThingsDeviceService.d.ts +0 -38
- package/dist/device/cloud/services/SmartThingsDeviceService.js +0 -52
- package/dist/device/index.d.ts +0 -4
- package/dist/device/local/interface.d.ts +0 -0
- package/dist/device/local/interface.js +0 -1
- package/dist/device/local/services/DeviceHub.service.d.ts +0 -11
- package/dist/device/local/services/DeviceHub.service.js +0 -40
- package/src/device/local/events/index.ts +0 -2
- /package/dist/{device/cloud/interfaces/IDevicesService.js → events/interfaces/DeviceEvent.js} +0 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# Troubleshooting Guide
|
|
2
|
+
|
|
3
|
+
## HTTP Request Issues
|
|
4
|
+
|
|
5
|
+
### Error: `TypeError: Cannot read properties of undefined (reading 'emit')`
|
|
6
|
+
|
|
7
|
+
This error typically occurs when HTTP requests fail due to network connectivity issues, invalid service URLs, or request timeouts.
|
|
8
|
+
|
|
9
|
+
#### Common Causes:
|
|
10
|
+
|
|
11
|
+
1. **Invalid or unreachable DEVICE_SERVICE URL**
|
|
12
|
+
2. **Network connectivity issues**
|
|
13
|
+
3. **Service is down or not responding**
|
|
14
|
+
4. **Request timeout (default: 30 seconds)**
|
|
15
|
+
5. **DNS resolution failures**
|
|
16
|
+
|
|
17
|
+
#### Solutions:
|
|
18
|
+
|
|
19
|
+
1. **Check Service URL Configuration**
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
// Ensure DEVICE_SERVICE URL is properly configured
|
|
23
|
+
const config: DeviceConfig = {
|
|
24
|
+
DEVICE_SERVICE: "http://localhost:3000", // or your actual service URL
|
|
25
|
+
// ... other config
|
|
26
|
+
};
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
2. **Verify Network Connectivity**
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# Test if the service is reachable
|
|
33
|
+
curl -v http://your-device-service-url/health
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
3. **Check Service Status**
|
|
37
|
+
|
|
38
|
+
- Ensure the device service is running
|
|
39
|
+
- Check if the service is listening on the correct port
|
|
40
|
+
- Verify firewall settings
|
|
41
|
+
|
|
42
|
+
4. **Enable Debug Logging**
|
|
43
|
+
```typescript
|
|
44
|
+
const config: DeviceConfig = {
|
|
45
|
+
LOGGER: {
|
|
46
|
+
info: (msg, ...args) => console.log(msg, ...args),
|
|
47
|
+
warn: (msg, ...args) => console.warn(msg, ...args),
|
|
48
|
+
error: (msg, ...args) => console.error(msg, ...args),
|
|
49
|
+
},
|
|
50
|
+
// ... other config
|
|
51
|
+
};
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
#### Recent Fixes Applied:
|
|
55
|
+
|
|
56
|
+
1. **Centralized Axios Instance**: All HTTP requests now use a single, properly configured axios instance
|
|
57
|
+
2. **Added Request Timeouts**: All HTTP requests have a 30-second timeout
|
|
58
|
+
3. **Improved Error Handling**: Better error messages with context
|
|
59
|
+
4. **Request/Response Interceptors**: Added logging for debugging
|
|
60
|
+
5. **URL Validation**: Service URLs are validated during initialization
|
|
61
|
+
6. **Retry Logic**: Failed requests can be retried with exponential backoff
|
|
62
|
+
|
|
63
|
+
#### Architecture Changes:
|
|
64
|
+
|
|
65
|
+
**Before**: Each repository created its own axios instance
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
// Old approach - each repository had its own axios setup
|
|
69
|
+
export class DeviceRepository {
|
|
70
|
+
private readonly axiosInstance;
|
|
71
|
+
|
|
72
|
+
constructor() {
|
|
73
|
+
this.axiosInstance = axios.create({
|
|
74
|
+
timeout: 30000,
|
|
75
|
+
// ... other config
|
|
76
|
+
});
|
|
77
|
+
// ... interceptors setup
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
**After**: Centralized axios instance shared across all repositories
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
// New approach - centralized axios instance
|
|
86
|
+
import { getDeviceServiceAxiosInstance } from "../../../utils/http-utils";
|
|
87
|
+
|
|
88
|
+
export class DeviceRepository {
|
|
89
|
+
private readonly axiosInstance;
|
|
90
|
+
|
|
91
|
+
constructor() {
|
|
92
|
+
this.axiosInstance = getDeviceServiceAxiosInstance();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### Benefits of Centralized Approach:
|
|
98
|
+
|
|
99
|
+
1. **Consistency**: All HTTP requests use the same configuration
|
|
100
|
+
2. **Maintainability**: Configuration changes only need to be made in one place
|
|
101
|
+
3. **Performance**: Single instance reduces memory usage
|
|
102
|
+
4. **Error Handling**: Centralized error handling and logging
|
|
103
|
+
5. **Testing**: Easier to mock and test HTTP requests
|
|
104
|
+
|
|
105
|
+
#### Debugging Steps:
|
|
106
|
+
|
|
107
|
+
1. **Check Logs**: Look for detailed error messages in the logs
|
|
108
|
+
2. **Verify Configuration**: Ensure all required environment variables are set
|
|
109
|
+
3. **Test Connectivity**: Use curl or similar tools to test service endpoints
|
|
110
|
+
4. **Check Network**: Verify network connectivity and DNS resolution
|
|
111
|
+
|
|
112
|
+
#### Environment Variables Required:
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
# Required for HTTP requests
|
|
116
|
+
DEVICE_SERVICE=http://your-device-service-url
|
|
117
|
+
|
|
118
|
+
# Required for database connections
|
|
119
|
+
ADMIN_DB_URI=postgresql://...
|
|
120
|
+
MONGODB_URI=mongodb://...
|
|
121
|
+
REDIS_HOST=localhost
|
|
122
|
+
REDIS_PORT=6379
|
|
123
|
+
|
|
124
|
+
# Required for AWS services
|
|
125
|
+
AWS_ACCESS_KEY_ID=your-access-key
|
|
126
|
+
AWS_SECRET_ACCESS_KEY=your-secret-key
|
|
127
|
+
AWS_REGION=your-region
|
|
128
|
+
EVENT_BUS_NAME=your-event-bus
|
|
129
|
+
|
|
130
|
+
# Required for analytics
|
|
131
|
+
POSTHOG_API_KEY=your-api-key
|
|
132
|
+
POSTHOG_HOST=your-posthog-host
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
#### Example Error Handling:
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { initialize } from "dt-common-device";
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
await initialize({
|
|
142
|
+
DEVICE_SERVICE: "http://localhost:3000",
|
|
143
|
+
LOGGER: console,
|
|
144
|
+
// ... other config
|
|
145
|
+
});
|
|
146
|
+
} catch (error) {
|
|
147
|
+
console.error("Initialization failed:", error.message);
|
|
148
|
+
// Handle initialization error
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
#### Repository Error Handling:
|
|
153
|
+
|
|
154
|
+
All repository classes now include:
|
|
155
|
+
|
|
156
|
+
- Proper try-catch blocks
|
|
157
|
+
- Detailed error logging
|
|
158
|
+
- Request timeouts
|
|
159
|
+
- Response validation
|
|
160
|
+
- Network error detection
|
|
161
|
+
- Centralized axios instance usage
|
|
162
|
+
|
|
163
|
+
#### Affected Files:
|
|
164
|
+
|
|
165
|
+
The following repository files now use the centralized axios instance:
|
|
166
|
+
|
|
167
|
+
- `src/device/local/repository/Device.repository.ts`
|
|
168
|
+
- `src/device/local/repository/Hub.repository.ts`
|
|
169
|
+
- `src/device/local/repository/Schedule.repository.ts`
|
|
170
|
+
|
|
171
|
+
Other repository files (Alert, Issue, Property, Connection) use database operations and don't require HTTP requests.
|
|
172
|
+
|
|
173
|
+
#### HTTP Utilities:
|
|
174
|
+
|
|
175
|
+
The centralized axios instance is managed in:
|
|
176
|
+
|
|
177
|
+
- `src/utils/http-utils.ts` - Contains the axios instance creation and configuration
|
|
178
|
+
|
|
179
|
+
If you continue to experience issues, please:
|
|
180
|
+
|
|
181
|
+
1. Check the service logs for detailed error messages
|
|
182
|
+
2. Verify all environment variables are correctly set
|
|
183
|
+
3. Test network connectivity to the service endpoints
|
|
184
|
+
4. Ensure the target services are running and accessible
|
package/dist/config/config.d.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { DeviceConfig } from "../types";
|
|
2
|
-
|
|
2
|
+
import { InternalEventSubscription } from "../events";
|
|
3
|
+
export declare function initialize(cfg: DeviceConfig): Promise<void>;
|
|
3
4
|
export declare function getConfig(): DeviceConfig;
|
|
4
|
-
export declare function
|
|
5
|
+
export declare function getEventSubscription(): InternalEventSubscription | null;
|
|
6
|
+
export declare function checkRequiredEnv(): void;
|
|
5
7
|
export declare function ensureAuditInitialized(): void;
|
|
6
8
|
/**
|
|
7
9
|
* Returns the PostgreSQL DB URI from environment variables.
|
|
8
10
|
* Throws an error if not set.
|
|
9
11
|
*/
|
|
10
12
|
export declare function getPostgresDbUri(): string;
|
|
13
|
+
export declare function getMongoUri(): string;
|
|
11
14
|
/**
|
|
12
15
|
* Returns the Redis DB Host and port from environment variables.
|
|
13
16
|
* Throws an error if not set.
|
|
@@ -16,3 +19,7 @@ export declare function getRedisDbHostAndPort(): {
|
|
|
16
19
|
host: string;
|
|
17
20
|
port: number;
|
|
18
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* Graceful shutdown function
|
|
24
|
+
*/
|
|
25
|
+
export declare function shutdown(): Promise<void>;
|
package/dist/config/config.js
CHANGED
|
@@ -5,28 +5,76 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.initialize = initialize;
|
|
7
7
|
exports.getConfig = getConfig;
|
|
8
|
-
exports.
|
|
8
|
+
exports.getEventSubscription = getEventSubscription;
|
|
9
|
+
exports.checkRequiredEnv = checkRequiredEnv;
|
|
9
10
|
exports.ensureAuditInitialized = ensureAuditInitialized;
|
|
10
11
|
exports.getPostgresDbUri = getPostgresDbUri;
|
|
12
|
+
exports.getMongoUri = getMongoUri;
|
|
11
13
|
exports.getRedisDbHostAndPort = getRedisDbHostAndPort;
|
|
14
|
+
exports.shutdown = shutdown;
|
|
12
15
|
const dt_audit_library_1 = require("dt-audit-library");
|
|
16
|
+
const db_1 = require("../db/db");
|
|
13
17
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
18
|
+
const events_1 = require("../events");
|
|
19
|
+
const http_utils_1 = require("../utils/http-utils");
|
|
14
20
|
dotenv_1.default.config();
|
|
15
21
|
let config = null;
|
|
16
22
|
let auditInitialized = false;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"ADMIN_SERVICE",
|
|
21
|
-
"OPERATIONAL_SERVICE",
|
|
22
|
-
"DEVICE_SERVICE",
|
|
23
|
-
"MONITORING_SERVICE",
|
|
24
|
-
"ENERGY_SERVICE",
|
|
25
|
-
].some((key) => Boolean(cfg[key]));
|
|
26
|
-
if (!hasAtLeastOneService) {
|
|
27
|
-
throw new Error("At least one http service must be initialized");
|
|
28
|
-
}
|
|
23
|
+
let eventSubscription = null;
|
|
24
|
+
async function initialize(cfg) {
|
|
25
|
+
// Initialize config
|
|
29
26
|
config = { ...cfg };
|
|
27
|
+
// Validate service URLs if provided
|
|
28
|
+
if (cfg.DEVICE_SERVICE && !(0, http_utils_1.validateServiceUrl)(cfg.DEVICE_SERVICE)) {
|
|
29
|
+
throw new Error(`Invalid DEVICE_SERVICE URL: ${cfg.DEVICE_SERVICE}`);
|
|
30
|
+
}
|
|
31
|
+
if (cfg.ADMIN_SERVICE && !(0, http_utils_1.validateServiceUrl)(cfg.ADMIN_SERVICE)) {
|
|
32
|
+
throw new Error(`Invalid ADMIN_SERVICE URL: ${cfg.ADMIN_SERVICE}`);
|
|
33
|
+
}
|
|
34
|
+
if (cfg.ACCESS_SERVICE && !(0, http_utils_1.validateServiceUrl)(cfg.ACCESS_SERVICE)) {
|
|
35
|
+
throw new Error(`Invalid ACCESS_SERVICE URL: ${cfg.ACCESS_SERVICE}`);
|
|
36
|
+
}
|
|
37
|
+
if (cfg.ENERGY_SERVICE && !(0, http_utils_1.validateServiceUrl)(cfg.ENERGY_SERVICE)) {
|
|
38
|
+
throw new Error(`Invalid ENERGY_SERVICE URL: ${cfg.ENERGY_SERVICE}`);
|
|
39
|
+
}
|
|
40
|
+
// Initialize internal event subscription if handler is provided
|
|
41
|
+
if (cfg.INTERNAL_EVENT_HANDLER) {
|
|
42
|
+
try {
|
|
43
|
+
eventSubscription = new events_1.InternalEventSubscription(cfg.INTERNAL_EVENT_HANDLER);
|
|
44
|
+
cfg.LOGGER.info("InternalEventSubscription initialized successfully");
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
cfg.LOGGER.error("Failed to initialize InternalEventSubscription", {
|
|
48
|
+
error,
|
|
49
|
+
});
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Connect to databases
|
|
54
|
+
try {
|
|
55
|
+
await (0, db_1.connectDatabase)();
|
|
56
|
+
cfg.LOGGER.info("Database connections established successfully");
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
cfg.LOGGER.error("Failed to connect to databases", { error });
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
// Subscribe to events if event subscription is available
|
|
63
|
+
if (eventSubscription) {
|
|
64
|
+
try {
|
|
65
|
+
await eventSubscription.subscribe();
|
|
66
|
+
cfg.LOGGER.info("Event subscription started successfully");
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
cfg.LOGGER.error(`Failed to start event subscription: ${error.message || error.toString()}`);
|
|
70
|
+
// Don't throw here as the main setup should continue
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Check if required env variables are set
|
|
74
|
+
checkRequiredEnv();
|
|
75
|
+
// Initialize audit
|
|
76
|
+
ensureAuditInitialized();
|
|
77
|
+
console.log("dt-common-device: Initialization completed successfully");
|
|
30
78
|
}
|
|
31
79
|
function getConfig() {
|
|
32
80
|
if (!config) {
|
|
@@ -34,12 +82,21 @@ function getConfig() {
|
|
|
34
82
|
}
|
|
35
83
|
return config;
|
|
36
84
|
}
|
|
37
|
-
function
|
|
85
|
+
function getEventSubscription() {
|
|
86
|
+
return eventSubscription;
|
|
87
|
+
}
|
|
88
|
+
function checkRequiredEnv() {
|
|
38
89
|
const requiredEnv = [
|
|
39
90
|
"AWS_SECRET_ACCESS_KEY",
|
|
40
91
|
"AWS_REGION",
|
|
41
92
|
"AWS_ACCESS_KEY_ID",
|
|
42
93
|
"EVENT_BUS_NAME",
|
|
94
|
+
"ADMIN_DB_URI",
|
|
95
|
+
"MONGODB_URI",
|
|
96
|
+
"REDIS_HOST",
|
|
97
|
+
"REDIS_PORT",
|
|
98
|
+
"POSTHOG_API_KEY",
|
|
99
|
+
"POSTHOG_HOST",
|
|
43
100
|
];
|
|
44
101
|
const missing = requiredEnv.filter((key) => !process.env[key]);
|
|
45
102
|
if (missing.length > 0) {
|
|
@@ -52,6 +109,7 @@ function ensureAuditInitialized() {
|
|
|
52
109
|
const apiKey = process.env.POSTHOG_API_KEY;
|
|
53
110
|
const host = process.env.POSTHOG_HOST;
|
|
54
111
|
if (!apiKey || !host) {
|
|
112
|
+
getConfig().LOGGER.error("POSTHOG_API_KEY and POSTHOG_HOST must be set in environment variables");
|
|
55
113
|
throw new Error("dt-common-device: POSTHOG_API_KEY and POSTHOG_HOST must be set in environment variables");
|
|
56
114
|
}
|
|
57
115
|
(0, dt_audit_library_1.initializeAudit)(apiKey, host);
|
|
@@ -64,10 +122,19 @@ function ensureAuditInitialized() {
|
|
|
64
122
|
function getPostgresDbUri() {
|
|
65
123
|
const fullUri = process.env.ADMIN_DB_URI;
|
|
66
124
|
if (!fullUri) {
|
|
125
|
+
getConfig().LOGGER.error("ADMIN_DB_URI must be set in environment variables or .env file");
|
|
67
126
|
throw new Error("dt-common-device: ADMIN_DB_URI must be set in environment variables or .env file");
|
|
68
127
|
}
|
|
69
128
|
return fullUri;
|
|
70
129
|
}
|
|
130
|
+
function getMongoUri() {
|
|
131
|
+
const fullUri = process.env.MONGODB_URI;
|
|
132
|
+
if (!fullUri) {
|
|
133
|
+
getConfig().LOGGER.error("MONGODB_URI must be set in environment variables or .env file");
|
|
134
|
+
throw new Error("dt-common-device: MONGODB_URI must be set in environment variables or .env file");
|
|
135
|
+
}
|
|
136
|
+
return fullUri;
|
|
137
|
+
}
|
|
71
138
|
/**
|
|
72
139
|
* Returns the Redis DB Host and port from environment variables.
|
|
73
140
|
* Throws an error if not set.
|
|
@@ -76,7 +143,23 @@ function getRedisDbHostAndPort() {
|
|
|
76
143
|
const host = process.env.REDIS_HOST;
|
|
77
144
|
const port = process.env.REDIS_PORT;
|
|
78
145
|
if (!host || !port) {
|
|
146
|
+
getConfig().LOGGER.error("REDIS_HOST and REDIS_PORT must be set in environment variables");
|
|
79
147
|
throw new Error("dt-common-device: REDIS_HOST and REDIS_PORT must be set in environment variables");
|
|
80
148
|
}
|
|
81
149
|
return { host, port: Number(port) };
|
|
82
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Graceful shutdown function
|
|
153
|
+
*/
|
|
154
|
+
async function shutdown() {
|
|
155
|
+
if (eventSubscription) {
|
|
156
|
+
try {
|
|
157
|
+
await eventSubscription.unsubscribe();
|
|
158
|
+
getConfig().LOGGER.info("Event subscription stopped successfully");
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
getConfig().LOGGER.error("Failed to stop event subscription", { error });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
getConfig().LOGGER.info("dt-common-device: Shutdown completed");
|
|
165
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export declare const DT_EVENT_TYPES: {
|
|
2
|
+
DEVICE: {
|
|
3
|
+
CREATE: {
|
|
4
|
+
SUCCESS: string;
|
|
5
|
+
FAILED: string;
|
|
6
|
+
};
|
|
7
|
+
UPDATE: {
|
|
8
|
+
SUCCESS: string;
|
|
9
|
+
FAILED: string;
|
|
10
|
+
};
|
|
11
|
+
DELETE: {
|
|
12
|
+
SUCCESS: string;
|
|
13
|
+
FAILED: string;
|
|
14
|
+
};
|
|
15
|
+
STATE: {
|
|
16
|
+
SET: string;
|
|
17
|
+
UNKNOWN: string;
|
|
18
|
+
UPDATED: string;
|
|
19
|
+
CHANGED: string;
|
|
20
|
+
};
|
|
21
|
+
STATUS: {
|
|
22
|
+
SET: string;
|
|
23
|
+
ONLINE: string;
|
|
24
|
+
OFFLINE: string;
|
|
25
|
+
UNKNOWN: string;
|
|
26
|
+
UPDATED: string;
|
|
27
|
+
CHANGED: string;
|
|
28
|
+
};
|
|
29
|
+
BATTERY: {
|
|
30
|
+
SET: string;
|
|
31
|
+
UNKNOWN: string;
|
|
32
|
+
LOW: string;
|
|
33
|
+
CRITICAL: string;
|
|
34
|
+
UPDATED: string;
|
|
35
|
+
CHANGED: string;
|
|
36
|
+
};
|
|
37
|
+
META_DATA: {
|
|
38
|
+
SET: string;
|
|
39
|
+
UPDATED: string;
|
|
40
|
+
CHANGED: string;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
CONNECTION: {
|
|
44
|
+
CREATE: {
|
|
45
|
+
SUCCESS: string;
|
|
46
|
+
FAILED: string;
|
|
47
|
+
};
|
|
48
|
+
UPDATE: {
|
|
49
|
+
SUCCESS: string;
|
|
50
|
+
FAILED: string;
|
|
51
|
+
};
|
|
52
|
+
DELETE: {
|
|
53
|
+
SUCCESS: string;
|
|
54
|
+
FAILED: string;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
PROPERTY: {
|
|
58
|
+
CREATE: {
|
|
59
|
+
SUCCESS: string;
|
|
60
|
+
FAILED: string;
|
|
61
|
+
};
|
|
62
|
+
UPDATE: {
|
|
63
|
+
SUCCESS: string;
|
|
64
|
+
FAILED: string;
|
|
65
|
+
};
|
|
66
|
+
DELETE: {
|
|
67
|
+
SUCCESS: string;
|
|
68
|
+
FAILED: string;
|
|
69
|
+
};
|
|
70
|
+
PREFERENCES: {
|
|
71
|
+
UPDATED: string;
|
|
72
|
+
CREATED: string;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DT_EVENT_TYPES = void 0;
|
|
4
|
+
exports.DT_EVENT_TYPES = {
|
|
5
|
+
DEVICE: {
|
|
6
|
+
CREATE: {
|
|
7
|
+
SUCCESS: "device.create.success",
|
|
8
|
+
FAILED: "device.create.failed",
|
|
9
|
+
},
|
|
10
|
+
UPDATE: {
|
|
11
|
+
SUCCESS: "device.update.success",
|
|
12
|
+
FAILED: "device.update.failed",
|
|
13
|
+
},
|
|
14
|
+
DELETE: {
|
|
15
|
+
SUCCESS: "device.delete.success",
|
|
16
|
+
FAILED: "device.delete.failed",
|
|
17
|
+
},
|
|
18
|
+
STATE: {
|
|
19
|
+
SET: "device.state.set",
|
|
20
|
+
UNKNOWN: "device.state.unknown",
|
|
21
|
+
UPDATED: "device.state.updated",
|
|
22
|
+
CHANGED: "device.state.changed",
|
|
23
|
+
},
|
|
24
|
+
STATUS: {
|
|
25
|
+
SET: "device.status.set",
|
|
26
|
+
ONLINE: "device.status.online",
|
|
27
|
+
OFFLINE: "device.status.offline",
|
|
28
|
+
UNKNOWN: "device.status.unknown",
|
|
29
|
+
UPDATED: "device.status.updated",
|
|
30
|
+
CHANGED: "device.status.changed",
|
|
31
|
+
},
|
|
32
|
+
BATTERY: {
|
|
33
|
+
SET: "device.battery.set",
|
|
34
|
+
UNKNOWN: "device.battery.unknown",
|
|
35
|
+
LOW: "device.battery.low",
|
|
36
|
+
CRITICAL: "device.battery.critical",
|
|
37
|
+
UPDATED: "device.battery.updated",
|
|
38
|
+
CHANGED: "device.battery.changed",
|
|
39
|
+
},
|
|
40
|
+
META_DATA: {
|
|
41
|
+
SET: "device.metaData.set",
|
|
42
|
+
UPDATED: "device.metaData.updated",
|
|
43
|
+
CHANGED: "device.metaData.changed",
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
CONNECTION: {
|
|
47
|
+
CREATE: {
|
|
48
|
+
SUCCESS: "connection.create.success",
|
|
49
|
+
FAILED: "connection.create.failed",
|
|
50
|
+
},
|
|
51
|
+
UPDATE: {
|
|
52
|
+
SUCCESS: "connection.update.success",
|
|
53
|
+
FAILED: "connection.update.failed",
|
|
54
|
+
},
|
|
55
|
+
DELETE: {
|
|
56
|
+
SUCCESS: "connection.delete.success",
|
|
57
|
+
FAILED: "connection.delete.failed",
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
PROPERTY: {
|
|
61
|
+
CREATE: {
|
|
62
|
+
SUCCESS: "property.create.success",
|
|
63
|
+
FAILED: "property.create.failed",
|
|
64
|
+
},
|
|
65
|
+
UPDATE: {
|
|
66
|
+
SUCCESS: "property.update.success",
|
|
67
|
+
FAILED: "property.update.failed",
|
|
68
|
+
},
|
|
69
|
+
DELETE: {
|
|
70
|
+
SUCCESS: "property.delete.success",
|
|
71
|
+
FAILED: "property.delete.failed",
|
|
72
|
+
},
|
|
73
|
+
PREFERENCES: {
|
|
74
|
+
UPDATED: "property.preferences.updated",
|
|
75
|
+
CREATED: "property.preferences.created",
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
};
|
package/dist/db/db.d.ts
CHANGED
package/dist/db/db.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.connectDatabase = void 0;
|
|
3
7
|
exports.getPostgresClient = getPostgresClient;
|
|
4
8
|
const pg_1 = require("pg");
|
|
5
9
|
const config_1 = require("../config/config");
|
|
6
|
-
const
|
|
7
|
-
let pool = null;
|
|
10
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
8
11
|
function getPostgresClient() {
|
|
12
|
+
const URI = (0, config_1.getPostgresDbUri)();
|
|
13
|
+
let pool = null;
|
|
9
14
|
if (!pool) {
|
|
10
15
|
pool = new pg_1.Pool({
|
|
11
16
|
connectionString: URI,
|
|
@@ -13,3 +18,14 @@ function getPostgresClient() {
|
|
|
13
18
|
}
|
|
14
19
|
return pool;
|
|
15
20
|
}
|
|
21
|
+
const connectDatabase = async () => {
|
|
22
|
+
try {
|
|
23
|
+
const URI = (0, config_1.getMongoUri)();
|
|
24
|
+
await mongoose_1.default.connect(URI);
|
|
25
|
+
console.log(`Connected to MongoDB at ${URI.split("/").at(-1)}`);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
console.error("Error connecting to MongoDB:", error);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
exports.connectDatabase = connectDatabase;
|
|
@@ -12,4 +12,5 @@ export declare abstract class CloudDevice implements ICloudDevice {
|
|
|
12
12
|
getBattery(deviceId: string): Promise<number | string>;
|
|
13
13
|
getState(deviceId: string): Promise<string>;
|
|
14
14
|
getStatus(connectionId: string, deviceId: string): Promise<string>;
|
|
15
|
+
toLocalDevice(): Promise<IDevice>;
|
|
15
16
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { CreateAlertData, AlertCategory, AlertSeverity, EntityType } from "../../../types/alert.types";
|
|
2
|
+
/**
|
|
3
|
+
* AlertBuilder - A builder pattern implementation for constructing CreateAlertData objects
|
|
4
|
+
*
|
|
5
|
+
* This builder provides a fluent interface for creating alert data with proper validation
|
|
6
|
+
* and default values. It follows the Builder pattern which is a standard design pattern
|
|
7
|
+
* in TypeScript for constructing complex objects.
|
|
8
|
+
*
|
|
9
|
+
* Usage example:
|
|
10
|
+
* const alertData = new AlertBuilder()
|
|
11
|
+
* .setCategory(AlertCategory.OPERATIONS)
|
|
12
|
+
* .setPropertyId("prop123")
|
|
13
|
+
* .setTitle("Device Offline")
|
|
14
|
+
* .setDescription("Device has been offline for more than 5 minutes")
|
|
15
|
+
* .setEntityId("device456")
|
|
16
|
+
* .setEntityType(EntityType.DEVICE)
|
|
17
|
+
* .setSeverity(AlertSeverity.HIGH)
|
|
18
|
+
* .setCreatedBy("user789")
|
|
19
|
+
* .build();
|
|
20
|
+
*/
|
|
21
|
+
export declare class AlertBuilder {
|
|
22
|
+
private data;
|
|
23
|
+
/**
|
|
24
|
+
* Sets the alert category
|
|
25
|
+
*/
|
|
26
|
+
setCategory(category: AlertCategory): AlertBuilder;
|
|
27
|
+
/**
|
|
28
|
+
* Sets the property ID
|
|
29
|
+
*/
|
|
30
|
+
setPropertyId(propertyId: string): AlertBuilder;
|
|
31
|
+
/**
|
|
32
|
+
* Sets the alert title
|
|
33
|
+
*/
|
|
34
|
+
setTitle(title: string): AlertBuilder;
|
|
35
|
+
/**
|
|
36
|
+
* Sets the alert description
|
|
37
|
+
*/
|
|
38
|
+
setDescription(description: string): AlertBuilder;
|
|
39
|
+
/**
|
|
40
|
+
* Sets the entity ID (optional)
|
|
41
|
+
*/
|
|
42
|
+
setEntityId(entityId?: string): AlertBuilder;
|
|
43
|
+
/**
|
|
44
|
+
* Sets the entity type
|
|
45
|
+
*/
|
|
46
|
+
setEntityType(entityType: EntityType): AlertBuilder;
|
|
47
|
+
/**
|
|
48
|
+
* Sets the alert severity (optional, defaults to MEDIUM)
|
|
49
|
+
*/
|
|
50
|
+
setSeverity(severity?: AlertSeverity): AlertBuilder;
|
|
51
|
+
/**
|
|
52
|
+
* Sets the user who created the alert (optional)
|
|
53
|
+
*/
|
|
54
|
+
setCreatedBy(createdBy?: string): AlertBuilder;
|
|
55
|
+
/**
|
|
56
|
+
* Sets the snooze until date (optional)
|
|
57
|
+
*/
|
|
58
|
+
setSnoozeUntil(snoozeUntil?: Date): AlertBuilder;
|
|
59
|
+
/**
|
|
60
|
+
* Validates that all required fields are present
|
|
61
|
+
*/
|
|
62
|
+
private validate;
|
|
63
|
+
/**
|
|
64
|
+
* Builds and returns the CreateAlertData object
|
|
65
|
+
* @throws Error if required fields are missing
|
|
66
|
+
*/
|
|
67
|
+
build(): CreateAlertData;
|
|
68
|
+
/**
|
|
69
|
+
* Resets the builder to its initial state
|
|
70
|
+
*/
|
|
71
|
+
reset(): AlertBuilder;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a new builder instance with predefined values for common alert types
|
|
74
|
+
*/
|
|
75
|
+
static createReadinessAlert(): AlertBuilder;
|
|
76
|
+
static createOperationsAlert(): AlertBuilder;
|
|
77
|
+
static createSecurityAlert(): AlertBuilder;
|
|
78
|
+
static createEnergyAlert(): AlertBuilder;
|
|
79
|
+
/**
|
|
80
|
+
* Creates a device-specific alert builder
|
|
81
|
+
*/
|
|
82
|
+
static createDeviceAlert(deviceId: string, propertyId: string): AlertBuilder;
|
|
83
|
+
/**
|
|
84
|
+
* Creates a hub-specific alert builder
|
|
85
|
+
*/
|
|
86
|
+
static createHubAlert(hubId: string, propertyId: string): AlertBuilder;
|
|
87
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example usage of AlertBuilder
|
|
3
|
+
* This file demonstrates various ways to use the AlertBuilder pattern
|
|
4
|
+
*/
|
|
5
|
+
export declare function createBasicAlert(): import("../../../types/alert.types").CreateAlertData;
|
|
6
|
+
export declare function createDeviceAlert(): import("../../../types/alert.types").CreateAlertData;
|
|
7
|
+
export declare function createSecurityAlert(): import("../../../types/alert.types").CreateAlertData;
|
|
8
|
+
export declare function createEnergyAlert(): import("../../../types/alert.types").CreateAlertData;
|
|
9
|
+
export declare function createHubAlert(): import("../../../types/alert.types").CreateAlertData;
|
|
10
|
+
export declare function createSnoozedAlert(): import("../../../types/alert.types").CreateAlertData;
|
|
11
|
+
export declare function createMultipleAlerts(): import("../../../types/alert.types").CreateAlertData[];
|