Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse SmartHome » How to read / save Binding config(How to read / save Binding config)
How to read / save Binding config [message #1775395] Sun, 29 October 2017 12:36 Go to next message
Martin K is currently offline Martin KFriend
Messages: 2
Registered: October 2017
Junior Member
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 #1775439 is a reply to message #1775395] Mon, 30 October 2017 08:57 Go to previous messageGo to next message
Kai Kreuzer is currently offline Kai KreuzerFriend
Messages: 661
Registered: December 2011
Senior Member
I do not really understand your code. The snippet above "extends BaseThingHandler" and "overrides activate", a method that does not exist at all in BaseThingHandler.

All there is to do is to implement "initialize()" in your handler and check the credentials in there. If all is fine, go ahead and discover other devices, add channels to the current Thing or do whatever you like. If the credentials are incorrect, set the Thing status to OFFLINE / configuration error.
Re: How to read / save Binding config [message #1775591 is a reply to message #1775439] Wed, 01 November 2017 21:20 Go to previous messageGo to next message
Martin K is currently offline Martin KFriend
Messages: 2
Registered: October 2017
Junior Member
Hi,

thank you for your fast response.
I made a mistake during copy and paste..
It's meant to be
public class NedapPowerRouterHandlerFactory extends BaseThingHandlerFactory


Trying to explain my problem again with different words:


  1. Binding is loaded the very first time (no credentials for https://mypowerrouter.com are set) My autodiscovery service will stop immediately since no credentials are provided so far.
  2. Once credentials are entered in the binding a connection to the previously mentioned website will be established and available features are checked
  3. Depending on the features (some people have batteries attached to their solar panels, some don't..) the discovery service will be started again and things are added accordingly to the inbox


It works fine this way. But after I restart project within the Dev-Environment (eclipse ide)
Things are still present but the binding lost the previously entered credentials...
Since my NedapPowerRouterHandlerFactory contains the httpclient which establishes the connection to gather data, all things won't be able to retrieve any data.
I use only one http-connection for all things which is located in
the HandlerFactory
All scheduled jobs from the things which retrieve data just call
    public static NedapHttpClient getRouterClient() {
        return routerClient;
    }

and work on this instance.

Thats why I ask for how to save and load configuration settings within the
HandlerFactory and not the ThingHandler.

Hopefully I explained my problem a bit more precise.
Sorry for asking such dumb questions but I want to share my work with the community and I don't want the user to configure credentials in each thing seperatly

Best
Martin
Re: How to read / save Binding config [message #1775783 is a reply to message #1775591] Mon, 06 November 2017 10:12 Go to previous message
Henning Treu is currently offline Henning TreuFriend
Messages: 42
Registered: April 2017
Member
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
-Dosgi.clean=true
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
Previous Topic:Stuck on finalizing installation
Next Topic:Questions about best practices implementing bindings
Goto Forum:
  


Current Time: Sat Nov 17 12:39:29 GMT 2018

Powered by FUDForum. Page generated in 0.02219 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top