2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.homematic.internal.type;
15 import static org.openhab.binding.homematic.internal.HomematicBindingConstants.BINDING_ID;
17 import org.apache.commons.lang.math.NumberUtils;
18 import org.openhab.binding.homematic.internal.model.HmChannel;
19 import org.openhab.binding.homematic.internal.model.HmDatapoint;
20 import org.openhab.binding.homematic.internal.model.HmDatapointInfo;
21 import org.openhab.binding.homematic.internal.model.HmDevice;
22 import org.openhab.binding.homematic.internal.model.HmGatewayInfo;
23 import org.openhab.binding.homematic.internal.model.HmParamsetType;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.type.ChannelGroupTypeUID;
30 import org.openhab.core.thing.type.ChannelTypeUID;
33 * Utility class for generating some UIDs.
35 * @author Gerhard Riegler - Initial contribution
37 public class UidUtils {
40 * Generates the ThingTypeUID for the given device. If it's a Homegear device, add a prefix because a Homegear
41 * device has more datapoints.
43 public static ThingTypeUID generateThingTypeUID(HmDevice device) {
44 if (!device.isGatewayExtras() && device.getGatewayId().equals(HmGatewayInfo.ID_HOMEGEAR)) {
45 return new ThingTypeUID(BINDING_ID, String.format("HG-%s", device.getType()));
47 return new ThingTypeUID(BINDING_ID, device.getType());
52 * Generates the ChannelTypeUID for the given datapoint with deviceType, channelNumber and datapointName.
54 public static ChannelTypeUID generateChannelTypeUID(HmDatapoint dp) {
55 return new ChannelTypeUID(BINDING_ID, String.format("%s_%s_%s", dp.getChannel().getDevice().getType(),
56 dp.getChannel().getNumber(), dp.getName()));
60 * Generates the ChannelTypeUID for the given datapoint with deviceType and channelNumber.
62 public static ChannelGroupTypeUID generateChannelGroupTypeUID(HmChannel channel) {
63 return new ChannelGroupTypeUID(BINDING_ID,
64 String.format("%s_%s", channel.getDevice().getType(), channel.getNumber()));
68 * Generates the ThingUID for the given device in the given bridge.
70 public static ThingUID generateThingUID(HmDevice device, Bridge bridge) {
71 ThingTypeUID thingTypeUID = generateThingTypeUID(device);
72 return new ThingUID(thingTypeUID, bridge.getUID(), device.getAddress());
76 * Generates the ChannelUID for the given datapoint with channelNumber and datapointName.
78 public static ChannelUID generateChannelUID(HmDatapoint dp, ThingUID thingUID) {
79 return new ChannelUID(thingUID, String.valueOf(dp.getChannel().getNumber()), dp.getName());
83 * Generates the HmDatapointInfo for the given thing and channelUID.
85 public static HmDatapointInfo createHmDatapointInfo(ChannelUID channelUID) {
86 return new HmDatapointInfo(channelUID.getThingUID().getId(), HmParamsetType.VALUES,
87 NumberUtils.toInt(channelUID.getGroupId()), channelUID.getIdWithoutGroup());
91 * Returns the address of the Homematic device from the given thing.
93 public static String getHomematicAddress(Thing thing) {
94 return thing.getUID().getId();