Search in sources :

Example 6 with Network

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

Example 7 with Network

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

the class CBusNetworkDiscovery method startScan.

@Override
protected void startScan() {
    if (cBusCGateHandler.getThing().getStatus().equals(ThingStatus.ONLINE)) {
        try {
            ArrayList<Network> networks = Network.listAll(cBusCGateHandler.getCGateSession(), false);
            for (Network network : networks) {
                logger.debug("Found Network: {} {}", network.getNetworkID(), network.getName());
                Map<String, Object> properties = new HashMap<>(3);
                properties.put(CBusBindingConstants.CONFIG_NETWORK_ID, network.getNetworkID());
                properties.put(CBusBindingConstants.PROPERTY_NETWORK_NAME, network.getName());
                properties.put(CBusBindingConstants.CONFIG_NETWORK_PROJECT, network.getProjectName());
                ThingUID uid = new ThingUID(CBusBindingConstants.BRIDGE_TYPE_NETWORK, network.getProjectName().toLowerCase().replace(" ", "_") + network.getNetworkID(), cBusCGateHandler.getThing().getUID().getId());
                DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(network.getProjectName() + "/" + network.getNetworkID() + " - " + network.getName()).withBridge(cBusCGateHandler.getThing().getUID()).build();
                thingDiscovered(result);
            }
        } catch (CGateException e) {
            logger.warn("Failed to discover networks", e);
        }
    }
}
Also used : DiscoveryResult(org.openhab.core.config.discovery.DiscoveryResult) HashMap(java.util.HashMap) Network(com.daveoxley.cbus.Network) ThingUID(org.openhab.core.thing.ThingUID) CGateException(com.daveoxley.cbus.CGateException)

Aggregations

CGateException (com.daveoxley.cbus.CGateException)7 Network (com.daveoxley.cbus.Network)7 ThingStatus (org.openhab.core.thing.ThingStatus)3 Application (com.daveoxley.cbus.Application)2 HashMap (java.util.HashMap)2 CBusNetworkConfiguration (org.openhab.binding.cbus.internal.CBusNetworkConfiguration)2 DiscoveryResult (org.openhab.core.config.discovery.DiscoveryResult)2 ThingUID (org.openhab.core.thing.ThingUID)2 CGateSession (com.daveoxley.cbus.CGateSession)1 Group (com.daveoxley.cbus.Group)1 Project (com.daveoxley.cbus.Project)1 Map (java.util.Map)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 Thing (org.openhab.core.thing.Thing)1 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)1 ThingHandler (org.openhab.core.thing.binding.ThingHandler)1