Skip to content

Commit 59ae252

Browse files
committed
Add 1.1.3 changes
1 parent f6858ed commit 59ae252

File tree

5 files changed

+104
-149
lines changed

5 files changed

+104
-149
lines changed

.eslintrc.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
module.exports = {
2-
"extends": "standard",
3-
"rules": {
4-
"semi": [2, "always"],
5-
"indent": ["error", 4],
6-
"padded-blocks": ["error", "always"],
7-
"no-useless-escape": 0,
8-
"object-curly-spacing": ["error", "never"]
2+
extends: 'standard',
3+
rules: {
4+
semi: [2, 'always'],
5+
indent: ['error', 4],
6+
'padded-blocks': ['error', 'always'],
7+
'no-useless-escape': 0,
8+
'object-curly-spacing': ['error', 'never'],
9+
'no-extend-native': ['error', {exceptions: ['Array']}],
10+
'standard/no-callback-literal': 0,
11+
'no-throw-literal': 0
912
}
10-
};
13+
};

app.js

Lines changed: 87 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
const electron = require('electron');
1212
const {clipboard, Menu, dialog, getCurrentWindow} = require('@electron/remote');
1313

14+
const util = require('util');
1415
const strftime = require('strftime').utc();
1516
const audiomoth = require('audiomoth-hid');
1617

1718
const versionChecker = require('./versionChecker.js');
1819
const nightMode = require('./nightMode.js');
20+
require('worker_threads');
1921

2022
/* UI components */
2123

@@ -35,9 +37,14 @@ const setTimeButton = document.getElementById('set-time-button');
3537

3638
const MILLISECONDS_IN_SECOND = 1000;
3739

40+
/* Whether or not communication with device is currently happening */
41+
3842
let communicating = false;
3943

40-
let currentTime, deviceId, firmwareVersion, firmwareDescription;
44+
/* Communication constants */
45+
46+
const MAXIMUM_RETRIES = 10;
47+
const DEFAULT_RETRY_INTERVAL = 100;
4148

4249
/* Time display functions */
4350

