10
10
import myDevices .cloud .cayennemqtt as cayennemqtt
11
11
from myDevices .utils .config import Config
12
12
from myDevices .utils .logger import debug , error , exception , info
13
+ from myDevices .utils .subprocess import executeCommand
14
+
15
+ PLUGIN_FOLDER = '/etc/myDevices/plugins'
13
16
14
17
15
18
class PluginManager ():
16
19
"""Loads plugins and reads/writes plugin data"""
17
20
18
21
def __init__ (self ):
19
22
"""Initializes the plugin manager and loads the plugin list"""
20
- self .plugin_folder = '/etc/myDevices/plugins'
23
+ self .plugin_folder = PLUGIN_FOLDER
21
24
self .plugins = {}
22
25
self .load_plugins ()
23
26
@@ -33,6 +36,8 @@ def load_plugin_from_file(self, filename):
33
36
enabled = config .get (section , 'enabled' , 'true' ).lower () == 'true'
34
37
if enabled :
35
38
plugin = {
39
+ 'filename' : filename ,
40
+ 'section' : section ,
36
41
'channel' : config .get (section , 'channel' ),
37
42
'name' : config .get (section , 'name' , section ),
38
43
'module' : config .get (section , 'module' ),
@@ -96,10 +101,13 @@ def convert_to_dict(self, value):
96
101
value_dict ['unit' ] = 'null'
97
102
return value_dict
98
103
99
- def is_plugin (self , plugin , channel ):
100
- """Returns True if the specified plugin:channel is a valid plugin """
104
+ def is_plugin (self , plugin , channel = None ):
105
+ """Returns True if the specified plugin or plugin :channel are valid plugins """
101
106
try :
102
- return plugin + ':' + channel in self .plugins .keys ()
107
+ key = plugin
108
+ if channel is not None :
109
+ key = plugin + ':' + channel
110
+ return key in self .plugins .keys ()
103
111
except :
104
112
return False
105
113
@@ -116,4 +124,20 @@ def write_value(self, plugin, channel, value):
116
124
return False
117
125
else :
118
126
return False
119
- return True
127
+ return True
128
+
129
+ def disable (self , plugin ):
130
+ """Disable the specified plugin"""
131
+ disabled = False
132
+ try :
133
+ output , result = executeCommand ('sudo python3 -m myDevices.plugins.disable "{}" "{}"' .format (self .plugins [plugin ]['filename' ], self .plugins [plugin ]['section' ]))
134
+ if result == 0 :
135
+ disabled = True
136
+ info ('Plugin \' {}\' disabled' .format (plugin ))
137
+ else :
138
+ info ('Plugin \' {}\' not disabled' .format (plugin ))
139
+ del self .plugins [plugin ]
140
+ except Exception as e :
141
+ info (e )
142
+ pass
143
+ return disabled
0 commit comments