edilkamin 1.10.1 → 1.11.0

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.
@@ -0,0 +1,387 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { strict as assert } from "assert";
11
+ import { aesDecrypt, aesEncrypt, crc16Modbus, createPacket, NOTIFY_CHARACTERISTIC_UUID, parseResponse, parsers, readCommands, SERVICE_UUID, WRITE_CHARACTERISTIC_UUID, writeCommands, } from "./bluetooth-protocol";
12
+ describe("bluetooth-protocol", () => {
13
+ describe("constants", () => {
14
+ it("exports SERVICE_UUID", () => {
15
+ assert.equal(SERVICE_UUID, "0000abf0-0000-1000-8000-00805f9b34fb");
16
+ });
17
+ it("exports WRITE_CHARACTERISTIC_UUID", () => {
18
+ assert.equal(WRITE_CHARACTERISTIC_UUID, "0000abf1-0000-1000-8000-00805f9b34fb");
19
+ });
20
+ it("exports NOTIFY_CHARACTERISTIC_UUID", () => {
21
+ assert.equal(NOTIFY_CHARACTERISTIC_UUID, "0000abf2-0000-1000-8000-00805f9b34fb");
22
+ });
23
+ });
24
+ describe("crc16Modbus", () => {
25
+ it("calculates correct CRC for power-on command", () => {
26
+ const command = new Uint8Array([0x01, 0x06, 0x03, 0x1c, 0x00, 0x01]);
27
+ const crc = crc16Modbus(command);
28
+ assert.equal(crc.length, 2);
29
+ // CRC is returned as [crcLo, crcHi]
30
+ assert.ok(crc[0] >= 0 && crc[0] <= 255);
31
+ assert.ok(crc[1] >= 0 && crc[1] <= 255);
32
+ });
33
+ it("returns 2 bytes for any input", () => {
34
+ const testCases = [
35
+ new Uint8Array([0x01, 0x03, 0x05, 0x25, 0x00, 0x01]),
36
+ new Uint8Array([0x01, 0x06, 0x04, 0x40, 0x00, 0x03]),
37
+ new Uint8Array([0x00]),
38
+ new Uint8Array([0xff, 0xff, 0xff]),
39
+ ];
40
+ for (const data of testCases) {
41
+ const crc = crc16Modbus(data);
42
+ assert.equal(crc.length, 2, `CRC for ${data.toString()} should be 2 bytes`);
43
+ }
44
+ });
45
+ it("produces different CRCs for different data", () => {
46
+ const crc1 = crc16Modbus(new Uint8Array([0x01, 0x06, 0x03, 0x1c, 0x00, 0x01]));
47
+ const crc2 = crc16Modbus(new Uint8Array([0x01, 0x06, 0x03, 0x1c, 0x00, 0x00]));
48
+ // CRCs should be different for different data
49
+ assert.ok(crc1[0] !== crc2[0] || crc1[1] !== crc2[1]);
50
+ });
51
+ });
52
+ describe("aesEncrypt/aesDecrypt", () => {
53
+ it("roundtrip returns original data", () => __awaiter(void 0, void 0, void 0, function* () {
54
+ const original = new Uint8Array(32).fill(0x42);
55
+ const encrypted = yield aesEncrypt(original);
56
+ const decrypted = yield aesDecrypt(encrypted);
57
+ assert.deepEqual(decrypted, original);
58
+ }));
59
+ it("produces 32-byte output for 32-byte input", () => __awaiter(void 0, void 0, void 0, function* () {
60
+ const input = new Uint8Array(32);
61
+ const encrypted = yield aesEncrypt(input);
62
+ assert.equal(encrypted.length, 32);
63
+ }));
64
+ it("produces different output for different input", () => __awaiter(void 0, void 0, void 0, function* () {
65
+ const input1 = new Uint8Array(32).fill(0x00);
66
+ const input2 = new Uint8Array(32).fill(0xff);
67
+ const encrypted1 = yield aesEncrypt(input1);
68
+ const encrypted2 = yield aesEncrypt(input2);
69
+ // At least some bytes should be different
70
+ let different = false;
71
+ for (let i = 0; i < 32; i++) {
72
+ if (encrypted1[i] !== encrypted2[i]) {
73
+ different = true;
74
+ break;
75
+ }
76
+ }
77
+ assert.ok(different, "Different inputs should produce different outputs");
78
+ }));
79
+ it("encrypted data is different from plaintext", () => __awaiter(void 0, void 0, void 0, function* () {
80
+ const original = new Uint8Array(32).fill(0xaa);
81
+ const encrypted = yield aesEncrypt(original);
82
+ // Encrypted should not equal original
83
+ let same = true;
84
+ for (let i = 0; i < 32; i++) {
85
+ if (encrypted[i] !== original[i]) {
86
+ same = false;
87
+ break;
88
+ }
89
+ }
90
+ assert.ok(!same, "Encrypted data should differ from original");
91
+ }));
92
+ });
93
+ describe("createPacket", () => {
94
+ it("produces 32-byte encrypted packet", () => __awaiter(void 0, void 0, void 0, function* () {
95
+ const command = readCommands.power;
96
+ const packet = yield createPacket(command);
97
+ assert.equal(packet.length, 32);
98
+ }));
99
+ it("rejects commands not exactly 6 bytes", () => __awaiter(void 0, void 0, void 0, function* () {
100
+ yield assert.rejects(() => createPacket(new Uint8Array([0x01, 0x02])), /must be exactly 6 bytes/);
101
+ }));
102
+ it("rejects empty commands", () => __awaiter(void 0, void 0, void 0, function* () {
103
+ yield assert.rejects(() => createPacket(new Uint8Array([])), /must be exactly 6 bytes/);
104
+ }));
105
+ it("rejects 7-byte commands", () => __awaiter(void 0, void 0, void 0, function* () {
106
+ yield assert.rejects(() => createPacket(new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07])), /must be exactly 6 bytes/);
107
+ }));
108
+ it("produces different packets for different commands", () => __awaiter(void 0, void 0, void 0, function* () {
109
+ const packet1 = yield createPacket(writeCommands.setPower(true));
110
+ const packet2 = yield createPacket(writeCommands.setPower(false));
111
+ // Packets should be different (different command bytes)
112
+ let different = false;
113
+ for (let i = 0; i < 32; i++) {
114
+ if (packet1[i] !== packet2[i]) {
115
+ different = true;
116
+ break;
117
+ }
118
+ }
119
+ assert.ok(different, "Different commands should produce different packets");
120
+ }));
121
+ });
122
+ describe("parseResponse", () => {
123
+ it("rejects responses not 32 bytes", () => __awaiter(void 0, void 0, void 0, function* () {
124
+ yield assert.rejects(() => parseResponse(new Uint8Array(16)), /must be exactly 32 bytes/);
125
+ }));
126
+ it("can decrypt and parse an encrypted packet", () => __awaiter(void 0, void 0, void 0, function* () {
127
+ // Create a command packet and try to parse it
128
+ // This tests that encrypt/decrypt work together
129
+ const command = readCommands.power;
130
+ const encrypted = yield createPacket(command);
131
+ // Parse the response (it's not a valid response but should decrypt)
132
+ const parsed = yield parseResponse(encrypted);
133
+ // Should have parsed something
134
+ assert.ok(typeof parsed.slaveAddress === "number");
135
+ assert.ok(typeof parsed.functionCode === "number");
136
+ assert.ok(typeof parsed.isError === "boolean");
137
+ }));
138
+ });
139
+ describe("readCommands", () => {
140
+ it("all commands are 6 bytes", () => {
141
+ Object.entries(readCommands).forEach(([name, cmd]) => {
142
+ assert.equal(cmd.length, 6, `${name} command must be 6 bytes`);
143
+ });
144
+ });
145
+ it("all commands use slave address 0x01", () => {
146
+ Object.entries(readCommands).forEach(([name, cmd]) => {
147
+ assert.equal(cmd[0], 0x01, `${name} should use slave address 0x01`);
148
+ });
149
+ });
150
+ it("all commands use function code 0x03", () => {
151
+ Object.entries(readCommands).forEach(([name, cmd]) => {
152
+ assert.equal(cmd[1], 0x03, `${name} should use function code 0x03`);
153
+ });
154
+ });
155
+ it("power command has correct register address", () => {
156
+ assert.equal(readCommands.power[2], 0x05);
157
+ assert.equal(readCommands.power[3], 0x29);
158
+ });
159
+ it("temperature command has correct register address", () => {
160
+ assert.equal(readCommands.temperature[2], 0x05);
161
+ assert.equal(readCommands.temperature[3], 0x25);
162
+ });
163
+ });
164
+ describe("writeCommands", () => {
165
+ it("setPower(true) produces correct bytes", () => {
166
+ const cmd = writeCommands.setPower(true);
167
+ assert.deepEqual(cmd, new Uint8Array([0x01, 0x06, 0x03, 0x1c, 0x00, 0x01]));
168
+ });
169
+ it("setPower(false) produces correct bytes", () => {
170
+ const cmd = writeCommands.setPower(false);
171
+ assert.deepEqual(cmd, new Uint8Array([0x01, 0x06, 0x03, 0x1c, 0x00, 0x00]));
172
+ });
173
+ it("setTemperature encodes correctly", () => {
174
+ const cmd = writeCommands.setTemperature(21.5);
175
+ // 21.5 * 10 = 215 = 0x00D7
176
+ assert.equal(cmd[0], 0x01); // slave address
177
+ assert.equal(cmd[1], 0x06); // function code
178
+ assert.equal(cmd[2], 0x05); // register hi
179
+ assert.equal(cmd[3], 0x25); // register lo
180
+ assert.equal(cmd[4], 0x00); // value hi
181
+ assert.equal(cmd[5], 0xd7); // value lo
182
+ });
183
+ it("setTemperature handles whole numbers", () => {
184
+ const cmd = writeCommands.setTemperature(20);
185
+ // 20 * 10 = 200 = 0x00C8
186
+ assert.equal(cmd[4], 0x00);
187
+ assert.equal(cmd[5], 0xc8);
188
+ });
189
+ it("setTemperature handles high temperatures", () => {
190
+ const cmd = writeCommands.setTemperature(30);
191
+ // 30 * 10 = 300 = 0x012C
192
+ assert.equal(cmd[4], 0x01);
193
+ assert.equal(cmd[5], 0x2c);
194
+ });
195
+ it("setPowerLevel validates range", () => {
196
+ assert.throws(() => writeCommands.setPowerLevel(0), /must be 1-5/);
197
+ assert.throws(() => writeCommands.setPowerLevel(6), /must be 1-5/);
198
+ assert.doesNotThrow(() => writeCommands.setPowerLevel(1));
199
+ assert.doesNotThrow(() => writeCommands.setPowerLevel(3));
200
+ assert.doesNotThrow(() => writeCommands.setPowerLevel(5));
201
+ });
202
+ it("setPowerLevel produces correct bytes", () => {
203
+ const cmd = writeCommands.setPowerLevel(3);
204
+ assert.equal(cmd[0], 0x01);
205
+ assert.equal(cmd[1], 0x06);
206
+ assert.equal(cmd[2], 0x04);
207
+ assert.equal(cmd[3], 0x40);
208
+ assert.equal(cmd[4], 0x00);
209
+ assert.equal(cmd[5], 0x03);
210
+ });
211
+ it("setFan1Speed validates range", () => {
212
+ assert.throws(() => writeCommands.setFan1Speed(-1), /must be 0-5/);
213
+ assert.throws(() => writeCommands.setFan1Speed(6), /must be 0-5/);
214
+ assert.doesNotThrow(() => writeCommands.setFan1Speed(0)); // auto
215
+ assert.doesNotThrow(() => writeCommands.setFan1Speed(5));
216
+ });
217
+ it("setFan2Speed validates range", () => {
218
+ assert.throws(() => writeCommands.setFan2Speed(-1), /must be 0-5/);
219
+ assert.throws(() => writeCommands.setFan2Speed(6), /must be 0-5/);
220
+ assert.doesNotThrow(() => writeCommands.setFan2Speed(0));
221
+ assert.doesNotThrow(() => writeCommands.setFan2Speed(5));
222
+ });
223
+ it("setAutoMode produces correct bytes", () => {
224
+ const cmdOn = writeCommands.setAutoMode(true);
225
+ assert.equal(cmdOn[5], 0x01);
226
+ const cmdOff = writeCommands.setAutoMode(false);
227
+ assert.equal(cmdOff[5], 0x00);
228
+ });
229
+ it("setStandby produces correct bytes", () => {
230
+ const cmdOn = writeCommands.setStandby(true);
231
+ assert.equal(cmdOn[5], 0x01);
232
+ const cmdOff = writeCommands.setStandby(false);
233
+ assert.equal(cmdOff[5], 0x00);
234
+ });
235
+ it("all write commands are 6 bytes", () => {
236
+ const commands = [
237
+ writeCommands.setPower(true),
238
+ writeCommands.setTemperature(21),
239
+ writeCommands.setPowerLevel(3),
240
+ writeCommands.setFan1Speed(2),
241
+ writeCommands.setFan2Speed(2),
242
+ writeCommands.setAutoMode(true),
243
+ writeCommands.setStandby(false),
244
+ ];
245
+ commands.forEach((cmd, i) => {
246
+ assert.equal(cmd.length, 6, `Command ${i} should be 6 bytes`);
247
+ });
248
+ });
249
+ it("all write commands use function code 0x06", () => {
250
+ const commands = [
251
+ writeCommands.setPower(true),
252
+ writeCommands.setTemperature(21),
253
+ writeCommands.setPowerLevel(3),
254
+ writeCommands.setFan1Speed(2),
255
+ writeCommands.setFan2Speed(2),
256
+ writeCommands.setAutoMode(true),
257
+ writeCommands.setStandby(false),
258
+ ];
259
+ commands.forEach((cmd, i) => {
260
+ assert.equal(cmd[1], 0x06, `Command ${i} should use function code 0x06`);
261
+ });
262
+ });
263
+ });
264
+ describe("parsers", () => {
265
+ it("boolean parser returns true for 0x01", () => {
266
+ const response = {
267
+ slaveAddress: 1,
268
+ functionCode: 0x03,
269
+ byteCount: 2,
270
+ data: new Uint8Array([0x00, 0x01]),
271
+ isError: false,
272
+ };
273
+ assert.equal(parsers.boolean(response), true);
274
+ });
275
+ it("boolean parser returns false for 0x00", () => {
276
+ const response = {
277
+ slaveAddress: 1,
278
+ functionCode: 0x03,
279
+ byteCount: 2,
280
+ data: new Uint8Array([0x00, 0x00]),
281
+ isError: false,
282
+ };
283
+ assert.equal(parsers.boolean(response), false);
284
+ });
285
+ it("temperature parser divides by 10", () => {
286
+ const response = {
287
+ slaveAddress: 1,
288
+ functionCode: 0x03,
289
+ byteCount: 2,
290
+ data: new Uint8Array([0x00, 0xd7]), // 215
291
+ isError: false,
292
+ };
293
+ assert.equal(parsers.temperature(response), 21.5);
294
+ });
295
+ it("temperature parser handles high temperatures", () => {
296
+ const response = {
297
+ slaveAddress: 1,
298
+ functionCode: 0x03,
299
+ byteCount: 2,
300
+ data: new Uint8Array([0x01, 0x2c]), // 300
301
+ isError: false,
302
+ };
303
+ assert.equal(parsers.temperature(response), 30);
304
+ });
305
+ it("number parser returns big-endian value", () => {
306
+ const response = {
307
+ slaveAddress: 1,
308
+ functionCode: 0x03,
309
+ byteCount: 2,
310
+ data: new Uint8Array([0x00, 0x03]), // power level 3
311
+ isError: false,
312
+ };
313
+ assert.equal(parsers.number(response), 3);
314
+ });
315
+ it("number parser handles larger values", () => {
316
+ const response = {
317
+ slaveAddress: 1,
318
+ functionCode: 0x03,
319
+ byteCount: 2,
320
+ data: new Uint8Array([0x01, 0x00]), // 256
321
+ isError: false,
322
+ };
323
+ assert.equal(parsers.number(response), 256);
324
+ });
325
+ it("boolean parser throws on error response", () => {
326
+ const errorResponse = {
327
+ slaveAddress: 1,
328
+ functionCode: 0x03,
329
+ data: new Uint8Array([0x02]), // error code
330
+ isError: true,
331
+ };
332
+ assert.throws(() => parsers.boolean(errorResponse), /Modbus error: 2/);
333
+ });
334
+ it("temperature parser throws on error response", () => {
335
+ const errorResponse = {
336
+ slaveAddress: 1,
337
+ functionCode: 0x03,
338
+ data: new Uint8Array([0x03]),
339
+ isError: true,
340
+ };
341
+ assert.throws(() => parsers.temperature(errorResponse), /Modbus error: 3/);
342
+ });
343
+ it("number parser throws on error response", () => {
344
+ const errorResponse = {
345
+ slaveAddress: 1,
346
+ functionCode: 0x03,
347
+ data: new Uint8Array([0x04]),
348
+ isError: true,
349
+ };
350
+ assert.throws(() => parsers.number(errorResponse), /Modbus error: 4/);
351
+ });
352
+ });
353
+ describe("integration", () => {
354
+ it("full roundtrip: create packet, decrypt, check structure", () => __awaiter(void 0, void 0, void 0, function* () {
355
+ // Create a power-on command
356
+ const command = writeCommands.setPower(true);
357
+ assert.deepEqual(command, new Uint8Array([0x01, 0x06, 0x03, 0x1c, 0x00, 0x01]));
358
+ // Create encrypted packet
359
+ const packet = yield createPacket(command);
360
+ assert.equal(packet.length, 32);
361
+ // Decrypt it back (as if we received it)
362
+ // Note: createPacket encrypts with padding, parseResponse expects response format
363
+ // This is not a true response but tests the encryption layer
364
+ }));
365
+ it("all read commands can create valid packets", () => __awaiter(void 0, void 0, void 0, function* () {
366
+ for (const [name, command] of Object.entries(readCommands)) {
367
+ const packet = yield createPacket(command);
368
+ assert.equal(packet.length, 32, `${name} should create 32-byte packet`);
369
+ }
370
+ }));
371
+ it("all write commands can create valid packets", () => __awaiter(void 0, void 0, void 0, function* () {
372
+ const commands = [
373
+ { name: "setPower", cmd: writeCommands.setPower(true) },
374
+ { name: "setTemperature", cmd: writeCommands.setTemperature(21) },
375
+ { name: "setPowerLevel", cmd: writeCommands.setPowerLevel(3) },
376
+ { name: "setFan1Speed", cmd: writeCommands.setFan1Speed(2) },
377
+ { name: "setFan2Speed", cmd: writeCommands.setFan2Speed(2) },
378
+ { name: "setAutoMode", cmd: writeCommands.setAutoMode(true) },
379
+ { name: "setStandby", cmd: writeCommands.setStandby(false) },
380
+ ];
381
+ for (const { name, cmd } of commands) {
382
+ const packet = yield createPacket(cmd);
383
+ assert.equal(packet.length, 32, `${name} should create 32-byte packet`);
384
+ }
385
+ }));
386
+ });
387
+ });
@@ -38,3 +38,5 @@ declare const scanForDevices: () => Promise<DiscoveredDevice[]>;
38
38
  declare const scanWithOptions: (options: RequestDeviceOptions) => Promise<BluetoothDevice>;
