Search in sources :

Example 6 with CGateException

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

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

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

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

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

the class CBusGroupHandler method getGroup.

@Nullable
private Group getGroup() {
    try {
        CBusNetworkHandler networkHandler = cBusNetworkHandler;
        if (networkHandler == null) {
            return null;
        }
        Network network = networkHandler.getNetwork();
        if (network != null) {
            Application application = network.getApplication(applicationId);
            if (application == null) {
                logger.debug("getGroup() Cant get application for id {}", applicationId);
                return null;
            }
            logger.debug("GetGroup for {}/id {}", applicationId, groupId);
            return application.getGroup(groupId);
        }
    } catch (CGateException e) {
        logger.debug("GetGroup for id {}/{} failed {}", applicationId, groupId, e.getMessage());
    }
    return null;
}
Also used : Network(com.daveoxley.cbus.Network) Application(com.daveoxley.cbus.Application) CGateException(com.daveoxley.cbus.CGateException) Nullable(org.eclipse.jdt.annotation.Nullable)

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