Search in sources :

Example 1 with CBusNetworkConfiguration

use of org.openhab.binding.cbus.internal.CBusNetworkConfiguration in project openhab-addons by openhab.

the class CBusNetworkHandler method cgateOnline.

private void cgateOnline() {
    CBusNetworkConfiguration configuration = this.configuration;
    if (configuration == null) {
        logger.debug("cgateOnline - NetworkHandler not initialised");
        return;
    }
    ThingStatus lastStatus = getThing().getStatus();
    logger.debug("cgateOnline {}", lastStatus);
    Integer networkID = configuration.id;
    String project = configuration.project;
    logger.debug("cgateOnline netid {} project {}", networkID, project);
    Project projectObject = getProjectObject();
    Network network = getNetwork();
    logger.debug("network {}", network);
    CBusCGateHandler cbusCGateHandler = getCBusCGateHandler();
    if (cbusCGateHandler == null) {
        logger.debug("NoCGateHandler");
        return;
    }
    try {
        if (projectObject == null) {
            CGateSession session = cbusCGateHandler.getCGateSession();
            if (session != null) {
                try {
                    projectObject = (Project) session.getCGateObject("//" + project);
                    this.projectObject = projectObject;
                } catch (CGateException ignore) {
                // We dont need to do anything other than stop this propagating
                }
            }
            if (projectObject == null) {
                logger.debug("Cant get projectobject");
                return;
            }
        }
        if (network == null) {
            CGateSession session = cbusCGateHandler.getCGateSession();
            if (session != null) {
                try {
                    network = (Network) session.getCGateObject("//" + project + "/" + networkID);
                    this.network = network;
                } catch (CGateException ignore) {
                // We dont need to do anything other than stop this propagating
                }
            }
            if (network == null) {
                logger.debug("cgateOnline: Cant get network");
                return;
            }
        }
        String state = network.getState();
        logger.debug("Network state is {}", state);
        if ("new".equals(state)) {
            projectObject.start();
            logger.debug("Need to wait for it to be synced");
        } else if ("sync".equals(state)) {
            logger.debug("Network is syncing so wait for it to be ok");
        }
        if (!"ok".equals(state)) {
            ScheduledFuture<?> initNetwork = this.initNetwork;
            if (initNetwork == null || initNetwork.isCancelled()) {
                this.initNetwork = scheduler.scheduleWithFixedDelay(this::checkNetworkOnline, 30, 30, TimeUnit.SECONDS);
                logger.debug("Schedule a check every minute");
            } else {
                logger.debug("initNetwork alreadys started");
            }
            updateStatus();
            return;
        }
    } catch (CGateException e) {
        logger.warn("Cannot load C-Bus network {}", networkID, e);
        updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.COMMUNICATION_ERROR);
    }
    updateStatus();
}
Also used : Project(com.daveoxley.cbus.Project) ThingStatus(org.openhab.core.thing.ThingStatus) Network(com.daveoxley.cbus.Network) CGateSession(com.daveoxley.cbus.CGateSession) CBusNetworkConfiguration(org.openhab.binding.cbus.internal.CBusNetworkConfiguration) CGateException(com.daveoxley.cbus.CGateException)

Example 2 with CBusNetworkConfiguration

use of org.openhab.binding.cbus.internal.CBusNetworkConfiguration in project openhab-addons by openhab.

the class CBusNetworkHandler method updateStatus.

private void updateStatus() {
    CBusNetworkConfiguration configuration = this.configuration;
    if (configuration == null) {
        logger.debug("updateStatus - NetworkHandler not initialised");
        return;
    }
    ThingStatus lastStatus = getThing().getStatus();
    Network network = getNetwork();
    CBusCGateHandler cbusCGateHandler = getCBusCGateHandler();
    try {
        if (cbusCGateHandler == null || !cbusCGateHandler.getThing().getStatus().equals(ThingStatus.ONLINE)) {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "CGate connection offline");
        } else if (network == null) {
            logger.debug("No network - set configuration error");
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No Network object available");
        } else if (network.isOnline()) {
            updateStatus(ThingStatus.ONLINE);
        } else {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Network is not reporting online");
        }
    } catch (CGateException e) {
        logger.warn("Problem checking network state for network {}", network.getNetworkID(), e);
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
    }
    if (!getThing().getStatus().equals(lastStatus)) {
        ScheduledFuture<?> networkSync = this.networkSync;
        if (lastStatus == ThingStatus.OFFLINE) {
            if (networkSync == null || networkSync.isCancelled()) {
                this.networkSync = scheduler.scheduleWithFixedDelay(this::doNetworkSync, 10, configuration.syncInterval, TimeUnit.SECONDS);
            }
        } else {
            if (networkSync != null) {
                networkSync.cancel(false);
            }
        }
        for (Thing thing : getThing().getThings()) {
            ThingHandler handler = thing.getHandler();
            if (handler instanceof CBusGroupHandler) {
                ((CBusGroupHandler) handler).updateStatus();
            }
        }
    }
}
Also used : ThingStatus(org.openhab.core.thing.ThingStatus) Network(com.daveoxley.cbus.Network) ThingHandler(org.openhab.core.thing.binding.ThingHandler) CBusNetworkConfiguration(org.openhab.binding.cbus.internal.CBusNetworkConfiguration) Thing(org.openhab.core.thing.Thing) CGateException(com.daveoxley.cbus.CGateException)

Aggregations

CGateException (com.daveoxley.cbus.CGateException)2 Network (com.daveoxley.cbus.Network)2 CBusNetworkConfiguration (org.openhab.binding.cbus.internal.CBusNetworkConfiguration)2 ThingStatus (org.openhab.core.thing.ThingStatus)2 CGateSession (com.daveoxley.cbus.CGateSession)1 Project (com.daveoxley.cbus.Project)1 Thing (org.openhab.core.thing.Thing)1 ThingHandler (org.openhab.core.thing.binding.ThingHandler)1