Skip to content

Commit

Permalink
[modbus] Export thing handler interface for upcoming bundles (openhab…
Browse files Browse the repository at this point in the history
…#4077)

* [modbus] Export thing handler interface for upcoming bundles

Exporting the thing handler interface and the required other interfaces
makes it possible to textend this binding by other bundles. Such an use
case is when custom handlers are written to specific devices.

* [modbus] Renamed internal constants class to ModbusBindingConstantsInternal

Signed-off-by: Nagy Attila Gabor <mrbig@sneaker.hu>
Signed-off-by: Pascal Larin <plarin@gmail.com>
  • Loading branch information
mrbig authored and chaton78 committed Jan 1, 2019
1 parent 485a57d commit 829193a
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 102 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.openhab.binding.modbus.internal.ModbusBindingConstants;
import org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal;
import org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler;
import org.openhab.binding.modbus.internal.handler.ModbusPollerThingHandler;
import org.openhab.binding.modbus.internal.handler.ModbusPollerThingHandlerImpl;
Expand Down Expand Up @@ -77,13 +77,13 @@ public class ModbusPollerThingHandlerTest {
private ThingHandlerCallback thingCallback;

public static BridgeBuilder createTcpThingBuilder(String id) {
return BridgeBuilder.create(ModbusBindingConstants.THING_TYPE_MODBUS_TCP,
new ThingUID(ModbusBindingConstants.THING_TYPE_MODBUS_TCP, id)).withLabel("label for " + id);
return BridgeBuilder.create(ModbusBindingConstantsInternal.THING_TYPE_MODBUS_TCP,
new ThingUID(ModbusBindingConstantsInternal.THING_TYPE_MODBUS_TCP, id)).withLabel("label for " + id);
}

public static BridgeBuilder createPollerThingBuilder(String id) {
return BridgeBuilder.create(ModbusBindingConstants.THING_TYPE_MODBUS_POLLER,
new ThingUID(ModbusBindingConstants.THING_TYPE_MODBUS_POLLER, id)).withLabel("label for " + id);
return BridgeBuilder.create(ModbusBindingConstantsInternal.THING_TYPE_MODBUS_POLLER,
new ThingUID(ModbusBindingConstantsInternal.THING_TYPE_MODBUS_POLLER, id)).withLabel("label for " + id);
}

private void registerThingToMockRegistry(Thing thing) {
Expand Down Expand Up @@ -168,7 +168,7 @@ public void testInitializeNonPolling()
pollerConfig.put("refresh", 0L); // 0 -> non polling
pollerConfig.put("start", 5);
pollerConfig.put("length", 9);
pollerConfig.put("type", ModbusBindingConstants.READ_TYPE_HOLDING_REGISTER);
pollerConfig.put("type", ModbusBindingConstantsInternal.READ_TYPE_HOLDING_REGISTER);
poller = createPollerThingBuilder("poller").withConfiguration(pollerConfig).withBridge(endpoint.getUID())
.build();
registerThingToMockRegistry(poller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.openhab.binding.modbus.internal.ModbusBindingConstants;
import org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal;
import org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler;
import org.openhab.io.transport.modbus.ModbusManager;
import org.openhab.io.transport.modbus.endpoint.EndpointPoolConfiguration;
Expand All @@ -42,8 +42,8 @@ public class ModbusTcpThingHandlerTest {
private ModbusManager modbusManager;

private static BridgeBuilder createTcpThingBuilder(String id) {
return BridgeBuilder.create(ModbusBindingConstants.THING_TYPE_MODBUS_TCP,
new ThingUID(ModbusBindingConstants.THING_TYPE_MODBUS_TCP, id));
return BridgeBuilder.create(ModbusBindingConstantsInternal.THING_TYPE_MODBUS_TCP,
new ThingUID(ModbusBindingConstantsInternal.THING_TYPE_MODBUS_TCP, id));
}

@SuppressWarnings("null")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ Import-Package: org.apache.commons.lang,
org.osgi.framework,
org.slf4j
Service-Component: OSGI-INF/*.xml
Export-Package: org.openhab.binding.modbus,
org.openhab.binding.modbus.handler
Bundle-ActivationPolicy: lazy
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2010-2018 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.modbus;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link ModbusBinding} class defines some constants
* public that might be used from other bundles as well.
*
* @author Sami Salonen - Initial contribution
* @author Nagy Attila Gabor - Split the original ModbusBindingConstants in two
*/
@NonNullByDefault
public class ModbusBindingConstants {

public static final String BINDING_ID = "modbus";

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.modbus.internal.handler;
package org.openhab.binding.modbus.handler;

import org.eclipse.jdt.annotation.NonNullByDefault;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.modbus.internal.handler;
package org.openhab.binding.modbus.handler;

import java.util.function.Supplier;

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

import static org.openhab.binding.modbus.ModbusBindingConstants.BINDING_ID;

import java.util.HashMap;
import java.util.Map;

Expand All @@ -22,9 +24,7 @@
* @author Sami Salonen - Initial contribution
*/
@NonNullByDefault
public class ModbusBindingConstants {

public static final String BINDING_ID = "modbus";
public class ModbusBindingConstantsInternal {

// List of all Thing Type UIDs
public static final ThingTypeUID THING_TYPE_MODBUS_TCP = new ThingTypeUID(BINDING_ID, "tcp");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
package org.openhab.binding.modbus.internal;

import static org.openhab.binding.modbus.internal.ModbusBindingConstants.*;
import static org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal.*;

import java.util.HashSet;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler;
import org.eclipse.smarthome.core.types.Command;
import org.openhab.binding.modbus.handler.ModbusEndpointThingHandler;
import org.openhab.binding.modbus.internal.ModbusConfigurationException;
import org.openhab.io.transport.modbus.ModbusManager;
import org.openhab.io.transport.modbus.ModbusManagerListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
package org.openhab.binding.modbus.internal.handler;

import static org.openhab.binding.modbus.internal.ModbusBindingConstants.*;
import static org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal.*;

import java.math.BigDecimal;
import java.util.Collection;
Expand Down Expand Up @@ -46,7 +46,9 @@
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.State;
import org.openhab.binding.modbus.internal.ModbusBindingConstants;
import org.openhab.binding.modbus.handler.EndpointNotInitializedException;
import org.openhab.binding.modbus.handler.ModbusEndpointThingHandler;
import org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal;
import org.openhab.binding.modbus.internal.ModbusConfigurationException;
import org.openhab.binding.modbus.internal.Transformation;
import org.openhab.binding.modbus.internal.config.ModbusDataConfiguration;
Expand Down Expand Up @@ -92,19 +94,19 @@ public class ModbusDataThingHandler extends BaseThingHandler implements ModbusRe
private static final Map<String, List<Class<? extends State>>> CHANNEL_ID_TO_ACCEPTED_TYPES = new HashMap<>();

static {
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstants.CHANNEL_SWITCH,
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstantsInternal.CHANNEL_SWITCH,
new SwitchItem("").getAcceptedDataTypes());
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstants.CHANNEL_CONTACT,
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstantsInternal.CHANNEL_CONTACT,
new ContactItem("").getAcceptedDataTypes());
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstants.CHANNEL_DATETIME,
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstantsInternal.CHANNEL_DATETIME,
new DateTimeItem("").getAcceptedDataTypes());
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstants.CHANNEL_DIMMER,
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstantsInternal.CHANNEL_DIMMER,
new DimmerItem("").getAcceptedDataTypes());
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstants.CHANNEL_NUMBER,
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstantsInternal.CHANNEL_NUMBER,
new NumberItem("").getAcceptedDataTypes());
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstants.CHANNEL_STRING,
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstantsInternal.CHANNEL_STRING,
new StringItem("").getAcceptedDataTypes());
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstants.CHANNEL_ROLLERSHUTTER,
CHANNEL_ID_TO_ACCEPTED_TYPES.put(ModbusBindingConstantsInternal.CHANNEL_ROLLERSHUTTER,
new RollershutterItem("").getAcceptedDataTypes());
}

Expand Down Expand Up @@ -692,7 +694,7 @@ public synchronized void onError(ModbusReadRequestBlueprint request, Exception e
error.getMessage(), error);
}
Map<@NonNull ChannelUID, @NonNull State> states = new HashMap<>();
states.put(new ChannelUID(getThing().getUID(), ModbusBindingConstants.CHANNEL_LAST_READ_ERROR),
states.put(new ChannelUID(getThing().getUID(), ModbusBindingConstantsInternal.CHANNEL_LAST_READ_ERROR),
new DateTimeType());

synchronized (this) {
Expand Down Expand Up @@ -727,7 +729,7 @@ public synchronized void onError(ModbusWriteRequestBlueprint request, Exception
error.getMessage(), error);
}
Map<@NonNull ChannelUID, @NonNull State> states = new HashMap<>();
states.put(new ChannelUID(getThing().getUID(), ModbusBindingConstants.CHANNEL_LAST_WRITE_ERROR),
states.put(new ChannelUID(getThing().getUID(), ModbusBindingConstantsInternal.CHANNEL_LAST_WRITE_ERROR),
new DateTimeType());

synchronized (this) {
Expand All @@ -752,7 +754,7 @@ public synchronized void onWriteResponse(ModbusWriteRequestBlueprint request, Mo
logger.debug("Successful write, matching request {}", request);
DateTimeType now = new DateTimeType();
updateStatus(ThingStatus.ONLINE);
updateState(ModbusBindingConstants.CHANNEL_LAST_WRITE_SUCCESS, now);
updateState(ModbusBindingConstantsInternal.CHANNEL_LAST_WRITE_SUCCESS, now);
}

/**
Expand Down Expand Up @@ -813,7 +815,7 @@ private Map<ChannelUID, State> processUpdatedValue(DecimalType numericState, boo
}
});

states.put(new ChannelUID(getThing().getUID(), ModbusBindingConstants.CHANNEL_LAST_READ_SUCCESS),
states.put(new ChannelUID(getThing().getUID(), ModbusBindingConstantsInternal.CHANNEL_LAST_READ_SUCCESS),
new DateTimeType());

synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.openhab.binding.modbus.handler.EndpointNotInitializedException;
import org.openhab.binding.modbus.handler.ModbusEndpointThingHandler;
import org.openhab.binding.modbus.internal.AtomicStampedKeyValue;
import org.openhab.binding.modbus.internal.ModbusBindingConstants;
import org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal;
import org.openhab.binding.modbus.internal.config.ModbusPollerConfiguration;
import org.openhab.io.transport.modbus.BitArray;
import org.openhab.io.transport.modbus.ModbusManager;
Expand Down Expand Up @@ -222,14 +224,14 @@ public void resetCache() {
private static class ModbusPollerReadRequest extends BasicModbusReadRequestBlueprint {

private static ModbusReadFunctionCode getFunctionCode(@Nullable String type) {
if (!ModbusBindingConstants.READ_FUNCTION_CODES.containsKey(type)) {
Object[] acceptedTypes = ModbusBindingConstants.READ_FUNCTION_CODES.keySet().toArray();
if (!ModbusBindingConstantsInternal.READ_FUNCTION_CODES.containsKey(type)) {
Object[] acceptedTypes = ModbusBindingConstantsInternal.READ_FUNCTION_CODES.keySet().toArray();
Arrays.sort(acceptedTypes);
throw new IllegalArgumentException(
String.format("No function code found for type='%s'. Was expecting one of: %s", type,
StringUtils.join(acceptedTypes, ", ")));
}
ModbusReadFunctionCode functionCode = ModbusBindingConstants.READ_FUNCTION_CODES.get(type);
ModbusReadFunctionCode functionCode = ModbusBindingConstantsInternal.READ_FUNCTION_CODES.get(type);
return functionCode;
}

Expand Down

0 comments on commit 829193a

Please sign in to comment.