Skip to content

Commit

Permalink
Merge pull request #72 from imrahil/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
Cosik authored May 7, 2020
2 parents cc337fe + ae7d2c1 commit 0377e94
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 110 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ Example:


## Change notes:
v 0.11 - added support for all platforms running under Armbian
v 0.14
- Temperature is visible, connection is no needed #47 #65
- Fix for python 3 - #68
- Support for shorter tool names - #29
- Fix for settings saving reported in #47
- Added possibility to remove target temperature output #57
- Added possibility to configure soc name on navbar #43

v 0.13
- added support for custom commands

v 0.11
- added support for all platforms running under Armbian
Binary file modified images/custom_cmd_cfg1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
136 changes: 79 additions & 57 deletions octoprint_navbartemp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding=utf-8
from __future__ import absolute_import

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

Expand All @@ -10,7 +10,7 @@
import sys
import os

from .libs.sbc import SBCFactory
from .libs.sbc import SBCFactory, SBC


class NavBarPlugin(octoprint.plugin.StartupPlugin,
Expand All @@ -19,12 +19,13 @@ class NavBarPlugin(octoprint.plugin.StartupPlugin,
octoprint.plugin.SettingsPlugin):

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

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

if sys.platform == "linux2":
self.sbc = SBCFactory().factory(self._logger)

if self.debugMode:
self.sbc.is_supported = True
self.sbc.debugMode = True
if self.sbc.is_supported and self.displayRaspiTemp:
self._logger.debug("Let's start RepeatedTimer!")
self.startTimer(30.0)
elif self.cmd_name:
self._checkTempTimer = RepeatedTimer(30.0, self.updateCustom, None, None, True)
self._checkTempTimer.start()
interval = 5.0 if self.debugMode else 30.0
self.start_soc_timer(interval)

if self.cmd_name:
interval = 5.0 if self.debugMode else 30.0
self.start_custom_timer(interval)

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

def startTimer(self, interval):
self._checkTempTimer = RepeatedTimer(interval, self.updateSoCTemp, None, None, True)
def start_soc_timer(self, interval):
self._checkTempTimer = RepeatedTimer(interval, self.update_soc_temp, run_first=True)
self._checkTempTimer.start()

def updateSoCTemp(self):
temp = self.sbc.checkSoCTemp()
self._logger.debug("match: %s" % temp)
cmd_rtv = self.getCustomResult()
def start_custom_timer(self, interval):
self._checkCmdTimer = RepeatedTimer(interval, self.update_custom, run_first=True)
self._checkCmdTimer.start()

def update_soc_temp(self):
temp = self.sbc.check_soc_temp()
self._logger.debug("match: %s" % temp)
self._plugin_manager.send_plugin_message(self._identifier,
dict(isSupported=self.sbc.is_supported,
soctemp=temp, cmd_result=cmd_rtv, cmd_name=self.cmd_name))
soctemp=temp))

def updateCustom(self):
cmd_rtv = self.getCustomResult()
def update_custom(self):
cmd_rtv = self.get_custom_result()
self._plugin_manager.send_plugin_message(self._identifier,
dict(isSupported=False, cmd_result=cmd_rtv, cmd_name=self.cmd_name))
dict(cmd_result=cmd_rtv, cmd_name=self.cmd_name))

def getCustomResult(self):
cmd_rtv = None
def get_custom_result(self):
if self.cmd:
try:
cmd_rtv = str(os.popen(self.cmd).read())
self._logger.debug("cmd_rtv: %s" % cmd_rtv)
return cmd_rtv
except:
except Exception:
self._logger.debug("cmd error")
return ""

