Skip to content

Commit

Permalink
Integrate status endpoint with job and transfer requests
Browse files Browse the repository at this point in the history
  • Loading branch information
voxelias authored and ondratu committed May 2, 2023
1 parent 28beed6 commit 2170e3b
Show file tree
Hide file tree
Showing 25 changed files with 700 additions and 673 deletions.
37 changes: 8 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,12 @@ let connectionProblem = false;
* Time is only approximate, it is not guaranteed!
*/
const requests = {
printer: {
get: () => getJson("/api/printer"),
status: {
get: () => getJson("/api/v1/status"),
init: true,
update: true,
// updateInterval: process.env.CONNECTION_UPDATE_INTERVAL,
},
profiles: {
// NOTE: I leave it like this until we change the API
get: () =>
process.env.PRINTER_TYPE === "sla"
? getJson("/api/printerprofiles")
: new Promise((resolve) => resolve({})),
init: true,
update: false,
},
job: {
get: () => getJson("/api/job"),
init: false,
update: true,
},
connection: {
get: () => getJson("/api/connection"),
init: process.env.WITH_CONNECTION,
update: process.env.WITH_CONNECTION,
updateInterval: process.env.CONNECTION_UPDATE_INTERVAL,
},
// version will be passed on init
};

async function getRequests(initialized) {
Expand Down Expand Up @@ -111,20 +91,20 @@ window.onload = () => {
});
});