39
39
  export { EDILKAMIN_DEVICE_NAME, EDILKAMIN_SERVICE_UUID, isWebBluetoothSupported, scanForDevices, scanWithOptions, };
40
40
  export type { DiscoveredDevice } from "./types";
41
+ export { aesDecrypt, aesEncrypt, crc16Modbus, createPacket, NOTIFY_CHARACTERISTIC_UUID, parseResponse, parsers, readCommands, SERVICE_UUID, WRITE_CHARACTERISTIC_UUID, writeCommands, } from "./bluetooth-protocol";
42
+ export type { ModbusResponse } from "./bluetooth-protocol";
@@ -98,3 +98,11 @@ const scanWithOptions = (options) => __awaiter(void 0, void 0, void 0, function*
98
98
  return navigator.bluetooth.requestDevice(options);
99
99
  });
100
100
  export { EDILKAMIN_DEVICE_NAME, EDILKAMIN_SERVICE_UUID, isWebBluetoothSupported, scanForDevices, scanWithOptions, };
101
+ // Protocol functions
102
+ export { aesDecrypt, aesEncrypt, crc16Modbus, createPacket,
103
+ // Constants
104
+ NOTIFY_CHARACTERISTIC_UUID, parseResponse,
105
+ // Commands
106
+ parsers, readCommands, SERVICE_UUID, WRITE_CHARACTERISTIC_UUID,
107
+ // Parsers
108
+ writeCommands, } from "./bluetooth-protocol";
@@ -1,7 +1,7 @@
1
1
  export { bleToWifiMac } from "./bluetooth-utils";
