]> git.basschouten.com Git - openhab-addons.git/blob
ac8d1529b688935e7810b5681940a4d88881301e
[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.teleinfo.internal;
14
15 import static org.openhab.binding.teleinfo.internal.TeleinfoBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
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;
38
39 /**
40  * The {@link TeleinfoDiscoveryService} class is the service to discover a skeleton for controller handlers.
41  *
42  * @author Nicolas SIBERIL - Initial contribution
43  */
44 @NonNullByDefault
45 public class TeleinfoDiscoveryService extends AbstractDiscoveryService
46         implements TeleinfoControllerHandlerListener, ThingHandlerService, DiscoveryService {
47
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);
57
58     private static final int SCAN_DURATION_IN_S = 60;
59
60     private final Logger logger = LoggerFactory.getLogger(TeleinfoDiscoveryService.class);
61     private @Nullable TeleinfoAbstractControllerHandler controllerHandler;
62
63     public TeleinfoDiscoveryService() {
64         super(SCAN_DURATION_IN_S);
65     }
66
67     public TeleinfoDiscoveryService(TeleinfoAbstractControllerHandler controllerHandler) {
68         super(SCAN_DURATION_IN_S);
69         this.controllerHandler = controllerHandler;
70     }
71
72     @Override
73     public Set<ThingTypeUID> getSupportedThingTypes() {
74         return SUPPORTED_THING_TYPES;
75     }
76
77     @Override
78     public void activate() {
79         TeleinfoAbstractControllerHandler controllerHandlerRef = controllerHandler;
80         if (controllerHandlerRef != null) {
81             logger.debug("Teleinfo discovery: Activate {}", controllerHandlerRef.getThing().getUID());
82         } else {
83             logNullControllerHandler();
84         }
85     }
86
87     @Override
88     public void deactivate() {
89         TeleinfoAbstractControllerHandler controllerHandlerRef = controllerHandler;
90         if (controllerHandlerRef != null) {
91             logger.debug("Teleinfo discovery: Deactivate {}", controllerHandlerRef.getThing().getUID());
92         } else {
93             logNullControllerHandler();
94         }
95     }
96
97     @Override
98     protected void startScan() {
99         TeleinfoAbstractControllerHandler controllerHandlerRef = controllerHandler;
100         if (controllerHandlerRef != null) {
101             logger.debug("Teleinfo discovery: Start {}", controllerHandlerRef.getThing().getUID());
102
103             // Start the search for new devices
104             controllerHandlerRef.addListener(this);
105         } else {
106             logNullControllerHandler();
107         }
108     }
109
110     @Override
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);
116             super.abortScan();
117         } else {
118             logNullControllerHandler();
119         }
120     }
121
122     @Override
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);
128             super.stopScan();
129         } else {
130             logNullControllerHandler();
131         }
132     }
133
134     @Override
135     public void onFrameReceived(Frame frame) {
136         detectNewElectricityMeterFromReceivedFrame(frame);
137     }
138
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");
145             }
146
147             String adco = frameSample.get(Label.ADCO) != null ? frameSample.get(Label.ADCO)
148                     : frameSample.get(Label.ADSC);
149             if (adco != null) {
150                 ThingUID thingUID = new ThingUID(getThingTypeUID(frameSample), adco,
151                         controllerHandlerRef.getThing().getUID().getId());
152
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();
159
160                 thingDiscovered(discoveryResult);
161             }
162         } else {
163             logNullControllerHandler();
164         }
165     }
166
167     private ThingTypeUID getThingTypeUID(final Frame teleinfoFrame) {
168         ThingTypeUID thingTypeUID;
169         try {
170             thingTypeUID = teleinfoFrame.getType().getThingTypeUid();
171         } catch (InvalidFrameException e) {
172             throw new IllegalStateException("Frame type can not be evaluated");
173         }
174         if (thingTypeUID != null) {
175             return thingTypeUID;
176         } else {
177             throw new IllegalStateException("Teleinfo frame type not supported: " + teleinfoFrame.getClass());
178         }
179     }
180
181     private Map<String, Object> getThingProperties(String adco) {
182         Map<String, Object> properties = new HashMap<>();
183         properties.put(THING_ELECTRICITY_METER_PROPERTY_ADCO, adco);
184
185         return properties;
186     }
187
188     @Override
189     public void setThingHandler(@Nullable ThingHandler handler) {
190         if (handler instanceof TeleinfoAbstractControllerHandler teleinfoAbstractControllerHandler) {
191             controllerHandler = teleinfoAbstractControllerHandler;
192         }
193     }
194
195     @Override
196     public @Nullable ThingHandler getThingHandler() {
197         return controllerHandler;
198     }
199
200     private void logNullControllerHandler() {
201         logger.warn("Null controller handler");
202     }
203 }