Skip to content

Commit

Permalink
Trycatch (#79)
Browse files Browse the repository at this point in the history
* Minor enhancements

GitHub Issue #10 and simple implementation of #42.

* Beta version with additional error handling, part 1

Some of GitHub issue #74. This check-in is for better printer error handling when running as an app.

* Attempt at better error trapping/logging

Work in progress...

* Change error handling a bit

GitHub Issue #74

* Write to log upon successful print

* Remove try/catch in printing

Electron printing doesn't catch "invalid deviceNmae" error. The try/catch/finally block did, but it caused a problem for printing when there were no printer issues. Remove try/catch/finally. Comment out some superfluous "WriteLog" statements.

* More for issue #74

* Implement global configuration option

Addresses GitHub issue #44 and parts of issue #76.

* Trap errors writing to log file

* Remove beta version number for 2.2 general release
  • Loading branch information
mgobat authored Oct 24, 2023
1 parent ab2a89b commit e5d0e6a
Show file tree
Hide file tree
Showing 11 changed files with 707 additions and 88 deletions.
9 changes: 8 additions & 1 deletion configWindow.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
Interval in minutes (ie: .25, 1, 1.5, 5):<input type="text" id="interval" size=3>
&emsp; <input type="checkbox" id="autostart" name="autostart">Auto Start <br>
</div>
<div id="activeHours">
Active time (ie: 08:00. 21:30):
<label>From:</label>
<input type="text" id="fromTime" size="3">
<label>Until:</label>
<input type="text" id="untilTime" size="3">
</div>
</div>
<br>
<div id="container">
Expand Down Expand Up @@ -177,7 +184,7 @@
<button type="submit">Save and Continue Printing</button>
&emsp;
<button type="button" id="cancelSettings" onclick="javascript:resumePrinting()">Cancel and Continue Printing</button>

&emsp; <label style="color: red" id="globalConfig" name="globalConfig" hidden>Global Configuration</label>
<!--
<input type='text' id='message' size=1000>
<br>
Expand Down
80 changes: 73 additions & 7 deletions configWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let availableAlmaPrinters = [];
let almaPrinters;
let cancelAvailable = false;
let editMode = false;
const remote = require ("electron").remote;

const form = document.querySelector('form');
form.addEventListener('submit', submitForm);
Expand All @@ -22,6 +23,16 @@ function submitForm(e){
let interval = 0;
let orientation;
let color;
let fromTime = document.querySelector('#fromTime').value;
let untilTime = document.querySelector('#untilTime').value;
let options = {
type: 'error',
buttons: ['OK'],
title: 'Configuration Error',

};
const timePattern = /^[0-2][0-9]:[0-5][0-9]/;

const region = document.querySelector('#region').value;
const apiKey = document.querySelector('#apiKey').value;
const autoStart = document.getElementById('autostart');
Expand All @@ -38,28 +49,66 @@ function submitForm(e){
}

if (apiKey.length == 0) {
alert ('An API key must be supplied.');
options.message = 'An API key must be supplied.'
dialog.showMessageBox (remote.getCurrentWindow(), options);
document.getElementById("apiKey").focus();
return false;
}

if (badInterval) {
alert ('The interval must be greater than 0.');
options.message = 'The interval must be greater than 0.';
dialog.showMessageBox (remote.getCurrentWindow(), options);
document.getElementById("interval").focus();
return false;
}
if (!timePattern.test (fromTime)) {
options.message = 'Invalid charcters in Active From time. Must be a valid time in 24-hour format.';
dialog.showMessageBox (remote.getCurrentWindow(), options);
document.getElementById("fromTime").focus();
return false;
}

if (!timePattern.test (untilTime)) {
options.message = 'Invalid charcters in Active Until time. Must be a valid time in 24-hour format.';
dialog.showMessageBox (remote.getCurrentWindow(), options);
document.getElementById("untilTime").focus();
return false;
}

if (fromTime > "24:00") {
options.message = 'Active From time must be a valid 24-hour time.';
dialog.showMessageBox (remote.getCurrentWindow(), options);
document.getElementById("fromTime").focus();
return false;
}

if (untilTime > "24:00") {
options.message = 'Active Until time must be a valid 24-hour time.'
dialog.showMessageBox (remote.getCurrentWindow(), options);
document.getElementById("untilTime").focus();
return false;
}

if (fromTime > untilTime) {
options.message = 'Active From time must be less than Active Until time.'
dialog.showMessageBox (remote.getCurrentWindow(), options);
document.getElementById("fromTime").focus();
return false;
}

if (almaPrinterProfiles.length == 0) {
alert ('At least one Alma Printer Profile must be defined.')
options.message = 'At least one Alma Printer Profile must be defined.'
dialog.showMessageBox (remote.getCurrentWindow(), options);
return false;
}
//If all required config values entered, it is ok to save.
if (apiKey.length && !badInterval && almaPrinterProfiles.length > 0) {

var configString = "{\"region\": \"" + region + "\",";
configString = configString + "\"apiKey\": \"" + apiKey + "\",";
configString = configString + "\"interval\": \"" + interval + "\",";
configString = configString + "\"autoStart\": \"" + autoStart.checked + "\",";
configString = configString + "\"fromTime\": \"" + fromTime + "\",";
configString = configString + "\"untilTime\": \"" + untilTime + "\",";
configString = configString + "\"almaPrinterProfiles\":";
configString = configString + JSON.stringify(almaPrinterProfiles);
configString = configString + "}";
Expand Down Expand Up @@ -92,6 +141,16 @@ ipcRenderer.on('send-settings', (event, configSettings) => {
document.getElementById('methodm').checked = true;
disableAutomaticOptions(true);
}

if (configSettings.fromTime == undefined)
document.getElementById('fromTime').value = "00:00";
else
document.getElementById('fromTime').value = configSettings.fromTime;
if (configSettings.untilTime == undefined)
document.getElementById('untilTime').value = "24:00";
else
document.getElementById('untilTime').value = configSettings.untilTime;

almaPrinterProfiles = configSettings.almaPrinterProfiles;
})

Expand Down Expand Up @@ -145,6 +204,13 @@ ipcRenderer.on('alma-printers', (event, almaPrinters) => {
}
})

ipcRenderer.on('global-config-flag', (event, usingGlobalConfig) => {
let element = document.getElementById('globalConfig');
if (usingGlobalConfig)
element.removeAttribute('hidden');
else
element.setAttribute('hidden', 'hidden');
})

const getPrinterQueues = async (type, offset) =>
await alma.getp(`/conf/printers?printout_queue=${type}&limit=100&offset=${offset}`);
Expand Down Expand Up @@ -255,7 +321,7 @@ function loadAlmaPrinters(almaPrinters) {
console.log ("Parsed Alma printers JSON. Number of printers = " + printersDefined);
let displayName;
for (i = 0; i < printersDefined; i++) {
if (almaPrinters.printer[i].description !== null) {
if (almaPrinters.printer[i].description !== null && almaPrinters.printer[i].description != undefined) {
displayName = almaPrinters.printer[i].name + " - " + almaPrinters.printer[i].description;
}
else {
Expand All @@ -276,7 +342,7 @@ function loadAvailableAlmaPrinters() {
let displayName;
for (let i = 0; i < availableAlmaPrinters.length; i++) {
availablePrinter = JSON.parse(JSON.stringify(availableAlmaPrinters[i]));
if (availablePrinter.description !== null) {
if (availablePrinter.description !== null && availablePrinter.description != undefined) {
displayName = availablePrinter.name + " - " + availablePrinter.description;
}
else {
Expand Down Expand Up @@ -530,7 +596,7 @@ function buildAlmaPrinterDisplayName(almaPrinters, id) {
for (let i = 0; i < printerCount; i++) {
if (almaPrinters.printer[i].id == id) {
displayName = almaPrinters.printer[i].name
if (almaPrinters.printer[i].description != null) {
if (almaPrinters.printer[i].description !== null && almaPrinters.printer[i].description != undefined) {
displayName = displayName + " - " + almaPrinters.printer[i].description;
};
return true;
Expand Down
2 changes: 1 addition & 1 deletion docsPrintIntervalPaused.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8">
<title>Alma Print Daemon 2.1.0</title>
<title>Alma Print Daemon 2.2.0</title>
</head>
<style>
#notification {
Expand Down
104 changes: 104 additions & 0 deletions docsPrintSleeping.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Alma Print Daemon 2.2.0</title>
</head>
<style>
#notification {
position: fixed;
bottom: 20px;
left: 20px;
width: 200px;
padding: 20px;
border-radius: 5px;
background-color: white;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
}
.hidden {
display: none;
}
#updateCheck {
position: fixed;
bottom: 20px;
left: 20px;
width: 200px;
padding: 20px;
border-radius: 5px;
background-color: white;
}
.hidden {
display: none;
}
</style>
<body>
<form>
<h4>
Printing is paused; time is outside active Alma Print Daemon hours.<br>
<br>
<br>
<button type="button" id="displayConfig" onclick="javascript:displayConfigPage()"> Update Configuration</button>
</h4>
<div id="updateCheck">
<button type="button" id="checkForUpdate" onclick="javascript:checkForSoftwareUpdate()"> Check for Update</button>
</div>
<div id="notification" class="hidden">
<p id="message"></p>
<button id="close-button" onClick="closeNotification()" class="hidden">
Close
</button>
<button id="restart-button" onClick="restartApp()" class="hidden">
Restart
</button>
</div>
<script>
const electron = require('electron');
const {ipcRenderer} = electron;
const form = document.querySelector('form');

form.addEventListener('submit', submitForm);

function submitForm(e){
e.preventDefault();
ipcRenderer.send('print-continue');
}

function displayConfigPage() {
ipcRenderer.send('display-config');
}

function checkForSoftwareUpdate() {
ipcRenderer.send('check-for-update');
}

const notification = document.getElementById('notification');
const message = document.getElementById('message');
const restartButton = document.getElementById('restart-button');
ipcRenderer.on('update_available', () => {
ipcRenderer.removeAllListeners('update_available');
message.innerText = 'A new update is available. Downloading now...';
notification.classList.remove('hidden');
});
ipcRenderer.on('update_not_available', () => {
ipcRenderer.removeAllListeners('update_not_available');
console.log ('Sofware is up-to-date');
message.innerText = 'Your software is up to date.';
notification.classList.remove('hidden');
});
ipcRenderer.on('update_downloaded', () => {
ipcRenderer.removeAllListeners('update_downloaded');
message.innerText = 'Update Downloaded. It will be installed on restart. Restart now?';
restartButton.classList.remove('hidden');
notification.classList.remove('hidden');
});

function closeNotification() {
notification.classList.add('hidden');
}
function restartApp() {
ipcRenderer.send('restart_app');
}
</script>
</form>
</body>
</html>
2 changes: 1 addition & 1 deletion docsPrintedInterval.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8">
<title>Alma Print Daemon 2.1.0</title>
<title>Alma Print Daemon 2.2.0</title>
</head>
<style>
#notification {
Expand Down
2 changes: 1 addition & 1 deletion docsPrintedManual.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8">
<title>Alma Print Daemon 2.1.0</title>
<title>Alma Print Daemon 2.2.0</title>
</head>
<style>
#notification {
Expand Down
Loading

0 comments on commit e5d0e6a

Please sign in to comment.