grid-apps-cmms/src/kiri/ui/client.js

358 lines
9.3 KiB
JavaScript
Raw Normal View History

2020-06-03 22:39:30 -04:00
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
2025-12-24 21:55:35 -05:00
import { api } from '../core/api.js';
import { noop } from '../core/utils.js';
import { codec } from '../core/codec.js';
import { Widget } from '../core/widget.js';
import { newPrint } from '../core/print.js';
2021-01-01 11:14:07 -05:00
// this code runs in kiri's main loop
let debug = self.debug === true,
time = Date.now,
seqid = 1,
2020-11-30 22:36:06 -05:00
syncd = {},
running = {},
2021-02-24 11:09:02 -05:00
worker = null,
2023-01-09 14:55:17 -05:00
minions = false,
restarting = false,
workpath,
poolpath;
2020-11-03 11:19:13 -05:00
/**
* @param {Function} fn name of function in kiri.worker
2020-11-03 11:19:13 -05:00
* @param {Object} data to send to server
* @param {Function} onreply function to call on reply messages
* @param {Object[]} zerocopy array of objects to pass using zerocopy
*/
function send(fn, data, onreply, zerocopy) {
let seq = seqid++;
if (onreply) {
// track it only when we expect and can handle a reply
running[seq] = { fn:onreply };
}
let msg = {
seq: seq,
task: fn,
time: time(),
data: data
};
// console.log('client send', msg);
// if (!fn) { console.trace({empty_fn: data}) };
try {
worker.postMessage(msg, zerocopy);
} catch (error) {
console.trace('work send error', {data, error});
}
}
2017-04-18 17:09:33 -04:00
2020-11-03 11:19:13 -05:00
// code is running in the browser / client context
export const client = {
2020-11-03 11:19:13 -05:00
send: send,
setWorkPath(path) {
workpath = path;
return client;
},
setPoolPath(path) {
poolpath = path;
return client;
},
pool: {
start() {
2023-01-09 14:55:17 -05:00
minions = true;
send("pool_start", { url: poolpath }, noop);
},
stop() {
2023-01-09 14:55:17 -05:00
minions = false;
send("pool_stop", {}, noop);
}
},
newWorker() {
2020-06-15 22:47:11 -04:00
if (self.createWorker) {
return self.createWorker();
} else if (self.kiri_work) {
const work = atob(self.kiri_work);
const blob = new Blob([ work ], { type: 'application/javascript' });
return new Worker(URL.createObjectURL(blob));
2020-06-15 22:47:11 -04:00
} else {
let worker = new Worker(workpath || api.const.PATHS.work, { type: 'module' });
worker.onerror = (error) => {
console.log({ WORKER_ERROR: error });
error.preventDefault();
};
worker.onmessageerror = (error) => {
console.log({ WORKER_MESSAGE_ERROR: error });
error.preventDefault();
};
return worker;
2020-06-15 22:47:11 -04:00
}
},
isBusy() {
let current = 0;
2021-02-24 11:09:02 -05:00
for (let rec of Object.values(running)) {
if (rec.fn) current++;
}
return current > 0;
},
2017-04-18 17:09:33 -04:00
start() {
client.restart();
},
restart() {
2021-02-24 11:09:02 -05:00
// prevent re-entry from cancel callback
if (restarting) {
return;
}
if (worker) {
worker.terminate();
}
2017-04-18 17:09:33 -04:00
2021-02-24 11:09:02 -05:00
restarting = true;
for (let key in running) {
let rec = running[key];
if (rec.fn) {
rec.fn({error: "cancelled operation"});
}
}
2017-04-18 17:09:33 -04:00
2021-01-13 14:31:38 -05:00
syncd = {};
running = {};
worker = client.newWorker();
client.onmessage = worker.onmessage = (e) => {
let now = time(),
reply = e.data,
record = running[reply.seq],
onreply = record ? record.fn : undefined;
2020-06-15 22:47:11 -04:00
// console.log('client recv', e)
if (reply.done) {
delete running[reply.seq];
}
// calculate and replace recv time
reply.time_recv = now - reply.time_recv;
if (onreply) {
onreply(reply.data, reply);
} else {
console.log({unexpected_reply: reply});
}
};
2021-02-24 11:09:02 -05:00
2023-01-09 14:55:17 -05:00
// restart minions if they were running
if (minions) {
client.pool.start();
}
2021-02-24 11:09:02 -05:00
restarting = false;
},
config(obj) {
send("config", obj, noop);
},
clear() {
2021-01-29 13:25:41 -05:00
syncd = {};
send("clear", {}, noop);
},
snap(data) {
send("snap", data, noop);
},
png(data, ondone) {
send("png", data, ondone);
},
// widget sync
sync(widgets) {
if (!widgets) {
widgets = api.widgets.all();
}
// sync any widget that has changed
2024-07-06 15:40:58 -04:00
for (let widget of widgets.filter(w => w.modified || !syncd[w.id])) {
syncd[widget.id] = true;
let vertices = widget.getGeoVertices();
send("sync", {
id: widget.id,
meta: widget.meta,
group: widget.group.id,
track: widget.track,
vertices,
2024-07-06 15:40:58 -04:00
position: widget.mesh.position,
}, done => {
widget.modified = false;
});
}
},
2024-07-06 15:40:58 -04:00
// for belt / angled printing
rotate(settings, callback) {
2021-01-24 00:13:09 -05:00
send("rotate", { settings }, reply => {
if (reply.group) {
2024-07-06 15:40:58 -04:00
// collect post-rotation data for slice/preview renders
for (let widget of Widget.Groups.forid(reply.group)) {
2021-01-24 00:13:09 -05:00
widget.belt = reply.belt;
}
} else if (callback) {
callback();
}
});
},
2024-07-06 15:40:58 -04:00
// called once for each widget
slice(settings, widget, callback) {
send("slice", {
id: widget.id,
anno: widget.annotations(),
settings: settings
}, reply => {
callback(reply);
2020-11-08 13:04:54 -05:00
if (reply.done || reply.error) {
// in belt mode, slicing modifies a widget and requires re-sync
if (settings.device.bedBelt) {
widget.modified = true;
}
2020-11-08 13:04:54 -05:00
}
});
},
2024-07-06 15:40:58 -04:00
// called once after each widget is sliced
// performs merged actions that consider all widget slice data
sliceAll(settings, callback) {
2021-07-13 22:41:22 -04:00
send("sliceAll", { settings }, callback);
},
2024-07-06 15:40:58 -04:00
// turn widget slices into unified path output
prepare(settings, update, done) {
send("prepare", { settings }, reply => {
2020-11-08 13:04:54 -05:00
if (reply.progress) {
update(reply.progress, reply.message, reply.layer);
2020-11-08 13:04:54 -05:00
}
if (reply.done) {
done(reply.output, reply.maxSpeed, reply.minSpeed);
2020-11-08 13:04:54 -05:00
}
if (reply.error) {
done(reply);
}
});
},
2024-07-06 15:40:58 -04:00
// convert path output to gcode or other target file output
export(settings, online, ondone) {
send("export", { settings }, reply => {
2020-04-14 09:26:59 -04:00
if (reply.line) {
online(reply.line);
}
if (reply.done) {
2020-11-08 17:35:40 -05:00
ondone(reply.output);
2020-04-14 09:26:59 -04:00
}
if (reply.error) {
ondone(null, reply.error);
}
2021-06-18 15:03:00 -04:00
if (reply.debug) {
api.event.emit("export.debug", reply.debug);
2021-06-18 15:03:00 -04:00
}
2020-04-14 09:26:59 -04:00
});
},
colors(colors, max, done) {
send("colors", { colors, max }, reply => {
2020-11-11 08:07:02 -05:00
done(reply);
});
},
parse(args, progress, done) {
2020-11-09 23:01:41 -05:00
// have to do this client side because DOMParser is not in workers
if (args.type === 'svg') {
const { settings, code, type } = args;
const origin = settings.origin;
const offset = {
x: origin.x,
y: -origin.y,
z: origin.z
};
const output = newPrint().parseSVG(code, offset);
send("parse_svg", output, reply => {
2020-11-09 23:01:41 -05:00
if (reply.parsed) {
done(codec.decode(reply.parsed));
2020-11-09 23:01:41 -05:00
}
});
return;
}
send("parse", args, reply => {
2020-11-09 13:57:18 -05:00
if (reply.progress) {
2021-02-26 09:32:34 -05:00
progress(reply.progress);
2020-11-09 13:57:18 -05:00
}
if (reply.parsed) {
done(codec.decode(reply.parsed), reply.maxSpeed, reply.minSpeed);
}
});
2020-11-26 23:18:28 -05:00
},
image2mesh(data, progress, output) {
2020-11-26 23:18:28 -05:00
send("image2mesh", data, reply => {
if (reply.progress) {
progress(reply.progress);
}
if (reply.vertices) {
output(reply.vertices);
2020-11-26 23:18:28 -05:00
}
}, [ data.png ]);
2021-02-01 16:39:22 -05:00
},
gerber2mesh(data, progress, output) {
send('gerber2mesh', data, reply => {
if (reply.progress) {
progress(reply.progress);
}
if (reply.vertices) {
output(reply.vertices);
}
});
},
zip(files, progress, output) {
2021-02-01 16:39:22 -05:00
send("zip", {files}, reply => {
2021-02-01 17:44:25 -05:00
if (reply.percent !== undefined) {
2021-02-01 17:13:55 -05:00
progress(reply);
} else {
output(reply);
}
2021-02-01 16:39:22 -05:00
});
},
wasm(enable) {
send("wasm", {enable}, reply => {
// console.log({wasm_worker_said: reply});
});
2023-01-09 14:55:17 -05:00
},
putCache(key, data) {
console.log({ putCache: key, data });
send("putCache", {key, data}, reply => {
// console.log({ putCache_reply: reply });
});
},
clearCache() {
send("clearCache", {}, reply => {
// console.log({ clearCache_reply: reply });
});
}
};
2017-04-18 17:09:33 -04:00
self.kiri_client = client;
export default client;