How to read / save Binding config [message #1775395] |
Sun, 29 October 2017 08:36  |
Eclipse User |
|
|
|
Hey all,
after searching for quite a while I was not able to determine how to save and load config data for a binding.
The background is that I only want to add things once the user is successfully logged in to a page.
This works fine for the first time the plugin is started.
After entering the credentials things will be discovered and added to the inbox. After a reboot though things are still present but not configured.
I don't want to configure each thing seperately but use the factoryhandler instead as a single point of configuration.
So far my code looks like this.
I added a config section to my binding.xml
...
<config-description>
<parameter name="username" type="text">
<label>Username</label>
<description>Username, used @ https://mypowerrouter.com</description>
<required>true</required>
</parameter>
<parameter name="password" type="text">
<label>Password</label>
<description>Password, used @ https://mypowerrouter.com</description>
<required>true</required>
</parameter>
</config-description>
And my factory:
public abstract class NedapPowerRouterHandler extends BaseThingHandler
...
@Override
protected void activate(ComponentContext componentContext) {
super.activate(componentContext);
Dictionary<String, Object> properties = componentContext.getProperties();
String user = (String) properties.get("username");
String pass = (String) properties.get("password");
if (StringUtils.isEmpty(user) || StringUtils.isEmpty(pass)) {
logger.warn("Username / Password needs to be configured!");
return;
}
logger.debug("Binding credentials updated with {} / {}", user, pass);
routerClient.setUsername(user);
routerClient.setPassword(pass);
try {
routerClient.login();
logger.debug("Login to mypowerrouter.com was {} ", routerClient.isUserLoggedIn());
} catch (ClientProtocolException e) {
return;
} catch (IOException e) {
return;
}
if (routerClient.isUserLoggedIn()) {
routerDiscovery.startManualScan();
} else {
logger.error("Wrong credentials provided or https://mypowerrouter.com not reachable");
}
};
...
Now this method is called each time I configure the binding but how do I read config date I set previously?
For Classes extending BaseThingHandler there is a getConfigAs() method available.
Is there something similiar for the factory or am I totally wrong and against the pattern which should normally be used?
Kind regards
Martin
|
|
|
|
|
Re: How to read / save Binding config [message #1775783 is a reply to message #1775591] |
Mon, 06 November 2017 05:12  |
Eclipse User |
|
|
|
Hi Martin,
first of all I´m looking forward to your contribution!
Regarding your question I will first try to describe your current problem with vanishing configurations and then suggest a solution which addresses the modelling of your binding and things:
When configuring a binding through PaperUI (which I assume you do) the values are handed over to OSGi ConfigAdmin and stored on disk. In the IDE setup however the OSGi runtime is started with the which will clean the ConfigAdmin settings on every startup.
From a modelling perspective you may implement a BridgeHandler and configure a Bridge thing with the credentials for a mypowerrouter.com account. The Bridge thing may then create an instance of the http client from the given credentials. Other ThingHandlers and the auto discovery may then use the http client from the bridge thing to communicate with mypowerrouter.com. This approach also allows users to connect to multiple mypowerrouter.com accounts from a single ESH instance by setting up multiple bridges.
Let me know if you have further questions. Cheers.
Henning
|
|
|
Powered by
FUDForum. Page generated in 0.04252 seconds