]> git.basschouten.com Git - openhab-addons.git/blob
bc7755c692ecbb5f86107e2d54f459f39c43739b
[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.teleinfo.internal.handler;
14
15 import static org.openhab.binding.teleinfo.internal.TeleinfoBindingConstants.*;
16
17 import java.util.Set;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.teleinfo.internal.serial.TeleinfoSerialControllerHandler;
24 import org.openhab.core.io.transport.serial.SerialPortManager;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
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.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Reference;
34
35 /**
36  * The {@link TeleinfoThingHandlerFactory} is responsible for creating things and thing
37  * handlers.
38  *
39  * @author Nicolas SIBERIL - Initial contribution
40  */
41 @NonNullByDefault
42 @Component(configurationPid = "binding.teleinfo", service = ThingHandlerFactory.class)
43 public class TeleinfoThingHandlerFactory extends BaseThingHandlerFactory {
44
45     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream.of(THING_TYPE_SERIAL_CONTROLLER,
46             THING_HC_CBEMM_ELECTRICITY_METER_TYPE_UID, THING_BASE_CBEMM_ELECTRICITY_METER_TYPE_UID,
47             THING_TEMPO_CBEMM_ELECTRICITY_METER_TYPE_UID, THING_EJP_CBEMM_ELECTRICITY_METER_TYPE_UID,
48             THING_HC_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID, THING_BASE_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID,
49             THING_TEMPO_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID, THING_EJP_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID,
50             THING_HC_CBETM_ELECTRICITY_METER_TYPE_UID, THING_BASE_CBETM_ELECTRICITY_METER_TYPE_UID,
51             THING_TEMPO_CBETM_ELECTRICITY_METER_TYPE_UID, THING_EJP_CBETM_ELECTRICITY_METER_TYPE_UID)
52             .collect(Collectors.toSet());
53
54     private final SerialPortManager serialPortManager;
55
56     @Activate
57     public TeleinfoThingHandlerFactory(@Reference SerialPortManager serialPortManager) {
58         this.serialPortManager = serialPortManager;
59     }
60
61     @Override
62     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
63         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
64     }
65
66     @Override
67     protected @Nullable ThingHandler createHandler(Thing thing) {
68         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
69
70         if (THING_TYPE_SERIAL_CONTROLLER.equals(thingTypeUID)) {
71             return new TeleinfoSerialControllerHandler((Bridge) thing, serialPortManager);
72         }
73
74         if (SUPPORTED_THING_TYPES_UIDS.contains(thing.getThingTypeUID())) {
75             return new TeleinfoElectricityMeterHandler(thing);
76         } else {
77             throw new IllegalStateException("Teleinfo frame type not supported: " + thing.getThingTypeUID());
78         }
79     }
80 }