Skip to content

Commit 0377e94

Browse files
authored
Merge pull request #72 from imrahil/devel
Devel
2 parents cc337fe + ae7d2c1 commit 0377e94

File tree

10 files changed

+173
-110
lines changed

10 files changed

+173
-110
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,16 @@ Example:
2424

2525

2626
## Change notes:
27-
v 0.11 - added support for all platforms running under Armbian
27+
v 0.14
28+
- Temperature is visible, connection is no needed #47 #65
29+
- Fix for python 3 - #68
30+
- Support for shorter tool names - #29
31+
- Fix for settings saving reported in #47
32+
- Added possibility to remove target temperature output #57
33+
- Added possibility to configure soc name on navbar #43
34+
35+
v 0.13
36+
- added support for custom commands
37+
38+
v 0.11
39+
- added support for all platforms running under Armbian

images/custom_cmd_cfg1.png

30 KB
Loading

octoprint_navbartemp/__init__.py

Lines changed: 79 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding=utf-8
22
from __future__ import absolute_import
33

4-
__author__ = "Jarek Szczepanski <imrahil@imrahil.com>"
4+
__author__ = "Jarek Szczepanski <imrahil@imrahil.com> & Cosik <cosik3d@gmail.com>"
55
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
66
__copyright__ = "Copyright (C) 2014 Jarek Szczepanski - Released under terms of the AGPLv3 License"
77

@@ -10,7 +10,7 @@
1010
import sys
1111
import os
1212

13-
from .libs.sbc import SBCFactory
13+
from .libs.sbc import SBCFactory, SBC
1414

1515

1616
class NavBarPlugin(octoprint.plugin.StartupPlugin,
@@ -19,12 +19,13 @@ class NavBarPlugin(octoprint.plugin.StartupPlugin,
1919
octoprint.plugin.SettingsPlugin):
2020

2121
def __init__(self):
22-
self.piSocTypes = (["BCM2708", "BCM2709",
23-
"BCM2835"]) # Array of raspberry pi SoC's to check against, saves having a large if/then statement later
22+
# Array of raspberry pi SoC's to check against, saves having a large if/then statement later
23+
self.piSocTypes = (["BCM2708", "BCM2709", "BCM2835"])
2424
self.debugMode = False # to simulate temp on Win/Mac
25-
self.displayRaspiTemp = True
25+
self.displayRaspiTemp = None
2626
self._checkTempTimer = None
27-
self.sbc = None
27+
self._checkCmdTimer = None
28+
self.sbc = SBC()
2829
self.cmd = None
2930
self.cmd_name = None
3031

@@ -38,96 +39,117 @@ def on_after_startup(self):
3839

3940
if sys.platform == "linux2":
4041
self.sbc = SBCFactory().factory(self._logger)
41-
42+
if self.debugMode:
43+
self.sbc.is_supported = True
44+
self.sbc.debugMode = True
4245
if self.sbc.is_supported and self.displayRaspiTemp:
4346
self._logger.debug("Let's start RepeatedTimer!")
44-
self.startTimer(30.0)
45-
elif self.cmd_name:
46-
self._checkTempTimer = RepeatedTimer(30.0, self.updateCustom, None, None, True)
47-
self._checkTempTimer.start()
47+
interval = 5.0 if self.debugMode else 30.0
48+
self.start_soc_timer(interval)
49+
50+
if self.cmd_name:
51+
interval = 5.0 if self.debugMode else 30.0
52+
self.start_custom_timer(interval)
4853

49-
# debug mode doesn't work if the OS is linux on a regular pc
5054
try:
5155
self._logger.debug("is supported? - %s" % self.sbc.is_supported)
52-
except:
56+
except Exception:
5357
self._logger.debug("Embeded platform is not detected")
5458

55-
def startTimer(self, interval):
56-
self._checkTempTimer = RepeatedTimer(interval, self.updateSoCTemp, None, None, True)
59+
def start_soc_timer(self, interval):
60+
self._checkTempTimer = RepeatedTimer(interval, self.update_soc_temp, run_first=True)
5761
self._checkTempTimer.start()
5862

59-
def updateSoCTemp(self):
60-
temp = self.sbc.checkSoCTemp()
61-
self._logger.debug("match: %s" % temp)
62-
cmd_rtv = self.getCustomResult()
63+
def start_custom_timer(self, interval):
64+
self._checkCmdTimer = RepeatedTimer(interval, self.update_custom, run_first=True)
65+
self._checkCmdTimer.start()
6366

67+
def update_soc_temp(self):
68+
temp = self.sbc.check_soc_temp()
69+
self._logger.debug("match: %s" % temp)
6470
self._plugin_manager.send_plugin_message(self._identifier,
6571
dict(isSupported=self.sbc.is_supported,
66-
soctemp=temp, cmd_result=cmd_rtv, cmd_name=self.cmd_name))
72+
soctemp=temp))
6773