initAuth().then((version) => {
if (version) appLoop(version);
initAuth().then(printer => {
if (printer) appLoop(printer);
});
};

async function appLoop(version) {
async function appLoop(printerInfo) {
let initialized = false;

while (true) {
let apiProblem = false;

try {
const responses = await getRequests(initialized);
if (responses.printer) connectionProblem = responses.printer.ok === null;
if (responses.status) connectionProblem = responses.status.ok === null;

Object.values(responses).forEach(({ ok, error }) => {
if (!ok) {
Expand All @@ -139,8 +119,7 @@ async function appLoop(version) {
update(responses);
} else {
if (!apiProblem) {
responses.version = { ok: true, payload: version };
init(responses);
init({...responses, printer: printerInfo});
initialized = true;
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/printer/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LinkState, translateState } from "../state";
const SEPARATOR = " - ";

export const getPrinterLabel = (context) => {
return buildTitle([context.version?.location, context.version?.name]);
return buildTitle([context.printer?.location, context.printer?.name]);
};

export const buildTitle = (titleItems) => {
Expand All @@ -14,18 +14,20 @@ export const buildTitle = (titleItems) => {
};

export const getStatusForTitle = (context) => {
const linkState = LinkState.fromApi(context.printer.state);
const linkState = context.state;
let stateText = translateState(linkState);

switch (linkState) {
case 'IDLE':
return '';

case 'PRINTING':
const progress = Math.round((context?.current?.progress?.completion || 0) * 100);
const progress = Math.round((context?.job?.progress || 0));
return `${stateText} ${progress}%`;

default:
return stateText;
}
}

export const getEstimatedEnd = (timeRemaining) => Math.round(Date.now() / 1000) + timeRemaining;
7 changes: 5 additions & 2 deletions src/printer/components/cameras.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Dropdown } from "./dropdown";
import { handleError } from "./errors";

let allowCloud = false;
let currentCameraId = null;
let currentCameraId = undefined;
let cameras = [];

const triggerScheme = {
Expand Down Expand Up @@ -51,7 +51,10 @@ const load = (context) => {
};

const update = (context, updateUI = updateCamerasUI) => {
allowCloud = context.connection.states.connect.ok;
if (currentCameraId === undefined) {
currentCameraId = context.camera.id;
}
allowCloud = context.link.connect.ok;
getJson("/api/v1/cameras")
.then((result) => {
const list = (result?.data?.camera_list || []).map((item) => {
Expand Down
22 changes: 12 additions & 10 deletions src/printer/components/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { setDisabled, setEnabled } from "../../helpers/element";
import updateProperties from "./updateProperties";
import { disableSteppers, extrude, homePrinthead, movePrinthead, retract, setBedTemperature, setFlowRate, setNozzleTemperature, setSpeed } from "./controlActions";
import { handleError } from "./errors";
import { LinkState } from "../../state";
import { context } from "../fdm";

let moveStep = 1;
let extrudeRetractStep = 1;
Expand All @@ -32,12 +34,8 @@ const load = (context) => {
};

const update = (context) => {
updateProperties("control", {
temperature: context.printer.temperature,
telemetry: context.printer.telemetry,
job: context.current,
});
updateButtons(context.printer.state, context.printer.telemetry, context.version);
updateProperties("control", context);
updateButtons(context);
}

function initButtons() {
Expand All @@ -55,7 +53,9 @@ function initDisableSteppersBtn() {
};
}

function updateButtons(state, telemetry, info) {
function updateButtons(context) {
const state = context.state;

const controls = document.querySelectorAll("#control button");
const nozzleControls = ["extrude", "retract"];
const whitelistWhenPrinting = [
Expand All @@ -73,15 +73,17 @@ function updateButtons(state, telemetry, info) {
"heated-bed-xy-move",
];

if (state.flags.printing || state.flags.pausing || state.flags.paused) {
const whitelist = state.flags.paused ? whitelistWhenPaused : whitelistWhenPrinting;
if ([LinkState.PRINTING || LinkState.BUSY || LinkState.PAUSED].includes(state)) {
const whitelist = state === LinkState.PAUSED ? whitelistWhenPaused : whitelistWhenPrinting;
controls.forEach(btn => {
const controlId = btn.id || btn.parentNode.id || btn.parentNode.parentNode.id;
setDisabled(btn, !whitelist.includes(controlId));
});
}

const canControlNozzle = (telemetry && info && telemetry["temp-nozzle"] >= info["min_extrusion_temp"]);
const nozzleTemp = context.telemetry.temperature.nozzle.current || 0;
const minExtrusionTemp = context.printer.minExtrusionTemp || 0;
const canControlNozzle = (nozzleTemp && minExtrusionTemp && nozzleTemp >= minExtrusionTemp);

nozzleControls.forEach(
control => setDisabled(document.getElementById(control), !canControlNozzle)
Expand Down
35 changes: 21 additions & 14 deletions src/printer/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ const redrawFiles = () => {
* @param {object} context
*/
export const update = (context) => {
const linkState = LinkState.fromApi(context.printer.state);
const linkState = context.state;
updateStorage();
updateFiles();
job.update(context, true);
Expand Down Expand Up @@ -323,9 +323,10 @@ export function load(context) {
loadPreviewQueued();
}
} else {
getImage(entry.target.getAttribute("data-src")).then(({ url }) => {
const dataSrc = entry.target.getAttribute("data-src");
getImage(dataSrc).then(({ url }) => {
entry.target.src = url;
});
}).catch(() => {}); // NOTE: ignore missing thumbnails
}
}
});
Expand All @@ -336,13 +337,11 @@ export function load(context) {
context = printer.getContext();
}

const previewFile = job.getPreviewFile();
const previewFile = context.files.selected;
if (previewFile) {
const file = findFile(previewFile.origin, previewFile.path);
if (!file) {
job.selectFilePreview(null);
} else if (file.date > previewFile.data) {
job.selectFilePreview(file);
context.selectFile(null);
}
}
job.update(context, true);
Expand Down Expand Up @@ -502,18 +501,26 @@ const onClickFile = (node) => {
};

function showPreview(file) {
const currentPreview = job.getPreviewFile();
const path = getCurrentApiPath(file.name);
const context = printer.getContext();
const currentPreview = context.files.selected;
const path = getCurrentPath();
const resource = getCurrentApiPath(file.name);

if (currentPreview === path) {
return;
}

getJson(path)
.then(result => {
job.selectFilePreview(result.data, path)
job.update(printer.getContext(), true)
});
context.selectFile({
...file,
path,
resource
});

//getJson(path)
// .then(result => {
// job.selectFilePreview(result.data, path)
// job.update(printer.getContext(), true)
// });

const jobElement = document.getElementById("job");
if (jobElement) {
Expand Down
Loading

0 comments on commit 2170e3b

Please sign in to comment.