dt-common-device 3.1.2 → 3.1.5

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.
@@ -37,9 +37,6 @@ async function initialize(cfg) {
37
37
  if (cfg.ENERGY_SERVICE && !(0, http_utils_1.validateServiceUrl)(cfg.ENERGY_SERVICE)) {
38
38
  throw new Error(`Invalid ENERGY_SERVICE URL: ${cfg.ENERGY_SERVICE}`);
39
39
  }
40
- if (cfg.REMOTE_SERVICE && !(0, http_utils_1.validateServiceUrl)(cfg.REMOTE_SERVICE)) {
41
- throw new Error(`Invalid REMOTE_SERVICE URL: ${cfg.REMOTE_SERVICE}`);
42
- }
43
40
  // Initialize internal event subscription if handler is provided
44
41
  if (cfg.INTERNAL_EVENT_HANDLER) {
45
42
  try {
@@ -100,7 +97,6 @@ function checkRequiredEnv() {
100
97
  "REDIS_PORT",
101
98
  "POSTHOG_API_KEY",
102
99
  "POSTHOG_HOST",
103
- "CLOUD_SERVICE_API_KEY",
104
100
  ];
105
101
  const missing = requiredEnv.filter((key) => !process.env[key]);
106
102
  if (missing.length > 0) {
@@ -15,7 +15,7 @@ export type IConfig = {
15
15
  REMOTE_SERVICE?: string;
16
16
  INTERNAL_EVENT_HANDLER: IInternalEvent;
17
17
  LOGGER: ILogger;
18
- CLOUD_SERVICE_API_KEY: string;
18
+ CLOUD_SERVICE_API_KEY?: string;
19
19
  [key: string]: any;
20
20
  };
21
21
  export {};
@@ -1,6 +1,8 @@
1
1
  export declare const CONNECTION_PROVIDERS: {
2
2
  readonly DEVICETHREAD: "Devicethread";
3
3
  readonly CLOUDBEDS: "Cloudbeds";
4
+ readonly STAYNTOUCH: "Stayntouch";
5
+ readonly HOTELKEY: "Hotelkey";
4
6
  readonly SALTOKS: "SaltoKS";
5
7
  readonly SCHLAGE: "Schlage";
6
8
  readonly SENSIBO: "Sensibo";
@@ -4,6 +4,8 @@ exports.CONNECTION_PROVIDERS = void 0;
4
4
  exports.CONNECTION_PROVIDERS = {
5
5
  DEVICETHREAD: "Devicethread",
6
6
  CLOUDBEDS: "Cloudbeds",
7
+ STAYNTOUCH: "Stayntouch",
8
+ HOTELKEY: "Hotelkey",
7
9
  SALTOKS: "SaltoKS",
8
10
  SCHLAGE: "Schlage",
9
11
  SENSIBO: "Sensibo",
@@ -1,6 +1,74 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
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 __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
19
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
20
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
21
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
22
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
23
+ var _, done = false;
24
+ for (var i = decorators.length - 1; i >= 0; i--) {
25
+ var context = {};
26
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
27
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
28
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
29
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
30
+ if (kind === "accessor") {
31
+ if (result === void 0) continue;
32
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
33
+ if (_ = accept(result.get)) descriptor.get = _;
34
+ if (_ = accept(result.set)) descriptor.set = _;
35
+ if (_ = accept(result.init)) initializers.unshift(_);
36
+ }
37
+ else if (_ = accept(result)) {
38
+ if (kind === "field") initializers.unshift(_);
39
+ else descriptor[key] = _;
40
+ }
41
+ }
42
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
43
+ done = true;
44
+ };
45
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
46
+ var useValue = arguments.length > 2;
47
+ for (var i = 0; i < initializers.length; i++) {
48
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
49
+ }
50
+ return useValue ? value : void 0;
51
+ };
52
+ var __importStar = (this && this.__importStar) || (function () {
53
+ var ownKeys = function(o) {
54
+ ownKeys = Object.getOwnPropertyNames || function (o) {
55
+ var ar = [];
56
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
57
+ return ar;
58
+ };
59
+ return ownKeys(o);
60
+ };
61
+ return function (mod) {
62
+ if (mod && mod.__esModule) return mod;
63
+ var result = {};
64
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
65
+ __setModuleDefault(result, mod);
66
+ return result;
67
+ };
68
+ })();
69
+ var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
70
+ if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
71
+ return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
4
72
  };
5
73
  Object.defineProperty(exports, "__esModule", { value: true });
6
74
  exports.DeviceEventHandler = void 0;
@@ -8,151 +76,167 @@ const constants_1 = require("../constants");
8
76
  const Event_1 = require("../constants/Event");
9
77
  const services_1 = require("../device/local/services");
10
78
  const BaseEventHandler_1 = require("./BaseEventHandler");
11
- const typedi_1 = __importDefault(require("typedi"));
12
- class DeviceEventHandler extends BaseEventHandler_1.BaseEventHandler {
13
- constructor() {
14
- super([
15
- Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE,
16
- Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE,
17
- Event_1.DT_EVENT_TYPES.DEVICE.STATUS.CHANGED,
18
- Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CRITICAL,
19
- Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LOW,
20
- Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED,
21
- ], 100);
22
- // Use dependency injection instead of creating new instance
23
- this.localHubService = typedi_1.default.get(services_1.LocalHubService);
24
- }
25
- async onEvent(event) {
26
- this.logger.info("[DT | CDL]:[DeviceEventHandler]: Processing event", {
27
- eventName: event.eventName,
28
- });
29
- try {
30
- const device = await this.localDeviceService.getDevice(event.deviceId);
31
- if (!device?.deviceId) {
32
- throw new Error("[DT | CDL]:[DeviceEventHandler]: deviceId does not exist");
33
- }
34
- await this.updateDeviceEventData(device, event);
35
- // Update relevant tables based on event type
36
- await this.updateTablesBasedOnEventType(event, device);
37
- // Save event logs
38
- await this.dumpToEventOperation(event, device);
79
+ const typedi_1 = __importStar(require("typedi"));
80
+ let DeviceEventHandler = (() => {
81
+ let _classDecorators = [(0, typedi_1.Service)()];
82
+ let _classDescriptor;
83
+ let _classExtraInitializers = [];
84
+ let _classThis;
85
+ let _classSuper = BaseEventHandler_1.BaseEventHandler;
86
+ var DeviceEventHandler = _classThis = class extends _classSuper {
87
+ constructor() {
88
+ super([
89
+ Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE,
90
+ Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE,
91
+ Event_1.DT_EVENT_TYPES.DEVICE.STATUS.CHANGED,
92
+ Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CRITICAL,
93
+ Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LOW,
94
+ Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED,
95
+ ], 100);
96
+ // Use dependency injection instead of creating new instance
97
+ this.localHubService = typedi_1.default.get(services_1.LocalHubService);
39
98
  }
40
- catch (error) {
41
- this.logger.error("[DT | CDL]:[DeviceEventHandler]: Error processing event", error);
42
- throw error;
99
+ async onEvent(event) {
100
+ this.logger.info("[DT | CDL]:[DeviceEventHandler]: Processing event", {
101
+ eventName: event.eventName,
102
+ });
103
+ try {
104
+ const device = await this.localDeviceService.getDevice(event.deviceId);
105
+ if (!device?.deviceId) {
106
+ throw new Error("[DT | CDL]:[DeviceEventHandler]: deviceId does not exist");
107
+ }
108
+ await this.updateDeviceEventData(device, event);
109
+ // Update relevant tables based on event type
110
+ await this.updateTablesBasedOnEventType(event, device);
111
+ // Save event logs
112
+ await this.dumpToEventOperation(event, device);
113
+ }
114
+ catch (error) {
115
+ this.logger.error("[DT | CDL]:[DeviceEventHandler]: Error processing event", error);
116
+ throw error;
117
+ }
43
118
  }
44
- }
45
- async updateDeviceEventData(deviceData, event) {
46
- switch (event.eventName) {
47
- case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE:
48
- case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE:
49
- await this.localDeviceService.setState(deviceData.deviceId, {
50
- status: {
51
- online: event.eventName === Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE,
52
- lastUpdated: new Date().toISOString(),
53
- error: {
54
- type: event.eventName === Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE
55
- ? "offline"
56
- : "",
57
- message: event.eventName === Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE
58
- ? "Device is offline"
59
- : "",
60
- default: {},
119
+ async updateDeviceEventData(deviceData, event) {
120
+ switch (event.eventName) {
121
+ case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE:
122
+ case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE:
123
+ await this.localDeviceService.setState(deviceData.deviceId, {
124
+ status: {
125
+ online: event.eventName === Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE,
126
+ lastUpdated: new Date().toISOString(),
127
+ error: {
128
+ type: event.eventName === Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE
129
+ ? "offline"
130
+ : "",
131
+ message: event.eventName === Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE
132
+ ? "Device is offline"
133
+ : "",
134
+ default: {},
135
+ },
61
136
  },
62
- },
63
- });
64
- break;
65
- case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED:
66
- await this.localDeviceService.setBatteryLevel(deviceData.deviceId, event?.data?.batteryLevel ?? 0, constants_1.Source.CLOUD_EVENT);
67
- break;
137
+ });
138
+ break;
139
+ case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED:
140
+ await this.localDeviceService.setBatteryLevel(deviceData.deviceId, event?.data?.batteryLevel ?? 0, constants_1.Source.CLOUD_EVENT);
141
+ break;
142
+ }
68
143
  }
69
- }
70
- async extractHubDetails(device) {
71
- const hubIds = device?.hubId;
72
- const hubDetails = [];
73
- if (hubIds) {
74
- for (const hubId of hubIds) {
75
- const hubDetail = await this.localHubService.getHub(hubId);
76
- hubDetails.push({
77
- id: hubDetail?.deviceId,
78
- name: hubDetail?.name,
79
- type: hubDetail?.deviceType?.type,
80
- brand: hubDetail?.specifications?.manufacturer,
81
- model: hubDetail?.specifications?.model,
82
- status: hubDetail?.status?.online ? "ONLINE" : "OFFLINE",
144
+ async extractHubDetails(device) {
145
+ const hubIds = device?.hubId;
146
+ const hubDetails = [];
147
+ if (hubIds) {
148
+ for (const hubId of hubIds) {
149
+ const hubDetail = await this.localHubService.getHub(hubId);
150
+ hubDetails.push({
151
+ id: hubDetail?.deviceId,
152
+ name: hubDetail?.name,
153
+ type: hubDetail?.deviceType?.type,
154
+ brand: hubDetail?.specifications?.manufacturer,
155
+ model: hubDetail?.specifications?.model,
156
+ status: hubDetail?.status?.online ? "ONLINE" : "OFFLINE",
157
+ });
158
+ }
159
+ }
160
+ return hubDetails;
161
+ }
162
+ constructDeviceEventEntity(eventData, device, hubDetails, options = {}) {
163
+ return {
164
+ event: {
165
+ eventId: options.eventId || eventData?.eventId || "",
166
+ eventName: options.eventName || eventData?.eventName || "",
167
+ status: options.status || eventData?.status || "",
168
+ mode: options.mode || eventData?.data?.mode,
169
+ userName: options.userName || eventData?.userName || "",
170
+ userId: options.userId || eventData?.userId || "",
171
+ userType: options.userType || eventData?.userType || "",
172
+ raw_event: options.rawEvent || eventData?.rawData || {},
173
+ batteryLevel: options.batteryLevel || eventData?.data?.batteryLevel || "",
174
+ reason: options.reason || eventData?.data?.reason || "",
175
+ eventDescription: options.eventDescription || eventData?.event || "",
176
+ },
177
+ device: {
178
+ id: eventData?.deviceId || "",
179
+ name: device?.name || "",
180
+ type: device?.deviceType?.type || "",
181
+ brand: device?.specifications?.manufacturer || "",
182
+ model: device?.specifications?.model || "",
183
+ },
184
+ hub: hubDetails,
185
+ property: {
186
+ id: eventData?.property?.id || "",
187
+ name: eventData?.property?.name || "",
188
+ location: eventData?.property?.apartment || "",
189
+ },
190
+ };
191
+ }
192
+ async dumpToEventOperation(data, device) {
193
+ try {
194
+ const hubDetails = await this.extractHubDetails(device);
195
+ const eventData = this.constructDeviceEventEntity(data, device, hubDetails, {
196
+ status: data.eventName === "lock.unlock" ? "UNLOCKED" : "LOCKED",
197
+ rawEvent: data.rawData,
83
198
  });
199
+ this.logger.info("[DT | CDL]:[DeviceEventHandler]: Event data dumped to event operation", { eventData });
200
+ }
201
+ catch (error) {
202
+ this.logger.error("[DT | CDL]:[DeviceEventHandler]: Error dumping to event operation", error);
84
203
  }
85
204
  }
86
- return hubDetails;
87
- }
88
- constructDeviceEventEntity(eventData, device, hubDetails, options = {}) {
89
- return {
90
- event: {
91
- eventId: options.eventId || eventData?.eventId || "",
92
- eventName: options.eventName || eventData?.eventName || "",
93
- status: options.status || eventData?.status || "",
94
- mode: options.mode || eventData?.data?.mode,
95
- userName: options.userName || eventData?.userName || "",
96
- userId: options.userId || eventData?.userId || "",
97
- userType: options.userType || eventData?.userType || "",
98
- raw_event: options.rawEvent || eventData?.rawData || {},
99
- batteryLevel: options.batteryLevel || eventData?.data?.batteryLevel || "",
100
- reason: options.reason || eventData?.data?.reason || "",
101
- eventDescription: options.eventDescription || eventData?.event || "",
102
- },
103
- device: {
104
- id: eventData?.deviceId || "",
105
- name: device?.name || "",
106
- type: device?.deviceType?.type || "",
107
- brand: device?.specifications?.manufacturer || "",
108
- model: device?.specifications?.model || "",
109
- },
110
- hub: hubDetails,
111
- property: {
112
- id: eventData?.property?.id || "",
113
- name: eventData?.property?.name || "",
114
- location: eventData?.property?.apartment || "",
115
- },
116
- };
117
- }
118
- async dumpToEventOperation(data, device) {
119
- try {
120
- const hubDetails = await this.extractHubDetails(device);
121
- const eventData = this.constructDeviceEventEntity(data, device, hubDetails, {
122
- status: data.eventName === "lock.unlock" ? "UNLOCKED" : "LOCKED",
123
- rawEvent: data.rawData,
124
- });
125
- this.logger.info("[DT | CDL]:[DeviceEventHandler]: Event data dumped to event operation", { eventData });
205
+ async updateTablesBasedOnEventType(eventData, device) {
206
+ switch (eventData.eventName) {
207
+ case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE:
208
+ case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE:
209
+ case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.CHANGED:
210
+ this.updateDeviceStatus(eventData, device);
211
+ break;
212
+ case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED:
213
+ case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CRITICAL:
214
+ case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LOW:
215
+ this.updateDeviceBattery(eventData, device);
216
+ break;
217
+ }
126
218
  }
127
- catch (error) {
128
- this.logger.error("[DT | CDL]:[DeviceEventHandler]: Error dumping to event operation", error);
219
+ async updateDeviceStatus(data, device) {
220
+ this.logger.info("[DT | CDL]:[DeviceEventHandler]: Updating device status", {
221
+ data,
222
+ device,
223
+ });
129
224
  }
130
- }
131
- async updateTablesBasedOnEventType(eventData, device) {
132
- switch (eventData.eventName) {
133
- case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE:
134
- case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE:
135
- case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.CHANGED:
136
- this.updateDeviceStatus(eventData, device);
137
- break;
138
- case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED:
139
- case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CRITICAL:
140
- case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LOW:
141
- this.updateDeviceBattery(eventData, device);
142
- break;
225
+ async updateDeviceBattery(data, device) {
226
+ this.logger.info("[DT | CDL]:[DeviceEventHandler]: Updating device battery", {
227
+ data,
228
+ device,
229
+ });
143
230
  }
144
- }
145
- async updateDeviceStatus(data, device) {
146
- this.logger.info("[DT | CDL]:[DeviceEventHandler]: Updating device status", {
147
- data,
148
- device,
149
- });
150
- }
151
- async updateDeviceBattery(data, device) {
152
- this.logger.info("[DT | CDL]:[DeviceEventHandler]: Updating device battery", {
153
- data,
154
- device,
155
- });
156
- }
157
- }
231
+ };
232
+ __setFunctionName(_classThis, "DeviceEventHandler");
233
+ (() => {
234
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
235
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
236
+ DeviceEventHandler = _classThis = _classDescriptor.value;
237
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
238
+ __runInitializers(_classThis, _classExtraInitializers);
239
+ })();
240
+ return DeviceEventHandler = _classThis;
241
+ })();
158
242
  exports.DeviceEventHandler = DeviceEventHandler;
@@ -43,6 +43,16 @@ class RateLimitUtils {
43
43
  windowMs: 10000, // 10 seconds
44
44
  provider: constants_1.CONNECTION_PROVIDERS.CLOUDBEDS,
45
45
  });
46
+ configs.set(constants_1.CONNECTION_PROVIDERS.STAYNTOUCH, {
47
+ maxRequests: 10,
48
+ windowMs: 10000, // 10 seconds
49
+ provider: constants_1.CONNECTION_PROVIDERS.STAYNTOUCH,
50
+ });
51
+ configs.set(constants_1.CONNECTION_PROVIDERS.HOTELKEY, {
52
+ maxRequests: 10,
53
+ windowMs: 10000, // 10 seconds
54
+ provider: constants_1.CONNECTION_PROVIDERS.HOTELKEY,
55
+ });
46
56
  configs.set(constants_1.CONNECTION_PROVIDERS.TTLOCK, {
47
57
  maxRequests: 15,
48
58
  windowMs: 60000, // 1 minute
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dt-common-device",
3
- "version": "3.1.2",
3
+ "version": "3.1.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -10,9 +10,9 @@
10
10
  ],
11
11
  "scripts": {
12
12
  "build": "tsc",
13
- "patch": "npm version patch && npm run build && npm publish",
14
- "minor": "npm version minor && npm run build && npm publish",
15
- "major": "npm version major && npm run build && npm publish",
13
+ "patch": "npm version patch && npm run build && npm publish && git push",
14
+ "minor": "npm version minor && npm run build && npm publish && git push",
15
+ "major": "npm version major && npm run build && npm publish && git push",
16
16
  "security:audit": "npm audit --audit-level=moderate",
17
17
  "security:fix": "npm audit fix",
18
18
  "security:check": "npm audit && npm outdated",