Search in sources :

Example 1 with HomematicGateway

use of org.eclipse.smarthome.binding.homematic.internal.communicator.HomematicGateway in project smarthome by eclipse.

the class HomematicThingHandler method doInitializeInBackground.

private void doInitializeInBackground() throws GatewayNotAvailableException, HomematicClientException, IOException {
    HomematicGateway gateway = getHomematicGateway();
    HmDevice device = gateway.getDevice(UidUtils.getHomematicAddress(getThing()));
    HmChannel channelZero = device.getChannel(0);
    loadHomematicChannelValues(channelZero);
    updateStatus(device);
    logger.debug("Initializing thing '{}' from gateway '{}'", getThing().getUID(), gateway.getId());
    // update properties
    Map<String, String> properties = editProperties();
    setProperty(properties, channelZero, PROPERTY_BATTERY_TYPE, VIRTUAL_DATAPOINT_NAME_BATTERY_TYPE);
    setProperty(properties, channelZero, Thing.PROPERTY_FIRMWARE_VERSION, VIRTUAL_DATAPOINT_NAME_FIRMWARE);
    setProperty(properties, channelZero, Thing.PROPERTY_SERIAL_NUMBER, device.getAddress());
    setProperty(properties, channelZero, PROPERTY_AES_KEY, DATAPOINT_NAME_AES_KEY);
    updateProperties(properties);
    // update data point list for reconfigurable channels
    for (HmChannel channel : device.getChannels()) {
        if (channel.isReconfigurable()) {
            loadHomematicChannelValues(channel);
            if (channel.checkForChannelFunctionChange()) {
                gateway.updateChannelValueDatapoints(channel);
            }
        }
    }
    // update configurations
    Configuration config = editConfiguration();
    for (HmChannel channel : device.getChannels()) {
        loadHomematicChannelValues(channel);
        for (HmDatapoint dp : channel.getDatapoints()) {
            if (dp.getParamsetType() == HmParamsetType.MASTER) {
                config.put(MetadataUtils.getParameterName(dp), dp.isEnumType() ? dp.getOptionValue() : dp.getValue());
            }
        }
    }
    updateConfiguration(config);
    // update thing channel list for reconfigurable channels (relies on the new value of the
    // CHANNEL_FUNCTION datapoint fetched during configuration update)
    List<Channel> thingChannels = new ArrayList<>(getThing().getChannels());
    if (updateDynamicChannelList(device, thingChannels)) {
        updateThing(editThing().withChannels(thingChannels).build());
    }
}
Also used : HmDevice(org.eclipse.smarthome.binding.homematic.internal.model.HmDevice) Configuration(org.eclipse.smarthome.config.core.Configuration) Channel(org.eclipse.smarthome.core.thing.Channel) HmChannel(org.eclipse.smarthome.binding.homematic.internal.model.HmChannel) ArrayList(java.util.ArrayList) HomematicGateway(org.eclipse.smarthome.binding.homematic.internal.communicator.HomematicGateway) HmChannel(org.eclipse.smarthome.binding.homematic.internal.model.HmChannel) HmDatapoint(org.eclipse.smarthome.binding.homematic.internal.model.HmDatapoint)

Example 2 with HomematicGateway

use of org.eclipse.smarthome.binding.homematic.internal.communicator.HomematicGateway in project smarthome by eclipse.

the class HomematicThingHandler method handleConfigurationUpdate.

@Override
public void handleConfigurationUpdate(Map<String, Object> configurationParameters) throws ConfigValidationException {
    super.handleConfigurationUpdate(configurationParameters);
    try {
        HomematicGateway gateway = getHomematicGateway();
        HmDevice device = gateway.getDevice(UidUtils.getHomematicAddress(getThing()));
        for (Entry<String, Object> configurationParameter : configurationParameters.entrySet()) {
            String key = configurationParameter.getKey();
            Object newValue = configurationParameter.getValue();
            if (key.startsWith("HMP_")) {
                key = StringUtils.removeStart(key, "HMP_");
                Integer channelNumber = NumberUtils.toInt(StringUtils.substringBefore(key, "_"));
                String dpName = StringUtils.substringAfter(key, "_");
                HmDatapointInfo dpInfo = new HmDatapointInfo(device.getAddress(), HmParamsetType.MASTER, channelNumber, dpName);
                HmDatapoint dp = device.getChannel(channelNumber).getDatapoint(dpInfo);
                if (dp != null) {
                    try {
                        if (newValue != null) {
                            if (newValue instanceof BigDecimal) {
                                final BigDecimal decimal = (BigDecimal) newValue;
                                if (dp.isIntegerType()) {
                                    newValue = decimal.intValue();
                                } else if (dp.isFloatType()) {
                                    newValue = decimal.doubleValue();
                                }
                            }
                            if (ObjectUtils.notEqual(dp.isEnumType() ? dp.getOptionValue() : dp.getValue(), newValue)) {
                                sendDatapoint(dp, new HmDatapointConfig(), newValue);
                            }
                        }
                    } catch (IOException ex) {
                        logger.error("Error setting thing property {}: {}", dpInfo, ex.getMessage());
                    }
                } else {
                    logger.error("Can't find datapoint for thing property {}", dpInfo);
                }
            }
        }
        gateway.triggerDeviceValuesReload(device);
    } catch (HomematicClientException | GatewayNotAvailableException ex) {
        logger.error("Error setting thing properties: {}", ex.getMessage(), ex);
    }
}
Also used : HomematicClientException(org.eclipse.smarthome.binding.homematic.internal.misc.HomematicClientException) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) HmDatapoint(org.eclipse.smarthome.binding.homematic.internal.model.HmDatapoint) HmDevice(org.eclipse.smarthome.binding.homematic.internal.model.HmDevice) HmDatapointConfig(org.eclipse.smarthome.binding.homematic.internal.model.HmDatapointConfig) HomematicGateway(org.eclipse.smarthome.binding.homematic.internal.communicator.HomematicGateway) HmDatapointInfo(org.eclipse.smarthome.binding.homematic.internal.model.HmDatapointInfo)

