Search in sources :

Example 1 with Group

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

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

Example 3 with Group

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

the class CBusGroupDiscovery method startScan.

@Override
protected synchronized void startScan() {
    if (cbusNetworkHandler.getThing().getStatus().equals(ThingStatus.ONLINE)) {
        ThingUID bridgeUid = cbusNetworkHandler.getThing().getBridgeUID();
        if (bridgeUid == null) {
            scanFinished();
            return;
        }
        try {
            Map<Integer, ThingTypeUID> applications = new HashMap<Integer, ThingTypeUID>();
            applications.put(CBusBindingConstants.CBUS_APPLICATION_LIGHTING, CBusBindingConstants.THING_TYPE_LIGHT);
            applications.put(CBusBindingConstants.CBUS_APPLICATION_DALI, CBusBindingConstants.THING_TYPE_DALI);
            applications.put(CBusBindingConstants.CBUS_APPLICATION_TEMPERATURE, CBusBindingConstants.THING_TYPE_TEMPERATURE);
            applications.put(CBusBindingConstants.CBUS_APPLICATION_TRIGGER, CBusBindingConstants.THING_TYPE_TRIGGER);
            Network network = cbusNetworkHandler.getNetwork();
            if (network == null) {
                scanFinished();
                return;
            }
            for (Map.Entry<Integer, ThingTypeUID> applicationItem : applications.entrySet()) {
                Application application = network.getApplication(applicationItem.getKey());
                if (application == null) {
                    continue;
                }
                ArrayList<Group> groups = application.getGroups(false);
                for (Group group : groups) {
                    logger.debug("Found group: {} {} {}", application.getName(), group.getGroupID(), group.getName());
                    Map<String, Object> properties = new HashMap<>();
                    properties.put(CBusBindingConstants.PROPERTY_APPLICATION_ID, Integer.toString(applicationItem.getKey()));
                    properties.put(CBusBindingConstants.CONFIG_GROUP_ID, Integer.toString(group.getGroupID()));
                    properties.put(CBusBindingConstants.PROPERTY_GROUP_NAME, group.getName());
                    properties.put(CBusBindingConstants.PROPERTY_NETWORK_ID, Integer.toString(network.getNetworkID()));
                    ThingUID uid = new ThingUID(applicationItem.getValue(), Integer.toString(group.getGroupID()), bridgeUid.getId(), cbusNetworkHandler.getThing().getUID().getId());
                    DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel("CBUS " + group.getName() + "(" + group.getGroupID() + ")").withBridge(cbusNetworkHandler.getThing().getUID()).build();
                    thingDiscovered(result);
                }
            }
        } catch (CGateException e) {
            logger.debug("Failed to discover groups", e);
        }
    }
    scanFinished();
}
Also used : Group(com.daveoxley.cbus.Group) HashMap(java.util.HashMap) CGateException(com.daveoxley.cbus.CGateException) DiscoveryResult(org.openhab.core.config.discovery.DiscoveryResult) ThingUID(org.openhab.core.thing.ThingUID) Network(com.daveoxley.cbus.Network) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) HashMap(java.util.HashMap) Map(java.util.Map) Application(com.daveoxley.cbus.Application)

Example 4 with Group

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

the class CBusGroupHandler method updateStatus.

public void updateStatus() {
    try {
        logger.debug("updateStatus UID: {} applicaton: {} group: {}", getThing().getUID(), applicationId, groupId);
        CBusNetworkHandler networkHandler = cBusNetworkHandler;
        if (networkHandler == null || !networkHandler.getThing().getStatus().equals(ThingStatus.ONLINE)) {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
        } else {
            Group group = this.group;
            if (group == null) {
                group = getGroup();
                this.group = group;
            }
            if (group == null) {
                logger.debug("Set state to configuration error -no group");
                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No Group object available");
            } else if (group.getNetwork().isOnline()) {
                updateStatus(ThingStatus.ONLINE);
                try {
                    Map<String, String> updatedProperties = editProperties();
                    updatedProperties.put(CBusBindingConstants.PROPERTY_GROUP_NAME, group.getName());
                    updateProperties(updatedProperties);
                } catch (CGateException ignore) {
                // Cant get name so properties wont be updated
                }
            } else {
                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Network is not reporting online");
            }
        }
    } catch (CGateException e) {
        logger.debug("Problem checking network state for network {}", e.getMessage());
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
    }
}
Also used : Group(com.daveoxley.cbus.Group) Map(java.util.Map) CGateException(com.daveoxley.cbus.CGateException)

Example 5 with Group

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

Aggregations

CGateException (com.daveoxley.cbus.CGateException)5 Group (com.daveoxley.cbus.Group)5 RefreshType (org.openhab.core.types.RefreshType)3 Map (java.util.Map)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 Application (com.daveoxley.cbus.Application)1 Network (com.daveoxley.cbus.Network)1 HashMap (java.util.HashMap)1 DiscoveryResult (org.openhab.core.config.discovery.DiscoveryResult)1 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)1 ThingUID (org.openhab.core.thing.ThingUID)1