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");
}
}
}
}
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();
}
}
}
}
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);
}
}
}
Aggregations