moved printer logic to doodle3dapi.printer

solves https://github.com/Doodle3D/Doodle3D-API/issues/6
solves https://github.com/Doodle3D/Doodle3D-API/issues/7
This commit is contained in:
casperlamboo 2015-07-24 17:37:01 +02:00
parent f3cadff7d5
commit 3e508cb862
8 changed files with 38221 additions and 755955 deletions

View File

@ -20,6 +20,7 @@ System.config({
"casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master",
"core-js": "npm:core-js@0.9.18",
"github/fetch": "github:github/fetch@0.9.0",
"jquery": "github:components/jquery@2.1.4",
"traceur": "github:jmcriffey/bower-traceur@0.0.90",
"traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.90",
"github:jspm/nodelibs-process@0.1.1": {

View File

@ -39,10 +39,16 @@ doodle3DManager.addEventListener('boxappeared', (event) => {
box.addEventListener('connect', (event) => {
row.style.color = 'black';
console.log('connect');
box.printer.sendGCode("G1 X100 X100");
});
box.addEventListener('disconnect', (event) => {
row.style.color = 'gray';
console.log('disconnect');
});
box.addEventListener('update', (event) => {
@ -57,7 +63,6 @@ doodle3DManager.addEventListener('boxappeared', (event) => {
hasControl.innerHTML = status.has_control;
hotend.innerHTML = status.hotend;
hotendTarget.innerHTML = status.hotend_target;
state.innerHTML = status.state;
totalLines.innerHTML = status.total_lines;
}
else {
@ -68,7 +73,6 @@ doodle3DManager.addEventListener('boxappeared', (event) => {
hasControl.innerHTML = '';
hotend.innerHTML = '';
hotendTarget.innerHTML = '';
state.innerHTML = '';
totalLines.innerHTML = '';
}
});

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,8 @@
},
"dependencies": {
"casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master",
"github/fetch": "github:github/fetch@^0.9.0"
"github/fetch": "github:github/fetch@^0.9.0",
"jquery": "github:components/jquery@^2.1.4"
},
"devDependencies": {
"babel": "npm:babel-core@^5.1.13",

View File

@ -25,19 +25,30 @@ export default class extends EventDispatcher {
this.update = new UpdateAPI(this.api);
this.alive = false;
this.maxBatchSize = 10*1024;
this.maxBufferedLines = 1024*1024;
this.autoUpdate = false;
this.state = {};
this._printBatches = [];
this._currentBatch = 0;
this.loaded = false;
}
setAutoUpdate (autoUpdate) {
setAutoUpdate (autoUpdate = true, updateInterval = 1000) {
this.updateInterval = updateInterval;
if (this.autoUpdate === autoUpdate) {
return;
}
this.autoUpdate = autoUpdate;
if (autoUpdate) {
this._initLoop();
}
return this;
}
_initLoop () {
this.network.alive().then(() => {
this.alive = true;
@ -46,14 +57,12 @@ export default class extends EventDispatcher {
type: 'connect'
});
if (!this.loaded) {
this.loaded = true;
}
this._updateState();
this._updateStateLoop();
}).catch((error) => {
if (this.alive) {
this.alive = false;
this.dispatchEvent({
@ -62,60 +71,14 @@ export default class extends EventDispatcher {
}
setTimeout(() => {
this.setAutoUpdate();
}, 1000);
this._initLoop();
}, this.updateInterval);
});
return this;
}
print (gcode) {
this._currentBatch = 0;
var lastIndex = 0;
while (lastIndex !== (gcode.length - 1)) {
var index = gcode.lastIndexOf('\n', lastIndex + maxBatchSize);
var batch = gcode.substring(lastIndex, index);
lastIndex = index;
this.printBatches.push(batch);
}
return this;
}
stopPrint (endCode = '') {
this._printBatches = [];
this._currentBatch = 0;
this.printer.stop(endCode).then((data) => {
console.log('Printer stop command sent');
});
return this;
}
_updateLoop () {
if (this._printBatches.length > 0 && (this.state['buffered_lines'] + this._printBatches[0].length) <= this.maxBufferedLines) {
//if (this._printBatches.length > 0 ) {
this._printBatch();
}
else {
setTimeout(() => {
this._updateState();
}, 1000);
}
}
_updateState () {
//que api calls so they don't overload the d3d box
_updateStateLoop () {
this.info.status().then((state) => {
this.state = state;
this.dispatchEvent({
@ -123,38 +86,18 @@ export default class extends EventDispatcher {
state
});
this._updateLoop();
}).catch((error) => {
console.warn(error);
this.setAutoUpdate();
});
}
_printBatch () {
var gcode = this._printBatches.shift();
var start = (this._currentBatch === 0) ? true : false;
var first = (this._currentBatch === 0) ? true : false;
var last = (this._printBatches.length === 0) ? true : false; //only for the node js server
this.printer.print(gcode, start, first, last).then((data) => {
console.log('batch sent: ' + this._currentBatch, data);
if (this._printBatches.length > 0) {
this._currentBatch ++;
}
else {
console.log('Finish sending gcode to printer');
if (this.autoUpdate) {
setTimeout(() => {
this._updateStateLoop();
}, this.updateInterval);
}
this._updateState();
}).catch((error) => {
console.warn(error);
this._printBatches.unshift(gcode);
this.setAutoUpdate();
if (this.autoUpdate) {
setTimeout(() => {
this._initLoop();
}, this.updateInterval);
}
});
}
}

View File

@ -14,6 +14,10 @@ export default class extends EventDispatcher {
if (autoUpdate) {
this._checkNew();
if (this.interval !== undefined) {
clearInterval(this.interval);
}
this.interval = setInterval(() => {
this._checkNew();
}, rate);
@ -39,19 +43,15 @@ export default class extends EventDispatcher {
_checkNew () {
rest.get(this.api + 'list.php').then((boxes) => {
var knownIPs = [];
for (var i = 0; i < this.boxes.length; i ++) {
var boxData = this.boxes[i].boxData;
knownIPs.push(boxData.localip);
}
for (var i = 0; i < boxes.length; i ++) {
var boxData = boxes[i];
var knownIPs = this.boxes.map((box) => box.boxData.localip);
for (var boxData of boxes) {
if (knownIPs.indexOf(boxData.localip) === -1) {
this.addBox(boxData);
}
}
});
}
}

View File

@ -3,6 +3,12 @@ import * as rest from './restapi.js';
export default class {
constructor (api) {
this.api = api;
this._printBatches = [];
this._currentBatch = 0;
this.maxBatchSize = 10*1024;
this.maxBufferedLines = 1024*1024; //yet to be implimented
}
temperature () {
@ -25,6 +31,51 @@ export default class {
return rest.post(this.api + 'printer/heatup', {});
}
sendGCode (gcode) {
if (!gcode.endsWith('\n')) {
gcode += '\n';
}
this._currentBatch = 0;
var lastIndex = 0;
while (lastIndex !== (gcode.length - 1)) {
var index = gcode.lastIndexOf('\n', lastIndex + this.maxBatchSize);
var batch = gcode.substring(lastIndex, index);
lastIndex = index;
this._printBatches.push(batch);
}
this._sendBatch();
}
_sendBatch () {
var gcode = this._printBatches.shift();
var start = (this._currentBatch === 0) ? true : false;
var first = (this._currentBatch === 0) ? true : false;
var last = (this._printBatches.length === 0) ? true : false; //only for the node js server
this.print(gcode, start, first, last).then((data) => {
console.log('batch sent: ' + this._currentBatch, data);
if (this._printBatches.length > 0) {
this._currentBatch ++;
_sendBatch();
}
else {
console.log('Finish sending gcode to printer');
}
}).catch((error) => {
this._printBatches.unshift(gcode);
setTimeout(() => {
this._sendBatch();
}, 1000);
});
}
print (gcode = '', first = false, start = false, last = false) {
var data = {gcode, first, start}
if (last) data.last = last;
@ -33,6 +84,9 @@ export default class {
}
stop (gcode = '') {
this._currentBatch = 0;
this._printBatches = [];
return rest.post(this.api + 'printer/stop', {gcode});
}
}

View File

@ -1,3 +1,52 @@
import $ from 'jquery';
export function get (url) {
return new Promise((resolve, reject) => {
$.ajax({
url: url,
dataType: 'json',
timeout: 5000,
success: (response) => {
if (response.status === 'success') {
resolve(response.data);
}
else {
reject(response.msg);
}
}
}).fail(reject);
});
}
export function post (url, data) {
return new Promise((resolve, reject) => {
$.ajax({
url: url,
type: 'POST',
data: data,
dataType: 'json',
timeout: 10000,
success: (response) => {
if (response.status === 'success') {
resolve(response.data);
}
else {
reject(response.msg);
}
}
}).fail(reject);
});
}
/*
import 'github/fetch';
export function get (url) {
@ -18,6 +67,7 @@ export function get (url) {
}
}).catch(reject);
});
}
@ -27,9 +77,9 @@ export function post (url, data) {
fetch(url, {
method: 'post',
enctype: 'x-www-form-urlencoded',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
'Accept': 'application/json'
},
body: JSON.stringify(data)
}).then((response) => {
@ -47,4 +97,5 @@ export function post (url, data) {
}).catch(reject);
});
}
}
*/