Skip to content

Commit

Permalink
Add 1.1.3 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pcprince committed Dec 12, 2023
1 parent f6858ed commit 59ae252
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 149 deletions.
19 changes: 11 additions & 8 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module.exports = {
"extends": "standard",
"rules": {
"semi": [2, "always"],
"indent": ["error", 4],
"padded-blocks": ["error", "always"],
"no-useless-escape": 0,
"object-curly-spacing": ["error", "never"]
extends: 'standard',
rules: {
semi: [2, 'always'],
indent: ['error', 4],
'padded-blocks': ['error', 'always'],
'no-useless-escape': 0,
'object-curly-spacing': ['error', 'never'],
'no-extend-native': ['error', {exceptions: ['Array']}],
'standard/no-callback-literal': 0,
'no-throw-literal': 0
}
};
};
222 changes: 87 additions & 135 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
const electron = require('electron');
const {clipboard, Menu, dialog, getCurrentWindow} = require('@electron/remote');

const util = require('util');
const strftime = require('strftime').utc();
const audiomoth = require('audiomoth-hid');

const versionChecker = require('./versionChecker.js');
const nightMode = require('./nightMode.js');
require('worker_threads');

/* UI components */

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

const MILLISECONDS_IN_SECOND = 1000;

/* Whether or not communication with device is currently happening */

let communicating = false;

let currentTime, deviceId, firmwareVersion, firmwareDescription;
/* Communication constants */

const MAXIMUM_RETRIES = 10;
const DEFAULT_RETRY_INTERVAL = 100;

/* Time display functions */

Expand Down Expand Up @@ -73,11 +80,7 @@ function disableDisplay () {

function enableDisplayAndShowTime (date) {

if (communicating) {

return;

}
if (communicating) return;

const strftimeUTC = strftime.timezone(0);

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

setTimeButton.disabled = false;

applicationMenu.getMenuItemById('copyid').enabled = true;

}

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

idLabel.classList.remove('grey');

applicationMenu.getMenuItemById('copyid').enabled = true;

}

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

}

/* Error response */
/* Utility functions */

function errorOccurred (err) {
async function callWithRetry (funcSync, argument, milliseconds, repeats) {

console.error(err);
let result;

disableDisplay();
let attempt = 0;

}
while (attempt < repeats) {

/* Device interaction functions */
try {

function requestFirmwareDescription () {
if (argument) {

audiomoth.getFirmwareDescription(function (err, description) {
result = await funcSync(argument);

if (communicating) return;
} else {

if (err) {
result = await funcSync();

errorOccurred(err);
}

} else if (description === null) {
break;

disableDisplay();
} catch (e) {

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

firmwareDescription = description;
await delay(interval);

requestFirmwareVersion();
attempt += 1;

}

});

}

function requestFirmwareVersion () {

audiomoth.getFirmwareVersion(function (err, versionArr) {

if (communicating) return;

if (err) {

errorOccurred(err);

} else if (versionArr === null) {

disableDisplay();

} else {

firmwareVersion = versionArr[0] + '.' + versionArr[1] + '.' + versionArr[2];
}

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

}
if (result === null) throw ('No device detected');

});
return result;

}

function requestBatteryState () {

audiomoth.getBatteryState(function (err, batteryState) {

if (communicating) return;

if (err) {

errorOccurred(err);
async function delay (milliseconds) {

} else if (batteryState === null) {

disableDisplay();

} else {

enableDisplayAndShowTime(currentTime);
enableDisplayAndShowID(deviceId);
enableDisplayAndShowVersionDescription(firmwareDescription);
enableDisplayAndShowVersionNumber(firmwareVersion);
enableDisplayAndShowBatteryState(batteryState);

}

});
return new Promise(resolve => setTimeout(resolve, milliseconds));

}

function requestID () {

audiomoth.getID(function (err, id) {

if (communicating) return;

if (err) {
/* Promisified versions of AudioMoth-HID calls */

errorOccurred(err);
const getFirmwareDescription = util.promisify(audiomoth.getFirmwareDescription);

} else if (id === null) {
const getFirmwareVersion = util.promisify(audiomoth.getFirmwareVersion);

disableDisplay();
const getBatteryState = util.promisify(audiomoth.getBatteryState);

} else {
const getID = util.promisify(audiomoth.getID);

deviceId = id;
const getTime = util.promisify(audiomoth.getTime);

requestFirmwareDescription();
const setTime = util.promisify(audiomoth.setTime);

}
/* Device interaction functions */

});
async function requestAudioMothTime () {

}
try {

function requestTime () {
/* Read from AudioMoth */

if (communicating) return;
const date = await callWithRetry(getTime, null, DEFAULT_RETRY_INTERVAL, MAXIMUM_RETRIES);

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

if (communicating) return;
const description = await callWithRetry(getFirmwareDescription, null, DEFAULT_RETRY_INTERVAL, MAXIMUM_RETRIES);

if (err) {
const versionArr = await callWithRetry(getFirmwareVersion, null, DEFAULT_RETRY_INTERVAL, MAXIMUM_RETRIES);

errorOccurred(err);
const batteryState = await callWithRetry(getBatteryState, null, DEFAULT_RETRY_INTERVAL, MAXIMUM_RETRIES);

} else if (date === null) {
/* No exceptions have occurred so update display */

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

} else {
enableDisplayAndShowTime(date);
enableDisplayAndShowID(id);
enableDisplayAndShowVersionDescription(description);
enableDisplayAndShowVersionNumber(firmwareVersion);
enableDisplayAndShowBatteryState(batteryState);

currentTime = date;
} catch (e) {

requestID();
/* Problem reading from AudioMoth or no AudioMoth */

}
disableDisplay();

});
}

const milliseconds = Date.now() % MILLISECONDS_IN_SECOND;

let delay = MILLISECONDS_IN_SECOND / 2 - milliseconds;

if (delay < 0) delay += MILLISECONDS_IN_SECOND;

setTimeout(requestTime, delay);
setTimeout(requestAudioMothTime, delay);

}

function setTime (time) {

audiomoth.setTime(time, function (err, date) {

if (err) {
async function setAudioMothTime (time) {

errorOccurred(err);
try {

} else if (date === null) {
await callWithRetry(setTime, time, DEFAULT_RETRY_INTERVAL, MAXIMUM_RETRIES);

disableDisplay();
} catch (e) {

} else {
disableDisplay();

enableDisplayAndShowTime(date);

}

});
}

}

Expand Down Expand Up @@ -377,8 +328,6 @@ initialiseDisplay();

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

communicating = true;

timeDisplay.classList.add('grey');

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

const MILLISECONDS_IN_SECOND = 1000;

/* Update button */

setTimeButton.disabled = true;

setTimeout(function () {

communicating = false;

requestTime();

setTimeButton.disabled = false;

}, 1500);

/* Increment to next second transition */

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

const now = new Date();

const sendTimeDiff = sendTime.getTime() - now.getTime();

/* Calculate when to re-enable time display */

communicating = true;

setTimeButton.disabled = true;

const updateDelay = sendTimeDiff <= 0 ? MILLISECONDS_IN_SECOND : sendTimeDiff;

setTimeout(function () {

communicating = false;

}, updateDelay);

/* Either send immediately or wait until the transition */

if (sendTimeDiff <= 0) {

setTime(sendTime);
console.log('Sending...');

setAudioMothTime(sendTime);

} else {

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

setTimeout(function () {

setTime(sendTime);
setAudioMothTime(sendTime);

}, sendTimeDiff);

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

});

requestTime();
requestAudioMothTime();
Loading

0 comments on commit 59ae252

Please sign in to comment.