From 3bacee2072946689fa04db547534661871b512e4 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Thu, 11 Jun 2015 11:08:56 +0200 Subject: [PATCH] improved index page index page now checks if doodle boxes are alive before displaying them --- doodle.html | 149 -------------------------------------------- index.html | 91 +++++++++++++-------------- src/box.js | 14 +++-- src/utils.js | 46 +++++--------- webworker/worker.js | 2 +- webworker_test.html | 26 +++++--- 6 files changed, 89 insertions(+), 239 deletions(-) delete mode 100644 doodle.html diff --git a/doodle.html b/doodle.html deleted file mode 100644 index 0f5f4e4..0000000 --- a/doodle.html +++ /dev/null @@ -1,149 +0,0 @@ - - - -Doedel Drie Dee - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/index.html b/index.html index 9d1b3a8..d0fda5b 100644 --- a/index.html +++ b/index.html @@ -3,10 +3,12 @@ Doedel Drie Dee - - + + @@ -18,66 +20,61 @@ var printers = []; $(document).ready(function () { "use strict"; - var listDoodle = $("#printers-doodle"); + var known = []; + var listSliceTest = $("#printers-slicetest"); var listSliceWebworker = $("#printers-webworker"); - listDoodle.append("
  • Wired Printer
  • "); - listSliceTest.append("
  • Wired Printer
  • "); - listSliceWebworker.append("
  • Wired Printer
  • "); + function addPrinter (name, localip) { + known.push(localip); - listDoodle.append("
  • Node Server
  • "); - listSliceTest.append("
  • Node Server
  • "); - listSliceWebworker.append("
  • Node Server
  • "); + var slice = $(""); + var webworker = $(""); + + listSliceTest.append(slice); + listSliceWebworker.append(webworker); - /* - printers.push({ - name: "wired box", - d3dbox: new D3D.Box("192.168.5.1") - }); - */ - - getAPI(api + "list.php", function (boxes) { - for (var i = 0; i < boxes.length; i ++) { - var box = boxes[i]; + var box = new D3D.Box(localip).init(); + box.onconnect = function () { + slice.removeClass('hidden'); + webworker.removeClass('hidden'); + }; + box.ondisconnect = function () { + slice.addClass('hidden'); + webworker.addClass('hidden'); + }; + } - /* - printers.push({ - name: box.wifiboxid, - d3dbox: new D3D.Box(box.localip) - }); - */ + function checkNewBoxes () { + getAPI(api + "list.php", function (error, boxes) { + if (error) { + console.warn('It appears that you are not connected to the internet, or the Doodle3D servers are down'); + return; + } - listDoodle.append("
  • " + box.wifiboxid + "
  • "); - listSliceTest.append("
  • " + box.wifiboxid + "
  • "); - listSliceWebworker.append("
  • " + box.wifiboxid + "
  • "); - } - }); + for (var i = 0; i < boxes.length; i ++) { + var box = boxes[i]; + if (known.indexOf(box.localip) === -1) { + + addPrinter(box.wifiboxid, box.localip); + } + } + }); + } + + addPrinter('Wired Printer', '192.168.5.1'); + addPrinter('Node Server', window.location.host + ":3000"); + + setInterval(checkNewBoxes, 10000); + checkNewBoxes(); }); -

    Doodle

    - -

    Slice Test

    Webworkers

    - \ No newline at end of file diff --git a/src/box.js b/src/box.js index e53919f..4a082f5 100644 --- a/src/box.js +++ b/src/box.js @@ -1,6 +1,6 @@ /****************************************************** * -* Box +* WiFi-Box * Representation of de Doodle3DBox * Handles all communication with the doodle box * JavaScript shell for api communication @@ -31,8 +31,6 @@ D3D.Box = function (localIp) { this.currentBatch = 0; this.loaded = false; - - this.init(); }; D3D.Box.prototype.init = function () { "use strict"; @@ -40,7 +38,12 @@ D3D.Box.prototype.init = function () { this.getNetworkAlive(function (error, data) { if (error) { - scope.alive = false; + if (scope.alive) { + scope.alive = false; + if (scope.ondisconnect !== undefined) { + scope.ondisconnect(); + } + } console.warn(error); setTimeout(function () { @@ -51,6 +54,9 @@ D3D.Box.prototype.init = function () { } scope.alive = true; + if (scope.onconnect !== undefined) { + scope.onconnect(); + } scope.getConfigAll(function (error, data) { if (error) { diff --git a/src/utils.js b/src/utils.js index f6d4a55..90ca9a7 100644 --- a/src/utils.js +++ b/src/utils.js @@ -6,24 +6,24 @@ ******************************************************/ var D3D = { - "version": "0.1", - "website": "http://www.doodle3d.com/", - "contact": "develop@doodle3d.com" + 'version': '0.1', + 'website': 'http://www.doodle3d.com/', + 'contact': 'develop@doodle3d.com' }; function sendAPI (url, data, callback) { - "use strict"; + 'use strict'; $.ajax({ url: url, - type: "POST", + type: 'POST', data: data, - dataType: "json", + dataType: 'json', timeout: 10000, success: function (response) { - if (response.status === "success") { + if (response.status === 'success') { if (callback !== undefined) { - callback(null, response.data); + callback(null, response); } } else { @@ -31,19 +31,19 @@ function sendAPI (url, data, callback) { } } }).fail(function () { - callback("Failed connecting to " + url); + callback('Failed connecting to ' + url); }); } function getAPI (url, callback) { - "use strict"; + 'use strict'; $.ajax({ url: url, - dataType: "json", + dataType: 'json', timeout: 5000, success: function (response) { - if (response.status === "success") { + if (response.status === 'success') { if (callback !== undefined) { callback(null, response.data); } @@ -53,37 +53,23 @@ function getAPI (url, callback) { } } }).fail(function () { - callback("Failed connecting to " + url); + callback('Failed connecting to ' + url); }); } -function loadSettings (url, callback) { - "use strict"; - - $.ajax({ - url: url, - dataType: "json", - success: function (response) { - if (callback !== undefined) { - callback(response); - } - } - }); -} - function downloadFile (file, data) { - "use strict"; + 'use strict'; var blob = new Blob([data], {type:'text/plain'}); - var button = document.createElement("a"); + var button = document.createElement('a'); button.download = file; button.href = window.URL.createObjectURL(blob); button.click(); } Array.prototype.clone = function () { - "use strict"; + 'use strict'; var array = []; for (var i = 0; i < this.length; i ++) { diff --git a/webworker/worker.js b/webworker/worker.js index 798d40d..68c0571 100644 --- a/webworker/worker.js +++ b/webworker/worker.js @@ -42,7 +42,7 @@ self.addEventListener("message", function (event) { case "SLICE": var gcode = slicer.getGCode(printer); - var blob = new Blob([gcode], {type: 'text/html'}); + var blob = new Blob([gcode], {type: 'text/plain'}); self.postMessage({ "cmd": "GCODE", diff --git a/webworker_test.html b/webworker_test.html index 41c65d1..e8eab79 100644 --- a/webworker_test.html +++ b/webworker_test.html @@ -47,8 +47,7 @@ function init () { var scene = createScene(); var localIp = location.hash.substring(1); - doodleBox = new D3D.Box(localIp); - + doodleBox = new D3D.Box(localIp).init(); doodleBox.onupdate = function (data) { document.getElementById('state').innerHTML = data.state; document.getElementById('bed_temp').innerHTML = data.bed; @@ -115,6 +114,8 @@ function init () { } function createScene () { + 'use strict'; + var scene = new THREE.Scene(); var renderer = new THREE.WebGLRenderer({canvas: document.getElementById('3d-preview'), antialias: true}); @@ -192,13 +193,22 @@ function createScene () { } } - loadSettings('settings/user_settings.json', function (data) { - USER_SETTINGS = data; - loaded(); + $.ajax({ + url: 'settings/user_settings.json', + dataType: 'json', + success: function (response) { + USER_SETTINGS = response; + loaded(); + } }); - loadSettings('settings/printer_settings.json', function (data) { - PRINTER_SETTINGS = data; - loaded(); + + $.ajax({ + url: 'settings/printer_settings.json', + dataType: 'json', + success: function (response) { + PRINTER_SETTINGS = response; + loaded(); + } }); })();