]> git.basschouten.com Git - openhab-addons.git/blob
de5b931ef5574a4fcd4a1313fe739761960c4a0e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.openhab.binding.homematic.internal.model.HmChannel;
18 import org.openhab.binding.homematic.internal.model.HmDatapoint;
19 import org.openhab.binding.homematic.internal.model.HmDatapointInfo;
20 import org.openhab.binding.homematic.internal.model.HmDevice;
21 import org.openhab.binding.homematic.internal.model.HmGatewayInfo;
22 import org.openhab.binding.homematic.internal.model.HmParamsetType;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.type.ChannelGroupTypeUID;
29 import org.openhab.core.thing.type.ChannelTypeUID;
30
31 /**
32  * Utility class for generating some UIDs.
33  *
34  * @author Gerhard Riegler - Initial contribution
35  */
36 public class UidUtils {
37
38     /**
39      * Generates the ThingTypeUID for the given device. If it's a Homegear device, add a prefix because a Homegear
40      * device has more datapoints.
41      */
42     public static ThingTypeUID generateThingTypeUID(HmDevice device) {
43         if (!device.isGatewayExtras() && device.getGatewayId().equals(HmGatewayInfo.ID_HOMEGEAR)) {
44             return new ThingTypeUID(BINDING_ID, String.format("HG-%s", device.getType()));
45         } else {
46             return new ThingTypeUID(BINDING_ID, device.getType());
47         }
48     }
49
50     /**
51      * Generates the ChannelTypeUID for the given datapoint with deviceType, channelNumber and datapointName.
52      */
53     public static ChannelTypeUID generateChannelTypeUID(HmDatapoint dp) {
54         return new ChannelTypeUID(BINDING_ID, String.format("%s_%s_%s", dp.getChannel().getDevice().getType(),
55                 dp.getChannel().getNumber(), dp.getName()));
56     }
57
58     /**
59      * Generates the ChannelTypeUID for the given datapoint with deviceType and channelNumber.
60      */
61     public static ChannelGroupTypeUID generateChannelGroupTypeUID(HmChannel channel) {
62         return new ChannelGroupTypeUID(BINDING_ID,
63                 String.format("%s_%s", channel.getDevice().getType(), channel.getNumber()));
64     }
65
66     /**
67      * Generates the ThingUID for the given device in the given bridge.
68      */
69     public static ThingUID generateThingUID(HmDevice device, Bridge bridge) {
70         ThingTypeUID thingTypeUID = generateThingTypeUID(device);
71         return new ThingUID(thingTypeUID, bridge.getUID(), device.getAddress());
72     }
73
74     /**
75      * Generates the ChannelUID for the given datapoint with channelNumber and datapointName.
76      */
77     public static ChannelUID generateChannelUID(HmDatapoint dp, ThingUID thingUID) {
78         return new ChannelUID(thingUID, String.valueOf(dp.getChannel().getNumber()), dp.getName());
79     }
80
81     /**
82      * Generates the HmDatapointInfo for the given thing and channelUID.
83      */
84     public static HmDatapointInfo createHmDatapointInfo(ChannelUID channelUID) {
85         int value;
86         try {
87             String groupID = channelUID.getGroupId();
88             value = groupID == null ? 0 : Integer.parseInt(groupID);
89         } catch (NumberFormatException e) {
90             value = 0;
91         }
92         return new HmDatapointInfo(channelUID.getThingUID().getId(), HmParamsetType.VALUES, value,
93                 channelUID.getIdWithoutGroup());
94     }
95
96     /**
97      * Returns the address of the Homematic device from the given thing.
98      */
99     public static String getHomematicAddress(Thing thing) {
100         return thing.getUID().getId();
101     }
102 }