Skip to content

Commit

Permalink
[nikohomecontrol] Prepare for translation (openhab#11319)
Browse files Browse the repository at this point in the history
* Externalize strings to support translation

Signed-off-by: Mark Herwege <[email protected]>

* Name events thread.

Signed-off-by: Mark Herwege <[email protected]>

* Fix formatting

Signed-off-by: Mark Herwege <[email protected]>

* Fix SAT warning

Signed-off-by: Mark Herwege <[email protected]>

* Fix threadname

Signed-off-by: Mark Herwege <[email protected]>

* Fix thing lifecycle.

Signed-off-by: Mark Herwege <[email protected]>

* Adjustments from review.

Signed-off-by: Mark Herwege <[email protected]>

* Adjustment from review.

Signed-off-by: Mark Herwege <[email protected]>
  • Loading branch information
mherwege authored Oct 23, 2021
1 parent 4c1e5d6 commit cdebcc7
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class NikoHomeControlBindingConstants {

public static final String BINDING_ID = "nikohomecontrol";

// Listener threadname prefix
public static final String THREAD_NAME_PREFIX = "OH-binding-";

// List of all Thing Type UIDs

// bridge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public NikoHomeControlActionHandler(Thing thing) {
public void handleCommand(ChannelUID channelUID, Command command) {
NikoHomeControlCommunication nhcComm = getCommunication();
if (nhcComm == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Bridge communication not initialized when trying to execute action command " + actionId);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED,
"@text/offline.bridge-unitialized");
return;
}

Expand All @@ -92,7 +92,7 @@ private void handleCommandSelection(ChannelUID channelUID, Command command) {

logger.debug("handle command {} for {}", command, channelUID);

if (command == REFRESH) {
if (REFRESH.equals(command)) {
actionEvent(nhcAction.getState());
return;
}
Expand All @@ -107,15 +107,12 @@ private void handleCommandSelection(ChannelUID channelUID, Command command) {
handleBrightnessCommand(command);
updateStatus(ThingStatus.ONLINE);
break;

case CHANNEL_ROLLERSHUTTER:
handleRollershutterCommand(command);
updateStatus(ThingStatus.ONLINE);
break;

default:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Channel unknown " + channelUID.getId());
logger.debug("unexpected command for channel {}", channelUID.getId());
}
}

Expand Down Expand Up @@ -217,23 +214,25 @@ public void initialize() {

NikoHomeControlCommunication nhcComm = getCommunication();
if (nhcComm == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Connection with controller not started yet, could not initialize action " + actionId);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED,
"@text/offline.bridge-unitialized");
return;
} else {
updateStatus(ThingStatus.UNKNOWN);
}

// We need to do this in a separate thread because we may have to wait for the communication to become active
scheduler.submit(() -> {
if (!nhcComm.communicationActive()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"No connection with controller, could not initialize action " + actionId);
"@text/offline.communication-error");
return;
}

NhcAction nhcAction = nhcComm.getActions().get(actionId);
if (nhcAction == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Action " + actionId + " does not match an action in the controller");
"@text/offline.configuration-error.actionId");
return;
}

Expand Down Expand Up @@ -313,7 +312,7 @@ public void actionEvent(int actionState) {
break;
default:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Unknown action type " + actionType);
"@text/offline.configuration-error.actionType");
}
}

Expand All @@ -328,7 +327,7 @@ public void actionInitialized() {
@Override
public void actionRemoved() {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Action " + actionId + " has been removed from the controller");
"@text/offline.configuration-error.actionRemoved");
}

private void restartCommunication(NikoHomeControlCommunication nhcComm) {
Expand All @@ -337,35 +336,27 @@ private void restartCommunication(NikoHomeControlCommunication nhcComm) {
nhcComm.restartCommunication();
// If still not active, take thing offline and return.
if (!nhcComm.communicationActive()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication error");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/offline.communication-error");
return;
}
// Also put the bridge back online
NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler();
if (nhcBridgeHandler != null) {
nhcBridgeHandler.bridgeOnline();
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED,
"@text/offline.bridge-unitialized");
}
}

