-
Notifications
You must be signed in to change notification settings - Fork 4
/
usb-power-profiling.js
1464 lines (1300 loc) · 45.9 KB
/
usb-power-profiling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const { usb, getDeviceList } = require('usb');
const HID = require('node-hid');
const http = require('http');
const url = require('url');
const { createDecipheriv } = require('node:crypto');
const { SerialPort } = require('serialport');
const { CRC } = require('crc-full');
const serveHandler = require('serve-handler');
const CHARGER_LAB_VENDOR_ID = 0x5FC9;
const FNIRSI_VENDOR_ID = 0x2E3C;
const KINGMETER_VENDOR_ID = 0x416;
const CHARGER_LAB_BLUE_VENDOR_ID = 0x472;
const GENERIC_VENDOR_ID = 0x483;
const SHIZUKU_PRODUCT_IDS = [0xFFFF, 0xFFFE, 0x374B];
const FNIRSI_PRODUCT_IDS = [0x3A, 0x3B];
const KINGMETER_PRODUCT_IDS = [0x5750, 0x5f50];
const WITRN_VENDOR_ID = 0x716;
const RUIDENG_VENDOR_ID = 0x28e9;
const YZXSTUDIO_VENDOR_ID = 0x1a86;
const DEBUG = false;//true;
const DEBUG_log = DEBUG ? console.log : () => {};
const MAX_SAMPLES = 4000000; // About 1.5h at 1kHz.
const gDevices = [];
var startTime, startPerformanceNow;
var gClosing;
function LogSampling() {
console.log(new Date(), "Sampling...");
}
function roundToNanoSecondPrecision(timeMs) {
return Math.round(timeMs * 1e6) / 1e6;
}
function int32Bytes(number) {
const buffer = Buffer.alloc(4);
buffer.writeInt32LE(number);
return buffer;
}
function resetDevice(device) {
return new Promise((resolve, reject) => device.reset(error => {
if (error) {
console.log("failed to reset device", error);
reject(error);
} else {
resolve();
}
}));
}
function sendBuffer(endPointOut, buffer) {
return new Promise((resolve, reject) => {
endPointOut.transfer(buffer, err => {
if (err) {
console.log("error while sending:", err);
reject(err);
}
resolve();
});
});
}
function findBulkInOutEndPoints(device) {
let endPointIn, endPointOut;
for (let interface of device.interfaces) {
let claimed = false;
for (let endPoint of interface.endpoints) {
if (endPoint.transferType != usb.LIBUSB_TRANSFER_TYPE_BULK) {
continue;
}
if (endPoint.direction == "in" && !endPointIn) {
endPointIn = endPoint;
if (!claimed) {
claimed = true;
interface.claim();
}
}
if (endPoint.direction == "out" && !endPointOut) {
endPointOut = endPoint;
if (interface.isKernelDriverActive()) {
try {
// Only required on linux to be able to claim
// the interface, otherwise a LIBUSB_ERROR_BUSY error
// is thrown
interface.detachKernelDriver();
} catch (e) {
// Throws failure on non-linux platforms
}
}
if (!claimed) {
claimed = true;
interface.claim();
}
}
}
}
return [endPointIn, endPointOut];
}
function addSample(self, time, power) {
// Limit the time precision to 1 µs.
time = Math.round(time * 1000) / 1000;
if (self.sampleTimes.length < MAX_SAMPLES) {
// Increase the buffer size if we have not reached MAX_SAMPLES yet.
self.sampleTimes.push(time);
self.samples.push(power);
} else {
// Otherwise, treat it as a ring buffer to avoid the cost of moving memory.
self.samples[self.firstSample] = power;
self.sampleTimes[self.firstSample] = time;
self.firstSample = (self.firstSample + 1) % MAX_SAMPLES;
}
}
function reorderSamples(self) {
if (self.firstSample == 0) {
return;
}
let samplesEnd = self.samples.splice(0, self.firstSample);
self.samples = self.samples.concat(samplesEnd);
let sampleTimesEnd = self.sampleTimes.splice(0, self.firstSample);
self.sampleTimes = self.sampleTimes.concat(sampleTimesEnd);
self.firstSample = 0;
}
function PowerZDevice(device) {
this.endPointIn = null;
this.endPointOut = null;
}
PowerZDevice.prototype = {
async sample() {
const CMD_GET_DATA = 0x0C;
const ATT_ADC = 0x001;
await sendBuffer(this.endPointOut,
Buffer.from([CMD_GET_DATA, 0, ATT_ADC << 1, 0]));
let data = await new Promise((resolve, reject) => {
this.endPointIn.transfer(64, (error, data) => {
if (error) {
console.log("error reading data", error);
reject(error);
}
resolve(data);
});
});
let v = data.readInt32LE(8);
let i = data.readInt32LE(12);
return (v / 1e6) * (i / 1e6);
},
async startSampling() {
try {
await resetDevice(this.device);
[this.endPointIn, this.endPointOut] = findBulkInOutEndPoints(this.device);
} catch(e) {
console.log(e);
}
if (!this.endPointOut || !this.endPointIn) {
console.log("failed to find endpoints");
return;
}
LogSampling();
let previousW = 0;
let w = 0;
this.interval = setInterval(async () => {
if (this._promise) {
return;
}
do {
try {
this._promise = this.sample();
w = Math.abs(await this._promise);
this._promise = null;
if (gClosing) {
return;
}
} catch(e) {
if (e.code == "ERR_BUFFER_OUT_OF_BOUNDS") {
// We sometimes get this error on the first sample when the previous
// shutdown wasn't clean.
// The next samples work fine, so just ignore the error.
continue;
}
console.log("aborting sampling", e);
return this.stopSampling();
}
} while (w == previousW);
previousW = w;
addSample(this,
roundToNanoSecondPrecision(performance.now() - startPerformanceNow),
w);
}, 1);
},
async stopSampling() {
clearInterval(this.interval);
if (this._promise) {
try {
await this._promise;
} catch(e) {}
}
this.endPointIn = null;
this.endPointOut = null;
}
};
function FnirsiDevice(device) {
}
FnirsiDevice.prototype = {
_crc: new CRC("CRC", 8, 0x39, 0x42),
checksum(data) { return this._crc.compute(data.slice(1,63)); },
COMMON_PREFIX: 0xAA,
// Not sure what this does, things work as well without sending it.
CMD_INIT: 0x81,
// Request samples from the device. Every 40ms the device will send a data
// packet containing 4 samples. Sampling will stop after 1s.
CMD_START_SAMPLING: 0x82,
// If sent before the end of the 1s sampling time, this command will make
// sampling continue for another 1s. Not sure why this command is needed,
// as just repeatedly sending CMD_START_SAMPLING seems to work just fine.
CMD_CONTINUE_SAMPLING: 0x83,
sendCommand(cmd) {
var outData = new Array(64).fill(0);
outData[0] = this.COMMON_PREFIX;
outData[1] = cmd;
outData[63] = this.checksum(outData);
return this.hidDevice.write(outData);
},
async startSampling() {
var previousSampleTime = performance.now();
const {idVendor, idProduct} = this.device.deviceDescriptor;
this.hidDevice = new HID.HID(idVendor, idProduct);
this.hidDevice.on('data', data => {
if (data[0] != this.COMMON_PREFIX || data[1] != 0x04) {
// ignore when not a data packet.
return;
}
const timeBetweenPackets = 40;
const samplesPerPacket = 4;
const intervalBetweenSamples = timeBetweenPackets / samplesPerPacket;
// The sampling is driven by the device, so it happens at a consistent
// rate. We sometimes have late packets, adjust the timestamps.
let now = performance.now();
const delta = now - previousSampleTime - timeBetweenPackets;
// Only adjust if the delay is reasonable.
if (delta > intervalBetweenSamples / 4 &&
delta < timeBetweenPackets) {
now -= delta;
}
previousSampleTime = now;
const sampleTime = now - startPerformanceNow;
if (data[63] != this.checksum(data)) {
console.log("Invalid CRC:", data[63], "computed:",
this.checksum(data));
}
for (sampleId = 0; sampleId < samplesPerPacket; ++sampleId) {
const offset = 2 + 15 * sampleId;
const voltage = data.readInt32LE(offset) / 1e5;
const current = data.readInt32LE(offset + 4) / 1e5;
const timeOffset =
intervalBetweenSamples * (samplesPerPacket - 1 - sampleId);
addSample(this, roundToNanoSecondPrecision(sampleTime - timeOffset),
voltage * current)
}
});
this.hidDevice.on('error', err => {
console.log("hid device error:", err);
clearInterval(this.timerId);
});
await this.sendCommand(this.CMD_START_SAMPLING);
LogSampling();
this.timerId = setInterval(async () => {
if (gClosing) {
return;
}
// Could send CMD_CONTINUE_SAMPLING instead, but if somehow we are late
// and sampling had already stopped, it would be ignored.
// CMD_START_SAMPLING works in all cases.
try {
await this.sendCommand(this.CMD_START_SAMPLING);
} catch(e) {
console.log("error sending command:", e);
}
}, 500); // Should be at least every second.
},
stopSampling() {
clearInterval(this.timerId);
this.hidDevice = null;
}
};
function ShizukuDevice(device) {
this.endPointIn = null;
this.endPointOut = null;
this.lastRequestId = 0;
this.expectedReplies = {};
}
ShizukuDevice.prototype = {
samplingInterval: 1, // value in ms.
BEGIN_DATA: 0xA5,
END_DATA: 0x5A,
CMD_STOP: 0x7,
CMD_START_SAMPLING: 0x9,
checksum(data) { return data.reduce((acc, curr) => acc ^ curr); },
async sendCommand(cmd, args = []) {
const promise = new Promise(resolve => {
this.expectedReplies[this.lastRequestId] = resolve;
});
const COMMON_REQUEST_PREFIX = 0x1;
const data = [COMMON_REQUEST_PREFIX, cmd, this.lastRequestId++, 0, ...args];
DEBUG_log("sending command", Buffer.from(data));
await sendBuffer(this.endPointOut,
Buffer.from([this.BEGIN_DATA,
...int32Bytes(data.length), ...data,
this.checksum(data), this.END_DATA]));
return promise;
},
samplingRequestId: -1,
initialTimeStamp: 0,
initialPerformanceNow: 0,
_pendingData: null,
ondata(data) {
if (this._pendingData) {
DEBUG_log("using _pendingData", this._pendingData, "and new data", data);
data = Buffer.concat([this._pendingData, data]);
this._pendingData = null;
}
if (data.length < 1 || data[0] != this.BEGIN_DATA) {
console.log("ignoring a bogus piece of data", data);
return;
}
const minLength =
1 /* BEGIN_DATA */ + 4 /* 32bit length */ +
1 /* checksum */ + 1 /* END_DATA */;
if (data.length < minLength) {
DEBUG_log("not a full data packet, keeping this piece of data for later", data);
this._pendingData = data;
return;
}
const length = data.readInt32LE(1);
if (data.length < minLength + length) {
DEBUG_log("not a full data packet, keeping this piece of data for later", data);
this._pendingData = data;
return;
}
const payloadStart = 1 + 4; // 1 for the header, 4 for the 32 bit length
let payload = data.slice(payloadStart, payloadStart + length);
if (this.checksum(payload) != data[payloadStart + length]) {
console.log("invalid checksum, expected:", this.checksum(payload),
"got:", data[payloadStart + length],
"payload:", payload);
return;
}
let nextData = null;
if (data.length > minLength + length) {
nextData = data.slice(minLength + length);
DEBUG_log("next data", nextData);
}
// Check if we received a reply we were waiting for.
// Unsure about the meaning of the first 2 bytes.
if (payload.length == 4 &&
payload[3] == 0x80 && // seems to mean "reply"
this.expectedReplies.hasOwnProperty(payload[2])) {
DEBUG_log("got a reply we were waiting for", payload);
this.expectedReplies[payload[2]]();
delete this.expectedReplies[payload[2]];
if (nextData) {
DEBUG_log("processing next data");
this.ondata(nextData);
}
return;
}
// At this point ignore anything that doesn't look like a sample.
if (payload.length < 4 /* header */ +
2 * 4 /* 2 32 bit floats */ +
8 /* 64 bit timestamp */ ||
payload[0] != 0x4 || // Not sure what this means. Maybe 'data' packet?
payload[1] != 0 || // ?? Maybe this was a 16 bit value.
payload[2] != this.samplingRequestId ||
payload[3] != 0) { // Seems to be 0x80 for replies and 0 otherwise.
console.log("ignoring unexpected payload", payload, JSON.stringify(this.expectedReplies));
if (nextData) {
DEBUG_log("processing next data");
this.ondata(nextData);
}
return;
}
const voltage = payload.readFloatLE(4);
const current = Math.abs(payload.readFloatLE(8));
// Seems to be the time in µs since the power meter was started.
const timestamp = payload.readBigUInt64LE(payload.length - 8);
if (!this.initialTimeStamp) {
this.initialTimeStamp = timestamp;
this.initialPerformanceNow = performance.now() - startPerformanceNow;
}
const time =
this.initialPerformanceNow + Number(timestamp - this.initialTimeStamp) / 1000;
const power = Math.round(voltage * current * 1e4) / 1e4;
DEBUG_log(new Date(), power);
addSample(this, roundToNanoSecondPrecision(time), power);
if (nextData) {
DEBUG_log("processing next data");
this.ondata(nextData);
}
},
async startSampling() {
this.deviceName = this.deviceName.replace(/ in Application Mode$/, "");
try {
await resetDevice(this.device);
} catch(e) {
// resetDevice already logs the error.
}
try {
[this.endPointIn, this.endPointOut] = findBulkInOutEndPoints(this.device);
} catch(e) {
console.log(e);
}
if (!this.endPointOut || !this.endPointIn) {
console.log("failed to find endpoints");
return;
}
// When sampling every 1ms, the default of keeping 3 data blocks pending
// in the kernel is not enough, as it's easy for our thread to be blocked
// for more than 3ms.
// 1024 is the maximum and allows us to recover if the main thread was
// blocked up to about 1.8s.
this.endPointIn.startPoll(1024);
this.endPointIn.on('data', data => this.ondata(data))
this.endPointIn.on('error', err => {
console.log("Error:", err);
this.endPointOut = null;
});
DEBUG_log("sending CMD_STOP before we start sampling");
await this.sendCommand(this.CMD_STOP);
this.samplingRequestId = this.lastRequestId;
await this.sendCommand(this.CMD_START_SAMPLING,
int32Bytes(this.samplingInterval));
LogSampling();
},
async stopSampling() {
if (!this.endPointOut) {
if (DEBUG) {
console.log("already in the process of stopping sampling");
}
return;
}
DEBUG_log("sending CMD_STOP");
const stopPromise = this.sendCommand(this.CMD_STOP);
this.endPointOut = null;
await stopPromise;
await new Promise(resolve => this.endPointIn.stopPoll(resolve));
this.endPointIn = null;
}
};
function KingMeterDevice(device) {
}
KingMeterDevice.prototype = {
_crc: CRC.default("CRC16_MODBUS"),
COMMON_PREFIX: 0x55,
// The following 4 commands are sent by the original Windows software
// after finding the device.
// They don't seem to be needed to get samples, so they are probably used to
// retrieve data like the firmware version number.
// [0x10, 0x02],
// [0x10, 0x03],
// [0x10, 0x04],
// [0x22, 0x80, 0xf1, 0x00],
// Will get 200 samples on the KingMeter, and only one on Atorch devices.
CMD_GET_SAMPLES: [0x22, 0x05, 0x0b, 0x00],
// The name of these commands matches the label of the UI element in the
// original Windows software. The actual sampling rate they produce doesn't
// really match, and is indicated in the comments.
CMD_1000SPS: [0x31, 0x1a, 0xb1, 0x01, 0x04], // every 2ms
CMD_100SPS: [0x31, 0x1a, 0xb1, 0x01, 0x03], // every 10ms
CMD_50SPS: [0x31, 0x1a, 0xb1, 0x01, 0x02], // every 20ms
CMD_10SPS: [0x31, 0x1a, 0xb1, 0x01, 0x01], // every 100ms
CMD_1SPS: [0x31, 0x1a, 0xb1, 0x01, 0x00], // every 100ms
sendCommand(cmd) {
// All commands are sent in a 64 buffer. The first byte is always the same,
// the second byte seems to be the length of the command, then there's a
// simple checksum on one byte followed by a CRC16.
var outData = new Array(64).fill(0);
let i = 0;
outData[i++] = this.COMMON_PREFIX;
outData[i++] = cmd.length + 1;
while (i < cmd.length + 2) {
outData[i] = cmd[i - 2];
++i;
}
let array = outData.slice(0, i);
outData[i++] = array.reduce((a,b) => (a + b) & 0xff);
if (this.isAtorch) {
outData[i++] = 0xee;
outData[i] = 0xff;
} else {
let checksum = this._crc.compute(array);
outData[i++] = checksum & 0xff;
outData[i] = checksum >> 8;
}
return this.hidDevice.write(outData);
},
async startSampling() {
const {idVendor, idProduct} = this.device.deviceDescriptor;
this.hidDevice = new HID.HID(idVendor, idProduct);
this.isAtorch = ["ACD15P", "C13P"].some(str => this.deviceName.includes(str));
this.hidDevice.on('data', data => {
// Only care about samples.
if (data[0] != 0xAA || data[1] != (this.isAtorch ? 0x40 : 0x25)) {
DEBUG_log("got unrecognized data", data.toString('hex'));
// The KingMeter periodically sends the aa 40 62 05 0b prefix followed
// by 25 nul bytes, 31 bytes of unknown data and 3 more nul bytes.
return;
}
if (DEBUG) {
let sample;
if (this.isAtorch) {
// First 8 bytes seem constant: aa 40 62 05 0b 00 eb 01
// Example of the following data:
// c6 a6 4f 00 35 0f 01 00 b6 87 05 00 8f 23 00 00 39 16 00 00 cb 2d 0d 00 52 75 32 00 90 d9 b7 01
// voltage current power d+ d- cc1 cc2 temp
// e89f87020000900a0000fc714e00010000002a0100000100
// Unknown data at bytes 40-64, seems constant or almost constant.
sample = {
v: data.readInt32LE(8) / 1e6,
i: data.readInt32LE(12) / 1e6,
p: data.readInt32LE(16) / 1e6,
"d+": data.readInt32LE(20) / 1e6,
"d-": data.readInt32LE(24) / 1e6,
"cc1": data.readInt32LE(28) / 1e6,
"cc2": data.readInt32LE(32) / 1e6,
temp: data.readInt32LE(36) / 1e6,
unknown: data.slice(40),
};
} else {
// All data packets seem to start with the same 6 bytes: aa 25 62 05 0b 01
// Example of the following data:
// cd 27 4f 00 1d 21 03 00 0e 7a 0f 00 16 00 00 00 cc 00 00 00 82 27 4f 00 b7 4b 03 00 07 01 01
// voltage1 current1 power d+ d- voltage2 current2 temp
//
// The voltage1/current1 and voltage2/current2 values don't match.
// There might be an input and output measurement.
// The power value matches neither v1 * c1 nor v2 * c2, but it is close.
// The meaning of the value in byte 37 is unknown. It seems to be sometimes
// 1, 2 or 4. Could be the charging protocol.
// Bytes 38-63 are always 0.
// Byte 64 contains another unknown value. It doesn't change enough
// between samples to feel like a checksum. It is 0x20 most of the time,
// but sometimes has other values (0, 1, 8, 0x21, 0x40, 0x61).
sample = {
v1: data.readInt32LE(6) / 1e6,
v2: data.readInt32LE(26) / 1e6,
i1: data.readInt32LE(10) / 1e6,
i2: data.readInt32LE(30) / 1e6,
p: data.readInt32LE(14) / 1e6,
"d+": data.readInt32LE(18) / 1e3,
"d-": data.readInt32LE(22) / 1e3,
temp: data.readInt16LE(34) / 10,
unknown1: data[36],
unknown2: data[63],
};
}
console.log(sample);
}
addSample(this,
roundToNanoSecondPrecision(performance.now() - startPerformanceNow),
data.readInt32LE(this.isAtorch ? 16 : 14) / 1e6);
});
this.hidDevice.on('error', err => {
console.log("hid device error:", err);
clearInterval(this.timerId);
});
if (!this.isAtorch) {
await this.sendCommand(this.CMD_1000SPS);
}
await this.sendCommand(this.CMD_GET_SAMPLES);
LogSampling();
this.timerId = setInterval(async () => {
if (gClosing) {
return;
}
try {
await this.sendCommand(this.CMD_GET_SAMPLES);
} catch(e) {
console.log("error sending command:", e);
}
}, this.isAtorch ? 1000
: 200); // The timer can be longer for slower sampling rates.
},
stopSampling() {
clearInterval(this.timerId);
this.hidDevice = null;
}
};
function PowerZBlueDevice(device) {
// expected product id: 0x2
}
PowerZBlueDevice.prototype = {
_crc: CRC.default("CRC16_MODBUS"),
checksum(data) { return this._crc.compute(data.slice(0,62)); },
async startSampling() {
const {idVendor, idProduct} = this.device.deviceDescriptor;
this.hidDevice = new HID.HID(idVendor, idProduct);
this.hidDevice.on('data', data => {
if (this.checksum(data) != data.readUInt16LE(62)) {
console.log(data.toString("hex"),
"Invalid CRC:", data.readUInt16LE(62), "computed:",
this.checksum(data));
return;
}
if (DEBUG) {
// First 3 bytes seem to always be [0, 3, 0x3b].
// Then there are 7 32bit BigEndian floats.
const sample = {
v: data.readFloatBE(3),
i: data.readFloatBE(7),
p: data.readFloatBE(11),
"d+": data.readFloatBE(15),
"d-": data.readFloatBE(19),
temp1: data.readFloatBE(23),
temp2: data.readFloatBE(27), // temp2 seems to == temp1
// The rest is unknown. Example of the unknown data:
// 01 00 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 01 00 00 00 00 00 00 28 08
// The first unknown byte is sometimes 0, sometimes 1.
// The other bytes don't change from one sample to another.
unknown: data.slice(31, 62),
};
console.log(sample);
}
addSample(this,
roundToNanoSecondPrecision(performance.now() - startPerformanceNow),
data.readFloatBE(11));
});
this.hidDevice.on('error', err => {
console.log("hid device error:", err);
});
LogSampling();
},
stopSampling() {
this.hidDevice = null;
}
};
function findVoltageOffset(data) {
var candidates = [
[offset => data.readInt32LE(offset) / 1e6, "Int32LE / 1e6"],
[offset => data.readInt32LE(offset) / 1e5, "Int32LE / 1e5"],
[offset => data.readInt32LE(offset) / 1e4, "Int32LE / 1e4"],
[offset => data.readInt32BE(offset) / 1e6, "Int32BE / 1e6"],
[offset => data.readInt32BE(offset) / 1e5, "Int32BE / 1e5"],
[offset => data.readInt32BE(offset) / 1e4, "Int32BE / 1e4"],
[offset => data.readFloatLE(offset), "FloatLE"],
[offset => data.readFloatBE(offset), "FloatBE"],
];
for (let offset = 0; offset < data.length - 4; ++offset) {
for (let [fun, desc] of candidates) {
let val = fun(offset);
if (val > 5 && val < 5.5) {
console.log("Offset:", offset, val, desc);
}
}
}
}
function WitrnDevice(device) {
}
WitrnDevice.prototype = {
async startSampling() {
const {idVendor, idProduct} = this.device.deviceDescriptor;
this.hidDevice = new HID.HID(idVendor, idProduct);
let initialTimeStamp = 0, initialPerformanceNow = 0;
let timestampWrapArounds = 0;
let lastTime_s = 0;
this.hidDevice.on('data', data => {
if (data[0] != 0xff || data[1] != 0x55) {
// All the data packets seem to start with 0xff 0x55.
DEBUG_log("ignoring unexpected packet", data[0], data[1]);
return;
}
const sum = array => array.reduce((acc, val) => acc + val);
const payloadChecksum = sum(data.slice(8, 62)) % 256;
const packetChecksum = (sum(data.slice(0, 8)) + payloadChecksum) % 256;
if (data[data.length - 2] != payloadChecksum ||
data[data.length - 1] != packetChecksum) {
console.log(data.toString("hex"),
"Invalid checksums:", [data[data.length - 2], data[data.length - 1]],
"computed:", [payloadChecksum, packetChecksum]);
return;
}
const v = data.readFloatLE(46);
const i = data.readFloatLE(50);
const time_s = data[2]; // time in seconds, on a single byte (ie. % 256)
const time_ms = data[3]; // time in ms, on a single byte (ie. % 256)
// data[4]: time_ms / 30
// data[5]: time_ms / 100
const time_ms_mod100 = data[6]; // time in ms, % 100
// data[7]: time_ms / 80
if (time_s < 10 && lastTime_s > 250) {
++timestampWrapArounds;
}
const timestamp =
(time_s + timestampWrapArounds * 256) * 1000 +
[0, 1, 2, 3].map(i => (256 * i + time_ms) % 1000).find(ms => ms % 100 == time_ms_mod100);
lastTime_s = time_s;
if (DEBUG) {
const sample = {
timestamp,
v,
i,
"d-": data.readFloatLE(34),
"d+": data.readFloatLE(30),
time: data.readUInt32LE(26), // time in seconds since the meter started.
rectime: data.readUInt32LE(22), // time in seconds displayed as 'rec' on the device
wh: data.readFloatLE(18),
ah: data.readFloatLE(14),
unknown1: data.slice(10, 14), // Very stable, or even constant.
unknown2: data.readFloatLE(38), // Always 25
unknown3: data.readFloatLE(42), // -73.<something that changes all the time>
};
console.log(sample);
}
if (!initialTimeStamp) {
initialTimeStamp = timestamp;
initialPerformanceNow = performance.now() - startPerformanceNow;
}
const time =
initialPerformanceNow + Number(timestamp - initialTimeStamp);
addSample(this, roundToNanoSecondPrecision(time), v * i);
});
this.hidDevice.on('error', err => {
console.log("hid device error:", err);
});
LogSampling();
},
stopSampling() {
this.hidDevice = null;
}
};
function RuiDengDevice(device) {
}
RuiDengDevice.prototype = {
_crc: CRC.default("CRC16_MODBUS"),
checksum(data) { return this._crc.compute(data.slice(0,60)); },
_key: Buffer.from([
88, 33, -6, 86, 1, -78, -16, 38,
-121, -1, 18, 4, 98, 42, 79, -80,
-122, -12, 2, 96, -127, 111, -102, 11,
-89, -15, 6, 97, -102, -72, 114, -120
]),
requestSample() {
this._lastSampleRequestTime = Date.now();
this.serialDevice.write("getva");
},
ondata(data) {
let buf = this.decipher.update(data);
if (buf.length < 64) {
console.log("received data is too short", buf.length, buf);
return;
}
if (this.checksum(buf) != buf.readUInt32LE(60)) {
console.log(buf.toString("hex"),
"Invalid CRC:", buf.readUInt32LE(60), "computed:",
this.checksum(buf));
return;
}
let prefix = buf.slice(0, 4).toString('ascii');
if (prefix == 'pac1') {
let power = buf.readUInt32LE(56) * 1e-4;
addSample(this,
roundToNanoSecondPrecision(performance.now() - startPerformanceNow),
power);
if (DEBUG) {
let sample = {
productName: buf.slice(4, 8).toString('ascii'),
version: buf.slice(8, 12).toString('ascii'),
serialNumber: buf.readUInt32LE(12),
unknown: buf.slice(16, 44),
sessionCount: buf.readUInt32LE(44),
voltage: buf.readUInt32LE(48) * 1e-4,
current: buf.readUInt32LE(52) * 1e-5,
power
};
console.log(sample);
}
} else if (prefix == 'pac2') {
if (DEBUG) {
let sample = {
temperature: buf.readUInt32LE(28),
resistance: buf.readUInt32LE(4) * 1e-1,
'd+': buf.readUInt32LE(32) * 1e-2,
'd-': buf.readUInt32LE(36) * 1e-2,
};
if (buf.readUInt32LE(28) == 1) {
sample.temperature *= -1;
}
console.log(sample);
}
} else if (prefix == 'pac3') {
if (DEBUG) {
// Always 0, print debug message if a non-zero value is present.
let allZero = true;
for (let i = 4; i < 60; ++i) {
if (buf[i] != 0) {
console.log("pac3 was expected to be all zero, but found data:",
buf.slice(4, 60).toString('hex'));
break;
}
}
}
this.requestSample();
} else {
console.log("unknown data packet:", buf.toString('hex'));
}
},
async startSampling() {
let serialDevices = (await SerialPort.list()).filter(d => d.manufacturer == "RuiDeng");
if (!serialDevices.length) {
console.log("serial device not found");
return;
}
DEBUG_log("Found serial devices", serialDevices);
this.decipher = createDecipheriv("AES-256-ECB", this._key, null);
this.decipher.setAutoPadding(false);
this.serialDevice = new SerialPort({path: serialDevices[0].path, baudRate: 115200});
await new Promise((resolve, reject) => this.serialDevice.on("open", err => {
if (err) {
reject("error opening serial port:" + err);
} else {
resolve();
}
}));
this.serialDevice.flush(); // discard pending data
this.serialDevice.on("error", console.log);
this.serialDevice.on("data", data => { this.ondata(data); });
this.requestSample();
this.interval = setInterval(() => {
let lastSampleAge = Date.now() - this._lastSampleRequestTime;
if (lastSampleAge > 200) {
DEBUG_log("The last sample is too old (" + lastSampleAge +
"ms), restarting sampling.");
this.requestSample();
}
}, 100);
LogSampling();
},
stopSampling() {
clearInterval(this.interval);
this.decipher = null;
this.serialDevice.close();
this.serialDevice = null;
}
};
function YzxStudioDevice(device) {
}
YzxStudioDevice.prototype = {
ondata(data) {
if (data.length < 28) {
console.log("received data is too short", data.length, data);
return;
}
if (data[0] != 0xAB) {
console.log("incorrect first byte", data[0]);
return;
}
const sum = array => array.reduce((acc, val) => acc + val);
const checksum = sum(data.slice(0, 27)) % 256;
if (checksum != data[27]) {
console.log(data.toString("hex"),
"Invalid checksum:", data[27], "computed:", checksum);
return;
}
let v = data.readInt32LE(3) * 1e-4;
let i = data.readUInt32LE(7) * 1e-4;
if (DEBUG) {
let sample = {
v, i,
Ah: data.readUInt32LE(11) * 1e-4,
Wh: data.readUInt32LE(15) * 1e-4,
// The T_ms value is changing in 250ms increment, but it only moves when
// the power is > 0, making it unsuitable for our sample times.
T_ms: data.readUInt32LE(19) * 1e1,
"d+": data.readUInt16LE(23) * 1e-3,
"d-": data.readUInt16LE(25) * 1e-3,
};
console.log(sample, "power", data.readInt32LE(3) * 1e-4 * data.readUInt32LE(7) * 1e-4);
}
addSample(this, roundToNanoSecondPrecision(performance.now() - startPerformanceNow), v * i);
},
async startSampling() {
this.deviceName = "YZXStudio";
let serialDevices = (await SerialPort.list()).filter(d => d.vendorId == "1a86");
if (!serialDevices.length) {
console.log("serial device not found");
return;
}
DEBUG_log("Found serial devices", serialDevices);
this.serialDevice = new SerialPort({path: serialDevices[0].path, baudRate: 115200});
await new Promise((resolve, reject) => this.serialDevice.on("open", err => {
if (err) {
reject("error opening serial port:" + err);
} else {
resolve();
}
}));
this.serialDevice.flush(); // discard pending data
this.serialDevice.on("error", console.log);
this.serialDevice.on("data", data => { this.ondata(data); });
LogSampling();
},
stopSampling() {
this.serialDevice.close();
this.serialDevice = null;