9
9
/* global document */
10
10
11
11
const electron = require ( 'electron' ) ;
12
- const clipboard = electron . remote . clipboard ;
13
- const menu = electron . remote . Menu ;
14
- const dialog = electron . remote . dialog ;
12
+ const { clipboard, Menu, dialog, getCurrentWindow} = require ( '@electron/remote' ) ;
15
13
16
14
const strftime = require ( 'strftime' ) . utc ( ) ;
17
15
const audiomoth = require ( 'audiomoth-hid' ) ;
18
16
19
17
const versionChecker = require ( './versionChecker.js' ) ;
18
+ const nightMode = require ( './nightMode.js' ) ;
20
19
21
20
/* UI components */
22
21
23
- var applicationMenu = menu . getApplicationMenu ( ) ;
22
+ const applicationMenu = Menu . getApplicationMenu ( ) ;
24
23
25
- var timeDisplay = document . getElementById ( 'time-display' ) ;
26
- var idLabel = document . getElementById ( 'id-label' ) ;
27
- var idDisplay = document . getElementById ( 'id-display' ) ;
28
- var firmwareVersionLabel = document . getElementById ( 'firmware-version-label' ) ;
29
- var firmwareVersionDisplay = document . getElementById ( 'firmware-version-display' ) ;
30
- var firmwareDescriptionLabel = document . getElementById ( 'firmware-description-label' ) ;
31
- var firmwareDescriptionDisplay = document . getElementById ( 'firmware-description-display' ) ;
32
- var batteryLabel = document . getElementById ( 'battery-label' ) ;
33
- var batteryDisplay = document . getElementById ( 'battery-display' ) ;
24
+ const timeDisplay = document . getElementById ( 'time-display' ) ;
25
+ const idLabel = document . getElementById ( 'id-label' ) ;
26
+ const idDisplay = document . getElementById ( 'id-display' ) ;
27
+ const firmwareVersionLabel = document . getElementById ( 'firmware-version-label' ) ;
28
+ const firmwareVersionDisplay = document . getElementById ( 'firmware-version-display' ) ;
29
+ const firmwareDescriptionLabel = document . getElementById ( 'firmware-description-label' ) ;
30
+ const firmwareDescriptionDisplay = document . getElementById ( 'firmware-description-display' ) ;
31
+ const batteryLabel = document . getElementById ( 'battery-label' ) ;
32
+ const batteryDisplay = document . getElementById ( 'battery-display' ) ;
34
33
35
- var setTimeButton = document . getElementById ( 'set-time-button' ) ;
34
+ const setTimeButton = document . getElementById ( 'set-time-button' ) ;
36
35
37
- var communicating = false ;
36
+ const MILLISECONDS_IN_SECOND = 1000 ;
38
37
39
- var currentTime , deviceId , firmwareVersion , firmwareDescription ;
38
+ let communicating = false ;
39
+
40
+ let currentTime , deviceId , firmwareVersion , firmwareDescription ;
40
41
41
42
/* Time display functions */
42
43
@@ -80,7 +81,7 @@ function enableDisplayAndShowTime (date) {
80
81
81
82
}
82
83
83
- var strftimeUTC = strftime . timezone ( 0 ) ;
84
+ const strftimeUTC = strftime . timezone ( 0 ) ;
84
85
85
86
timeDisplay . textContent = strftimeUTC ( '%H:%M:%S %d/%m/%Y UTC' , date ) ;
86
87
@@ -150,6 +151,8 @@ function requestFirmwareDescription () {
150
151
151
152
audiomoth . getFirmwareDescription ( function ( err , description ) {
152
153
154
+ if ( communicating ) return ;
155
+
153
156
if ( err ) {
154
157
155
158
errorOccurred ( err ) ;
@@ -174,6 +177,8 @@ function requestFirmwareVersion () {
174
177
175
178
audiomoth . getFirmwareVersion ( function ( err , versionArr ) {
176
179
180
+ if ( communicating ) return ;
181
+
177
182
if ( err ) {
178
183
179
184
errorOccurred ( err ) ;
@@ -198,6 +203,8 @@ function requestBatteryState () {
198
203
199
204
audiomoth . getBatteryState ( function ( err , batteryState ) {
200
205
206
+ if ( communicating ) return ;
207
+
201
208
if ( err ) {
202
209
203
210
errorOccurred ( err ) ;
@@ -224,6 +231,8 @@ function requestID () {
224
231
225
232
audiomoth . getID ( function ( err , id ) {
226
233
234
+ if ( communicating ) return ;
235
+
227
236
if ( err ) {
228
237
229
238
errorOccurred ( err ) ;
@@ -246,14 +255,12 @@ function requestID () {
246
255
247
256
function requestTime ( ) {
248
257
249
- if ( communicating ) {
250
-
251
- return ;
252
-
253
- }
258
+ if ( communicating ) return ;
254
259
255
260
audiomoth . getTime ( function ( err , date ) {
256
261
262
+ if ( communicating ) return ;
263
+
257
264
if ( err ) {
258
265
259
266
errorOccurred ( err ) ;
@@ -270,10 +277,16 @@ function requestTime () {
270
277
271
278
}
272
279
273
- setTimeout ( requestTime , 300 ) ;
274
-
275
280
} ) ;
276
281
282
+ const milliseconds = Date . now ( ) % MILLISECONDS_IN_SECOND ;
283
+
284
+ let delay = MILLISECONDS_IN_SECOND / 2 - milliseconds ;
285
+
286
+ if ( delay < 0 ) delay += MILLISECONDS_IN_SECOND ;
287
+
288
+ setTimeout ( requestTime , delay ) ;
289
+
277
290
}
278
291
279
292
function setTime ( time ) {
@@ -315,13 +328,11 @@ electron.ipcRenderer.on('update-check', function () {
315
328
316
329
versionChecker . checkLatestRelease ( function ( response ) {
317
330
318
- var buttonIndex ;
319
-
320
331
if ( response . error ) {
321
332
322
333
console . error ( response . error ) ;
323
334
324
- dialog . showMessageBox ( electron . remote . getCurrentWindow ( ) , {
335
+ dialog . showMessageBox ( getCurrentWindow ( ) , {
325
336
type : 'error' ,
326
337
title : 'Failed to check for updates' ,
327
338
message : response . error
@@ -333,7 +344,7 @@ electron.ipcRenderer.on('update-check', function () {
333
344
334
345
if ( response . updateNeeded === false ) {
335
346
336
- dialog . showMessageBox ( electron . remote . getCurrentWindow ( ) , {
347
+ dialog . showMessageBox ( getCurrentWindow ( ) , {
337
348
type : 'info' ,
338
349
title : 'Update not needed' ,
339
350
message : 'Your app is on the latest version (' + response . latestVersion + ').'
@@ -343,7 +354,7 @@ electron.ipcRenderer.on('update-check', function () {
343
354
344
355
}
345
356
346
- buttonIndex = dialog . showMessageBoxSync ( {
357
+ const buttonIndex = dialog . showMessageBoxSync ( {
347
358
type : 'warning' ,
348
359
buttons : [ 'Yes' , 'No' ] ,
349
360
title : 'Are you sure?' ,
@@ -368,16 +379,15 @@ initialiseDisplay();
368
379
369
380
setTimeButton . addEventListener ( 'click' , function ( ) {
370
381
371
- var sendTime , now , delay , sendTimeDiff , USB_LAG , MINIMUM_DELAY , MILLISECONDS_IN_SECOND ;
372
-
373
382
communicating = true ;
383
+
374
384
timeDisplay . style . color = 'lightgrey' ;
375
385
376
- USB_LAG = 20 ;
386
+ const USB_LAG = 20 ;
377
387
378
- MINIMUM_DELAY = 100 ;
388
+ const MINIMUM_DELAY = 100 ;
379
389
380
- MILLISECONDS_IN_SECOND = 1000 ;
390
+ const MILLISECONDS_IN_SECOND = 1000 ;
381
391
382
392
/* Update button */
383
393
@@ -395,18 +405,18 @@ setTimeButton.addEventListener('click', function () {
395
405
396
406
/* Increment to next second transition */
397
407
398
- sendTime = new Date ( ) ;
408
+ const sendTime = new Date ( ) ;
399
409
400
- delay = MILLISECONDS_IN_SECOND - sendTime . getMilliseconds ( ) - USB_LAG ;
410
+ let delay = MILLISECONDS_IN_SECOND - sendTime . getMilliseconds ( ) - USB_LAG ;
401
411
402
412
if ( delay < MINIMUM_DELAY ) delay += MILLISECONDS_IN_SECOND ;
403
413
404
414
sendTime . setMilliseconds ( sendTime . getMilliseconds ( ) + delay ) ;
405
415
406
416
/* Calculate how long to wait until second transition */
407
417
408
- now = new Date ( ) ;
409
- sendTimeDiff = sendTime . getTime ( ) - now . getTime ( ) ;
418
+ const now = new Date ( ) ;
419
+ const sendTimeDiff = sendTime . getTime ( ) - now . getTime ( ) ;
410
420
411
421
/* Either send immediately or wait until the transition */
412
422
@@ -428,4 +438,18 @@ setTimeButton.addEventListener('click', function () {
428
438
429
439
} ) ;
430
440
441
+ electron . ipcRenderer . on ( 'night-mode' , ( e , nm ) => {
442
+
443
+ if ( nm !== undefined ) {
444
+
445
+ nightMode . setNightMode ( nm ) ;
446
+
447
+ } else {
448
+
449
+ nightMode . toggle ( ) ;
450
+
451
+ }
452
+
453
+ } ) ;
454
+
431
455
requestTime ( ) ;
0 commit comments