Skip to content

Commit

Permalink
Merge pull request #2 from Pinaute/cost_per_weight
Browse files Browse the repository at this point in the history
Add feature : cost per weight
  • Loading branch information
jasiek authored Apr 10, 2017
2 parents 79966e3 + d82353b commit cc05f26
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 28 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)

Expand Down
17 changes: 12 additions & 5 deletions octoprint_cost/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"])
)

Expand Down
63 changes: 49 additions & 14 deletions octoprint_cost/static/js/cost.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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) {
Expand Down
41 changes: 36 additions & 5 deletions octoprint_cost/templates/cost_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,50 @@
<div class="control-group">
<label class="control-label">{{ _('Currency') }}</label>
<div class="controls">
<input type="text" class="input-block-level" data-bind="value: settings.plugins.cost.currency">
<input type="text" class="input-mini text-right" data-bind="value: settings.plugins.cost.currency">
</div>
</div>
<div class="control-group">
<label class="control-label">{{ _('Cost per hour') }}</label>
<label class="control-label">{{ _('Cost per time') }}</label>
<div class="controls">
<input type="text" class="input-block-level" data-bind="value: settings.plugins.cost.cost_per_hour">
<div class="input-append">
<input step="0.01" min="0" class="input-mini text-right" data-bind="value: settings.plugins.cost.cost_per_time" type="number">
<span class="add-on" data-bind="text: costPerTime"></span>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">{{ _('Cost per meter') }}</label>
<label class="control-label">{{ _('Density of filament') }}</label>
<div class="controls">
<input type="text" class="input-block-level" data-bind="value: settings.plugins.cost.cost_per_meter">
<div class="input-append">
<input step="0.01" min="0" class="input-mini text-right" data-bind="value: settings.plugins.cost.density_of_filament" type="number">
<span class="add-on">g/cm<sup>3</sup></span>
</div>
</div>
</div>
<div class="control-group" title="Makes OctoPrint try to connect to the printer automatically during start up">
<div class="controls">
<label class="checkbox">
<input data-bind="checked: check_cost" type="checkbox">Cost per length or weight
</label>
</div>
</div>
<div class="row-fluid">
<div class="offset3 span3">Weight</div>
<div class="span5">Length</div>
</div>
<div class="control-group">
<div class="controls">
<div class="row-fluid">
<div class="input-append span4">
<input step="0.01" min="0" class="input-mini text-right" data-bind="value: settings.plugins.cost.cost_per_weight, enable: check_cost" type="number">
<span class="add-on" data-bind="text: costPerWeight"></span>
</div>
<div class="input-append span4">
<input step="0.001" min="0" class="input-mini text-right" data-bind="value: settings.plugins.cost.cost_per_length, disable: check_cost" type="number">
<span class="add-on" data-bind="text: costPerLength"></span>
</div>
</div>
</div>
</div>
</form>
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint-Cost"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "0.1.2"
plugin_version = "0.1.3"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit cc05f26

Please sign in to comment.