]> git.basschouten.com Git - openhab-addons.git/blob
c2aa4d161f4cd394c53e2bb6415772f9b4214437
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.homematic.internal.type;
14
15 import static org.openhab.binding.homematic.internal.HomematicBindingConstants.BINDING_ID;
16
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;
31
32 /**
33  * Utility class for generating some UIDs.
34  *
35  * @author Gerhard Riegler - Initial contribution
36  */
37 public class UidUtils {
38
39     /**
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.
42      */
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()));
46         } else {
47             return new ThingTypeUID(BINDING_ID, device.getType());
48         }
49     }
50
51     /**
52      * Generates the ChannelTypeUID for the given datapoint with deviceType, channelNumber and datapointName.
53      */
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()));
57     }
58
59     /**
60      * Generates the ChannelTypeUID for the given datapoint with deviceType and channelNumber.
61      */
62     public static ChannelGroupTypeUID generateChannelGroupTypeUID(HmChannel channel) {
63         return new ChannelGroupTypeUID(BINDING_ID,
64                 String.format("%s_%s", channel.getDevice().getType(), channel.getNumber()));
65     }
66
67     /**
68      * Generates the ThingUID for the given device in the given bridge.
69      */
70     public static ThingUID generateThingUID(HmDevice device, Bridge bridge) {
71         ThingTypeUID thingTypeUID = generateThingTypeUID(device);
72         return new ThingUID(thingTypeUID, bridge.getUID(), device.getAddress());
73     }
74
75     /**
76      * Generates the ChannelUID for the given datapoint with channelNumber and datapointName.
77      */
78     public static ChannelUID generateChannelUID(HmDatapoint dp, ThingUID thingUID) {
79         return new ChannelUID(thingUID, String.valueOf(dp.getChannel().getNumber()), dp.getName());
80     }
81
82     /**
83      * Generates the HmDatapointInfo for the given thing and channelUID.
84      */
85     public static HmDatapointInfo createHmDatapointInfo(ChannelUID channelUID) {
86         return new HmDatapointInfo(channelUID.getThingUID().getId(), HmParamsetType.VALUES,
87                 NumberUtils.toInt(channelUID.getGroupId()), channelUID.getIdWithoutGroup());
88     }
89
90     /**
91      * Returns the address of the Homematic device from the given thing.
92      */
93     public static String getHomematicAddress(Thing thing) {
94         return thing.getUID().getId();
95     }
96 }