Example 3 with HomematicGateway

use of org.eclipse.smarthome.binding.homematic.internal.communicator.HomematicGateway in project smarthome by eclipse.

the class HomematicDeviceDiscoveryService method enableInstallMode.

/**
 * Will set controller in <i>installMode==true</i>, but only if the bridge
 * is ONLINE (e.g. not during INITIALIZATION).
 */
private void enableInstallMode() {
    try {
        HomematicGateway gateway = bridgeHandler.getGateway();
        ThingStatus bridgeStatus = null;
        if (bridgeHandler != null) {
            Thing bridge = bridgeHandler.getThing();
            bridgeStatus = bridge.getStatus();
            updateInstallModeDuration(bridge);
        }
        if (ThingStatus.ONLINE == bridgeStatus) {
            gateway.setInstallMode(true, installModeDuration);
            int remaining = gateway.getInstallMode();
            if (remaining > 0) {
                setIsInInstallMode();
                logger.debug("Successfully put controller in install mode. Remaining time: {} seconds", remaining);
            } else {
                logger.warn("Controller did not accept requested install mode");
            }
        } else {
            logger.debug("Will not attempt to set controller in install mode, because bridge is not ONLINE.");
        }
    } catch (Exception ex) {
        logger.warn("Failed to set Homematic controller in install mode", ex);
    }
}
Also used : ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) HomematicGateway(org.eclipse.smarthome.binding.homematic.internal.communicator.HomematicGateway) Thing(org.eclipse.smarthome.core.thing.Thing) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with HomematicGateway

use of org.eclipse.smarthome.binding.homematic.internal.communicator.HomematicGateway in project smarthome by eclipse.

the class HomematicThingHandler method updateChannelState.

/**
 * Evaluates the channel and datapoint for this channelUID and updates the state of the channel.
 */
private void updateChannelState(ChannelUID channelUID) throws GatewayNotAvailableException, HomematicClientException, IOException, ConverterException {
    HomematicGateway gateway = getHomematicGateway();
    HmDatapointInfo dpInfo = UidUtils.createHmDatapointInfo(channelUID);
    HmDatapoint dp = gateway.getDatapoint(dpInfo);
    Channel channel = getThing().getChannel(channelUID.getId());
    updateChannelState(dp, channel);
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) HmChannel(org.eclipse.smarthome.binding.homematic.internal.model.HmChannel) HomematicGateway(org.eclipse.smarthome.binding.homematic.internal.communicator.HomematicGateway) HmDatapointInfo(org.eclipse.smarthome.binding.homematic.internal.model.HmDatapointInfo) HmDatapoint(org.eclipse.smarthome.binding.homematic.internal.model.HmDatapoint)

Example 5 with HomematicGateway

use of org.eclipse.smarthome.binding.homematic.internal.communicator.HomematicGateway in project smarthome by eclipse.

