0
0
mirror of https://github.com/Doodle3D/doodle3d-connect.git synced 2024-11-05 07:03:24 +01:00
doodle3d-connect/js/PrintPage.js
2017-07-11 17:17:06 +02:00

211 lines
6.0 KiB
JavaScript

/*
* This file is part of the Doodle3D project (http://doodle3d.com).
*
* Copyright (c) 2013-2017, Doodle3D
* This software is licensed under the terms of the GNU GPL v2 or later.
* See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
*/
(function (w) {
var _serverAPI = new ServerAPI();
var _connectAPI = new ConnectAPI();
var _infoAPI = new InfoAPI();
var _networkAPI = new NetworkAPI();
var _printerAPI = new PrinterAPI();
var _configAPI = new ConfigAPI();
var PAGE_ID = "#print";
var _pageData = {};
var _self = this;
var _wifiboxSettings;
var _slicerSettings;
$.mobile.document.on("pagebeforeshow", PAGE_ID, function( event, data ) {
_pageData = d3d.util.getPageParams(PAGE_ID);
if(_pageData === undefined) {
console.log("ERROR",PAGE_ID,"_pageData undefined");
$.mobile.changePage("#boxes");
return;
}
var boxURL = "http://"+_pageData.localip;
//disabled by default
$("#btnPrint").button().on("click", print);
$("#btnPrint").button('disable');
loadGCodeInfoFromServer(d3d.pageParams.uuid);
_connectAPI.list(function(successData) {
console.log("_connectAPI.list success",successData);
$("#lstBoxes").empty();
$("#lstBoxes").append($("<option/>"));
for (var i in successData) {
var box = successData[i];
var selected = (box.localip===_pageData.localip) ? "selected " : "";
$("#lstBoxes").append($("<option "+selected+" value="+box.localip+">"+box.wifiboxid+"</option>"));
}
$("#lstBoxes").append($("<option value='other'>Other...</option>"));
$("#lstBoxes").selectmenu("refresh", true);
if (_pageData.localip) {
onSelectWiFiBox(_pageData.localip);
}
}, function(failData) {
console.log("_connectAPI.list failData",failData);
});
$("#lstBoxes").on("change", function(data) {
var ip = $(this).val();
console.log("lstBoxes change",ip);
onSelectWiFiBox(ip);
});
});
$.mobile.document.on( "pagebeforehide", PAGE_ID, function( event, data ) {
_connectAPI.stop();
});
function onSelectWiFiBox(ip) {
$("#infoWiFiBox").text("");
if (!ip) {
$("#btnPrint").button('disable');
return;
}
else if (ip==="other") {
// redirect
$.mobile.changePage("#boxes");
} else {
var boxURL = "http://"+ip;
_infoAPI.init(boxURL);
_networkAPI.init(boxURL);
_printerAPI.init(boxURL);
_configAPI.init(boxURL);
_networkAPI.status(function(successData) {
console.log("network status",successData);
// $("#lstPrint li.boxItem p").text(
var netInfo = successData.statusMessage + " (" + successData.ssid + " @ " + successData.localip + ")";
_infoAPI.getStatus(function(successData) {
console.log(successData);
var state = successData.state;
if (state==="idle") {
state="ready";
$("#btnPrint").button('enable');
}
var info = netInfo + " - Printer status: ";
info += "<span class='"+state+"'>"+state+"</span>";
$("#infoWiFiBox").html(info);
}, function(failData) {
console.log(failData);
$("#infoWiFiBox").html("<span class='error'>failed to retrieve <em>printer status</em> from WiFi-Box</span>");
});
}, function(failData) {
console.log("_networkAPI status failed",failData);
$("#infoWiFiBox").html("<span class='error'>failed to retrieve <em>network status</em> from WiFi-Box</span>");
});
}
}
function print() {
_configAPI.loadAll(function(successData) {
_wifiboxSettings = successData;
var slicerPrinterType = _slicerSettings.type;
var wifiboxPrinterType = _wifiboxSettings["printer.type"];
if (slicerPrinterType!==wifiboxPrinterType) {
var override = window.confirm("The GCODE file was sliced for '"+slicerPrinterType+"'.\n"+
"Your WiFi-Box is currently configured for '"+wifiboxPrinterType+"'\n\n"+
"Do you want to override the settings on your WiFi-Box with the new settings from the slicer?");
if (override) {
//
_configAPI.savePrinterType(slicerPrinterType,function(successData) {
console.log(successData);
window.alert(successData.validation["printer.type"] + " (printer.type)");
},function(failData) {
window.alert("Could not save printer type '",slicerPrinterType,"')' to WiFi-Box");
});
}
}
});
if (true) {
return;
}
//var startcode = _configAPI.subsituteVariables(_wifiboxSettings["printer.startcode"],_wifiboxSettings);
//var endcode = _configAPI.subsituteVariables(_wifiboxSettings["printer.endcode"],_wifiboxSettings);
var data = {
"id": d3d.pageParams.uuid,
// "start_code": startcode,
// "end_code": endcode
};
//console.log("fetchPrint",d3d.pageParams.uuid,data);
_printerAPI.fetch(data,function(successData) {
console.log("fetchPrint success",successData);
var url = d3d.util.replaceURLParameters("#control",_pageData);
$.mobile.changePage(url);
},function(failData) {
console.log("fetchPrint fail",failData);
window.alert("Problem: " + failData.msg);
});
}
function clearInfo() {
$("#infoFile").text("...");
$("#infoPrinter").text("...");
$("#infoMaterial").html("...");
$("#iconPrinter").attr('src','img/icons/blank.png');
}
function loadGCodeInfoFromServer(uuid) {
_serverAPI.init("https://gcodeserver.doodle3d.com");
_serverAPI.getInfo(uuid, function(successData) {
console.log("getInfo success",successData);
var filesize = d3d.util.formatBytes(successData["bytes"]);
_serverAPI.fetchHeader(d3d.pageParams.uuid,function(successData) {
console.log("_serverAPI fetchHeader success",successData);
var header = successData;
_slicerSettings = header; //copy header json data into _slicerSettings
var printerId = header.type;
$("#infoFile").text(header.name + " (" + filesize + ")");
$("#infoPrinter").text(printerId);
$("#infoMaterial").html(header.filamentThickness + "mm @ " + header.temperature + "&deg;C");
$("#iconPrinter").attr('src','img/icons/printers/'+printerId+'.png');
}, function(failData) {
console.log("_serverAPI fetchHeader fail",failData);
clearInfo();
});
},function(failData) {
clearInfo();
});
}
})(window);