@@ -73,11 +80,7 @@ function disableDisplay () {
7380

7481
function enableDisplayAndShowTime (date) {
7582

76-
if (communicating) {
77-
78-
return;
79-
80-
}
83+
if (communicating) return;
8184

8285
const strftimeUTC = strftime.timezone(0);
8386

@@ -87,8 +90,6 @@ function enableDisplayAndShowTime (date) {
8790

8891
setTimeButton.disabled = false;
8992

90-
applicationMenu.getMenuItemById('copyid').enabled = true;
91-
9293
}
9394

9495
/* Device information display functions */
@@ -111,6 +112,8 @@ function enableDisplayAndShowID (id) {
111112

112113
idLabel.classList.remove('grey');
113114

115+
applicationMenu.getMenuItemById('copyid').enabled = true;
116+
114117
}
115118

116119
function enableDisplayAndShowVersionNumber (version) {
@@ -133,179 +136,127 @@ function enableDisplayAndShowVersionDescription (description) {
133136

134137
}
135138

136-
/* Error response */
139+
/* Utility functions */
137140

138-
function errorOccurred (err) {
141+
async function callWithRetry (funcSync, argument, milliseconds, repeats) {
139142

140-
console.error(err);
143+
let result;
141144

142-
disableDisplay();
145+
let attempt = 0;
143146

144-
}
147+
while (attempt < repeats) {
145148

146-
/* Device interaction functions */
149+
try {
147150

148-
function requestFirmwareDescription () {
151+
if (argument) {
149152

150-
audiomoth.getFirmwareDescription(function (err, description) {
153+
result = await funcSync(argument);
151154

152-
if (communicating) return;
155+
} else {
153156

154-
if (err) {
157+
result = await funcSync();
155158

156-
errorOccurred(err);
159+
}
157160

158-
} else if (description === null) {
161+
break;
159162

160-
disableDisplay();
163+
} catch (e) {
161164

162-
} else {
165+
const interval = milliseconds / 2 + milliseconds / 2 * Math.random();
163166

164-
firmwareDescription = description;
167+
await delay(interval);
165168

166-
requestFirmwareVersion();
169+
attempt += 1;
167170

168171
}
169172

170-
});
171-
172-
}
173-
174-
function requestFirmwareVersion () {
175-
176-
audiomoth.getFirmwareVersion(function (err, versionArr) {
177-
178-
if (communicating) return;
179-
180-
if (err) {
181-
182-
errorOccurred(err);
183-
184-
} else if (versionArr === null) {
185-
186-
disableDisplay();
187-
188-
} else {
189-
190-
firmwareVersion = versionArr[0] + '.' + versionArr[1] + '.' + versionArr[2];
173+
}
191174

192-
requestBatteryState();
175+
if (result === undefined) throw ('Error: Repeated attempts to access the device failed.');
193176

194-
}
177+
if (result === null) throw ('No device detected');
195178

196-
});
179+
return result;
197180

198181
}
199182

200-
function requestBatteryState () {
201-
202-
audiomoth.getBatteryState(function (err, batteryState) {
203-
204-
if (communicating) return;
205-
206-
if (err) {
207-
208-
errorOccurred(err);
183+
async function delay (milliseconds) {
209184

210-
} else if (batteryState === null) {
211-
212-
disableDisplay();
213-
214-
} else {
215-
216-
enableDisplayAndShowTime(currentTime);
217-
enableDisplayAndShowID(deviceId);
218-
enableDisplayAndShowVersionDescription(firmwareDescription);
219-
enableDisplayAndShowVersionNumber(firmwareVersion);
220-
enableDisplayAndShowBatteryState(batteryState);
221-
222-
}
223-
224-
});
185+
return new Promise(resolve => setTimeout(resolve, milliseconds));
225186

226187
}
227188

228-
function requestID () {
229-
230-
audiomoth.getID(function (err, id) {
231-
232-
if (communicating) return;
233-
234-
if (err) {
189+
/* Promisified versions of AudioMoth-HID calls */
235190

236-
errorOccurred(err);
191+
const getFirmwareDescription = util.promisify(audiomoth.getFirmwareDescription);
237192

238-
} else if (id === null) {
193+
const getFirmwareVersion = util.promisify(audiomoth.getFirmwareVersion);
239194

240-
disableDisplay();
195+
const getBatteryState = util.promisify(audiomoth.getBatteryState);
241196

242-
} else {
197+
const getID = util.promisify(audiomoth.getID);
243198

244-
deviceId = id;
199+
const getTime = util.promisify(audiomoth.getTime);
245200

246-
requestFirmwareDescription();
201+
const setTime = util.promisify(audiomoth.setTime);
247202

248-
}
203+
/* Device interaction functions */
249204

250-
});
205+
async function requestAudioMothTime () {
251206

252-
}
207+
try {
253208

254-
function requestTime () {
209+
/* Read from AudioMoth */
255210

256-
if (communicating) return;
211+
const date = await callWithRetry(getTime, null, DEFAULT_RETRY_INTERVAL, MAXIMUM_RETRIES);
257212

258-
audiomoth.getTime(function (err, date) {
213+
const id = await callWithRetry(getID, null, DEFAULT_RETRY_INTERVAL, MAXIMUM_RETRIES);
259214

260-
if (communicating) return;
215+
const description = await callWithRetry(getFirmwareDescription, null, DEFAULT_RETRY_INTERVAL, MAXIMUM_RETRIES);
261216

262-
if (err) {
217+
const versionArr = await callWithRetry(getFirmwareVersion, null, DEFAULT_RETRY_INTERVAL, MAXIMUM_RETRIES);
263218

264-
errorOccurred(err);
219+
const batteryState = await callWithRetry(getBatteryState, null, DEFAULT_RETRY_INTERVAL, MAXIMUM_RETRIES);
265220

266-
} else if (date === null) {
221+
/* No exceptions have occurred so update display */
267222

268-
disableDisplay();
223+
const firmwareVersion = versionArr[0] + '.' + versionArr[1] + '.' + versionArr[2];
269224

270-
} else {
225+
enableDisplayAndShowTime(date);
226+
enableDisplayAndShowID(id);
227+
enableDisplayAndShowVersionDescription(description);
228+
enableDisplayAndShowVersionNumber(firmwareVersion);
229+
enableDisplayAndShowBatteryState(batteryState);
271230

272-
currentTime = date;
231+
} catch (e) {
273232

274-
requestID();
233+
/* Problem reading from AudioMoth or no AudioMoth */
275234

276-
}
235+
disableDisplay();
277236

278-
});
237+
}
279238

280239
const milliseconds = Date.now() % MILLISECONDS_IN_SECOND;
281240

282241
let delay = MILLISECONDS_IN_SECOND / 2 - milliseconds;
283242

284243
if (delay < 0) delay += MILLISECONDS_IN_SECOND;
285244

286-
setTimeout(requestTime, delay);
245+
setTimeout(requestAudioMothTime, delay);
287246

288247
}
289248

290-
function setTime (time) {
291-
292-
audiomoth.setTime(time, function (err, date) {
293-
294-
if (err) {
249+
async function setAudioMothTime (time) {
295250

296-
errorOccurred(err);
251+
try {
297252

298-
} else if (date === null) {
253+
await callWithRetry(setTime, time, DEFAULT_RETRY_INTERVAL, MAXIMUM_RETRIES);
299254

300-
disableDisplay();
255+
} catch (e) {
301256

302-
} else {
257+
disableDisplay();
303258

304-
enableDisplayAndShowTime(date);
305-
306-
}
307-
308-
});
259+
}
309260

310261
}
311262

@@ -377,8 +328,6 @@ initialiseDisplay();
377328

378329
setTimeButton.addEventListener('click', function () {
379330

380-
communicating = true;
381-
382331
timeDisplay.classList.add('grey');
383332

384333
const USB_LAG = 20;
@@ -387,20 +336,6 @@ setTimeButton.addEventListener('click', function () {
387336

388337
const MILLISECONDS_IN_SECOND = 1000;
389338

390-
/* Update button */
391-
392-
setTimeButton.disabled = true;
393-
394-
setTimeout(function () {
395-
396-
communicating = false;
397-
398-
requestTime();
399-
400-
setTimeButton.disabled = false;
401-
402-
}, 1500);
403-
404339
/* Increment to next second transition */
405340

406341
const sendTime = new Date();
@@ -414,21 +349,38 @@ setTimeButton.addEventListener('click', function () {
414349
/* Calculate how long to wait until second transition */
415350

416351
const now = new Date();
352+
417353
const sendTimeDiff = sendTime.getTime() - now.getTime();
418354

355+
/* Calculate when to re-enable time display */
356+
357+
communicating = true;
358+
359+
setTimeButton.disabled = true;
360+
361+
const updateDelay = sendTimeDiff <= 0 ? MILLISECONDS_IN_SECOND : sendTimeDiff;
362+
363+
setTimeout(function () {
364+
365+
communicating = false;
366+
367+
}, updateDelay);
368+
419369
/* Either send immediately or wait until the transition */
420370

421371
if (sendTimeDiff <= 0) {
422372

423-
setTime(sendTime);
373+
console.log('Sending...');
374+
375+
setAudioMothTime(sendTime);
424376

425377
} else {
426378

427379
console.log('Sending in', sendTimeDiff);
428380

429381
setTimeout(function () {
430382

431-
setTime(sendTime);
383+
setAudioMothTime(sendTime);
432384

433385
}, sendTimeDiff);
434386

@@ -456,4 +408,4 @@ electron.ipcRenderer.on('poll-night-mode', () => {
456408

457409
});
458410

459-
requestTime();
411+
requestAudioMothTime();

0 commit comments

Comments
 (0)