Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch image immediately before presenting/sending #4

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions herald/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Gpio} from 'onoff';
import morgan from 'morgan';
import {$} from 'execa';
import express from 'express';
import {Readable} from 'stream';

// Relay for ringing the doorbell.
const relay = new Gpio(4, 'high', {activeLow: true});
Expand Down Expand Up @@ -53,15 +54,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 +71,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(Readable.fromWeb(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);
8 changes: 6 additions & 2 deletions sentinel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Gpio} from 'onoff';
import debounce from 'debounce';
import {v4 as uuid} from 'uuid';
import nodemailer from 'nodemailer';
import {Readable} from 'stream';

const sensor = new Gpio(3, 'in', 'both');

Expand Down Expand Up @@ -61,8 +62,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 +76,7 @@ function sendEmailNotification() {
html: `<img src="cid:${cid}">`,
attachments: [{
filename: EMAIL_ATTACHMENT_FILENAME,
path: SNAP_URL,
content: Readable.fromWeb(res.body),
cid }]
});
}
Expand Down