11# coding=utf-8
22from __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
1010import sys
1111import os
1212
13- from .libs .sbc import SBCFactory
13+ from .libs .sbc import SBCFactory , SBC
1414
1515
1616class 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
157180def __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-
0 commit comments