|
22 | 22 |
|
23 | 23 | import org.eclipse.jdt.annotation.NonNullByDefault;
|
24 | 24 | import org.eclipse.jdt.annotation.Nullable;
|
25 |
| -import org.openhab.binding.teleinfo.internal.dto.Frame; |
26 |
| -import org.openhab.binding.teleinfo.internal.dto.cbemm.FrameCbemmBaseOption; |
27 |
| -import org.openhab.binding.teleinfo.internal.dto.cbemm.FrameCbemmEjpOption; |
28 |
| -import org.openhab.binding.teleinfo.internal.dto.cbemm.FrameCbemmHcOption; |
29 |
| -import org.openhab.binding.teleinfo.internal.dto.cbemm.FrameCbemmTempoOption; |
30 |
| -import org.openhab.binding.teleinfo.internal.dto.cbemm.evoicc.FrameCbemmEvolutionIccBaseOption; |
31 |
| -import org.openhab.binding.teleinfo.internal.dto.cbemm.evoicc.FrameCbemmEvolutionIccEjpOption; |
32 |
| -import org.openhab.binding.teleinfo.internal.dto.cbemm.evoicc.FrameCbemmEvolutionIccHcOption; |
33 |
| -import org.openhab.binding.teleinfo.internal.dto.cbemm.evoicc.FrameCbemmEvolutionIccTempoOption; |
34 |
| -import org.openhab.binding.teleinfo.internal.dto.cbetm.FrameCbetmLongBaseOption; |
35 |
| -import org.openhab.binding.teleinfo.internal.dto.cbetm.FrameCbetmLongEjpOption; |
36 |
| -import org.openhab.binding.teleinfo.internal.dto.cbetm.FrameCbetmLongHcOption; |
37 |
| -import org.openhab.binding.teleinfo.internal.dto.cbetm.FrameCbetmLongTempoOption; |
38 |
| -import org.openhab.binding.teleinfo.internal.dto.common.FrameAdco; |
| 25 | +import org.openhab.binding.teleinfo.internal.data.Frame; |
39 | 26 | import org.openhab.binding.teleinfo.internal.handler.TeleinfoAbstractControllerHandler;
|
40 | 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; |
41 | 30 | import org.openhab.core.config.discovery.AbstractDiscoveryService;
|
42 | 31 | import org.openhab.core.config.discovery.DiscoveryResult;
|
43 | 32 | import org.openhab.core.config.discovery.DiscoveryResultBuilder;
|
@@ -85,6 +74,7 @@ public Set<ThingTypeUID> getSupportedThingTypes() {
|
85 | 74 | return SUPPORTED_THING_TYPES;
|
86 | 75 | }
|
87 | 76 |
|
| 77 | + @Override |
88 | 78 | public void activate() {
|
89 | 79 | TeleinfoAbstractControllerHandler controllerHandlerRef = controllerHandler;
|
90 | 80 | if (controllerHandlerRef != null) {
|
@@ -142,75 +132,56 @@ protected synchronized void stopScan() {
|
142 | 132 | }
|
143 | 133 |
|
144 | 134 | @Override
|
145 |
| - public void onFrameReceived(TeleinfoAbstractControllerHandler controllerHandler, Frame frame) { |
| 135 | + public void onFrameReceived(Frame frame) { |
146 | 136 | detectNewElectricityMeterFromReceivedFrame(frame);
|
147 | 137 | }
|
148 | 138 |
|
149 | 139 | private void detectNewElectricityMeterFromReceivedFrame(final Frame frameSample) {
|
150 | 140 | TeleinfoAbstractControllerHandler controllerHandlerRef = controllerHandler;
|
151 | 141 | if (controllerHandlerRef != null) {
|
152 |
| - logger.debug("New eletricity meter detection from frame {}", frameSample.getId()); |
153 |
| - if (!(frameSample instanceof FrameAdco)) { |
154 |
| - throw new IllegalStateException("Teleinfo frame type not supported: " + frameSample.getClass()); |
| 142 | + logger.debug("New eletricity meter detection from frame {}", frameSample); |
| 143 | + if (frameSample.get(Label.ADCO) == null) { |
| 144 | + throw new IllegalStateException("Missing ADCO key"); |
155 | 145 | }
|
156 |
| - final FrameAdco frameAdco = (FrameAdco) frameSample; |
157 | 146 |
|
158 |
| - ThingUID thingUID = new ThingUID(getThingTypeUID(frameAdco), frameAdco.getAdco(), |
159 |
| - controllerHandlerRef.getThing().getUID().getId()); |
| 147 | + String adco = frameSample.get(Label.ADCO); |
| 148 | + if (adco != null) { |
| 149 | + ThingUID thingUID = new ThingUID(getThingTypeUID(frameSample), adco, |
| 150 | + controllerHandlerRef.getThing().getUID().getId()); |
160 | 151 |
|
161 |
| - final Map<String, Object> properties = getThingProperties(frameAdco); |
162 |
| - final String representationProperty = THING_ELECTRICITY_METER_PROPERTY_ADCO; |
163 |
| - DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties) |
164 |
| - .withLabel("Teleinfo ADCO " + frameAdco.getAdco()).withThingType(getThingTypeUID(frameAdco)) |
165 |
| - .withBridge(controllerHandlerRef.getThing().getUID()) |
166 |
| - .withRepresentationProperty(representationProperty).build(); |
| 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(); |
167 | 158 |
|
168 |
| - thingDiscovered(discoveryResult); |
| 159 | + thingDiscovered(discoveryResult); |
| 160 | + } |
169 | 161 | } else {
|
170 | 162 | logNullControllerHandler();
|
171 | 163 | }
|
172 | 164 | }
|
173 | 165 |
|
174 | 166 | private ThingTypeUID getThingTypeUID(final Frame teleinfoFrame) {
|
175 |
| - if (teleinfoFrame instanceof FrameCbemmHcOption) { |
176 |
| - return THING_HC_CBEMM_ELECTRICITY_METER_TYPE_UID; |
177 |
| - } else if (teleinfoFrame instanceof FrameCbemmBaseOption) { |
178 |
| - return THING_BASE_CBEMM_ELECTRICITY_METER_TYPE_UID; |
179 |
| - } else if (teleinfoFrame instanceof FrameCbemmEjpOption) { |
180 |
| - return THING_EJP_CBEMM_ELECTRICITY_METER_TYPE_UID; |
181 |
| - } else if (teleinfoFrame instanceof FrameCbemmTempoOption) { |
182 |
| - return THING_TEMPO_CBEMM_ELECTRICITY_METER_TYPE_UID; |
183 |
| - } else if (teleinfoFrame instanceof FrameCbemmEvolutionIccHcOption) { |
184 |
| - return THING_HC_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID; |
185 |
| - } else if (teleinfoFrame instanceof FrameCbemmEvolutionIccBaseOption) { |
186 |
| - return THING_BASE_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID; |
187 |
| - } else if (teleinfoFrame instanceof FrameCbemmEvolutionIccEjpOption) { |
188 |
| - return THING_EJP_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID; |
189 |
| - } else if (teleinfoFrame instanceof FrameCbemmEvolutionIccTempoOption) { |
190 |
| - return THING_TEMPO_CBEMM_EVO_ICC_ELECTRICITY_METER_TYPE_UID; |
191 |
| - } else if (teleinfoFrame instanceof FrameCbetmLongHcOption) { |
192 |
| - return THING_HC_CBETM_ELECTRICITY_METER_TYPE_UID; |
193 |
| - } else if (teleinfoFrame instanceof FrameCbetmLongBaseOption) { |
194 |
| - return THING_BASE_CBETM_ELECTRICITY_METER_TYPE_UID; |
195 |
| - } else if (teleinfoFrame instanceof FrameCbetmLongEjpOption) { |
196 |
| - return THING_EJP_CBETM_ELECTRICITY_METER_TYPE_UID; |
197 |
| - } else if (teleinfoFrame instanceof FrameCbetmLongTempoOption) { |
198 |
| - return THING_TEMPO_CBETM_ELECTRICITY_METER_TYPE_UID; |
| 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; |
199 | 175 | } else {
|
200 | 176 | throw new IllegalStateException("Teleinfo frame type not supported: " + teleinfoFrame.getClass());
|
201 | 177 | }
|
202 | 178 | }
|
203 | 179 |
|
204 |
| - private Map<String, Object> getThingProperties(final Frame teleinfoFrame) { |
205 |
| - Map<String, Object> properties = new HashMap<String, Object>(); |
206 |
| - if (teleinfoFrame instanceof FrameAdco) { |
207 |
| - final FrameAdco frameAdco = (FrameAdco) teleinfoFrame; |
208 |
| - properties.put(THING_ELECTRICITY_METER_PROPERTY_ADCO, frameAdco.getAdco()); |
209 |
| - |
210 |
| - return properties; |
211 |
| - } |
| 180 | + private Map<String, Object> getThingProperties(String adco) { |
| 181 | + Map<String, Object> properties = new HashMap<>(); |
| 182 | + properties.put(THING_ELECTRICITY_METER_PROPERTY_ADCO, adco); |
212 | 183 |
|
213 |
| - throw new IllegalStateException("Teleinfo frame type not supported: " + teleinfoFrame.getClass()); |
| 184 | + return properties; |
214 | 185 | }
|
215 | 186 |
|
216 | 187 | @Override
|
|
0 commit comments