-
Notifications
You must be signed in to change notification settings - Fork 7
/
lifeDisplay.js
596 lines (326 loc) · 16.8 KB
/
lifeDisplay.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
/****************************************************************************
* lifeDisplay.js
* openacousticdevices.info
* July 2017
*****************************************************************************/
'use strict';
const constants = require('./constants.js');
/* global document */
const MAX_WAV_SIZE = 4294966806;
/* UI components */
const lifeDisplayPanel = document.getElementById('life-display-panel');
/* Whether or not the life display box should display the warning or original information */
let displaySizeWarning = true;
exports.getPanel = () => {
return lifeDisplayPanel;
};
/* Obtain number of recording periods in a day and the total extra time in the form of truncated recording periods */
function getDailyCounts (timePeriods, recSecs, sleepSecs) {
let totalPrepTime = 0;
const recordingTimes = [];
const periodRecordingTimes = [];
let totalSleepTime = 0;
let containsTruncatedRecording = false;
for (let i = 0; i < timePeriods.length; i += 1) {
const currentPeriodRecordingTimes = [];
const startMins = timePeriods[i].startMins;
const endMins = timePeriods[i].endMins;
let periodSecs = 0;
if (startMins < endMins) {
periodSecs = endMins - startMins;
} else if (startMins === endMins) {
periodSecs = constants.MINUTES_IN_DAY;
} else if (startMins > endMins) {
periodSecs = (constants.MINUTES_IN_DAY - startMins) + endMins;
}
periodSecs *= constants.SECONDS_IN_MINUTE;
/* First recording in a period has prep time scheduled for just before period */
periodSecs += 1;
while (periodSecs > 1) {
/* If there's enough time to open a new file and record */
if (periodSecs >= 1 + recSecs + sleepSecs) {
totalPrepTime += 1;
periodSecs -= 1;
if (sleepSecs === 0) {
/* Prep time cuts into recording time */
recordingTimes.push(recSecs - 1);
currentPeriodRecordingTimes.push(recSecs - 1);
periodSecs -= recSecs - 1;
} else {
recordingTimes.push(recSecs);
currentPeriodRecordingTimes.push(recSecs);
periodSecs -= recSecs;
/* Prep time cuts into sleep time */
totalSleepTime += sleepSecs - 1;
periodSecs -= sleepSecs - 1;
}
} else {
containsTruncatedRecording = true;
totalPrepTime += 1;
periodSecs -= 1;
const truncatedRecordingLength = Math.min(recSecs, periodSecs);
recordingTimes.push(truncatedRecordingLength);
currentPeriodRecordingTimes.push(truncatedRecordingLength);
periodSecs -= truncatedRecordingLength;
totalSleepTime += Math.min(periodSecs, sleepSecs);
periodSecs = 0;
}
}
/* If there's an extra second left (not enough time to open a file and record anything), then just sleep */
if (periodSecs > 0) {
totalSleepTime += periodSecs;
}
/* Record how many recordings occur and in which period */
periodRecordingTimes.push(currentPeriodRecordingTimes);
}
return {
prepTime: totalPrepTime,
recordingTimes,
periodRecordingTimes,
sleepTime: totalSleepTime,
containsTruncatedRecording
};
}
/* Add units to file size string */
function formatFileSize (fileSize) {
fileSize = Math.round(fileSize / 1000);
if (fileSize < 10000) {
return fileSize + ' kB';
}
fileSize = Math.round(fileSize / 1000);
if (fileSize < 10000) {
return fileSize + ' MB';
}
fileSize = Math.round(fileSize / 1000);
return fileSize + ' GB';
}
function getFileSize (sampleRate, sampleRateDivider, secs) {
return sampleRate / sampleRateDivider * 2 * secs;
}
function calculateLengthMins (startMins, endMins) {
let length;
if (startMins < endMins) {
length = endMins - startMins;
} else if (startMins === endMins) {
length = constants.MINUTES_IN_DAY;
} else if (startMins > endMins) {
length = (constants.MINUTES_IN_DAY - startMins) + endMins;
}
return length;
}
function calculateGpsEnergyUsage (schedule, acquireGpsFixBeforeAfter, waitMinsBeforeRecording, dutyEnabled, recSecs, sleepSecs) {
const waitSecsBeforeRecording = waitMinsBeforeRecording * constants.SECONDS_IN_MINUTE;
let fixCount = 0;
for (let i = 0; i < schedule.length; i++) {
/* Gap between current period and next one */
let gapLength = 0;
/* Calculate the gaps between recordings */
let timeToAddToNextGap = 0;
if (acquireGpsFixBeforeAfter === 'individual' && dutyEnabled) {
const periodStart = schedule[i].startMins;
let periodEnd = schedule[i].endMins;
periodEnd += periodStart > periodEnd ? constants.MINUTES_IN_DAY : 0;
const periodLengthSecs = (periodEnd - periodStart) * constants.SECONDS_IN_MINUTE;
const completeCycleCount = Math.floor(periodLengthSecs / (recSecs + sleepSecs));
const overflow = periodLengthSecs % (recSecs + sleepSecs);
let gapCount = 0;
if (overflow === 0) {
/* Record + sleep cycles fit into the period */
gapCount = completeCycleCount > 0 ? completeCycleCount - 1 : 0;
timeToAddToNextGap = sleepSecs;
} else if (overflow <= recSecs) {
/* The end of the period occurs on a record phase */
gapCount = completeCycleCount;
} else {
/* The end of the period occurs on a sleep phase */
gapCount = completeCycleCount;
timeToAddToNextGap = overflow - recSecs;
}
fixCount += sleepSecs > waitSecsBeforeRecording ? 2 * gapCount : sleepSecs > constants.MINIMUM_GPS_FIX_TIME ? gapCount : 0;
timeToAddToNextGap /= constants.SECONDS_IN_MINUTE;
}
// console.log('\tFixes after adding fixes inside period', fixCount);
/* Calculate gap between periods */
const gapStart = schedule[i].endMins;
/* The next period could be the current period if only 1 exists */
let gapEnd = schedule[(i + 1) % schedule.length].startMins;
gapEnd += gapStart > gapEnd ? 1440 : 0;
gapLength += gapEnd - gapStart;
gapLength += timeToAddToNextGap;
// console.log('Time added from inside period:', timeToAddToNextGap);
// console.log('Gap length:', gapLength);
if (gapLength > waitMinsBeforeRecording) {
/* If the gap between periods is longer than waitSecsBeforeRecording, then the period gets 2 GPS fixes */
fixCount += 2;
} else if (gapLength >= constants.MINIMUM_GPS_FIX_TIME / constants.SECONDS_IN_MINUTE) {
/* If the gap is more than MINIMUM_GPS_FIX_TIME and less than waitSecsBeforeRecording, the period gets 1 GPS fix */
fixCount += 1;
}
/* Otherwise, 0 GPS fixes */
// console.log('\tFixes after adding fixes between period and next one', fixCount);
// console.log('-');
}
// console.log('GPS fix count:', fixCount);
// console.log('-------');
return fixCount * constants.GPS_FIX_CONSUMPTION * constants.GPS_FIX_TIME;
}
/* Update storage and energy usage values in life display box */
exports.updateLifeDisplay = (schedule, configuration, recLength, sleepLength, amplitudeThresholdingEnabled, frequencyTriggerEnabled, dutyEnabled, energySaverChecked, gpsEnabled, acquireGpsFixBeforeAfter, timeBeforeRecording) => {
const thresholdingEnabled = amplitudeThresholdingEnabled || frequencyTriggerEnabled;
/* If no recording periods exist, do not perform energy calculations */
if (schedule.length === 0) {
lifeDisplayPanel.innerHTML = 'Each day this will produce 0 files, totalling 0 MB.<br/>Daily energy consumption will be approximately 0 mAh.';
return;
}
const energySaverEnabled = energySaverChecked && (configuration.trueSampleRate <= 48);
const fileOpenEnergy = energySaverEnabled ? 35.0 : 40.0;
const waitTime = 0.25;
const waitEnergy = energySaverEnabled ? 7.0 : 10.0;
const sleepCurrent = 0.1;
const recordingCurrent = energySaverEnabled ? configuration.energySaverRecordCurrent : configuration.recordCurrent;
const listenCurrent = energySaverEnabled ? configuration.energySaverListenCurrent : configuration.listenCurrent;
let preparationInstances = 0;
/* It's possible for file sizes to vary but overall storage consumption be known, so whether or not 'up to' is used in two locations in the information text varies */
let upToFile = thresholdingEnabled;
const upToTotal = thresholdingEnabled;
let totalSleepTime = 0;
let totalSize = 0;
let maxLength = 0;
let recordingTimes = [];
/* Calculate the amount of time spent recording each day */
if (dutyEnabled) {
const countResponse = getDailyCounts(schedule, recLength, sleepLength);
preparationInstances = countResponse.prepTime;
recordingTimes = countResponse.recordingTimes;
totalSleepTime = countResponse.sleepTime;
/* Calculate the size of a days worth of recordings */
for (let i = 0; i < recordingTimes.length; i++) {
totalSize += configuration.sampleRate / configuration.sampleRateDivider * 2 * recordingTimes[i];
}
/* If any files are truncated, file size can vary */
if (countResponse.containsTruncatedRecording) {
upToFile = true;
}
} else {
maxLength = 0;
for (let i = 0; i < schedule.length; i++) {
const startMins = schedule[i].startMins;
const endMins = schedule[i].endMins;
const lengthMins = calculateLengthMins(startMins, endMins);
preparationInstances += 1;
const recordingLength = (lengthMins * 60) - 1;
recordingTimes.push(recordingLength);
/* If the periods differ in size, include 'up to' when describing the file size. If amplitude thresholding is enabled, it already will include this */
if (i > 0 && !thresholdingEnabled) {
const prevPeriod = schedule[i - 1];
const prevLength = prevPeriod.endMins - prevPeriod.startMins;
if (lengthMins !== prevLength) {
upToFile = true;
}
}
totalSize += getFileSize(configuration.sampleRate, configuration.sampleRateDivider, recordingLength);
maxLength = (recordingLength > maxLength) ? recordingLength : maxLength;
}
}
const longestRecording = Math.max(...recordingTimes);
let upToSize = getFileSize(configuration.sampleRate, configuration.sampleRateDivider, longestRecording);
let recordingsWillBeSplit = false;
const maxWavLength = Math.floor(MAX_WAV_SIZE / (configuration.sampleRate / configuration.sampleRateDivider * 2));
for (let i = 0; i < recordingTimes.length; i++) {
const recLength = recordingTimes[i];
const recSize = getFileSize(configuration.sampleRate, configuration.sampleRateDivider, recLength);
if (recSize > MAX_WAV_SIZE) {
upToFile = true;
upToSize = MAX_WAV_SIZE;
recordingsWillBeSplit = true;
/* Cap recording at max length */
recordingTimes[i] = maxWavLength;
const timeRemaining = recLength - maxWavLength;
/* If there's not enough remaining time to open a file and also record, don't bother */
if (timeRemaining > 1) {
preparationInstances += 1;
/* Order doesn't matter for storage/energy consumption, so just append recording period to the end */
recordingTimes.push(timeRemaining - 1);
}
}
}
const totalRecCount = recordingTimes.length;
const totalRecLength = recordingTimes.reduce((a, b) => a + b, 0);
/* Generate life display message */
let text = '';
if (recordingsWillBeSplit && displaySizeWarning) {
text += '<b>Recordings will exceed the 4.3 GB max size of a WAV file so will be split.</b><br/>';
} else {
text += 'Each day this will produce ';
text += totalRecCount + ' file';
text += totalRecCount > 1 ? 's, ' : ' ';
if (totalRecCount > 1) {
text += 'each ';
text += upToFile ? 'up to ' : '';
text += formatFileSize(upToSize) + ', ';
}
text += 'totalling ';
text += upToTotal ? 'up to ' : '';
text += formatFileSize(totalSize) + '.<br/>';
}
/* Use the file count to work out the average file open time if daily folders are enabled */
const fileOpenTime = 0.5 + 3.5 * (totalRecCount / 10000) / 2;
/* Add all the time outside the schedule as sleep time */
totalSleepTime += Math.max(0, 86400 - preparationInstances - totalRecLength - totalSleepTime);
/* Preparation is split between opening the file and waiting. Each takes half a second */
const fileOpenEnergyUsage = preparationInstances * fileOpenTime * fileOpenEnergy / 3600;
const waitEnergyUsage = preparationInstances * waitTime * waitEnergy / 3600;
const recordingEnergyUsage = totalRecLength * recordingCurrent / 3600;
const listeningEnergyUsage = totalRecLength * listenCurrent / 3600;
const sleepEnergyUsage = totalSleepTime * sleepCurrent / 3600;
let gpsEnergyUsage = 0.0;
if (gpsEnabled) {
gpsEnergyUsage = calculateGpsEnergyUsage(schedule, acquireGpsFixBeforeAfter, timeBeforeRecording, dutyEnabled, recLength, sleepLength);
}
if (thresholdingEnabled) {
let minEnergyUsed = 0;
minEnergyUsed += fileOpenEnergyUsage;
minEnergyUsed += waitEnergyUsage;
minEnergyUsed += listeningEnergyUsage;
minEnergyUsed += sleepEnergyUsage;
const minEnergyPrecision = minEnergyUsed > 100 ? 10 : minEnergyUsed > 50 ? 5 : minEnergyUsed > 20 ? 2 : 1;
minEnergyUsed = Math.round(minEnergyUsed / minEnergyPrecision) * minEnergyPrecision;
// Add GPS energy after rounding so it's clear what the effect of the GPS on energy consumption is
minEnergyUsed += gpsEnergyUsage;
minEnergyUsed = Math.round(minEnergyUsed);
let maxEnergyUsed = 0;
maxEnergyUsed += fileOpenEnergyUsage;
maxEnergyUsed += waitEnergyUsage;
maxEnergyUsed += recordingEnergyUsage;
maxEnergyUsed += sleepEnergyUsage;
const maxEnergyPrecision = maxEnergyUsed > 100 ? 10 : maxEnergyUsed > 50 ? 5 : maxEnergyUsed > 20 ? 2 : 1;
maxEnergyUsed = Math.round(maxEnergyUsed / maxEnergyPrecision) * maxEnergyPrecision;
// Add GPS energy after rounding so it's clear what the effect of the GPS on energy consumption is
maxEnergyUsed += gpsEnergyUsage;
maxEnergyUsed = Math.round(maxEnergyUsed);
text += 'Daily energy consumption will be between ' + minEnergyUsed + ' and ' + maxEnergyUsed + ' mAh.';
} else {
let energyUsed = 0;
energyUsed += fileOpenEnergyUsage;
energyUsed += waitEnergyUsage;
energyUsed += recordingEnergyUsage;
energyUsed += sleepEnergyUsage;
const energyPrecision = energyUsed > 100 ? 10 : energyUsed > 50 ? 5 : energyUsed > 20 ? 2 : 1;
energyUsed = Math.round(energyUsed / energyPrecision) * energyPrecision;
// Add GPS energy after rounding so it's clear what the effect of the GPS on energy consumption is
energyUsed += gpsEnergyUsage;
energyUsed = Math.round(energyUsed);
text += 'Daily energy consumption will be approximately ' + energyUsed + ' mAh.';
}
lifeDisplayPanel.innerHTML = text;
};
exports.toggleSizeWarning = (updateFunction) => {
displaySizeWarning = !displaySizeWarning;
updateFunction();
if (!displaySizeWarning) {
setTimeout(() => {
displaySizeWarning = true;
updateFunction();
}, 5000);
}
};