2
2
  export { decompressBuffer, isBuffer, processResponse } from "./buffer-utils";
3
3
  export { API_URL, NEW_API_URL, OLD_API_URL } from "./constants";
4
- export { configure, getSession, signIn } from "./library";
4
+ export { configure, deriveUsageAnalytics, getSession, signIn } from "./library";
5
5
  export { serialNumberDisplay, serialNumberFromHex, serialNumberToHex, } from "./serial-utils";
6
6
  export { AlarmEntryType, AlarmsLogType, BufferEncodedType, CommandsType, DeviceAssociationBody, DeviceAssociationResponse, DeviceInfoRawType, DeviceInfoType, DiscoveredDevice, EditDeviceAssociationBody, PowerDistributionType, RegenerationDataType, ServiceCountersType, ServiceStatusType, StatusCountersType, StatusType, TemperaturesType, TotalCountersType, UsageAnalyticsType, UserParametersType, } from "./types";
7
7
  export { AlarmCode, AlarmDescriptions } from "./types";
@@ -2,7 +2,7 @@ import { configure } from "./library";
2
2
  export { bleToWifiMac } from "./bluetooth-utils";
3
3
  export { decompressBuffer, isBuffer, processResponse } from "./buffer-utils";
4
4
  export { API_URL, NEW_API_URL, OLD_API_URL } from "./constants";
