Search in sources :

Example 1 with Network

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

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

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

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

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