##~~ SettingsPlugin
# ~~ SettingsPlugin
def get_settings_defaults(self):
return dict(displayRaspiTemp=self.displayRaspiTemp,
return dict(displayRaspiTemp=True,
piSocTypes=self.piSocTypes,
cmd=self.cmd,
cmd_name=None
cmd="",
cmd_name="",
useShortNames=False,
makeMoreRoom=False,
soc_name="SoC",
)

def on_settings_save(self, data):
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
diff = super(NavBarPlugin, self).on_settings_save(data)
self._logger.debug("data: " + str(data))

if "displayRaspiTemp" in data:
self.displayRaspiTemp = data["displayRaspiTemp"]
if self.displayRaspiTemp:
interval = 5.0 if self.debugMode else 30.0
self.start_soc_timer(interval)
else:
if self._checkTempTimer is not None:
try:
self._checkTempTimer.cancel()
except Exceptionx:
pass
if "cmd" in data:
self.cmd = data["cmd"]
self.cmd_name = data["cmd_name"]
if self.cmd:
interval = 5.0 if self.debugMode else 30.0
self.start_custom_timer(interval)
else:
if self._checkCmdTimer is not None:
try:
self._checkCmdTimer.cancel()
except Exceptionx:
pass
self._plugin_manager.send_plugin_message(self._identifier, dict())

self.displayRaspiTemp = self._settings.get(["displayRaspiTemp"])
self.cmd = self._settings.get(["cmd"])
self.cmd_name = self._settings.get(["cmd_name"])
return diff

if self.displayRaspiTemp:
interval = 5.0 if self.debugMode else 30.0
self.startTimer(interval)
else:
if self._checkTempTimer is not None:
try:
self._checkTempTimer.cancel()
except:
pass
self._plugin_manager.send_plugin_message(self._identifier, dict())

##~~ TemplatePlugin API
# ~~ TemplatePlugin API
def get_template_configs(self):
try:
if self.sbc.is_supported:
return [
dict(type="settings", template="navbartemp_settings_sbc.jinja2")
]
else:
return [dict(type="settings", template="navbartemp_settings.jinja2")]
except:
# Todo: settings have to be fixed
# if self.sbc.is_supported:
# return [
# dict(type="settings", template="navbartemp_settings_sbc.jinja2")
# ]
# else:
return [dict(type="settings", template="navbartemp_settings.jinja2")]
except Exception:
return []

##~~ AssetPlugin API
# ~~ AssetPlugin API
def get_assets(self):
return {
"js": ["js/navbartemp.js"],
"css": ["css/navbartemp.css"],
"less": ["less/navbartemp.less"]
}

##~~ Softwareupdate hook
# ~~ Softwareupdate hook
def get_update_information(self):
return dict(

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

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


def __plugin_load__():
global __plugin_implementation__
Expand All @@ -162,4 +185,3 @@ def __plugin_load__():
__plugin_hooks__ = {
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
}

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

import os
import re


class SBCFactory(object):

piSocTypes = (["BCM2708", "BCM2709",
"BCM2835"]) # Array of raspberry pi SoC's to check against, saves having a large if/then statement later
# Array of raspberry pi SoC's to check against, saves having a large if/then statement later
piSocTypes = (["BCM2708", "BCM2709", "BCM2835"])

# Create based on class name:
def factory(self, logger):
Expand All @@ -28,7 +27,6 @@ def factory(self, logger):
return Armbian(logger)
elif self._is_rpi(logger):
return RPi(logger)

return SBC()

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

if not match:
return False
elif match.group(1) in self.piSocTypes:
logger.info("Broadcom detected")
logger.debug("Broadcom detected")
return True
return False

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


class SBC(object):

temp_cmd = ''
is_supported = False
debugMode = False
parse_pattern = ''
_logger = None

def check_soc_temp(self):
if self.debugMode:
import random
return str(round(random.uniform(5, 60), 2))

def checkSoCTemp(self):
if self.is_supported:
from sarge import run, Capture

#The following generate a log entry every 30 sec, not very good to write so much to the SD card. uncomment for debugging purposes.
#self._logger.info("Checking SoC internal temperature")

p = run(self.temp_cmd, stdout=Capture())
if p.returncode == 1:
self.is_supported = False
self._logger.info("SoC temperature not found.")
self._logger.debug("SoC temperature not found.")
else:
p = p.stdout.text

# elif self.debugMode: # doesn't work on linux
# import random
# def randrange_float(start, stop, step):
# return random.randint(0, int((stop - start) / step)) * step + start
#
# p = "temp=%s'C" % randrange_float(5, 60, 0.1)

self._logger.debug("response from sarge: %s" % p)
self._logger.debug("used pattern: %r" % self.parse_pattern)
match = re.search(self.parse_pattern, p)
Expand All @@ -92,13 +85,14 @@ def checkSoCTemp(self):
self._logger.debug("match: not found")
self.is_supported = False
else:
temp = self.parse_tepmerature(match)
temp = self.parse_temperature(match)
self._logger.debug("match: %s" % str(temp))

return temp

return 0

def parse_tepmerature(self, re_output):
def parse_temperature(self, re_output):
return re_output.group(1)


Expand All @@ -116,10 +110,10 @@ class Armbian(SBC):
def __init__(self, logger):
self.is_supported = True
self.temp_cmd = 'cat /etc/armbianmonitor/datasources/soctemp'
self.parse_pattern = '(\d+)'
self.parse_pattern = r'(\d+)'
self._logger = logger

def parse_tepmerature(self, re_output):
def parse_temperature(self, re_output):
"""
We are expecting that temp of SoC will be no more that 3 digit int. Armbian on Odroid is returning ex 44000 but
on orangePi 26
Expand Down
Loading

0 comments on commit 0377e94

Please sign in to comment.