Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Gerhard Riegler <gerhard.riegler@gmail.com>
  • Loading branch information
gerrieg committed May 11, 2017
1 parent 4a31266 commit 22f69c4
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 28 deletions.
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.openhab.binding.homematic.handler.HomematicBridgeHandler;
import org.openhab.binding.homematic.internal.communicator.HomematicGateway;
import org.openhab.binding.homematic.internal.model.HmDevice;
import org.openhab.binding.homematic.type.UidUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -105,10 +106,10 @@ public void loadDevices() {
@Override
public void run() {
try {
bridgeHandler.getGateway().loadAllDeviceMetadata();
final HomematicGateway gateway = bridgeHandler.getGateway();
gateway.loadAllDeviceMetadata();
bridgeHandler.getTypeGenerator().validateFirmwares();
logger.debug("Finished Homematic device discovery scan on gateway '{}'",
bridgeHandler.getGateway().getId());
logger.debug("Finished Homematic device discovery scan on gateway '{}'", gateway.getId());
} catch (Throwable ex) {
logger.error("{}", ex.getMessage(), ex);
} finally {
Expand Down
Expand Up @@ -86,7 +86,7 @@ public void run() {
logger.error("{}", ex.getMessage(), ex);
}
}

gateway.startWatchdogs();
} catch (IOException ex) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, ex.getMessage());
dispose();
Expand Down Expand Up @@ -237,8 +237,11 @@ public void updateThing(HmDevice device) {
public void onStateUpdated(HmDatapoint dp) {
Thing hmThing = getThingByUID(UidUtils.generateThingUID(dp.getChannel().getDevice(), getThing()));
if (hmThing != null && hmThing.getHandler() != null) {
HomematicThingHandler thingHandler = (HomematicThingHandler) hmThing.getHandler();
thingHandler.updateDatapointState(dp);
final ThingStatus status = hmThing.getStatus();
if (status == ThingStatus.ONLINE || status == ThingStatus.OFFLINE) {
HomematicThingHandler thingHandler = (HomematicThingHandler) hmThing.getHandler();
thingHandler.updateDatapointState(dp);
}
}
}

Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.smarthome.config.core.validation.ConfigValidationException;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.StopMoveType;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
Expand Down Expand Up @@ -320,15 +321,16 @@ private HmDatapointConfig getChannelConfig(Channel channel, HmDatapoint dp) {
* Returns the Homematic gateway if the bridge is available.
*/
private HomematicGateway getHomematicGateway() throws BridgeHandlerNotAvailableException {
if (getBridge() == null || getBridge().getHandler() == null
|| ((HomematicBridgeHandler) getBridge().getHandler()).getGateway() == null) {
final Bridge bridge = getBridge();
if (bridge == null || bridge.getHandler() == null
|| ((HomematicBridgeHandler) bridge.getHandler()).getGateway() == null) {
if (thing.getStatus() != ThingStatus.INITIALIZING) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR);
}
throw new BridgeHandlerNotAvailableException("BridgeHandler not yet available!");
}

return ((HomematicBridgeHandler) getBridge().getHandler()).getGateway();
return ((HomematicBridgeHandler) bridge.getHandler()).getGateway();
}

/**
Expand Down Expand Up @@ -360,10 +362,11 @@ public void handleConfigurationUpdate(Map<String, Object> configurationParameter
try {
if (newValue != null) {
if (newValue instanceof BigDecimal) {
final BigDecimal decimal = (BigDecimal) newValue;
if (dp.isIntegerType()) {
newValue = ((BigDecimal) newValue).intValue();
newValue = decimal.intValue();
} else if (dp.isFloatType()) {
newValue = ((BigDecimal) newValue).doubleValue();
newValue = decimal.doubleValue();
}
}
if (ObjectUtils.notEqual(dp.isEnumType() ? dp.getOptionValue() : dp.getValue(),
Expand Down
Expand Up @@ -175,7 +175,6 @@ public void initialize() throws IOException {
logger.debug("Used Homematic transfer modes: {}", sb.toString());
startClients();
startServers();
startWatchdogs();
}

/**
Expand Down Expand Up @@ -252,9 +251,10 @@ private void stopServers() {
}

/**
* Starts the connection and event tracker threads.
* {@inheritDoc}
*/
private void startWatchdogs() {
@Override
public void startWatchdogs() {
ScheduledExecutorService scheduler = ThreadPoolManager.getScheduledPool(GATEWAY_POOL_NAME);

if (config.getReconnectInterval() == 0) {
Expand Down
Expand Up @@ -80,4 +80,9 @@ public void sendDatapoint(HmDatapoint dp, HmDatapointConfig dpConfig, Object new
*/
public void loadRssiValues() throws IOException;

/**
* Starts the connection and event tracker threads.
*/
public void startWatchdogs();

}
Expand Up @@ -128,20 +128,17 @@ public void generate(HmDevice device) {
List<ChannelDefinition> channelDefinitions = new ArrayList<ChannelDefinition>();
// generate channel
for (HmDatapoint dp : channel.getDatapoints().values()) {
if (!isIgnoredDatapoint(dp)) {
if (dp.getParamsetType() == HmParamsetType.VALUES) {
ChannelTypeUID channelTypeUID = UidUtils.generateChannelTypeUID(dp);
ChannelType channelType = channelTypeProvider.getChannelType(channelTypeUID,
Locale.getDefault());
if (channelType == null) {
channelType = createChannelType(dp, channelTypeUID);
channelTypeProvider.addChannelType(channelType);
}

ChannelDefinition channelDef = new ChannelDefinition(dp.getName(),
channelType.getUID());
channelDefinitions.add(channelDef);
if (!isIgnoredDatapoint(dp) && dp.getParamsetType() == HmParamsetType.VALUES) {
ChannelTypeUID channelTypeUID = UidUtils.generateChannelTypeUID(dp);
ChannelType channelType = channelTypeProvider.getChannelType(channelTypeUID,
Locale.getDefault());
if (channelType == null) {
channelType = createChannelType(dp, channelTypeUID);
channelTypeProvider.addChannelType(channelType);
}

ChannelDefinition channelDef = new ChannelDefinition(dp.getName(), channelType.getUID());
channelDefinitions.add(channelDef);
}
}

Expand Down
Expand Up @@ -64,7 +64,7 @@ public static ChannelGroupTypeUID generateChannelGroupTypeUID(HmChannel channel)
* Generates the ThingUID for the given device in the given bridge.
*/
public static ThingUID generateThingUID(HmDevice device, Bridge bridge) {
ThingTypeUID thingTypeUID = generateThingTypeUID(device);
final ThingTypeUID thingTypeUID = generateThingTypeUID(device);
return new ThingUID(thingTypeUID, bridge.getUID(), device.getAddress());
}

Expand Down

0 comments on commit 22f69c4

Please sign in to comment.