|
| 1 | +""" |
| 2 | +This module provides a class for interfacing with digital, analog and PWM plugins. |
| 3 | +""" |
| 4 | +from myDevices.plugins.manager import PluginManager |
| 5 | +from myDevices.utils.logger import debug, info, exception |
| 6 | + |
| 7 | + |
| 8 | +class InputOutput(): |
| 9 | + """Reads/writes data from a digital, analog or PWM input/output plugin.""" |
| 10 | + DATA_TYPES = {'digital': {'in': 'digital_sensor', 'out':'digital_actuator'}, |
| 11 | + 'analog': {'in': 'analog_sensor', 'out': 'analog_actuator'}} |
| 12 | + |
| 13 | + def __init__(self, plugin_id, io_type, function): |
| 14 | + """Initializes the analog input/output. |
| 15 | + |
| 16 | + Arguments: |
| 17 | + plugin_id: Extension plugin ID in the format 'plugin_name:section', e.g. 'cayenne-pca9685:PCA9685' |
| 18 | + io_type: The type of IO, 'analog' or 'digital' |
| 19 | + function: The pin function, 'in' if the pin is an input, 'out' if it is an output |
| 20 | + """ |
| 21 | + self.plugin_id = plugin_id |
| 22 | + self.plugin = None |
| 23 | + self.io_type = io_type.lower() |
| 24 | + self.function = function.lower() |
| 25 | + self.current_functions = {} |
| 26 | + self.read_args = {} |
| 27 | + self.write_args = {} |
| 28 | + self.plugin_manager = PluginManager() |
| 29 | + self.set_plugin() |
| 30 | + |
| 31 | + def set_plugin(self): |
| 32 | + """Sets the plugin_id plugin.""" |
| 33 | + if not self.plugin: |
| 34 | + self.plugin = self.plugin_manager.get_plugin_by_id(self.plugin_id) |
| 35 | + try: |
| 36 | + self.read_args = self.plugin_manager.get_args(self.plugin, 'read_args') |
| 37 | + self.write_args = self.plugin_manager.get_args(self.plugin, 'write_args') |
| 38 | + except: |
| 39 | + pass |
| 40 | + |
| 41 | + def set_function(self, channel): |
| 42 | + """Sets the input/output function.""" |
| 43 | + try: |
| 44 | + if self.plugin and (channel not in self.current_functions or self.function != self.current_functions[channel]): |
| 45 | + function = getattr(self.plugin['instance'], self.plugin['set_function'])(channel, self.function).lower() |
| 46 | + self.current_functions[channel] = function |
| 47 | + if function == 'in': |
| 48 | + try: |
| 49 | + debug('Register callback for channel {}'.format(channel)) |
| 50 | + getattr(self.plugin['instance'], self.plugin['register_callback'])(channel, self.data_changed, data=channel) |
| 51 | + except: |
| 52 | + debug('Unable to register callback for channel {}'.format(channel)) |
| 53 | + pass |
| 54 | + except: |
| 55 | + debug('Error setting function') |
| 56 | + |
| 57 | + def to_tuple(self, value): |
| 58 | + """Converts value to tuple with the appropriate data type.""" |
| 59 | + try: |
| 60 | + return (value, InputOutput.DATA_TYPES[self.io_type][self.function]) |
| 61 | + except: |
| 62 | + return value |
| 63 | + |
| 64 | + def read(self, channel, data_type=None): |
| 65 | + """Gets the data value for the channel as a tuple with the type.""" |
| 66 | + return self.to_tuple(self.read_value(channel, data_type)) |
| 67 | + |
| 68 | + def read_value(self, channel, data_type=None): |
| 69 | + """Read the data value on the specified channel.""" |
| 70 | + self.set_plugin() |
| 71 | + self.set_function(channel) |
| 72 | + result = None |
| 73 | + try: |
| 74 | + result = getattr(self.plugin['instance'], self.plugin['read'])(channel, data_type=data_type, **self.read_args) |
| 75 | + except: |
| 76 | + info('Error reading value from plugin {}, channel {}, {}'.format(self.plugin_id, channel, self.plugin)) |
| 77 | + return result |
| 78 | + |
| 79 | + def write(self, value, channel, data_type=None): |
| 80 | + """Write the digital value for the channel.""" |
| 81 | + info('IO write, value {}, channel {}'.format(value, channel)) |
| 82 | + return self.write_value(value, channel, data_type) |
| 83 | + |
| 84 | + def write_value(self, value, channel, data_type=None): |
| 85 | + """Write the data value on the specified channel.""" |
| 86 | + self.set_plugin() |
| 87 | + self.set_function(channel) |
| 88 | + result = None |
| 89 | + try: |
| 90 | + result = getattr(self.plugin['instance'], self.plugin['write'])(channel, value, data_type=data_type, **self.write_args) |
| 91 | + except ValueError as e: |
| 92 | + debug(e) |
| 93 | + return result |
| 94 | + |
| 95 | + def register_callback(self, callback): |
| 96 | + """Register a callback for data changes.""" |
| 97 | + info('Registering callback: {}'.format(callback)) |
| 98 | + self.callback = callback |
| 99 | + |
| 100 | + def unregister_callback(self): |
| 101 | + """Register a callback for data changes.""" |
| 102 | + self.callback = None |
| 103 | + |
| 104 | + def data_changed(self, channel, value): |
| 105 | + """Callback that is called when data has changed.""" |
| 106 | + if self.callback: |
| 107 | + self.callback(self.to_tuple(value)) |
0 commit comments