Skip to content

Commit 1431baa

Browse files
committed
Added settings dialog for internal temperature on Raspberry Pi
1 parent a35c0d9 commit 1431baa

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

octoprint_navbartemp/__init__.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212

1313
class NavBarPlugin(octoprint.plugin.StartupPlugin,
1414
octoprint.plugin.TemplatePlugin,
15-
octoprint.plugin.AssetPlugin):
15+
octoprint.plugin.AssetPlugin,
16+
octoprint.plugin.SettingsPlugin):
1617

1718
def __init__(self):
1819
self.isRaspi = False
19-
self.debugMode = False # to simulate temp on Win/Mac
20+
self.debugMode = True # to simulate temp on Win/Mac
21+
self.displayRaspiTemp = True
22+
self._checkTempTimer = None
2023

2124
def on_after_startup(self):
25+
self.displayRaspiTemp = self._settings.get(["displayRaspiTemp"])
26+
self._logger.debug("displayRaspiTemp: %s" % self.displayRaspiTemp)
27+
2228
if sys.platform == "linux2":
2329
with open('/proc/cpuinfo', 'r') as infile:
2430
cpuinfo = infile.read()
@@ -35,17 +41,20 @@ def on_after_startup(self):
3541
self._logger.debug("Pi 2")
3642
self.isRaspi = True
3743

38-
if self.isRaspi:
44+
if self.isRaspi and self.displayRaspiTemp:
3945
self._logger.debug("Let's start RepeatedTimer!")
40-
t = RepeatedTimer(30.0, self.checkRaspiTemp)
41-
t.start()
46+
self.startTimer(30.0)
4247
elif self.debugMode:
4348
self.isRaspi = True
44-
t = RepeatedTimer(5.0, self.checkRaspiTemp)
45-
t.start()
49+
if self.displayRaspiTemp:
50+
self.startTimer(5.0)
4651

4752
self._logger.debug("is Raspberry Pi? - %s" % self.isRaspi)
4853

54+
def startTimer(self, interval):
55+
self._checkTempTimer = RepeatedTimer(interval, self.checkRaspiTemp, None, None, True)
56+
self._checkTempTimer.start()
57+
4958
def checkRaspiTemp(self):
5059
from sarge import run, Capture
5160

@@ -54,12 +63,11 @@ def checkRaspiTemp(self):
5463
if sys.platform == "linux2":
5564
p = run("/opt/vc/bin/vcgencmd measure_temp", stdout=Capture())
5665
p = p.stdout.text
66+
5767
elif self.debugMode:
5868
import random
59-
6069
def randrange_float(start, stop, step):
6170
return random.randint(0, int((stop - start) / step)) * step + start
62-
6371
p = "temp=%s'C" % randrange_float(5, 60, 0.1)
6472

6573
self._logger.debug("response from sarge: %s" % p)
@@ -73,8 +81,36 @@ def randrange_float(start, stop, step):
7381
self._plugin_manager.send_plugin_message(self._identifier, dict(israspi=self.isRaspi, raspitemp=temp))
7482

7583

84+
##~~ SettingsPlugin
85+
def get_settings_defaults(self):
86+
return dict(displayRaspiTemp = self.displayRaspiTemp)
87+
88+
def on_settings_save(self, data):
89+
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
90+
91+
self.displayRaspiTemp = self._settings.get(["displayRaspiTemp"])
92+
93+
if self.displayRaspiTemp:
94+
interval = 5.0 if self.debugMode else 30.0
95+
self.startTimer(interval)
96+
else:
97+
if self._checkTempTimer is not None:
98+
try:
99+
self._checkTempTimer.cancel()
100+
except:
101+
pass
102+
self._plugin_manager.send_plugin_message(self._identifier, dict())
103+
104+
##~~ TemplatePlugin API
105+
def get_template_configs(self):
106+
if self.isRaspi:
107+
return [
108+
dict(type="settings", template="navbartemp_settings_raspi.jinja2")
109+
]
110+
else:
111+
return []
112+
76113
##~~ AssetPlugin API
77-
78114
def get_assets(self):
79115
return {
80116
"js": ["js/navbartemp.js"],

octoprint_navbartemp/static/js/navbartemp.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ $(function() {
33
var self = this;
44

55
self.navBarTempModel = parameters[0];
6+
self.global_settings = parameters[1];
67
self.raspiTemp = ko.observable();
78
self.isRaspi = ko.observable(false);
89

10+
self.onBeforeBinding = function () {
11+
self.settings = self.global_settings.settings.plugins.navbartemp;
12+
};
13+
914
self.onDataUpdaterPluginMessage = function(plugin, data) {
1015
if (plugin != "navbartemp") {
1116
return;
@@ -24,8 +29,8 @@ $(function() {
2429

2530
ADDITIONAL_VIEWMODELS.push([
2631
NavbarTempViewModel,
27-
["temperatureViewModel"],
28-
["#navbar_plugin_navbartemp"]
32+
["temperatureViewModel", "settingsViewModel"],
33+
["#navbar_plugin_navbartemp", "#settings_plugin_navbartemp"]
2934
]);
3035
});
3136

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<h4>Configuration for Raspberry Pi</h4>
2+
3+
<form class="form-horizontal">
4+
<div class="control-group">
5+
<label class="control-label" for="settings-navbartemp-display"></label>
6+
<div class="controls">
7+
<label class="checkbox">
8+
<input type="checkbox" data-bind="checked: settings.displayRaspiTemp"> Display internal temperature
9+
</label>
10+
</div>
11+
</div>
12+
</form>

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
plugin_identifier = "navbartemp"
77
plugin_package = "octoprint_%s" % plugin_identifier
88
plugin_name = "OctoPrint-NavbarTemp"
9-
plugin_version = "0.6.1"
9+
plugin_version = "0.7"
1010
plugin_description = "Displays temperatures on navbar"
1111
plugin_author = "Jarek Szczepanski"
1212
plugin_author_email = "imrahil@imrahil.com"

0 commit comments

Comments
 (0)