Search in sources :

Example 11 with CGateException

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

the class CBusLightHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    Group group = this.group;
    if (group == null) {
        return;
    }
    if (command instanceof RefreshType) {
        try {
            int level = group.getLevel();
            logger.debug("handle RefreshType Command for Chanell {} Group {} level {}", channelUID, groupId, level);
            if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_STATE)) {
                updateState(channelUID, (level > 0) ? OnOffType.ON : OnOffType.OFF);
            } else if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_LEVEL)) {
                updateState(channelUID, new PercentType((int) (level * 100 / 255.0)));
            }
        } catch (CGateException e) {
            logger.debug("Failed to getLevel for group {}", groupId, e);
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
        }
    } else {
        if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_STATE)) {
            logger.debug("Channel State command for {}: {}", channelUID, command);
            if (command instanceof OnOffType) {
                try {
                    if (command == OnOffType.ON) {
                        group.on();
                    } else if (command == OnOffType.OFF) {
                        group.off();
                    }
                } catch (CGateException e) {
                    logger.debug("Failed to send command {} to {}", command, group, e);
                    updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
                }
            }
        } else if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_LEVEL)) {
            logger.debug("Channel Level command for {}: {}", channelUID, command);
            try {
                if (command instanceof OnOffType) {
                    if (command == OnOffType.ON) {
                        group.on();
                    } else if (command == OnOffType.OFF) {
                        group.off();
                    }
                } else if (command instanceof PercentType) {
                    PercentType value = (PercentType) command;
                    group.ramp((int) Math.round(value.doubleValue() / 100 * 255), 0);
                } else if (command instanceof IncreaseDecreaseType) {
                    int level = group.getLevel();
                    if (command == IncreaseDecreaseType.DECREASE) {
                        level = Math.max(level - 1, 0);
                    } else if (command == IncreaseDecreaseType.INCREASE) {
                        level = Math.min(level + 1, 255);
                    }
                    group.ramp(level, 0);
                    logger.debug("Change group level to {}", level);
                }
            } catch (CGateException e) {
                logger.debug("Failed to send command {} to {}", command, group, e);
                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
            }
        }
    }
}
Also used : Group(com.daveoxley.cbus.Group) OnOffType(org.openhab.core.library.types.OnOffType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) PercentType(org.openhab.core.library.types.PercentType) RefreshType(org.openhab.core.types.RefreshType) CGateException(com.daveoxley.cbus.CGateException)

Example 12 with CGateException

use of com.daveoxley.cbus.CGateException 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 13 with CGateException

use of com.daveoxley.cbus.CGateException 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)13 Network (com.daveoxley.cbus.Network)7 Group (com.daveoxley.cbus.Group)5 CGateSession (com.daveoxley.cbus.CGateSession)3 ThingStatus (org.openhab.core.thing.ThingStatus)3 RefreshType (org.openhab.core.types.RefreshType)3 Application (com.daveoxley.cbus.Application)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CBusNetworkConfiguration (org.openhab.binding.cbus.internal.CBusNetworkConfiguration)2 DiscoveryResult (org.openhab.core.config.discovery.DiscoveryResult)2 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)2 OnOffType (org.openhab.core.library.types.OnOffType)2 PercentType (org.openhab.core.library.types.PercentType)2 ThingUID (org.openhab.core.thing.ThingUID)2 CGateConnectException (com.daveoxley.cbus.CGateConnectException)1 Project (com.daveoxley.cbus.Project)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 CBusThreadPool (org.openhab.binding.cbus.internal.CBusThreadPool)1 Thing (org.openhab.core.thing.Thing)1