Skip to content

Commit

Permalink
feat: prevent machine from sleeping while plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Mar 14, 2019
1 parent ffcecce commit cbd3dee
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"cors": "^2.8.5",
"express": "^4.16.4",
"serialport": "^7.1.3",
"wake-lock": "^0.2.0",
"ws": "^6.1.3",
"yargs": "^12.0.5"
},
Expand Down
1 change: 1 addition & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
declare module '*.svg';
declare module 'wake-lock';
34 changes: 21 additions & 13 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import cors from "cors";
import express from "express";
import http from "http";
import path from "path";
import { WakeLock } from "wake-lock";
import WebSocket from "ws";
import { EBB } from "./ebb";
import { Device, PenMotion, Plan } from "./planning";
Expand Down Expand Up @@ -49,22 +50,29 @@ export function startServer(port: number, device: string | null = null, enableCo
});
});

app.post("/plot", (req, res) => {
app.post("/plot", async (req, res) => {
const plan = Plan.deserialize(req.body);
console.log(`Received plan of estimated duration ${formatDuration(plan.duration())}`);
if (ebb != null) {
console.log("Beginning plot...");
const begin = Date.now();
doPlot(plan).then(() => {
const end = Date.now();
console.log(`Plot took ${formatDuration((end - begin) / 1000)}`);
});
} else {
simulatePlot(plan).then(() => {
console.log("Simulation complete");
});
}
console.log(ebb != null ? "Beginning plot..." : "Simulating plot...");
res.status(200).end();

const begin = Date.now();
let wakeLock: any;
try {
wakeLock = new WakeLock("saxi plotting");
} catch (e) {
console.warn("Couldn't acquire wake lock. Ensure your machine does not sleep during plotting");
}

try {
await (ebb != null ? doPlot(plan) : simulatePlot(plan));
const end = Date.now();
console.log(`Plot took ${formatDuration((end - begin) / 1000)}`);
} finally {
if (wakeLock) {
wakeLock.release();
}
}
});

app.post("/cancel", (req, res) => {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
}
},
"files": [
"src/cli.ts"
"src/cli.ts",
"src/global.d.ts"
]
}

0 comments on commit cbd3dee

Please sign in to comment.