Search in sources :

Example 1 with CGateSession

use of com.daveoxley.cbus.CGateSession 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 CGateSession

use of com.daveoxley.cbus.CGateSession in project openhab-addons by openhab.

the class CBusCGateHandler method connect.

private void connect() {
    CGateSession cGateSession = this.cGateSession;
    if (cGateSession == null) {
        cGateSession = CGateInterface.connect(this.ipAddress, 20023, 20024, 20025, new CBusThreadPool());
        cGateSession.registerEventCallback(new EventMonitor());
        cGateSession.registerStatusChangeCallback(new StatusChangeMonitor());
        this.cGateSession = cGateSession;
    }
    if (cGateSession.isConnected()) {
        logger.debug("CGate session reports online");
        updateStatus(ThingStatus.ONLINE);
    } else {
        try {
            cGateSession.connect();
            updateStatus();
        } catch (CGateConnectException e) {
            updateStatus();
            logger.debug("Failed to connect to CGate:", e);
            try {
                cGateSession.close();
            } catch (CGateException ignore) {
            // We dont really care if an exception is thrown when clossing the connection after a failure
            // connecting.
            }
        }
    }
}
Also used : CGateConnectException(com.daveoxley.cbus.CGateConnectException) CGateSession(com.daveoxley.cbus.CGateSession) CBusThreadPool(org.openhab.binding.cbus.internal.CBusThreadPool) CGateException(com.daveoxley.cbus.CGateException)

Example 3 with CGateSession

use of com.daveoxley.cbus.CGateSession in project openhab-addons by openhab.

the class CBusCGateHandler method dispose.

@Override
public void dispose() {
    ScheduledFuture<?> keepAliveFuture = this.keepAliveFuture;
    if (keepAliveFuture != null) {
        keepAliveFuture.cancel(true);
    }
    CGateSession cGateSession = this.cGateSession;
    if (cGateSession != null && cGateSession.isConnected()) {
        try {
            cGateSession.close();
        } catch (CGateException e) {
            logger.warn("Cannot close CGate session", e);
        }
    } else {
        logger.debug("no session or it is disconnected");
    }
    super.dispose();
}
Also used : CGateSession(com.daveoxley.cbus.CGateSession) CGateException(com.daveoxley.cbus.CGateException)

Example 4 with CGateSession

use of com.daveoxley.cbus.CGateSession in project openhab-addons by openhab.

the class CBusCGateHandler method updateStatus.

private void updateStatus() {
    ThingStatus lastStatus = getThing().getStatus();
    CGateSession cGateSession = this.cGateSession;
    if (cGateSession == null) {
        return;
    }
    if (cGateSession.isConnected()) {
        updateStatus(ThingStatus.ONLINE);
    } else {
        if (lastStatus != ThingStatus.OFFLINE) {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
        }
    }
    if (!getThing().getStatus().equals(lastStatus)) {
        boolean isOnline = getThing().getStatus().equals(ThingStatus.ONLINE);
        updateChildThings(isOnline);
    }
}
Also used : ThingStatus(org.openhab.core.thing.ThingStatus) CGateSession(com.daveoxley.cbus.CGateSession)

Aggregations

CGateSession (com.daveoxley.cbus.CGateSession)4 CGateException (com.daveoxley.cbus.CGateException)3 ThingStatus (org.openhab.core.thing.ThingStatus)2 CGateConnectException (com.daveoxley.cbus.CGateConnectException)1 Network (com.daveoxley.cbus.Network)1 Project (com.daveoxley.cbus.Project)1 CBusNetworkConfiguration (org.openhab.binding.cbus.internal.CBusNetworkConfiguration)1 CBusThreadPool (org.openhab.binding.cbus.internal.CBusThreadPool)1