private @Nullable NikoHomeControlCommunication getCommunication() {
NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler();
if (nhcBridgeHandler == null) {
updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED,
"No bridge initialized for action " + actionId);
return null;
}
NikoHomeControlCommunication nhcComm = nhcBridgeHandler.getCommunication();
return nhcComm;
return nhcBridgeHandler != null ? nhcBridgeHandler.getCommunication() : null;
}

private @Nullable NikoHomeControlBridgeHandler getBridgeHandler() {
Bridge nhcBridge = getBridge();
if (nhcBridge == null) {
updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED,
"No bridge initialized for action " + actionId);
return null;
}
NikoHomeControlBridgeHandler nhcBridgeHandler = (NikoHomeControlBridgeHandler) nhcBridge.getHandler();
return nhcBridgeHandler;
return nhcBridge != null ? (NikoHomeControlBridgeHandler) nhcBridge.getHandler() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ protected void startCommunication() {
return;
}

updateStatus(ThingStatus.UNKNOWN);

scheduler.submit(() -> {
comm.startCommunication();
if (!comm.communicationActive()) {
Expand Down Expand Up @@ -141,7 +143,7 @@ private void setupRefreshTimer(int refreshInterval) {
*/
protected void bridgeOffline() {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
"Error with bridge connection");
"@text/offline.communication-error");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.binding.nikohomecontrol.internal.handler;

import static org.openhab.binding.nikohomecontrol.internal.NikoHomeControlBindingConstants.THREAD_NAME_PREFIX;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -50,11 +52,12 @@ public void initialize() {
logger.debug("bridge handler host {}, port {}", addr, port);

if (addr != null) {
nhcComm = new NikoHomeControlCommunication1(this, scheduler);
String eventThreadName = THREAD_NAME_PREFIX + thing.getUID().getAsString();
nhcComm = new NikoHomeControlCommunication1(this, scheduler, eventThreadName);
startCommunication();
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
"Cannot resolve bridge IP with hostname " + config.addr);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
"@text/offline.configuration-error.ip");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ public void initialize() {
// advanced configuration, skipping token validation.
// This behavior would allow the same logic to be used (with profile UUID) as before token validation
// was introduced.
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, "Token is empty");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
"@text/offline.configuration-error.tokenEmpty");
return;
}
} else {
Date now = new Date();
if (expiryDate.before(now)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
"Hobby api token has expired");
"@text/offline.configuration-error.tokenExpired");
return;
}
}
Expand All @@ -90,7 +91,7 @@ public void initialize() {
} catch (CertificateException e) {
// this should not happen unless there is a programming error
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
"Not able to set SSL context");
"@text/offline.communication-error");
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
return;
}

if (command == REFRESH) {
if (REFRESH.equals(command)) {
energyMeterEvent(nhcEnergyMeter.getPower());
}
}
Expand All @@ -77,24 +77,26 @@ public void initialize() {

NikoHomeControlCommunication nhcComm = getCommunication();
if (nhcComm == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Connection with controller not started yet, could not initialize energy meter " + energyMeterId);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED,
"@text/offline.bridge-unitialized");
return;
} else {
updateStatus(ThingStatus.UNKNOWN);
}

// We need to do this in a separate thread because we may have to wait for the
// communication to become active
scheduler.submit(() -> {
if (!nhcComm.communicationActive()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"No connection with controller, could not initialize energy meter " + energyMeterId);
"@text/offline.communication-error");
return;
}

NhcEnergyMeter nhcEnergyMeter = nhcComm.getEnergyMeters().get(energyMeterId);
if (nhcEnergyMeter == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Energy meter " + energyMeterId + " does not match a energy meter in the controller");
"@text/offline.configuration-error.energyMeterId");
return;
}

Expand Down Expand Up @@ -163,55 +165,48 @@ public void energyMeterInitialized() {
@Override
public void energyMeterRemoved() {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Energy meter " + energyMeterId + " has been removed from the controller");
"@text/offline.configuration-error.energyMeterRemoved");
}