5
- export { configure, getSession, signIn } from "./library";
5
+ export { configure, deriveUsageAnalytics, getSession, signIn } from "./library";
6
6
  export { serialNumberDisplay, serialNumberFromHex, serialNumberToHex, } from "./serial-utils";
7
7
  export { AlarmCode, AlarmDescriptions } from "./types";
8
8
  export const { deviceInfo, registerDevice, editDevice, setPower, setPowerOff, setPowerOn, getPower, getEnvironmentTemperature, getTargetTemperature, setTargetTemperature, } = configure();
@@ -29,6 +29,22 @@ declare const createAuthService: (auth: typeof amplifyAuth) => {
29
29
  getSession: (forceRefresh?: boolean, legacy?: boolean) => Promise<string>;
30
30
  };
31
31
  declare const signIn: (username: string, password: string, legacy?: boolean) => Promise<string>, getSession: (forceRefresh?: boolean, legacy?: boolean) => Promise<string>;
32
+ /**
33
+ * Derives usage analytics from an existing DeviceInfo response.
34
+ * This is a pure function that performs client-side calculations without API calls.
35
+ *
36
+ * Use this when you already have a DeviceInfo object (e.g., from a previous deviceInfo() call)
37
+ * to avoid making an additional API request.
38
+ *
39
+ * @param {DeviceInfoType} deviceInfo - The device info response object.
40
+ * @param {number} [serviceThreshold=2000] - Service threshold in hours.
41
+ * @returns {UsageAnalyticsType} - Comprehensive usage analytics.
42
+ *
43
+ * @example
44
+ * const info = await api.deviceInfo(token, mac);
45
+ * const analytics = deriveUsageAnalytics(info);
46
+ */
47
+ export declare const deriveUsageAnalytics: (deviceInfo: DeviceInfoType, serviceThreshold?: number) => UsageAnalyticsType;
32
48
  /**
33
49
  * Configures the library for API interactions.
34
50
  * Initializes API methods with a specified base URL.
@@ -543,6 +543,62 @@ const getServiceTime = (baseURL) =>
543
543
  * Most devices use 2000 hours.
544
544
  */