the class HomematicThingHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    logger.debug("Received command '{}' for channel '{}'", command, channelUID);
    HmDatapoint dp = null;
    try {
        HomematicGateway gateway = getHomematicGateway();
        HmDatapointInfo dpInfo = UidUtils.createHmDatapointInfo(channelUID);
        if (RefreshType.REFRESH == command) {
            logger.debug("Refreshing {}", dpInfo);
            dpInfo = new HmDatapointInfo(dpInfo.getAddress(), HmParamsetType.VALUES, 0, VIRTUAL_DATAPOINT_NAME_RELOAD_FROM_GATEWAY);
            dp = gateway.getDatapoint(dpInfo);
            sendDatapoint(dp, new HmDatapointConfig(), Boolean.TRUE);
        } else {
            Channel channel = getThing().getChannel(channelUID.getId());
            if (channel == null) {
                logger.warn("Channel '{}' not found in thing '{}' on gateway '{}'", channelUID, getThing().getUID(), gateway.getId());
            } else {
                if (StopMoveType.STOP == command && DATAPOINT_NAME_LEVEL.equals(dpInfo.getName())) {
                    // special case with stop type (rollershutter)
                    dpInfo.setName(DATAPOINT_NAME_STOP);
                    HmDatapoint stopDp = gateway.getDatapoint(dpInfo);
                    ChannelUID stopChannelUID = UidUtils.generateChannelUID(stopDp, getThing().getUID());
                    handleCommand(stopChannelUID, OnOffType.ON);
                } else {
                    dp = gateway.getDatapoint(dpInfo);
                    TypeConverter<?> converter = ConverterFactory.createConverter(channel.getAcceptedItemType());
                    Object newValue = converter.convertToBinding(command, dp);
                    HmDatapointConfig config = getChannelConfig(channel, dp);
                    sendDatapoint(dp, config, newValue);
                }
            }
        }
    } catch (HomematicClientException | GatewayNotAvailableException ex) {
        logger.warn("{}", ex.getMessage());
    } catch (IOException ex) {
        if (dp != null && dp.getChannel().getDevice().isOffline()) {
            logger.warn("Device '{}' is OFFLINE, can't send command '{}' for channel '{}'", dp.getChannel().getDevice().getAddress(), command, channelUID);
            logger.trace("{}", ex.getMessage(), ex);
        } else {
            logger.error("{}", ex.getMessage(), ex);
        }
    } catch (ConverterTypeException ex) {
        logger.warn("{}, please check the item type and the commands in your scripts", ex.getMessage());
    } catch (Exception ex) {
        logger.error("{}", ex.getMessage(), ex);
    }
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) HmChannel(org.eclipse.smarthome.binding.homematic.internal.model.HmChannel) HomematicClientException(org.eclipse.smarthome.binding.homematic.internal.misc.HomematicClientException) IOException(java.io.IOException) ConverterException(org.eclipse.smarthome.binding.homematic.internal.converter.ConverterException) ConverterTypeException(org.eclipse.smarthome.binding.homematic.internal.converter.ConverterTypeException) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException) HomematicClientException(org.eclipse.smarthome.binding.homematic.internal.misc.HomematicClientException) IOException(java.io.IOException) HmDatapoint(org.eclipse.smarthome.binding.homematic.internal.model.HmDatapoint) HmDatapointConfig(org.eclipse.smarthome.binding.homematic.internal.model.HmDatapointConfig) ConverterTypeException(org.eclipse.smarthome.binding.homematic.internal.converter.ConverterTypeException) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) HomematicGateway(org.eclipse.smarthome.binding.homematic.internal.communicator.HomematicGateway) HmDatapointInfo(org.eclipse.smarthome.binding.homematic.internal.model.HmDatapointInfo)

Aggregations

HomematicGateway (org.eclipse.smarthome.binding.homematic.internal.communicator.HomematicGateway)7 HmDatapoint (org.eclipse.smarthome.binding.homematic.internal.model.HmDatapoint)4 HmChannel (org.eclipse.smarthome.binding.homematic.internal.model.HmChannel)3 HmDatapointInfo (org.eclipse.smarthome.binding.homematic.internal.model.HmDatapointInfo)3 Channel (org.eclipse.smarthome.core.thing.Channel)3 IOException (java.io.IOException)2 HomematicClientException (org.eclipse.smarthome.binding.homematic.internal.misc.HomematicClientException)2 HmDatapointConfig (org.eclipse.smarthome.binding.homematic.internal.model.HmDatapointConfig)2 HmDevice (org.eclipse.smarthome.binding.homematic.internal.model.HmDevice)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 HomematicBridgeHandler (org.eclipse.smarthome.binding.homematic.handler.HomematicBridgeHandler)1 ConverterException (org.eclipse.smarthome.binding.homematic.internal.converter.ConverterException)1 ConverterTypeException (org.eclipse.smarthome.binding.homematic.internal.converter.ConverterTypeException)1 HomematicTypeGenerator (org.eclipse.smarthome.binding.homematic.internal.type.HomematicTypeGenerator)1 BridgeHelper.createHomematicBridge (org.eclipse.smarthome.binding.homematic.test.util.BridgeHelper.createHomematicBridge)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 ConfigValidationException (org.eclipse.smarthome.config.core.validation.ConfigValidationException)1 Bridge (org.eclipse.smarthome.core.thing.Bridge)1