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());
}
}
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);
}
}
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);
}
}
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);
}
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);
}
}
Aggregations