2014-02-10 14:54:07 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Doodle3D project (http://doodle3d.com).
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 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.
|
|
|
|
*/
|
2015-04-25 17:26:12 +02:00
|
|
|
|
2014-02-10 14:54:07 +01:00
|
|
|
function PrinterAPI() {
|
2015-04-27 23:38:01 +02:00
|
|
|
var className = 'PrinterAPI';
|
2015-04-25 17:26:12 +02:00
|
|
|
|
|
|
|
this.remainingLines = [];
|
|
|
|
this.totalLinesAtStart = 0;
|
|
|
|
|
2015-04-27 23:38:01 +02:00
|
|
|
this.init = function() {
|
|
|
|
console.log(className,'init is deprecated');
|
|
|
|
}
|
|
|
|
|
2015-04-25 17:26:12 +02:00
|
|
|
this.state = function(success,fail) {
|
2015-06-02 14:38:55 +02:00
|
|
|
API.get('printer/state',{},success,fail);
|
2015-04-25 17:26:12 +02:00
|
|
|
};
|
2014-02-10 14:54:07 +01:00
|
|
|
|
2015-04-25 17:26:12 +02:00
|
|
|
this.listAll = function(success,fail) {
|
2015-06-02 14:38:55 +02:00
|
|
|
API.get('printer/listall',{},success,fail);
|
2015-04-25 17:26:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.temperature = function(success,fail) {
|
2015-06-02 14:38:55 +02:00
|
|
|
API.get('printer/temperature',{},success,fail);
|
2015-04-25 17:26:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.progress = function(success,fail) {
|
2015-06-02 14:38:55 +02:00
|
|
|
API.get('printer/progress',{},success,fail);
|
2014-02-10 14:54:07 +01:00
|
|
|
}
|
2015-04-25 17:26:12 +02:00
|
|
|
|
|
|
|
function _printPartPost(lines,data,cb) {
|
|
|
|
|
|
|
|
API.post('printer/print',data,function(response) {
|
|
|
|
console.log('print part success',response);
|
|
|
|
setTimeout(function() {
|
|
|
|
_printPart(lines,false,false,cb);
|
|
|
|
},10);
|
|
|
|
|
|
|
|
},function(jqXHR,textStatus) {
|
|
|
|
console.log('print fail jqHXR:',jqXHR,"textStatus:",textStatus);
|
|
|
|
if (textStatus=="timeout") {
|
|
|
|
console.log('TIMEOUT, waiting to try again');
|
|
|
|
setTimeout(function() {
|
|
|
|
console.log('now try again');
|
|
|
|
_printPartPost(lines,data,cb);
|
|
|
|
},5000);
|
|
|
|
} else {
|
|
|
|
console.log("_printPartPost FATAL error:",textStatus);
|
2014-02-10 14:54:07 +01:00
|
|
|
}
|
2015-04-25 17:26:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function _printPart(lines,first,start,cb) {
|
|
|
|
var chunk = lines.splice(0,500);
|
|
|
|
console.log('printPart',chunk.length,lines.length);
|
|
|
|
|
|
|
|
if (chunk.length>0) {
|
|
|
|
var data = {gcode: chunk.join("\n"), first: first, start: start};
|
|
|
|
|
|
|
|
_printPartPost(lines,data,function() {
|
2015-04-27 23:38:01 +02:00
|
|
|
// console.log('_printPartPost cb');
|
|
|
|
// cb(); //??? needed
|
2015-04-25 17:26:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
console.log('no more print parts');
|
|
|
|
cb(); //finished
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.print = function(gcode,start,first,success,fail) {
|
|
|
|
//need a check here for state??
|
|
|
|
this.remainingLines = gcode.split("\n");
|
|
|
|
this.totalLinesAtStart = this.remainingLines.length;
|
|
|
|
// console.log('remainingLines.length',this.remainingLines.length);
|
|
|
|
_printPart(this.remainingLines,true,true,function() {
|
|
|
|
console.log('done sending');
|
2014-02-10 14:54:07 +01:00
|
|
|
});
|
|
|
|
};
|
2015-04-25 17:26:12 +02:00
|
|
|
|
|
|
|
this.stop = function(endcode,success,fail) {
|
|
|
|
//need a check here for state??
|
|
|
|
// console.log('remainingLines',this.remainingLines.length);
|
|
|
|
this.remainingLines.length = 0; //clear array
|
|
|
|
totalLinesAtStart = 0;
|
|
|
|
API.post('printer/stop',{gcode:endcode},success,fail);
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:54:07 +01:00
|
|
|
}
|