Skip to content

Commit 2435bf0

Browse files
committed
Treat extensions like standard plugins, add read_args.
1 parent 63e7cd7 commit 2435bf0

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

myDevices/devices/analog/helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def setADCInstance(self):
3636
if not self.adc:
3737
self.adc = instance.deviceInstance(self.adcname)
3838
if not self.adc:
39-
self.adc = self.pluginManager.get_extension(self.adcname)
39+
self.adc = self.pluginManager.get_plugin_by_id(self.adcname)
4040

4141
@response("%d")
4242
def read(self):

myDevices/plugins/analog.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
This module provides classes for interfacing with analog plugins.
3+
"""
4+
import json
5+
from myDevices.plugins.manager import PluginManager
6+
from myDevices.utils.logger import info
7+
8+
9+
class AnalogInput():
10+
"""Reads data from an analog input."""
11+
12+
def __init__(self, adc_name):
13+
"""Initializes the analog input.
14+
15+
Arguments:
16+
adc_name: Name of analog-to-digital converter plugin in the format 'plugin_name:section'
17+
"""
18+
self.adc_name = adc_name
19+
self.adc = None
20+
self.read_args = {}
21+
self.pluginManager = PluginManager()
22+
self.set_adc()
23+
24+
def set_adc(self):
25+
"""Sets the ADC plugin."""
26+
if not self.adc:
27+
self.adc = self.pluginManager.get_plugin_by_id(self.adc_name)
28+
self.read_args = json.loads(self.adc['read_args'])
29+
30+
def read_value(self, channel, data_type=None):
31+
"""Read the data value on the specified channel."""
32+
self.set_adc()
33+
value = getattr(self.adc['instance'], self.adc['read'])(channel, data_type=data_type, **self.read_args)
34+
return value
35+
36+
def read_float(self, channel):
37+
"""Read the float value on the specified channel."""
38+
return self.read_value(channel, 'float')
39+
40+
def read_volt(self, channel):
41+
"""Read the voltage on the specified channel."""
42+
return self.read_value(channel, 'volt')

myDevices/plugins/manager.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import os
88
import sys
9+
from configparser import NoOptionError
910

1011
import myDevices.cloud.cayennemqtt as cayennemqtt
1112
from myDevices.utils.config import Config
@@ -24,7 +25,6 @@ def __init__(self, callback=None):
2425
self.plugin_folder = PLUGIN_FOLDER
2526
self.callback = callback
2627
self.plugins = {}
27-
self.extensions = {}
2828

2929
def load_plugin_from_file(self, filename):
3030
"""Loads a plugin from a specified plugin config file and adds it to the plugin list."""
@@ -43,18 +43,17 @@ def load_plugin_from_file(self, filename):
4343
try:
4444
enabled = config.get(section, 'enabled', 'true').lower() == 'true'
4545
inherit = config.get(section, 'inherit', None)
46-
extension = config.get(section, 'extension', 'false').lower() == 'true'
4746
if enabled or section in inherited_from:
4847
plugin = {
4948
'enabled': enabled,
5049
'filename': filename,
5150
'section': section,
5251
'name': config.get(section, 'name', section),
5352
}
54-
if not extension:
53+
try:
5554
plugin['channel'] = config.get(section, 'channel')
5655
plugin['id'] = plugin_name + ':' + plugin['channel']
57-
else:
56+
except NoOptionError:
5857
plugin['id'] = plugin_name + ':' + section
5958
inherit_items = {}
6059
if inherit in config.sections():
@@ -79,6 +78,9 @@ def load_plugin_from_file(self, filename):
7978
device_class = getattr(imported_module, plugin['class'])
8079
plugin['instance'] = device_class(**json.loads(plugin['init_args']))
8180
self.override_plugin_value(config, section, 'read', plugin)
81+
if 'read_args' not in plugin:
82+
plugin['read_args'] = '{}'
83+
self.override_plugin_value(config, section, 'read_args', plugin)
8284
try:
8385
self.override_plugin_value(config, section, 'write', plugin)
8486
except:
@@ -92,10 +94,7 @@ def load_plugin_from_file(self, filename):
9294
self.override_plugin_value(config, section, 'unregister_callback', plugin)
9395
except:
9496
pass
95-
if not extension:
96-
self.plugins[plugin['id']] = plugin
97-
else:
98-
self.extensions[plugin['id']] = plugin
97+
self.plugins[plugin['id']] = plugin
9998
loaded.append(section)
10099
except Exception as e:
101100
error(e)
@@ -111,20 +110,19 @@ def load_plugins(self):
111110
#Remove any disabled plugins that were only loaded because they are inherited from.
112111
self.plugins = {key:value for key, value in self.plugins.items() if value['enabled']}
113112
info('Enabled plugins: {}'.format(self.plugins.keys()))
114-
info('Enabled extensions: {}'.format(self.extensions.keys()))
115113

116114
def get_plugin(self, filename, section):
117115
"""Return the plugin for the corresponding filename and section."""
118116
return next(plugin for plugin in self.plugins.values() if plugin['filename'] == filename and plugin['section'] == section)
119117

120-
def get_extension(self, id):
121-
"""Return the extension with the corresponding id."""
122-
extension = None
118+
def get_plugin_by_id(self, id):
119+
"""Return the plugin with the corresponding id."""
120+
plugin = None
123121
try:
124-
extension = self.extensions[id]
122+
plugin = self.plugins[id]
125123
except:
126124
pass
127-
return extension
125+
return plugin
128126

129127
def override_plugin_value(self, config, section, key, plugin):
130128
"""Override the plugin value for the specified key if it exists in the config file"""
@@ -138,10 +136,11 @@ def get_plugin_readings(self):
138136
readings = []
139137
for key, plugin in self.plugins.items():
140138
try:
141-
value = getattr(plugin['instance'], plugin['read'])()
142-
value_dict = self.convert_to_dict(value)
143-
if value_dict:
144-
cayennemqtt.DataChannel.add(readings, cayennemqtt.DEV_SENSOR, key, name=plugin['name'], **value_dict)
139+
if 'channel' in plugin:
140+
value = getattr(plugin['instance'], plugin['read'])(**json.loads(plugin['read_args']))
141+
value_dict = self.convert_to_dict(value)
142+
if value_dict:
143+
cayennemqtt.DataChannel.add(readings, cayennemqtt.DEV_SENSOR, key, name=plugin['name'], **value_dict)
145144
except KeyError as e:
146145
debug('Missing key {} in plugin \'{}\''.format(e, plugin['name']))
147146
except:

0 commit comments

Comments
 (0)