@Override
// Subscribing to power readings starts an intensive data flow, therefore only do it when there is an item linked to
// the channel
public void channelLinked(ChannelUID channelUID) {
NikoHomeControlCommunication nhcComm = getCommunication();
if (nhcComm == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Bridge communication not initialized when trying to start energy meter " + energyMeterId);
return;
if (nhcComm != null) {
// This can be expensive, therefore do it in a job.
scheduler.submit(() -> {
if (!nhcComm.communicationActive()) {
restartCommunication(nhcComm);
}

if (nhcComm.communicationActive()) {
nhcComm.startEnergyMeter(energyMeterId);
updateStatus(ThingStatus.ONLINE);
}
});
}

// This can be expensive, therefore do it in a job.
scheduler.submit(() -> {
if (!nhcComm.communicationActive()) {
restartCommunication(nhcComm);
}

if (nhcComm.communicationActive()) {
nhcComm.startEnergyMeter(energyMeterId);
updateStatus(ThingStatus.ONLINE);
}
});
}

@Override
public void channelUnlinked(ChannelUID channelUID) {
NikoHomeControlCommunication nhcComm = getCommunication();
if (nhcComm == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Bridge communication not initialized when trying to stop energy meter " + energyMeterId);
return;
if (nhcComm != null) {
// This can be expensive, therefore do it in a job.
scheduler.submit(() -> {
if (!nhcComm.communicationActive()) {
restartCommunication(nhcComm);
}

if (nhcComm.communicationActive()) {
nhcComm.stopEnergyMeter(energyMeterId);
// as this is momentary power production/consumption, we set it UNDEF as we do not get readings
// anymore
updateState(CHANNEL_POWER, UnDefType.UNDEF);
updateStatus(ThingStatus.ONLINE);
}
});
}

// This can be expensive, therefore do it in a job.
scheduler.submit(() -> {
if (!nhcComm.communicationActive()) {
restartCommunication(nhcComm);
}

if (nhcComm.communicationActive()) {
nhcComm.stopEnergyMeter(energyMeterId);
// as this is momentary power production/consumption, we set it UNDEF as we do not get readings anymore
updateState(CHANNEL_POWER, UnDefType.UNDEF);
updateStatus(ThingStatus.ONLINE);
}
});
}

private void restartCommunication(NikoHomeControlCommunication nhcComm) {
Expand All @@ -220,35 +215,27 @@ private void restartCommunication(NikoHomeControlCommunication nhcComm) {
nhcComm.restartCommunication();
// If still not active, take thing offline and return.
if (!nhcComm.communicationActive()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication error");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/offline.communication-error");
return;
}
// Also put the bridge back online
NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler();
if (nhcBridgeHandler != null) {
nhcBridgeHandler.bridgeOnline();
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED,
"@text/offline.bridge-unitialized");
}
}

private @Nullable NikoHomeControlCommunication getCommunication() {
NikoHomeControlBridgeHandler nhcBridgeHandler = getBridgeHandler();
if (nhcBridgeHandler == null) {
updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED,
"No bridge initialized for energy meter " + energyMeterId);
return null;
}
NikoHomeControlCommunication nhcComm = nhcBridgeHandler.getCommunication();
return nhcComm;
return nhcBridgeHandler != null ? nhcBridgeHandler.getCommunication() : null;
}

private @Nullable NikoHomeControlBridgeHandler getBridgeHandler() {
Bridge nhcBridge = getBridge();
if (nhcBridge == null) {
updateStatus(ThingStatus.UNINITIALIZED, ThingStatusDetail.BRIDGE_UNINITIALIZED,
"No bridge initialized for energy meter " + energyMeterId);
return null;
}
NikoHomeControlBridgeHandler nhcBridgeHandler = (NikoHomeControlBridgeHandler) nhcBridge.getHandler();
return nhcBridgeHandler;
return nhcBridge != null ? (NikoHomeControlBridgeHandler) nhcBridge.getHandler() : null;
}
}
Loading

0 comments on commit cdebcc7

Please sign in to comment.