Adding a new interface can be straightforward if you are familiar with the extension system. This page expands on the concepts outlined in the extension developer docs focusing on the interfaces.
Lets look at adding an interface for a sensor to a counter
example extension. The file should be named after the namespace for the extension we are providing an interface for. In this example a file named sensor.py
is created under a counter
extension folder.
This counter
extension directory would look like the following:
mudpi
└── extensions
└── counter
└── __init__.py
└── extension.json
└── sensor.py
An interface needs to extend from the BaseInterface
class at minimum. The interface must be named Interface
so that MudPi can easily locate the class dynamically.
The interface requires a load()
method that accepts a config dict in which it needs to process. The load()
method needs to return True if successful. This dict of data will already have been passed to the interface's validate()
method to be checked for configuration errors.
If the interface is adding a component it should do so by passing the instantiated component to the add_component
method.
Here is what the sensor.py
file for the counter
extension could look like:
from mudpi.extensions import BaseInterface
from mudpi.extensions.sensor import Sensor
class Interface(BaseInterface):
def load(self, config):
sensor = CounterSensor(self.mudpi, config)
self.add_component(sensor)
return True
def validate(self, config):
if not config[self.namespace].get('start_count'):
raise ConfigError('Missing `start_count` in hello configs.')
return config
class CounterSensor(Sensor):
@property
def state(self):
return self._state
def init(self):
self._state = self.config.get('start_count', 0)
def update(self):
self._state+=1
return True
The interface will load()
a CounterSensor
which in this case just increments each time it updates. If you want to learn more on components specifically read the developer component docs.
You could then use the following config to load the sensor through this interface:
"sensor": [{
"key": "counter_1",
"interface": "counter",
"start_count": 0
}]
A good place to start if you are looking to create an interface is the example
extension. This extension is focused on simple interfaces for testing each of the core components that support interface. There you can find some references on how to get started with your own interface up.