2 * Copyright (c) 2010-2020 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.miio.internal.handler;
15 import static org.openhab.binding.miio.internal.MiIoBindingConstants.*;
17 import java.awt.Color;
18 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
24 import java.util.concurrent.TimeUnit;
26 import javax.measure.Unit;
27 import javax.measure.format.ParserException;
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.openhab.binding.miio.internal.MiIoBindingConfiguration;
32 import org.openhab.binding.miio.internal.MiIoCommand;
33 import org.openhab.binding.miio.internal.MiIoQuantiyTypes;
34 import org.openhab.binding.miio.internal.MiIoSendCommand;
35 import org.openhab.binding.miio.internal.Utils;
36 import org.openhab.binding.miio.internal.basic.ActionConditions;
37 import org.openhab.binding.miio.internal.basic.CommandParameterType;
38 import org.openhab.binding.miio.internal.basic.Conversions;
39 import org.openhab.binding.miio.internal.basic.MiIoBasicChannel;
40 import org.openhab.binding.miio.internal.basic.MiIoBasicDevice;
41 import org.openhab.binding.miio.internal.basic.MiIoDatabaseWatchService;
42 import org.openhab.binding.miio.internal.basic.MiIoDeviceAction;
43 import org.openhab.binding.miio.internal.basic.MiIoDeviceActionCondition;
44 import org.openhab.binding.miio.internal.cloud.CloudConnector;
45 import org.openhab.binding.miio.internal.transport.MiIoAsyncCommunication;
46 import org.openhab.core.cache.ExpiringCache;
47 import org.openhab.core.library.types.DecimalType;
48 import org.openhab.core.library.types.HSBType;
49 import org.openhab.core.library.types.OnOffType;
50 import org.openhab.core.library.types.PercentType;
51 import org.openhab.core.library.types.QuantityType;
52 import org.openhab.core.library.types.StringType;
53 import org.openhab.core.library.unit.SIUnits;
54 import org.openhab.core.library.unit.SmartHomeUnits;
55 import org.openhab.core.thing.Channel;
56 import org.openhab.core.thing.ChannelUID;
57 import org.openhab.core.thing.Thing;
58 import org.openhab.core.thing.binding.builder.ChannelBuilder;
59 import org.openhab.core.thing.binding.builder.ThingBuilder;
60 import org.openhab.core.thing.type.ChannelTypeRegistry;
61 import org.openhab.core.thing.type.ChannelTypeUID;
62 import org.openhab.core.types.Command;
63 import org.openhab.core.types.RefreshType;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
67 import com.google.gson.Gson;
68 import com.google.gson.GsonBuilder;
69 import com.google.gson.JsonArray;
70 import com.google.gson.JsonElement;
71 import com.google.gson.JsonIOException;
72 import com.google.gson.JsonObject;
73 import com.google.gson.JsonPrimitive;
74 import com.google.gson.JsonSyntaxException;
77 * The {@link MiIoBasicHandler} is responsible for handling commands, which are
78 * sent to one of the channels.
80 * @author Marcel Verpaalen - Initial contribution
83 public class MiIoBasicHandler extends MiIoAbstractHandler {
84 private final Logger logger = LoggerFactory.getLogger(MiIoBasicHandler.class);
85 private boolean hasChannelStructure;
87 private final ExpiringCache<Boolean> updateDataCache = new ExpiringCache<>(CACHE_EXPIRY, () -> {
88 miIoScheduler.schedule(this::updateData, 0, TimeUnit.SECONDS);
92 List<MiIoBasicChannel> refreshList = new ArrayList<>();
93 private Map<String, MiIoBasicChannel> refreshListCustomCommands = new HashMap<>();
95 private @Nullable MiIoBasicDevice miioDevice;
96 private Map<ChannelUID, MiIoBasicChannel> actions = new HashMap<>();
97 private ChannelTypeRegistry channelTypeRegistry;
99 public MiIoBasicHandler(Thing thing, MiIoDatabaseWatchService miIoDatabaseWatchService,
100 CloudConnector cloudConnector, ChannelTypeRegistry channelTypeRegistry) {
101 super(thing, miIoDatabaseWatchService, cloudConnector);
102 this.channelTypeRegistry = channelTypeRegistry;
106 public void initialize() {
108 hasChannelStructure = false;
109 isIdentified = false;
110 refreshList = new ArrayList<>();
111 refreshListCustomCommands = new HashMap<>();
115 public void handleCommand(ChannelUID channelUID, Command receivedCommand) {
116 Command command = receivedCommand;
117 if (command == RefreshType.REFRESH) {
118 if (updateDataCache.isExpired()) {
119 logger.debug("Refreshing {}", channelUID);
120 updateDataCache.getValue();
122 logger.debug("Refresh {} skipped. Already refreshing", channelUID);
126 if (channelUID.getId().equals(CHANNEL_COMMAND)) {
127 cmds.put(sendCommand(command.toString()), command.toString());
130 logger.debug("Locating action for {} channel '{}': '{}'", getThing().getUID(), channelUID.getId(), command);
131 if (!actions.isEmpty()) {
132 MiIoBasicChannel miIoBasicChannel = actions.get(channelUID);
133 if (miIoBasicChannel != null) {
135 for (MiIoDeviceAction action : miIoBasicChannel.getActions()) {
137 JsonElement value = null;
138 JsonArray parameters = action.getParameters().deepCopy();
139 for (int i = 0; i < action.getParameters().size(); i++) {
140 JsonElement p = action.getParameters().get(i);
141 if (p.isJsonPrimitive() && p.getAsString().toLowerCase().contains("$value$")) {
146 String cmd = action.getCommand();
147 CommandParameterType paramType = action.getparameterType();
148 if (command instanceof QuantityType) {
149 QuantityType<?> qtc = null;
151 if (!miIoBasicChannel.getUnit().isBlank()) {
152 Unit<?> unit = MiIoQuantiyTypes.get(miIoBasicChannel.getUnit());
154 qtc = ((QuantityType<?>) command).toUnit(unit);
157 } catch (ParserException e) {
161 command = new DecimalType(qtc.toBigDecimal());
163 logger.debug("Could not convert QuantityType to '{}'", miIoBasicChannel.getUnit());
164 command = new DecimalType(((QuantityType<?>) command).toBigDecimal());
167 if (paramType == CommandParameterType.COLOR) {
168 if (command instanceof HSBType) {
169 HSBType hsb = (HSBType) command;
170 Color color = Color.getHSBColor(hsb.getHue().floatValue() / 360,
171 hsb.getSaturation().floatValue() / 100, hsb.getBrightness().floatValue() / 100);
172 value = new JsonPrimitive(
173 (color.getRed() << 16) + (color.getGreen() << 8) + color.getBlue());
174 } else if (command instanceof DecimalType) {
175 // actually brightness is being set instead of a color
176 value = new JsonPrimitive(((DecimalType) command).toBigDecimal());
177 } else if (command instanceof OnOffType) {
178 value = new JsonPrimitive(command == OnOffType.ON ? 100 : 0);
180 logger.debug("Unsupported command for COLOR: {}", command);
182 } else if (command instanceof OnOffType) {
183 if (paramType == CommandParameterType.ONOFF) {
184 value = new JsonPrimitive(command == OnOffType.ON ? "on" : "off");
185 } else if (paramType == CommandParameterType.ONOFFPARA) {
186 cmd = cmd.replace("*", command == OnOffType.ON ? "on" : "off");
187 value = new JsonArray();
188 } else if (paramType == CommandParameterType.ONOFFBOOL) {
189 boolean boolCommand = command == OnOffType.ON;
190 value = new JsonPrimitive(boolCommand);
191 } else if (paramType == CommandParameterType.ONOFFBOOLSTRING) {
192 value = new JsonPrimitive(command == OnOffType.ON ? "true" : "false");
194 } else if (command instanceof DecimalType) {
195 value = new JsonPrimitive(((DecimalType) command).toBigDecimal());
196 } else if (command instanceof StringType) {
197 if (paramType == CommandParameterType.STRING) {
198 value = new JsonPrimitive(command.toString().toLowerCase());
199 } else if (paramType == CommandParameterType.CUSTOMSTRING) {
200 value = new JsonPrimitive(parameters.get(valuePos).getAsString().replace("$value",
201 command.toString().toLowerCase()));
204 value = new JsonPrimitive(command.toString().toLowerCase());
206 if (paramType == CommandParameterType.EMPTY) {
207 value = new JsonArray();
209 final MiIoDeviceActionCondition miIoDeviceActionCondition = action.getCondition();
210 if (miIoDeviceActionCondition != null) {
211 value = ActionConditions.executeAction(miIoDeviceActionCondition, deviceVariables, value,
214 // Check for miot channel
216 if (action.isMiOtAction()) {
217 value = miotActionTransform(action, miIoBasicChannel, value);
218 } else if (miIoBasicChannel.isMiOt()) {
219 value = miotTransform(miIoBasicChannel, value);
222 if (paramType != CommandParameterType.NONE && paramType != CommandParameterType.ONOFFPARA
224 if (parameters.size() > 0) {
225 parameters.set(valuePos, value);
227 parameters.add(value);
230 if (action.isMiOtAction() && parameters.size() > 0 && parameters.get(0).isJsonObject()) {
231 // hack as unlike any other commands miot actions parameters appear to be send as a json object
232 // instead of a json array
233 cmd = cmd + parameters.get(0).getAsJsonObject().toString();
235 cmd = cmd + parameters.toString();
238 logger.debug("Sending command {}", cmd);
241 if (miIoDeviceActionCondition != null) {
242 logger.debug("Conditional command {} not send, condition '{}' not met", cmd,
243 miIoDeviceActionCondition.getName());
245 logger.debug("Command not send. Value null");
250 logger.debug("Channel Id {} not in mapping.", channelUID.getId());
251 if (logger.isTraceEnabled()) {
252 for (ChannelUID a : actions.keySet()) {
253 logger.trace("Available entries: {} : {}", a, actions.get(a).getFriendlyName());
257 updateDataCache.invalidateValue();
258 miIoScheduler.schedule(() -> {
260 }, 3000, TimeUnit.MILLISECONDS);
262 logger.debug("Actions not loaded yet");
266 private @Nullable JsonElement miotTransform(MiIoBasicChannel miIoBasicChannel, @Nullable JsonElement value) {
267 JsonObject json = new JsonObject();
268 json.addProperty("did", miIoBasicChannel.getChannel());
269 json.addProperty("siid", miIoBasicChannel.getSiid());
270 json.addProperty("piid", miIoBasicChannel.getPiid());
271 json.add("value", value);
275 private @Nullable JsonElement miotActionTransform(MiIoDeviceAction action, MiIoBasicChannel miIoBasicChannel,
276 @Nullable JsonElement value) {
277 JsonObject json = new JsonObject();
278 json.addProperty("did", miIoBasicChannel.getChannel());
279 json.addProperty("siid", action.getSiid());
280 json.addProperty("aiid", action.getAiid());
282 json.add("in", value);
288 protected synchronized void updateData() {
289 logger.debug("Periodic update for '{}' ({})", getThing().getUID().toString(), getThing().getThingTypeUID());
290 final MiIoAsyncCommunication miioCom = getConnection();
292 if (!hasConnection() || skipUpdate() || miioCom == null) {
295 checkChannelStructure();
297 sendCommand(MiIoCommand.MIIO_INFO);
299 final MiIoBasicDevice midevice = miioDevice;
300 if (midevice != null) {
301 refreshProperties(midevice);
302 refreshCustomProperties(midevice);
305 } catch (Exception e) {
306 logger.debug("Error while updating '{}': ", getThing().getUID().toString(), e);
310 private void refreshCustomProperties(MiIoBasicDevice midevice) {
311 for (MiIoBasicChannel miChannel : refreshListCustomCommands.values()) {
312 sendCommand(miChannel.getChannelCustomRefreshCommand());
316 private boolean refreshProperties(MiIoBasicDevice device) {
317 MiIoCommand command = MiIoCommand.getCommand(device.getDevice().getPropertyMethod());
318 int maxProperties = device.getDevice().getMaxProperties();
319 JsonArray getPropString = new JsonArray();
320 for (MiIoBasicChannel miChannel : refreshList) {
321 if (!isLinked(miChannel.getChannel())) {
322 logger.debug("Skip refresh of channel {} for {} as it is not linked", miChannel.getChannel(),
323 getThing().getUID());
326 JsonElement property;
327 if (miChannel.isMiOt()) {
328 JsonObject json = new JsonObject();
329 json.addProperty("did", miChannel.getProperty());
330 json.addProperty("siid", miChannel.getSiid());
331 json.addProperty("piid", miChannel.getPiid());
334 property = new JsonPrimitive(miChannel.getProperty());
336 getPropString.add(property);
337 if (getPropString.size() >= maxProperties) {
338 sendRefreshProperties(command, getPropString);
339 getPropString = new JsonArray();
342 if (getPropString.size() > 0) {
343 sendRefreshProperties(command, getPropString);
348 private void sendRefreshProperties(MiIoCommand command, JsonArray getPropString) {
349 sendCommand(command, getPropString.toString());
353 * Checks if the channel structure has been build already based on the model data. If not build it.
355 private void checkChannelStructure() {
356 final MiIoBindingConfiguration configuration = this.configuration;
357 if (configuration == null) {
360 if (!hasChannelStructure) {
361 if (configuration.model == null || configuration.model.isEmpty()) {
362 logger.debug("Model needs to be determined");
363 isIdentified = false;
365 hasChannelStructure = buildChannelStructure(configuration.model);
368 if (hasChannelStructure) {
369 refreshList = new ArrayList<>();
370 final MiIoBasicDevice miioDevice = this.miioDevice;
371 if (miioDevice != null) {
372 for (MiIoBasicChannel miChannel : miioDevice.getDevice().getChannels()) {
373 if (miChannel.getRefresh()) {
374 if (miChannel.getChannelCustomRefreshCommand().isBlank()) {
375 refreshList.add(miChannel);
377 String i = miChannel.getChannelCustomRefreshCommand().split("\\[")[0];
378 refreshListCustomCommands.put(i.trim(), miChannel);
387 private boolean buildChannelStructure(String deviceName) {
388 logger.debug("Building Channel Structure for {} - Model: {}", getThing().getUID().toString(), deviceName);
389 URL fn = miIoDatabaseWatchService.getDatabaseUrl(deviceName);
391 logger.warn("Database entry for model '{}' cannot be found.", deviceName);
395 JsonObject deviceMapping = Utils.convertFileToJSON(fn);
396 logger.debug("Using device database: {} for device {}", fn.getFile(), deviceName);
397 Gson gson = new GsonBuilder().serializeNulls().create();
398 miioDevice = gson.fromJson(deviceMapping, MiIoBasicDevice.class);
399 for (Channel ch : getThing().getChannels()) {
400 logger.debug("Current thing channels {}, type: {}", ch.getUID(), ch.getChannelTypeUID());
402 ThingBuilder thingBuilder = editThing();
403 int channelsAdded = 0;
405 // make a map of the actions
406 actions = new HashMap<>();
407 final MiIoBasicDevice device = this.miioDevice;
408 if (device != null) {
409 for (MiIoBasicChannel miChannel : device.getDevice().getChannels()) {
410 logger.debug("properties {}", miChannel);
411 if (!miChannel.getType().isEmpty()) {
412 ChannelUID channelUID = addChannel(thingBuilder, miChannel.getChannel(),
413 miChannel.getChannelType(), miChannel.getType(), miChannel.getFriendlyName());
414 if (channelUID != null) {
415 actions.put(channelUID, miChannel);
418 logger.debug("Channel for {} ({}) not loaded", miChannel.getChannel(),
419 miChannel.getFriendlyName());
422 logger.debug("Channel {} ({}), not loaded, missing type", miChannel.getChannel(),
423 miChannel.getFriendlyName());
427 // only update if channels were added/removed
428 if (channelsAdded > 0) {
429 logger.debug("Current thing channels added: {}", channelsAdded);
430 updateThing(thingBuilder.build());
433 } catch (JsonIOException | JsonSyntaxException e) {
434 logger.warn("Error parsing database Json", e);
435 } catch (IOException e) {
436 logger.warn("Error reading database file", e);
437 } catch (Exception e) {
438 logger.warn("Error creating channel structure", e);
443 private @Nullable ChannelUID addChannel(ThingBuilder thingBuilder, @Nullable String channel, String channelType,
444 @Nullable String datatype, String friendlyName) {
445 if (channel == null || channel.isEmpty() || datatype == null || datatype.isEmpty()) {
446 logger.info("Channel '{}', UID '{}' cannot be added incorrectly configured database. ", channel,
447 getThing().getUID());
450 ChannelUID channelUID = new ChannelUID(getThing().getUID(), channel);
452 // TODO: Need to understand if this harms anything. If yes, channel only to be added when not there already.
453 // current way allows to have no issues when channels are changing.
454 if (getThing().getChannel(channel) != null) {
455 logger.info("Channel '{}' for thing {} already exist... removing", channel, getThing().getUID());
456 thingBuilder.withoutChannel(new ChannelUID(getThing().getUID(), channel));
458 ChannelBuilder newChannel = ChannelBuilder.create(channelUID, datatype).withLabel(friendlyName);
459 boolean useGenericChannelType = false;
460 if (!channelType.isBlank()) {
461 ChannelTypeUID channelTypeUID = new ChannelTypeUID(channelType);
462 if (channelTypeRegistry.getChannelType(channelTypeUID) != null) {
463 newChannel = newChannel.withType(channelTypeUID);
465 logger.debug("ChannelType '{}' is not available. Check the Json file for {}", channelTypeUID,
466 getThing().getUID());
467 useGenericChannelType = true;
470 useGenericChannelType = true;
472 if (useGenericChannelType) {
473 newChannel = newChannel.withType(new ChannelTypeUID(BINDING_ID, datatype.toLowerCase()));
475 thingBuilder.withChannel(newChannel.build());
479 private @Nullable MiIoBasicChannel getChannel(String parameter) {
480 for (MiIoBasicChannel refreshEntry : refreshList) {
481 if (refreshEntry.getProperty().equals(parameter)) {
485 logger.trace("Did not find channel for {} in {}", parameter, refreshList);
489 private void updatePropsFromJsonArray(MiIoSendCommand response) {
490 JsonArray res = response.getResult().getAsJsonArray();
491 JsonArray para = parser.parse(response.getCommandString()).getAsJsonObject().get("params").getAsJsonArray();
492 if (res.size() != para.size()) {
493 logger.debug("Unexpected size different. Request size {}, response size {}. (Req: {}, Resp:{})",
494 para.size(), res.size(), para, res);
497 for (int i = 0; i < para.size(); i++) {
498 // This is a miot parameter
500 final JsonElement paraElement = para.get(i);
501 if (paraElement.isJsonObject()) { // miot channel
502 param = paraElement.getAsJsonObject().get("did").getAsString();
504 param = paraElement.getAsString();
506 JsonElement val = res.get(i);
507 if (val.isJsonNull()) {
508 logger.debug("Property '{}' returned null (is it supported?).", param);
510 } else if (val.isJsonObject()) { // miot channel
511 val = val.getAsJsonObject().get("value");
513 MiIoBasicChannel basicChannel = getChannel(param);
514 updateChannel(basicChannel, param, val);
518 private void updatePropsFromJsonObject(MiIoSendCommand response) {
519 JsonObject res = response.getResult().getAsJsonObject();
520 for (Object k : res.keySet()) {
521 String param = (String) k;
522 JsonElement val = res.get(param);
523 if (val.isJsonNull()) {
524 logger.debug("Property '{}' returned null (is it supported?).", param);
527 MiIoBasicChannel basicChannel = getChannel(param);
528 updateChannel(basicChannel, param, val);
532 private void updateChannel(@Nullable MiIoBasicChannel basicChannel, String param, JsonElement value) {
533 JsonElement val = value;
534 if (basicChannel == null) {
535 logger.debug("Channel not found for {}", param);
538 final String transformation = basicChannel.getTransfortmation();
539 if (transformation != null) {
540 JsonElement transformed = Conversions.execute(transformation, val);
541 logger.debug("Transformed with '{}': {} {} -> {} ", transformation, basicChannel.getFriendlyName(), val,
546 String[] chType = basicChannel.getType().toLowerCase().split(":");
549 quantityTypeUpdate(basicChannel, val, chType.length > 1 ? chType[1] : "");
552 updateState(basicChannel.getChannel(), new PercentType(val.getAsBigDecimal()));
555 updateState(basicChannel.getChannel(), new StringType(val.getAsString()));
558 updateState(basicChannel.getChannel(), val.getAsString().toLowerCase().equals("on")
559 || val.getAsString().toLowerCase().equals("true") ? OnOffType.ON : OnOffType.OFF);
562 Color rgb = new Color(val.getAsInt());
563 HSBType hsb = HSBType.fromRGB(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
564 updateState(basicChannel.getChannel(), hsb);
567 logger.debug("No update logic for channeltype '{}' ", basicChannel.getType());
569 } catch (Exception e) {
570 logger.debug("Error updating {} property {} with '{}' : {}: {}", getThing().getUID(),
571 basicChannel.getChannel(), val, e.getClass().getCanonicalName(), e.getMessage());
572 logger.trace("Property update error detail:", e);
576 private void quantityTypeUpdate(MiIoBasicChannel basicChannel, JsonElement val, String type) {
577 if (!basicChannel.getUnit().isBlank()) {
578 Unit<?> unit = MiIoQuantiyTypes.get(basicChannel.getUnit());
580 logger.debug("'{}' channel '{}' has unit '{}' with symbol '{}'.", getThing().getUID(),
581 basicChannel.getChannel(), basicChannel.getUnit(), unit);
582 updateState(basicChannel.getChannel(), new QuantityType<>(val.getAsBigDecimal(), unit));
584 logger.debug("Unit '{}' used by '{}' channel '{}' is not found.. using default unit.",
585 getThing().getUID(), basicChannel.getUnit(), basicChannel.getChannel());
588 // if no unit is provided or unit not found use default units, these units have so far been seen for miio
590 switch (type.toLowerCase()) {
592 updateState(basicChannel.getChannel(), new QuantityType<>(val.getAsBigDecimal(), SIUnits.CELSIUS));
594 case "electriccurrent":
595 updateState(basicChannel.getChannel(),
596 new QuantityType<>(val.getAsBigDecimal(), SmartHomeUnits.AMPERE));
599 updateState(basicChannel.getChannel(), new QuantityType<>(val.getAsBigDecimal(), SmartHomeUnits.WATT));
602 updateState(basicChannel.getChannel(), new QuantityType<>(val.getAsBigDecimal(), SmartHomeUnits.HOUR));
605 updateState(basicChannel.getChannel(), new DecimalType(val.getAsBigDecimal()));
610 public void onMessageReceived(MiIoSendCommand response) {
611 super.onMessageReceived(response);
612 if (response.isError()) {
616 switch (response.getCommand()) {
622 if (response.getResult().isJsonArray()) {
623 updatePropsFromJsonArray(response);
624 } else if (response.getResult().isJsonObject()) {
625 updatePropsFromJsonObject(response);
629 if (refreshListCustomCommands.containsKey(response.getMethod())) {
630 logger.debug("Processing custom refresh command response for !{}", response.getMethod());
631 MiIoBasicChannel ch = refreshListCustomCommands.get(response.getMethod());
632 if (response.getResult().isJsonArray()) {
633 JsonArray cmdResponse = response.getResult().getAsJsonArray();
634 final String transformation = ch.getTransfortmation();
635 if (transformation == null || transformation.isBlank()) {
636 updateChannel(ch, ch.getChannel(),
637 cmdResponse.get(0).isJsonPrimitive() ? cmdResponse.get(0)
638 : new JsonPrimitive(cmdResponse.get(0).toString()));
640 updateChannel(ch, ch.getChannel(), cmdResponse);
643 updateChannel(ch, ch.getChannel(), new JsonPrimitive(response.getResult().toString()));
648 } catch (Exception e) {
649 logger.debug("Error while handing message {}", response.getResponse(), e);