-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosd-printer-module-authorized.js
446 lines (371 loc) · 16.4 KB
/
osd-printer-module-authorized.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"use strict()";
var printerModuleCount = 0;
var numModulesPerRow = 4;
var snapshotHeightToWidthRatio = 1.3333333; // 440px/330px
var shouldUpdateSnapshots = true;
var offlineImageSrc = "img/offline.png";
// Instantiate the printer module
var printerModule = function(printer) {
this.DOM = this.createPrinterModule(printer);
this.id = this.DOM.id;
};
// This method is essentially a constructor for a printer module DOM object.
// The id for the module is generated here, so you can't get the id of the
// module until after running this function. This function also creates its own
// rows, which is a TODO since that is a little weird.
printerModule.prototype.createPrinterModule = function(printer) {
// Create a new row if needed
if (printerModuleCount % numModulesPerRow === 0) {
var row = document.createElement("DIV");
row.className = "row";
document.getElementById("container").appendChild(row);
}
//Update module count
printerModuleCount++;
// Create module DOM elements
var col = document.createElement("DIV");
col.className = "col-xs-12 col-sm-6 col-md-6 col-lg-3 col-xl-3";
var module = document.createElement("DIV");
module.className = "printer_module";
module.id = printerModuleCount;
var printerName = document.createElement("DIV");
printerName.className = "printer_name";
if(printer !== undefined) // Properly constucted printer object
printerName.innerHTML = printer.name;
else {
// Badly constructed printers still need a name
printerName.innerHTML = "Error" + module.id;
}
var snapshotWrapper = document.createElement("DIV");
snapshotWrapper.className = "snapshot_wrapper";
var snapshot = document.createElement("IMG");
snapshot.className = "snapshot";
snapshot.id = "snapshot_" + Number(module.id);
snapshot.src = printer.octoprintWebcamSnapshotUrl;
snapshot.alt = printerName.innerHTML;
snapshot.setAttribute("data-rotation-angle", "0");
snapshot.setAttribute("data-horizontal-flip", "False"); // Capitalized because of database
snapshot.setAttribute("data-vertical-flip", "False"); // Capitalized because of database
// Show the error image if the connection can't be established
snapshot.onerror = function() {
snapshot.src = offlineImageSrc;
setTimeout(function(){
if(shouldUpdateSnapshots) {
var printerId = snapshot.id.replace("snapshot_", "");
var printer = getPrinterByPrinterId(printerId);
snapshot.src = printer.octoprintWebcamSnapshotUrl;
}
}, 3000);
};
snapshotWrapper.appendChild(snapshot);
var infoOverlay = this.createInfoOverlay(Number(module.id), printer); // Overlay that covers snapshot
snapshotWrapper.appendChild(infoOverlay);
var settingsOverlay = this.createSettingsOverlay(Number(module.id)); // Overlay that covers snapshot
snapshotWrapper.appendChild(settingsOverlay);
var toolbar = document.createElement("DIV");
toolbar.className = "toolbar";
toolbar.id = "toolbar_" + Number(module.id);
var printerIcon = document.createElement("IMG");
printerIcon.className = "icon printer_icon";
printerIcon.id = "printer_icon_" + Number(module.id);
printerIcon.src = "img/3dprinter_icon.png";
var infoIcon = document.createElement("IMG");
infoIcon.className = "icon info_icon";
infoIcon.id = "info_icon" + Number(module.id);
infoIcon.src = "img/info_icon.png";
var settingsIcon = document.createElement("IMG");
settingsIcon.className = "icon settings_icon";
settingsIcon.id = "settings_icon" + Number(module.id);
settingsIcon.src = "img/gear_icon.png";
var status = document.createElement("DIV");
status.className = "status";
status.id = "status_" + Number(module.id);
var progressContainer = document.createElement("DIV");
progressContainer.className = "progress";
var progressBar = document.createElement("DIV");
progressBar.className = "progress-bar progress-bar-danger progress-bar-striped active";
progressBar.id = "progress_bar_" + Number(module.id);
progressBar.role = "progressbar";
$(progressBar).attr("aria-valuenow", "0");
$(progressBar).attr("aria-valuemin", "0");
$(progressBar).attr("aria-valuemax", "100");
$(progressBar).attr("style", "width:0%");
progressBar.innerHTML = "0%";
progressContainer.appendChild(progressBar);
status.appendChild(progressContainer);
// Structure them
module.appendChild(printerName);
module.appendChild(snapshotWrapper);
module.appendChild(toolbar);
toolbar.appendChild(printerIcon);
toolbar.appendChild(infoIcon);
toolbar.appendChild(settingsIcon);
module.appendChild(status);
col.appendChild(module);
// Display them
document.getElementById("container").lastChild.appendChild(col);
return module;
};
printerModule.prototype.updateProgressBar = function(value) {
var progressBar = document.getElementById("progress_bar_" + Number(this.id));
$(progressBar).attr("aria-valuenow", "" + value + "%");
$(progressBar).attr("style", "width:" + value + "%");
progressBar.innerHTML = "" + value + "%";
};
printerModule.prototype.updateJobName = function(value) {
if(value !== null) {
var textArea = document.getElementById("file_value_" + Number(this.id));
textArea.innerHTML = value;
}
};
printerModule.prototype.updatePrintApproximation = function(value) {
if(value !== null) {
var textArea = document.getElementById("approx_total_print_time_value_" + Number(this.id));
textArea.innerHTML = String(value).toHHMMSS();
}
};
printerModule.prototype.updateProgress = function(value) {
var textArea;
// Update status overlay views
if (value.printTime !== null) {
textArea = document.getElementById("print_time_value_" + Number(this.id));
textArea.innerHTML = String(value.printTime).toHHMMSS();
}
if (value.printTimeLeft !== null) {
textArea = document.getElementById("print_time_left_value_" + Number(this.id));
textArea.innerHTML = String(value.printTimeLeft).toHHMMSS();
}
// Update progress bar
if (value.completion !== null) {
this.updateProgressBar(value.completion.toFixed(0)); // Remove decimals
}
};
printerModule.prototype.updateState = function(state) {
// Update state text
if(state.text !== null) {
var textArea = document.getElementById("state_value_" + Number(this.id));
textArea.innerHTML = state.text;
}
// Update printer module color
if(state.flags !== null) {
if(state.flags.paused === true) {
this.DOM.className = "printer_module paused";
}
else if(state.flags.printing === true) {
this.DOM.className = "printer_module printing";
}
else if(state.flags.error === true) {
this.DOM.className = "printer_module error";
}
else if(state.flags.ready === true) {
this.DOM.className = "printer_module idle";
}
else {
this.DOM.className = "printer_module offline";
}
}
};
printerModule.prototype.updateBedTemp = function(value) {
// TODO
};
printerModule.prototype.updateExtruderTemps = function(value) {
// TODO
};
printerModule.prototype.updatePrinterStatus = function(message) {
this.updateState(message.state);
this.updateJobName(message.job.file.name);
this.updatePrintApproximation(message.job.estimatedPrintTime);
this.updateProgress(message.progress);
};
printerModule.prototype.updateSnapshotView = function() {
if(shouldUpdateSnapshots) {
var snapshot = document.getElementById("snapshot_" + this.id);
var copy = snapshot.src;
snapshot.src = copy;
}
};
printerModule.prototype.createInfoOverlay = function(moduleId, printer) {
// Create elements
var infoOverlay = document.createElement("DIV");
infoOverlay.className = "overlay info_overlay";
infoOverlay.id = "info_overlay" + moduleId;
var overlayContent = document.createElement("DIV");
overlayContent.className = "info_overlay_content";
/*************************************************/
var overlayTextLine = document.createElement("DIV");
overlayTextLine.className = "text_line";
overlayContent.append(overlayTextLine);
var printerType = document.createElement("DIV");
printerType.className = "printer_type_value";
printerType.id = "printer_type_value_" + moduleId;
printerType.innerHTML = "Type: " + printer.type;
overlayTextLine.append(printerType);
/*************************************************/
overlayTextLine = document.createElement("DIV");
overlayTextLine.className = "text_line";
overlayContent.append(overlayTextLine);
var stateKey = document.createElement("DIV");
stateKey.className = "state_key";
stateKey.id = "state_key_" + moduleId;
stateKey.innerHTML = "State: ";
overlayTextLine.append(stateKey);
var stateValue = document.createElement("DIV");
stateValue.className = "state_value";
stateValue.id = "state_value_" + moduleId;
overlayTextLine.append(stateValue);
/*************************************************/
overlayTextLine = document.createElement("DIV");
overlayTextLine.className = "text_line";
overlayContent.append(overlayTextLine);
var fileKey = document.createElement("DIV");
fileKey.className = "file_key";
fileKey.id = "file_key_" + moduleId;
fileKey.innerHTML = "File: ";
overlayTextLine.append(fileKey);
var fileValue = document.createElement("DIV");
fileValue.className = "file_value";
fileValue.id = "file_value_" + moduleId;
overlayTextLine.append(fileValue);
/*************************************************/
overlayTextLine = document.createElement("DIV");
overlayTextLine.className = "text_line";
overlayContent.append(overlayTextLine);
var approxTotalPrintTimeKey = document.createElement("DIV");
approxTotalPrintTimeKey.className = "approx_total_print_time_key";
approxTotalPrintTimeKey.id = "approx_total_print_time_key_" + moduleId;
approxTotalPrintTimeKey.innerHTML = "Approx Total Print Time: ";
overlayTextLine.append(approxTotalPrintTimeKey);
var approxTotalPrintTimeValue = document.createElement("DIV");
approxTotalPrintTimeValue.className = "approx_total_print_time_value";
approxTotalPrintTimeValue.id = "approx_total_print_time_value_" + moduleId;
overlayTextLine.append(approxTotalPrintTimeValue);
/*************************************************/
overlayTextLine = document.createElement("DIV");
overlayTextLine.className = "text_line";
overlayContent.append(overlayTextLine);
var printTimeKey = document.createElement("DIV");
printTimeKey.className = "print_time_key";
printTimeKey.id = "print_time_key_" + moduleId;
printTimeKey.innerHTML = "Print Time: ";
overlayTextLine.append(printTimeKey);
var printTimeValue = document.createElement("DIV");
printTimeValue.className = "print_time_value";
printTimeValue.id = "print_time_value_" + moduleId;
overlayTextLine.append(printTimeValue);
/*************************************************/
overlayTextLine = document.createElement("DIV");
overlayTextLine.className = "text_line";
overlayContent.append(overlayTextLine);
var printTimeLeftKey = document.createElement("DIV");
printTimeLeftKey.className = "print_time_left_key";
printTimeLeftKey.id = "print_time_left_key_" + moduleId;
printTimeLeftKey.innerHTML = "Print Time Left: ";
overlayTextLine.append(printTimeLeftKey);
var printTimeLeftValue = document.createElement("DIV");
printTimeLeftValue.className = "print_time_left_value";
printTimeLeftValue.id = "print_time_left_value_" + moduleId;
overlayTextLine.append(printTimeLeftValue);
// Structure it
infoOverlay.appendChild(overlayContent);
return infoOverlay;
};
printerModule.prototype.createSettingsOverlay = function(id) {
// Create elements
var settingsOverlay = document.createElement("DIV");
settingsOverlay.className = "overlay settings_overlay";
settingsOverlay.id = "settings_overlay" + id;
var overlayContent = document.createElement("DIV");
overlayContent.className = "settings_overlay_content";
var rotateLeft = document.createElement("A");
rotateLeft.className = "icon rotate_left_link";
rotateLeft.id = "rotate_left_link_" + id;
var rotateLeftIcon = document.createElement("IMG");
rotateLeftIcon.className = "icon rotate_left_icon";
rotateLeftIcon.id = "rotate_left_icon_" + id;
rotateLeftIcon.src = "img/rotate_left.png";
$(rotateLeft).on("click", function() {
var id = this.id.replace("rotate_left_link_", "");
var printer = getPrinterByModuleId(id);
printer.printerModule.rotateSnapshotLeft90Deg();
// Get the rotation angle stored in the snapshot img tag
// var image = document.getElementById("snapshot_" + id);
// angle = Number(image.getAttribute("data-rotation-angle"));
// Update the server database
// sendRotationUpdateToServer(id, angle);
});
var rotateRight = document.createElement("A");
rotateRight.className = "icon rotate_right_link";
rotateRight.id = "rotate_right_link_" + id;
var rotateRightIcon = document.createElement("IMG");
rotateRightIcon.className = "icon rotate_right_icon";
rotateRightIcon.id = "rotate_right_icon_" + id;
rotateRightIcon.src = "img/rotate_right.png";
$(rotateRight).on("click", function() {
var id = this.id.replace("rotate_right_link_", "");
var printer = getPrinterByModuleId(id);
printer.printerModule.rotateSnapshotRight90Deg();
// Get the rotation angle stored in the snapshot img tag
// var image = document.getElementById("snapshot_" + id);
// angle = Number(image.getAttribute("data-rotation-angle"));
// Update the server database
// sendRotationUpdateToServer(id, angle);
});
// Structure them
rotateLeft.appendChild(rotateLeftIcon);
rotateRight.appendChild(rotateRightIcon);
overlayContent.appendChild(rotateLeft);
overlayContent.appendChild(rotateRight);
settingsOverlay.appendChild(overlayContent);
return settingsOverlay;
};
printerModule.prototype.rotateSnapshotLeft90Deg = function() {
// Get the rotation angle stored in the snapshot img tag
var image = document.getElementById("snapshot_" + this.id);
var oldAngle = Number(image.getAttribute("data-rotation-angle"));
var newAngle = (oldAngle - 90) % 360;
image.setAttribute("data-rotation-angle", newAngle); /* Store the rotation */
// Perform the rotation
image.style.webkitTransform = "rotate("+newAngle+"deg) scale("+snapshotHeightToWidthRatio+")";
};
printerModule.prototype.rotateSnapshotRight90Deg = function() {
// Get the rotation angle stored in the snapshot img tag
var image = document.getElementById("snapshot_" + this.id);
var oldAngle = Number(image.getAttribute("data-rotation-angle"));
var newAngle = (oldAngle + 90) % 360;
image.setAttribute("data-rotation-angle", newAngle); /* Store the rotation */
// Perform the rotation
image.style.webkitTransform = "rotate("+newAngle+"deg) scale("+snapshotHeightToWidthRatio+")";
};
printerModule.prototype.flipSnapshotHorizontally = function() {
var image = document.getElementById("snapshot_" + this.id);
// Toggle the flip state
var flipped = image.getAttribute("data-horizontal-flip");
if(flipped == "True"){
image.setAttribute("data-horizontal-flip", "False");
}
else {
image.setAttribute("data-horizontal-flip", "True");
}
// Perform the flip
image.style.webkitTransform = "rotateY(180deg)";
};
printerModule.prototype.flipSnapshotVertically = function() {
var image = document.getElementById("snapshot_" + this.id);
// Toggle the flip state
var flipped = image.getAttribute("data-vertical-flip");
if(flipped == "True"){
image.setAttribute("data-vertical-flip", "False");
}
else {
image.setAttribute("data-vertical-flip", "True");
}
// Perform the flip
image.style.webkitTransform = "rotateX(180deg)";
};
function sendRotationUpdateToServer(printerId, angle) {
var message = { "message_type":"rotation_update",
"message": angle,
"printer_id": printerId };
message = JSON.stringify(message);
socket.send(message);
}