2 * Copyright (c) 2010-2023 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.teleinfo.internal;
15 import static org.openhab.binding.teleinfo.internal.TeleinfoBindingConstants.*;
17 import java.util.HashMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.teleinfo.internal.data.Frame;
24 import org.openhab.binding.teleinfo.internal.handler.TeleinfoAbstractControllerHandler;
25 import org.openhab.binding.teleinfo.internal.handler.TeleinfoControllerHandlerListener;
26 import org.openhab.binding.teleinfo.internal.reader.io.serialport.InvalidFrameException;
27 import org.openhab.binding.teleinfo.internal.reader.io.serialport.Label;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.config.discovery.DiscoveryService;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerService;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link TeleinfoDiscoveryService} class is the service to discover a skeleton for controller handlers.
42 * @author Nicolas SIBERIL - Initial contribution
45 public class TeleinfoDiscoveryService extends AbstractDiscoveryService
46 implements TeleinfoControllerHandlerListener, ThingHandlerService, DiscoveryService {
48 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_HC_CBEMM_ELECTRICITY_METER_TYPE_UID,
49 THING_BASE_CBEMM_ELECTRICITY_METER_TYPE_UID, THING_TEMPO_CBEMM_ELECTRICITY_METER_TYPE_UID,
50 THING_EJP_CBEMM_ELECTRICITY_METER_TYPE_UID, THING_HC_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID,
51 THING_BASE_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID, THING_TEMPO_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID,
52 THING_EJP_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID, THING_HC_CBETM_ELECTRICITY_METER_TYPE_UID,
53 THING_BASE_CBETM_ELECTRICITY_METER_TYPE_UID, THING_TEMPO_CBETM_ELECTRICITY_METER_TYPE_UID,
54 THING_EJP_CBETM_ELECTRICITY_METER_TYPE_UID, THING_LSMT_PROD_ELECTRICITY_METER_TYPE_UID,
55 THING_LSMT_ELECTRICITY_METER_TYPE_UID, THING_LSMM_PROD_ELECTRICITY_METER_TYPE_UID,
56 THING_LSMM_ELECTRICITY_METER_TYPE_UID);
58 private static final int SCAN_DURATION_IN_S = 60;
60 private final Logger logger = LoggerFactory.getLogger(TeleinfoDiscoveryService.class);
61 private @Nullable TeleinfoAbstractControllerHandler controllerHandler;
63 public TeleinfoDiscoveryService() {
64 super(SCAN_DURATION_IN_S);
67 public TeleinfoDiscoveryService(TeleinfoAbstractControllerHandler controllerHandler) {
68 super(SCAN_DURATION_IN_S);
69 this.controllerHandler = controllerHandler;
73 public Set<ThingTypeUID> getSupportedThingTypes() {
74 return SUPPORTED_THING_TYPES;
78 public void activate() {
79 TeleinfoAbstractControllerHandler controllerHandlerRef = controllerHandler;
80 if (controllerHandlerRef != null) {
81 logger.debug("Teleinfo discovery: Activate {}", controllerHandlerRef.getThing().getUID());
83 logNullControllerHandler();
88 public void deactivate() {
89 TeleinfoAbstractControllerHandler controllerHandlerRef = controllerHandler;
90 if (controllerHandlerRef != null) {
91 logger.debug("Teleinfo discovery: Deactivate {}", controllerHandlerRef.getThing().getUID());
93 logNullControllerHandler();
98 protected void startScan() {
99 TeleinfoAbstractControllerHandler controllerHandlerRef = controllerHandler;
100 if (controllerHandlerRef != null) {
101 logger.debug("Teleinfo discovery: Start {}", controllerHandlerRef.getThing().getUID());
103 // Start the search for new devices
104 controllerHandlerRef.addListener(this);
106 logNullControllerHandler();
111 public synchronized void abortScan() {
112 TeleinfoAbstractControllerHandler controllerHandlerRef = controllerHandler;
113 if (controllerHandlerRef != null) {
114 logger.debug("Teleinfo discovery: Abort {}", controllerHandlerRef.getThing().getUID());
115 controllerHandlerRef.removeListener(this);
118 logNullControllerHandler();
123 protected synchronized void stopScan() {
124 TeleinfoAbstractControllerHandler controllerHandlerRef = controllerHandler;
125 if (controllerHandlerRef != null) {
126 logger.debug("Teleinfo discovery: Stop {}", controllerHandlerRef.getThing().getUID());
127 controllerHandlerRef.removeListener(this);
130 logNullControllerHandler();
135 public void onFrameReceived(Frame frame) {
136 detectNewElectricityMeterFromReceivedFrame(frame);
139 private void detectNewElectricityMeterFromReceivedFrame(final Frame frameSample) {
140 TeleinfoAbstractControllerHandler controllerHandlerRef = controllerHandler;
141 if (controllerHandlerRef != null) {
142 logger.debug("New eletricity meter detection from frame {}", frameSample);
143 if (frameSample.get(Label.ADCO) == null && frameSample.get(Label.ADSC) == null) {
144 throw new IllegalStateException("Missing ADCO or ADSC key");
147 String adco = frameSample.get(Label.ADCO) != null ? frameSample.get(Label.ADCO)
148 : frameSample.get(Label.ADSC);
150 ThingUID thingUID = new ThingUID(getThingTypeUID(frameSample), adco,
151 controllerHandlerRef.getThing().getUID().getId());
153 final Map<String, Object> properties = getThingProperties(adco);
154 final String representationProperty = THING_ELECTRICITY_METER_PROPERTY_ADCO;
155 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
156 .withLabel("Teleinfo ADCO/ADSC " + adco).withThingType(getThingTypeUID(frameSample))
157 .withBridge(controllerHandlerRef.getThing().getUID())
158 .withRepresentationProperty(representationProperty).build();
160 thingDiscovered(discoveryResult);
163 logNullControllerHandler();
167 private ThingTypeUID getThingTypeUID(final Frame teleinfoFrame) {
168 ThingTypeUID thingTypeUID;
170 thingTypeUID = teleinfoFrame.getType().getThingTypeUid();
171 } catch (InvalidFrameException e) {
172 throw new IllegalStateException("Frame type can not be evaluated");
174 if (thingTypeUID != null) {
177 throw new IllegalStateException("Teleinfo frame type not supported: " + teleinfoFrame.getClass());
181 private Map<String, Object> getThingProperties(String adco) {
182 Map<String, Object> properties = new HashMap<>();
183 properties.put(THING_ELECTRICITY_METER_PROPERTY_ADCO, adco);
189 public void setThingHandler(@Nullable ThingHandler handler) {
190 if (handler instanceof TeleinfoAbstractControllerHandler teleinfoAbstractControllerHandler) {
191 controllerHandler = teleinfoAbstractControllerHandler;
196 public @Nullable ThingHandler getThingHandler() {
197 return controllerHandler;
200 private void logNullControllerHandler() {
201 logger.warn("Null controller handler");