Search in sources :

Example 1 with CGateException

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

the class CBusDaliHandler 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_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_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("Cannot 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 2 with CGateException

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

the class CBusNetworkHandler method checkNetworkOnline.

private void checkNetworkOnline() {
    Network network = getNetwork();
    try {
        if (network != null && network.isOnline()) {
            logger.debug("Network is online");
            ScheduledFuture<?> initNetwork = this.initNetwork;
            if (initNetwork != null) {
                initNetwork.cancel(false);
                this.initNetwork = null;
            }
        } else {
            ThingStatus lastStatus = getThing().getStatus();
            logger.debug("Network still not online {}", lastStatus);
        }
    } catch (CGateException e) {
        logger.warn("Cannot check if network is online {} ", network.getNetworkID());
    }
    updateStatus();
}
Also used : Network(com.daveoxley.cbus.Network) ThingStatus(org.openhab.core.thing.ThingStatus) CGateException(com.daveoxley.cbus.CGateException)

Example 3 with CGateException

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

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

the class CBusNetworkHandler method doNetworkSync.

private void doNetworkSync() {
    Network network = getNetwork();
    try {
        if (getThing().getStatus().equals(ThingStatus.ONLINE) && network != null) {
            logger.info("Starting network sync on network {}", network.getNetworkID());
            network.startSync();
        }
    } catch (CGateException e) {
        logger.warn("Cannot start network sync on network {} - {}", network.getNetworkID(), e.getMessage());
    }
}
Also used : Network(com.daveoxley.cbus.Network) CGateException(com.daveoxley.cbus.CGateException)

Example 5 with CGateException

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

the class CBusTemperatureHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    // Read only thing - no commands to handle
    if (command instanceof RefreshType) {
        try {
            Group group = this.group;
            if (group != null) {
                int level = group.getLevel();
                logger.debug("handle RefreshType Command for Chanell {} Group {} level {}", channelUID, groupId, level);
                if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_TEMP)) {
                    updateState(channelUID, new QuantityType<>(level, CELSIUS));
                }
            }
        } catch (CGateException e) {
            logger.debug("Failed to getLevel for group {}", groupId, e);
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
        }
    }
}
Also used : Group(com.daveoxley.cbus.Group) RefreshType(org.openhab.core.types.RefreshType) 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