Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nikobus] fixed refresh handling #9114

Merged
merged 3 commits into from Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,18 +14,17 @@

import static org.openhab.binding.nikobus.internal.NikobusBindingConstants.CHANNEL_OUTPUT_PREFIX;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.nikobus.internal.protocol.NikobusCommand;
import org.openhab.binding.nikobus.internal.protocol.SwitchModuleCommandFactory;
import org.openhab.binding.nikobus.internal.protocol.SwitchModuleGroup;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
Expand All @@ -46,12 +45,21 @@ abstract class NikobusModuleHandler extends NikobusBaseThingHandler {
private final EnumSet<SwitchModuleGroup> pendingRefresh = EnumSet.noneOf(SwitchModuleGroup.class);
private final Logger logger = LoggerFactory.getLogger(NikobusModuleHandler.class);
private final Map<String, Integer> cachedStates = new HashMap<>();
private final List<ChannelUID> linkedChannels = new ArrayList<>();

protected NikobusModuleHandler(Thing thing) {
super(thing);
}

@Override
public void initialize() {
super.initialize();

if (thing.getStatus() != ThingStatus.OFFLINE) {
// Fetch all linked channels to get initial values.
thing.getChannels().forEach(channel -> refreshChannel(channel.getUID()));
}
}

@Override
public void dispose() {
super.dispose();
Expand All @@ -67,6 +75,8 @@ public void dispose() {

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("handleCommand '{}' for channel '{}'", command, channelUID.getId());

if (command instanceof RefreshType) {
refreshChannel(channelUID);
} else {
Expand All @@ -85,26 +95,11 @@ private void refreshChannel(ChannelUID channelUID) {
updateGroup(SwitchModuleGroup.mapFromChannel(channelUID));
}

@Override
public void channelLinked(ChannelUID channelUID) {
synchronized (linkedChannels) {
linkedChannels.add(channelUID);
}
super.channelLinked(channelUID);
}

@Override
public void channelUnlinked(ChannelUID channelUID) {
synchronized (linkedChannels) {
linkedChannels.remove(channelUID);
}
super.channelUnlinked(channelUID);
}

public void refreshModule() {
Set<SwitchModuleGroup> groups = new HashSet<>();
synchronized (linkedChannels) {
for (ChannelUID channelUID : linkedChannels) {
for (Channel channel : thing.getChannels()) {
ChannelUID channelUID = channel.getUID();
if (isLinked(channelUID)) {
groups.add(SwitchModuleGroup.mapFromChannel(channelUID));
}
}
Expand Down
Expand Up @@ -118,27 +118,41 @@ public void initialize() {

impactedModules.clear();

try {
ThingUID bridgeUID = thing.getBridgeUID();
if (bridgeUID == null) {
throw new IllegalArgumentException("Bridge does not exist!");
Object impactedModulesObject = getConfig().get(CONFIG_IMPACTED_MODULES);
if (impactedModulesObject != null) {
try {
Bridge bridge = getBridge();
if (bridge == null) {
throw new IllegalArgumentException("Bridge does not exist!");
}

ThingUID bridgeUID = thing.getBridgeUID();
if (bridgeUID == null) {
throw new IllegalArgumentException("Unable to read BridgeUID!");
}

String[] impactedModulesString = impactedModulesObject.toString().split(",");
for (String impactedModuleString : impactedModulesString) {
ImpactedModuleUID impactedModuleUID = new ImpactedModuleUID(impactedModuleString.trim());
ThingTypeUID thingTypeUID = new ThingTypeUID(bridgeUID.getBindingId(),
impactedModuleUID.getThingTypeId());
ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, impactedModuleUID.getThingId());

if (!bridge.getThings().stream().anyMatch(thing -> thing.getUID().equals(thingUID))) {
throw new IllegalArgumentException(
"Impacted module " + thingUID + " not found for '" + impactedModuleString + "'");
}

impactedModules.add(new ImpactedModule(thingUID, impactedModuleUID.getGroup()));
}
} catch (RuntimeException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
return;
}

String[] impactedModulesString = getConfig().get(CONFIG_IMPACTED_MODULES).toString().split(",");
for (String impactedModuleString : impactedModulesString) {
ImpactedModuleUID impactedModuleUID = new ImpactedModuleUID(impactedModuleString.trim());
ThingTypeUID thingTypeUID = new ThingTypeUID(bridgeUID.getBindingId(),
impactedModuleUID.getThingTypeId());
ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, impactedModuleUID.getThingId());
impactedModules.add(new ImpactedModule(thingUID, impactedModuleUID.getGroup()));
}
} catch (RuntimeException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
return;
logger.debug("Impacted modules for {} = {}", thing.getUID(), impactedModules);
}

logger.debug("Impacted modules for {} = {}", thing.getUID(), impactedModules);

NikobusPcLinkHandler pcLink = getPcLink();
if (pcLink != null) {
pcLink.addListener(getAddress(), this::commandReceived);
Expand Down Expand Up @@ -183,8 +197,10 @@ private void commandReceived() {

updateState(CHANNEL_BUTTON, OnOffType.ON);

Utils.cancel(requestUpdateFuture);
requestUpdateFuture = scheduler.schedule(this::update, 400, TimeUnit.MILLISECONDS);
if (!impactedModules.isEmpty()) {
Utils.cancel(requestUpdateFuture);
requestUpdateFuture = scheduler.schedule(this::update, 400, TimeUnit.MILLISECONDS);
}
}

private void update() {
Expand Down
Expand Up @@ -12,7 +12,8 @@
*/
package org.openhab.binding.nikobus.internal.utils;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.util.HexUtils;

/**
Expand All @@ -21,6 +22,7 @@
* @author Davy Vanherbergen - Initial contribution
* @author Boris Krivonog - Removed dependency to javax.xml.bind.DatatypeConverter
*/
@NonNullByDefault
public class CRCUtil {

private static final int CRC_INIT = 0xFFFF;
Expand All @@ -35,7 +37,7 @@ public class CRCUtil {
* String representing hex numbers.
* @return input string + CRC.
*/
public static String appendCRC(String input) {
public static @Nullable String appendCRC(@Nullable String input) {
if (input == null) {
return null;
}
Expand All @@ -54,7 +56,7 @@ public static String appendCRC(String input) {
}

check = check & CRC_INIT;
String checksum = StringUtils.leftPad(Integer.toHexString(check), 4, "0");
String checksum = leftPadWithZeros(Integer.toHexString(check), 4);
return (input + checksum).toUpperCase();
}

Expand Down Expand Up @@ -85,6 +87,14 @@ public static String appendCRC2(String input) {
}
}

return input + StringUtils.leftPad(Integer.toHexString(check), 2, "0").toUpperCase();
return input + leftPadWithZeros(Integer.toHexString(check), 2).toUpperCase();
}

private static String leftPadWithZeros(String text, int size) {
StringBuilder builder = new StringBuilder(text);
while (builder.length() < size) {
builder.insert(0, '0');
}
return builder.toString();
}
}
Expand Up @@ -14,13 +14,17 @@

import java.util.concurrent.Future;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* The {@link Utils} class defines commonly used utility functions.
*
* @author Boris Krivonog - Initial contribution
*/
@NonNullByDefault
public class Utils {
public static void cancel(Future<?> future) {
public static void cancel(@Nullable Future<?> future) {
if (future != null) {
future.cancel(true);
}
Expand Down
Expand Up @@ -42,7 +42,7 @@
</parameter>
<parameter name="impactedModules" type="text">
<label>Impacted Modules</label>
<description>Comma separated list of impacted modules, i.e. 4C6C-1,4C6C-2</description>
<description>Comma separated list of impacted modules, i.e. switch-module:s1:1</description>
</parameter>
</config-description>
</thing-type>
Expand Down