68-
def updateCustom(self):
69-
cmd_rtv = self.getCustomResult()
74+
def update_custom(self):
75+
cmd_rtv = self.get_custom_result()
7076
self._plugin_manager.send_plugin_message(self._identifier,
71-
dict(isSupported=False, cmd_result=cmd_rtv, cmd_name=self.cmd_name))
77+
dict(cmd_result=cmd_rtv, cmd_name=self.cmd_name))
7278

73-
def getCustomResult(self):
74-
cmd_rtv = None
79+
def get_custom_result(self):
7580
if self.cmd:
7681
try:
7782
cmd_rtv = str(os.popen(self.cmd).read())
7883
self._logger.debug("cmd_rtv: %s" % cmd_rtv)
7984
return cmd_rtv
80-
except:
85+
except Exception:
8186
self._logger.debug("cmd error")
8287
return ""
8388

84-
##~~ SettingsPlugin
89+
# ~~ SettingsPlugin
8590
def get_settings_defaults(self):
86-
return dict(displayRaspiTemp=self.displayRaspiTemp,
91+
return dict(displayRaspiTemp=True,
8792
piSocTypes=self.piSocTypes,
88-
cmd=self.cmd,
89-
cmd_name=None
93+
cmd="",
94+
cmd_name="",
95+
useShortNames=False,
96+
makeMoreRoom=False,
97+
soc_name="SoC",
9098
)
9199

92100
def on_settings_save(self, data):
93-
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
101+
diff = super(NavBarPlugin, self).on_settings_save(data)
102+
self._logger.debug("data: " + str(data))
103+
104+
if "displayRaspiTemp" in data:
105+
self.displayRaspiTemp = data["displayRaspiTemp"]
106+
if self.displayRaspiTemp:
107+
interval = 5.0 if self.debugMode else 30.0
108+
self.start_soc_timer(interval)
109+
else:
110+
if self._checkTempTimer is not None:
111+
try:
112+
self._checkTempTimer.cancel()
113+
except Exceptionx:
114+
pass
115+
if "cmd" in data:
116+
self.cmd = data["cmd"]
117+
self.cmd_name = data["cmd_name"]
118+
if self.cmd:
119+
interval = 5.0 if self.debugMode else 30.0
120+
self.start_custom_timer(interval)
121+
else:
122+
if self._checkCmdTimer is not None:
123+
try:
124+
self._checkCmdTimer.cancel()
125+
except Exceptionx:
126+
pass
127+
self._plugin_manager.send_plugin_message(self._identifier, dict())
94128

95-
self.displayRaspiTemp = self._settings.get(["displayRaspiTemp"])
96-
self.cmd = self._settings.get(["cmd"])
97-
self.cmd_name = self._settings.get(["cmd_name"])
129+
return diff
98130

99-
if self.displayRaspiTemp:
100-
interval = 5.0 if self.debugMode else 30.0
101-
self.startTimer(interval)
102-
else:
103-
if self._checkTempTimer is not None:
104-
try:
105-
self._checkTempTimer.cancel()
106-
except:
107-
pass
108-
self._plugin_manager.send_plugin_message(self._identifier, dict())
109-
110-
##~~ TemplatePlugin API
131+
# ~~ TemplatePlugin API
111132
def get_template_configs(self):
112133
try:
113-
if self.sbc.is_supported:
114-
return [
115-
dict(type="settings", template="navbartemp_settings_sbc.jinja2")
116-
]
117-
else:
118-
return [dict(type="settings", template="navbartemp_settings.jinja2")]
119-
except:
134+
# Todo: settings have to be fixed
135+
# if self.sbc.is_supported:
136+
# return [
137+
# dict(type="settings", template="navbartemp_settings_sbc.jinja2")
138+
# ]
139+
# else:
140+
return [dict(type="settings", template="navbartemp_settings.jinja2")]
141+
except Exception:
120142
return []
121143

122-
##~~ AssetPlugin API
144+
# ~~ AssetPlugin API
123145
def get_assets(self):
124146
return {
125147
"js": ["js/navbartemp.js"],
126148
"css": ["css/navbartemp.css"],
127149
"less": ["less/navbartemp.less"]
128150
}
129151

130-
##~~ Softwareupdate hook
152+
# ~~ Softwareupdate hook
131153
def get_update_information(self):
132154
return dict(
133155

@@ -152,7 +174,8 @@ def get_update_information(self):
152174

153175
# Starting with OctoPrint 1.4.0 OctoPrint will also support to run under Python 3 in addition to the deprecated
154176
# Python 2. New plugins should make sure to run under both versions for now.
155-
__plugin_pythoncompat__ = ">=2.7,<4" # python 2 and 3
177+
__plugin_pythoncompat__ = ">=2.7,<4" # python 2 and 3
178+
156179

157180
def __plugin_load__():
158181
global __plugin_implementation__
@@ -162,4 +185,3 @@ def __plugin_load__():
162185
__plugin_hooks__ = {
163186
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
164187
}
165-

octoprint_navbartemp/libs/sbc.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
'''
1+
"""
22
33
This module contain factory patter for different hardware platforms. Adding new platform is simple, first you need
44
add class with inheriting from SBC. Inside you have to define differences between parent class and child. You can easily
55
overwrite methods and parameters. For reference please look at Armbiand and RPi classes.
66
77
Last step is to define way of detecting platform type. It could be very different depending on OS.
88
9-
'''
9+
"""
1010

1111
import os
1212
import re
1313

1414

1515
class SBCFactory(object):
16-
17-
piSocTypes = (["BCM2708", "BCM2709",
18-
"BCM2835"]) # Array of raspberry pi SoC's to check against, saves having a large if/then statement later
16+
# Array of raspberry pi SoC's to check against, saves having a large if/then statement later
17+
piSocTypes = (["BCM2708", "BCM2709", "BCM2835"])
1918

2019
# Create based on class name:
2120
def factory(self, logger):
@@ -28,7 +27,6 @@ def factory(self, logger):
2827
return Armbian(logger)
2928
elif self._is_rpi(logger):
3029
return RPi(logger)
31-
3230
return SBC()
3331

3432
def _is_rpi(self, logger):
@@ -40,12 +38,12 @@ def _is_rpi(self, logger):
4038
with open('/proc/cpuinfo', 'r') as infile:
4139
cpuinfo = infile.read()
4240
# Match a line like 'Hardware : BCM2709'
43-
match = re.search('Hardware\s+:\s+(\w+)', cpuinfo, flags=re.MULTILINE | re.IGNORECASE)
41+
match = re.search(r'Hardware\s+:\s+(\w+)', cpuinfo, flags=re.MULTILINE | re.IGNORECASE)
4442

4543
if not match:
4644
return False
4745
elif match.group(1) in self.piSocTypes:
48-
logger.info("Broadcom detected")
46+
logger.debug("Broadcom detected")
4947
return True
5048
return False
5149

@@ -58,32 +56,27 @@ def _is_armbian(self):
5856

5957

6058
class SBC(object):
61-
6259
temp_cmd = ''
6360
is_supported = False
6461
debugMode = False
6562
parse_pattern = ''
63+
_logger = None
64+
65+
def check_soc_temp(self):
66+
if self.debugMode:
67+
import random
68+
return str(round(random.uniform(5, 60), 2))
6669

67-
def checkSoCTemp(self):
6870
if self.is_supported:
6971
from sarge import run, Capture
70-
71-
#The following generate a log entry every 30 sec, not very good to write so much to the SD card. uncomment for debugging purposes.
72-
#self._logger.info("Checking SoC internal temperature")
72+
7373
p = run(self.temp_cmd, stdout=Capture())
7474
if p.returncode == 1:
7575
self.is_supported = False
76-
self._logger.info("SoC temperature not found.")
76+
self._logger.debug("SoC temperature not found.")
7777
else:
7878
p = p.stdout.text
7979

80-
# elif self.debugMode: # doesn't work on linux
81-
# import random
82-
# def randrange_float(start, stop, step):
83-
# return random.randint(0, int((stop - start) / step)) * step + start
84-
#
85-
# p = "temp=%s'C" % randrange_float(5, 60, 0.1)
86-
8780
self._logger.debug("response from sarge: %s" % p)
8881
self._logger.debug("used pattern: %r" % self.parse_pattern)
8982
match = re.search(self.parse_pattern, p)
@@ -92,13 +85,14 @@ def checkSoCTemp(self):
9285
self._logger.debug("match: not found")
9386
self.is_supported = False
9487
else:
95-
temp = self.parse_tepmerature(match)
88+
temp = self.parse_temperature(match)
9689
self._logger.debug("match: %s" % str(temp))
9790

9891
return temp
92+
9993
return 0
10094

101-
def parse_tepmerature(self, re_output):
95+
def parse_temperature(self, re_output):
10296
return re_output.group(1)
10397

10498

@@ -116,10 +110,10 @@ class Armbian(SBC):
116110
def __init__(self, logger):
117111
self.is_supported = True
118112
self.temp_cmd = 'cat /etc/armbianmonitor/datasources/soctemp'
119-
self.parse_pattern = '(\d+)'
113+
self.parse_pattern = r'(\d+)'
120114
self._logger = logger
121115

122-
def parse_tepmerature(self, re_output):
116+
def parse_temperature(self, re_output):
123117
"""
124118
We are expecting that temp of SoC will be no more that 3 digit int. Armbian on Odroid is returning ex 44000 but
125119
on orangePi 26

0 commit comments

Comments
 (0)