diff --git a/README.md b/README.md index 1a7c9de..32a7391 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # OctoPrint-Cost -This plugin will display a cost estimate based on the filament length and time to print, like this: +This plugin will display a cost estimate based on the filament length or weight and time to print, like this: ![screenshot](doc/screenshot.png) @@ -16,8 +16,9 @@ or manually using this URL: There are three configuration variables: * Currency - a free-form string. -* Cost per hour - cost in currency units per one hour of use. -* Cost per meter - cost in currency units per one meter of filament. +* Cost per time - cost in currency units per one hour of use. +* Cost per length - cost in currency units per one meter of filament. +* Cost per weight - cost in currency units per one kilogram of filament. ## License (MIT) diff --git a/octoprint_cost/__init__.py b/octoprint_cost/__init__.py index 1c575fa..2a49ed5 100644 --- a/octoprint_cost/__init__.py +++ b/octoprint_cost/__init__.py @@ -17,9 +17,14 @@ class CostPlugin(octoprint.plugin.SettingsPlugin, def get_settings_defaults(self): return dict( - cost_per_hour=1.50, - cost_per_meter=0.5, - currency='£' + currency="€", + weight="kg", + length="m", + time="h", + cost_per_time=1.50, + cost_per_length=0.08, + cost_per_weight=25, + density_of_filament=1.25 ) def get_template_configs(self): @@ -29,8 +34,10 @@ def get_template_configs(self): def get_template_vars(self): return dict( - cost_per_hour=self._settings.get(["cost_per_hour"]), - cost_per_meter=self._settings.get(["cost_per_meter"]), + cost_per_time=self._settings.get(["cost_per_time"]), + cost_per_length=self._settings.get(["cost_per_length"]), + cost_per_weight=self._settings.get(["cost_per_weight"]), + density_of_filament=self._settings.get(["density_of_filament"]), currency=self._settings.get(["currency"]) ) diff --git a/octoprint_cost/static/js/cost.js b/octoprint_cost/static/js/cost.js index 5733e7e..51a37c2 100644 --- a/octoprint_cost/static/js/cost.js +++ b/octoprint_cost/static/js/cost.js @@ -12,19 +12,46 @@ $(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; + }); + settingsState.costPerLength = ko.pureComputed(function() { + 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; + }); + 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_meter = settingsState.settings.plugins.cost.cost_per_meter(); - var cost_per_hour = settingsState.settings.plugins.cost.cost_per_hour(); + 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_meters = printerState.filament()[0].data().length / 1000; - var expected_time_hours = printerState.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; + } - var totalCost = cost_per_meter * filament_used_meters + expected_time_hours * cost_per_hour; - return '' + currency + totalCost.toFixed(2); }); @@ -36,21 +63,29 @@ $(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_meter = settingsState.settings.plugins.cost.cost_per_meter(); - var cost_per_hour = settingsState.settings.plugins.cost.cost_per_hour(); + 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_meters = gcode.filament.tool0.length / 1000; - var expected_time_hours = gcode.estimatedPrintTime / 3600; + 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 totalCost = cost_per_meter * filament_used_meters + expected_time_hours * cost_per_hour; + 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); } } - + return output; }; - + self.onStartup = function() { var element = $("#state").find(".accordion-inner .progress"); if (element.length) { diff --git a/octoprint_cost/templates/cost_settings.jinja2 b/octoprint_cost/templates/cost_settings.jinja2 index 8a7c899..8f534e4 100644 --- a/octoprint_cost/templates/cost_settings.jinja2 +++ b/octoprint_cost/templates/cost_settings.jinja2 @@ -2,19 +2,50 @@