]> git.basschouten.com Git - openhab-addons.git/blob
f3ff6c4bcac81163bcb9636cf0100c38d7cf5722
[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;
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 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.teleinfo.internal.data.Frame;
26 import org.openhab.binding.teleinfo.internal.handler.TeleinfoAbstractControllerHandler;
27 import org.openhab.binding.teleinfo.internal.handler.TeleinfoControllerHandlerListener;
28 import org.openhab.binding.teleinfo.internal.reader.io.serialport.InvalidFrameException;
29 import org.openhab.binding.teleinfo.internal.reader.io.serialport.Label;
30 import org.openhab.core.config.discovery.AbstractDiscoveryService;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.config.discovery.DiscoveryService;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerService;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * The {@link TeleinfoDiscoveryService} class is the service to discover a skeleton for controller handlers.
43  *
44  * @author Nicolas SIBERIL - Initial contribution
45  */
46 @NonNullByDefault
47 public class TeleinfoDiscoveryService extends AbstractDiscoveryService
48         implements TeleinfoControllerHandlerListener, ThingHandlerService, DiscoveryService {
49
50     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Stream.of(THING_HC_CBEMM_ELECTRICITY_METER_TYPE_UID,
51             THING_BASE_CBEMM_ELECTRICITY_METER_TYPE_UID, THING_TEMPO_CBEMM_ELECTRICITY_METER_TYPE_UID,
52             THING_EJP_CBEMM_ELECTRICITY_METER_TYPE_UID, THING_HC_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID,
53             THING_BASE_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID, THING_TEMPO_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID,
54             THING_EJP_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID, THING_HC_CBETM_ELECTRICITY_METER_TYPE_UID,
55             THING_BASE_CBETM_ELECTRICITY_METER_TYPE_UID, THING_TEMPO_CBETM_ELECTRICITY_METER_TYPE_UID,
56             THING_EJP_CBETM_ELECTRICITY_METER_TYPE_UID).collect(Collectors.toSet());
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) {
144                 throw new IllegalStateException("Missing ADCO key");
145             }
146
147             String adco = frameSample.get(Label.ADCO);
148             if (adco != null) {
149                 ThingUID thingUID = new ThingUID(getThingTypeUID(frameSample), adco,
150                         controllerHandlerRef.getThing().getUID().getId());
151
152                 final Map<String, Object> properties = getThingProperties(adco);
153                 final String representationProperty = THING_ELECTRICITY_METER_PROPERTY_ADCO;
154                 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
155                         .withLabel("Teleinfo ADCO " + adco).withThingType(getThingTypeUID(frameSample))
156                         .withBridge(controllerHandlerRef.getThing().getUID())
157                         .withRepresentationProperty(representationProperty).build();
158
159                 thingDiscovered(discoveryResult);
160             }
161         } else {
162             logNullControllerHandler();
163         }
164     }
165
166     private ThingTypeUID getThingTypeUID(final Frame teleinfoFrame) {
167         ThingTypeUID thingTypeUID;
168         try {
169             thingTypeUID = teleinfoFrame.getType().getThingTypeUid();
170         } catch (InvalidFrameException e) {
171             throw new IllegalStateException("Frame type can not be evaluated");
172         }
173         if (thingTypeUID != null) {
174             return thingTypeUID;
175         } else {
176             throw new IllegalStateException("Teleinfo frame type not supported: " + teleinfoFrame.getClass());
177         }
178     }
179
180     private Map<String, Object> getThingProperties(String adco) {
181         Map<String, Object> properties = new HashMap<>();
182         properties.put(THING_ELECTRICITY_METER_PROPERTY_ADCO, adco);
183
184         return properties;
185     }
186
187     @Override
188     public void setThingHandler(@Nullable ThingHandler handler) {
189         if (handler instanceof TeleinfoAbstractControllerHandler) {
190             controllerHandler = (TeleinfoAbstractControllerHandler) handler;
191         }
192     }
193
194     @Override
195     public @Nullable ThingHandler getThingHandler() {
196         return controllerHandler;
197     }
198
199     private void logNullControllerHandler() {
200         logger.warn("Null controller handler");
201     }
202 }