545
545
  const DEFAULT_SERVICE_THRESHOLD = 2000;
546
+ /**
547
+ * Derives usage analytics from an existing DeviceInfo response.
548
+ * This is a pure function that performs client-side calculations without API calls.
549
+ *
550
+ * Use this when you already have a DeviceInfo object (e.g., from a previous deviceInfo() call)
551
+ * to avoid making an additional API request.
552
+ *
553
+ * @param {DeviceInfoType} deviceInfo - The device info response object.
554
+ * @param {number} [serviceThreshold=2000] - Service threshold in hours.
555
+ * @returns {UsageAnalyticsType} - Comprehensive usage analytics.
556
+ *
557
+ * @example
558
+ * const info = await api.deviceInfo(token, mac);
559
+ * const analytics = deriveUsageAnalytics(info);
560
+ */
561
+ export const deriveUsageAnalytics = (deviceInfo, serviceThreshold = DEFAULT_SERVICE_THRESHOLD) => {
562
+ const totalCounters = deviceInfo.nvm.total_counters;
563
+ const serviceCounters = deviceInfo.nvm.service_counters;
564
+ const regeneration = deviceInfo.nvm.regeneration;
565
+ const alarmsLog = deviceInfo.nvm.alarms_log;
566
+ const totalOperatingHours = totalCounters.p1_working_time +
567
+ totalCounters.p2_working_time +
568
+ totalCounters.p3_working_time +
569
+ totalCounters.p4_working_time +
570
+ totalCounters.p5_working_time;
571
+ const hoursSinceService = serviceCounters.p1_working_time +
572
+ serviceCounters.p2_working_time +
573
+ serviceCounters.p3_working_time +
574
+ serviceCounters.p4_working_time +
575
+ serviceCounters.p5_working_time;
576
+ const powerDistribution = totalOperatingHours === 0
577
+ ? { p1: 0, p2: 0, p3: 0, p4: 0, p5: 0 }
578
+ : {
579
+ p1: (totalCounters.p1_working_time / totalOperatingHours) * 100,
580
+ p2: (totalCounters.p2_working_time / totalOperatingHours) * 100,
581
+ p3: (totalCounters.p3_working_time / totalOperatingHours) * 100,
582
+ p4: (totalCounters.p4_working_time / totalOperatingHours) * 100,
583
+ p5: (totalCounters.p5_working_time / totalOperatingHours) * 100,
584
+ };
585
+ return {
586
+ totalPowerOns: totalCounters.power_ons,
587
+ totalOperatingHours,
588
+ powerDistribution,
589
+ serviceStatus: {
590
+ totalServiceHours: deviceInfo.status.counters.service_time,
591
+ hoursSinceService,
592
+ serviceThresholdHours: serviceThreshold,
593
+ isServiceDue: hoursSinceService >= serviceThreshold,
594
+ },
595
+ blackoutCount: regeneration.blackout_counter,
596
+ lastMaintenanceDate: regeneration.last_intervention > 0
597
+ ? new Date(regeneration.last_intervention * 1000)
598
+ : null,
599
+ alarmCount: alarmsLog.number,
600
+ };
601
+ };
546
602
  const getTotalOperatingHours = (baseURL) =>
