Skip to content

Commit

Permalink
Fetch image immediately before presenting/sending
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
stuartpb authored Mar 6, 2024
1 parent 72cbb80 commit 7f30a51
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
21 changes: 12 additions & 9 deletions herald/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ function killViewerProcess() {
return viewerProcess.then(lose, lose);
}

function startViewerProcess(location, duration) {
async function startViewerProcess(imageStream, duration) {
// defer to the termination of any existing viewer process
if (viewerProcess) {
return killViewerProcess().then(
startViewerProcess.bind(null, location, duration));
await killViewerProcess();
return startViewerProcess(imageStream, duration);
}

// set up a new viewer process
viewerProcess = asDesktopUser`feh -F ${location}`;
viewerProcess = asDesktopUser({input: imageStream})`feh -F -`;

// log any errors, except the one we're expecting
viewerProcess.catch(logNonSigTermErrors);
Expand All @@ -70,20 +70,23 @@ function startViewerProcess(location, duration) {
if (duration) {
viewerTimeout = setTimeout(killViewerProcess, duration);
}
}

// ensure that we're returning a resolved Promise context,
// whether this was called from killViewerProcess.then or not
return Promise.resolve();
async function fetchAndDisplay(location, duration) {
const res = await fetch(location);
if (!res.ok) throw new Error(
`HTTP error fetching image: ${response.status} ${response.statusText}`);
return startViewerProcess(res.body, duration);
}

app.post('/present/still', (req, res, next) => {
return Promise.all([
// wake the display
asDesktopUser`xset dpms force on`,
// present the still
startViewerProcess(req.query.location, req.query.duration)
fetchAndDisplay(req.query.location, req.query.duration)

].map(p=>p.catch(next))).then(()=>res.send());
].map(p => p.catch(next))).then(() => res.send());
});

app.listen(80);
7 changes: 5 additions & 2 deletions sentinel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ const mailer = EMAIL_RECIPIENT && nodemailer.createTransport({
},
});

function sendEmailNotification() {
async function sendEmailNotification() {
const cid = uuid();
const res = await fetch(SNAP_URL);
if (!res.ok) throw new Error(
`HTTP error fetching image: ${response.status} ${response.statusText}`);
return mailer.sendMail({
from: {name: EMAIL_SENDER_NAME, address: EMAIL_SENDER_ADDRESS},
to: EMAIL_RECIPIENT,
Expand All @@ -72,7 +75,7 @@ function sendEmailNotification() {
html: `<img src="cid:${cid}">`,
attachments: [{
filename: EMAIL_ATTACHMENT_FILENAME,
path: SNAP_URL,
content: res.body,
cid }]
});
}
Expand Down

0 comments on commit 7f30a51

Please sign in to comment.