diff --git a/README.md b/README.md index 32a7391..c365b39 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +# Important note regarding the maintenance of this project. + +I no longer have regular access to a 3D printer to be able to test changes to this plugin. If you're looking for a plugin +which is maintained regularly, you should check out: https://github.com/malnvenshorn/OctoPrint-CostEstimation + # OctoPrint-Cost This plugin will display a cost estimate based on the filament length or weight and time to print, like this: diff --git a/octoprint_cost/static/js/cost.js b/octoprint_cost/static/js/cost.js index 51a37c2..f86a7f0 100644 --- a/octoprint_cost/static/js/cost.js +++ b/octoprint_cost/static/js/cost.js @@ -12,45 +12,44 @@ $(function() { var self = this; // There must be a nicer way of doing this. - + settingsState.check_cost = ko.observable(true); settingsState.costPerWeight = ko.pureComputed(function() { - var currency = settingsState.settings.plugins.cost.currency(); - var weight = settingsState.settings.plugins.cost.weight(); - return currency + '/' + weight; + var currency = settingsState.settings.plugins.cost.currency(); + var weight = settingsState.settings.plugins.cost.weight(); + return currency + '/' + weight; }); settingsState.costPerLength = ko.pureComputed(function() { - var currency = settingsState.settings.plugins.cost.currency(); - var length = settingsState.settings.plugins.cost.length(); - return currency + '/' + length; + var currency = settingsState.settings.plugins.cost.currency(); + var length = settingsState.settings.plugins.cost.length(); + return currency + '/' + length; }); settingsState.costPerTime = ko.pureComputed(function() { - var currency = settingsState.settings.plugins.cost.currency(); - var time = settingsState.settings.plugins.cost.time(); - return currency + '/' + time; + var currency = settingsState.settings.plugins.cost.currency(); + var time = settingsState.settings.plugins.cost.time(); + return currency + '/' + time; }); - + printerState.costString = ko.pureComputed(function() { if (settingsState.settings === undefined) return '-'; if (printerState.filament().length == 0) return '-'; var currency = settingsState.settings.plugins.cost.currency(); - var cost_per_length = settingsState.settings.plugins.cost.cost_per_length(); - var cost_per_weight = settingsState.settings.plugins.cost.cost_per_weight(); - var density_of_filament = settingsState.settings.plugins.cost.density_of_filament(); + var cost_per_length = settingsState.settings.plugins.cost.cost_per_length(); + var cost_per_weight = settingsState.settings.plugins.cost.cost_per_weight(); + var density_of_filament = settingsState.settings.plugins.cost.density_of_filament(); var cost_per_time = settingsState.settings.plugins.cost.cost_per_time(); var filament_used_length = printerState.filament()[0].data().length / 1000; - var filament_used_volume = printerState.filament()[0].data().volume / 1000; - var expected_time = printerState.estimatedPrintTime() / 3600; + var filament_used_volume = printerState.filament()[0].data().volume / 1000; + var expected_time = (printerState.printTime() + printerState.printTimeLeft()) / 3600; - if (settingsState.check_cost()) { - var totalCost = cost_per_weight * filament_used_volume * density_of_filament + expected_time * cost_per_time; - } - else { - var totalCost = cost_per_length * filament_used_length + expected_time * cost_per_time; - } + if (settingsState.check_cost()) { + var totalCost = cost_per_weight * filament_used_volume * density_of_filament + expected_time * cost_per_time; + } else { + var totalCost = cost_per_length * filament_used_length + expected_time * cost_per_time; + } return '' + currency + totalCost.toFixed(2); }); @@ -63,23 +62,32 @@ $(function() { var gcode = data.gcodeAnalysis; if (gcode.hasOwnProperty('filament') && gcode.filament.hasOwnProperty('tool0') && gcode.hasOwnProperty('estimatedPrintTime')) { var currency = settingsState.settings.plugins.cost.currency(); - var cost_per_length = settingsState.settings.plugins.cost.cost_per_length(); + var cost_per_length = settingsState.settings.plugins.cost.cost_per_length(); var cost_per_weight = settingsState.settings.plugins.cost.cost_per_weight(); - var density_of_filament = settingsState.settings.plugins.cost.density_of_filament(); + var density_of_filament = settingsState.settings.plugins.cost.density_of_filament(); var cost_per_time = settingsState.settings.plugins.cost.cost_per_time(); - var filament_used_length = gcode.filament.tool0.length / 1000; - var filament_used_volume = gcode.filament.tool0.volume / 1000; - var expected_time = gcode.estimatedPrintTime / 3600; + var filament_used_volume = gcode.filament.tool0.volume / 1000; + + // Use last print time instead of estimation for prints that are already printed at least once + if (data["prints"] && data["prints"]["last"] && data["prints"]["last"]["printTime"]) { + var expected_time = data["prints"]["last"]["printTime"] / 3600; + } else { + var expected_time = gcode.estimatedPrintTime / 3600; + } - if (settingsState.check_cost()) { - var totalCost = cost_per_weight * filament_used_volume * density_of_filament + expected_time * cost_per_time; - } - else { - var totalCost = cost_per_length * filament_used_length + expected_time * cost_per_time; - } + if (settingsState.check_cost()) { + var totalCost = cost_per_weight * filament_used_volume * density_of_filament + expected_time * cost_per_time; + } else { + var totalCost = cost_per_length * filament_used_length + expected_time * cost_per_time; + } - output += gettext("Cost") + ": " + currency + totalCost.toFixed(2); + // Build different output string for first time prints (the octoprint frontend adds a new line or not depending on this. A bug?) + if (data["prints"] && data["prints"]["last"] && data["prints"]["last"]["printTime"]) { + output += "
" + gettext("Cost") + ": " + currency + totalCost.toFixed(2); + } else { + output += gettext("Cost") + ": " + currency + totalCost.toFixed(2); + } } } diff --git a/octoprint_cost/templates/cost_settings.jinja2 b/octoprint_cost/templates/cost_settings.jinja2 index 8f534e4..7c27a10 100644 --- a/octoprint_cost/templates/cost_settings.jinja2 +++ b/octoprint_cost/templates/cost_settings.jinja2 @@ -23,10 +23,10 @@ -
+