547
603
  /**
548
604
  * Calculates total operating hours across all power levels.
@@ -621,45 +677,7 @@ const getUsageAnalytics = (baseURL) =>
621
677
  */
622
678
  (jwtToken_1, macAddress_1, ...args_1) => __awaiter(void 0, [jwtToken_1, macAddress_1, ...args_1], void 0, function* (jwtToken, macAddress, serviceThreshold = DEFAULT_SERVICE_THRESHOLD) {
623
679
  const info = yield deviceInfo(baseURL)(jwtToken, macAddress);
624
- const totalCounters = info.nvm.total_counters;
625
- const serviceCounters = info.nvm.service_counters;
626
- const regeneration = info.nvm.regeneration;
627
- const alarmsLog = info.nvm.alarms_log;
628
- const totalOperatingHours = totalCounters.p1_working_time +
629
- totalCounters.p2_working_time +
630
- totalCounters.p3_working_time +
631
- totalCounters.p4_working_time +
632
- totalCounters.p5_working_time;
633
- const hoursSinceService = serviceCounters.p1_working_time +
634
- serviceCounters.p2_working_time +
635
- serviceCounters.p3_working_time +
636
- serviceCounters.p4_working_time +
637
- serviceCounters.p5_working_time;
638
- const powerDistribution = totalOperatingHours === 0
639
- ? { p1: 0, p2: 0, p3: 0, p4: 0, p5: 0 }
640
- : {
641
- p1: (totalCounters.p1_working_time / totalOperatingHours) * 100,
642
- p2: (totalCounters.p2_working_time / totalOperatingHours) * 100,
643
- p3: (totalCounters.p3_working_time / totalOperatingHours) * 100,
644
- p4: (totalCounters.p4_working_time / totalOperatingHours) * 100,
645
- p5: (totalCounters.p5_working_time / totalOperatingHours) * 100,
646
- };
647
- return {
648
- totalPowerOns: totalCounters.power_ons,
649
- totalOperatingHours,
650
- powerDistribution,
651
- serviceStatus: {
652
- totalServiceHours: info.status.counters.service_time,
653
- hoursSinceService,
654
- serviceThresholdHours: serviceThreshold,
655
- isServiceDue: hoursSinceService >= serviceThreshold,
656
- },
657
- blackoutCount: regeneration.blackout_counter,
658
- lastMaintenanceDate: regeneration.last_intervention > 0
659
- ? new Date(regeneration.last_intervention * 1000)
660
- : null,
661
- alarmCount: alarmsLog.number,
662
- };
680
+ return deriveUsageAnalytics(info, serviceThreshold);
663
681
  });
664
682
  const registerDevice = (baseURL) =>
665
683
  /**