]> git.basschouten.com Git - openhab-addons.git/blob
bb0e881b38b7ecc8d174eab91380a82cdb0c620f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.hdanywhere.internal;
14
15 import static org.openhab.binding.hdanywhere.internal.HDanywhereBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
22 import org.openhab.binding.hdanywhere.internal.handler.Mhub4K431Handler;
23 import org.openhab.binding.hdanywhere.internal.handler.MultiroomPlusHandler;
24 import org.openhab.core.config.core.Configuration;
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.binding.BaseThingHandlerFactory;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerFactory;
31 import org.osgi.service.component.annotations.Component;
32
33 /**
34  * The {@link HDanywhereHandlerFactory} is responsible for creating things and
35  * thing handlers.
36  *
37  * @author Karel Goderis - Initial contribution
38  */
39 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.hdanywhere")
40 public class HDanywhereHandlerFactory extends BaseThingHandlerFactory {
41
42     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
43             .unmodifiableSet(Stream.of(THING_TYPE_MULTIROOMPLUS, THING_TYPE_MHUB4K431).collect(Collectors.toSet()));
44
45     @Override
46     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
47         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
48     }
49
50     @Override
51     protected ThingHandler createHandler(Thing thing) {
52         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
53
54         if (thingTypeUID.equals(THING_TYPE_MULTIROOMPLUS)) {
55             return new MultiroomPlusHandler(thing);
56         }
57
58         if (thingTypeUID.equals(THING_TYPE_MHUB4K431)) {
59             return new Mhub4K431Handler(thing);
60         }
61
62         return null;
63     }
64
65     @Override
66     public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
67             ThingUID bridgeUID) {
68         if (HDanywhereBindingConstants.THING_TYPE_MULTIROOMPLUS.equals(thingTypeUID)) {
69             ThingUID matrixUID = getMultiroomPlusUID(thingTypeUID, thingUID, configuration);
70             return super.createThing(thingTypeUID, configuration, matrixUID, null);
71         }
72         if (HDanywhereBindingConstants.THING_TYPE_MHUB4K431.equals(thingTypeUID)) {
73             ThingUID matrixUID = getMhub4K431UID(thingTypeUID, thingUID, configuration);
74             return super.createThing(thingTypeUID, configuration, matrixUID, null);
75         }
76         throw new IllegalArgumentException(
77                 "The thing type " + thingTypeUID + " is not supported by the HDanywhere binding.");
78     }
79
80     private ThingUID getMultiroomPlusUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration) {
81         String ipAddress = (String) configuration.get(MultiroomPlusHandler.IP_ADDRESS);
82
83         if (thingUID == null) {
84             thingUID = new ThingUID(thingTypeUID, ipAddress.replaceAll("[^A-Za-z0-9_]", "_"));
85         }
86         return thingUID;
87     }
88
89     private ThingUID getMhub4K431UID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration) {
90         String ipAddress = (String) configuration.get(Mhub4K431Handler.IP_ADDRESS);
91
92         if (thingUID == null) {
93             thingUID = new ThingUID(thingTypeUID, ipAddress.replaceAll("[^A-Za-z0-9_]", "_"));
94         }
95         return thingUID;
96     }
97 }