]> git.basschouten.com Git - openhab-addons.git/blob
cfc64a3877ea15036313b7d3446215a557f60104
[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
14 package org.openhab.binding.mqtt.espmilighthub.internal.handler;
15
16 import static org.openhab.binding.mqtt.MqttBindingConstants.BINDING_ID;
17 import static org.openhab.binding.mqtt.espmilighthub.internal.EspMilightHubBindingConstants.*;
18
19 import java.math.BigDecimal;
20 import java.math.MathContext;
21 import java.nio.charset.StandardCharsets;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.mqtt.espmilighthub.internal.ConfigOptions;
29 import org.openhab.binding.mqtt.espmilighthub.internal.Helper;
30 import org.openhab.binding.mqtt.handler.AbstractBrokerHandler;
31 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
32 import org.openhab.core.io.transport.mqtt.MqttMessageSubscriber;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.HSBType;
35 import org.openhab.core.library.types.IncreaseDecreaseType;
36 import org.openhab.core.library.types.OnOffType;
37 import org.openhab.core.library.types.PercentType;
38 import org.openhab.core.library.types.StringType;
39 import org.openhab.core.thing.Bridge;
40 import org.openhab.core.thing.ChannelUID;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.ThingRegistry;
43 import org.openhab.core.thing.ThingStatus;
44 import org.openhab.core.thing.ThingStatusDetail;
45 import org.openhab.core.thing.ThingStatusInfo;
46 import org.openhab.core.thing.binding.BaseThingHandler;
47 import org.openhab.core.thing.binding.ThingHandler;
48 import org.openhab.core.types.Command;
49 import org.openhab.core.types.RefreshType;
50 import org.openhab.core.types.State;
51 import org.openhab.core.util.ColorUtil;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 /**
56  * The {@link EspMilightHubHandler} is responsible for handling commands of the globes, which are then
57  * sent to one of the bridges to be sent out by MQTT.
58  *
59  * @author Matthew Skinner - Initial contribution
60  */
61 @NonNullByDefault
62 public class EspMilightHubHandler extends BaseThingHandler implements MqttMessageSubscriber {
63     // these are all constants used in color conversion calcuations.
64     // strings are necessary to prevent floating point loss of precision
65     private static final BigDecimal BIG_DECIMAL_THOUSAND = new BigDecimal(1000);
66     private static final BigDecimal BIG_DECIMAL_MILLION = new BigDecimal(1000000);
67
68     private static final BigDecimal[][] KANG_X_COEFFICIENTS = {
69             { new BigDecimal("-3.0258469"), new BigDecimal("2.1070379"), new BigDecimal("0.2226347"),
70                     new BigDecimal("0.24039") },
71             { new BigDecimal("-0.2661239"), new BigDecimal("-0.234589"), new BigDecimal("0.8776956"),
72                     new BigDecimal("0.179910") } };
73
74     private static final BigDecimal[][] KANG_Y_COEFFICIENTS = {
75             { new BigDecimal("3.0817580"), new BigDecimal("-5.8733867"), new BigDecimal("3.75112997"),
76                     new BigDecimal("-0.37001483") },
77             { new BigDecimal("-0.9549476"), new BigDecimal("-1.37418593"), new BigDecimal("2.09137015"),
78                     new BigDecimal("-0.16748867") },
79             { new BigDecimal("-1.1063814"), new BigDecimal("-1.34811020"), new BigDecimal("2.18555832"),
80                     new BigDecimal("-0.20219683") } };
81
82     private static final BigDecimal BIG_DECIMAL_03320 = new BigDecimal("0.3320");
83     private static final BigDecimal BIG_DECIMAL_01858 = new BigDecimal("0.1858");
84     private static final BigDecimal[] MCCAMY_COEFFICIENTS = { new BigDecimal(437), new BigDecimal(3601),
85             new BigDecimal(6862), new BigDecimal(5517) };
86
87     private static final BigDecimal BIG_DECIMAL_2 = new BigDecimal(2);
88     private static final BigDecimal BIG_DECIMAL_3 = new BigDecimal(3);
89     private static final BigDecimal BIG_DECIMAL_4 = new BigDecimal(4);
90     private static final BigDecimal BIG_DECIMAL_6 = new BigDecimal(6);
91     private static final BigDecimal BIG_DECIMAL_12 = new BigDecimal(12);
92
93     private static final BigDecimal BIG_DECIMAL_0292 = new BigDecimal("0.292");
94     private static final BigDecimal BIG_DECIMAL_024 = new BigDecimal("0.24");
95
96     private static final BigDecimal[] CORM_COEFFICIENTS = { new BigDecimal("-0.00616793"), new BigDecimal("0.0893944"),
97             new BigDecimal("-0.5179722"), new BigDecimal("1.5317403"), new BigDecimal("-2.4243787"),
98             new BigDecimal("1.925865"), new BigDecimal("-0.471106") };
99
100     private static final BigDecimal BIG_DECIMAL_153 = new BigDecimal(153);
101     private static final BigDecimal BIG_DECIMAL_217 = new BigDecimal(217);
102
103     private final Logger logger = LoggerFactory.getLogger(this.getClass());
104     private @Nullable MqttBrokerConnection connection;
105     private ThingRegistry thingRegistry;
106     private String globeType = "";
107     private String bulbMode = "";
108     private String remotesGroupID = "";
109     private String channelPrefix = "";
110     private String fullCommandTopic = "";
111     private String fullStatesTopic = "";
112     private BigDecimal maxColourTemp = BigDecimal.ZERO;
113     private BigDecimal minColourTemp = BigDecimal.ZERO;
114     private BigDecimal savedLevel = BIG_DECIMAL_100;
115     private ConfigOptions config = new ConfigOptions();
116
117     public EspMilightHubHandler(Thing thing, ThingRegistry thingRegistry) {
118         super(thing);
119         this.thingRegistry = thingRegistry;
120     }
121
122     void changeChannel(String channel, State state) {
123         updateState(new ChannelUID(channelPrefix + channel), state);
124         // Remote code of 0 means that all channels need to follow this change.
125         if ("0".equals(remotesGroupID)) {
126             switch (globeType) {
127                 // These two are 8 channel remotes
128                 case "fut091":
129                 case "fut089":
130                     updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "5:" + channel),
131                             state);
132                     updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "6:" + channel),
133                             state);
134                     updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "7:" + channel),
135                             state);
136                     updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "8:" + channel),
137                             state);
138                 default:
139                     updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "1:" + channel),
140                             state);
141                     updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "2:" + channel),
142                             state);
143                     updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "3:" + channel),
144                             state);
145                     updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "4:" + channel),
146                             state);
147             }
148         }
149     }
150
151     private void processIncomingState(String messageJSON) {
152         // Need to handle State and Level at the same time to process level=0 as off//
153         PercentType tempBulbLevel = PercentType.ZERO;
154         String bulbState = Helper.resolveJSON(messageJSON, "\"state\":\"", 3);
155         String bulbLevel = Helper.resolveJSON(messageJSON, "\"level\":", 3);
156         if (!bulbLevel.isEmpty()) {
157             if ("0".equals(bulbLevel) || "OFF".equals(bulbState)) {
158                 changeChannel(CHANNEL_LEVEL, OnOffType.OFF);
159             } else {
160                 tempBulbLevel = new PercentType(Integer.valueOf(bulbLevel));
161                 changeChannel(CHANNEL_LEVEL, tempBulbLevel);
162             }
163         } else if ("ON".equals(bulbState) || "OFF".equals(bulbState)) { // NOTE: Level is missing when this runs
164             changeChannel(CHANNEL_LEVEL, OnOffType.valueOf(bulbState));
165         }
166         bulbMode = Helper.resolveJSON(messageJSON, "\"bulb_mode\":\"", 5);
167         switch (bulbMode) {
168             case "white":
169                 if (hasRGB()) {
170                     changeChannel(CHANNEL_BULB_MODE, new StringType("white"));
171                     changeChannel(CHANNEL_DISCO_MODE, new StringType("None"));
172                 }
173                 String bulbCTempS = Helper.resolveJSON(messageJSON, "\"color_temp\":", 3);
174                 if (!bulbCTempS.isEmpty()) {
175                     var bulbCTemp = Integer.valueOf(bulbCTempS);
176                     changeChannel(CHANNEL_COLOURTEMP, scaleMireds(bulbCTemp));
177                     if (hasRGB()) {
178                         changeChannel(CHANNEL_COLOUR, calculateHSBFromColorTemp(bulbCTemp, tempBulbLevel));
179                     }
180                 }
181                 break;
182             case "color":
183                 changeChannel(CHANNEL_BULB_MODE, new StringType("color"));
184                 changeChannel(CHANNEL_DISCO_MODE, new StringType("None"));
185                 String bulbHue = Helper.resolveJSON(messageJSON, "\"hue\":", 3);
186                 String bulbSaturation = Helper.resolveJSON(messageJSON, "\"saturation\":", 3);
187                 if (bulbHue.isEmpty()) {
188                     logger.warn("Milight MQTT message came in as being a colour mode, but was missing a HUE value.");
189                 } else {
190                     if (bulbSaturation.isEmpty()) {
191                         bulbSaturation = "100";
192                     }
193                     // 360 isn't allowed by OpenHAB
194                     if ("360".equals(bulbHue)) {
195                         bulbHue = "0";
196                     }
197                     var hsb = new HSBType(new DecimalType(Integer.valueOf(bulbHue)),
198                             new PercentType(Integer.valueOf(bulbSaturation)), tempBulbLevel);
199                     changeChannel(CHANNEL_COLOUR, hsb);
200                     if (hasCCT()) {
201                         changeChannel(CHANNEL_COLOURTEMP, scaleMireds(calculateColorTempFromHSB(hsb)));
202                     }
203                 }
204                 break;
205             case "scene":
206                 if (hasRGB()) {
207                     changeChannel(CHANNEL_BULB_MODE, new StringType("scene"));
208                 }
209                 String bulbDiscoMode = Helper.resolveJSON(messageJSON, "\"mode\":", 1);
210                 if (!bulbDiscoMode.isEmpty()) {
211                     changeChannel(CHANNEL_DISCO_MODE, new StringType(bulbDiscoMode));
212                 }
213                 break;
214             case "night":
215                 if (hasRGB()) {
216                     changeChannel(CHANNEL_BULB_MODE, new StringType("night"));
217                     if (config.oneTriggersNightMode) {
218                         changeChannel(CHANNEL_LEVEL, new PercentType("1"));
219                     }
220                 }
221                 break;
222         }
223     }
224
225     private boolean hasCCT() {
226         switch (globeType) {
227             case "rgb_cct":
228             case "cct":
229             case "fut089":
230             case "fut091":
231                 return true;
232             default:
233                 return false;
234         }
235     }
236
237     private boolean hasRGB() {
238         switch (globeType) {
239             case "rgb_cct":
240             case "rgb":
241             case "rgbw":
242             case "fut089":
243                 return true;
244             default:
245                 return false;
246         }
247     }
248
249     /**
250      * Scales mireds to 0-100%
251      */
252     private static PercentType scaleMireds(int mireds) {
253         // range in mireds is 153-370
254         // 100 - (mireds - 153) / (370 - 153) * 100
255         if (mireds >= 370) {
256             return PercentType.HUNDRED;
257         } else if (mireds <= 153) {
258             return PercentType.ZERO;
259         }
260         return new PercentType(BIG_DECIMAL_100.subtract(new BigDecimal(mireds).subtract(BIG_DECIMAL_153)
261                 .divide(BIG_DECIMAL_217, MathContext.DECIMAL128).multiply(BIG_DECIMAL_100)));
262     }
263
264     private static BigDecimal polynomialFit(BigDecimal x, BigDecimal[] coefficients) {
265         var result = BigDecimal.ZERO;
266         var xAccumulator = BigDecimal.ONE;
267         // forms K[4]*x^0 + K[3]*x^1 + K[2]*x^2 + K[1]*x^3 + K[0]*x^4
268         // (or reverse the order of terms for the usual way of writing it in academic papers)
269         for (int i = coefficients.length - 1; i >= 0; i--) {
270             result = result.add(coefficients[i].multiply(xAccumulator));
271             xAccumulator = xAccumulator.multiply(x);
272         }
273         return result;
274     }
275
276     // https://www.jkps.or.kr/journal/download_pdf.php?spage=865&volume=41&number=6 (8) and (9)
277     private static HSBType calculateHSBFromColorTemp(int mireds, PercentType brightness) {
278         var cct = BIG_DECIMAL_MILLION.divide(new BigDecimal(mireds), MathContext.DECIMAL128);
279         var cctInt = cct.intValue();
280
281         BigDecimal[] coefficients;
282         // 1667K to 4000K and 4000K to 25000K; no range checks since our mired range fits within this
283         if (cctInt <= 4000) {
284             coefficients = KANG_X_COEFFICIENTS[1];
285         } else {
286             coefficients = KANG_X_COEFFICIENTS[0];
287         }
288         BigDecimal x = polynomialFit(BIG_DECIMAL_THOUSAND.divide(cct, MathContext.DECIMAL128), coefficients);
289
290         if (cctInt <= 2222) {
291             coefficients = KANG_Y_COEFFICIENTS[2];
292         } else if (cctInt <= 4000) {
293             coefficients = KANG_Y_COEFFICIENTS[1];
294         } else {
295             coefficients = KANG_Y_COEFFICIENTS[0];
296         }
297         BigDecimal y = polynomialFit(x, coefficients);
298         var rawHsb = ColorUtil.xyToHsb(new double[] { x.doubleValue(), y.doubleValue() });
299         return new HSBType(rawHsb.getHue(), rawHsb.getSaturation(), brightness);
300     }
301
302     // https://www.waveformlighting.com/tech/calculate-color-temperature-cct-from-cie-1931-xy-coordinates/
303     private static int calculateColorTempFromHSB(HSBType hsb) {
304         PercentType[] xy = hsb.toXY();
305         var x = xy[0].toBigDecimal().divide(BIG_DECIMAL_100);
306         var y = xy[1].toBigDecimal().divide(BIG_DECIMAL_100);
307         var n = x.subtract(BIG_DECIMAL_03320).divide(BIG_DECIMAL_01858.subtract(y), MathContext.DECIMAL128);
308         BigDecimal cctK = polynomialFit(n, MCCAMY_COEFFICIENTS);
309         return BIG_DECIMAL_MILLION.divide(cctK, MathContext.DECIMAL128).round(new MathContext(0)).intValue();
310     }
311
312     // https://cormusa.org/wp-content/uploads/2018/04/CORM_2011_Calculation_of_CCT_and_Duv_and_Practical_Conversion_Formulae.pdf
313     // page 19
314     private static BigDecimal calculateDuvFromHSB(HSBType hsb) {
315         PercentType[] xy = hsb.toXY();
316         var x = xy[0].toBigDecimal().divide(BIG_DECIMAL_100);
317         var y = xy[1].toBigDecimal().divide(BIG_DECIMAL_100);
318         var u = BIG_DECIMAL_4.multiply(x).divide(
319                 BIG_DECIMAL_2.multiply(x).negate().add(BIG_DECIMAL_12.multiply(y).add(BIG_DECIMAL_3)),
320                 MathContext.DECIMAL128);
321         var v = BIG_DECIMAL_6.multiply(y).divide(
322                 BIG_DECIMAL_2.multiply(x).negate().add(BIG_DECIMAL_12.multiply(y).add(BIG_DECIMAL_3)),
323                 MathContext.DECIMAL128);
324         var Lfp = u.subtract(BIG_DECIMAL_0292).pow(2).add(v.subtract(BIG_DECIMAL_024).pow(2))
325                 .sqrt(MathContext.DECIMAL128);
326         var a = new BigDecimal(
327                 Math.acos(u.subtract(BIG_DECIMAL_0292).divide(Lfp, MathContext.DECIMAL128).doubleValue()));
328         BigDecimal Lbb = polynomialFit(a, CORM_COEFFICIENTS);
329         return Lfp.subtract(Lbb);
330     }
331
332     /*
333      * Used to calculate the colour temp for a globe if you want the light to get warmer as it is dimmed like a
334      * traditional halogen globe
335      */
336     private int autoColourTemp(int brightness) {
337         return minColourTemp.subtract(
338                 minColourTemp.subtract(maxColourTemp).divide(BIG_DECIMAL_100).multiply(new BigDecimal(brightness)))
339                 .intValue();
340     }
341
342     void turnOff() {
343         if (config.powerFailsToMinimum) {
344             sendMQTT("{\"state\":\"OFF\",\"level\":0}");
345         } else {
346             sendMQTT("{\"state\":\"OFF\"}");
347         }
348     }
349
350     void handleLevelColour(Command command) {
351         int mireds;
352
353         if (command instanceof OnOffType) {
354             if (OnOffType.ON.equals(command)) {
355                 sendMQTT("{\"state\":\"ON\",\"level\":" + savedLevel + "}");
356                 return;
357             } else {
358                 turnOff();
359             }
360         } else if (command instanceof IncreaseDecreaseType) {
361             if (IncreaseDecreaseType.INCREASE.equals(command)) {
362                 if (savedLevel.intValue() <= 90) {
363                     savedLevel = savedLevel.add(BigDecimal.TEN);
364                 }
365             } else {
366                 if (savedLevel.intValue() >= 10) {
367                     savedLevel = savedLevel.subtract(BigDecimal.TEN);
368                 }
369             }
370             sendMQTT("{\"state\":\"ON\",\"level\":" + savedLevel.intValue() + "}");
371             return;
372         } else if (command instanceof HSBType hsb) {
373             // This feature allows google home or Echo to trigger white mode when asked to turn color to white.
374             if (hsb.getHue().intValue() == config.whiteHue && hsb.getSaturation().intValue() == config.whiteSat) {
375                 if (hasCCT()) {
376                     sendMQTT("{\"state\":\"ON\",\"color_temp\":" + config.favouriteWhite + "}");
377                 } else {// globe must only have 1 type of white
378                     sendMQTT("{\"command\":\"set_white\"}");
379                 }
380                 return;
381             } else if (PercentType.ZERO.equals(hsb.getBrightness())) {
382                 turnOff();
383                 return;
384             } else if (config.duvThreshold.compareTo(BigDecimal.ONE) < 0
385                     && calculateDuvFromHSB(hsb).abs().compareTo(config.duvThreshold) <= 0
386                     && (mireds = calculateColorTempFromHSB(hsb)) >= 153 && mireds <= 370) {
387                 sendMQTT("{\"state\":\"ON\",\"level\":" + hsb.getBrightness() + ",\"color_temp\":" + mireds + "}");
388             } else if (config.whiteThreshold != -1 && hsb.getSaturation().intValue() <= config.whiteThreshold) {
389                 sendMQTT("{\"command\":\"set_white\"}");// Can't send the command and level in the same message.
390                 sendMQTT("{\"level\":" + hsb.getBrightness().intValue() + "}");
391             } else {
392                 sendMQTT("{\"state\":\"ON\",\"level\":" + hsb.getBrightness().intValue() + ",\"hue\":"
393                         + hsb.getHue().intValue() + ",\"saturation\":" + hsb.getSaturation().intValue() + "}");
394             }
395             savedLevel = hsb.getBrightness().toBigDecimal();
396             return;
397         } else if (command instanceof PercentType percentType) {
398             if (percentType.intValue() == 0) {
399                 turnOff();
400                 return;
401             } else if (percentType.intValue() == 1 && config.oneTriggersNightMode) {
402                 sendMQTT("{\"command\":\"night_mode\"}");
403                 return;
404             }
405             sendMQTT("{\"state\":\"ON\",\"level\":" + command + "}");
406             savedLevel = percentType.toBigDecimal();
407             if (hasCCT()) {
408                 if (config.dimmedCT > 0 && "white".equals(bulbMode)) {
409                     sendMQTT("{\"state\":\"ON\",\"color_temp\":" + autoColourTemp(savedLevel.intValue()) + "}");
410                 }
411             }
412             return;
413         }
414     }
415
416     @Override
417     public void handleCommand(ChannelUID channelUID, Command command) {
418         if (command instanceof RefreshType) {
419             return;
420         }
421         switch (channelUID.getId()) {
422             case CHANNEL_COLOUR:
423             case CHANNEL_LEVEL:
424                 handleLevelColour(command);
425                 break;
426             case CHANNEL_BULB_MODE:
427                 bulbMode = command.toString();
428                 break;
429             case CHANNEL_COLOURTEMP:
430                 int scaledCommand = (int) Math.round((370 - (2.17 * Float.valueOf(command.toString()))));
431                 sendMQTT("{\"state\":\"ON\",\"level\":" + savedLevel + ",\"color_temp\":" + scaledCommand + "}");
432                 break;
433             case CHANNEL_COMMAND:
434                 sendMQTT("{\"command\":\"" + command + "\"}");
435                 break;
436             case CHANNEL_DISCO_MODE:
437                 sendMQTT("{\"mode\":\"" + command + "\"}");
438                 break;
439         }
440     }
441
442     @Override
443     public void initialize() {
444         config = getConfigAs(ConfigOptions.class);
445         if (config.dimmedCT > 0) {
446             maxColourTemp = new BigDecimal(config.favouriteWhite);
447             minColourTemp = new BigDecimal(config.dimmedCT);
448             if (minColourTemp.intValue() <= maxColourTemp.intValue()) {
449                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
450                         "The dimmedCT config value must be greater than the favourite White value.");
451                 return;
452             }
453         }
454
455         globeType = thing.getThingTypeUID().getId();// eg rgb_cct
456         String globeLocation = this.getThing().getUID().getId();// eg 0x014
457         remotesGroupID = globeLocation.substring(globeLocation.length() - 1, globeLocation.length());// eg 4
458         String remotesIDCode = globeLocation.substring(0, globeLocation.length() - 1);// eg 0x01
459         fullCommandTopic = COMMANDS_BASE_TOPIC + remotesIDCode + "/" + globeType + "/" + remotesGroupID;
460         fullStatesTopic = STATES_BASE_TOPIC + remotesIDCode + "/" + globeType + "/" + remotesGroupID;
461         // Need to remove the lowercase x from 0x12AB in case it contains all numbers
462         String caseCheck = globeLocation.substring(2, globeLocation.length() - 1);
463         if (!caseCheck.equals(caseCheck.toUpperCase())) {
464             logger.warn("The milight globe {}{} is using lowercase for the remote code when the hub needs UPPERCASE",
465                     remotesIDCode, remotesGroupID);
466         }
467         channelPrefix = BINDING_ID + ":" + globeType + ":" + thing.getBridgeUID().getId() + ":" + remotesIDCode
468                 + remotesGroupID + ":";
469         bridgeStatusChanged(getBridgeStatus());
470     }
471
472     private void sendMQTT(String payload) {
473         MqttBrokerConnection localConnection = connection;
474         if (localConnection != null) {
475             localConnection.publish(fullCommandTopic, payload.getBytes(), 1, false);
476         }
477     }
478
479     @Override
480     public void processMessage(String topic, byte[] payload) {
481         String state = new String(payload, StandardCharsets.UTF_8);
482         logger.trace("Received the following new Milight state:{}:{}", topic, state);
483
484         if (topic.equals(STATUS_TOPIC)) {
485             if (state.equals(CONNECTED)) {
486                 updateStatus(ThingStatus.ONLINE);
487             } else {
488                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
489                         "Waiting for 'milight/status: connected' MQTT message to be sent from your ESP Milight hub.");
490             }
491         } else {
492             try {
493                 processIncomingState(state);
494             } catch (Exception e) {
495                 logger.warn("Failed processing Milight state {} for {}", state, topic, e);
496             }
497         }
498     }
499
500     public ThingStatusInfo getBridgeStatus() {
501         Bridge b = getBridge();
502         if (b != null) {
503             return b.getStatusInfo();
504         } else {
505             return new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, null);
506         }
507     }
508
509     @Override
510     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
511         if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
512             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
513             connection = null;
514             return;
515         }
516         if (bridgeStatusInfo.getStatus() != ThingStatus.ONLINE) {
517             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
518             return;
519         }
520
521         Bridge localBridge = this.getBridge();
522         if (localBridge == null) {
523             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED,
524                     "Bridge is missing or offline, you need to setup a working MQTT broker first.");
525             return;
526         }
527         ThingHandler handler = localBridge.getHandler();
528         if (handler instanceof AbstractBrokerHandler abh) {
529             final MqttBrokerConnection connection;
530             try {
531                 connection = abh.getConnectionAsync().get(500, TimeUnit.MILLISECONDS);
532             } catch (InterruptedException | ExecutionException | TimeoutException ignored) {
533                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED,
534                         "Bridge handler has no valid broker connection!");
535                 return;
536             }
537             this.connection = connection;
538             updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING,
539                     "Waiting for 'milight/status: connected' MQTT message to be received. Check hub has 'MQTT Client Status Topic' configured.");
540             connection.subscribe(fullStatesTopic, this);
541             connection.subscribe(STATUS_TOPIC, this);
542         }
543         return;
544     }
545
546     @Override
547     public void dispose() {
548         MqttBrokerConnection localConnection = connection;
549         if (localConnection != null) {
550             localConnection.unsubscribe(fullStatesTopic, this);
551             localConnection.unsubscribe(STATUS_TOPIC, this);
552         }
553     }
554 }