8
8
9
9
/* global document */
10
10
11
- var electron = require ( 'electron' ) ;
12
- var clipboard = electron . remote . clipboard ;
13
- var menu = electron . remote . Menu ;
11
+ const electron = require ( 'electron' ) ;
12
+ const clipboard = electron . remote . clipboard ;
13
+ const menu = electron . remote . Menu ;
14
+ const dialog = electron . remote . dialog ;
14
15
15
- var strftime = require ( 'strftime' ) . utc ( ) ;
16
- var audiomoth = require ( 'audiomoth-hid' ) ;
16
+ const strftime = require ( 'strftime' ) . utc ( ) ;
17
+ const audiomoth = require ( 'audiomoth-hid' ) ;
18
+
19
+ const versionChecker = require ( './versionChecker.js' ) ;
17
20
18
21
/* UI components */
19
22
@@ -31,6 +34,10 @@ var batteryDisplay = document.getElementById('battery-display');
31
34
32
35
var setTimeButton = document . getElementById ( 'set-time-button' ) ;
33
36
37
+ var communicating = false ;
38
+
39
+ var currentTime , deviceId , firmwareVersion , firmwareDescription ;
40
+
34
41
/* Time display functions */
35
42
36
43
function initialiseDisplay ( ) {
@@ -67,11 +74,17 @@ function disableDisplay () {
67
74
68
75
function enableDisplayAndShowTime ( date ) {
69
76
77
+ if ( communicating ) {
78
+
79
+ return ;
80
+
81
+ }
82
+
70
83
var strftimeUTC = strftime . timezone ( 0 ) ;
71
84
72
85
timeDisplay . textContent = strftimeUTC ( '%H:%M:%S %d/%m/%Y UTC' , date ) ;
73
86
74
- timeDisplay . style . color = 'black ' ;
87
+ timeDisplay . style . color = '' ;
75
88
76
89
setTimeButton . disabled = false ;
77
90
@@ -85,39 +98,39 @@ function enableDisplayAndShowBatteryState (batteryState) {
85
98
86
99
batteryDisplay . textContent = batteryState ;
87
100
88
- batteryDisplay . style . color = 'black ' ;
101
+ batteryDisplay . style . color = '' ;
89
102
90
- batteryLabel . style . color = 'black ' ;
103
+ batteryLabel . style . color = '' ;
91
104
92
105
}
93
106
94
107
function enableDisplayAndShowID ( id ) {
95
108
96
109
idDisplay . textContent = id ;
97
110
98
- idDisplay . style . color = 'black ' ;
111
+ idDisplay . style . color = '' ;
99
112
100
- idLabel . style . color = 'black ' ;
113
+ idLabel . style . color = '' ;
101
114
102
115
}
103
116
104
117
function enableDisplayAndShowVersionNumber ( version ) {
105
118
106
119
firmwareVersionDisplay . textContent = version ;
107
120
108
- firmwareVersionDisplay . style . color = 'black ' ;
121
+ firmwareVersionDisplay . style . color = '' ;
109
122
110
- firmwareVersionLabel . style . color = 'black ' ;
123
+ firmwareVersionLabel . style . color = '' ;
111
124
112
125
}
113
126
114
127
function enableDisplayAndShowVersionDescription ( description ) {
115
128
116
129
firmwareDescriptionDisplay . textContent = description ;
117
130
118
- firmwareDescriptionDisplay . style . color = 'black ' ;
131
+ firmwareDescriptionDisplay . style . color = '' ;
119
132
120
- firmwareDescriptionLabel . style . color = 'black ' ;
133
+ firmwareDescriptionLabel . style . color = '' ;
121
134
122
135
}
123
136
@@ -147,7 +160,9 @@ function requestFirmwareDescription () {
147
160
148
161
} else {
149
162
150
- enableDisplayAndShowVersionDescription ( description ) ;
163
+ firmwareDescription = description ;
164
+
165
+ requestFirmwareVersion ( ) ;
151
166
152
167
}
153
168
@@ -169,9 +184,9 @@ function requestFirmwareVersion () {
169
184
170
185
} else {
171
186
172
- enableDisplayAndShowVersionNumber ( versionArr [ 0 ] + '.' + versionArr [ 1 ] + '.' + versionArr [ 2 ] ) ;
187
+ firmwareVersion = versionArr [ 0 ] + '.' + versionArr [ 1 ] + '.' + versionArr [ 2 ] ;
173
188
174
- requestFirmwareDescription ( ) ;
189
+ requestBatteryState ( ) ;
175
190
176
191
}
177
192
@@ -193,10 +208,12 @@ function requestBatteryState () {
193
208
194
209
} else {
195
210
211
+ enableDisplayAndShowTime ( currentTime ) ;
212
+ enableDisplayAndShowID ( deviceId ) ;
213
+ enableDisplayAndShowVersionDescription ( firmwareDescription ) ;
214
+ enableDisplayAndShowVersionNumber ( firmwareVersion ) ;
196
215
enableDisplayAndShowBatteryState ( batteryState ) ;
197
216
198
- requestFirmwareVersion ( ) ;
199
-
200
217
}
201
218
202
219
} ) ;
@@ -217,9 +234,9 @@ function requestID () {
217
234
218
235
} else {
219
236
220
- enableDisplayAndShowID ( id ) ;
237
+ deviceId = id ;
221
238
222
- requestBatteryState ( ) ;
239
+ requestFirmwareDescription ( ) ;
223
240
224
241
}
225
242
@@ -229,6 +246,12 @@ function requestID () {
229
246
230
247
function requestTime ( ) {
231
248
249
+ if ( communicating ) {
250
+
251
+ return ;
252
+
253
+ }
254
+
232
255
audiomoth . getTime ( function ( err , date ) {
233
256
234
257
if ( err ) {
@@ -241,21 +264,21 @@ function requestTime () {
241
264
242
265
} else {
243
266
244
- enableDisplayAndShowTime ( date ) ;
267
+ currentTime = date ;
245
268
246
269
requestID ( ) ;
247
270
248
271
}
249
272
250
- setTimeout ( requestTime , 1000 ) ;
273
+ setTimeout ( requestTime , 300 ) ;
251
274
252
275
} ) ;
253
276
254
277
}
255
278
256
- function setTime ( ) {
279
+ function setTime ( time ) {
257
280
258
- audiomoth . setTime ( new Date ( ) , function ( err , date ) {
281
+ audiomoth . setTime ( time , function ( err , date ) {
259
282
260
283
if ( err ) {
261
284
@@ -269,13 +292,6 @@ function setTime () {
269
292
270
293
enableDisplayAndShowTime ( date ) ;
271
294
272
- setTimeButton . style . color = 'green' ;
273
- setTimeout ( function ( ) {
274
-
275
- setTimeButton . style . color = '' ;
276
-
277
- } , 1000 ) ;
278
-
279
295
}
280
296
281
297
} ) ;
@@ -295,12 +311,121 @@ electron.ipcRenderer.on('copyID', function () {
295
311
296
312
} ) ;
297
313
314
+ electron . ipcRenderer . on ( 'update-check' , function ( ) {
315
+
316
+ versionChecker . checkLatestRelease ( function ( response ) {
317
+
318
+ var buttonIndex ;
319
+
320
+ if ( response . error ) {
321
+
322
+ console . error ( response . error ) ;
323
+
324
+ dialog . showMessageBox ( electron . remote . getCurrentWindow ( ) , {
325
+ type : 'error' ,
326
+ title : 'Failed to check for updates' ,
327
+ message : response . error
328
+ } ) ;
329
+
330
+ return ;
331
+
332
+ }
333
+
334
+ if ( response . updateNeeded === false ) {
335
+
336
+ dialog . showMessageBox ( electron . remote . getCurrentWindow ( ) , {
337
+ type : 'info' ,
338
+ title : 'Update not needed' ,
339
+ message : 'Your app is on the latest version (' + response . latestVersion + ').'
340
+ } ) ;
341
+
342
+ return ;
343
+
344
+ }
345
+
346
+ buttonIndex = dialog . showMessageBoxSync ( {
347
+ type : 'warning' ,
348
+ buttons : [ 'Yes' , 'No' ] ,
349
+ title : 'Are you sure?' ,
350
+ message : 'A newer version of this app is available (' + response . latestVersion + '), would you like to download it?'
351
+ } ) ;
352
+
353
+ if ( buttonIndex === 0 ) {
354
+
355
+ electron . shell . openExternal ( 'https://www.openacousticdevices.info/applications' ) ;
356
+
357
+ }
358
+
359
+ } ) ;
360
+
361
+ } ) ;
362
+
298
363
/* Main code entry point */
299
364
300
365
disableDisplay ( ) ;
301
366
302
367
initialiseDisplay ( ) ;
303
368
304
- setTimeButton . addEventListener ( 'click' , setTime ) ;
369
+ setTimeButton . addEventListener ( 'click' , function ( ) {
370
+
371
+ var sendTime , now , delay , sendTimeDiff , USB_LAG , MINIMUM_DELAY , MILLISECONDS_IN_SECOND ;
372
+
373
+ communicating = true ;
374
+ timeDisplay . style . color = 'lightgrey' ;
375
+
376
+ USB_LAG = 20 ;
377
+
378
+ MINIMUM_DELAY = 100 ;
379
+
380
+ MILLISECONDS_IN_SECOND = 1000 ;
381
+
382
+ /* Update button */
383
+
384
+ setTimeButton . disabled = true ;
385
+
386
+ setTimeout ( function ( ) {
387
+
388
+ communicating = false ;
389
+
390
+ requestTime ( ) ;
391
+
392
+ setTimeButton . disabled = false ;
393
+
394
+ } , 1500 ) ;
395
+
396
+ /* Increment to next second transition */
397
+
398
+ sendTime = new Date ( ) ;
399
+
400
+ delay = MILLISECONDS_IN_SECOND - sendTime . getMilliseconds ( ) - USB_LAG ;
401
+
402
+ if ( delay < MINIMUM_DELAY ) delay += MILLISECONDS_IN_SECOND ;
403
+
404
+ sendTime . setMilliseconds ( sendTime . getMilliseconds ( ) + delay ) ;
405
+
406
+ /* Calculate how long to wait until second transition */
407
+
408
+ now = new Date ( ) ;
409
+ sendTimeDiff = sendTime . getTime ( ) - now . getTime ( ) ;
410
+
411
+ /* Either send immediately or wait until the transition */
412
+
413
+ if ( sendTimeDiff <= 0 ) {
414
+
415
+ setTime ( sendTime ) ;
416
+
417
+ } else {
418
+
419
+ console . log ( 'Sending in' , sendTimeDiff ) ;
420
+
421
+ setTimeout ( function ( ) {
422
+
423
+ setTime ( sendTime ) ;
424
+
425
+ } , sendTimeDiff ) ;
426
+
427
+ }
428
+
429
+ } ) ;
305
430
306
431
requestTime ( ) ;
0 commit comments