Search in sources :

Example 1 with PowerDogLocalApiBindingProvider

use of org.openhab.binding.powerdoglocalapi.PowerDogLocalApiBindingProvider in project openhab1-addons by openhab.

the class PowerDogLocalApiBinding method internalReceiveUpdate.

/**
     * @{inheritDoc
     */
@Override
protected void internalReceiveUpdate(String itemName, State newState) {
    logger.debug("internalReceiveUpdate({},{}) is called!", itemName, newState);
    // cycle on all available powerdogs
    for (PowerDogLocalApiBindingProvider provider : providers) {
        if (!provider.providesBindingFor(itemName)) {
            continue;
        }
        // only in case of an outbinding, this need to be handled
        if (provider.getOutBindingItemNames().contains(itemName)) {
            // check if item may send update already now
            // time indicated in config is the minimum time between two
            // updates
            Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
            if (lastUpdateTimeStamp == null) {
                lastUpdateTimeStamp = 0L;
            }
            long age = System.currentTimeMillis() - lastUpdateTimeStamp;
            boolean itemMayUpdate = (age >= provider.getRefreshInterval(itemName));
            if (itemMayUpdate) {
                // Convert new State to PowerDog set Current_Value string
                String value = "0";
                if (newState instanceof OnOffType) {
                    if (newState == OnOffType.ON) {
                        value = "1";
                    }
                } else // the best solution, but it is sufficient
                if (newState instanceof OpenClosedType) {
                    if (newState == OpenClosedType.OPEN) {
                        value = "1";
                    }
                } else // see comment above
                if (newState instanceof PercentType) {
                    value = newState.toString();
                } else if (newState instanceof DecimalType) {
                    value = newState.toString();
                }
                // Get the unit serverId from the binding, and relate that
                // to the config
                String unit = provider.getServerId(itemName);
                PowerDogLocalApiServerConfig server = serverList.get(unit);
                try {
                    logger.debug("PowerDogLocalApi sending to PowerDog");
                    // Perform XML RPC call
                    PowerDog powerdog = (PowerDog) XmlRpcProxy.createProxy(server.url(), "", new Class[] { PowerDog.class }, false);
                    XmlRpcStruct response = powerdog.setLinearSensorDevice(server.password, provider.getValueId(itemName), value);
                    lastUpdateMap.put(itemName, System.currentTimeMillis());
                    logger.debug("PowerDog.setLinearSensorDevice() result: {}", response.toString());
                } catch (Exception e) {
                    logger.warn("PowerDogLocalApi sending to PowerDog failed");
                    logger.warn(e.getMessage());
                }
            }
        }
    }
}
Also used : PowerDogLocalApiBindingProvider(org.openhab.binding.powerdoglocalapi.PowerDogLocalApiBindingProvider) PercentType(org.openhab.core.library.types.PercentType) MalformedURLException(java.net.MalformedURLException) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DecimalType(org.openhab.core.library.types.DecimalType) XmlRpcStruct(redstone.xmlrpc.XmlRpcStruct)

Example 2 with PowerDogLocalApiBindingProvider

use of org.openhab.binding.powerdoglocalapi.PowerDogLocalApiBindingProvider in project openhab1-addons by openhab.

the class PowerDogLocalApiBinding method execute.

/**
     * @{inheritDoc
     */
@Override
protected void execute() {
    logger.debug("execute() method is called!");
    // cycle over all available powerdogs
    for (PowerDogLocalApiBindingProvider provider : providers) {
        /*
             * TODO: check if reading of out-bindings is useful - currently it
             * seems to be applicable only for openhab init (but it works
             * without, so I give it a try) and in case several clients are
             * writing to the same PowerAPI, which is risky due to concurrency
             */
        for (String itemName : provider.getInBindingItemNames()) {
            // get item specific refresh interval
            int refreshInterval = provider.getRefreshInterval(itemName);
            // check if item needs update
            Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
            if (lastUpdateTimeStamp == null) {
                lastUpdateTimeStamp = 0L;
            }
            long age = System.currentTimeMillis() - lastUpdateTimeStamp;
            boolean itemNeedsUpdate = (age >= refreshInterval);
            if (itemNeedsUpdate) {
                logger.debug("Item '{}' is about to be refreshed now", itemName);
                // Get the unit serverId from the binding, and relate that
                // to the config
                String unit = provider.getServerId(itemName);
                PowerDogLocalApiServerConfig server = serverList.get(unit);
                // get the server specific update time and check if it needs
                // an update
                boolean serverNeedsUpdate = false;
                if (server == null) {
                    serverNeedsUpdate = false;
                    logger.error("Unknown PowerDog server referenced: {}", unit);
                    continue;
                } else {
                    age = System.currentTimeMillis() - server.lastUpdate;
                    serverNeedsUpdate = (age >= server.refresh);
                }
                XmlRpcStruct response = loadPowerDogResponse(serverNeedsUpdate, server);
                // update item state
                if (response != null) {
                    String value = getVariable(response, provider.getValueId(itemName), provider.getName(itemName));
                    if (value != null) {
                        Class<? extends Item> itemType = provider.getItemType(itemName);
                        State state = createState(itemType, value);
                        eventPublisher.postUpdate(itemName, state);
                        lastUpdateMap.put(itemName, System.currentTimeMillis());
                    }
                }
            }
        }
    }
    logger.debug("execute() method is finished!");
}
Also used : State(org.openhab.core.types.State) PowerDogLocalApiBindingProvider(org.openhab.binding.powerdoglocalapi.PowerDogLocalApiBindingProvider) XmlRpcStruct(redstone.xmlrpc.XmlRpcStruct)

Aggregations

PowerDogLocalApiBindingProvider (org.openhab.binding.powerdoglocalapi.PowerDogLocalApiBindingProvider)2 XmlRpcStruct (redstone.xmlrpc.XmlRpcStruct)2 MalformedURLException (java.net.MalformedURLException)1 DecimalType (org.openhab.core.library.types.DecimalType)1 OnOffType (org.openhab.core.library.types.OnOffType)1 OpenClosedType (org.openhab.core.library.types.OpenClosedType)1 PercentType (org.openhab.core.library.types.PercentType)1 State (org.openhab.core.types.State)1